blob: 7eb4a259ea8c08eaef2af1847c584017856db542 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgmane3c05852023-11-03 12:21:36 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
Hanno Becker74716312017-10-02 10:00:37 +01007
Paul Bakker5121ce52009-01-03 21:22:43 +00008/*
Simon Butcherbdae02c2016-01-20 00:44:42 +00009 * The following sources were referenced in the design of this implementation
10 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000011 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000012 * [1] A method for obtaining digital signatures and public-key cryptosystems
13 * R Rivest, A Shamir, and L Adleman
14 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
15 *
16 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
17 * Menezes, van Oorschot and Vanstone
18 *
Janos Follathe81102e2017-03-22 13:38:28 +000019 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
20 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
21 * Stefan Mangard
22 * https://arxiv.org/abs/1702.08719v2
23 *
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/rsa.h"
Janos Follathd6b09652023-11-21 09:33:54 +000031#include "bignum_core.h"
Chris Jones66a4cd42021-03-09 16:04:12 +000032#include "rsa_alt_helpers.h"
Tomi Fontanilles573dc232023-12-10 14:57:51 +020033#include "rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/oid.h"
Valerio Settib328c442024-01-23 10:48:45 +010035#include "mbedtls/asn1write.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000037#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020038#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020039#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020040#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000041
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
gufe44c2620da2020-08-03 17:56:50 +020044#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000045#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010049
Valerio Setti201e6432024-02-01 17:19:37 +010050/*
51 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
52 *
53 * The value zero is:
54 * - never a valid value for an RSA parameter
55 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
56 *
57 * Since values can't be omitted in PKCS#1, passing a zero value to
58 * rsa_complete() would be incorrect, so reject zero values early.
59 */
60static int asn1_get_nonzero_mpi(unsigned char **p,
61 const unsigned char *end,
62 mbedtls_mpi *X)
63{
64 int ret;
65
66 ret = mbedtls_asn1_get_mpi(p, end, X);
67 if (ret != 0) {
68 return ret;
69 }
70
71 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
72 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
73 }
74
75 return 0;
76}
77
Valerio Setti135ebde2024-02-01 17:00:29 +010078int mbedtls_rsa_parse_key(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +010079{
80 int ret, version;
81 size_t len;
82 unsigned char *p, *end;
83
84 mbedtls_mpi T;
85 mbedtls_mpi_init(&T);
86
87 p = (unsigned char *) key;
88 end = p + keylen;
89
90 /*
91 * This function parses the RSAPrivateKey (PKCS#1)
92 *
93 * RSAPrivateKey ::= SEQUENCE {
94 * version Version,
95 * modulus INTEGER, -- n
96 * publicExponent INTEGER, -- e
97 * privateExponent INTEGER, -- d
98 * prime1 INTEGER, -- p
99 * prime2 INTEGER, -- q
100 * exponent1 INTEGER, -- d mod (p-1)
101 * exponent2 INTEGER, -- d mod (q-1)
102 * coefficient INTEGER, -- (inverse of q) mod p
103 * otherPrimeInfos OtherPrimeInfos OPTIONAL
104 * }
105 */
106 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
107 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
108 return ret;
109 }
110
Valerio Setti9de84bd2024-02-08 17:40:27 +0100111 if (end != p + len) {
112 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
113 }
Valerio Setti44ff9502024-02-01 16:51:05 +0100114
115 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
116 return ret;
117 }
118
119 if (version != 0) {
120 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
121 }
122
123 /* Import N */
124 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
125 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
126 NULL, NULL)) != 0) {
127 goto cleanup;
128 }
129
130 /* Import E */
131 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
132 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
133 NULL, &T)) != 0) {
134 goto cleanup;
135 }
136
137 /* Import D */
138 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
139 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
140 &T, NULL)) != 0) {
141 goto cleanup;
142 }
143
144 /* Import P */
145 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
146 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
147 NULL, NULL)) != 0) {
148 goto cleanup;
149 }
150
151 /* Import Q */
152 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
153 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
154 NULL, NULL)) != 0) {
155 goto cleanup;
156 }
157
158#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
159 /*
160 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
161 * that they can be easily recomputed from D, P and Q. However by
162 * parsing them from the PKCS1 structure it is possible to avoid
163 * recalculating them which both reduces the overhead of loading
164 * RSA private keys into memory and also avoids side channels which
165 * can arise when computing those values, since all of D, P, and Q
166 * are secret. See https://eprint.iacr.org/2020/055 for a
167 * description of one such attack.
168 */
169
170 /* Import DP */
171 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
172 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
173 goto cleanup;
174 }
175
176 /* Import DQ */
177 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
178 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
179 goto cleanup;
180 }
181
182 /* Import QP */
183 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
184 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
185 goto cleanup;
186 }
187
188#else
189 /* Verify existence of the CRT params */
190 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
191 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
192 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
193 goto cleanup;
194 }
195#endif
196
197 /* rsa_complete() doesn't complete anything with the default
198 * implementation but is still called:
199 * - for the benefit of alternative implementation that may want to
200 * pre-compute stuff beyond what's provided (eg Montgomery factors)
201 * - as is also sanity-checks the key
202 *
203 * Furthermore, we also check the public part for consistency with
204 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
205 */
206 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
207 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
208 goto cleanup;
209 }
210
211 if (p != end) {
212 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
213 }
214
215cleanup:
216
217 mbedtls_mpi_free(&T);
218
219 if (ret != 0) {
220 mbedtls_rsa_free(rsa);
221 }
222
223 return ret;
224}
225
Valerio Setti201e6432024-02-01 17:19:37 +0100226int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +0100227{
Valerio Setti201e6432024-02-01 17:19:37 +0100228 unsigned char *p = (unsigned char *) key;
229 unsigned char *end = (unsigned char *) (key + keylen);
Valerio Setti44ff9502024-02-01 16:51:05 +0100230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
231 size_t len;
232
233 /*
234 * RSAPublicKey ::= SEQUENCE {
235 * modulus INTEGER, -- n
236 * publicExponent INTEGER -- e
237 * }
238 */
239
Valerio Setti201e6432024-02-01 17:19:37 +0100240 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
Valerio Setti44ff9502024-02-01 16:51:05 +0100241 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
242 return ret;
243 }
244
Valerio Setti9de84bd2024-02-08 17:40:27 +0100245 if (end != p + len) {
246 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
247 }
Valerio Settife329ce2024-02-06 08:00:18 +0100248
Valerio Setti44ff9502024-02-01 16:51:05 +0100249 /* Import N */
Valerio Setti201e6432024-02-01 17:19:37 +0100250 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100251 return ret;
252 }
253
Valerio Setti201e6432024-02-01 17:19:37 +0100254 if ((ret = mbedtls_rsa_import_raw(rsa, p, len, NULL, 0, NULL, 0,
Valerio Setti44ff9502024-02-01 16:51:05 +0100255 NULL, 0, NULL, 0)) != 0) {
256 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
257 }
258
Valerio Setti201e6432024-02-01 17:19:37 +0100259 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100260
261 /* Import E */
Valerio Setti201e6432024-02-01 17:19:37 +0100262 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100263 return ret;
264 }
265
266 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
Valerio Setti201e6432024-02-01 17:19:37 +0100267 NULL, 0, p, len)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100268 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
269 }
270
Valerio Setti201e6432024-02-01 17:19:37 +0100271 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100272
273 if (mbedtls_rsa_complete(rsa) != 0 ||
274 mbedtls_rsa_check_pubkey(rsa) != 0) {
275 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
276 }
277
Valerio Setti201e6432024-02-01 17:19:37 +0100278 if (p != end) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100279 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
280 }
281
282 return 0;
283}
284
Valerio Setti135ebde2024-02-01 17:00:29 +0100285int mbedtls_rsa_write_key(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100286 unsigned char **p)
287{
288 size_t len = 0;
289 int ret;
290
291 mbedtls_mpi T; /* Temporary holding the exported parameters */
292
293 /*
294 * Export the parameters one after another to avoid simultaneous copies.
295 */
296
297 mbedtls_mpi_init(&T);
298
299 /* Export QP */
300 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
301 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
302 goto end_of_export;
303 }
304 len += ret;
305
306 /* Export DQ */
307 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
308 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
309 goto end_of_export;
310 }
311 len += ret;
312
313 /* Export DP */
314 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
315 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
316 goto end_of_export;
317 }
318 len += ret;
319
320 /* Export Q */
321 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, &T, NULL, NULL)) != 0 ||
322 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
323 goto end_of_export;
324 }
325 len += ret;
326
327 /* Export P */
328 if ((ret = mbedtls_rsa_export(rsa, NULL, &T, NULL, NULL, NULL)) != 0 ||
329 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
330 goto end_of_export;
331 }
332 len += ret;
333
334 /* Export D */
335 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, &T, NULL)) != 0 ||
336 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
337 goto end_of_export;
338 }
339 len += ret;
340
341 /* Export E */
342 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
343 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
344 goto end_of_export;
345 }
346 len += ret;
347
348 /* Export N */
349 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
350 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
351 goto end_of_export;
352 }
353 len += ret;
354
355end_of_export:
356
357 mbedtls_mpi_free(&T);
358 if (ret < 0) {
359 return ret;
360 }
361
362 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
363 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
364 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
365 MBEDTLS_ASN1_CONSTRUCTED |
366 MBEDTLS_ASN1_SEQUENCE));
367
368 return (int) len;
369}
370
371/*
372 * RSAPublicKey ::= SEQUENCE {
373 * modulus INTEGER, -- n
374 * publicExponent INTEGER -- e
375 * }
376 */
Valerio Setti135ebde2024-02-01 17:00:29 +0100377int mbedtls_rsa_write_pubkey(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100378 unsigned char **p)
379{
380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
381 size_t len = 0;
382 mbedtls_mpi T;
383
384 mbedtls_mpi_init(&T);
385
386 /* Export E */
387 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
388 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
389 goto end_of_export;
390 }
391 len += ret;
392
393 /* Export N */
394 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
395 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
396 goto end_of_export;
397 }
398 len += ret;
399
400end_of_export:
401
402 mbedtls_mpi_free(&T);
403 if (ret < 0) {
404 return ret;
405 }
406
407 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
408 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
409 MBEDTLS_ASN1_SEQUENCE));
410
411 return (int) len;
412}
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100413
414#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
415
416/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
417 * operation (EME-PKCS1-v1_5 decoding).
418 *
419 * \note The return value from this function is a sensitive value
420 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
421 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
422 * is often a situation that an attacker can provoke and leaking which
423 * one is the result is precisely the information the attacker wants.
424 *
425 * \param input The input buffer which is the payload inside PKCS#1v1.5
426 * encryption padding, called the "encoded message EM"
427 * by the terminology.
428 * \param ilen The length of the payload in the \p input buffer.
429 * \param output The buffer for the payload, called "message M" by the
430 * PKCS#1 terminology. This must be a writable buffer of
431 * length \p output_max_len bytes.
432 * \param olen The address at which to store the length of
433 * the payload. This must not be \c NULL.
434 * \param output_max_len The length in bytes of the output buffer \p output.
435 *
436 * \return \c 0 on success.
437 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
438 * The output buffer is too small for the unpadded payload.
439 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
440 * The input doesn't contain properly formatted padding.
441 */
442static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
443 size_t ilen,
444 unsigned char *output,
445 size_t output_max_len,
446 size_t *olen)
447{
448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
449 size_t i, plaintext_max_size;
450
451 /* The following variables take sensitive values: their value must
452 * not leak into the observable behavior of the function other than
453 * the designated outputs (output, olen, return value). Otherwise
454 * this would open the execution of the function to
455 * side-channel-based variants of the Bleichenbacher padding oracle
456 * attack. Potential side channels include overall timing, memory
457 * access patterns (especially visible to an adversary who has access
458 * to a shared memory cache), and branches (especially visible to
459 * an adversary who has access to a shared code cache or to a shared
460 * branch predictor). */
461 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100462 mbedtls_ct_condition_t bad;
463 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100464 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100465 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100466
467 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
468 : output_max_len;
469
470 /* Check and get padding length in constant time and constant
471 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100472 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100473
474
475 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
476 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100477 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100478
479 /* Read the whole buffer. Set pad_done to nonzero if we find
480 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100481 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100482 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100483 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100484 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100485 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100486 }
487
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100488 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100489 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100490
491 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100492 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100493
494 /* If the padding is valid, set plaintext_size to the number of
495 * remaining bytes after stripping the padding. If the padding
496 * is invalid, avoid leaking this fact through the size of the
497 * output: use the maximum message size that fits in the output
498 * buffer. Do it without branches to avoid leaking the padding
499 * validity through timing. RSA keys are small enough that all the
500 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100501 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100502 bad, (unsigned) plaintext_max_size,
503 (unsigned) (ilen - pad_count - 3));
504
505 /* Set output_too_large to 0 if the plaintext fits in the output
506 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100507 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100508 plaintext_max_size);
509
510 /* Set ret without branches to avoid timing attacks. Return:
511 * - INVALID_PADDING if the padding is bad (bad != 0).
512 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
513 * plaintext does not fit in the output buffer.
514 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100515 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100516 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100517 MBEDTLS_ERR_RSA_INVALID_PADDING,
518 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100519 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100520
521 /* If the padding is bad or the plaintext is too large, zero the
522 * data that we're about to copy to the output buffer.
523 * We need to copy the same amount of data
524 * from the same buffer whether the padding is good or not to
525 * avoid leaking the padding validity through overall timing or
526 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100527 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100528
529 /* If the plaintext is too large, truncate it to the buffer size.
530 * Copy anyway to avoid revealing the length through timing, because
531 * revealing the length is as bad as revealing the padding validity
532 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100533 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100534 (unsigned) plaintext_max_size,
535 (unsigned) plaintext_size);
536
537 /* Move the plaintext to the leftmost position where it can start in
538 * the working buffer, i.e. make it start plaintext_max_size from
539 * the end of the buffer. Do this with a memory access trace that
540 * does not depend on the plaintext size. After this move, the
541 * starting location of the plaintext is no longer sensitive
542 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100543 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
544 plaintext_max_size,
545 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100546
547 /* Finally copy the decrypted plaintext plus trailing zeros into the output
548 * buffer. If output_max_len is 0, then output may be an invalid pointer
549 * and the result of memcpy() would be undefined; prevent undefined
550 * behavior making sure to depend only on output_max_len (the size of the
551 * user-provided output buffer), which is independent from plaintext
552 * length, validity of padding, success of the decryption, and other
553 * secrets. */
554 if (output_max_len != 0) {
555 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
556 }
557
558 /* Report the amount of data we copied to the output buffer. In case
559 * of errors (bad padding or output too large), the value of *olen
560 * when this function returns is not specified. Making it equivalent
561 * to the good case limits the risks of leaking the padding validity. */
562 *olen = plaintext_size;
563
564 return ret;
565}
566
567#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
568
Hanno Beckera565f542017-10-11 11:00:19 +0100569#if !defined(MBEDTLS_RSA_ALT)
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
572 const mbedtls_mpi *N,
573 const mbedtls_mpi *P, const mbedtls_mpi *Q,
574 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100575{
Janos Follath24eed8d2019-11-22 13:21:35 +0000576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
579 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
580 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
581 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
582 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
583 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100584 }
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (N != NULL) {
587 ctx->len = mbedtls_mpi_size(&ctx->N);
588 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100591}
592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
594 unsigned char const *N, size_t N_len,
595 unsigned char const *P, size_t P_len,
596 unsigned char const *Q, size_t Q_len,
597 unsigned char const *D, size_t D_len,
598 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100599{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000600 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 if (N != NULL) {
603 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
604 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100605 }
606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if (P != NULL) {
608 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
609 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (Q != NULL) {
612 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
613 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (D != NULL) {
616 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
617 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (E != NULL) {
620 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
621 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100622
623cleanup:
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 if (ret != 0) {
626 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
627 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100630}
631
Hanno Becker705fc682017-10-10 17:57:02 +0100632/*
633 * Checks whether the context fields are set in such a way
634 * that the RSA primitives will be able to execute without error.
635 * It does *not* make guarantees for consistency of the parameters.
636 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100637static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
638 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100639{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100640#if !defined(MBEDTLS_RSA_NO_CRT)
641 /* blinding_needed is only used for NO_CRT to decide whether
642 * P,Q need to be present or not. */
643 ((void) blinding_needed);
644#endif
645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
647 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
648 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000649 }
Hanno Becker705fc682017-10-10 17:57:02 +0100650
651 /*
652 * 1. Modular exponentiation needs positive, odd moduli.
653 */
654
655 /* Modular exponentiation wrt. N is always used for
656 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
658 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
659 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100660 }
661
662#if !defined(MBEDTLS_RSA_NO_CRT)
663 /* Modular exponentiation for P and Q is only
664 * used for private key operations and if CRT
665 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (is_priv &&
667 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
668 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
669 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
670 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
671 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100672 }
673#endif /* !MBEDTLS_RSA_NO_CRT */
674
675 /*
676 * 2. Exponents must be positive
677 */
678
679 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
681 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
682 }
Hanno Becker705fc682017-10-10 17:57:02 +0100683
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100684#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100685 /* For private key operations, use D or DP & DQ
686 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
688 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
689 }
Hanno Becker705fc682017-10-10 17:57:02 +0100690#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (is_priv &&
692 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
693 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
694 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100695 }
696#endif /* MBEDTLS_RSA_NO_CRT */
697
698 /* Blinding shouldn't make exponents negative either,
699 * so check that P, Q >= 1 if that hasn't yet been
700 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100701#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (is_priv && blinding_needed &&
703 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
704 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
705 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100706 }
707#endif
708
709 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100710 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100711#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (is_priv &&
713 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
714 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100715 }
716#endif
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100719}
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100722{
723 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500724 int have_N, have_P, have_Q, have_D, have_E;
725#if !defined(MBEDTLS_RSA_NO_CRT)
726 int have_DP, have_DQ, have_QP;
727#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500728 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
731 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
732 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
733 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
734 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500735
736#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
738 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
739 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500740#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100741
Hanno Becker617c1ae2017-08-23 14:11:24 +0100742 /*
743 * Check whether provided parameters are enough
744 * to deduce all others. The following incomplete
745 * parameter sets for private keys are supported:
746 *
747 * (1) P, Q missing.
748 * (2) D and potentially N missing.
749 *
750 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100751
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500752 n_missing = have_P && have_Q && have_D && have_E;
753 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
754 d_missing = have_P && have_Q && !have_D && have_E;
755 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100756
757 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500758 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (!is_priv && !is_pub) {
761 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
762 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100763
764 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100765 * Step 1: Deduce N if P, Q are provided.
766 */
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (!have_N && have_P && have_Q) {
769 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
770 &ctx->Q)) != 0) {
771 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100772 }
773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100775 }
776
777 /*
778 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100779 */
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (pq_missing) {
782 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
783 &ctx->P, &ctx->Q);
784 if (ret != 0) {
785 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
786 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 } else if (d_missing) {
789 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
790 &ctx->Q,
791 &ctx->E,
792 &ctx->D)) != 0) {
793 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100794 }
795 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100796
Hanno Becker617c1ae2017-08-23 14:11:24 +0100797 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100798 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100799 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100800 */
801
Hanno Becker23344b52017-08-23 07:43:27 +0100802#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (is_priv && !(have_DP && have_DQ && have_QP)) {
804 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
805 &ctx->DP, &ctx->DQ, &ctx->QP);
806 if (ret != 0) {
807 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
808 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100809 }
Hanno Becker23344b52017-08-23 07:43:27 +0100810#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100811
812 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100813 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100814 */
815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100817}
818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
820 unsigned char *N, size_t N_len,
821 unsigned char *P, size_t P_len,
822 unsigned char *Q, size_t Q_len,
823 unsigned char *D, size_t D_len,
824 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100825{
826 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500827 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100828
829 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500830 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
832 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
833 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
834 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
835 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100838 /* If we're trying to export private parameters for a public key,
839 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if (P != NULL || Q != NULL || D != NULL) {
841 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
842 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100843
844 }
845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (N != NULL) {
847 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
848 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (P != NULL) {
851 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
852 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (Q != NULL) {
855 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
856 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (D != NULL) {
859 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
860 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (E != NULL) {
863 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
864 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100865
866cleanup:
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100869}
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
872 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
873 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100874{
Janos Follath24eed8d2019-11-22 13:21:35 +0000875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500876 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100877
878 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500879 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
881 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
882 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
883 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
884 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100887 /* If we're trying to export private parameters for a public key,
888 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (P != NULL || Q != NULL || D != NULL) {
890 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
891 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100892
893 }
894
895 /* Export all requested core parameters. */
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
898 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
899 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
900 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
901 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
902 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100903 }
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100906}
907
908/*
909 * Export CRT parameters
910 * This must also be implemented if CRT is not used, for being able to
911 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
912 * can be used in this case.
913 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100914int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
915 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100916{
Janos Follath24eed8d2019-11-22 13:21:35 +0000917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500918 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100919
920 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500921 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
923 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
924 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
925 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
926 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if (!is_priv) {
929 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
930 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100931
Hanno Beckerdc95c892017-08-23 06:57:02 +0100932#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100933 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
935 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
936 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
937 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100938 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100939#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
941 DP, DQ, QP)) != 0) {
942 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100943 }
944#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100947}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100948
Paul Bakker5121ce52009-01-03 21:22:43 +0000949/*
950 * Initialize an RSA context
951 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000953{
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
Ronald Cronc1905a12021-06-05 11:11:14 +0200956 ctx->padding = MBEDTLS_RSA_PKCS_V15;
957 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100960 /* Set ctx->ver to nonzero to indicate that the mutex has been
961 * initialized and will need to be freed. */
962 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200964#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000965}
966
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100967/*
968 * Set padding for an existing RSA context
969 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
971 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100972{
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200974#if defined(MBEDTLS_PKCS1_V15)
975 case MBEDTLS_RSA_PKCS_V15:
976 break;
977#endif
978
979#if defined(MBEDTLS_PKCS1_V21)
980 case MBEDTLS_RSA_PKCS_V21:
981 break;
982#endif
983 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200985 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200986
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200987#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
989 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200990 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200991 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 return MBEDTLS_ERR_RSA_INVALID_PADDING;
993 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200994 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200995#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500996
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100997 ctx->padding = padding;
998 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +01001001}
1002
Hanno Becker617c1ae2017-08-23 14:11:24 +01001003/*
Yanray Wang83548b52023-03-15 16:46:34 +08001004 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +08001005 */
1006int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
1007{
Yanray Wang644b9012023-03-15 16:50:31 +08001008 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +08001009}
1010
1011/*
Yanray Wang12cb3962023-03-01 10:20:02 +08001012 * Get hash identifier of mbedtls_md_type_t type
1013 */
Yanray Wangd41684e2023-03-17 18:54:22 +08001014int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +08001015{
Yanray Wang644b9012023-03-15 16:50:31 +08001016 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +08001017}
1018
1019/*
Gilles Peskine19f1adf2024-02-01 22:17:44 +01001020 * Get length in bits of RSA modulus
1021 */
1022size_t mbedtls_rsa_get_bitlen(const mbedtls_rsa_context *ctx)
1023{
1024 return mbedtls_mpi_bitlen(&ctx->N);
1025}
1026
1027/*
Hanno Becker617c1ae2017-08-23 14:11:24 +01001028 * Get length in bytes of RSA modulus
1029 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001030size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +01001031{
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +01001033}
1034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
1037/*
1038 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001039 *
1040 * This generation method follows the RSA key pair generation procedure of
1041 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
1044 int (*f_rng)(void *, unsigned char *, size_t),
1045 void *p_rng,
1046 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +00001047{
Janos Follath24eed8d2019-11-22 13:21:35 +00001048 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -08001049 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +01001050 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001051
Janos Follathb8fc1b02018-09-03 15:37:01 +01001052 /*
1053 * If the modulus is 1024 bit long or shorter, then the security strength of
1054 * the RSA algorithm is less than or equal to 80 bits and therefore an error
1055 * rate of 2^-80 is sufficient.
1056 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +01001058 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 }
Janos Follathb8fc1b02018-09-03 15:37:01 +01001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 mbedtls_mpi_init(&H);
1062 mbedtls_mpi_init(&G);
1063 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001064
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001065 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +01001066 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1067 goto cleanup;
1068 }
1069
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001070 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001071 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1072 goto cleanup;
1073 }
1074
1075 /*
1076 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001077 * 1. |P-Q| > 2^( nbits / 2 - 100 )
1078 * 2. GCD( E, (P-1)*(Q-1) ) == 1
1079 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +00001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 do {
1084 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
1085 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
1088 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001089
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001090 /* 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 +01001091 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
1092 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001093 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001096 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (H.s < 0) {
1098 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
1099 }
Janos Follathef441782016-09-21 13:18:12 +01001100
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001101 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
1103 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
1104 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001105
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001106 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
1108 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -08001109 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001111
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001112 /* 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 +01001113 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
1114 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
1115 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 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 -08001118 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001120
1121 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001123
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001124 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
1126 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001131
Jethro Beekman97f95c92018-02-13 15:50:36 -08001132#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 * DP = D mod (P - 1)
1135 * DQ = D mod (Q - 1)
1136 * QP = Q^-1 mod P
1137 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
1139 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001140#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
Hanno Becker83aad1f2017-08-23 06:45:10 +01001142 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
1145cleanup:
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 mbedtls_mpi_free(&H);
1148 mbedtls_mpi_free(&G);
1149 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if (ret != 0) {
1152 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +01001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if ((-ret & ~0x7f) == 0) {
1155 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
1156 }
1157 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 }
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001161}
1162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
1165/*
1166 * Check a public RSA key
1167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001168int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001169{
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
1171 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001172 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
1175 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001176 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
1179 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
1180 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
1181 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1182 }
1183
1184 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001185}
1186
1187/*
Hanno Becker705fc682017-10-10 17:57:02 +01001188 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001191{
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
1193 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
1194 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 }
Paul Bakker48377d92013-08-30 12:06:24 +02001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
1198 &ctx->D, &ctx->E, NULL, NULL) != 0) {
1199 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001201
Hanno Beckerb269a852017-08-25 08:03:21 +01001202#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
1204 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
1205 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +01001206 }
1207#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001210}
1211
1212/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001213 * Check if contexts holding a public and private key match
1214 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001215int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
1216 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001217{
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
1219 mbedtls_rsa_check_privkey(prv) != 0) {
1220 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001221 }
1222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
1224 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
1225 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001226 }
1227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001229}
1230
1231/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 * Do an RSA public key operation
1233 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001234int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
1235 const unsigned char *input,
1236 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001237{
Janos Follath24eed8d2019-11-22 13:21:35 +00001238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001239 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
1243 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1244 }
Hanno Becker705fc682017-10-10 17:57:02 +01001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001248#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1250 return ret;
1251 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001252#endif
1253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001257 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1258 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 }
1260
1261 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
1263 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001264
1265cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1268 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1269 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001270#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 if (ret != 0) {
1275 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
1276 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001279}
1280
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001281/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001282 * Generate or update blinding values, see section 10 of:
1283 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001284 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001285 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1288 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001289{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001290 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001291 mbedtls_mpi R;
1292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001296 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1298 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1299 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1300 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001301
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001302 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001303 }
1304
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001305 /* Unblinding value: Vf = random number, invertible mod N */
1306 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +02001308 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
1309 goto cleanup;
1310 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 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 +02001313
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001314 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
1316 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
1317 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001318
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001319 /* At this point, Vi is invertible mod N if and only if both Vf and R
1320 * are invertible mod N. If one of them isn't, we don't need to know
1321 * which one, we just loop and choose new values for both of them.
1322 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
1324 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001325 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -05001329
1330 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
1332 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001333
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001334 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001335 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 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 +02001337
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001338
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001339cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001343}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001344
Paul Bakker5121ce52009-01-03 21:22:43 +00001345/*
Janos Follathd6b09652023-11-21 09:33:54 +00001346 * Unblind
1347 * T = T * Vf mod N
1348 */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001349static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, const mbedtls_mpi *N)
Janos Follathd6b09652023-11-21 09:33:54 +00001350{
1351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1352 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
1353 const size_t nlimbs = N->n;
1354 const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
1355 mbedtls_mpi RR, M_T;
1356
1357 mbedtls_mpi_init(&RR);
1358 mbedtls_mpi_init(&M_T);
1359
1360 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
1361 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
1362
1363 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
1364 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
1365
Janos Follathe6750b22023-12-27 10:22:59 +00001366 /* T = T * Vf mod N
1367 * Reminder: montmul(A, B, N) = A * B * R^-1 mod N
1368 * Usually both operands are multiplied by R mod N beforehand (by calling
1369 * `to_mont_rep()` on them), yielding a result that's also * R mod N (aka
1370 * "in the Montgomery domain"). Here we only multiply one operand by R mod
1371 * N, so the result is directly what we want - no need to call
1372 * `from_mont_rep()` on it. */
Janos Follathd6b09652023-11-21 09:33:54 +00001373 mbedtls_mpi_core_to_mont_rep(T->p, T->p, N->p, nlimbs, mm, RR.p, M_T.p);
Janos Follathd6b09652023-11-21 09:33:54 +00001374 mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1375
1376cleanup:
1377
1378 mbedtls_mpi_free(&RR);
1379 mbedtls_mpi_free(&M_T);
1380
1381 return ret;
1382}
1383
1384/*
Janos Follathe81102e2017-03-22 13:38:28 +00001385 * Exponent blinding supposed to prevent side-channel attacks using multiple
1386 * traces of measurements to recover the RSA key. The more collisions are there,
1387 * the more bits of the key can be recovered. See [3].
1388 *
1389 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001390 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001391 *
1392 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001393 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001394 *
1395 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1396 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1397 * Thus in this sense with 28 byte blinding the security is not reduced by
1398 * side-channel attacks like the one in [3])
1399 *
1400 * This countermeasure does not help if the key recovery is possible with a
1401 * single trace.
1402 */
1403#define RSA_EXPONENT_BLINDING 28
1404
1405/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 * Do an RSA private key operation
1407 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001408int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1409 int (*f_rng)(void *, unsigned char *, size_t),
1410 void *p_rng,
1411 const unsigned char *input,
1412 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001413{
Janos Follath24eed8d2019-11-22 13:21:35 +00001414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001415 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001416
1417 /* Temporary holding the result */
1418 mbedtls_mpi T;
1419
1420 /* Temporaries holding P-1, Q-1 and the
1421 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001422 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001423
1424#if !defined(MBEDTLS_RSA_NO_CRT)
1425 /* Temporaries holding the results mod p resp. mod q. */
1426 mbedtls_mpi TP, TQ;
1427
1428 /* Temporaries holding the blinded exponents for
1429 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001430 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001431#else
1432 /* Temporary holding the blinded exponent (if used). */
1433 mbedtls_mpi D_blind;
Hanno Becker43f94722017-08-25 11:50:00 +01001434#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001435
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001436 /* Temporaries holding the initial input and the double
1437 * checked result; should be the same in the end. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001438 mbedtls_mpi input_blinded, check_result_blinded;
Paul Bakker5121ce52009-01-03 21:22:43 +00001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (f_rng == NULL) {
1441 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1442 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 if (rsa_check_context(ctx, 1 /* private key checks */,
1445 1 /* blinding on */) != 0) {
1446 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001447 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001448
Hanno Becker06811ce2017-05-03 15:10:34 +01001449#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1451 return ret;
1452 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001453#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001454
Hanno Becker06811ce2017-05-03 15:10:34 +01001455 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 mbedtls_mpi_init(&P1);
1459 mbedtls_mpi_init(&Q1);
1460 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001461
Janos Follathe81102e2017-03-22 13:38:28 +00001462#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001464#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 mbedtls_mpi_init(&DP_blind);
1466 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001467#endif
1468
Hanno Becker06811ce2017-05-03 15:10:34 +01001469#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001471#endif
1472
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001473 mbedtls_mpi_init(&input_blinded);
1474 mbedtls_mpi_init(&check_result_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001475
1476 /* End of MPI initialization */
1477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1479 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001480 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1481 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 }
1483
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001484 /*
1485 * Blinding
1486 * T = T * Vi mod N
1487 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1489 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1490 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001491
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001492 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&input_blinded, &T));
Janos Follath6bcbc922023-11-21 09:46:43 +00001493
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001494 /*
1495 * Exponent blinding
1496 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1498 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001499
Janos Follathf9203b42017-03-22 15:13:15 +00001500#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001501 /*
1502 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1503 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1505 f_rng, p_rng));
1506 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1507 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1508 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathf9203b42017-03-22 15:13:15 +00001509#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001510 /*
1511 * DP_blind = ( P - 1 ) * R + DP
1512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1514 f_rng, p_rng));
1515 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1516 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1517 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001518
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001519 /*
1520 * DQ_blind = ( Q - 1 ) * R + DQ
1521 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1523 f_rng, p_rng));
1524 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1525 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1526 &ctx->DQ));
Janos Follathe81102e2017-03-22 13:38:28 +00001527#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath47ee7702023-12-27 10:33:00 +00001530 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &D_blind, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001531#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001532 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001533 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001535 * TP = input ^ dP mod P
1536 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001537 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001538
Janos Follath47ee7702023-12-27 10:33:00 +00001539 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, &DP_blind, &ctx->P, &ctx->RP));
1540 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, &DQ_blind, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001541
1542 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001543 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1546 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1547 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001548
1549 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001550 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001551 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1553 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001555
Hanno Becker2dec5e82017-10-03 07:49:52 +01001556 /* Verify the result to prevent glitching attacks. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001557 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&check_result_blinded, &T, &ctx->E,
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 &ctx->N, &ctx->RN));
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001559 if (mbedtls_mpi_cmp_mpi(&check_result_blinded, &input_blinded) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001560 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1561 goto cleanup;
1562 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001563
Janos Follath6bcbc922023-11-21 09:46:43 +00001564 /*
1565 * Unblind
1566 * T = T * Vf mod N
1567 */
1568 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1569
Paul Bakker5121ce52009-01-03 21:22:43 +00001570 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001572
1573cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1576 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1577 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001578#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 mbedtls_mpi_free(&P1);
1581 mbedtls_mpi_free(&Q1);
1582 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001583
Janos Follathe81102e2017-03-22 13:38:28 +00001584#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001586#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 mbedtls_mpi_free(&DP_blind);
1588 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001589#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001592
1593#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001595#endif
1596
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001597 mbedtls_mpi_free(&check_result_blinded);
1598 mbedtls_mpi_free(&input_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (ret != 0 && ret >= -0x007f) {
1601 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001605}
1606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001608/**
1609 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1610 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001611 * \param dst buffer to mask
1612 * \param dlen length of destination buffer
1613 * \param src source of the mask generation
1614 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001615 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001616 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001617static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1618 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001619{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001620 unsigned char counter[4];
1621 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001622 unsigned int hlen;
1623 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001624 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001625 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001626 const mbedtls_md_info_t *md_info;
1627 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 mbedtls_md_init(&md_ctx);
1630 md_info = mbedtls_md_info_from_type(md_alg);
1631 if (md_info == NULL) {
1632 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1633 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 mbedtls_md_init(&md_ctx);
1636 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001637 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 memset(mask, 0, sizeof(mask));
1643 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001644
Simon Butcher02037452016-03-01 21:19:12 +00001645 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001646 p = dst;
1647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001649 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001651 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001655 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 }
1657 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001658 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 }
1660 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001661 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 }
1663 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001664 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001668 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001670
1671 counter[3]++;
1672
1673 dlen -= use_len;
1674 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001675
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001676exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001681}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001682
1683/**
1684 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1685 *
1686 * \param hash the input hash
1687 * \param hlen length of the input hash
1688 * \param salt the input salt
1689 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001690 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001691 * \param md_alg message digest to use
1692 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001693static int hash_mprime(const unsigned char *hash, size_t hlen,
1694 const unsigned char *salt, size_t slen,
1695 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001696{
1697 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001698
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001699 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1703 if (md_info == NULL) {
1704 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1705 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 mbedtls_md_init(&md_ctx);
1708 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001709 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 }
1711 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001712 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 }
1714 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001715 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 }
1717 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001718 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 }
1720 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001721 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 }
1723 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001724 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001726
1727exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001731}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001732
1733/**
1734 * Compute a hash.
1735 *
1736 * \param md_alg algorithm to use
1737 * \param input input message to hash
1738 * \param ilen input length
1739 * \param output the output buffer - must be large enough for \p md_alg
1740 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001741static int compute_hash(mbedtls_md_type_t md_alg,
1742 const unsigned char *input, size_t ilen,
1743 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001744{
1745 const mbedtls_md_info_t *md_info;
1746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 md_info = mbedtls_md_info_from_type(md_alg);
1748 if (md_info == NULL) {
1749 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1750 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001757/*
1758 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1759 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001760int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1761 int (*f_rng)(void *, unsigned char *, size_t),
1762 void *p_rng,
1763 const unsigned char *label, size_t label_len,
1764 size_t ilen,
1765 const unsigned char *input,
1766 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001767{
1768 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001770 unsigned char *p = output;
1771 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (f_rng == NULL) {
1774 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1775 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001776
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001777 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 if (hlen == 0) {
1779 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1780 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001781
1782 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001783
Simon Butcher02037452016-03-01 21:19:12 +00001784 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1786 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1787 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001790
1791 *p++ = 0;
1792
Simon Butcher02037452016-03-01 21:19:12 +00001793 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1795 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1796 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001797
1798 p += hlen;
1799
Simon Butcher02037452016-03-01 21:19:12 +00001800 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1802 if (ret != 0) {
1803 return ret;
1804 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001805 p += hlen;
1806 p += olen - 2 * hlen - 2 - ilen;
1807 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 if (ilen != 0) {
1809 memcpy(p, input, ilen);
1810 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001811
Simon Butcher02037452016-03-01 21:19:12 +00001812 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001814 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 return ret;
1816 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001817
Simon Butcher02037452016-03-01 21:19:12 +00001818 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001820 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 return ret;
1822 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001825}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001829/*
1830 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1831 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001832int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1833 int (*f_rng)(void *, unsigned char *, size_t),
1834 void *p_rng, size_t ilen,
1835 const unsigned char *input,
1836 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001837{
1838 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001840 unsigned char *p = output;
1841
Paul Bakkerb3869132013-02-28 17:21:01 +01001842 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001843
Simon Butcher02037452016-03-01 21:19:12 +00001844 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (ilen + 11 < ilen || olen < ilen + 11) {
1846 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1847 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001848
1849 nb_pad = olen - 3 - ilen;
1850
1851 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (f_rng == NULL) {
1854 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1855 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001856
1857 *p++ = MBEDTLS_RSA_CRYPT;
1858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001860 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001861
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001862 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 ret = f_rng(p_rng, p, 1);
1864 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001865
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001866 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 if (rng_dl == 0 || ret != 0) {
1868 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1869 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001870
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001871 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001872 }
1873
1874 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if (ilen != 0) {
1876 memcpy(p, input, ilen);
1877 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001880}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001882
Paul Bakker5121ce52009-01-03 21:22:43 +00001883/*
1884 * Add the message padding, then do an RSA operation
1885 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001886int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1887 int (*f_rng)(void *, unsigned char *, size_t),
1888 void *p_rng,
1889 size_t ilen,
1890 const unsigned char *input,
1891 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001892{
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894#if defined(MBEDTLS_PKCS1_V15)
1895 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1897 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001898#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#if defined(MBEDTLS_PKCS1_V21)
1901 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1903 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001904#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001905
1906 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001908 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001909}
1910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001912/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001913 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001915int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1916 int (*f_rng)(void *, unsigned char *, size_t),
1917 void *p_rng,
1918 const unsigned char *label, size_t label_len,
1919 size_t *olen,
1920 const unsigned char *input,
1921 unsigned char *output,
1922 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001923{
Janos Follath24eed8d2019-11-22 13:21:35 +00001924 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001925 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001926 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001927 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001929 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001930 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001931
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001932 /*
1933 * Parameters sanity checks
1934 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1936 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1937 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
1939 ilen = ctx->len;
1940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 if (ilen < 16 || ilen > sizeof(buf)) {
1942 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1943 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001944
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001945 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (hlen == 0) {
1947 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1948 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001949
Janos Follathc17cda12016-02-11 11:08:18 +00001950 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 if (2 * hlen + 2 > ilen) {
1952 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1953 }
Janos Follathc17cda12016-02-11 11:08:18 +00001954
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001955 /*
1956 * RSA operation
1957 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001961 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001963
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001964 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001965 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001966 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001967 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001969 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 /* DB: Apply dbMask to maskedDB */
1971 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001972 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001973 goto cleanup;
1974 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001975
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001976 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1978 label, label_len, lhash);
1979 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001980 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001982
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001983 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001984 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001985 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 p = buf;
1987
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001988 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001989
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001990 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001991
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001992 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001993 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001994 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001995
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001996 /* Get zero-padding len, but always read till end of buffer
1997 * (minus one, for the 01 byte) */
1998 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001999 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01002001 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
2002 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002003 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002004
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002005 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002006 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01002007
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002008 /*
2009 * The only information "leaked" is whether the padding was correct or not
2010 * (eg, no data is copied if it was not correct). This meets the
2011 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
2012 * the different error conditions.
2013 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002014 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002015 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2016 goto cleanup;
2017 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002018
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002019 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002020 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
2021 goto cleanup;
2022 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002023
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002024 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 if (*olen != 0) {
2026 memcpy(output, p, *olen);
2027 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002028 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002029
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002030cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 mbedtls_platform_zeroize(buf, sizeof(buf));
2032 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002035}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002039/*
2040 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
2041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002042int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
2043 int (*f_rng)(void *, unsigned char *, size_t),
2044 void *p_rng,
2045 size_t *olen,
2046 const unsigned char *input,
2047 unsigned char *output,
2048 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002049{
2050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2051 size_t ilen;
2052 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
2053
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002054 ilen = ctx->len;
2055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2057 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2058 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 if (ilen < 16 || ilen > sizeof(buf)) {
2061 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2062 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002067 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
2071 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002072
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002073cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002077}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002079
2080/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002081 * Do an RSA operation, then remove the message padding
2082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002083int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
2084 int (*f_rng)(void *, unsigned char *, size_t),
2085 void *p_rng,
2086 size_t *olen,
2087 const unsigned char *input,
2088 unsigned char *output,
2089 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01002090{
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092#if defined(MBEDTLS_PKCS1_V15)
2093 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
2095 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02002096#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#if defined(MBEDTLS_PKCS1_V21)
2099 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
2101 olen, input, output,
2102 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01002103#endif
2104
2105 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002107 }
2108}
2109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002111static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2112 int (*f_rng)(void *, unsigned char *, size_t),
2113 void *p_rng,
2114 mbedtls_md_type_t md_alg,
2115 unsigned int hashlen,
2116 const unsigned char *hash,
2117 int saltlen,
2118 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002119{
2120 size_t olen;
2121 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002122 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002123 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00002124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002125 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002126 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002129 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (f_rng == NULL) {
2133 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2134 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002135
2136 olen = ctx->len;
2137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002139 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002140 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (exp_hashlen == 0) {
2142 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2143 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (hashlen != exp_hashlen) {
2146 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2147 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002148 }
2149
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002150 hash_id = (mbedtls_md_type_t) ctx->hash_id;
2151 if (hash_id == MBEDTLS_MD_NONE) {
2152 hash_id = md_alg;
2153 }
2154 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (hlen == 0) {
2156 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2157 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
2160 /* Calculate the largest possible salt length, up to the hash size.
2161 * Normally this is the hash length, which is the maximum salt length
2162 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
2163 * enough room, use the maximum salt length that fits. The constraint is
2164 * that the hash length plus the salt length plus 2 bytes must be at most
2165 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
2166 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002167 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if (olen < hlen + min_slen + 2) {
2169 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2170 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002171 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002173 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 }
2175 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
2176 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2177 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02002178 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002179 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01002182
Simon Butcher02037452016-03-01 21:19:12 +00002183 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002185 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01002186 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002187
2188 /* Generate salt of length slen in place in the encoded message */
2189 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
2191 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
2192 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02002193
Paul Bakkerb3869132013-02-28 17:21:01 +01002194 p += slen;
2195
Simon Butcher02037452016-03-01 21:19:12 +00002196 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002197 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (ret != 0) {
2199 return ret;
2200 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002201
Simon Butcher02037452016-03-01 21:19:12 +00002202 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002204 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002206
Simon Butcher02037452016-03-01 21:19:12 +00002207 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002208 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 if (ret != 0) {
2210 return ret;
2211 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2214 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01002215
2216 p += hlen;
2217 *p++ = 0xBC;
2218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002220}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002221
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002222static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2223 int (*f_rng)(void *, unsigned char *, size_t),
2224 void *p_rng,
2225 mbedtls_md_type_t md_alg,
2226 unsigned int hashlen,
2227 const unsigned char *hash,
2228 int saltlen,
2229 unsigned char *sig)
2230{
2231 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2232 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2233 }
Valerio Settie700d802024-02-26 13:52:34 +01002234 if ((ctx->hash_id == MBEDTLS_MD_NONE) && (md_alg == MBEDTLS_MD_NONE)) {
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002235 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2236 }
2237 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
2238 sig);
2239}
2240
2241int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2242 int (*f_rng)(void *, unsigned char *, size_t),
2243 void *p_rng,
2244 mbedtls_md_type_t md_alg,
2245 unsigned int hashlen,
2246 const unsigned char *hash,
2247 unsigned char *sig)
2248{
2249 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
2250 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2251}
2252
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002253/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02002254 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
2255 * the option to pass in the salt length.
2256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002257int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
2258 int (*f_rng)(void *, unsigned char *, size_t),
2259 void *p_rng,
2260 mbedtls_md_type_t md_alg,
2261 unsigned int hashlen,
2262 const unsigned char *hash,
2263 int saltlen,
2264 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02002265{
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2267 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02002268}
2269
Cédric Meuterf3fab332020-04-25 11:30:45 +02002270/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002271 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
2272 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002273int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2274 int (*f_rng)(void *, unsigned char *, size_t),
2275 void *p_rng,
2276 mbedtls_md_type_t md_alg,
2277 unsigned int hashlen,
2278 const unsigned char *hash,
2279 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002280{
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2282 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002283}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002287/*
2288 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
2289 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002290
2291/* Construct a PKCS v1.5 encoding of a hashed message
2292 *
2293 * This is used both for signature generation and verification.
2294 *
2295 * Parameters:
2296 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002297 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002298 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002299 * - hash: Buffer containing the hashed message or the raw data.
2300 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002301 * - dst: Buffer to hold the encoded message.
2302 *
2303 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002304 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002305 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002306 *
2307 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
2309 unsigned int hashlen,
2310 const unsigned char *hash,
2311 size_t dst_len,
2312 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01002313{
2314 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002315 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002316 unsigned char *p = dst;
2317 const char *oid = NULL;
2318
2319 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002321 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if (md_size == 0) {
2323 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2324 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2327 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2328 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 if (hashlen != md_size) {
2331 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2332 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002333
2334 /* Double-check that 8 + hashlen + oid_size can be used as a
2335 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01002337 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 10 + hashlen + oid_size < 10 + hashlen) {
2339 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2340 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002341
2342 /*
2343 * Static bounds check:
2344 * - Need 10 bytes for five tag-length pairs.
2345 * (Insist on 1-byte length encodings to protect against variants of
2346 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2347 * - Need hashlen bytes for hash
2348 * - Need oid_size bytes for hash alg OID.
2349 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if (nb_pad < 10 + hashlen + oid_size) {
2351 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2352 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002353 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 } else {
2355 if (nb_pad < hashlen) {
2356 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2357 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002358
2359 nb_pad -= hashlen;
2360 }
2361
Hanno Becker2b2f8982017-09-27 17:10:03 +01002362 /* Need space for signature header and padding delimiter (3 bytes),
2363 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 if (nb_pad < 3 + 8) {
2365 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2366 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002367 nb_pad -= 3;
2368
2369 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002370 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002371
2372 /* Write signature header and padding */
2373 *p++ = 0;
2374 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002376 p += nb_pad;
2377 *p++ = 0;
2378
2379 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 if (md_alg == MBEDTLS_MD_NONE) {
2381 memcpy(p, hash, hashlen);
2382 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002383 }
2384
2385 /* Signing hashed data, add corresponding ASN.1 structure
2386 *
2387 * DigestInfo ::= SEQUENCE {
2388 * digestAlgorithm DigestAlgorithmIdentifier,
2389 * digest Digest }
2390 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2391 * Digest ::= OCTET STRING
2392 *
2393 * Schematic:
2394 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2395 * TAG-NULL + LEN [ NULL ] ]
2396 * TAG-OCTET + LEN [ HASH ] ]
2397 */
2398 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002400 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002402 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002403 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002405 p += oid_size;
2406 *p++ = MBEDTLS_ASN1_NULL;
2407 *p++ = 0x00;
2408 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002409 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002411 p += hashlen;
2412
2413 /* Just a sanity-check, should be automatic
2414 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if (p != dst + dst_len) {
2416 mbedtls_platform_zeroize(dst, dst_len);
2417 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002418 }
2419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002421}
2422
Paul Bakkerb3869132013-02-28 17:21:01 +01002423/*
2424 * Do an RSA operation to sign the message digest
2425 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002426int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2427 int (*f_rng)(void *, unsigned char *, size_t),
2428 void *p_rng,
2429 mbedtls_md_type_t md_alg,
2430 unsigned int hashlen,
2431 const unsigned char *hash,
2432 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002433{
Janos Follath24eed8d2019-11-22 13:21:35 +00002434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002435 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002438 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2442 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2443 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002444
Hanno Beckerfdf38032017-09-06 12:35:55 +01002445 /*
2446 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2447 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2450 ctx->len, sig)) != 0) {
2451 return ret;
2452 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002453
Hanno Beckerfdf38032017-09-06 12:35:55 +01002454 /* Private key operation
2455 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002456 * In order to prevent Lenstra's attack, make the signature in a
2457 * temporary buffer and check it before returning it.
2458 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 sig_try = mbedtls_calloc(1, ctx->len);
2461 if (sig_try == NULL) {
2462 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002463 }
2464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 verif = mbedtls_calloc(1, ctx->len);
2466 if (verif == NULL) {
2467 mbedtls_free(sig_try);
2468 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2469 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2472 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2473
2474 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002475 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2476 goto cleanup;
2477 }
2478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002480
2481cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002482 mbedtls_zeroize_and_free(sig_try, ctx->len);
2483 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (ret != 0) {
2486 memset(sig, '!', ctx->len);
2487 }
2488 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002491
2492/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 * Do an RSA operation to sign the message digest
2494 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002495int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2496 int (*f_rng)(void *, unsigned char *, size_t),
2497 void *p_rng,
2498 mbedtls_md_type_t md_alg,
2499 unsigned int hashlen,
2500 const unsigned char *hash,
2501 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002502{
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002504 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#if defined(MBEDTLS_PKCS1_V15)
2509 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2511 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002512#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#if defined(MBEDTLS_PKCS1_V21)
2515 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2517 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002518#endif
2519
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002523}
2524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002526/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002527 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2530 mbedtls_md_type_t md_alg,
2531 unsigned int hashlen,
2532 const unsigned char *hash,
2533 mbedtls_md_type_t mgf1_hash_id,
2534 int expected_salt_len,
2535 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002536{
Janos Follath24eed8d2019-11-22 13:21:35 +00002537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002538 size_t siglen;
2539 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002540 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002541 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002542 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002543 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002547 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002549
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 siglen = ctx->len;
2551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 if (siglen < 16 || siglen > sizeof(buf)) {
2553 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2554 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (ret != 0) {
2559 return ret;
2560 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
2562 p = buf;
2563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (buf[siglen - 1] != 0xBC) {
2565 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002566 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (md_alg != MBEDTLS_MD_NONE) {
2569 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002570 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (exp_hashlen == 0) {
2572 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2573 }
2574
2575 if (hashlen != exp_hashlen) {
2576 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2577 }
2578 }
2579
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002580 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (hlen == 0) {
2582 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2583 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002584
Simon Butcher02037452016-03-01 21:19:12 +00002585 /*
2586 * Note: EMSA-PSS verification is over the length of N - 1 bits
2587 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (buf[0] >> (8 - siglen * 8 + msb)) {
2591 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2592 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002593
Simon Butcher02037452016-03-01 21:19:12 +00002594 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002596 p++;
2597 siglen -= 1;
2598 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (siglen < hlen + 2) {
2601 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2602 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002603 hash_start = p + siglen - hlen - 1;
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2606 if (ret != 0) {
2607 return ret;
2608 }
Paul Bakker02303e82013-01-03 11:08:31 +01002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002613 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (*p++ != 0x01) {
2617 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2618 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002619
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002620 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2623 observed_salt_len != (size_t) expected_salt_len) {
2624 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002625 }
2626
Simon Butcher02037452016-03-01 21:19:12 +00002627 /*
2628 * Generate H = Hash( M' )
2629 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2631 result, mgf1_hash_id);
2632 if (ret != 0) {
2633 return ret;
2634 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 if (memcmp(hash_start, result, hlen) != 0) {
2637 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2638 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002641}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002642
2643/*
2644 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2645 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2647 mbedtls_md_type_t md_alg,
2648 unsigned int hashlen,
2649 const unsigned char *hash,
2650 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002651{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002652 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002654 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002659 : md_alg;
2660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2662 md_alg, hashlen, hash,
2663 mgf1_hash_id,
2664 MBEDTLS_RSA_SALT_LEN_ANY,
2665 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002666
2667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002671/*
2672 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2673 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002674int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2675 mbedtls_md_type_t md_alg,
2676 unsigned int hashlen,
2677 const unsigned char *hash,
2678 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002679{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002680 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002681 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002682 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002685 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002687
2688 sig_len = ctx->len;
2689
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002690 /*
2691 * Prepare expected PKCS1 v1.5 encoding of hash.
2692 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2695 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002696 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2697 goto cleanup;
2698 }
2699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2701 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002702 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002704
2705 /*
2706 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2707 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 ret = mbedtls_rsa_public(ctx, sig, encoded);
2710 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002711 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002713
Simon Butcher02037452016-03-01 21:19:12 +00002714 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002715 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002716 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2719 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002720 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2721 goto cleanup;
2722 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002723
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002724cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002727 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002728 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002731 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002732 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002735}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
2738/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002739 * Do an RSA operation and check the message digest
2740 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002741int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2742 mbedtls_md_type_t md_alg,
2743 unsigned int hashlen,
2744 const unsigned char *hash,
2745 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002746{
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002748 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752#if defined(MBEDTLS_PKCS1_V15)
2753 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2755 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002756#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758#if defined(MBEDTLS_PKCS1_V21)
2759 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2761 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002762#endif
2763
2764 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002766 }
2767}
2768
2769/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002770 * Copy the components of an RSA key
2771 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002772int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002773{
Janos Follath24eed8d2019-11-22 13:21:35 +00002774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002775
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002776 dst->len = src->len;
2777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2779 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2782 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2783 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002784
2785#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2787 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2788 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2789 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2790 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002791#endif
2792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002794
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2796 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002797
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002798 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002799 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002800
2801cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 if (ret != 0) {
2803 mbedtls_rsa_free(dst);
2804 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002805
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002807}
2808
2809/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002810 * Free the components of an RSA key
2811 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002812void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002813{
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002815 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 mbedtls_mpi_free(&ctx->Vi);
2819 mbedtls_mpi_free(&ctx->Vf);
2820 mbedtls_mpi_free(&ctx->RN);
2821 mbedtls_mpi_free(&ctx->D);
2822 mbedtls_mpi_free(&ctx->Q);
2823 mbedtls_mpi_free(&ctx->P);
2824 mbedtls_mpi_free(&ctx->E);
2825 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002826
Hanno Becker33c30a02017-08-23 07:00:22 +01002827#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 mbedtls_mpi_free(&ctx->RQ);
2829 mbedtls_mpi_free(&ctx->RP);
2830 mbedtls_mpi_free(&ctx->QP);
2831 mbedtls_mpi_free(&ctx->DQ);
2832 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002833#endif /* MBEDTLS_RSA_NO_CRT */
2834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002836 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 if (ctx->ver != 0) {
2838 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002839 ctx->ver = 0;
2840 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002841#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002842}
2843
Hanno Beckerab377312017-08-23 16:24:51 +01002844#endif /* !MBEDTLS_RSA_ALT */
2845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Paul Bakker5121ce52009-01-03 21:22:43 +00002848
2849/*
2850 * Example RSA-1024 keypair, for test purposes
2851 */
2852#define KEY_LEN 128
2853
2854#define RSA_N "9292758453063D803DD603D5E777D788" \
2855 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2856 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2857 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2858 "93A89813FBF3C4F8066D2D800F7C38A8" \
2859 "1AE31942917403FF4946B0A83D3D3E05" \
2860 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2861 "5E94BB77B07507233A0BC7BAC8F90F79"
2862
2863#define RSA_E "10001"
2864
2865#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2866 "66CA472BC44D253102F8B4A9D3BFA750" \
2867 "91386C0077937FE33FA3252D28855837" \
2868 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2869 "DF79C5CE07EE72C7F123142198164234" \
2870 "CABB724CF78B8173B9F880FC86322407" \
2871 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2872 "071513A1E85B5DFA031F21ECAE91A34D"
2873
2874#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2875 "2C01CAD19EA484A87EA4377637E75500" \
2876 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2877 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2878
2879#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2880 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2881 "910E4168387E3C30AA1E00C339A79508" \
2882 "8452DD96A9A5EA5D9DCA68DA636032AF"
2883
Paul Bakker5121ce52009-01-03 21:22:43 +00002884#define PT_LEN 24
2885#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2886 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002889static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002890{
gufe44c2620da2020-08-03 17:56:50 +02002891#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002892 size_t i;
2893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002895 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 }
Paul Bakker545570e2010-07-18 09:00:25 +00002897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002899 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002901#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002903 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002907#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002908
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002910}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002912
Paul Bakker5121ce52009-01-03 21:22:43 +00002913/*
2914 * Checkup routine
2915 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002916int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002917{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002918 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002920 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 unsigned char rsa_plaintext[PT_LEN];
2923 unsigned char rsa_decrypted[PT_LEN];
2924 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002925#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002926 unsigned char sha1sum[20];
2927#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
Hanno Becker3a701162017-08-22 13:52:43 +01002929 mbedtls_mpi K;
2930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 mbedtls_mpi_init(&K);
2932 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2935 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2936 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2937 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2938 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2939 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2940 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2941 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2942 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2943 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 if (verbose != 0) {
2948 mbedtls_printf(" RSA key validation: ");
2949 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2952 mbedtls_rsa_check_privkey(&rsa) != 0) {
2953 if (verbose != 0) {
2954 mbedtls_printf("failed\n");
2955 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002956
Hanno Becker5bc87292017-05-03 15:09:31 +01002957 ret = 1;
2958 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002959 }
2960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 if (verbose != 0) {
2962 mbedtls_printf("passed\n PKCS#1 encryption : ");
2963 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2968 PT_LEN, rsa_plaintext,
2969 rsa_ciphertext) != 0) {
2970 if (verbose != 0) {
2971 mbedtls_printf("failed\n");
2972 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002973
Hanno Becker5bc87292017-05-03 15:09:31 +01002974 ret = 1;
2975 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002976 }
2977
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 if (verbose != 0) {
2979 mbedtls_printf("passed\n PKCS#1 decryption : ");
2980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2983 &len, rsa_ciphertext, rsa_decrypted,
2984 sizeof(rsa_decrypted)) != 0) {
2985 if (verbose != 0) {
2986 mbedtls_printf("failed\n");
2987 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Hanno Becker5bc87292017-05-03 15:09:31 +01002989 ret = 1;
2990 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002991 }
2992
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2994 if (verbose != 0) {
2995 mbedtls_printf("failed\n");
2996 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002997
Hanno Becker5bc87292017-05-03 15:09:31 +01002998 ret = 1;
2999 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003000 }
3001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 if (verbose != 0) {
3003 mbedtls_printf("passed\n");
3004 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003005
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003006#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 if (verbose != 0) {
3008 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01003009 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003010
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01003011 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
3012 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 if (verbose != 0) {
3014 mbedtls_printf("failed\n");
3015 }
3016
3017 return 1;
3018 }
3019
3020 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
3021 MBEDTLS_MD_SHA1, 20,
3022 sha1sum, rsa_ciphertext) != 0) {
3023 if (verbose != 0) {
3024 mbedtls_printf("failed\n");
3025 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003026
Hanno Becker5bc87292017-05-03 15:09:31 +01003027 ret = 1;
3028 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003029 }
3030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 if (verbose != 0) {
3032 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
3033 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
3036 sha1sum, rsa_ciphertext) != 0) {
3037 if (verbose != 0) {
3038 mbedtls_printf("failed\n");
3039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
Hanno Becker5bc87292017-05-03 15:09:31 +01003041 ret = 1;
3042 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 }
3044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 if (verbose != 0) {
3046 mbedtls_printf("passed\n");
3047 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003048#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003049
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 if (verbose != 0) {
3051 mbedtls_printf("\n");
3052 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003053
Paul Bakker3d8fb632014-04-17 12:42:41 +02003054cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 mbedtls_mpi_free(&K);
3056 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02003058 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003061}
3062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065#endif /* MBEDTLS_RSA_C */