blob: 557faaf36354792830d343f372dff3b5cdb51f84 [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 Follath5f316972024-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"
Janos Follath24eed8d2019-11-22 13:21:35 +000038#include "mbedtls/error.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
159#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
160 /*
161 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
162 * that they can be easily recomputed from D, P and Q. However by
163 * parsing them from the PKCS1 structure it is possible to avoid
164 * recalculating them which both reduces the overhead of loading
165 * RSA private keys into memory and also avoids side channels which
166 * can arise when computing those values, since all of D, P, and Q
167 * are secret. See https://eprint.iacr.org/2020/055 for a
168 * description of one such attack.
169 */
170
171 /* Import DP */
172 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
173 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
174 goto cleanup;
175 }
176
177 /* Import DQ */
178 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
179 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
180 goto cleanup;
181 }
182
183 /* Import QP */
184 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
185 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
186 goto cleanup;
187 }
188
189#else
190 /* Verify existence of the CRT params */
191 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
192 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
193 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
194 goto cleanup;
195 }
196#endif
197
198 /* rsa_complete() doesn't complete anything with the default
199 * implementation but is still called:
200 * - for the benefit of alternative implementation that may want to
201 * pre-compute stuff beyond what's provided (eg Montgomery factors)
202 * - as is also sanity-checks the key
203 *
204 * Furthermore, we also check the public part for consistency with
205 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
206 */
207 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
208 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
209 goto cleanup;
210 }
211
212 if (p != end) {
213 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
214 }
215
216cleanup:
217
218 mbedtls_mpi_free(&T);
219
220 if (ret != 0) {
221 mbedtls_rsa_free(rsa);
222 }
223
224 return ret;
225}
226
Valerio Setti201e6432024-02-01 17:19:37 +0100227int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +0100228{
Valerio Setti201e6432024-02-01 17:19:37 +0100229 unsigned char *p = (unsigned char *) key;
230 unsigned char *end = (unsigned char *) (key + keylen);
Valerio Setti44ff9502024-02-01 16:51:05 +0100231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
232 size_t len;
233
234 /*
235 * RSAPublicKey ::= SEQUENCE {
236 * modulus INTEGER, -- n
237 * publicExponent INTEGER -- e
238 * }
239 */
240
Valerio Setti201e6432024-02-01 17:19:37 +0100241 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
Valerio Setti44ff9502024-02-01 16:51:05 +0100242 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
243 return ret;
244 }
245
Valerio 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
415#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
416
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
568#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
569
Hanno Beckera565f542017-10-11 11:00:19 +0100570#if !defined(MBEDTLS_RSA_ALT)
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
573 const mbedtls_mpi *N,
574 const mbedtls_mpi *P, const mbedtls_mpi *Q,
575 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100576{
Janos Follath24eed8d2019-11-22 13:21:35 +0000577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
580 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
581 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
582 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
583 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
584 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100585 }
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (N != NULL) {
588 ctx->len = mbedtls_mpi_size(&ctx->N);
589 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100592}
593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
595 unsigned char const *N, size_t N_len,
596 unsigned char const *P, size_t P_len,
597 unsigned char const *Q, size_t Q_len,
598 unsigned char const *D, size_t D_len,
599 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100600{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000601 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (N != NULL) {
604 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
605 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100606 }
607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (P != NULL) {
609 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
610 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (Q != NULL) {
613 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
614 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (D != NULL) {
617 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
618 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (E != NULL) {
621 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
622 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100623
624cleanup:
625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (ret != 0) {
627 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
628 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100631}
632
Hanno Becker705fc682017-10-10 17:57:02 +0100633/*
634 * Checks whether the context fields are set in such a way
635 * that the RSA primitives will be able to execute without error.
636 * It does *not* make guarantees for consistency of the parameters.
637 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100638static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
639 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100640{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100641#if !defined(MBEDTLS_RSA_NO_CRT)
642 /* blinding_needed is only used for NO_CRT to decide whether
643 * P,Q need to be present or not. */
644 ((void) blinding_needed);
645#endif
646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
648 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
649 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000650 }
Hanno Becker705fc682017-10-10 17:57:02 +0100651
652 /*
653 * 1. Modular exponentiation needs positive, odd moduli.
654 */
655
656 /* Modular exponentiation wrt. N is always used for
657 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
659 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
660 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100661 }
662
663#if !defined(MBEDTLS_RSA_NO_CRT)
664 /* Modular exponentiation for P and Q is only
665 * used for private key operations and if CRT
666 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (is_priv &&
668 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
669 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
670 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
671 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
672 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100673 }
674#endif /* !MBEDTLS_RSA_NO_CRT */
675
676 /*
677 * 2. Exponents must be positive
678 */
679
680 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
682 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
683 }
Hanno Becker705fc682017-10-10 17:57:02 +0100684
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100685#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100686 /* For private key operations, use D or DP & DQ
687 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
689 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
690 }
Hanno Becker705fc682017-10-10 17:57:02 +0100691#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (is_priv &&
693 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
694 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
695 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100696 }
697#endif /* MBEDTLS_RSA_NO_CRT */
698
699 /* Blinding shouldn't make exponents negative either,
700 * so check that P, Q >= 1 if that hasn't yet been
701 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100702#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (is_priv && blinding_needed &&
704 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
705 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
706 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100707 }
708#endif
709
710 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100711 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100712#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (is_priv &&
714 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
715 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100716 }
717#endif
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100720}
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100723{
724 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500725 int have_N, have_P, have_Q, have_D, have_E;
726#if !defined(MBEDTLS_RSA_NO_CRT)
727 int have_DP, have_DQ, have_QP;
728#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500729 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
732 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
733 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
734 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
735 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500736
737#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
739 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
740 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500741#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100742
Hanno Becker617c1ae2017-08-23 14:11:24 +0100743 /*
744 * Check whether provided parameters are enough
745 * to deduce all others. The following incomplete
746 * parameter sets for private keys are supported:
747 *
748 * (1) P, Q missing.
749 * (2) D and potentially N missing.
750 *
751 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100752
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500753 n_missing = have_P && have_Q && have_D && have_E;
754 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
755 d_missing = have_P && have_Q && !have_D && have_E;
756 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100757
758 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500759 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (!is_priv && !is_pub) {
762 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
763 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100764
765 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100766 * Step 1: Deduce N if P, Q are provided.
767 */
768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (!have_N && have_P && have_Q) {
770 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
771 &ctx->Q)) != 0) {
772 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100773 }
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100776 }
777
778 /*
779 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100780 */
781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (pq_missing) {
783 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
784 &ctx->P, &ctx->Q);
785 if (ret != 0) {
786 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
787 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 } else if (d_missing) {
790 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
791 &ctx->Q,
792 &ctx->E,
793 &ctx->D)) != 0) {
794 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100795 }
796 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100797
Hanno Becker617c1ae2017-08-23 14:11:24 +0100798 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100799 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100800 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100801 */
802
Hanno Becker23344b52017-08-23 07:43:27 +0100803#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (is_priv && !(have_DP && have_DQ && have_QP)) {
805 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
806 &ctx->DP, &ctx->DQ, &ctx->QP);
807 if (ret != 0) {
808 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
809 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100810 }
Hanno Becker23344b52017-08-23 07:43:27 +0100811#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100812
813 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100814 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100815 */
816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100818}
819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
821 unsigned char *N, size_t N_len,
822 unsigned char *P, size_t P_len,
823 unsigned char *Q, size_t Q_len,
824 unsigned char *D, size_t D_len,
825 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100826{
827 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500828 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100829
830 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500831 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
833 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
834 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
835 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
836 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100839 /* If we're trying to export private parameters for a public key,
840 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (P != NULL || Q != NULL || D != NULL) {
842 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
843 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100844
845 }
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (N != NULL) {
848 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
849 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (P != NULL) {
852 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
853 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (Q != NULL) {
856 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
857 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (D != NULL) {
860 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
861 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if (E != NULL) {
864 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
865 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100866
867cleanup:
868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100870}
871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
873 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
874 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100875{
Janos Follath24eed8d2019-11-22 13:21:35 +0000876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500877 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100878
879 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500880 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
882 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
883 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
884 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
885 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100888 /* If we're trying to export private parameters for a public key,
889 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (P != NULL || Q != NULL || D != NULL) {
891 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
892 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100893
894 }
895
896 /* Export all requested core parameters. */
897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
899 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
900 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
901 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
902 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
903 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100904 }
905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100907}
908
909/*
910 * Export CRT parameters
911 * This must also be implemented if CRT is not used, for being able to
912 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
913 * can be used in this case.
914 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
916 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100917{
Janos Follath24eed8d2019-11-22 13:21:35 +0000918 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500919 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100920
921 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500922 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
924 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
925 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
926 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
927 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if (!is_priv) {
930 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
931 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100932
Hanno Beckerdc95c892017-08-23 06:57:02 +0100933#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100934 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
936 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
937 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
938 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100939 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100940#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
942 DP, DQ, QP)) != 0) {
943 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100944 }
945#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100948}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100949
Paul Bakker5121ce52009-01-03 21:22:43 +0000950/*
951 * Initialize an RSA context
952 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000954{
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
Ronald Cronc1905a12021-06-05 11:11:14 +0200957 ctx->padding = MBEDTLS_RSA_PKCS_V15;
958 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100961 /* Set ctx->ver to nonzero to indicate that the mutex has been
962 * initialized and will need to be freed. */
963 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200965#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000966}
967
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100968/*
969 * Set padding for an existing RSA context
970 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
972 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100973{
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200975#if defined(MBEDTLS_PKCS1_V15)
976 case MBEDTLS_RSA_PKCS_V15:
977 break;
978#endif
979
980#if defined(MBEDTLS_PKCS1_V21)
981 case MBEDTLS_RSA_PKCS_V21:
982 break;
983#endif
984 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200986 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200987
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200988#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
990 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200991 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200992 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return MBEDTLS_ERR_RSA_INVALID_PADDING;
994 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200995 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200996#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500997
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100998 ctx->padding = padding;
999 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +02001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +01001002}
1003
Hanno Becker617c1ae2017-08-23 14:11:24 +01001004/*
Yanray Wang83548b52023-03-15 16:46:34 +08001005 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +08001006 */
1007int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
1008{
Yanray Wang644b9012023-03-15 16:50:31 +08001009 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +08001010}
1011
1012/*
Yanray Wang12cb3962023-03-01 10:20:02 +08001013 * Get hash identifier of mbedtls_md_type_t type
1014 */
Yanray Wangd41684e2023-03-17 18:54:22 +08001015int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +08001016{
Yanray Wang644b9012023-03-15 16:50:31 +08001017 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +08001018}
1019
1020/*
Gilles Peskine19f1adf2024-02-01 22:17:44 +01001021 * Get length in bits of RSA modulus
1022 */
1023size_t mbedtls_rsa_get_bitlen(const mbedtls_rsa_context *ctx)
1024{
1025 return mbedtls_mpi_bitlen(&ctx->N);
1026}
1027
1028/*
Hanno Becker617c1ae2017-08-23 14:11:24 +01001029 * Get length in bytes of RSA modulus
1030 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001031size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +01001032{
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +01001034}
1035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
1038/*
1039 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001040 *
1041 * This generation method follows the RSA key pair generation procedure of
1042 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001044int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
1045 int (*f_rng)(void *, unsigned char *, size_t),
1046 void *p_rng,
1047 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +00001048{
Janos Follath24eed8d2019-11-22 13:21:35 +00001049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -08001050 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +01001051 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Janos Follathb8fc1b02018-09-03 15:37:01 +01001053 /*
1054 * If the modulus is 1024 bit long or shorter, then the security strength of
1055 * the RSA algorithm is less than or equal to 80 bits and therefore an error
1056 * rate of 2^-80 is sufficient.
1057 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +01001059 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 }
Janos Follathb8fc1b02018-09-03 15:37:01 +01001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 mbedtls_mpi_init(&H);
1063 mbedtls_mpi_init(&G);
1064 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001066 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +01001067 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1068 goto cleanup;
1069 }
1070
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001071 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1073 goto cleanup;
1074 }
1075
1076 /*
1077 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001078 * 1. |P-Q| > 2^( nbits / 2 - 100 )
1079 * 2. GCD( E, (P-1)*(Q-1) ) == 1
1080 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +00001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 do {
1085 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
1086 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
1089 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001091 /* 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 +01001092 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
1093 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001094 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001096
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001097 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if (H.s < 0) {
1099 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
1100 }
Janos Follathef441782016-09-21 13:18:12 +01001101
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001102 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
1104 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
1105 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001106
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001107 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
1109 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -08001110 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001112
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001113 /* 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 +01001114 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
1115 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
1116 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 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 -08001119 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001121
1122 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001125 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
1127 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001132
Jethro Beekman97f95c92018-02-13 15:50:36 -08001133#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 * DP = D mod (P - 1)
1136 * DQ = D mod (Q - 1)
1137 * QP = Q^-1 mod P
1138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
1140 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001141#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
Hanno Becker83aad1f2017-08-23 06:45:10 +01001143 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +00001145
1146cleanup:
1147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 mbedtls_mpi_free(&H);
1149 mbedtls_mpi_free(&G);
1150 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if (ret != 0) {
1153 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +01001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if ((-ret & ~0x7f) == 0) {
1156 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
1157 }
1158 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 }
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001162}
1163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
1166/*
1167 * Check a public RSA key
1168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001170{
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
1172 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
1176 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001177 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
1180 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
1181 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
1182 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1183 }
1184
1185 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001186}
1187
1188/*
Hanno Becker705fc682017-10-10 17:57:02 +01001189 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001192{
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
1194 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
1195 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 }
Paul Bakker48377d92013-08-30 12:06:24 +02001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
1199 &ctx->D, &ctx->E, NULL, NULL) != 0) {
1200 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001202
Hanno Beckerb269a852017-08-25 08:03:21 +01001203#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
1205 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
1206 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +01001207 }
1208#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001211}
1212
1213/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001214 * Check if contexts holding a public and private key match
1215 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
1217 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001218{
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
1220 mbedtls_rsa_check_privkey(prv) != 0) {
1221 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001222 }
1223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
1225 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
1226 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001227 }
1228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001230}
1231
1232/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 * Do an RSA public key operation
1234 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001235int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
1236 const unsigned char *input,
1237 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001238{
Janos Follath24eed8d2019-11-22 13:21:35 +00001239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001240 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
1244 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1245 }
Hanno Becker705fc682017-10-10 17:57:02 +01001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001249#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1251 return ret;
1252 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001253#endif
1254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001258 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1259 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 }
1261
1262 olen = ctx->len;
Janos Follath9d72df82024-08-15 16:06:19 +01001263 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod_unsafe(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
1266cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1269 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1270 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001271#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (ret != 0) {
1276 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
1277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001280}
1281
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001282/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001283 * Generate or update blinding values, see section 10 of:
1284 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001285 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001286 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1289 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001290{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001291 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001292 mbedtls_mpi R;
1293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001297 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1299 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1300 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1301 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001302
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001303 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001304 }
1305
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001306 /* Unblinding value: Vf = random number, invertible mod N */
1307 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +02001309 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
1310 goto cleanup;
1311 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 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 +02001314
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001315 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
1317 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
1318 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001319
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001320 /* At this point, Vi is invertible mod N if and only if both Vf and R
1321 * are invertible mod N. If one of them isn't, we don't need to know
1322 * which one, we just loop and choose new values for both of them.
1323 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
1325 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001326 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -05001330
1331 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
1333 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001334
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001335 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001336 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 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 +02001338
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001339
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001340cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001344}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001345
Paul Bakker5121ce52009-01-03 21:22:43 +00001346/*
Janos Follathd6b09652023-11-21 09:33:54 +00001347 * Unblind
1348 * T = T * Vf mod N
1349 */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001350static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, const mbedtls_mpi *N)
Janos Follathd6b09652023-11-21 09:33:54 +00001351{
1352 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1353 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
1354 const size_t nlimbs = N->n;
1355 const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
1356 mbedtls_mpi RR, M_T;
1357
1358 mbedtls_mpi_init(&RR);
1359 mbedtls_mpi_init(&M_T);
1360
1361 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
1362 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
1363
1364 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
1365 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
1366
Janos Follathe6750b22023-12-27 10:22:59 +00001367 /* T = T * Vf mod N
1368 * Reminder: montmul(A, B, N) = A * B * R^-1 mod N
1369 * Usually both operands are multiplied by R mod N beforehand (by calling
1370 * `to_mont_rep()` on them), yielding a result that's also * R mod N (aka
1371 * "in the Montgomery domain"). Here we only multiply one operand by R mod
1372 * N, so the result is directly what we want - no need to call
1373 * `from_mont_rep()` on it. */
Janos Follathd6b09652023-11-21 09:33:54 +00001374 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 +00001375 mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1376
1377cleanup:
1378
1379 mbedtls_mpi_free(&RR);
1380 mbedtls_mpi_free(&M_T);
1381
1382 return ret;
1383}
1384
1385/*
Janos Follathe81102e2017-03-22 13:38:28 +00001386 * Exponent blinding supposed to prevent side-channel attacks using multiple
1387 * traces of measurements to recover the RSA key. The more collisions are there,
1388 * the more bits of the key can be recovered. See [3].
1389 *
1390 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001391 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001392 *
1393 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001394 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001395 *
1396 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1397 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1398 * Thus in this sense with 28 byte blinding the security is not reduced by
1399 * side-channel attacks like the one in [3])
1400 *
1401 * This countermeasure does not help if the key recovery is possible with a
1402 * single trace.
1403 */
1404#define RSA_EXPONENT_BLINDING 28
1405
1406/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 * Do an RSA private key operation
1408 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001409int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1410 int (*f_rng)(void *, unsigned char *, size_t),
1411 void *p_rng,
1412 const unsigned char *input,
1413 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001414{
Janos Follath24eed8d2019-11-22 13:21:35 +00001415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001416 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001417
1418 /* Temporary holding the result */
1419 mbedtls_mpi T;
1420
1421 /* Temporaries holding P-1, Q-1 and the
1422 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001423 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001424
1425#if !defined(MBEDTLS_RSA_NO_CRT)
1426 /* Temporaries holding the results mod p resp. mod q. */
1427 mbedtls_mpi TP, TQ;
1428
1429 /* Temporaries holding the blinded exponents for
1430 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001431 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001432#else
1433 /* Temporary holding the blinded exponent (if used). */
1434 mbedtls_mpi D_blind;
Hanno Becker43f94722017-08-25 11:50:00 +01001435#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001436
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001437 /* Temporaries holding the initial input and the double
1438 * checked result; should be the same in the end. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001439 mbedtls_mpi input_blinded, check_result_blinded;
Paul Bakker5121ce52009-01-03 21:22:43 +00001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 if (f_rng == NULL) {
1442 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1443 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 if (rsa_check_context(ctx, 1 /* private key checks */,
1446 1 /* blinding on */) != 0) {
1447 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001448 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001449
Hanno Becker06811ce2017-05-03 15:10:34 +01001450#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1452 return ret;
1453 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001454#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001455
Hanno Becker06811ce2017-05-03 15:10:34 +01001456 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 mbedtls_mpi_init(&P1);
1460 mbedtls_mpi_init(&Q1);
1461 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001462
Janos Follathe81102e2017-03-22 13:38:28 +00001463#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001465#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 mbedtls_mpi_init(&DP_blind);
1467 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001468#endif
1469
Hanno Becker06811ce2017-05-03 15:10:34 +01001470#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001472#endif
1473
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001474 mbedtls_mpi_init(&input_blinded);
1475 mbedtls_mpi_init(&check_result_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001476
1477 /* End of MPI initialization */
1478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1480 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001481 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1482 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 }
1484
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001485 /*
1486 * Blinding
1487 * T = T * Vi mod N
1488 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1490 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1491 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001492
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001493 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&input_blinded, &T));
Janos Follath6bcbc922023-11-21 09:46:43 +00001494
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001495 /*
1496 * Exponent blinding
1497 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1499 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001500
Janos Follathf9203b42017-03-22 15:13:15 +00001501#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001502 /*
1503 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1506 f_rng, p_rng));
1507 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1508 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1509 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathf9203b42017-03-22 15:13:15 +00001510#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001511 /*
1512 * DP_blind = ( P - 1 ) * R + DP
1513 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1515 f_rng, p_rng));
1516 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1517 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1518 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001519
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001520 /*
1521 * DQ_blind = ( Q - 1 ) * R + DQ
1522 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1524 f_rng, p_rng));
1525 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1526 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1527 &ctx->DQ));
Janos Follathe81102e2017-03-22 13:38:28 +00001528#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath47ee7702023-12-27 10:33:00 +00001531 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &D_blind, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001532#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001533 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001534 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001536 * TP = input ^ dP mod P
1537 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001538 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001539
Janos Follath47ee7702023-12-27 10:33:00 +00001540 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, &DP_blind, &ctx->P, &ctx->RP));
1541 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, &DQ_blind, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001542
1543 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001544 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1547 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1548 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001549
1550 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001551 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1554 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001556
Hanno Becker2dec5e82017-10-03 07:49:52 +01001557 /* Verify the result to prevent glitching attacks. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001558 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&check_result_blinded, &T, &ctx->E,
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 &ctx->N, &ctx->RN));
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001560 if (mbedtls_mpi_cmp_mpi(&check_result_blinded, &input_blinded) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001561 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1562 goto cleanup;
1563 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001564
Janos Follath6bcbc922023-11-21 09:46:43 +00001565 /*
1566 * Unblind
1567 * T = T * Vf mod N
1568 */
1569 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1570
Paul Bakker5121ce52009-01-03 21:22:43 +00001571 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001573
1574cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1577 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1578 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001579#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 mbedtls_mpi_free(&P1);
1582 mbedtls_mpi_free(&Q1);
1583 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001584
Janos Follathe81102e2017-03-22 13:38:28 +00001585#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001587#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 mbedtls_mpi_free(&DP_blind);
1589 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001590#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001593
1594#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001596#endif
1597
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001598 mbedtls_mpi_free(&check_result_blinded);
1599 mbedtls_mpi_free(&input_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if (ret != 0 && ret >= -0x007f) {
1602 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1603 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001606}
1607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001609/**
1610 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1611 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001612 * \param dst buffer to mask
1613 * \param dlen length of destination buffer
1614 * \param src source of the mask generation
1615 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001616 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001617 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001618static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1619 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001620{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001621 unsigned char counter[4];
1622 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001623 unsigned int hlen;
1624 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001625 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001626 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001627 const mbedtls_md_info_t *md_info;
1628 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 mbedtls_md_init(&md_ctx);
1631 md_info = mbedtls_md_info_from_type(md_alg);
1632 if (md_info == NULL) {
1633 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1634 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 mbedtls_md_init(&md_ctx);
1637 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001638 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 memset(mask, 0, sizeof(mask));
1644 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001645
Simon Butcher02037452016-03-01 21:19:12 +00001646 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001647 p = dst;
1648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001650 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001652 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001656 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 }
1658 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001659 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 }
1661 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001662 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 }
1664 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001665 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001669 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001671
1672 counter[3]++;
1673
1674 dlen -= use_len;
1675 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001676
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001677exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001682}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001683
1684/**
1685 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1686 *
1687 * \param hash the input hash
1688 * \param hlen length of the input hash
1689 * \param salt the input salt
1690 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001691 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001692 * \param md_alg message digest to use
1693 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001694static int hash_mprime(const unsigned char *hash, size_t hlen,
1695 const unsigned char *salt, size_t slen,
1696 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001697{
1698 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001699
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001700 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1704 if (md_info == NULL) {
1705 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1706 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 mbedtls_md_init(&md_ctx);
1709 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001710 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 }
1712 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001713 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 }
1715 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001716 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 }
1718 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001719 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 }
1721 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001722 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 }
1724 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001725 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001727
1728exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001730
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001732}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001733
1734/**
1735 * Compute a hash.
1736 *
1737 * \param md_alg algorithm to use
1738 * \param input input message to hash
1739 * \param ilen input length
1740 * \param output the output buffer - must be large enough for \p md_alg
1741 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001742static int compute_hash(mbedtls_md_type_t md_alg,
1743 const unsigned char *input, size_t ilen,
1744 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001745{
1746 const mbedtls_md_info_t *md_info;
1747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 md_info = mbedtls_md_info_from_type(md_alg);
1749 if (md_info == NULL) {
1750 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1751 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001754}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001758/*
1759 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1760 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001761int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1762 int (*f_rng)(void *, unsigned char *, size_t),
1763 void *p_rng,
1764 const unsigned char *label, size_t label_len,
1765 size_t ilen,
1766 const unsigned char *input,
1767 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001768{
1769 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001771 unsigned char *p = output;
1772 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if (f_rng == NULL) {
1775 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1776 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001777
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001778 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 if (hlen == 0) {
1780 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1781 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001782
1783 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001784
Simon Butcher02037452016-03-01 21:19:12 +00001785 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1787 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1788 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001791
1792 *p++ = 0;
1793
Simon Butcher02037452016-03-01 21:19:12 +00001794 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1796 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1797 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001798
1799 p += hlen;
1800
Simon Butcher02037452016-03-01 21:19:12 +00001801 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1803 if (ret != 0) {
1804 return ret;
1805 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001806 p += hlen;
1807 p += olen - 2 * hlen - 2 - ilen;
1808 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 if (ilen != 0) {
1810 memcpy(p, input, ilen);
1811 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001812
Simon Butcher02037452016-03-01 21:19:12 +00001813 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001815 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 return ret;
1817 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001818
Simon Butcher02037452016-03-01 21:19:12 +00001819 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001821 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 return ret;
1823 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001826}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001830/*
1831 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1832 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001833int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1834 int (*f_rng)(void *, unsigned char *, size_t),
1835 void *p_rng, size_t ilen,
1836 const unsigned char *input,
1837 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001838{
1839 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001841 unsigned char *p = output;
1842
Paul Bakkerb3869132013-02-28 17:21:01 +01001843 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001844
Simon Butcher02037452016-03-01 21:19:12 +00001845 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if (ilen + 11 < ilen || olen < ilen + 11) {
1847 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1848 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001849
1850 nb_pad = olen - 3 - ilen;
1851
1852 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 if (f_rng == NULL) {
1855 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1856 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001857
1858 *p++ = MBEDTLS_RSA_CRYPT;
1859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001861 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001862
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001863 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 ret = f_rng(p_rng, p, 1);
1865 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001866
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001867 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 if (rng_dl == 0 || ret != 0) {
1869 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1870 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001871
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001872 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001873 }
1874
1875 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if (ilen != 0) {
1877 memcpy(p, input, ilen);
1878 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001881}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001883
Paul Bakker5121ce52009-01-03 21:22:43 +00001884/*
1885 * Add the message padding, then do an RSA operation
1886 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001887int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1888 int (*f_rng)(void *, unsigned char *, size_t),
1889 void *p_rng,
1890 size_t ilen,
1891 const unsigned char *input,
1892 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001893{
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895#if defined(MBEDTLS_PKCS1_V15)
1896 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1898 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001899#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901#if defined(MBEDTLS_PKCS1_V21)
1902 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1904 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001905#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
1907 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001910}
1911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001913/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001914 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001916int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1917 int (*f_rng)(void *, unsigned char *, size_t),
1918 void *p_rng,
1919 const unsigned char *label, size_t label_len,
1920 size_t *olen,
1921 const unsigned char *input,
1922 unsigned char *output,
1923 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001924{
Janos Follath24eed8d2019-11-22 13:21:35 +00001925 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001926 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001927 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001928 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001930 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001931 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001932
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001933 /*
1934 * Parameters sanity checks
1935 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1937 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1938 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
1940 ilen = ctx->len;
1941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (ilen < 16 || ilen > sizeof(buf)) {
1943 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1944 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001946 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (hlen == 0) {
1948 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1949 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001950
Janos Follathc17cda12016-02-11 11:08:18 +00001951 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 if (2 * hlen + 2 > ilen) {
1953 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1954 }
Janos Follathc17cda12016-02-11 11:08:18 +00001955
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001956 /*
1957 * RSA operation
1958 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001962 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001964
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001965 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001966 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001967 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001968 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001970 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 /* DB: Apply dbMask to maskedDB */
1972 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001973 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001974 goto cleanup;
1975 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001976
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001977 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1979 label, label_len, lhash);
1980 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001981 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001983
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001984 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001985 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001986 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 p = buf;
1988
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001989 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001990
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001991 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001992
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001993 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001994 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001995 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001996
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001997 /* Get zero-padding len, but always read till end of buffer
1998 * (minus one, for the 01 byte) */
1999 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01002000 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01002002 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
2003 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002004 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002005
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002006 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002007 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01002008
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002009 /*
2010 * The only information "leaked" is whether the padding was correct or not
2011 * (eg, no data is copied if it was not correct). This meets the
2012 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
2013 * the different error conditions.
2014 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002015 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002016 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2017 goto cleanup;
2018 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002019
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002020 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002021 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
2022 goto cleanup;
2023 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002024
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002025 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (*olen != 0) {
2027 memcpy(output, p, *olen);
2028 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002029 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002030
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002031cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 mbedtls_platform_zeroize(buf, sizeof(buf));
2033 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002036}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002040/*
2041 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
2042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002043int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
2044 int (*f_rng)(void *, unsigned char *, size_t),
2045 void *p_rng,
2046 size_t *olen,
2047 const unsigned char *input,
2048 unsigned char *output,
2049 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002050{
2051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2052 size_t ilen;
2053 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
2054
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002055 ilen = ctx->len;
2056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2058 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2059 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (ilen < 16 || ilen > sizeof(buf)) {
2062 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2063 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002068 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
2072 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002073
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002074cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002078}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002080
2081/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002082 * Do an RSA operation, then remove the message padding
2083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002084int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
2085 int (*f_rng)(void *, unsigned char *, size_t),
2086 void *p_rng,
2087 size_t *olen,
2088 const unsigned char *input,
2089 unsigned char *output,
2090 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01002091{
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#if defined(MBEDTLS_PKCS1_V15)
2094 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
2096 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02002097#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_PKCS1_V21)
2100 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
2102 olen, input, output,
2103 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01002104#endif
2105
2106 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002108 }
2109}
2110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002112static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2113 int (*f_rng)(void *, unsigned char *, size_t),
2114 void *p_rng,
2115 mbedtls_md_type_t md_alg,
2116 unsigned int hashlen,
2117 const unsigned char *hash,
2118 int saltlen,
2119 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002120{
2121 size_t olen;
2122 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002123 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002124 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00002125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002126 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002127 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002130 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (f_rng == NULL) {
2134 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2135 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002136
2137 olen = ctx->len;
2138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002140 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002141 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 if (exp_hashlen == 0) {
2143 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2144 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (hashlen != exp_hashlen) {
2147 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2148 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002149 }
2150
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002151 hash_id = (mbedtls_md_type_t) ctx->hash_id;
2152 if (hash_id == MBEDTLS_MD_NONE) {
2153 hash_id = md_alg;
2154 }
2155 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if (hlen == 0) {
2157 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2158 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
2161 /* Calculate the largest possible salt length, up to the hash size.
2162 * Normally this is the hash length, which is the maximum salt length
2163 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
2164 * enough room, use the maximum salt length that fits. The constraint is
2165 * that the hash length plus the salt length plus 2 bytes must be at most
2166 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
2167 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002168 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (olen < hlen + min_slen + 2) {
2170 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2171 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002172 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002174 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 }
2176 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
2177 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2178 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02002179 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002180 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01002183
Simon Butcher02037452016-03-01 21:19:12 +00002184 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002186 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01002187 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002188
2189 /* Generate salt of length slen in place in the encoded message */
2190 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
2192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
2193 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02002194
Paul Bakkerb3869132013-02-28 17:21:01 +01002195 p += slen;
2196
Simon Butcher02037452016-03-01 21:19:12 +00002197 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002198 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (ret != 0) {
2200 return ret;
2201 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002202
Simon Butcher02037452016-03-01 21:19:12 +00002203 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002205 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002207
Simon Butcher02037452016-03-01 21:19:12 +00002208 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002209 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (ret != 0) {
2211 return ret;
2212 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2215 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01002216
2217 p += hlen;
2218 *p++ = 0xBC;
2219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002221}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002222
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002223static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2224 int (*f_rng)(void *, unsigned char *, size_t),
2225 void *p_rng,
2226 mbedtls_md_type_t md_alg,
2227 unsigned int hashlen,
2228 const unsigned char *hash,
2229 int saltlen,
2230 unsigned char *sig)
2231{
2232 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2233 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2234 }
Valerio Settie700d802024-02-26 13:52:34 +01002235 if ((ctx->hash_id == MBEDTLS_MD_NONE) && (md_alg == MBEDTLS_MD_NONE)) {
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002236 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2237 }
2238 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
2239 sig);
2240}
2241
2242int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2243 int (*f_rng)(void *, unsigned char *, size_t),
2244 void *p_rng,
2245 mbedtls_md_type_t md_alg,
2246 unsigned int hashlen,
2247 const unsigned char *hash,
2248 unsigned char *sig)
2249{
2250 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
2251 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2252}
2253
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002254/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02002255 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
2256 * the option to pass in the salt length.
2257 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002258int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
2259 int (*f_rng)(void *, unsigned char *, size_t),
2260 void *p_rng,
2261 mbedtls_md_type_t md_alg,
2262 unsigned int hashlen,
2263 const unsigned char *hash,
2264 int saltlen,
2265 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02002266{
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2268 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02002269}
2270
Cédric Meuterf3fab332020-04-25 11:30:45 +02002271/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002272 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
2273 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002274int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2275 int (*f_rng)(void *, unsigned char *, size_t),
2276 void *p_rng,
2277 mbedtls_md_type_t md_alg,
2278 unsigned int hashlen,
2279 const unsigned char *hash,
2280 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002281{
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2283 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002284}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002288/*
2289 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
2290 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002291
2292/* Construct a PKCS v1.5 encoding of a hashed message
2293 *
2294 * This is used both for signature generation and verification.
2295 *
2296 * Parameters:
2297 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002298 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002299 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002300 * - hash: Buffer containing the hashed message or the raw data.
2301 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002302 * - dst: Buffer to hold the encoded message.
2303 *
2304 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002305 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002306 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002307 *
2308 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002309static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
2310 unsigned int hashlen,
2311 const unsigned char *hash,
2312 size_t dst_len,
2313 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01002314{
2315 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002316 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002317 unsigned char *p = dst;
2318 const char *oid = NULL;
2319
2320 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002322 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 if (md_size == 0) {
2324 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2325 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2328 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2329 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (hashlen != md_size) {
2332 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2333 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002334
2335 /* Double-check that 8 + hashlen + oid_size can be used as a
2336 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01002338 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 10 + hashlen + oid_size < 10 + hashlen) {
2340 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2341 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002342
2343 /*
2344 * Static bounds check:
2345 * - Need 10 bytes for five tag-length pairs.
2346 * (Insist on 1-byte length encodings to protect against variants of
2347 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2348 * - Need hashlen bytes for hash
2349 * - Need oid_size bytes for hash alg OID.
2350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 if (nb_pad < 10 + hashlen + oid_size) {
2352 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2353 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002354 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 } else {
2356 if (nb_pad < hashlen) {
2357 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2358 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002359
2360 nb_pad -= hashlen;
2361 }
2362
Hanno Becker2b2f8982017-09-27 17:10:03 +01002363 /* Need space for signature header and padding delimiter (3 bytes),
2364 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 if (nb_pad < 3 + 8) {
2366 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2367 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002368 nb_pad -= 3;
2369
2370 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002371 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002372
2373 /* Write signature header and padding */
2374 *p++ = 0;
2375 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002377 p += nb_pad;
2378 *p++ = 0;
2379
2380 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (md_alg == MBEDTLS_MD_NONE) {
2382 memcpy(p, hash, hashlen);
2383 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002384 }
2385
2386 /* Signing hashed data, add corresponding ASN.1 structure
2387 *
2388 * DigestInfo ::= SEQUENCE {
2389 * digestAlgorithm DigestAlgorithmIdentifier,
2390 * digest Digest }
2391 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2392 * Digest ::= OCTET STRING
2393 *
2394 * Schematic:
2395 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2396 * TAG-NULL + LEN [ NULL ] ]
2397 * TAG-OCTET + LEN [ HASH ] ]
2398 */
2399 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002401 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002403 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002404 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002406 p += oid_size;
2407 *p++ = MBEDTLS_ASN1_NULL;
2408 *p++ = 0x00;
2409 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002410 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002412 p += hashlen;
2413
2414 /* Just a sanity-check, should be automatic
2415 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (p != dst + dst_len) {
2417 mbedtls_platform_zeroize(dst, dst_len);
2418 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002419 }
2420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002422}
2423
Paul Bakkerb3869132013-02-28 17:21:01 +01002424/*
2425 * Do an RSA operation to sign the message digest
2426 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2428 int (*f_rng)(void *, unsigned char *, size_t),
2429 void *p_rng,
2430 mbedtls_md_type_t md_alg,
2431 unsigned int hashlen,
2432 const unsigned char *hash,
2433 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002434{
Janos Follath24eed8d2019-11-22 13:21:35 +00002435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002436 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002439 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2443 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2444 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002445
Hanno Beckerfdf38032017-09-06 12:35:55 +01002446 /*
2447 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2448 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2451 ctx->len, sig)) != 0) {
2452 return ret;
2453 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002454
Hanno Beckerfdf38032017-09-06 12:35:55 +01002455 /* Private key operation
2456 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002457 * In order to prevent Lenstra's attack, make the signature in a
2458 * temporary buffer and check it before returning it.
2459 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 sig_try = mbedtls_calloc(1, ctx->len);
2462 if (sig_try == NULL) {
2463 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002464 }
2465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 verif = mbedtls_calloc(1, ctx->len);
2467 if (verif == NULL) {
2468 mbedtls_free(sig_try);
2469 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2470 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2473 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2474
2475 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002476 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2477 goto cleanup;
2478 }
2479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002481
2482cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002483 mbedtls_zeroize_and_free(sig_try, ctx->len);
2484 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (ret != 0) {
2487 memset(sig, '!', ctx->len);
2488 }
2489 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002490}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002492
2493/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 * Do an RSA operation to sign the message digest
2495 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002496int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2497 int (*f_rng)(void *, unsigned char *, size_t),
2498 void *p_rng,
2499 mbedtls_md_type_t md_alg,
2500 unsigned int hashlen,
2501 const unsigned char *hash,
2502 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002503{
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002505 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#if defined(MBEDTLS_PKCS1_V15)
2510 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2512 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002513#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#if defined(MBEDTLS_PKCS1_V21)
2516 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2518 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002519#endif
2520
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002524}
2525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002527/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002528 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002530int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2531 mbedtls_md_type_t md_alg,
2532 unsigned int hashlen,
2533 const unsigned char *hash,
2534 mbedtls_md_type_t mgf1_hash_id,
2535 int expected_salt_len,
2536 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002537{
Janos Follath24eed8d2019-11-22 13:21:35 +00002538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002539 size_t siglen;
2540 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002541 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002542 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002543 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002544 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002548 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002550
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 siglen = ctx->len;
2552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (siglen < 16 || siglen > sizeof(buf)) {
2554 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (ret != 0) {
2560 return ret;
2561 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
2563 p = buf;
2564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (buf[siglen - 1] != 0xBC) {
2566 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002567 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002568
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 if (md_alg != MBEDTLS_MD_NONE) {
2570 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002571 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (exp_hashlen == 0) {
2573 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2574 }
2575
2576 if (hashlen != exp_hashlen) {
2577 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2578 }
2579 }
2580
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002581 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (hlen == 0) {
2583 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2584 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002585
Simon Butcher02037452016-03-01 21:19:12 +00002586 /*
2587 * Note: EMSA-PSS verification is over the length of N - 1 bits
2588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if (buf[0] >> (8 - siglen * 8 + msb)) {
2592 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2593 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002594
Simon Butcher02037452016-03-01 21:19:12 +00002595 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002597 p++;
2598 siglen -= 1;
2599 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 if (siglen < hlen + 2) {
2602 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2603 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002604 hash_start = p + siglen - hlen - 1;
2605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2607 if (ret != 0) {
2608 return ret;
2609 }
Paul Bakker02303e82013-01-03 11:08:31 +01002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002614 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (*p++ != 0x01) {
2618 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2619 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002620
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002621 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2624 observed_salt_len != (size_t) expected_salt_len) {
2625 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002626 }
2627
Simon Butcher02037452016-03-01 21:19:12 +00002628 /*
2629 * Generate H = Hash( M' )
2630 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2632 result, mgf1_hash_id);
2633 if (ret != 0) {
2634 return ret;
2635 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 if (memcmp(hash_start, result, hlen) != 0) {
2638 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2639 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002642}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002643
2644/*
2645 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2646 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2648 mbedtls_md_type_t md_alg,
2649 unsigned int hashlen,
2650 const unsigned char *hash,
2651 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002652{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002653 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002655 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002660 : md_alg;
2661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2663 md_alg, hashlen, hash,
2664 mgf1_hash_id,
2665 MBEDTLS_RSA_SALT_LEN_ANY,
2666 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002667
2668}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002672/*
2673 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2674 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002675int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2676 mbedtls_md_type_t md_alg,
2677 unsigned int hashlen,
2678 const unsigned char *hash,
2679 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002680{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002681 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002682 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002683 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002686 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002688
2689 sig_len = ctx->len;
2690
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002691 /*
2692 * Prepare expected PKCS1 v1.5 encoding of hash.
2693 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2696 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002697 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2698 goto cleanup;
2699 }
2700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2702 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002703 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002705
2706 /*
2707 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2708 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002709
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 ret = mbedtls_rsa_public(ctx, sig, encoded);
2711 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002712 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002714
Simon Butcher02037452016-03-01 21:19:12 +00002715 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002716 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002717 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2720 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002721 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2722 goto cleanup;
2723 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002724
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002725cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002726
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002728 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002729 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002732 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002733 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002736}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
2739/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002740 * Do an RSA operation and check the message digest
2741 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002742int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2743 mbedtls_md_type_t md_alg,
2744 unsigned int hashlen,
2745 const unsigned char *hash,
2746 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002747{
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002749 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753#if defined(MBEDTLS_PKCS1_V15)
2754 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2756 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002757#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759#if defined(MBEDTLS_PKCS1_V21)
2760 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2762 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002763#endif
2764
2765 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002767 }
2768}
2769
2770/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002771 * Copy the components of an RSA key
2772 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002773int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002774{
Janos Follath24eed8d2019-11-22 13:21:35 +00002775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002776
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002777 dst->len = src->len;
2778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2780 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2783 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2784 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002785
2786#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2788 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2789 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2790 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2791 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002792#endif
2793
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002795
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2797 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002798
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002799 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002800 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002801
2802cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 if (ret != 0) {
2804 mbedtls_rsa_free(dst);
2805 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002808}
2809
2810/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 * Free the components of an RSA key
2812 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002813void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002814{
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002816 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 mbedtls_mpi_free(&ctx->Vi);
2820 mbedtls_mpi_free(&ctx->Vf);
2821 mbedtls_mpi_free(&ctx->RN);
2822 mbedtls_mpi_free(&ctx->D);
2823 mbedtls_mpi_free(&ctx->Q);
2824 mbedtls_mpi_free(&ctx->P);
2825 mbedtls_mpi_free(&ctx->E);
2826 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002827
Hanno Becker33c30a02017-08-23 07:00:22 +01002828#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 mbedtls_mpi_free(&ctx->RQ);
2830 mbedtls_mpi_free(&ctx->RP);
2831 mbedtls_mpi_free(&ctx->QP);
2832 mbedtls_mpi_free(&ctx->DQ);
2833 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002834#endif /* MBEDTLS_RSA_NO_CRT */
2835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002837 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 if (ctx->ver != 0) {
2839 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002840 ctx->ver = 0;
2841 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002842#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002843}
2844
Hanno Beckerab377312017-08-23 16:24:51 +01002845#endif /* !MBEDTLS_RSA_ALT */
2846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002848
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
2850/*
2851 * Example RSA-1024 keypair, for test purposes
2852 */
2853#define KEY_LEN 128
2854
2855#define RSA_N "9292758453063D803DD603D5E777D788" \
2856 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2857 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2858 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2859 "93A89813FBF3C4F8066D2D800F7C38A8" \
2860 "1AE31942917403FF4946B0A83D3D3E05" \
2861 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2862 "5E94BB77B07507233A0BC7BAC8F90F79"
2863
2864#define RSA_E "10001"
2865
2866#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2867 "66CA472BC44D253102F8B4A9D3BFA750" \
2868 "91386C0077937FE33FA3252D28855837" \
2869 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2870 "DF79C5CE07EE72C7F123142198164234" \
2871 "CABB724CF78B8173B9F880FC86322407" \
2872 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2873 "071513A1E85B5DFA031F21ECAE91A34D"
2874
2875#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2876 "2C01CAD19EA484A87EA4377637E75500" \
2877 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2878 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2879
2880#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2881 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2882 "910E4168387E3C30AA1E00C339A79508" \
2883 "8452DD96A9A5EA5D9DCA68DA636032AF"
2884
Paul Bakker5121ce52009-01-03 21:22:43 +00002885#define PT_LEN 24
2886#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2887 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002890static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002891{
gufe44c2620da2020-08-03 17:56:50 +02002892#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002893 size_t i;
2894
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002896 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 }
Paul Bakker545570e2010-07-18 09:00:25 +00002898
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002900 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002902#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002904 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002908#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002911}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002913
Paul Bakker5121ce52009-01-03 21:22:43 +00002914/*
2915 * Checkup routine
2916 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002917int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002918{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002919 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002921 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 unsigned char rsa_plaintext[PT_LEN];
2924 unsigned char rsa_decrypted[PT_LEN];
2925 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002926#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002927 unsigned char sha1sum[20];
2928#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002929
Hanno Becker3a701162017-08-22 13:52:43 +01002930 mbedtls_mpi K;
2931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 mbedtls_mpi_init(&K);
2933 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2936 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2937 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2938 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2939 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2940 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2941 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2942 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2943 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2944 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 if (verbose != 0) {
2949 mbedtls_printf(" RSA key validation: ");
2950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2953 mbedtls_rsa_check_privkey(&rsa) != 0) {
2954 if (verbose != 0) {
2955 mbedtls_printf("failed\n");
2956 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
Hanno Becker5bc87292017-05-03 15:09:31 +01002958 ret = 1;
2959 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002960 }
2961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 if (verbose != 0) {
2963 mbedtls_printf("passed\n PKCS#1 encryption : ");
2964 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002967
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2969 PT_LEN, rsa_plaintext,
2970 rsa_ciphertext) != 0) {
2971 if (verbose != 0) {
2972 mbedtls_printf("failed\n");
2973 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
Hanno Becker5bc87292017-05-03 15:09:31 +01002975 ret = 1;
2976 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002977 }
2978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 if (verbose != 0) {
2980 mbedtls_printf("passed\n PKCS#1 decryption : ");
2981 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002982
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2984 &len, rsa_ciphertext, rsa_decrypted,
2985 sizeof(rsa_decrypted)) != 0) {
2986 if (verbose != 0) {
2987 mbedtls_printf("failed\n");
2988 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002989
Hanno Becker5bc87292017-05-03 15:09:31 +01002990 ret = 1;
2991 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 }
2993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2995 if (verbose != 0) {
2996 mbedtls_printf("failed\n");
2997 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002998
Hanno Becker5bc87292017-05-03 15:09:31 +01002999 ret = 1;
3000 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 }
3002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 if (verbose != 0) {
3004 mbedtls_printf("passed\n");
3005 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003006
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003007#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 if (verbose != 0) {
3009 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01003010 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003011
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01003012 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
3013 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 if (verbose != 0) {
3015 mbedtls_printf("failed\n");
3016 }
3017
3018 return 1;
3019 }
3020
3021 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
3022 MBEDTLS_MD_SHA1, 20,
3023 sha1sum, rsa_ciphertext) != 0) {
3024 if (verbose != 0) {
3025 mbedtls_printf("failed\n");
3026 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003027
Hanno Becker5bc87292017-05-03 15:09:31 +01003028 ret = 1;
3029 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003030 }
3031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 if (verbose != 0) {
3033 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
3034 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
3037 sha1sum, rsa_ciphertext) != 0) {
3038 if (verbose != 0) {
3039 mbedtls_printf("failed\n");
3040 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003041
Hanno Becker5bc87292017-05-03 15:09:31 +01003042 ret = 1;
3043 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 }
3045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 if (verbose != 0) {
3047 mbedtls_printf("passed\n");
3048 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003049#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 if (verbose != 0) {
3052 mbedtls_printf("\n");
3053 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003054
Paul Bakker3d8fb632014-04-17 12:42:41 +02003055cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 mbedtls_mpi_free(&K);
3057 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02003059 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003062}
3063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066#endif /* MBEDTLS_RSA_C */