blob: 3df00c06a0ffa2492916b06b1738542374ec94f6 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgmane3c05852023-11-03 12:21:36 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
Hanno Becker74716312017-10-02 10:00:37 +01007
Paul Bakker5121ce52009-01-03 21:22:43 +00008/*
Simon Butcherbdae02c2016-01-20 00:44:42 +00009 * The following sources were referenced in the design of this implementation
10 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000011 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000012 * [1] A method for obtaining digital signatures and public-key cryptosystems
13 * R Rivest, A Shamir, and L Adleman
14 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
15 *
16 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
17 * Menezes, van Oorschot and Vanstone
18 *
Janos Follathe81102e2017-03-22 13:38:28 +000019 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
20 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
21 * Stefan Mangard
22 * https://arxiv.org/abs/1702.08719v2
23 *
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/rsa.h"
Janos Follathd6b09652023-11-21 09:33:54 +000031#include "bignum_core.h"
Chris Jones66a4cd42021-03-09 16:04:12 +000032#include "rsa_alt_helpers.h"
Tomi Fontanilles573dc232023-12-10 14:57:51 +020033#include "rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/oid.h"
Valerio Settib328c442024-01-23 10:48:45 +010035#include "mbedtls/asn1write.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000037#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020038#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020039#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020040#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000041
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
gufe44c2620da2020-08-03 17:56:50 +020044#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000045#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010049
Valerio Setti201e6432024-02-01 17:19:37 +010050/*
51 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
52 *
53 * The value zero is:
54 * - never a valid value for an RSA parameter
55 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
56 *
57 * Since values can't be omitted in PKCS#1, passing a zero value to
58 * rsa_complete() would be incorrect, so reject zero values early.
59 */
60static int asn1_get_nonzero_mpi(unsigned char **p,
61 const unsigned char *end,
62 mbedtls_mpi *X)
63{
64 int ret;
65
66 ret = mbedtls_asn1_get_mpi(p, end, X);
67 if (ret != 0) {
68 return ret;
69 }
70
71 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
72 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
73 }
74
75 return 0;
76}
77
Valerio Setti135ebde2024-02-01 17:00:29 +010078int mbedtls_rsa_parse_key(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +010079{
80 int ret, version;
81 size_t len;
82 unsigned char *p, *end;
83
84 mbedtls_mpi T;
85 mbedtls_mpi_init(&T);
86
87 p = (unsigned char *) key;
88 end = p + keylen;
89
90 /*
91 * This function parses the RSAPrivateKey (PKCS#1)
92 *
93 * RSAPrivateKey ::= SEQUENCE {
94 * version Version,
95 * modulus INTEGER, -- n
96 * publicExponent INTEGER, -- e
97 * privateExponent INTEGER, -- d
98 * prime1 INTEGER, -- p
99 * prime2 INTEGER, -- q
100 * exponent1 INTEGER, -- d mod (p-1)
101 * exponent2 INTEGER, -- d mod (q-1)
102 * coefficient INTEGER, -- (inverse of q) mod p
103 * otherPrimeInfos OtherPrimeInfos OPTIONAL
104 * }
105 */
106 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
107 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
108 return ret;
109 }
110
Valerio Setti9de84bd2024-02-08 17:40:27 +0100111 if (end != p + len) {
112 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
113 }
Valerio Setti44ff9502024-02-01 16:51:05 +0100114
115 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
116 return ret;
117 }
118
119 if (version != 0) {
120 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
121 }
122
123 /* Import N */
124 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
125 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
126 NULL, NULL)) != 0) {
127 goto cleanup;
128 }
129
130 /* Import E */
131 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
132 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
133 NULL, &T)) != 0) {
134 goto cleanup;
135 }
136
137 /* Import D */
138 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
139 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
140 &T, NULL)) != 0) {
141 goto cleanup;
142 }
143
144 /* Import P */
145 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
146 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
147 NULL, NULL)) != 0) {
148 goto cleanup;
149 }
150
151 /* Import Q */
152 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
153 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
154 NULL, NULL)) != 0) {
155 goto cleanup;
156 }
157
Thomas Daubneyf47b66e2024-06-04 18:15:44 +0100158#if !defined(MBEDTLS_RSA_NO_CRT)
Valerio Setti44ff9502024-02-01 16:51:05 +0100159 /*
160 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
161 * that they can be easily recomputed from D, P and Q. However by
162 * parsing them from the PKCS1 structure it is possible to avoid
163 * recalculating them which both reduces the overhead of loading
164 * RSA private keys into memory and also avoids side channels which
165 * can arise when computing those values, since all of D, P, and Q
166 * are secret. See https://eprint.iacr.org/2020/055 for a
167 * description of one such attack.
168 */
169
170 /* Import DP */
171 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
172 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
173 goto cleanup;
174 }
175
176 /* Import DQ */
177 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
178 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
179 goto cleanup;
180 }
181
182 /* Import QP */
183 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
184 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
185 goto cleanup;
186 }
187
188#else
189 /* Verify existence of the CRT params */
190 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
191 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
192 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
193 goto cleanup;
194 }
195#endif
196
197 /* rsa_complete() doesn't complete anything with the default
198 * implementation but is still called:
199 * - for the benefit of alternative implementation that may want to
200 * pre-compute stuff beyond what's provided (eg Montgomery factors)
201 * - as is also sanity-checks the key
202 *
203 * Furthermore, we also check the public part for consistency with
204 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
205 */
206 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
207 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
208 goto cleanup;
209 }
210
211 if (p != end) {
212 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
213 }
214
215cleanup:
216
217 mbedtls_mpi_free(&T);
218
219 if (ret != 0) {
220 mbedtls_rsa_free(rsa);
221 }
222
223 return ret;
224}
225
Valerio Setti201e6432024-02-01 17:19:37 +0100226int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +0100227{
Valerio Setti201e6432024-02-01 17:19:37 +0100228 unsigned char *p = (unsigned char *) key;
229 unsigned char *end = (unsigned char *) (key + keylen);
Valerio Setti44ff9502024-02-01 16:51:05 +0100230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
231 size_t len;
232
233 /*
234 * RSAPublicKey ::= SEQUENCE {
235 * modulus INTEGER, -- n
236 * publicExponent INTEGER -- e
237 * }
238 */
239
Valerio Setti201e6432024-02-01 17:19:37 +0100240 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
Valerio Setti44ff9502024-02-01 16:51:05 +0100241 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
242 return ret;
243 }
244
Valerio Setti9de84bd2024-02-08 17:40:27 +0100245 if (end != p + len) {
246 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
247 }
Valerio Settife329ce2024-02-06 08:00:18 +0100248
Valerio Setti44ff9502024-02-01 16:51:05 +0100249 /* Import N */
Valerio Setti201e6432024-02-01 17:19:37 +0100250 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100251 return ret;
252 }
253
Valerio Setti201e6432024-02-01 17:19:37 +0100254 if ((ret = mbedtls_rsa_import_raw(rsa, p, len, NULL, 0, NULL, 0,
Valerio Setti44ff9502024-02-01 16:51:05 +0100255 NULL, 0, NULL, 0)) != 0) {
256 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
257 }
258
Valerio Setti201e6432024-02-01 17:19:37 +0100259 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100260
261 /* Import E */
Valerio Setti201e6432024-02-01 17:19:37 +0100262 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100263 return ret;
264 }
265
266 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
Valerio Setti201e6432024-02-01 17:19:37 +0100267 NULL, 0, p, len)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100268 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
269 }
270
Valerio Setti201e6432024-02-01 17:19:37 +0100271 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100272
273 if (mbedtls_rsa_complete(rsa) != 0 ||
274 mbedtls_rsa_check_pubkey(rsa) != 0) {
275 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
276 }
277
Valerio Setti201e6432024-02-01 17:19:37 +0100278 if (p != end) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100279 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
280 }
281
282 return 0;
283}
284
Valerio Setti135ebde2024-02-01 17:00:29 +0100285int mbedtls_rsa_write_key(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100286 unsigned char **p)
287{
288 size_t len = 0;
289 int ret;
290
291 mbedtls_mpi T; /* Temporary holding the exported parameters */
292
293 /*
294 * Export the parameters one after another to avoid simultaneous copies.
295 */
296
297 mbedtls_mpi_init(&T);
298
299 /* Export QP */
300 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
301 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
302 goto end_of_export;
303 }
304 len += ret;
305
306 /* Export DQ */
307 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
308 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
309 goto end_of_export;
310 }
311 len += ret;
312
313 /* Export DP */
314 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
315 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
316 goto end_of_export;
317 }
318 len += ret;
319
320 /* Export Q */
321 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, &T, NULL, NULL)) != 0 ||
322 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
323 goto end_of_export;
324 }
325 len += ret;
326
327 /* Export P */
328 if ((ret = mbedtls_rsa_export(rsa, NULL, &T, NULL, NULL, NULL)) != 0 ||
329 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
330 goto end_of_export;
331 }
332 len += ret;
333
334 /* Export D */
335 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, &T, NULL)) != 0 ||
336 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
337 goto end_of_export;
338 }
339 len += ret;
340
341 /* Export E */
342 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
343 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
344 goto end_of_export;
345 }
346 len += ret;
347
348 /* Export N */
349 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
350 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
351 goto end_of_export;
352 }
353 len += ret;
354
355end_of_export:
356
357 mbedtls_mpi_free(&T);
358 if (ret < 0) {
359 return ret;
360 }
361
362 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
363 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
364 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
365 MBEDTLS_ASN1_CONSTRUCTED |
366 MBEDTLS_ASN1_SEQUENCE));
367
368 return (int) len;
369}
370
371/*
372 * RSAPublicKey ::= SEQUENCE {
373 * modulus INTEGER, -- n
374 * publicExponent INTEGER -- e
375 * }
376 */
Valerio Setti135ebde2024-02-01 17:00:29 +0100377int mbedtls_rsa_write_pubkey(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100378 unsigned char **p)
379{
380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
381 size_t len = 0;
382 mbedtls_mpi T;
383
384 mbedtls_mpi_init(&T);
385
386 /* Export E */
387 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
388 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
389 goto end_of_export;
390 }
391 len += ret;
392
393 /* Export N */
394 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
395 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
396 goto end_of_export;
397 }
398 len += ret;
399
400end_of_export:
401
402 mbedtls_mpi_free(&T);
403 if (ret < 0) {
404 return ret;
405 }
406
407 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
408 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
409 MBEDTLS_ASN1_SEQUENCE));
410
411 return (int) len;
412}
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100413
Thomas Daubneyf47b66e2024-06-04 18:15:44 +0100414#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C)
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100415
416/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
417 * operation (EME-PKCS1-v1_5 decoding).
418 *
419 * \note The return value from this function is a sensitive value
420 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
421 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
422 * is often a situation that an attacker can provoke and leaking which
423 * one is the result is precisely the information the attacker wants.
424 *
425 * \param input The input buffer which is the payload inside PKCS#1v1.5
426 * encryption padding, called the "encoded message EM"
427 * by the terminology.
428 * \param ilen The length of the payload in the \p input buffer.
429 * \param output The buffer for the payload, called "message M" by the
430 * PKCS#1 terminology. This must be a writable buffer of
431 * length \p output_max_len bytes.
432 * \param olen The address at which to store the length of
433 * the payload. This must not be \c NULL.
434 * \param output_max_len The length in bytes of the output buffer \p output.
435 *
436 * \return \c 0 on success.
437 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
438 * The output buffer is too small for the unpadded payload.
439 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
440 * The input doesn't contain properly formatted padding.
441 */
442static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
443 size_t ilen,
444 unsigned char *output,
445 size_t output_max_len,
446 size_t *olen)
447{
448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
449 size_t i, plaintext_max_size;
450
451 /* The following variables take sensitive values: their value must
452 * not leak into the observable behavior of the function other than
453 * the designated outputs (output, olen, return value). Otherwise
454 * this would open the execution of the function to
455 * side-channel-based variants of the Bleichenbacher padding oracle
456 * attack. Potential side channels include overall timing, memory
457 * access patterns (especially visible to an adversary who has access
458 * to a shared memory cache), and branches (especially visible to
459 * an adversary who has access to a shared code cache or to a shared
460 * branch predictor). */
461 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100462 mbedtls_ct_condition_t bad;
463 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100464 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100465 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100466
467 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
468 : output_max_len;
469
470 /* Check and get padding length in constant time and constant
471 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100472 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100473
474
475 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
476 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100477 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100478
479 /* Read the whole buffer. Set pad_done to nonzero if we find
480 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100481 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100482 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100483 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100484 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100485 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100486 }
487
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100488 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100489 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100490
491 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100492 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100493
494 /* If the padding is valid, set plaintext_size to the number of
495 * remaining bytes after stripping the padding. If the padding
496 * is invalid, avoid leaking this fact through the size of the
497 * output: use the maximum message size that fits in the output
498 * buffer. Do it without branches to avoid leaking the padding
499 * validity through timing. RSA keys are small enough that all the
500 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100501 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100502 bad, (unsigned) plaintext_max_size,
503 (unsigned) (ilen - pad_count - 3));
504
505 /* Set output_too_large to 0 if the plaintext fits in the output
506 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100507 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100508 plaintext_max_size);
509
510 /* Set ret without branches to avoid timing attacks. Return:
511 * - INVALID_PADDING if the padding is bad (bad != 0).
512 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
513 * plaintext does not fit in the output buffer.
514 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100515 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100516 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100517 MBEDTLS_ERR_RSA_INVALID_PADDING,
518 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100519 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100520
521 /* If the padding is bad or the plaintext is too large, zero the
522 * data that we're about to copy to the output buffer.
523 * We need to copy the same amount of data
524 * from the same buffer whether the padding is good or not to
525 * avoid leaking the padding validity through overall timing or
526 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100527 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100528
529 /* If the plaintext is too large, truncate it to the buffer size.
530 * Copy anyway to avoid revealing the length through timing, because
531 * revealing the length is as bad as revealing the padding validity
532 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100533 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100534 (unsigned) plaintext_max_size,
535 (unsigned) plaintext_size);
536
537 /* Move the plaintext to the leftmost position where it can start in
538 * the working buffer, i.e. make it start plaintext_max_size from
539 * the end of the buffer. Do this with a memory access trace that
540 * does not depend on the plaintext size. After this move, the
541 * starting location of the plaintext is no longer sensitive
542 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100543 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
544 plaintext_max_size,
545 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100546
547 /* Finally copy the decrypted plaintext plus trailing zeros into the output
548 * buffer. If output_max_len is 0, then output may be an invalid pointer
549 * and the result of memcpy() would be undefined; prevent undefined
550 * behavior making sure to depend only on output_max_len (the size of the
551 * user-provided output buffer), which is independent from plaintext
552 * length, validity of padding, success of the decryption, and other
553 * secrets. */
554 if (output_max_len != 0) {
555 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
556 }
557
558 /* Report the amount of data we copied to the output buffer. In case
559 * of errors (bad padding or output too large), the value of *olen
560 * when this function returns is not specified. Making it equivalent
561 * to the good case limits the risks of leaking the padding validity. */
562 *olen = plaintext_size;
563
564 return ret;
565}
566
Thomas Daubneyf47b66e2024-06-04 18:15:44 +0100567#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C */
Hanno Beckera565f542017-10-11 11:00:19 +0100568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
570 const mbedtls_mpi *N,
571 const mbedtls_mpi *P, const mbedtls_mpi *Q,
572 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100573{
Janos Follath24eed8d2019-11-22 13:21:35 +0000574 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
577 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
578 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
579 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
580 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
581 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100582 }
583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (N != NULL) {
585 ctx->len = mbedtls_mpi_size(&ctx->N);
586 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100589}
590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
592 unsigned char const *N, size_t N_len,
593 unsigned char const *P, size_t P_len,
594 unsigned char const *Q, size_t Q_len,
595 unsigned char const *D, size_t D_len,
596 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100597{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000598 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 if (N != NULL) {
601 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
602 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100603 }
604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if (P != NULL) {
606 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
607 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 if (Q != NULL) {
610 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
611 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 if (D != NULL) {
614 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
615 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (E != NULL) {
618 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
619 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100620
621cleanup:
622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if (ret != 0) {
624 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
625 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100628}
629
Hanno Becker705fc682017-10-10 17:57:02 +0100630/*
631 * Checks whether the context fields are set in such a way
632 * that the RSA primitives will be able to execute without error.
633 * It does *not* make guarantees for consistency of the parameters.
634 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100635static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
636 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100637{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100638#if !defined(MBEDTLS_RSA_NO_CRT)
639 /* blinding_needed is only used for NO_CRT to decide whether
640 * P,Q need to be present or not. */
641 ((void) blinding_needed);
642#endif
643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
645 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
646 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000647 }
Hanno Becker705fc682017-10-10 17:57:02 +0100648
649 /*
650 * 1. Modular exponentiation needs positive, odd moduli.
651 */
652
653 /* Modular exponentiation wrt. N is always used for
654 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
656 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
657 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100658 }
659
660#if !defined(MBEDTLS_RSA_NO_CRT)
661 /* Modular exponentiation for P and Q is only
662 * used for private key operations and if CRT
663 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (is_priv &&
665 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
666 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
667 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
668 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
669 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100670 }
671#endif /* !MBEDTLS_RSA_NO_CRT */
672
673 /*
674 * 2. Exponents must be positive
675 */
676
677 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
679 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
680 }
Hanno Becker705fc682017-10-10 17:57:02 +0100681
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100682#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100683 /* For private key operations, use D or DP & DQ
684 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
686 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
687 }
Hanno Becker705fc682017-10-10 17:57:02 +0100688#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (is_priv &&
690 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
691 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
692 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100693 }
694#endif /* MBEDTLS_RSA_NO_CRT */
695
696 /* Blinding shouldn't make exponents negative either,
697 * so check that P, Q >= 1 if that hasn't yet been
698 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100699#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (is_priv && blinding_needed &&
701 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
702 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
703 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100704 }
705#endif
706
707 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100708 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100709#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (is_priv &&
711 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
712 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100713 }
714#endif
715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100717}
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100720{
721 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500722 int have_N, have_P, have_Q, have_D, have_E;
723#if !defined(MBEDTLS_RSA_NO_CRT)
724 int have_DP, have_DQ, have_QP;
725#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500726 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
729 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
730 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
731 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
732 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500733
734#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
736 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
737 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500738#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100739
Hanno Becker617c1ae2017-08-23 14:11:24 +0100740 /*
741 * Check whether provided parameters are enough
742 * to deduce all others. The following incomplete
743 * parameter sets for private keys are supported:
744 *
745 * (1) P, Q missing.
746 * (2) D and potentially N missing.
747 *
748 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100749
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500750 n_missing = have_P && have_Q && have_D && have_E;
751 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
752 d_missing = have_P && have_Q && !have_D && have_E;
753 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100754
755 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500756 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if (!is_priv && !is_pub) {
759 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
760 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100761
762 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100763 * Step 1: Deduce N if P, Q are provided.
764 */
765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if (!have_N && have_P && have_Q) {
767 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
768 &ctx->Q)) != 0) {
769 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100770 }
771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100773 }
774
775 /*
776 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100777 */
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (pq_missing) {
780 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
781 &ctx->P, &ctx->Q);
782 if (ret != 0) {
783 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
784 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 } else if (d_missing) {
787 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
788 &ctx->Q,
789 &ctx->E,
790 &ctx->D)) != 0) {
791 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100792 }
793 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100794
Hanno Becker617c1ae2017-08-23 14:11:24 +0100795 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100796 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100797 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100798 */
799
Hanno Becker23344b52017-08-23 07:43:27 +0100800#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if (is_priv && !(have_DP && have_DQ && have_QP)) {
802 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
803 &ctx->DP, &ctx->DQ, &ctx->QP);
804 if (ret != 0) {
805 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
806 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100807 }
Hanno Becker23344b52017-08-23 07:43:27 +0100808#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100809
810 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100811 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100812 */
813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100815}
816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
818 unsigned char *N, size_t N_len,
819 unsigned char *P, size_t P_len,
820 unsigned char *Q, size_t Q_len,
821 unsigned char *D, size_t D_len,
822 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100823{
824 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500825 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100826
827 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500828 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
830 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
831 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
832 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
833 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100836 /* If we're trying to export private parameters for a public key,
837 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (P != NULL || Q != NULL || D != NULL) {
839 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
840 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100841
842 }
843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (N != NULL) {
845 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
846 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (P != NULL) {
849 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
850 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (Q != NULL) {
853 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
854 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if (D != NULL) {
857 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
858 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (E != NULL) {
861 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
862 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100863
864cleanup:
865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100867}
868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
870 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
871 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100872{
Janos Follath24eed8d2019-11-22 13:21:35 +0000873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500874 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100875
876 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500877 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
879 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
880 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
881 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
882 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100885 /* If we're trying to export private parameters for a public key,
886 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (P != NULL || Q != NULL || D != NULL) {
888 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
889 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100890
891 }
892
893 /* Export all requested core parameters. */
894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
896 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
897 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
898 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
899 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
900 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100901 }
902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100904}
905
906/*
907 * Export CRT parameters
908 * This must also be implemented if CRT is not used, for being able to
909 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
910 * can be used in this case.
911 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
913 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100914{
Janos Follath24eed8d2019-11-22 13:21:35 +0000915 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500916 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100917
918 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500919 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
921 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
922 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
923 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
924 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (!is_priv) {
927 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
928 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100929
Hanno Beckerdc95c892017-08-23 06:57:02 +0100930#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100931 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
933 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
934 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
935 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100936 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100937#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
939 DP, DQ, QP)) != 0) {
940 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100941 }
942#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100945}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100946
Paul Bakker5121ce52009-01-03 21:22:43 +0000947/*
948 * Initialize an RSA context
949 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000951{
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Ronald Cronc1905a12021-06-05 11:11:14 +0200954 ctx->padding = MBEDTLS_RSA_PKCS_V15;
955 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100958 /* Set ctx->ver to nonzero to indicate that the mutex has been
959 * initialized and will need to be freed. */
960 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200962#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000963}
964
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100965/*
966 * Set padding for an existing RSA context
967 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
969 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100970{
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200972#if defined(MBEDTLS_PKCS1_V15)
973 case MBEDTLS_RSA_PKCS_V15:
974 break;
975#endif
976
977#if defined(MBEDTLS_PKCS1_V21)
978 case MBEDTLS_RSA_PKCS_V21:
979 break;
980#endif
981 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200983 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200984
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200985#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
987 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200988 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200989 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 return MBEDTLS_ERR_RSA_INVALID_PADDING;
991 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200992 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200993#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500994
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100995 ctx->padding = padding;
996 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100999}
1000
Hanno Becker617c1ae2017-08-23 14:11:24 +01001001/*
Yanray Wang83548b52023-03-15 16:46:34 +08001002 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +08001003 */
1004int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
1005{
Yanray Wang644b9012023-03-15 16:50:31 +08001006 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +08001007}
1008
1009/*
Yanray Wang12cb3962023-03-01 10:20:02 +08001010 * Get hash identifier of mbedtls_md_type_t type
1011 */
Yanray Wangd41684e2023-03-17 18:54:22 +08001012int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +08001013{
Yanray Wang644b9012023-03-15 16:50:31 +08001014 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +08001015}
1016
1017/*
Gilles Peskine19f1adf2024-02-01 22:17:44 +01001018 * Get length in bits of RSA modulus
1019 */
1020size_t mbedtls_rsa_get_bitlen(const mbedtls_rsa_context *ctx)
1021{
1022 return mbedtls_mpi_bitlen(&ctx->N);
1023}
1024
1025/*
Hanno Becker617c1ae2017-08-23 14:11:24 +01001026 * Get length in bytes of RSA modulus
1027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +01001029{
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +01001031}
1032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
1035/*
1036 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001037 *
1038 * This generation method follows the RSA key pair generation procedure of
1039 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
1042 int (*f_rng)(void *, unsigned char *, size_t),
1043 void *p_rng,
1044 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +00001045{
Janos Follath24eed8d2019-11-22 13:21:35 +00001046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -08001047 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +01001048 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Janos Follathb8fc1b02018-09-03 15:37:01 +01001050 /*
1051 * If the modulus is 1024 bit long or shorter, then the security strength of
1052 * the RSA algorithm is less than or equal to 80 bits and therefore an error
1053 * rate of 2^-80 is sufficient.
1054 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +01001056 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 }
Janos Follathb8fc1b02018-09-03 15:37:01 +01001058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 mbedtls_mpi_init(&H);
1060 mbedtls_mpi_init(&G);
1061 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001063 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +01001064 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1065 goto cleanup;
1066 }
1067
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001068 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1070 goto cleanup;
1071 }
1072
1073 /*
1074 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001075 * 1. |P-Q| > 2^( nbits / 2 - 100 )
1076 * 2. GCD( E, (P-1)*(Q-1) ) == 1
1077 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +00001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 do {
1082 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
1083 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
1086 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001088 /* 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 +01001089 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
1090 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001093
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001094 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (H.s < 0) {
1096 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
1097 }
Janos Follathef441782016-09-21 13:18:12 +01001098
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001099 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
1101 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
1102 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001103
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001104 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
1106 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -08001107 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001109
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001110 /* 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 +01001111 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
1112 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
1113 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 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 -08001116 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001118
1119 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001122 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
1124 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001129
Jethro Beekman97f95c92018-02-13 15:50:36 -08001130#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 * DP = D mod (P - 1)
1133 * DQ = D mod (Q - 1)
1134 * QP = Q^-1 mod P
1135 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
1137 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001138#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Hanno Becker83aad1f2017-08-23 06:45:10 +01001140 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
1143cleanup:
1144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 mbedtls_mpi_free(&H);
1146 mbedtls_mpi_free(&G);
1147 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (ret != 0) {
1150 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +01001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if ((-ret & ~0x7f) == 0) {
1153 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
1154 }
1155 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 }
1157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001159}
1160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
1163/*
1164 * Check a public RSA key
1165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001166int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001167{
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
1169 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001170 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
1173 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001174 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
1177 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
1178 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
1179 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1180 }
1181
1182 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001183}
1184
1185/*
Hanno Becker705fc682017-10-10 17:57:02 +01001186 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001188int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001189{
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
1191 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
1192 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 }
Paul Bakker48377d92013-08-30 12:06:24 +02001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
1196 &ctx->D, &ctx->E, NULL, NULL) != 0) {
1197 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001199
Hanno Beckerb269a852017-08-25 08:03:21 +01001200#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
1202 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
1203 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +01001204 }
1205#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001208}
1209
1210/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001211 * Check if contexts holding a public and private key match
1212 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001213int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
1214 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001215{
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
1217 mbedtls_rsa_check_privkey(prv) != 0) {
1218 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001219 }
1220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
1222 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
1223 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001224 }
1225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001227}
1228
1229/*
Janos Follath6872c5f2024-08-22 13:00:12 +01001230 * This function is identical to mbedtls_mpi_exp_mod() the only difference is that this function is
1231 * not constant time.
1232 *
1233 * WARNING! This function is not constant time.
1234 */
1235int mbedtls_mpi_exp_mod_unsafe(mbedtls_mpi *X, const mbedtls_mpi *A,
1236 const mbedtls_mpi *E, const mbedtls_mpi *N,
1237 mbedtls_mpi *prec_RR);
1238
1239/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 * Do an RSA public key operation
1241 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001242int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
1243 const unsigned char *input,
1244 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001245{
Janos Follath24eed8d2019-11-22 13:21:35 +00001246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001247 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
1251 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1252 }
Hanno Becker705fc682017-10-10 17:57:02 +01001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001256#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1258 return ret;
1259 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001260#endif
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001265 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1266 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 }
1268
1269 olen = ctx->len;
Janos Follath87253af2024-08-15 16:06:19 +01001270 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod_unsafe(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
1273cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1276 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1277 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (ret != 0) {
1283 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
1284 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001287}
1288
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001289/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001290 * Generate or update blinding values, see section 10 of:
1291 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001292 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001293 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1296 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001297{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001298 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001299 mbedtls_mpi R;
1300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001304 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1306 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1307 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1308 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001309
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001310 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001311 }
1312
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001313 /* Unblinding value: Vf = random number, invertible mod N */
1314 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +02001316 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
1317 goto cleanup;
1318 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 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 +02001321
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001322 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
1324 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
1325 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001326
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001327 /* At this point, Vi is invertible mod N if and only if both Vf and R
1328 * are invertible mod N. If one of them isn't, we don't need to know
1329 * which one, we just loop and choose new values for both of them.
1330 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
1332 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001333 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -05001337
1338 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
1340 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001341
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001342 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001343 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 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 +02001345
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001346
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001347cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001351}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001352
Paul Bakker5121ce52009-01-03 21:22:43 +00001353/*
Janos Follathd6b09652023-11-21 09:33:54 +00001354 * Unblind
1355 * T = T * Vf mod N
1356 */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001357static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, const mbedtls_mpi *N)
Janos Follathd6b09652023-11-21 09:33:54 +00001358{
1359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1360 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
1361 const size_t nlimbs = N->n;
1362 const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
1363 mbedtls_mpi RR, M_T;
1364
1365 mbedtls_mpi_init(&RR);
1366 mbedtls_mpi_init(&M_T);
1367
1368 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
1369 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
1370
1371 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
1372 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
1373
Janos Follathe6750b22023-12-27 10:22:59 +00001374 /* T = T * Vf mod N
1375 * Reminder: montmul(A, B, N) = A * B * R^-1 mod N
1376 * Usually both operands are multiplied by R mod N beforehand (by calling
1377 * `to_mont_rep()` on them), yielding a result that's also * R mod N (aka
1378 * "in the Montgomery domain"). Here we only multiply one operand by R mod
1379 * N, so the result is directly what we want - no need to call
1380 * `from_mont_rep()` on it. */
Janos Follathd6b09652023-11-21 09:33:54 +00001381 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 +00001382 mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1383
1384cleanup:
1385
1386 mbedtls_mpi_free(&RR);
1387 mbedtls_mpi_free(&M_T);
1388
1389 return ret;
1390}
1391
1392/*
Janos Follathe81102e2017-03-22 13:38:28 +00001393 * Exponent blinding supposed to prevent side-channel attacks using multiple
1394 * traces of measurements to recover the RSA key. The more collisions are there,
1395 * the more bits of the key can be recovered. See [3].
1396 *
1397 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001398 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001399 *
1400 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001401 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001402 *
1403 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1404 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1405 * Thus in this sense with 28 byte blinding the security is not reduced by
1406 * side-channel attacks like the one in [3])
1407 *
1408 * This countermeasure does not help if the key recovery is possible with a
1409 * single trace.
1410 */
1411#define RSA_EXPONENT_BLINDING 28
1412
1413/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 * Do an RSA private key operation
1415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001416int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1417 int (*f_rng)(void *, unsigned char *, size_t),
1418 void *p_rng,
1419 const unsigned char *input,
1420 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001421{
Janos Follath24eed8d2019-11-22 13:21:35 +00001422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001423 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001424
1425 /* Temporary holding the result */
1426 mbedtls_mpi T;
1427
1428 /* Temporaries holding P-1, Q-1 and the
1429 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001430 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001431
1432#if !defined(MBEDTLS_RSA_NO_CRT)
1433 /* Temporaries holding the results mod p resp. mod q. */
1434 mbedtls_mpi TP, TQ;
1435
1436 /* Temporaries holding the blinded exponents for
1437 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001438 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001439#else
1440 /* Temporary holding the blinded exponent (if used). */
1441 mbedtls_mpi D_blind;
Hanno Becker43f94722017-08-25 11:50:00 +01001442#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001443
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001444 /* Temporaries holding the initial input and the double
1445 * checked result; should be the same in the end. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001446 mbedtls_mpi input_blinded, check_result_blinded;
Paul Bakker5121ce52009-01-03 21:22:43 +00001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if (f_rng == NULL) {
1449 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1450 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (rsa_check_context(ctx, 1 /* private key checks */,
1453 1 /* blinding on */) != 0) {
1454 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001455 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001456
Hanno Becker06811ce2017-05-03 15:10:34 +01001457#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1459 return ret;
1460 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001461#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001462
Hanno Becker06811ce2017-05-03 15:10:34 +01001463 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 mbedtls_mpi_init(&P1);
1467 mbedtls_mpi_init(&Q1);
1468 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001469
Janos Follathe81102e2017-03-22 13:38:28 +00001470#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001472#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 mbedtls_mpi_init(&DP_blind);
1474 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001475#endif
1476
Hanno Becker06811ce2017-05-03 15:10:34 +01001477#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001479#endif
1480
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001481 mbedtls_mpi_init(&input_blinded);
1482 mbedtls_mpi_init(&check_result_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001483
1484 /* End of MPI initialization */
1485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1487 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001488 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1489 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 }
1491
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001492 /*
1493 * Blinding
1494 * T = T * Vi mod N
1495 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1497 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1498 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001499
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001500 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&input_blinded, &T));
Janos Follath6bcbc922023-11-21 09:46:43 +00001501
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001502 /*
1503 * Exponent blinding
1504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1506 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001507
Janos Follathf9203b42017-03-22 15:13:15 +00001508#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001509 /*
1510 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1513 f_rng, p_rng));
1514 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1515 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1516 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathf9203b42017-03-22 15:13:15 +00001517#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001518 /*
1519 * DP_blind = ( P - 1 ) * R + DP
1520 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1522 f_rng, p_rng));
1523 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1524 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1525 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001526
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001527 /*
1528 * DQ_blind = ( Q - 1 ) * R + DQ
1529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1531 f_rng, p_rng));
1532 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1533 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1534 &ctx->DQ));
Janos Follathe81102e2017-03-22 13:38:28 +00001535#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath47ee7702023-12-27 10:33:00 +00001538 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &D_blind, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001539#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001540 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001541 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001543 * TP = input ^ dP mod P
1544 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001546
Janos Follath47ee7702023-12-27 10:33:00 +00001547 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, &DP_blind, &ctx->P, &ctx->RP));
1548 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, &DQ_blind, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001549
1550 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001551 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1554 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1555 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001556
1557 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001558 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001559 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1561 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001563
Hanno Becker2dec5e82017-10-03 07:49:52 +01001564 /* Verify the result to prevent glitching attacks. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001565 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&check_result_blinded, &T, &ctx->E,
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 &ctx->N, &ctx->RN));
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001567 if (mbedtls_mpi_cmp_mpi(&check_result_blinded, &input_blinded) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001568 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1569 goto cleanup;
1570 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001571
Janos Follath6bcbc922023-11-21 09:46:43 +00001572 /*
1573 * Unblind
1574 * T = T * Vf mod N
1575 */
1576 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1577
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001580
1581cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1584 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1585 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001586#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 mbedtls_mpi_free(&P1);
1589 mbedtls_mpi_free(&Q1);
1590 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001591
Janos Follathe81102e2017-03-22 13:38:28 +00001592#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001594#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 mbedtls_mpi_free(&DP_blind);
1596 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001597#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001600
1601#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001603#endif
1604
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001605 mbedtls_mpi_free(&check_result_blinded);
1606 mbedtls_mpi_free(&input_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 if (ret != 0 && ret >= -0x007f) {
1609 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1610 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001613}
1614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001616/**
1617 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1618 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001619 * \param dst buffer to mask
1620 * \param dlen length of destination buffer
1621 * \param src source of the mask generation
1622 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001623 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001624 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001625static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1626 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001627{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001628 unsigned char counter[4];
1629 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001630 unsigned int hlen;
1631 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001632 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001633 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001634 const mbedtls_md_info_t *md_info;
1635 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 mbedtls_md_init(&md_ctx);
1638 md_info = mbedtls_md_info_from_type(md_alg);
1639 if (md_info == NULL) {
1640 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1641 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 mbedtls_md_init(&md_ctx);
1644 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001645 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 memset(mask, 0, sizeof(mask));
1651 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001652
Simon Butcher02037452016-03-01 21:19:12 +00001653 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001654 p = dst;
1655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001657 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001659 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001663 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 }
1665 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 }
1668 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001669 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 }
1671 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001672 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001676 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001678
1679 counter[3]++;
1680
1681 dlen -= use_len;
1682 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001683
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001684exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001689}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001690
1691/**
1692 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1693 *
1694 * \param hash the input hash
1695 * \param hlen length of the input hash
1696 * \param salt the input salt
1697 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001698 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001699 * \param md_alg message digest to use
1700 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001701static int hash_mprime(const unsigned char *hash, size_t hlen,
1702 const unsigned char *salt, size_t slen,
1703 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001704{
1705 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001706
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001707 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1711 if (md_info == NULL) {
1712 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1713 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 mbedtls_md_init(&md_ctx);
1716 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001717 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 }
1719 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001720 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 }
1722 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001723 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 }
1725 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001726 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 }
1728 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001729 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 }
1731 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001732 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001734
1735exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001739}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001740
1741/**
1742 * Compute a hash.
1743 *
1744 * \param md_alg algorithm to use
1745 * \param input input message to hash
1746 * \param ilen input length
1747 * \param output the output buffer - must be large enough for \p md_alg
1748 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001749static int compute_hash(mbedtls_md_type_t md_alg,
1750 const unsigned char *input, size_t ilen,
1751 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001752{
1753 const mbedtls_md_info_t *md_info;
1754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 md_info = mbedtls_md_info_from_type(md_alg);
1756 if (md_info == NULL) {
1757 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1758 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001761}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001765/*
1766 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1767 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001768int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1769 int (*f_rng)(void *, unsigned char *, size_t),
1770 void *p_rng,
1771 const unsigned char *label, size_t label_len,
1772 size_t ilen,
1773 const unsigned char *input,
1774 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001775{
1776 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001777 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001778 unsigned char *p = output;
1779 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (f_rng == NULL) {
1782 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1783 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001784
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001785 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 if (hlen == 0) {
1787 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1788 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001789
1790 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001791
Simon Butcher02037452016-03-01 21:19:12 +00001792 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1794 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1795 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001798
1799 *p++ = 0;
1800
Simon Butcher02037452016-03-01 21:19:12 +00001801 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1803 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1804 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001805
1806 p += hlen;
1807
Simon Butcher02037452016-03-01 21:19:12 +00001808 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1810 if (ret != 0) {
1811 return ret;
1812 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001813 p += hlen;
1814 p += olen - 2 * hlen - 2 - ilen;
1815 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 if (ilen != 0) {
1817 memcpy(p, input, ilen);
1818 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001819
Simon Butcher02037452016-03-01 21:19:12 +00001820 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001822 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 return ret;
1824 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001825
Simon Butcher02037452016-03-01 21:19:12 +00001826 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001828 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 return ret;
1830 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001833}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001837/*
1838 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1839 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001840int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1841 int (*f_rng)(void *, unsigned char *, size_t),
1842 void *p_rng, size_t ilen,
1843 const unsigned char *input,
1844 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001845{
1846 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001847 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001848 unsigned char *p = output;
1849
Paul Bakkerb3869132013-02-28 17:21:01 +01001850 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001851
Simon Butcher02037452016-03-01 21:19:12 +00001852 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (ilen + 11 < ilen || olen < ilen + 11) {
1854 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1855 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001856
1857 nb_pad = olen - 3 - ilen;
1858
1859 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001860
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (f_rng == NULL) {
1862 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1863 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001864
1865 *p++ = MBEDTLS_RSA_CRYPT;
1866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001868 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001869
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001870 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 ret = f_rng(p_rng, p, 1);
1872 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001873
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001874 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if (rng_dl == 0 || ret != 0) {
1876 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1877 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001878
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001879 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001880 }
1881
1882 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 if (ilen != 0) {
1884 memcpy(p, input, ilen);
1885 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001888}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001890
Paul Bakker5121ce52009-01-03 21:22:43 +00001891/*
1892 * Add the message padding, then do an RSA operation
1893 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001894int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1895 int (*f_rng)(void *, unsigned char *, size_t),
1896 void *p_rng,
1897 size_t ilen,
1898 const unsigned char *input,
1899 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001900{
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#if defined(MBEDTLS_PKCS1_V15)
1903 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1905 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001906#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#if defined(MBEDTLS_PKCS1_V21)
1909 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1911 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001912#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001913
1914 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001917}
1918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001920/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001921 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001923int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1924 int (*f_rng)(void *, unsigned char *, size_t),
1925 void *p_rng,
1926 const unsigned char *label, size_t label_len,
1927 size_t *olen,
1928 const unsigned char *input,
1929 unsigned char *output,
1930 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001931{
Janos Follath24eed8d2019-11-22 13:21:35 +00001932 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001933 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001934 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001935 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001937 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001938 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001939
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001940 /*
1941 * Parameters sanity checks
1942 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1944 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1945 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001946
1947 ilen = ctx->len;
1948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (ilen < 16 || ilen > sizeof(buf)) {
1950 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1951 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001952
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001953 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 if (hlen == 0) {
1955 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1956 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001957
Janos Follathc17cda12016-02-11 11:08:18 +00001958 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if (2 * hlen + 2 > ilen) {
1960 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1961 }
Janos Follathc17cda12016-02-11 11:08:18 +00001962
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001963 /*
1964 * RSA operation
1965 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001969 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001972 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001973 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001974 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001975 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001977 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 /* DB: Apply dbMask to maskedDB */
1979 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001980 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001981 goto cleanup;
1982 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001983
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001984 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1986 label, label_len, lhash);
1987 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001988 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001990
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001991 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001992 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001993 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 p = buf;
1995
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001996 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001997
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001998 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001999
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01002000 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002001 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01002002 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01002003
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002004 /* Get zero-padding len, but always read till end of buffer
2005 * (minus one, for the 01 byte) */
2006 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01002007 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01002009 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
2010 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002011 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002012
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002013 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002014 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01002015
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002016 /*
2017 * The only information "leaked" is whether the padding was correct or not
2018 * (eg, no data is copied if it was not correct). This meets the
2019 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
2020 * the different error conditions.
2021 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002022 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002023 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2024 goto cleanup;
2025 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002026
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002027 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002028 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
2029 goto cleanup;
2030 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002031
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002032 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 if (*olen != 0) {
2034 memcpy(output, p, *olen);
2035 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002036 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002037
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002038cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 mbedtls_platform_zeroize(buf, sizeof(buf));
2040 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002043}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002047/*
2048 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
2049 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002050int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
2051 int (*f_rng)(void *, unsigned char *, size_t),
2052 void *p_rng,
2053 size_t *olen,
2054 const unsigned char *input,
2055 unsigned char *output,
2056 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002057{
2058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2059 size_t ilen;
2060 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
2061
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002062 ilen = ctx->len;
2063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2065 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2066 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 if (ilen < 16 || ilen > sizeof(buf)) {
2069 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2070 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002075 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
2079 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002080
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002081cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
2088/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002089 * Do an RSA operation, then remove the message padding
2090 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
2092 int (*f_rng)(void *, unsigned char *, size_t),
2093 void *p_rng,
2094 size_t *olen,
2095 const unsigned char *input,
2096 unsigned char *output,
2097 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01002098{
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#if defined(MBEDTLS_PKCS1_V15)
2101 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
2103 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02002104#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#if defined(MBEDTLS_PKCS1_V21)
2107 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
2109 olen, input, output,
2110 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01002111#endif
2112
2113 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002115 }
2116}
2117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002119static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2120 int (*f_rng)(void *, unsigned char *, size_t),
2121 void *p_rng,
2122 mbedtls_md_type_t md_alg,
2123 unsigned int hashlen,
2124 const unsigned char *hash,
2125 int saltlen,
2126 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002127{
2128 size_t olen;
2129 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002130 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002131 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00002132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002133 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002134 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002137 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002139
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (f_rng == NULL) {
2141 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2142 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002143
2144 olen = ctx->len;
2145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002147 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002148 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (exp_hashlen == 0) {
2150 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2151 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (hashlen != exp_hashlen) {
2154 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2155 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002156 }
2157
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002158 hash_id = (mbedtls_md_type_t) ctx->hash_id;
2159 if (hash_id == MBEDTLS_MD_NONE) {
2160 hash_id = md_alg;
2161 }
2162 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (hlen == 0) {
2164 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2165 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
2168 /* Calculate the largest possible salt length, up to the hash size.
2169 * Normally this is the hash length, which is the maximum salt length
2170 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
2171 * enough room, use the maximum salt length that fits. The constraint is
2172 * that the hash length plus the salt length plus 2 bytes must be at most
2173 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
2174 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002175 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (olen < hlen + min_slen + 2) {
2177 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2178 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002179 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002181 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 }
2183 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
2184 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2185 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02002186 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002187 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01002190
Simon Butcher02037452016-03-01 21:19:12 +00002191 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002193 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01002194 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002195
2196 /* Generate salt of length slen in place in the encoded message */
2197 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
2199 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
2200 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02002201
Paul Bakkerb3869132013-02-28 17:21:01 +01002202 p += slen;
2203
Simon Butcher02037452016-03-01 21:19:12 +00002204 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002205 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if (ret != 0) {
2207 return ret;
2208 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002209
Simon Butcher02037452016-03-01 21:19:12 +00002210 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002212 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002214
Simon Butcher02037452016-03-01 21:19:12 +00002215 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002216 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (ret != 0) {
2218 return ret;
2219 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2222 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01002223
2224 p += hlen;
2225 *p++ = 0xBC;
2226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002228}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002229
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002230static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2231 int (*f_rng)(void *, unsigned char *, size_t),
2232 void *p_rng,
2233 mbedtls_md_type_t md_alg,
2234 unsigned int hashlen,
2235 const unsigned char *hash,
2236 int saltlen,
2237 unsigned char *sig)
2238{
2239 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2240 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2241 }
Valerio Settie700d802024-02-26 13:52:34 +01002242 if ((ctx->hash_id == MBEDTLS_MD_NONE) && (md_alg == MBEDTLS_MD_NONE)) {
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002243 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2244 }
2245 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
2246 sig);
2247}
2248
2249int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2250 int (*f_rng)(void *, unsigned char *, size_t),
2251 void *p_rng,
2252 mbedtls_md_type_t md_alg,
2253 unsigned int hashlen,
2254 const unsigned char *hash,
2255 unsigned char *sig)
2256{
2257 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
2258 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2259}
2260
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002261/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02002262 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
2263 * the option to pass in the salt length.
2264 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002265int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
2266 int (*f_rng)(void *, unsigned char *, size_t),
2267 void *p_rng,
2268 mbedtls_md_type_t md_alg,
2269 unsigned int hashlen,
2270 const unsigned char *hash,
2271 int saltlen,
2272 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02002273{
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2275 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02002276}
2277
Cédric Meuterf3fab332020-04-25 11:30:45 +02002278/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002279 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
2280 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002281int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2282 int (*f_rng)(void *, unsigned char *, size_t),
2283 void *p_rng,
2284 mbedtls_md_type_t md_alg,
2285 unsigned int hashlen,
2286 const unsigned char *hash,
2287 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002288{
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2290 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002291}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002295/*
2296 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
2297 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002298
2299/* Construct a PKCS v1.5 encoding of a hashed message
2300 *
2301 * This is used both for signature generation and verification.
2302 *
2303 * Parameters:
2304 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002305 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002306 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002307 * - hash: Buffer containing the hashed message or the raw data.
2308 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002309 * - dst: Buffer to hold the encoded message.
2310 *
2311 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002312 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002313 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002314 *
2315 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002316static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
2317 unsigned int hashlen,
2318 const unsigned char *hash,
2319 size_t dst_len,
2320 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01002321{
2322 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002323 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002324 unsigned char *p = dst;
2325 const char *oid = NULL;
2326
2327 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002329 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 if (md_size == 0) {
2331 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2332 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2335 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2336 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if (hashlen != md_size) {
2339 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2340 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002341
2342 /* Double-check that 8 + hashlen + oid_size can be used as a
2343 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01002345 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 10 + hashlen + oid_size < 10 + hashlen) {
2347 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2348 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002349
2350 /*
2351 * Static bounds check:
2352 * - Need 10 bytes for five tag-length pairs.
2353 * (Insist on 1-byte length encodings to protect against variants of
2354 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2355 * - Need hashlen bytes for hash
2356 * - Need oid_size bytes for hash alg OID.
2357 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (nb_pad < 10 + hashlen + oid_size) {
2359 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2360 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002361 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 } else {
2363 if (nb_pad < hashlen) {
2364 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2365 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002366
2367 nb_pad -= hashlen;
2368 }
2369
Hanno Becker2b2f8982017-09-27 17:10:03 +01002370 /* Need space for signature header and padding delimiter (3 bytes),
2371 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 if (nb_pad < 3 + 8) {
2373 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2374 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002375 nb_pad -= 3;
2376
2377 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002378 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002379
2380 /* Write signature header and padding */
2381 *p++ = 0;
2382 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002384 p += nb_pad;
2385 *p++ = 0;
2386
2387 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 if (md_alg == MBEDTLS_MD_NONE) {
2389 memcpy(p, hash, hashlen);
2390 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002391 }
2392
2393 /* Signing hashed data, add corresponding ASN.1 structure
2394 *
2395 * DigestInfo ::= SEQUENCE {
2396 * digestAlgorithm DigestAlgorithmIdentifier,
2397 * digest Digest }
2398 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2399 * Digest ::= OCTET STRING
2400 *
2401 * Schematic:
2402 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2403 * TAG-NULL + LEN [ NULL ] ]
2404 * TAG-OCTET + LEN [ HASH ] ]
2405 */
2406 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002408 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002410 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002411 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002413 p += oid_size;
2414 *p++ = MBEDTLS_ASN1_NULL;
2415 *p++ = 0x00;
2416 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002417 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002419 p += hashlen;
2420
2421 /* Just a sanity-check, should be automatic
2422 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (p != dst + dst_len) {
2424 mbedtls_platform_zeroize(dst, dst_len);
2425 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002426 }
2427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002429}
2430
Paul Bakkerb3869132013-02-28 17:21:01 +01002431/*
2432 * Do an RSA operation to sign the message digest
2433 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002434int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2435 int (*f_rng)(void *, unsigned char *, size_t),
2436 void *p_rng,
2437 mbedtls_md_type_t md_alg,
2438 unsigned int hashlen,
2439 const unsigned char *hash,
2440 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002441{
Janos Follath24eed8d2019-11-22 13:21:35 +00002442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002443 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002446 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2450 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2451 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002452
Hanno Beckerfdf38032017-09-06 12:35:55 +01002453 /*
2454 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2455 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2458 ctx->len, sig)) != 0) {
2459 return ret;
2460 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002461
Hanno Beckerfdf38032017-09-06 12:35:55 +01002462 /* Private key operation
2463 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002464 * In order to prevent Lenstra's attack, make the signature in a
2465 * temporary buffer and check it before returning it.
2466 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 sig_try = mbedtls_calloc(1, ctx->len);
2469 if (sig_try == NULL) {
2470 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002471 }
2472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 verif = mbedtls_calloc(1, ctx->len);
2474 if (verif == NULL) {
2475 mbedtls_free(sig_try);
2476 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2477 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2480 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2481
2482 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002483 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2484 goto cleanup;
2485 }
2486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002488
2489cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002490 mbedtls_zeroize_and_free(sig_try, ctx->len);
2491 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (ret != 0) {
2494 memset(sig, '!', ctx->len);
2495 }
2496 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002499
2500/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 * Do an RSA operation to sign the message digest
2502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002503int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2504 int (*f_rng)(void *, unsigned char *, size_t),
2505 void *p_rng,
2506 mbedtls_md_type_t md_alg,
2507 unsigned int hashlen,
2508 const unsigned char *hash,
2509 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002510{
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002512 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516#if defined(MBEDTLS_PKCS1_V15)
2517 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2519 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002520#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522#if defined(MBEDTLS_PKCS1_V21)
2523 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2525 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002526#endif
2527
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002531}
2532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002534/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002535 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002537int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2538 mbedtls_md_type_t md_alg,
2539 unsigned int hashlen,
2540 const unsigned char *hash,
2541 mbedtls_md_type_t mgf1_hash_id,
2542 int expected_salt_len,
2543 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002544{
Janos Follath24eed8d2019-11-22 13:21:35 +00002545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002546 size_t siglen;
2547 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002548 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002549 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002550 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002551 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002555 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002557
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 siglen = ctx->len;
2559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (siglen < 16 || siglen > sizeof(buf)) {
2561 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2562 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 if (ret != 0) {
2567 return ret;
2568 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
2570 p = buf;
2571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (buf[siglen - 1] != 0xBC) {
2573 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002574 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (md_alg != MBEDTLS_MD_NONE) {
2577 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002578 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (exp_hashlen == 0) {
2580 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2581 }
2582
2583 if (hashlen != exp_hashlen) {
2584 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2585 }
2586 }
2587
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002588 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 if (hlen == 0) {
2590 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2591 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002592
Simon Butcher02037452016-03-01 21:19:12 +00002593 /*
2594 * Note: EMSA-PSS verification is over the length of N - 1 bits
2595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if (buf[0] >> (8 - siglen * 8 + msb)) {
2599 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2600 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002601
Simon Butcher02037452016-03-01 21:19:12 +00002602 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002604 p++;
2605 siglen -= 1;
2606 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 if (siglen < hlen + 2) {
2609 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2610 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002611 hash_start = p + siglen - hlen - 1;
2612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2614 if (ret != 0) {
2615 return ret;
2616 }
Paul Bakker02303e82013-01-03 11:08:31 +01002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002621 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (*p++ != 0x01) {
2625 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2626 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002627
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002628 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2631 observed_salt_len != (size_t) expected_salt_len) {
2632 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002633 }
2634
Simon Butcher02037452016-03-01 21:19:12 +00002635 /*
2636 * Generate H = Hash( M' )
2637 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2639 result, mgf1_hash_id);
2640 if (ret != 0) {
2641 return ret;
2642 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002643
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 if (memcmp(hash_start, result, hlen) != 0) {
2645 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2646 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002647
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002649}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002650
2651/*
2652 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2653 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002654int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2655 mbedtls_md_type_t md_alg,
2656 unsigned int hashlen,
2657 const unsigned char *hash,
2658 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002659{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002660 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002662 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002667 : md_alg;
2668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2670 md_alg, hashlen, hash,
2671 mgf1_hash_id,
2672 MBEDTLS_RSA_SALT_LEN_ANY,
2673 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002674
2675}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002679/*
2680 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2681 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002682int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2683 mbedtls_md_type_t md_alg,
2684 unsigned int hashlen,
2685 const unsigned char *hash,
2686 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002687{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002688 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002689 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002690 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002693 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002695
2696 sig_len = ctx->len;
2697
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002698 /*
2699 * Prepare expected PKCS1 v1.5 encoding of hash.
2700 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2703 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002704 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2705 goto cleanup;
2706 }
2707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2709 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002710 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002712
2713 /*
2714 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2715 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 ret = mbedtls_rsa_public(ctx, sig, encoded);
2718 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002719 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002721
Simon Butcher02037452016-03-01 21:19:12 +00002722 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002723 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002724 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2727 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002728 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2729 goto cleanup;
2730 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002731
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002732cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002735 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002736 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002739 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002740 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002745
2746/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002747 * Do an RSA operation and check the message digest
2748 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002749int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2750 mbedtls_md_type_t md_alg,
2751 unsigned int hashlen,
2752 const unsigned char *hash,
2753 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002754{
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002756 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760#if defined(MBEDTLS_PKCS1_V15)
2761 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2763 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002764#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766#if defined(MBEDTLS_PKCS1_V21)
2767 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2769 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002770#endif
2771
2772 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002774 }
2775}
2776
2777/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002778 * Copy the components of an RSA key
2779 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002780int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002781{
Janos Follath24eed8d2019-11-22 13:21:35 +00002782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002783
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002784 dst->len = src->len;
2785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2787 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002788
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2790 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2791 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002792
2793#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2795 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2796 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2797 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2798 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002799#endif
2800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2804 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002805
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002806 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002807 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002808
2809cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 if (ret != 0) {
2811 mbedtls_rsa_free(dst);
2812 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002815}
2816
2817/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 * Free the components of an RSA key
2819 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002820void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002821{
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002823 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002825
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 mbedtls_mpi_free(&ctx->Vi);
2827 mbedtls_mpi_free(&ctx->Vf);
2828 mbedtls_mpi_free(&ctx->RN);
2829 mbedtls_mpi_free(&ctx->D);
2830 mbedtls_mpi_free(&ctx->Q);
2831 mbedtls_mpi_free(&ctx->P);
2832 mbedtls_mpi_free(&ctx->E);
2833 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002834
Hanno Becker33c30a02017-08-23 07:00:22 +01002835#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 mbedtls_mpi_free(&ctx->RQ);
2837 mbedtls_mpi_free(&ctx->RP);
2838 mbedtls_mpi_free(&ctx->QP);
2839 mbedtls_mpi_free(&ctx->DQ);
2840 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002841#endif /* MBEDTLS_RSA_NO_CRT */
2842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002844 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 if (ctx->ver != 0) {
2846 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002847 ctx->ver = 0;
2848 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002849#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002850}
2851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
2855/*
2856 * Example RSA-1024 keypair, for test purposes
2857 */
2858#define KEY_LEN 128
2859
2860#define RSA_N "9292758453063D803DD603D5E777D788" \
2861 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2862 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2863 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2864 "93A89813FBF3C4F8066D2D800F7C38A8" \
2865 "1AE31942917403FF4946B0A83D3D3E05" \
2866 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2867 "5E94BB77B07507233A0BC7BAC8F90F79"
2868
2869#define RSA_E "10001"
2870
2871#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2872 "66CA472BC44D253102F8B4A9D3BFA750" \
2873 "91386C0077937FE33FA3252D28855837" \
2874 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2875 "DF79C5CE07EE72C7F123142198164234" \
2876 "CABB724CF78B8173B9F880FC86322407" \
2877 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2878 "071513A1E85B5DFA031F21ECAE91A34D"
2879
2880#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2881 "2C01CAD19EA484A87EA4377637E75500" \
2882 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2883 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2884
2885#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2886 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2887 "910E4168387E3C30AA1E00C339A79508" \
2888 "8452DD96A9A5EA5D9DCA68DA636032AF"
2889
Paul Bakker5121ce52009-01-03 21:22:43 +00002890#define PT_LEN 24
2891#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2892 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002895static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002896{
gufe44c2620da2020-08-03 17:56:50 +02002897#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002898 size_t i;
2899
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002901 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 }
Paul Bakker545570e2010-07-18 09:00:25 +00002903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002905 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002907#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002909 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002911
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002913#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002918
Paul Bakker5121ce52009-01-03 21:22:43 +00002919/*
2920 * Checkup routine
2921 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002922int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002923{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002924 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002926 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 unsigned char rsa_plaintext[PT_LEN];
2929 unsigned char rsa_decrypted[PT_LEN];
2930 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002931#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002932 unsigned char sha1sum[20];
2933#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002934
Hanno Becker3a701162017-08-22 13:52:43 +01002935 mbedtls_mpi K;
2936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 mbedtls_mpi_init(&K);
2938 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2941 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2942 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2943 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2944 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2945 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2946 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2947 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2948 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2949 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002952
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 if (verbose != 0) {
2954 mbedtls_printf(" RSA key validation: ");
2955 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2958 mbedtls_rsa_check_privkey(&rsa) != 0) {
2959 if (verbose != 0) {
2960 mbedtls_printf("failed\n");
2961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Hanno Becker5bc87292017-05-03 15:09:31 +01002963 ret = 1;
2964 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002965 }
2966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 if (verbose != 0) {
2968 mbedtls_printf("passed\n PKCS#1 encryption : ");
2969 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2974 PT_LEN, rsa_plaintext,
2975 rsa_ciphertext) != 0) {
2976 if (verbose != 0) {
2977 mbedtls_printf("failed\n");
2978 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002979
Hanno Becker5bc87292017-05-03 15:09:31 +01002980 ret = 1;
2981 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002982 }
2983
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 if (verbose != 0) {
2985 mbedtls_printf("passed\n PKCS#1 decryption : ");
2986 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002987
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2989 &len, rsa_ciphertext, rsa_decrypted,
2990 sizeof(rsa_decrypted)) != 0) {
2991 if (verbose != 0) {
2992 mbedtls_printf("failed\n");
2993 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002994
Hanno Becker5bc87292017-05-03 15:09:31 +01002995 ret = 1;
2996 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 }
2998
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
3000 if (verbose != 0) {
3001 mbedtls_printf("failed\n");
3002 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
Hanno Becker5bc87292017-05-03 15:09:31 +01003004 ret = 1;
3005 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003006 }
3007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 if (verbose != 0) {
3009 mbedtls_printf("passed\n");
3010 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003011
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003012#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 if (verbose != 0) {
3014 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01003015 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01003017 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
3018 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 if (verbose != 0) {
3020 mbedtls_printf("failed\n");
3021 }
3022
3023 return 1;
3024 }
3025
3026 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
3027 MBEDTLS_MD_SHA1, 20,
3028 sha1sum, rsa_ciphertext) != 0) {
3029 if (verbose != 0) {
3030 mbedtls_printf("failed\n");
3031 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Hanno Becker5bc87292017-05-03 15:09:31 +01003033 ret = 1;
3034 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 }
3036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 if (verbose != 0) {
3038 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
3039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
3042 sha1sum, rsa_ciphertext) != 0) {
3043 if (verbose != 0) {
3044 mbedtls_printf("failed\n");
3045 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003046
Hanno Becker5bc87292017-05-03 15:09:31 +01003047 ret = 1;
3048 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003049 }
3050
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 if (verbose != 0) {
3052 mbedtls_printf("passed\n");
3053 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003054#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 if (verbose != 0) {
3057 mbedtls_printf("\n");
3058 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003059
Paul Bakker3d8fb632014-04-17 12:42:41 +02003060cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 mbedtls_mpi_free(&K);
3062 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02003064 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003067}
3068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071#endif /* MBEDTLS_RSA_C */