blob: 458ee26a3e8cb61fba456923fe60bb716198c21a [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"
Janos Follathc870e052024-08-22 14:53:13 +010032#include "bignum_internal.h"
Chris Jones66a4cd42021-03-09 16:04:12 +000033#include "rsa_alt_helpers.h"
Tomi Fontanilles573dc232023-12-10 14:57:51 +020034#include "rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/oid.h"
Valerio Settib328c442024-01-23 10:48:45 +010036#include "mbedtls/asn1write.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050037#include "mbedtls/platform_util.h"
Harry Ramseya05bfee2024-10-14 07:19:01 +010038#include "mbedtls/error_common.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020039#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020040#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020041#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
gufe44c2620da2020-08-03 17:56:50 +020045#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000046#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010050
Valerio Setti201e6432024-02-01 17:19:37 +010051/*
52 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
53 *
54 * The value zero is:
55 * - never a valid value for an RSA parameter
56 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
57 *
58 * Since values can't be omitted in PKCS#1, passing a zero value to
59 * rsa_complete() would be incorrect, so reject zero values early.
60 */
61static int asn1_get_nonzero_mpi(unsigned char **p,
62 const unsigned char *end,
63 mbedtls_mpi *X)
64{
65 int ret;
66
67 ret = mbedtls_asn1_get_mpi(p, end, X);
68 if (ret != 0) {
69 return ret;
70 }
71
72 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
73 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
74 }
75
76 return 0;
77}
78
Valerio Setti135ebde2024-02-01 17:00:29 +010079int mbedtls_rsa_parse_key(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +010080{
81 int ret, version;
82 size_t len;
83 unsigned char *p, *end;
84
85 mbedtls_mpi T;
86 mbedtls_mpi_init(&T);
87
88 p = (unsigned char *) key;
89 end = p + keylen;
90
91 /*
92 * This function parses the RSAPrivateKey (PKCS#1)
93 *
94 * RSAPrivateKey ::= SEQUENCE {
95 * version Version,
96 * modulus INTEGER, -- n
97 * publicExponent INTEGER, -- e
98 * privateExponent INTEGER, -- d
99 * prime1 INTEGER, -- p
100 * prime2 INTEGER, -- q
101 * exponent1 INTEGER, -- d mod (p-1)
102 * exponent2 INTEGER, -- d mod (q-1)
103 * coefficient INTEGER, -- (inverse of q) mod p
104 * otherPrimeInfos OtherPrimeInfos OPTIONAL
105 * }
106 */
107 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
108 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
109 return ret;
110 }
111
Valerio Setti9de84bd2024-02-08 17:40:27 +0100112 if (end != p + len) {
113 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
114 }
Valerio Setti44ff9502024-02-01 16:51:05 +0100115
116 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
117 return ret;
118 }
119
120 if (version != 0) {
121 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
122 }
123
124 /* Import N */
125 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
126 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
127 NULL, NULL)) != 0) {
128 goto cleanup;
129 }
130
131 /* Import E */
132 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
133 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
134 NULL, &T)) != 0) {
135 goto cleanup;
136 }
137
138 /* Import D */
139 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
140 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
141 &T, NULL)) != 0) {
142 goto cleanup;
143 }
144
145 /* Import P */
146 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
147 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
148 NULL, NULL)) != 0) {
149 goto cleanup;
150 }
151
152 /* Import Q */
153 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
154 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
155 NULL, NULL)) != 0) {
156 goto cleanup;
157 }
158
Thomas Daubneyf47b66e2024-06-04 18:15:44 +0100159#if !defined(MBEDTLS_RSA_NO_CRT)
Valerio Setti44ff9502024-02-01 16:51:05 +0100160 /*
161 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
162 * that they can be easily recomputed from D, P and Q. However by
163 * parsing them from the PKCS1 structure it is possible to avoid
164 * recalculating them which both reduces the overhead of loading
165 * RSA private keys into memory and also avoids side channels which
166 * can arise when computing those values, since all of D, P, and Q
167 * are secret. See https://eprint.iacr.org/2020/055 for a
168 * description of one such attack.
169 */
170
171 /* Import DP */
172 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
173 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
174 goto cleanup;
175 }
176
177 /* Import DQ */
178 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
179 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
180 goto cleanup;
181 }
182
183 /* Import QP */
184 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
185 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
186 goto cleanup;
187 }
188
189#else
190 /* Verify existence of the CRT params */
191 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
192 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
193 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
194 goto cleanup;
195 }
196#endif
197
198 /* rsa_complete() doesn't complete anything with the default
199 * implementation but is still called:
200 * - for the benefit of alternative implementation that may want to
201 * pre-compute stuff beyond what's provided (eg Montgomery factors)
202 * - as is also sanity-checks the key
203 *
204 * Furthermore, we also check the public part for consistency with
205 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
206 */
207 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
208 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
209 goto cleanup;
210 }
211
212 if (p != end) {
213 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
214 }
215
216cleanup:
217
218 mbedtls_mpi_free(&T);
219
220 if (ret != 0) {
221 mbedtls_rsa_free(rsa);
222 }
223
224 return ret;
225}
226
Valerio Setti201e6432024-02-01 17:19:37 +0100227int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +0100228{
Valerio Setti201e6432024-02-01 17:19:37 +0100229 unsigned char *p = (unsigned char *) key;
230 unsigned char *end = (unsigned char *) (key + keylen);
Valerio Setti44ff9502024-02-01 16:51:05 +0100231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
232 size_t len;
233
234 /*
235 * RSAPublicKey ::= SEQUENCE {
236 * modulus INTEGER, -- n
237 * publicExponent INTEGER -- e
238 * }
239 */
240
Valerio Setti201e6432024-02-01 17:19:37 +0100241 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
Valerio Setti44ff9502024-02-01 16:51:05 +0100242 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
243 return ret;
244 }
245
Valerio Setti9de84bd2024-02-08 17:40:27 +0100246 if (end != p + len) {
247 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
248 }
Valerio Settife329ce2024-02-06 08:00:18 +0100249
Valerio Setti44ff9502024-02-01 16:51:05 +0100250 /* Import N */
Valerio Setti201e6432024-02-01 17:19:37 +0100251 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100252 return ret;
253 }
254
Valerio Setti201e6432024-02-01 17:19:37 +0100255 if ((ret = mbedtls_rsa_import_raw(rsa, p, len, NULL, 0, NULL, 0,
Valerio Setti44ff9502024-02-01 16:51:05 +0100256 NULL, 0, NULL, 0)) != 0) {
257 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
258 }
259
Valerio Setti201e6432024-02-01 17:19:37 +0100260 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100261
262 /* Import E */
Valerio Setti201e6432024-02-01 17:19:37 +0100263 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100264 return ret;
265 }
266
267 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
Valerio Setti201e6432024-02-01 17:19:37 +0100268 NULL, 0, p, len)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100269 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
270 }
271
Valerio Setti201e6432024-02-01 17:19:37 +0100272 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100273
274 if (mbedtls_rsa_complete(rsa) != 0 ||
275 mbedtls_rsa_check_pubkey(rsa) != 0) {
276 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
277 }
278
Valerio Setti201e6432024-02-01 17:19:37 +0100279 if (p != end) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100280 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
281 }
282
283 return 0;
284}
285
Valerio Setti135ebde2024-02-01 17:00:29 +0100286int mbedtls_rsa_write_key(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100287 unsigned char **p)
288{
289 size_t len = 0;
290 int ret;
291
292 mbedtls_mpi T; /* Temporary holding the exported parameters */
293
294 /*
295 * Export the parameters one after another to avoid simultaneous copies.
296 */
297
298 mbedtls_mpi_init(&T);
299
300 /* Export QP */
301 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
302 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
303 goto end_of_export;
304 }
305 len += ret;
306
307 /* Export DQ */
308 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
309 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
310 goto end_of_export;
311 }
312 len += ret;
313
314 /* Export DP */
315 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
316 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
317 goto end_of_export;
318 }
319 len += ret;
320
321 /* Export Q */
322 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, &T, NULL, NULL)) != 0 ||
323 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
324 goto end_of_export;
325 }
326 len += ret;
327
328 /* Export P */
329 if ((ret = mbedtls_rsa_export(rsa, NULL, &T, NULL, NULL, NULL)) != 0 ||
330 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
331 goto end_of_export;
332 }
333 len += ret;
334
335 /* Export D */
336 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, &T, NULL)) != 0 ||
337 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
338 goto end_of_export;
339 }
340 len += ret;
341
342 /* Export E */
343 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
344 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
345 goto end_of_export;
346 }
347 len += ret;
348
349 /* Export N */
350 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
351 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
352 goto end_of_export;
353 }
354 len += ret;
355
356end_of_export:
357
358 mbedtls_mpi_free(&T);
359 if (ret < 0) {
360 return ret;
361 }
362
363 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
364 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
365 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
366 MBEDTLS_ASN1_CONSTRUCTED |
367 MBEDTLS_ASN1_SEQUENCE));
368
369 return (int) len;
370}
371
372/*
373 * RSAPublicKey ::= SEQUENCE {
374 * modulus INTEGER, -- n
375 * publicExponent INTEGER -- e
376 * }
377 */
Valerio Setti135ebde2024-02-01 17:00:29 +0100378int mbedtls_rsa_write_pubkey(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100379 unsigned char **p)
380{
381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
382 size_t len = 0;
383 mbedtls_mpi T;
384
385 mbedtls_mpi_init(&T);
386
387 /* Export E */
388 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
389 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
390 goto end_of_export;
391 }
392 len += ret;
393
394 /* Export N */
395 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
396 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
397 goto end_of_export;
398 }
399 len += ret;
400
401end_of_export:
402
403 mbedtls_mpi_free(&T);
404 if (ret < 0) {
405 return ret;
406 }
407
408 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
409 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
410 MBEDTLS_ASN1_SEQUENCE));
411
412 return (int) len;
413}
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100414
Thomas Daubneyf47b66e2024-06-04 18:15:44 +0100415#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C)
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100416
417/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
418 * operation (EME-PKCS1-v1_5 decoding).
419 *
420 * \note The return value from this function is a sensitive value
421 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
422 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
423 * is often a situation that an attacker can provoke and leaking which
424 * one is the result is precisely the information the attacker wants.
425 *
426 * \param input The input buffer which is the payload inside PKCS#1v1.5
427 * encryption padding, called the "encoded message EM"
428 * by the terminology.
429 * \param ilen The length of the payload in the \p input buffer.
430 * \param output The buffer for the payload, called "message M" by the
431 * PKCS#1 terminology. This must be a writable buffer of
432 * length \p output_max_len bytes.
433 * \param olen The address at which to store the length of
434 * the payload. This must not be \c NULL.
435 * \param output_max_len The length in bytes of the output buffer \p output.
436 *
437 * \return \c 0 on success.
438 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
439 * The output buffer is too small for the unpadded payload.
440 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
441 * The input doesn't contain properly formatted padding.
442 */
443static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
444 size_t ilen,
445 unsigned char *output,
446 size_t output_max_len,
447 size_t *olen)
448{
449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
450 size_t i, plaintext_max_size;
451
452 /* The following variables take sensitive values: their value must
453 * not leak into the observable behavior of the function other than
454 * the designated outputs (output, olen, return value). Otherwise
455 * this would open the execution of the function to
456 * side-channel-based variants of the Bleichenbacher padding oracle
457 * attack. Potential side channels include overall timing, memory
458 * access patterns (especially visible to an adversary who has access
459 * to a shared memory cache), and branches (especially visible to
460 * an adversary who has access to a shared code cache or to a shared
461 * branch predictor). */
462 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100463 mbedtls_ct_condition_t bad;
464 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100465 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100466 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100467
468 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
469 : output_max_len;
470
471 /* Check and get padding length in constant time and constant
472 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100473 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100474
475
476 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
477 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100478 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100479
480 /* Read the whole buffer. Set pad_done to nonzero if we find
481 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100482 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100483 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100484 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100485 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100486 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100487 }
488
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100489 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100490 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100491
492 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100493 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100494
495 /* If the padding is valid, set plaintext_size to the number of
496 * remaining bytes after stripping the padding. If the padding
497 * is invalid, avoid leaking this fact through the size of the
498 * output: use the maximum message size that fits in the output
499 * buffer. Do it without branches to avoid leaking the padding
500 * validity through timing. RSA keys are small enough that all the
501 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100502 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100503 bad, (unsigned) plaintext_max_size,
504 (unsigned) (ilen - pad_count - 3));
505
506 /* Set output_too_large to 0 if the plaintext fits in the output
507 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100508 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100509 plaintext_max_size);
510
511 /* Set ret without branches to avoid timing attacks. Return:
512 * - INVALID_PADDING if the padding is bad (bad != 0).
513 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
514 * plaintext does not fit in the output buffer.
515 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100516 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100517 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100518 MBEDTLS_ERR_RSA_INVALID_PADDING,
519 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100520 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100521
522 /* If the padding is bad or the plaintext is too large, zero the
523 * data that we're about to copy to the output buffer.
524 * We need to copy the same amount of data
525 * from the same buffer whether the padding is good or not to
526 * avoid leaking the padding validity through overall timing or
527 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100528 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100529
530 /* If the plaintext is too large, truncate it to the buffer size.
531 * Copy anyway to avoid revealing the length through timing, because
532 * revealing the length is as bad as revealing the padding validity
533 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100534 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100535 (unsigned) plaintext_max_size,
536 (unsigned) plaintext_size);
537
538 /* Move the plaintext to the leftmost position where it can start in
539 * the working buffer, i.e. make it start plaintext_max_size from
540 * the end of the buffer. Do this with a memory access trace that
541 * does not depend on the plaintext size. After this move, the
542 * starting location of the plaintext is no longer sensitive
543 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100544 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
545 plaintext_max_size,
546 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100547
548 /* Finally copy the decrypted plaintext plus trailing zeros into the output
549 * buffer. If output_max_len is 0, then output may be an invalid pointer
550 * and the result of memcpy() would be undefined; prevent undefined
551 * behavior making sure to depend only on output_max_len (the size of the
552 * user-provided output buffer), which is independent from plaintext
553 * length, validity of padding, success of the decryption, and other
554 * secrets. */
555 if (output_max_len != 0) {
556 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
557 }
558
559 /* Report the amount of data we copied to the output buffer. In case
560 * of errors (bad padding or output too large), the value of *olen
561 * when this function returns is not specified. Making it equivalent
562 * to the good case limits the risks of leaking the padding validity. */
563 *olen = plaintext_size;
564
565 return ret;
566}
567
Thomas Daubneyf47b66e2024-06-04 18:15:44 +0100568#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C */
Hanno Beckera565f542017-10-11 11:00:19 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
571 const mbedtls_mpi *N,
572 const mbedtls_mpi *P, const mbedtls_mpi *Q,
573 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100574{
Janos Follath24eed8d2019-11-22 13:21:35 +0000575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
578 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
579 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
580 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
581 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
582 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100583 }
584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (N != NULL) {
586 ctx->len = mbedtls_mpi_size(&ctx->N);
587 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100590}
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
593 unsigned char const *N, size_t N_len,
594 unsigned char const *P, size_t P_len,
595 unsigned char const *Q, size_t Q_len,
596 unsigned char const *D, size_t D_len,
597 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100598{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000599 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if (N != NULL) {
602 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
603 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100604 }
605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if (P != NULL) {
607 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
608 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (Q != NULL) {
611 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
612 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (D != NULL) {
615 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
616 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if (E != NULL) {
619 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
620 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100621
622cleanup:
623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (ret != 0) {
625 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
626 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100629}
630
Hanno Becker705fc682017-10-10 17:57:02 +0100631/*
632 * Checks whether the context fields are set in such a way
633 * that the RSA primitives will be able to execute without error.
634 * It does *not* make guarantees for consistency of the parameters.
635 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100636static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
637 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100638{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100639#if !defined(MBEDTLS_RSA_NO_CRT)
640 /* blinding_needed is only used for NO_CRT to decide whether
641 * P,Q need to be present or not. */
642 ((void) blinding_needed);
643#endif
644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
646 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
647 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000648 }
Hanno Becker705fc682017-10-10 17:57:02 +0100649
650 /*
651 * 1. Modular exponentiation needs positive, odd moduli.
652 */
653
654 /* Modular exponentiation wrt. N is always used for
655 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
657 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
658 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100659 }
660
661#if !defined(MBEDTLS_RSA_NO_CRT)
662 /* Modular exponentiation for P and Q is only
663 * used for private key operations and if CRT
664 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (is_priv &&
666 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
667 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
668 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
669 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
670 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100671 }
672#endif /* !MBEDTLS_RSA_NO_CRT */
673
674 /*
675 * 2. Exponents must be positive
676 */
677
678 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
680 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
681 }
Hanno Becker705fc682017-10-10 17:57:02 +0100682
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100683#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100684 /* For private key operations, use D or DP & DQ
685 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
687 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
688 }
Hanno Becker705fc682017-10-10 17:57:02 +0100689#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (is_priv &&
691 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
692 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
693 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100694 }
695#endif /* MBEDTLS_RSA_NO_CRT */
696
697 /* Blinding shouldn't make exponents negative either,
698 * so check that P, Q >= 1 if that hasn't yet been
699 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100700#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (is_priv && blinding_needed &&
702 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
703 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
704 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100705 }
706#endif
707
708 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100709 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100710#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (is_priv &&
712 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
713 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100714 }
715#endif
716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100718}
719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100721{
722 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500723 int have_N, have_P, have_Q, have_D, have_E;
724#if !defined(MBEDTLS_RSA_NO_CRT)
725 int have_DP, have_DQ, have_QP;
726#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500727 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
730 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
731 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
732 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
733 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500734
735#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
737 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
738 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500739#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100740
Hanno Becker617c1ae2017-08-23 14:11:24 +0100741 /*
742 * Check whether provided parameters are enough
743 * to deduce all others. The following incomplete
744 * parameter sets for private keys are supported:
745 *
746 * (1) P, Q missing.
747 * (2) D and potentially N missing.
748 *
749 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100750
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500751 n_missing = have_P && have_Q && have_D && have_E;
752 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
753 d_missing = have_P && have_Q && !have_D && have_E;
754 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100755
756 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500757 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 if (!is_priv && !is_pub) {
760 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
761 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100762
763 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100764 * Step 1: Deduce N if P, Q are provided.
765 */
766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 if (!have_N && have_P && have_Q) {
768 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
769 &ctx->Q)) != 0) {
770 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100771 }
772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100774 }
775
776 /*
777 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100778 */
779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (pq_missing) {
781 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
782 &ctx->P, &ctx->Q);
783 if (ret != 0) {
784 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
785 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 } else if (d_missing) {
788 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
789 &ctx->Q,
790 &ctx->E,
791 &ctx->D)) != 0) {
792 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100793 }
794 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100795
Hanno Becker617c1ae2017-08-23 14:11:24 +0100796 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100797 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100798 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100799 */
800
Hanno Becker23344b52017-08-23 07:43:27 +0100801#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (is_priv && !(have_DP && have_DQ && have_QP)) {
803 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
804 &ctx->DP, &ctx->DQ, &ctx->QP);
805 if (ret != 0) {
806 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
807 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100808 }
Hanno Becker23344b52017-08-23 07:43:27 +0100809#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100810
811 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100812 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100813 */
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100816}
817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
819 unsigned char *N, size_t N_len,
820 unsigned char *P, size_t P_len,
821 unsigned char *Q, size_t Q_len,
822 unsigned char *D, size_t D_len,
823 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100824{
825 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500826 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100827
828 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500829 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
831 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
832 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
833 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
834 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100837 /* If we're trying to export private parameters for a public key,
838 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (P != NULL || Q != NULL || D != NULL) {
840 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
841 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100842
843 }
844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (N != NULL) {
846 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
847 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 if (P != NULL) {
850 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
851 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (Q != NULL) {
854 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
855 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (D != NULL) {
858 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
859 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (E != NULL) {
862 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
863 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100864
865cleanup:
866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100868}
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
871 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
872 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100873{
Janos Follath24eed8d2019-11-22 13:21:35 +0000874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500875 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100876
877 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500878 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
880 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
881 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
882 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
883 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100886 /* If we're trying to export private parameters for a public key,
887 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if (P != NULL || Q != NULL || D != NULL) {
889 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
890 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100891
892 }
893
894 /* Export all requested core parameters. */
895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
897 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
898 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
899 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
900 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
901 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100902 }
903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100905}
906
907/*
908 * Export CRT parameters
909 * This must also be implemented if CRT is not used, for being able to
910 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
911 * can be used in this case.
912 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
914 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100915{
Janos Follath24eed8d2019-11-22 13:21:35 +0000916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500917 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100918
919 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500920 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
922 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
923 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
924 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
925 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if (!is_priv) {
928 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
929 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100930
Hanno Beckerdc95c892017-08-23 06:57:02 +0100931#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100932 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
934 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
935 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
936 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100937 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100938#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
940 DP, DQ, QP)) != 0) {
941 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100942 }
943#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100946}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100947
Paul Bakker5121ce52009-01-03 21:22:43 +0000948/*
949 * Initialize an RSA context
950 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000952{
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
Ronald Cronc1905a12021-06-05 11:11:14 +0200955 ctx->padding = MBEDTLS_RSA_PKCS_V15;
956 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100959 /* Set ctx->ver to nonzero to indicate that the mutex has been
960 * initialized and will need to be freed. */
961 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200963#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000964}
965
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100966/*
967 * Set padding for an existing RSA context
968 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
970 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100971{
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200973#if defined(MBEDTLS_PKCS1_V15)
974 case MBEDTLS_RSA_PKCS_V15:
975 break;
976#endif
977
978#if defined(MBEDTLS_PKCS1_V21)
979 case MBEDTLS_RSA_PKCS_V21:
980 break;
981#endif
982 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200984 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200985
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200986#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
988 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200989 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200990 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return MBEDTLS_ERR_RSA_INVALID_PADDING;
992 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200993 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200994#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500995
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100996 ctx->padding = padding;
997 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +01001000}
1001
Hanno Becker617c1ae2017-08-23 14:11:24 +01001002/*
Yanray Wang83548b52023-03-15 16:46:34 +08001003 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +08001004 */
1005int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
1006{
Yanray Wang644b9012023-03-15 16:50:31 +08001007 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +08001008}
1009
1010/*
Yanray Wang12cb3962023-03-01 10:20:02 +08001011 * Get hash identifier of mbedtls_md_type_t type
1012 */
Yanray Wangd41684e2023-03-17 18:54:22 +08001013int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +08001014{
Yanray Wang644b9012023-03-15 16:50:31 +08001015 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +08001016}
1017
1018/*
Gilles Peskine19f1adf2024-02-01 22:17:44 +01001019 * Get length in bits of RSA modulus
1020 */
1021size_t mbedtls_rsa_get_bitlen(const mbedtls_rsa_context *ctx)
1022{
1023 return mbedtls_mpi_bitlen(&ctx->N);
1024}
1025
1026/*
Hanno Becker617c1ae2017-08-23 14:11:24 +01001027 * Get length in bytes of RSA modulus
1028 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +01001030{
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +01001032}
1033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001035
1036/*
1037 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001038 *
1039 * This generation method follows the RSA key pair generation procedure of
1040 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
1043 int (*f_rng)(void *, unsigned char *, size_t),
1044 void *p_rng,
1045 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +00001046{
Janos Follath24eed8d2019-11-22 13:21:35 +00001047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -08001048 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +01001049 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
Janos Follathb8fc1b02018-09-03 15:37:01 +01001051 /*
1052 * If the modulus is 1024 bit long or shorter, then the security strength of
1053 * the RSA algorithm is less than or equal to 80 bits and therefore an error
1054 * rate of 2^-80 is sufficient.
1055 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +01001057 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 }
Janos Follathb8fc1b02018-09-03 15:37:01 +01001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 mbedtls_mpi_init(&H);
1061 mbedtls_mpi_init(&G);
1062 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001063
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001064 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +01001065 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1066 goto cleanup;
1067 }
1068
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001069 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1071 goto cleanup;
1072 }
1073
1074 /*
1075 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001076 * 1. |P-Q| > 2^( nbits / 2 - 100 )
1077 * 2. GCD( E, (P-1)*(Q-1) ) == 1
1078 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 do {
1083 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
1084 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
1087 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001089 /* 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 +01001090 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
1091 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001095 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if (H.s < 0) {
1097 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
1098 }
Janos Follathef441782016-09-21 13:18:12 +01001099
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001100 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
1102 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
1103 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001104
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001105 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
1107 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -08001108 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001110
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001111 /* 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 +01001112 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
1113 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
1114 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 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 -08001117 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001119
1120 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001123 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
1125 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001130
Jethro Beekman97f95c92018-02-13 15:50:36 -08001131#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 * DP = D mod (P - 1)
1134 * DQ = D mod (Q - 1)
1135 * QP = Q^-1 mod P
1136 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
1138 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001139#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
Hanno Becker83aad1f2017-08-23 06:45:10 +01001141 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
1144cleanup:
1145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 mbedtls_mpi_free(&H);
1147 mbedtls_mpi_free(&G);
1148 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if (ret != 0) {
1151 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +01001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if ((-ret & ~0x7f) == 0) {
1154 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
1155 }
1156 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001157 }
1158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001160}
1161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001163
1164/*
1165 * Check a public RSA key
1166 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001168{
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
1170 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
1174 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001175 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
1178 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
1179 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
1180 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1181 }
1182
1183 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001184}
1185
1186/*
Hanno Becker705fc682017-10-10 17:57:02 +01001187 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001189int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001190{
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
1192 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
1193 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 }
Paul Bakker48377d92013-08-30 12:06:24 +02001195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
1197 &ctx->D, &ctx->E, NULL, NULL) != 0) {
1198 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001200
Hanno Beckerb269a852017-08-25 08:03:21 +01001201#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
1203 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
1204 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +01001205 }
1206#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001209}
1210
1211/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001212 * Check if contexts holding a public and private key match
1213 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001214int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
1215 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001216{
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
1218 mbedtls_rsa_check_privkey(prv) != 0) {
1219 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001220 }
1221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
1223 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
1224 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001225 }
1226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001228}
1229
1230/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 * Do an RSA public key operation
1232 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001233int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
1234 const unsigned char *input,
1235 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001236{
Janos Follath24eed8d2019-11-22 13:21:35 +00001237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001238 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
1242 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1243 }
Hanno Becker705fc682017-10-10 17:57:02 +01001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001247#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1249 return ret;
1250 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001251#endif
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001256 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1257 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 }
1259
1260 olen = ctx->len;
Janos Follath87253af2024-08-15 16:06:19 +01001261 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod_unsafe(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
1264cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1267 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1268 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001269#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (ret != 0) {
1274 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
1275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001278}
1279
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001280/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001281 * Generate or update blinding values, see section 10 of:
1282 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001283 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001284 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001285 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1287 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001288{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001289 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001290 mbedtls_mpi R;
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001295 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1297 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1298 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1299 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001300
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001301 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001302 }
1303
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001304 /* Unblinding value: Vf = random number, invertible mod N */
1305 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +02001307 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
1308 goto cleanup;
1309 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 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 +02001312
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001313 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
1315 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
1316 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001317
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001318 /* At this point, Vi is invertible mod N if and only if both Vf and R
1319 * are invertible mod N. If one of them isn't, we don't need to know
1320 * which one, we just loop and choose new values for both of them.
1321 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
1323 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001324 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -05001328
1329 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
1331 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001332
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001333 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001334 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 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 +02001336
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001337
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001338cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001342}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001343
Paul Bakker5121ce52009-01-03 21:22:43 +00001344/*
Janos Follathd6b09652023-11-21 09:33:54 +00001345 * Unblind
1346 * T = T * Vf mod N
1347 */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001348static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, const mbedtls_mpi *N)
Janos Follathd6b09652023-11-21 09:33:54 +00001349{
1350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1351 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
1352 const size_t nlimbs = N->n;
1353 const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
1354 mbedtls_mpi RR, M_T;
1355
1356 mbedtls_mpi_init(&RR);
1357 mbedtls_mpi_init(&M_T);
1358
1359 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
1360 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
1361
1362 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
1363 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
1364
Janos Follathe6750b22023-12-27 10:22:59 +00001365 /* T = T * Vf mod N
1366 * Reminder: montmul(A, B, N) = A * B * R^-1 mod N
1367 * Usually both operands are multiplied by R mod N beforehand (by calling
1368 * `to_mont_rep()` on them), yielding a result that's also * R mod N (aka
1369 * "in the Montgomery domain"). Here we only multiply one operand by R mod
1370 * N, so the result is directly what we want - no need to call
1371 * `from_mont_rep()` on it. */
Janos Follathd6b09652023-11-21 09:33:54 +00001372 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 +00001373 mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1374
1375cleanup:
1376
1377 mbedtls_mpi_free(&RR);
1378 mbedtls_mpi_free(&M_T);
1379
1380 return ret;
1381}
1382
1383/*
Janos Follathe81102e2017-03-22 13:38:28 +00001384 * Exponent blinding supposed to prevent side-channel attacks using multiple
1385 * traces of measurements to recover the RSA key. The more collisions are there,
1386 * the more bits of the key can be recovered. See [3].
1387 *
1388 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001389 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001390 *
1391 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001392 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001393 *
1394 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1395 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1396 * Thus in this sense with 28 byte blinding the security is not reduced by
1397 * side-channel attacks like the one in [3])
1398 *
1399 * This countermeasure does not help if the key recovery is possible with a
1400 * single trace.
1401 */
1402#define RSA_EXPONENT_BLINDING 28
1403
1404/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 * Do an RSA private key operation
1406 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001407int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1408 int (*f_rng)(void *, unsigned char *, size_t),
1409 void *p_rng,
1410 const unsigned char *input,
1411 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001412{
Janos Follath24eed8d2019-11-22 13:21:35 +00001413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001414 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001415
1416 /* Temporary holding the result */
1417 mbedtls_mpi T;
1418
1419 /* Temporaries holding P-1, Q-1 and the
1420 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001421 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001422
1423#if !defined(MBEDTLS_RSA_NO_CRT)
1424 /* Temporaries holding the results mod p resp. mod q. */
1425 mbedtls_mpi TP, TQ;
1426
1427 /* Temporaries holding the blinded exponents for
1428 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001429 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001430#else
1431 /* Temporary holding the blinded exponent (if used). */
1432 mbedtls_mpi D_blind;
Hanno Becker43f94722017-08-25 11:50:00 +01001433#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001434
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001435 /* Temporaries holding the initial input and the double
1436 * checked result; should be the same in the end. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001437 mbedtls_mpi input_blinded, check_result_blinded;
Paul Bakker5121ce52009-01-03 21:22:43 +00001438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (f_rng == NULL) {
1440 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1441 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (rsa_check_context(ctx, 1 /* private key checks */,
1444 1 /* blinding on */) != 0) {
1445 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001446 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001447
Hanno Becker06811ce2017-05-03 15:10:34 +01001448#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1450 return ret;
1451 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001452#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001453
Hanno Becker06811ce2017-05-03 15:10:34 +01001454 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 mbedtls_mpi_init(&P1);
1458 mbedtls_mpi_init(&Q1);
1459 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001460
Janos Follathe81102e2017-03-22 13:38:28 +00001461#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001463#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 mbedtls_mpi_init(&DP_blind);
1465 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001466#endif
1467
Hanno Becker06811ce2017-05-03 15:10:34 +01001468#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001470#endif
1471
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001472 mbedtls_mpi_init(&input_blinded);
1473 mbedtls_mpi_init(&check_result_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001474
1475 /* End of MPI initialization */
1476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1478 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001479 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1480 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 }
1482
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001483 /*
1484 * Blinding
1485 * T = T * Vi mod N
1486 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1488 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1489 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001490
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001491 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&input_blinded, &T));
Janos Follath6bcbc922023-11-21 09:46:43 +00001492
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001493 /*
1494 * Exponent blinding
1495 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1497 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001498
Janos Follathf9203b42017-03-22 15:13:15 +00001499#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001500 /*
1501 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1504 f_rng, p_rng));
1505 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1506 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1507 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathf9203b42017-03-22 15:13:15 +00001508#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001509 /*
1510 * DP_blind = ( P - 1 ) * R + DP
1511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1513 f_rng, p_rng));
1514 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1515 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1516 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001517
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001518 /*
1519 * DQ_blind = ( Q - 1 ) * R + DQ
1520 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1522 f_rng, p_rng));
1523 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1524 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1525 &ctx->DQ));
Janos Follathe81102e2017-03-22 13:38:28 +00001526#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath47ee7702023-12-27 10:33:00 +00001529 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &D_blind, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001530#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001531 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001532 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001534 * TP = input ^ dP mod P
1535 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001536 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001537
Janos Follath47ee7702023-12-27 10:33:00 +00001538 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, &DP_blind, &ctx->P, &ctx->RP));
1539 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, &DQ_blind, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
1541 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001542 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1545 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1546 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001547
1548 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001549 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001550 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1552 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001554
Hanno Becker2dec5e82017-10-03 07:49:52 +01001555 /* Verify the result to prevent glitching attacks. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001556 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&check_result_blinded, &T, &ctx->E,
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 &ctx->N, &ctx->RN));
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001558 if (mbedtls_mpi_cmp_mpi(&check_result_blinded, &input_blinded) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001559 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1560 goto cleanup;
1561 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001562
Janos Follath6bcbc922023-11-21 09:46:43 +00001563 /*
1564 * Unblind
1565 * T = T * Vf mod N
1566 */
1567 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1568
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
1572cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1575 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1576 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001577#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 mbedtls_mpi_free(&P1);
1580 mbedtls_mpi_free(&Q1);
1581 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001582
Janos Follathe81102e2017-03-22 13:38:28 +00001583#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001585#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 mbedtls_mpi_free(&DP_blind);
1587 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001588#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001591
1592#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001594#endif
1595
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001596 mbedtls_mpi_free(&check_result_blinded);
1597 mbedtls_mpi_free(&input_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 if (ret != 0 && ret >= -0x007f) {
1600 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001604}
1605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001607/**
1608 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1609 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001610 * \param dst buffer to mask
1611 * \param dlen length of destination buffer
1612 * \param src source of the mask generation
1613 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001614 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001615 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001616static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1617 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001618{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001619 unsigned char counter[4];
1620 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001621 unsigned int hlen;
1622 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001623 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001624 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001625 const mbedtls_md_info_t *md_info;
1626 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 mbedtls_md_init(&md_ctx);
1629 md_info = mbedtls_md_info_from_type(md_alg);
1630 if (md_info == NULL) {
1631 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1632 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 mbedtls_md_init(&md_ctx);
1635 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001636 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 memset(mask, 0, sizeof(mask));
1642 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001643
Simon Butcher02037452016-03-01 21:19:12 +00001644 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001645 p = dst;
1646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001648 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001650 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001654 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 }
1656 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001657 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 }
1659 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001660 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 }
1662 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001663 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001667 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001669
1670 counter[3]++;
1671
1672 dlen -= use_len;
1673 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001674
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001675exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001680}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001681
1682/**
1683 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1684 *
1685 * \param hash the input hash
1686 * \param hlen length of the input hash
1687 * \param salt the input salt
1688 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001689 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001690 * \param md_alg message digest to use
1691 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001692static int hash_mprime(const unsigned char *hash, size_t hlen,
1693 const unsigned char *salt, size_t slen,
1694 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001695{
1696 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001697
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001698 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1702 if (md_info == NULL) {
1703 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1704 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 mbedtls_md_init(&md_ctx);
1707 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001708 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 }
1710 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001711 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 }
1713 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001714 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 }
1716 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001717 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 }
1719 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001720 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 }
1722 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001723 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001725
1726exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001730}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001731
1732/**
1733 * Compute a hash.
1734 *
1735 * \param md_alg algorithm to use
1736 * \param input input message to hash
1737 * \param ilen input length
1738 * \param output the output buffer - must be large enough for \p md_alg
1739 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001740static int compute_hash(mbedtls_md_type_t md_alg,
1741 const unsigned char *input, size_t ilen,
1742 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001743{
1744 const mbedtls_md_info_t *md_info;
1745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 md_info = mbedtls_md_info_from_type(md_alg);
1747 if (md_info == NULL) {
1748 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1749 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001752}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001756/*
1757 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1758 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001759int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1760 int (*f_rng)(void *, unsigned char *, size_t),
1761 void *p_rng,
1762 const unsigned char *label, size_t label_len,
1763 size_t ilen,
1764 const unsigned char *input,
1765 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001766{
1767 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001769 unsigned char *p = output;
1770 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 if (f_rng == NULL) {
1773 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1774 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001775
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001776 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 if (hlen == 0) {
1778 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1779 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001780
1781 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001782
Simon Butcher02037452016-03-01 21:19:12 +00001783 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1785 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1786 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001789
1790 *p++ = 0;
1791
Simon Butcher02037452016-03-01 21:19:12 +00001792 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1794 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1795 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001796
1797 p += hlen;
1798
Simon Butcher02037452016-03-01 21:19:12 +00001799 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1801 if (ret != 0) {
1802 return ret;
1803 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001804 p += hlen;
1805 p += olen - 2 * hlen - 2 - ilen;
1806 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 if (ilen != 0) {
1808 memcpy(p, input, ilen);
1809 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001810
Simon Butcher02037452016-03-01 21:19:12 +00001811 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001813 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 return ret;
1815 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001816
Simon Butcher02037452016-03-01 21:19:12 +00001817 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001819 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 return ret;
1821 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001824}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001828/*
1829 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1830 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001831int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1832 int (*f_rng)(void *, unsigned char *, size_t),
1833 void *p_rng, size_t ilen,
1834 const unsigned char *input,
1835 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001836{
1837 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001839 unsigned char *p = output;
1840
Paul Bakkerb3869132013-02-28 17:21:01 +01001841 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001842
Simon Butcher02037452016-03-01 21:19:12 +00001843 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if (ilen + 11 < ilen || olen < ilen + 11) {
1845 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1846 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001847
1848 nb_pad = olen - 3 - ilen;
1849
1850 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 if (f_rng == NULL) {
1853 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1854 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001855
1856 *p++ = MBEDTLS_RSA_CRYPT;
1857
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001859 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001860
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001861 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 ret = f_rng(p_rng, p, 1);
1863 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001864
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001865 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 if (rng_dl == 0 || ret != 0) {
1867 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1868 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001869
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001870 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001871 }
1872
1873 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 if (ilen != 0) {
1875 memcpy(p, input, ilen);
1876 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001879}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001881
Paul Bakker5121ce52009-01-03 21:22:43 +00001882/*
1883 * Add the message padding, then do an RSA operation
1884 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001885int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1886 int (*f_rng)(void *, unsigned char *, size_t),
1887 void *p_rng,
1888 size_t ilen,
1889 const unsigned char *input,
1890 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001891{
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893#if defined(MBEDTLS_PKCS1_V15)
1894 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1896 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001897#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899#if defined(MBEDTLS_PKCS1_V21)
1900 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1902 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001903#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001904
1905 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001908}
1909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001911/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001912 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001913 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001914int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1915 int (*f_rng)(void *, unsigned char *, size_t),
1916 void *p_rng,
1917 const unsigned char *label, size_t label_len,
1918 size_t *olen,
1919 const unsigned char *input,
1920 unsigned char *output,
1921 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001922{
Janos Follath24eed8d2019-11-22 13:21:35 +00001923 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001924 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001925 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001926 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001928 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001929 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001930
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001931 /*
1932 * Parameters sanity checks
1933 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1935 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1936 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001937
1938 ilen = ctx->len;
1939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if (ilen < 16 || ilen > sizeof(buf)) {
1941 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1942 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001943
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001944 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 if (hlen == 0) {
1946 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1947 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001948
Janos Follathc17cda12016-02-11 11:08:18 +00001949 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (2 * hlen + 2 > ilen) {
1951 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1952 }
Janos Follathc17cda12016-02-11 11:08:18 +00001953
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001954 /*
1955 * RSA operation
1956 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001960 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001962
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001963 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001964 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001965 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001966 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001968 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 /* DB: Apply dbMask to maskedDB */
1970 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001971 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001972 goto cleanup;
1973 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001974
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001975 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1977 label, label_len, lhash);
1978 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001979 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001981
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001982 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001983 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001984 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001985 p = buf;
1986
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001987 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001988
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001989 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001990
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001991 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001992 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001993 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001994
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001995 /* Get zero-padding len, but always read till end of buffer
1996 * (minus one, for the 01 byte) */
1997 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001998 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01002000 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
2001 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002002 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002003
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002004 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002005 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01002006
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002007 /*
2008 * The only information "leaked" is whether the padding was correct or not
2009 * (eg, no data is copied if it was not correct). This meets the
2010 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
2011 * the different error conditions.
2012 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002013 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002014 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2015 goto cleanup;
2016 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002017
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002018 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002019 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
2020 goto cleanup;
2021 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002022
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002023 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (*olen != 0) {
2025 memcpy(output, p, *olen);
2026 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002027 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002028
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002029cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 mbedtls_platform_zeroize(buf, sizeof(buf));
2031 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002038/*
2039 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
2040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002041int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
2042 int (*f_rng)(void *, unsigned char *, size_t),
2043 void *p_rng,
2044 size_t *olen,
2045 const unsigned char *input,
2046 unsigned char *output,
2047 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002048{
2049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2050 size_t ilen;
2051 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
2052
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002053 ilen = ctx->len;
2054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2056 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2057 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (ilen < 16 || ilen > sizeof(buf)) {
2060 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2061 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002066 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
2070 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002071
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002072cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002076}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002078
2079/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002080 * Do an RSA operation, then remove the message padding
2081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002082int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
2083 int (*f_rng)(void *, unsigned char *, size_t),
2084 void *p_rng,
2085 size_t *olen,
2086 const unsigned char *input,
2087 unsigned char *output,
2088 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01002089{
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#if defined(MBEDTLS_PKCS1_V15)
2092 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
2094 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02002095#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#if defined(MBEDTLS_PKCS1_V21)
2098 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
2100 olen, input, output,
2101 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01002102#endif
2103
2104 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002106 }
2107}
2108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002110static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2111 int (*f_rng)(void *, unsigned char *, size_t),
2112 void *p_rng,
2113 mbedtls_md_type_t md_alg,
2114 unsigned int hashlen,
2115 const unsigned char *hash,
2116 int saltlen,
2117 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002118{
2119 size_t olen;
2120 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002121 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002122 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00002123 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002124 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002125 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002128 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 if (f_rng == NULL) {
2132 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2133 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002134
2135 olen = ctx->len;
2136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002138 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002139 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (exp_hashlen == 0) {
2141 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2142 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 if (hashlen != exp_hashlen) {
2145 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2146 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002147 }
2148
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002149 hash_id = (mbedtls_md_type_t) ctx->hash_id;
2150 if (hash_id == MBEDTLS_MD_NONE) {
2151 hash_id = md_alg;
2152 }
2153 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (hlen == 0) {
2155 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2156 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
2159 /* Calculate the largest possible salt length, up to the hash size.
2160 * Normally this is the hash length, which is the maximum salt length
2161 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
2162 * enough room, use the maximum salt length that fits. The constraint is
2163 * that the hash length plus the salt length plus 2 bytes must be at most
2164 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
2165 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002166 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (olen < hlen + min_slen + 2) {
2168 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2169 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002170 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002172 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 }
2174 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
2175 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2176 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02002177 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002178 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01002181
Simon Butcher02037452016-03-01 21:19:12 +00002182 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002184 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01002185 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002186
2187 /* Generate salt of length slen in place in the encoded message */
2188 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
2190 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
2191 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02002192
Paul Bakkerb3869132013-02-28 17:21:01 +01002193 p += slen;
2194
Simon Butcher02037452016-03-01 21:19:12 +00002195 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002196 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (ret != 0) {
2198 return ret;
2199 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002200
Simon Butcher02037452016-03-01 21:19:12 +00002201 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002203 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002205
Simon Butcher02037452016-03-01 21:19:12 +00002206 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002207 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 if (ret != 0) {
2209 return ret;
2210 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2213 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01002214
2215 p += hlen;
2216 *p++ = 0xBC;
2217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002219}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002220
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002221static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2222 int (*f_rng)(void *, unsigned char *, size_t),
2223 void *p_rng,
2224 mbedtls_md_type_t md_alg,
2225 unsigned int hashlen,
2226 const unsigned char *hash,
2227 int saltlen,
2228 unsigned char *sig)
2229{
2230 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2231 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2232 }
Valerio Settie700d802024-02-26 13:52:34 +01002233 if ((ctx->hash_id == MBEDTLS_MD_NONE) && (md_alg == MBEDTLS_MD_NONE)) {
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002234 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2235 }
2236 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
2237 sig);
2238}
2239
2240int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2241 int (*f_rng)(void *, unsigned char *, size_t),
2242 void *p_rng,
2243 mbedtls_md_type_t md_alg,
2244 unsigned int hashlen,
2245 const unsigned char *hash,
2246 unsigned char *sig)
2247{
2248 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
2249 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2250}
2251
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002252/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02002253 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
2254 * the option to pass in the salt length.
2255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002256int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
2257 int (*f_rng)(void *, unsigned char *, size_t),
2258 void *p_rng,
2259 mbedtls_md_type_t md_alg,
2260 unsigned int hashlen,
2261 const unsigned char *hash,
2262 int saltlen,
2263 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02002264{
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2266 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02002267}
2268
Cédric Meuterf3fab332020-04-25 11:30:45 +02002269/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002270 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
2271 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002272int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2273 int (*f_rng)(void *, unsigned char *, size_t),
2274 void *p_rng,
2275 mbedtls_md_type_t md_alg,
2276 unsigned int hashlen,
2277 const unsigned char *hash,
2278 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002279{
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2281 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002282}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002286/*
2287 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
2288 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002289
2290/* Construct a PKCS v1.5 encoding of a hashed message
2291 *
2292 * This is used both for signature generation and verification.
2293 *
2294 * Parameters:
2295 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002296 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002297 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002298 * - hash: Buffer containing the hashed message or the raw data.
2299 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002300 * - dst: Buffer to hold the encoded message.
2301 *
2302 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002303 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002304 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002305 *
2306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002307static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
2308 unsigned int hashlen,
2309 const unsigned char *hash,
2310 size_t dst_len,
2311 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01002312{
2313 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002314 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002315 unsigned char *p = dst;
2316 const char *oid = NULL;
2317
2318 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002320 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 if (md_size == 0) {
2322 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2323 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2326 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2327 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if (hashlen != md_size) {
2330 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2331 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002332
2333 /* Double-check that 8 + hashlen + oid_size can be used as a
2334 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01002336 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 10 + hashlen + oid_size < 10 + hashlen) {
2338 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2339 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002340
2341 /*
2342 * Static bounds check:
2343 * - Need 10 bytes for five tag-length pairs.
2344 * (Insist on 1-byte length encodings to protect against variants of
2345 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2346 * - Need hashlen bytes for hash
2347 * - Need oid_size bytes for hash alg OID.
2348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 if (nb_pad < 10 + hashlen + oid_size) {
2350 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2351 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002352 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 } else {
2354 if (nb_pad < hashlen) {
2355 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2356 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002357
2358 nb_pad -= hashlen;
2359 }
2360
Hanno Becker2b2f8982017-09-27 17:10:03 +01002361 /* Need space for signature header and padding delimiter (3 bytes),
2362 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 if (nb_pad < 3 + 8) {
2364 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2365 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002366 nb_pad -= 3;
2367
2368 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002369 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002370
2371 /* Write signature header and padding */
2372 *p++ = 0;
2373 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002375 p += nb_pad;
2376 *p++ = 0;
2377
2378 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 if (md_alg == MBEDTLS_MD_NONE) {
2380 memcpy(p, hash, hashlen);
2381 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002382 }
2383
2384 /* Signing hashed data, add corresponding ASN.1 structure
2385 *
2386 * DigestInfo ::= SEQUENCE {
2387 * digestAlgorithm DigestAlgorithmIdentifier,
2388 * digest Digest }
2389 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2390 * Digest ::= OCTET STRING
2391 *
2392 * Schematic:
2393 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2394 * TAG-NULL + LEN [ NULL ] ]
2395 * TAG-OCTET + LEN [ HASH ] ]
2396 */
2397 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002399 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002401 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002402 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002404 p += oid_size;
2405 *p++ = MBEDTLS_ASN1_NULL;
2406 *p++ = 0x00;
2407 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002408 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002410 p += hashlen;
2411
2412 /* Just a sanity-check, should be automatic
2413 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if (p != dst + dst_len) {
2415 mbedtls_platform_zeroize(dst, dst_len);
2416 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002417 }
2418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002420}
2421
Paul Bakkerb3869132013-02-28 17:21:01 +01002422/*
2423 * Do an RSA operation to sign the message digest
2424 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2426 int (*f_rng)(void *, unsigned char *, size_t),
2427 void *p_rng,
2428 mbedtls_md_type_t md_alg,
2429 unsigned int hashlen,
2430 const unsigned char *hash,
2431 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002432{
Janos Follath24eed8d2019-11-22 13:21:35 +00002433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002434 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002437 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2441 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2442 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002443
Hanno Beckerfdf38032017-09-06 12:35:55 +01002444 /*
2445 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2446 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2449 ctx->len, sig)) != 0) {
2450 return ret;
2451 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002452
Hanno Beckerfdf38032017-09-06 12:35:55 +01002453 /* Private key operation
2454 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002455 * In order to prevent Lenstra's attack, make the signature in a
2456 * temporary buffer and check it before returning it.
2457 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 sig_try = mbedtls_calloc(1, ctx->len);
2460 if (sig_try == NULL) {
2461 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002462 }
2463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 verif = mbedtls_calloc(1, ctx->len);
2465 if (verif == NULL) {
2466 mbedtls_free(sig_try);
2467 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2468 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2471 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2472
2473 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002474 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2475 goto cleanup;
2476 }
2477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002479
2480cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002481 mbedtls_zeroize_and_free(sig_try, ctx->len);
2482 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 if (ret != 0) {
2485 memset(sig, '!', ctx->len);
2486 }
2487 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002490
2491/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 * Do an RSA operation to sign the message digest
2493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2495 int (*f_rng)(void *, unsigned char *, size_t),
2496 void *p_rng,
2497 mbedtls_md_type_t md_alg,
2498 unsigned int hashlen,
2499 const unsigned char *hash,
2500 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002501{
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002503 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#if defined(MBEDTLS_PKCS1_V15)
2508 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2510 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002511#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#if defined(MBEDTLS_PKCS1_V21)
2514 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2516 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002517#endif
2518
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002522}
2523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002525/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002526 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002528int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2529 mbedtls_md_type_t md_alg,
2530 unsigned int hashlen,
2531 const unsigned char *hash,
2532 mbedtls_md_type_t mgf1_hash_id,
2533 int expected_salt_len,
2534 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002535{
Janos Follath24eed8d2019-11-22 13:21:35 +00002536 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002537 size_t siglen;
2538 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002539 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002540 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002541 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002542 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002546 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002548
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 siglen = ctx->len;
2550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 if (siglen < 16 || siglen > sizeof(buf)) {
2552 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2553 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 if (ret != 0) {
2558 return ret;
2559 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
2561 p = buf;
2562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (buf[siglen - 1] != 0xBC) {
2564 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002565 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (md_alg != MBEDTLS_MD_NONE) {
2568 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002569 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (exp_hashlen == 0) {
2571 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2572 }
2573
2574 if (hashlen != exp_hashlen) {
2575 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2576 }
2577 }
2578
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002579 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 if (hlen == 0) {
2581 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2582 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002583
Simon Butcher02037452016-03-01 21:19:12 +00002584 /*
2585 * Note: EMSA-PSS verification is over the length of N - 1 bits
2586 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002588
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 if (buf[0] >> (8 - siglen * 8 + msb)) {
2590 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2591 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002592
Simon Butcher02037452016-03-01 21:19:12 +00002593 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002595 p++;
2596 siglen -= 1;
2597 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (siglen < hlen + 2) {
2600 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2601 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002602 hash_start = p + siglen - hlen - 1;
2603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2605 if (ret != 0) {
2606 return ret;
2607 }
Paul Bakker02303e82013-01-03 11:08:31 +01002608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002612 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 if (*p++ != 0x01) {
2616 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2617 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002618
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002619 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2622 observed_salt_len != (size_t) expected_salt_len) {
2623 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002624 }
2625
Simon Butcher02037452016-03-01 21:19:12 +00002626 /*
2627 * Generate H = Hash( M' )
2628 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2630 result, mgf1_hash_id);
2631 if (ret != 0) {
2632 return ret;
2633 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 if (memcmp(hash_start, result, hlen) != 0) {
2636 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2637 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002640}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002641
2642/*
2643 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2644 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002645int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2646 mbedtls_md_type_t md_alg,
2647 unsigned int hashlen,
2648 const unsigned char *hash,
2649 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002650{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002651 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002653 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002658 : md_alg;
2659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2661 md_alg, hashlen, hash,
2662 mgf1_hash_id,
2663 MBEDTLS_RSA_SALT_LEN_ANY,
2664 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002665
2666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002670/*
2671 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2672 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002673int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2674 mbedtls_md_type_t md_alg,
2675 unsigned int hashlen,
2676 const unsigned char *hash,
2677 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002678{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002679 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002680 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002681 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002684 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002686
2687 sig_len = ctx->len;
2688
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002689 /*
2690 * Prepare expected PKCS1 v1.5 encoding of hash.
2691 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2694 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002695 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2696 goto cleanup;
2697 }
2698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2700 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002701 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002703
2704 /*
2705 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2706 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 ret = mbedtls_rsa_public(ctx, sig, encoded);
2709 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002710 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002712
Simon Butcher02037452016-03-01 21:19:12 +00002713 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002714 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002715 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2718 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002719 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2720 goto cleanup;
2721 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002722
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002723cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002726 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002727 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002730 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002731 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002734}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
2737/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002738 * Do an RSA operation and check the message digest
2739 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002740int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2741 mbedtls_md_type_t md_alg,
2742 unsigned int hashlen,
2743 const unsigned char *hash,
2744 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002745{
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002747 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751#if defined(MBEDTLS_PKCS1_V15)
2752 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2754 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002755#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757#if defined(MBEDTLS_PKCS1_V21)
2758 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2760 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002761#endif
2762
2763 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002765 }
2766}
2767
2768/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002769 * Copy the components of an RSA key
2770 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002771int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002772{
Janos Follath24eed8d2019-11-22 13:21:35 +00002773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002774
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002775 dst->len = src->len;
2776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2778 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002779
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2781 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2782 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002783
2784#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2786 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2787 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2788 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2789 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002790#endif
2791
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002793
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2795 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002796
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002797 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002798 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002799
2800cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 if (ret != 0) {
2802 mbedtls_rsa_free(dst);
2803 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002806}
2807
2808/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 * Free the components of an RSA key
2810 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002811void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002812{
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002814 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002816
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 mbedtls_mpi_free(&ctx->Vi);
2818 mbedtls_mpi_free(&ctx->Vf);
2819 mbedtls_mpi_free(&ctx->RN);
2820 mbedtls_mpi_free(&ctx->D);
2821 mbedtls_mpi_free(&ctx->Q);
2822 mbedtls_mpi_free(&ctx->P);
2823 mbedtls_mpi_free(&ctx->E);
2824 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002825
Hanno Becker33c30a02017-08-23 07:00:22 +01002826#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 mbedtls_mpi_free(&ctx->RQ);
2828 mbedtls_mpi_free(&ctx->RP);
2829 mbedtls_mpi_free(&ctx->QP);
2830 mbedtls_mpi_free(&ctx->DQ);
2831 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002832#endif /* MBEDTLS_RSA_NO_CRT */
2833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002835 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 if (ctx->ver != 0) {
2837 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002838 ctx->ver = 0;
2839 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002840#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002841}
2842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
2846/*
2847 * Example RSA-1024 keypair, for test purposes
2848 */
2849#define KEY_LEN 128
2850
2851#define RSA_N "9292758453063D803DD603D5E777D788" \
2852 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2853 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2854 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2855 "93A89813FBF3C4F8066D2D800F7C38A8" \
2856 "1AE31942917403FF4946B0A83D3D3E05" \
2857 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2858 "5E94BB77B07507233A0BC7BAC8F90F79"
2859
2860#define RSA_E "10001"
2861
2862#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2863 "66CA472BC44D253102F8B4A9D3BFA750" \
2864 "91386C0077937FE33FA3252D28855837" \
2865 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2866 "DF79C5CE07EE72C7F123142198164234" \
2867 "CABB724CF78B8173B9F880FC86322407" \
2868 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2869 "071513A1E85B5DFA031F21ECAE91A34D"
2870
2871#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2872 "2C01CAD19EA484A87EA4377637E75500" \
2873 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2874 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2875
2876#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2877 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2878 "910E4168387E3C30AA1E00C339A79508" \
2879 "8452DD96A9A5EA5D9DCA68DA636032AF"
2880
Paul Bakker5121ce52009-01-03 21:22:43 +00002881#define PT_LEN 24
2882#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2883 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002886static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002887{
gufe44c2620da2020-08-03 17:56:50 +02002888#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002889 size_t i;
2890
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002892 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 }
Paul Bakker545570e2010-07-18 09:00:25 +00002894
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002896 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002898#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002900 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002904#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002909
Paul Bakker5121ce52009-01-03 21:22:43 +00002910/*
2911 * Checkup routine
2912 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002913int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002914{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002915 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002917 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002919 unsigned char rsa_plaintext[PT_LEN];
2920 unsigned char rsa_decrypted[PT_LEN];
2921 unsigned char rsa_ciphertext[KEY_LEN];
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01002922#if defined(PSA_WANT_ALG_SHA_1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002923 unsigned char sha1sum[20];
2924#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
Hanno Becker3a701162017-08-22 13:52:43 +01002926 mbedtls_mpi K;
2927
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 mbedtls_mpi_init(&K);
2929 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2932 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2933 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2934 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2935 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2936 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2937 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2938 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2939 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2940 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 if (verbose != 0) {
2945 mbedtls_printf(" RSA key validation: ");
2946 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2949 mbedtls_rsa_check_privkey(&rsa) != 0) {
2950 if (verbose != 0) {
2951 mbedtls_printf("failed\n");
2952 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Hanno Becker5bc87292017-05-03 15:09:31 +01002954 ret = 1;
2955 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002956 }
2957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 if (verbose != 0) {
2959 mbedtls_printf("passed\n PKCS#1 encryption : ");
2960 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2965 PT_LEN, rsa_plaintext,
2966 rsa_ciphertext) != 0) {
2967 if (verbose != 0) {
2968 mbedtls_printf("failed\n");
2969 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002970
Hanno Becker5bc87292017-05-03 15:09:31 +01002971 ret = 1;
2972 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002973 }
2974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 if (verbose != 0) {
2976 mbedtls_printf("passed\n PKCS#1 decryption : ");
2977 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2980 &len, rsa_ciphertext, rsa_decrypted,
2981 sizeof(rsa_decrypted)) != 0) {
2982 if (verbose != 0) {
2983 mbedtls_printf("failed\n");
2984 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002985
Hanno Becker5bc87292017-05-03 15:09:31 +01002986 ret = 1;
2987 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002988 }
2989
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2991 if (verbose != 0) {
2992 mbedtls_printf("failed\n");
2993 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002994
Hanno Becker5bc87292017-05-03 15:09:31 +01002995 ret = 1;
2996 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 }
2998
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 if (verbose != 0) {
3000 mbedtls_printf("passed\n");
3001 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003002
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01003003#if defined(PSA_WANT_ALG_SHA_1)
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 if (verbose != 0) {
3005 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01003006 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003007
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01003008 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
3009 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 if (verbose != 0) {
3011 mbedtls_printf("failed\n");
3012 }
3013
3014 return 1;
3015 }
3016
3017 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
3018 MBEDTLS_MD_SHA1, 20,
3019 sha1sum, rsa_ciphertext) != 0) {
3020 if (verbose != 0) {
3021 mbedtls_printf("failed\n");
3022 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Hanno Becker5bc87292017-05-03 15:09:31 +01003024 ret = 1;
3025 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 }
3027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 if (verbose != 0) {
3029 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
3030 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
3033 sha1sum, rsa_ciphertext) != 0) {
3034 if (verbose != 0) {
3035 mbedtls_printf("failed\n");
3036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003037
Hanno Becker5bc87292017-05-03 15:09:31 +01003038 ret = 1;
3039 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003040 }
3041
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 if (verbose != 0) {
3043 mbedtls_printf("passed\n");
3044 }
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01003045#endif /* PSA_WANT_ALG_SHA_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 if (verbose != 0) {
3048 mbedtls_printf("\n");
3049 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003050
Paul Bakker3d8fb632014-04-17 12:42:41 +02003051cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 mbedtls_mpi_free(&K);
3053 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02003055 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003058}
3059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062#endif /* MBEDTLS_RSA_C */