blob: f4c08626c6a520369eabb04b9dd0425dc00c5035 [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 Setti447bbce2024-02-07 08:02:03 +0100111 /* mbedtls_asn1_get_tag() already ensures that len is valid (i.e. p+len <= end)*/
Valerio Setti44ff9502024-02-01 16:51:05 +0100112 end = p + len;
113
114 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
115 return ret;
116 }
117
118 if (version != 0) {
119 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
120 }
121
122 /* Import N */
123 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
124 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
125 NULL, NULL)) != 0) {
126 goto cleanup;
127 }
128
129 /* Import E */
130 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
131 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
132 NULL, &T)) != 0) {
133 goto cleanup;
134 }
135
136 /* Import D */
137 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
138 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
139 &T, NULL)) != 0) {
140 goto cleanup;
141 }
142
143 /* Import P */
144 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
145 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
146 NULL, NULL)) != 0) {
147 goto cleanup;
148 }
149
150 /* Import Q */
151 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
152 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
153 NULL, NULL)) != 0) {
154 goto cleanup;
155 }
156
157#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
158 /*
159 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
160 * that they can be easily recomputed from D, P and Q. However by
161 * parsing them from the PKCS1 structure it is possible to avoid
162 * recalculating them which both reduces the overhead of loading
163 * RSA private keys into memory and also avoids side channels which
164 * can arise when computing those values, since all of D, P, and Q
165 * are secret. See https://eprint.iacr.org/2020/055 for a
166 * description of one such attack.
167 */
168
169 /* Import DP */
170 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
171 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
172 goto cleanup;
173 }
174
175 /* Import DQ */
176 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
177 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
178 goto cleanup;
179 }
180
181 /* Import QP */
182 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
183 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
184 goto cleanup;
185 }
186
187#else
188 /* Verify existence of the CRT params */
189 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
190 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
191 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
192 goto cleanup;
193 }
194#endif
195
196 /* rsa_complete() doesn't complete anything with the default
197 * implementation but is still called:
198 * - for the benefit of alternative implementation that may want to
199 * pre-compute stuff beyond what's provided (eg Montgomery factors)
200 * - as is also sanity-checks the key
201 *
202 * Furthermore, we also check the public part for consistency with
203 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
204 */
205 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
206 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
207 goto cleanup;
208 }
209
210 if (p != end) {
211 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
212 }
213
214cleanup:
215
216 mbedtls_mpi_free(&T);
217
218 if (ret != 0) {
219 mbedtls_rsa_free(rsa);
220 }
221
222 return ret;
223}
224
Valerio Setti201e6432024-02-01 17:19:37 +0100225int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +0100226{
Valerio Setti201e6432024-02-01 17:19:37 +0100227 unsigned char *p = (unsigned char *) key;
228 unsigned char *end = (unsigned char *) (key + keylen);
Valerio Setti44ff9502024-02-01 16:51:05 +0100229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
230 size_t len;
231
232 /*
233 * RSAPublicKey ::= SEQUENCE {
234 * modulus INTEGER, -- n
235 * publicExponent INTEGER -- e
236 * }
237 */
238
Valerio Setti201e6432024-02-01 17:19:37 +0100239 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
Valerio Setti44ff9502024-02-01 16:51:05 +0100240 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
241 return ret;
242 }
243
Valerio Setti447bbce2024-02-07 08:02:03 +0100244 /* mbedtls_asn1_get_tag() already ensures that len is valid (i.e. p+len <= end)*/
Valerio Settife329ce2024-02-06 08:00:18 +0100245 end = p + len;
246
Valerio Setti44ff9502024-02-01 16:51:05 +0100247 /* Import N */
Valerio Setti201e6432024-02-01 17:19:37 +0100248 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100249 return ret;
250 }
251
Valerio Setti201e6432024-02-01 17:19:37 +0100252 if ((ret = mbedtls_rsa_import_raw(rsa, p, len, NULL, 0, NULL, 0,
Valerio Setti44ff9502024-02-01 16:51:05 +0100253 NULL, 0, NULL, 0)) != 0) {
254 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
255 }
256
Valerio Setti201e6432024-02-01 17:19:37 +0100257 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100258
259 /* Import E */
Valerio Setti201e6432024-02-01 17:19:37 +0100260 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100261 return ret;
262 }
263
264 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
Valerio Setti201e6432024-02-01 17:19:37 +0100265 NULL, 0, p, len)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100266 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
267 }
268
Valerio Setti201e6432024-02-01 17:19:37 +0100269 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100270
271 if (mbedtls_rsa_complete(rsa) != 0 ||
272 mbedtls_rsa_check_pubkey(rsa) != 0) {
273 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
274 }
275
Valerio Setti201e6432024-02-01 17:19:37 +0100276 if (p != end) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100277 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
278 }
279
280 return 0;
281}
282
Valerio Setti135ebde2024-02-01 17:00:29 +0100283int mbedtls_rsa_write_key(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100284 unsigned char **p)
285{
286 size_t len = 0;
287 int ret;
288
289 mbedtls_mpi T; /* Temporary holding the exported parameters */
290
291 /*
292 * Export the parameters one after another to avoid simultaneous copies.
293 */
294
295 mbedtls_mpi_init(&T);
296
297 /* Export QP */
298 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
299 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
300 goto end_of_export;
301 }
302 len += ret;
303
304 /* Export DQ */
305 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
306 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
307 goto end_of_export;
308 }
309 len += ret;
310
311 /* Export DP */
312 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
313 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
314 goto end_of_export;
315 }
316 len += ret;
317
318 /* Export Q */
319 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, &T, NULL, NULL)) != 0 ||
320 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
321 goto end_of_export;
322 }
323 len += ret;
324
325 /* Export P */
326 if ((ret = mbedtls_rsa_export(rsa, NULL, &T, NULL, NULL, NULL)) != 0 ||
327 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
328 goto end_of_export;
329 }
330 len += ret;
331
332 /* Export D */
333 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, &T, NULL)) != 0 ||
334 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
335 goto end_of_export;
336 }
337 len += ret;
338
339 /* Export E */
340 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
341 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
342 goto end_of_export;
343 }
344 len += ret;
345
346 /* Export N */
347 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
348 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
349 goto end_of_export;
350 }
351 len += ret;
352
353end_of_export:
354
355 mbedtls_mpi_free(&T);
356 if (ret < 0) {
357 return ret;
358 }
359
360 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
361 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
362 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
363 MBEDTLS_ASN1_CONSTRUCTED |
364 MBEDTLS_ASN1_SEQUENCE));
365
366 return (int) len;
367}
368
369/*
370 * RSAPublicKey ::= SEQUENCE {
371 * modulus INTEGER, -- n
372 * publicExponent INTEGER -- e
373 * }
374 */
Valerio Setti135ebde2024-02-01 17:00:29 +0100375int mbedtls_rsa_write_pubkey(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100376 unsigned char **p)
377{
378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
379 size_t len = 0;
380 mbedtls_mpi T;
381
382 mbedtls_mpi_init(&T);
383
384 /* Export E */
385 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
386 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
387 goto end_of_export;
388 }
389 len += ret;
390
391 /* Export N */
392 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
393 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
394 goto end_of_export;
395 }
396 len += ret;
397
398end_of_export:
399
400 mbedtls_mpi_free(&T);
401 if (ret < 0) {
402 return ret;
403 }
404
405 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
406 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
407 MBEDTLS_ASN1_SEQUENCE));
408
409 return (int) len;
410}
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100411
412#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
413
414/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
415 * operation (EME-PKCS1-v1_5 decoding).
416 *
417 * \note The return value from this function is a sensitive value
418 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
419 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
420 * is often a situation that an attacker can provoke and leaking which
421 * one is the result is precisely the information the attacker wants.
422 *
423 * \param input The input buffer which is the payload inside PKCS#1v1.5
424 * encryption padding, called the "encoded message EM"
425 * by the terminology.
426 * \param ilen The length of the payload in the \p input buffer.
427 * \param output The buffer for the payload, called "message M" by the
428 * PKCS#1 terminology. This must be a writable buffer of
429 * length \p output_max_len bytes.
430 * \param olen The address at which to store the length of
431 * the payload. This must not be \c NULL.
432 * \param output_max_len The length in bytes of the output buffer \p output.
433 *
434 * \return \c 0 on success.
435 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
436 * The output buffer is too small for the unpadded payload.
437 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
438 * The input doesn't contain properly formatted padding.
439 */
440static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
441 size_t ilen,
442 unsigned char *output,
443 size_t output_max_len,
444 size_t *olen)
445{
446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
447 size_t i, plaintext_max_size;
448
449 /* The following variables take sensitive values: their value must
450 * not leak into the observable behavior of the function other than
451 * the designated outputs (output, olen, return value). Otherwise
452 * this would open the execution of the function to
453 * side-channel-based variants of the Bleichenbacher padding oracle
454 * attack. Potential side channels include overall timing, memory
455 * access patterns (especially visible to an adversary who has access
456 * to a shared memory cache), and branches (especially visible to
457 * an adversary who has access to a shared code cache or to a shared
458 * branch predictor). */
459 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100460 mbedtls_ct_condition_t bad;
461 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100462 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100463 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100464
465 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
466 : output_max_len;
467
468 /* Check and get padding length in constant time and constant
469 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100470 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100471
472
473 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
474 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100475 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100476
477 /* Read the whole buffer. Set pad_done to nonzero if we find
478 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100479 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100480 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100481 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100482 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100483 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100484 }
485
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100486 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100487 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100488
489 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100490 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100491
492 /* If the padding is valid, set plaintext_size to the number of
493 * remaining bytes after stripping the padding. If the padding
494 * is invalid, avoid leaking this fact through the size of the
495 * output: use the maximum message size that fits in the output
496 * buffer. Do it without branches to avoid leaking the padding
497 * validity through timing. RSA keys are small enough that all the
498 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100499 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100500 bad, (unsigned) plaintext_max_size,
501 (unsigned) (ilen - pad_count - 3));
502
503 /* Set output_too_large to 0 if the plaintext fits in the output
504 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100505 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100506 plaintext_max_size);
507
508 /* Set ret without branches to avoid timing attacks. Return:
509 * - INVALID_PADDING if the padding is bad (bad != 0).
510 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
511 * plaintext does not fit in the output buffer.
512 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100513 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100514 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100515 MBEDTLS_ERR_RSA_INVALID_PADDING,
516 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100517 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100518
519 /* If the padding is bad or the plaintext is too large, zero the
520 * data that we're about to copy to the output buffer.
521 * We need to copy the same amount of data
522 * from the same buffer whether the padding is good or not to
523 * avoid leaking the padding validity through overall timing or
524 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100525 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100526
527 /* If the plaintext is too large, truncate it to the buffer size.
528 * Copy anyway to avoid revealing the length through timing, because
529 * revealing the length is as bad as revealing the padding validity
530 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100531 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100532 (unsigned) plaintext_max_size,
533 (unsigned) plaintext_size);
534
535 /* Move the plaintext to the leftmost position where it can start in
536 * the working buffer, i.e. make it start plaintext_max_size from
537 * the end of the buffer. Do this with a memory access trace that
538 * does not depend on the plaintext size. After this move, the
539 * starting location of the plaintext is no longer sensitive
540 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100541 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
542 plaintext_max_size,
543 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100544
545 /* Finally copy the decrypted plaintext plus trailing zeros into the output
546 * buffer. If output_max_len is 0, then output may be an invalid pointer
547 * and the result of memcpy() would be undefined; prevent undefined
548 * behavior making sure to depend only on output_max_len (the size of the
549 * user-provided output buffer), which is independent from plaintext
550 * length, validity of padding, success of the decryption, and other
551 * secrets. */
552 if (output_max_len != 0) {
553 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
554 }
555
556 /* Report the amount of data we copied to the output buffer. In case
557 * of errors (bad padding or output too large), the value of *olen
558 * when this function returns is not specified. Making it equivalent
559 * to the good case limits the risks of leaking the padding validity. */
560 *olen = plaintext_size;
561
562 return ret;
563}
564
565#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
566
Hanno Beckera565f542017-10-11 11:00:19 +0100567#if !defined(MBEDTLS_RSA_ALT)
568
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/*
Hanno Becker617c1ae2017-08-23 14:11:24 +01001018 * Get length in bytes of RSA modulus
1019 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001020size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +01001021{
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +01001023}
1024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
1027/*
1028 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001029 *
1030 * This generation method follows the RSA key pair generation procedure of
1031 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +00001032 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001033int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
1034 int (*f_rng)(void *, unsigned char *, size_t),
1035 void *p_rng,
1036 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +00001037{
Janos Follath24eed8d2019-11-22 13:21:35 +00001038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -08001039 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +01001040 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Janos Follathb8fc1b02018-09-03 15:37:01 +01001042 /*
1043 * If the modulus is 1024 bit long or shorter, then the security strength of
1044 * the RSA algorithm is less than or equal to 80 bits and therefore an error
1045 * rate of 2^-80 is sufficient.
1046 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +01001048 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 }
Janos Follathb8fc1b02018-09-03 15:37:01 +01001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 mbedtls_mpi_init(&H);
1052 mbedtls_mpi_init(&G);
1053 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001055 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +01001056 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1057 goto cleanup;
1058 }
1059
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001060 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001061 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1062 goto cleanup;
1063 }
1064
1065 /*
1066 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001067 * 1. |P-Q| > 2^( nbits / 2 - 100 )
1068 * 2. GCD( E, (P-1)*(Q-1) ) == 1
1069 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 do {
1074 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
1075 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
1078 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001080 /* 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 +01001081 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
1082 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001083 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001086 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (H.s < 0) {
1088 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
1089 }
Janos Follathef441782016-09-21 13:18:12 +01001090
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001091 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
1093 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
1094 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001095
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001096 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
1098 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -08001099 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001101
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001102 /* 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 +01001103 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
1104 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
1105 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 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 -08001108 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001110
1111 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001113
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001114 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
1116 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001121
Jethro Beekman97f95c92018-02-13 15:50:36 -08001122#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 * DP = D mod (P - 1)
1125 * DQ = D mod (Q - 1)
1126 * QP = Q^-1 mod P
1127 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
1129 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001130#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001131
Hanno Becker83aad1f2017-08-23 06:45:10 +01001132 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
1135cleanup:
1136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 mbedtls_mpi_free(&H);
1138 mbedtls_mpi_free(&G);
1139 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (ret != 0) {
1142 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +01001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if ((-ret & ~0x7f) == 0) {
1145 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
1146 }
1147 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 }
1149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001151}
1152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
1155/*
1156 * Check a public RSA key
1157 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001158int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001159{
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
1161 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001162 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
1165 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001166 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
1169 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
1170 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
1171 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1172 }
1173
1174 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001175}
1176
1177/*
Hanno Becker705fc682017-10-10 17:57:02 +01001178 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001180int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001181{
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
1183 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
1184 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 }
Paul Bakker48377d92013-08-30 12:06:24 +02001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
1188 &ctx->D, &ctx->E, NULL, NULL) != 0) {
1189 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001191
Hanno Beckerb269a852017-08-25 08:03:21 +01001192#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
1194 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
1195 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +01001196 }
1197#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001200}
1201
1202/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001203 * Check if contexts holding a public and private key match
1204 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001205int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
1206 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001207{
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
1209 mbedtls_rsa_check_privkey(prv) != 0) {
1210 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001211 }
1212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
1214 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
1215 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001216 }
1217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001219}
1220
1221/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 * Do an RSA public key operation
1223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001224int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
1225 const unsigned char *input,
1226 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001227{
Janos Follath24eed8d2019-11-22 13:21:35 +00001228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001229 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
1233 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1234 }
Hanno Becker705fc682017-10-10 17:57:02 +01001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001237
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001238#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1240 return ret;
1241 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001242#endif
1243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001247 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1248 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 }
1250
1251 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
1253 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
1255cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1258 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1259 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001260#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if (ret != 0) {
1265 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
1266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001269}
1270
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001271/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001272 * Generate or update blinding values, see section 10 of:
1273 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001274 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001275 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1278 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001279{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001280 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001281 mbedtls_mpi R;
1282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001286 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1288 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1289 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1290 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001291
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001292 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001293 }
1294
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001295 /* Unblinding value: Vf = random number, invertible mod N */
1296 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +02001298 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
1299 goto cleanup;
1300 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 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 +02001303
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001304 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
1306 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
1307 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001308
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001309 /* At this point, Vi is invertible mod N if and only if both Vf and R
1310 * are invertible mod N. If one of them isn't, we don't need to know
1311 * which one, we just loop and choose new values for both of them.
1312 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
1314 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001315 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -05001319
1320 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
1322 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001323
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001324 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001325 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 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 +02001327
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001328
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001329cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001333}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001334
Paul Bakker5121ce52009-01-03 21:22:43 +00001335/*
Janos Follathd6b09652023-11-21 09:33:54 +00001336 * Unblind
1337 * T = T * Vf mod N
1338 */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001339static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, const mbedtls_mpi *N)
Janos Follathd6b09652023-11-21 09:33:54 +00001340{
1341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1342 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
1343 const size_t nlimbs = N->n;
1344 const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
1345 mbedtls_mpi RR, M_T;
1346
1347 mbedtls_mpi_init(&RR);
1348 mbedtls_mpi_init(&M_T);
1349
1350 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
1351 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
1352
1353 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
1354 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
1355
Janos Follathe6750b22023-12-27 10:22:59 +00001356 /* T = T * Vf mod N
1357 * Reminder: montmul(A, B, N) = A * B * R^-1 mod N
1358 * Usually both operands are multiplied by R mod N beforehand (by calling
1359 * `to_mont_rep()` on them), yielding a result that's also * R mod N (aka
1360 * "in the Montgomery domain"). Here we only multiply one operand by R mod
1361 * N, so the result is directly what we want - no need to call
1362 * `from_mont_rep()` on it. */
Janos Follathd6b09652023-11-21 09:33:54 +00001363 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 +00001364 mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1365
1366cleanup:
1367
1368 mbedtls_mpi_free(&RR);
1369 mbedtls_mpi_free(&M_T);
1370
1371 return ret;
1372}
1373
1374/*
Janos Follathe81102e2017-03-22 13:38:28 +00001375 * Exponent blinding supposed to prevent side-channel attacks using multiple
1376 * traces of measurements to recover the RSA key. The more collisions are there,
1377 * the more bits of the key can be recovered. See [3].
1378 *
1379 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001380 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001381 *
1382 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001383 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001384 *
1385 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1386 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1387 * Thus in this sense with 28 byte blinding the security is not reduced by
1388 * side-channel attacks like the one in [3])
1389 *
1390 * This countermeasure does not help if the key recovery is possible with a
1391 * single trace.
1392 */
1393#define RSA_EXPONENT_BLINDING 28
1394
1395/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 * Do an RSA private key operation
1397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1399 int (*f_rng)(void *, unsigned char *, size_t),
1400 void *p_rng,
1401 const unsigned char *input,
1402 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001403{
Janos Follath24eed8d2019-11-22 13:21:35 +00001404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001405 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001406
1407 /* Temporary holding the result */
1408 mbedtls_mpi T;
1409
1410 /* Temporaries holding P-1, Q-1 and the
1411 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001412 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001413
1414#if !defined(MBEDTLS_RSA_NO_CRT)
1415 /* Temporaries holding the results mod p resp. mod q. */
1416 mbedtls_mpi TP, TQ;
1417
1418 /* Temporaries holding the blinded exponents for
1419 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001420 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001421#else
1422 /* Temporary holding the blinded exponent (if used). */
1423 mbedtls_mpi D_blind;
Hanno Becker43f94722017-08-25 11:50:00 +01001424#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001425
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001426 /* Temporaries holding the initial input and the double
1427 * checked result; should be the same in the end. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001428 mbedtls_mpi input_blinded, check_result_blinded;
Paul Bakker5121ce52009-01-03 21:22:43 +00001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (f_rng == NULL) {
1431 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1432 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if (rsa_check_context(ctx, 1 /* private key checks */,
1435 1 /* blinding on */) != 0) {
1436 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001437 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001438
Hanno Becker06811ce2017-05-03 15:10:34 +01001439#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1441 return ret;
1442 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001443#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001444
Hanno Becker06811ce2017-05-03 15:10:34 +01001445 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 mbedtls_mpi_init(&P1);
1449 mbedtls_mpi_init(&Q1);
1450 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001451
Janos Follathe81102e2017-03-22 13:38:28 +00001452#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001454#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 mbedtls_mpi_init(&DP_blind);
1456 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001457#endif
1458
Hanno Becker06811ce2017-05-03 15:10:34 +01001459#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001461#endif
1462
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001463 mbedtls_mpi_init(&input_blinded);
1464 mbedtls_mpi_init(&check_result_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001465
1466 /* End of MPI initialization */
1467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1469 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001470 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1471 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 }
1473
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001474 /*
1475 * Blinding
1476 * T = T * Vi mod N
1477 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1479 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1480 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001481
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001482 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&input_blinded, &T));
Janos Follath6bcbc922023-11-21 09:46:43 +00001483
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001484 /*
1485 * Exponent blinding
1486 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1488 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001489
Janos Follathf9203b42017-03-22 15:13:15 +00001490#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001491 /*
1492 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1495 f_rng, p_rng));
1496 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1497 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1498 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathf9203b42017-03-22 15:13:15 +00001499#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001500 /*
1501 * DP_blind = ( P - 1 ) * R + DP
1502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1504 f_rng, p_rng));
1505 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1506 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1507 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001508
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001509 /*
1510 * DQ_blind = ( Q - 1 ) * R + DQ
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(&DQ_blind, &Q1, &R));
1515 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1516 &ctx->DQ));
Janos Follathe81102e2017-03-22 13:38:28 +00001517#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath47ee7702023-12-27 10:33:00 +00001520 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &D_blind, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001521#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001522 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001523 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001524 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001525 * TP = input ^ dP mod P
1526 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001528
Janos Follath47ee7702023-12-27 10:33:00 +00001529 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, &DP_blind, &ctx->P, &ctx->RP));
1530 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, &DQ_blind, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001531
1532 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001533 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1536 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1537 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
1539 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001540 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001541 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1543 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001545
Hanno Becker2dec5e82017-10-03 07:49:52 +01001546 /* Verify the result to prevent glitching attacks. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001547 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&check_result_blinded, &T, &ctx->E,
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 &ctx->N, &ctx->RN));
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001549 if (mbedtls_mpi_cmp_mpi(&check_result_blinded, &input_blinded) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001550 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1551 goto cleanup;
1552 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001553
Janos Follath6bcbc922023-11-21 09:46:43 +00001554 /*
1555 * Unblind
1556 * T = T * Vf mod N
1557 */
1558 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1559
Paul Bakker5121ce52009-01-03 21:22:43 +00001560 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001562
1563cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1566 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1567 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001568#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 mbedtls_mpi_free(&P1);
1571 mbedtls_mpi_free(&Q1);
1572 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001573
Janos Follathe81102e2017-03-22 13:38:28 +00001574#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001576#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 mbedtls_mpi_free(&DP_blind);
1578 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001579#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001582
1583#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001585#endif
1586
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001587 mbedtls_mpi_free(&check_result_blinded);
1588 mbedtls_mpi_free(&input_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (ret != 0 && ret >= -0x007f) {
1591 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1592 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001595}
1596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001598/**
1599 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1600 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001601 * \param dst buffer to mask
1602 * \param dlen length of destination buffer
1603 * \param src source of the mask generation
1604 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001605 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1608 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001609{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001610 unsigned char counter[4];
1611 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001612 unsigned int hlen;
1613 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001614 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001615 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001616 const mbedtls_md_info_t *md_info;
1617 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 mbedtls_md_init(&md_ctx);
1620 md_info = mbedtls_md_info_from_type(md_alg);
1621 if (md_info == NULL) {
1622 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1623 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 mbedtls_md_init(&md_ctx);
1626 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001627 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 memset(mask, 0, sizeof(mask));
1633 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001634
Simon Butcher02037452016-03-01 21:19:12 +00001635 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001636 p = dst;
1637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001639 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001641 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001645 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 }
1647 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001648 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 }
1650 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001651 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 }
1653 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001654 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001658 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001660
1661 counter[3]++;
1662
1663 dlen -= use_len;
1664 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001665
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001666exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001671}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001672
1673/**
1674 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1675 *
1676 * \param hash the input hash
1677 * \param hlen length of the input hash
1678 * \param salt the input salt
1679 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001680 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001681 * \param md_alg message digest to use
1682 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001683static int hash_mprime(const unsigned char *hash, size_t hlen,
1684 const unsigned char *salt, size_t slen,
1685 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001686{
1687 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001688
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001689 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1693 if (md_info == NULL) {
1694 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1695 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 mbedtls_md_init(&md_ctx);
1698 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001699 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 }
1701 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001702 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 }
1704 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001705 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 }
1707 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001708 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 }
1710 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001711 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 }
1713 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001714 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001716
1717exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001721}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001722
1723/**
1724 * Compute a hash.
1725 *
1726 * \param md_alg algorithm to use
1727 * \param input input message to hash
1728 * \param ilen input length
1729 * \param output the output buffer - must be large enough for \p md_alg
1730 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001731static int compute_hash(mbedtls_md_type_t md_alg,
1732 const unsigned char *input, size_t ilen,
1733 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001734{
1735 const mbedtls_md_info_t *md_info;
1736
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 md_info = mbedtls_md_info_from_type(md_alg);
1738 if (md_info == NULL) {
1739 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1740 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001747/*
1748 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1749 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001750int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1751 int (*f_rng)(void *, unsigned char *, size_t),
1752 void *p_rng,
1753 const unsigned char *label, size_t label_len,
1754 size_t ilen,
1755 const unsigned char *input,
1756 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001757{
1758 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001759 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001760 unsigned char *p = output;
1761 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001762
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 if (f_rng == NULL) {
1764 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1765 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001766
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001767 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 if (hlen == 0) {
1769 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1770 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001771
1772 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001773
Simon Butcher02037452016-03-01 21:19:12 +00001774 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1776 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1777 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001780
1781 *p++ = 0;
1782
Simon Butcher02037452016-03-01 21:19:12 +00001783 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1785 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1786 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001787
1788 p += hlen;
1789
Simon Butcher02037452016-03-01 21:19:12 +00001790 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1792 if (ret != 0) {
1793 return ret;
1794 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001795 p += hlen;
1796 p += olen - 2 * hlen - 2 - ilen;
1797 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 if (ilen != 0) {
1799 memcpy(p, input, ilen);
1800 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001801
Simon Butcher02037452016-03-01 21:19:12 +00001802 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001804 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 return ret;
1806 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001807
Simon Butcher02037452016-03-01 21:19:12 +00001808 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001810 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 return ret;
1812 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001819/*
1820 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1821 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001822int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1823 int (*f_rng)(void *, unsigned char *, size_t),
1824 void *p_rng, size_t ilen,
1825 const unsigned char *input,
1826 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001827{
1828 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001829 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001830 unsigned char *p = output;
1831
Paul Bakkerb3869132013-02-28 17:21:01 +01001832 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001833
Simon Butcher02037452016-03-01 21:19:12 +00001834 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 if (ilen + 11 < ilen || olen < ilen + 11) {
1836 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1837 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001838
1839 nb_pad = olen - 3 - ilen;
1840
1841 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (f_rng == NULL) {
1844 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1845 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001846
1847 *p++ = MBEDTLS_RSA_CRYPT;
1848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001850 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001851
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001852 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 ret = f_rng(p_rng, p, 1);
1854 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001855
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001856 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 if (rng_dl == 0 || ret != 0) {
1858 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1859 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001860
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001861 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001862 }
1863
1864 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 if (ilen != 0) {
1866 memcpy(p, input, ilen);
1867 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001870}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001872
Paul Bakker5121ce52009-01-03 21:22:43 +00001873/*
1874 * Add the message padding, then do an RSA operation
1875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001876int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1877 int (*f_rng)(void *, unsigned char *, size_t),
1878 void *p_rng,
1879 size_t ilen,
1880 const unsigned char *input,
1881 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001882{
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884#if defined(MBEDTLS_PKCS1_V15)
1885 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1887 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001888#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890#if defined(MBEDTLS_PKCS1_V21)
1891 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1893 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001894#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001895
1896 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001899}
1900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001902/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001903 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001905int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1906 int (*f_rng)(void *, unsigned char *, size_t),
1907 void *p_rng,
1908 const unsigned char *label, size_t label_len,
1909 size_t *olen,
1910 const unsigned char *input,
1911 unsigned char *output,
1912 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001913{
Janos Follath24eed8d2019-11-22 13:21:35 +00001914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001915 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001916 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001917 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001919 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001920 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001921
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001922 /*
1923 * Parameters sanity checks
1924 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1926 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1927 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
1929 ilen = ctx->len;
1930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 if (ilen < 16 || ilen > sizeof(buf)) {
1932 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001934
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001935 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if (hlen == 0) {
1937 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1938 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001939
Janos Follathc17cda12016-02-11 11:08:18 +00001940 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 if (2 * hlen + 2 > ilen) {
1942 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1943 }
Janos Follathc17cda12016-02-11 11:08:18 +00001944
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001945 /*
1946 * RSA operation
1947 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001951 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001953
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001954 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001955 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001956 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001957 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001959 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 /* DB: Apply dbMask to maskedDB */
1961 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001962 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001963 goto cleanup;
1964 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001965
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001966 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1968 label, label_len, lhash);
1969 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001970 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001972
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001973 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001974 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001975 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 p = buf;
1977
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001978 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001979
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001980 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001981
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001982 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001983 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001984 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001985
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001986 /* Get zero-padding len, but always read till end of buffer
1987 * (minus one, for the 01 byte) */
1988 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001989 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001991 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1992 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001993 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001994
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001995 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001996 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001997
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001998 /*
1999 * The only information "leaked" is whether the padding was correct or not
2000 * (eg, no data is copied if it was not correct). This meets the
2001 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
2002 * the different error conditions.
2003 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002004 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002005 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2006 goto cleanup;
2007 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002008
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002009 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002010 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
2011 goto cleanup;
2012 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002013
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002014 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 if (*olen != 0) {
2016 memcpy(output, p, *olen);
2017 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002018 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002019
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002020cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 mbedtls_platform_zeroize(buf, sizeof(buf));
2022 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002025}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002029/*
2030 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
2031 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
2033 int (*f_rng)(void *, unsigned char *, size_t),
2034 void *p_rng,
2035 size_t *olen,
2036 const unsigned char *input,
2037 unsigned char *output,
2038 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002039{
2040 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2041 size_t ilen;
2042 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
2043
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002044 ilen = ctx->len;
2045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2047 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2048 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 if (ilen < 16 || ilen > sizeof(buf)) {
2051 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2052 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002057 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
2061 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002062
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002063cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002069
2070/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002071 * Do an RSA operation, then remove the message padding
2072 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002073int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
2074 int (*f_rng)(void *, unsigned char *, size_t),
2075 void *p_rng,
2076 size_t *olen,
2077 const unsigned char *input,
2078 unsigned char *output,
2079 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01002080{
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#if defined(MBEDTLS_PKCS1_V15)
2083 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
2085 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02002086#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#if defined(MBEDTLS_PKCS1_V21)
2089 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
2091 olen, input, output,
2092 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01002093#endif
2094
2095 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002097 }
2098}
2099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002101static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2102 int (*f_rng)(void *, unsigned char *, size_t),
2103 void *p_rng,
2104 mbedtls_md_type_t md_alg,
2105 unsigned int hashlen,
2106 const unsigned char *hash,
2107 int saltlen,
2108 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002109{
2110 size_t olen;
2111 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002112 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002113 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00002114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002115 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002116 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002117
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002119 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 if (f_rng == NULL) {
2123 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2124 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002125
2126 olen = ctx->len;
2127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002129 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002130 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 if (exp_hashlen == 0) {
2132 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2133 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if (hashlen != exp_hashlen) {
2136 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2137 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002138 }
2139
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002140 hash_id = (mbedtls_md_type_t) ctx->hash_id;
2141 if (hash_id == MBEDTLS_MD_NONE) {
2142 hash_id = md_alg;
2143 }
2144 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (hlen == 0) {
2146 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2147 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
2150 /* Calculate the largest possible salt length, up to the hash size.
2151 * Normally this is the hash length, which is the maximum salt length
2152 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
2153 * enough room, use the maximum salt length that fits. The constraint is
2154 * that the hash length plus the salt length plus 2 bytes must be at most
2155 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
2156 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002157 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 if (olen < hlen + min_slen + 2) {
2159 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2160 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002161 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002163 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 }
2165 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
2166 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2167 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02002168 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002169 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01002172
Simon Butcher02037452016-03-01 21:19:12 +00002173 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002175 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01002176 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002177
2178 /* Generate salt of length slen in place in the encoded message */
2179 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
2181 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
2182 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02002183
Paul Bakkerb3869132013-02-28 17:21:01 +01002184 p += slen;
2185
Simon Butcher02037452016-03-01 21:19:12 +00002186 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002187 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (ret != 0) {
2189 return ret;
2190 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002191
Simon Butcher02037452016-03-01 21:19:12 +00002192 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002194 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002196
Simon Butcher02037452016-03-01 21:19:12 +00002197 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002198 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (ret != 0) {
2200 return ret;
2201 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2204 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01002205
2206 p += hlen;
2207 *p++ = 0xBC;
2208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002210}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002211
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002212static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2213 int (*f_rng)(void *, unsigned char *, size_t),
2214 void *p_rng,
2215 mbedtls_md_type_t md_alg,
2216 unsigned int hashlen,
2217 const unsigned char *hash,
2218 int saltlen,
2219 unsigned char *sig)
2220{
2221 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2222 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2223 }
2224 if (ctx->hash_id == MBEDTLS_MD_NONE) {
2225 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2226 }
2227 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
2228 sig);
2229}
2230
2231int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2232 int (*f_rng)(void *, unsigned char *, size_t),
2233 void *p_rng,
2234 mbedtls_md_type_t md_alg,
2235 unsigned int hashlen,
2236 const unsigned char *hash,
2237 unsigned char *sig)
2238{
2239 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
2240 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2241}
2242
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002243/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02002244 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
2245 * the option to pass in the salt length.
2246 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002247int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
2248 int (*f_rng)(void *, unsigned char *, size_t),
2249 void *p_rng,
2250 mbedtls_md_type_t md_alg,
2251 unsigned int hashlen,
2252 const unsigned char *hash,
2253 int saltlen,
2254 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02002255{
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2257 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02002258}
2259
Cédric Meuterf3fab332020-04-25 11:30:45 +02002260/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002261 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
2262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002263int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2264 int (*f_rng)(void *, unsigned char *, size_t),
2265 void *p_rng,
2266 mbedtls_md_type_t md_alg,
2267 unsigned int hashlen,
2268 const unsigned char *hash,
2269 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002270{
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2272 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002273}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002277/*
2278 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
2279 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002280
2281/* Construct a PKCS v1.5 encoding of a hashed message
2282 *
2283 * This is used both for signature generation and verification.
2284 *
2285 * Parameters:
2286 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002287 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002288 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002289 * - hash: Buffer containing the hashed message or the raw data.
2290 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002291 * - dst: Buffer to hold the encoded message.
2292 *
2293 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002294 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002295 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002296 *
2297 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002298static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
2299 unsigned int hashlen,
2300 const unsigned char *hash,
2301 size_t dst_len,
2302 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01002303{
2304 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002305 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002306 unsigned char *p = dst;
2307 const char *oid = NULL;
2308
2309 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002311 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (md_size == 0) {
2313 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2314 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2317 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2318 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 if (hashlen != md_size) {
2321 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2322 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002323
2324 /* Double-check that 8 + hashlen + oid_size can be used as a
2325 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01002327 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 10 + hashlen + oid_size < 10 + hashlen) {
2329 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2330 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002331
2332 /*
2333 * Static bounds check:
2334 * - Need 10 bytes for five tag-length pairs.
2335 * (Insist on 1-byte length encodings to protect against variants of
2336 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2337 * - Need hashlen bytes for hash
2338 * - Need oid_size bytes for hash alg OID.
2339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (nb_pad < 10 + hashlen + oid_size) {
2341 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2342 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002343 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 } else {
2345 if (nb_pad < hashlen) {
2346 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2347 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002348
2349 nb_pad -= hashlen;
2350 }
2351
Hanno Becker2b2f8982017-09-27 17:10:03 +01002352 /* Need space for signature header and padding delimiter (3 bytes),
2353 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 if (nb_pad < 3 + 8) {
2355 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2356 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002357 nb_pad -= 3;
2358
2359 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002360 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002361
2362 /* Write signature header and padding */
2363 *p++ = 0;
2364 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002366 p += nb_pad;
2367 *p++ = 0;
2368
2369 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (md_alg == MBEDTLS_MD_NONE) {
2371 memcpy(p, hash, hashlen);
2372 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002373 }
2374
2375 /* Signing hashed data, add corresponding ASN.1 structure
2376 *
2377 * DigestInfo ::= SEQUENCE {
2378 * digestAlgorithm DigestAlgorithmIdentifier,
2379 * digest Digest }
2380 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2381 * Digest ::= OCTET STRING
2382 *
2383 * Schematic:
2384 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2385 * TAG-NULL + LEN [ NULL ] ]
2386 * TAG-OCTET + LEN [ HASH ] ]
2387 */
2388 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002390 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002392 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002393 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002395 p += oid_size;
2396 *p++ = MBEDTLS_ASN1_NULL;
2397 *p++ = 0x00;
2398 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002399 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002401 p += hashlen;
2402
2403 /* Just a sanity-check, should be automatic
2404 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (p != dst + dst_len) {
2406 mbedtls_platform_zeroize(dst, dst_len);
2407 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002408 }
2409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002411}
2412
Paul Bakkerb3869132013-02-28 17:21:01 +01002413/*
2414 * Do an RSA operation to sign the message digest
2415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2417 int (*f_rng)(void *, unsigned char *, size_t),
2418 void *p_rng,
2419 mbedtls_md_type_t md_alg,
2420 unsigned int hashlen,
2421 const unsigned char *hash,
2422 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002423{
Janos Follath24eed8d2019-11-22 13:21:35 +00002424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002425 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002428 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2432 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2433 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002434
Hanno Beckerfdf38032017-09-06 12:35:55 +01002435 /*
2436 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2437 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2440 ctx->len, sig)) != 0) {
2441 return ret;
2442 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002443
Hanno Beckerfdf38032017-09-06 12:35:55 +01002444 /* Private key operation
2445 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002446 * In order to prevent Lenstra's attack, make the signature in a
2447 * temporary buffer and check it before returning it.
2448 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 sig_try = mbedtls_calloc(1, ctx->len);
2451 if (sig_try == NULL) {
2452 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002453 }
2454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 verif = mbedtls_calloc(1, ctx->len);
2456 if (verif == NULL) {
2457 mbedtls_free(sig_try);
2458 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2459 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2462 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2463
2464 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002465 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2466 goto cleanup;
2467 }
2468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002470
2471cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002472 mbedtls_zeroize_and_free(sig_try, ctx->len);
2473 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 if (ret != 0) {
2476 memset(sig, '!', ctx->len);
2477 }
2478 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002481
2482/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 * Do an RSA operation to sign the message digest
2484 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002485int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2486 int (*f_rng)(void *, unsigned char *, size_t),
2487 void *p_rng,
2488 mbedtls_md_type_t md_alg,
2489 unsigned int hashlen,
2490 const unsigned char *hash,
2491 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002492{
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002494 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002496
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498#if defined(MBEDTLS_PKCS1_V15)
2499 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2501 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002502#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504#if defined(MBEDTLS_PKCS1_V21)
2505 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2507 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002508#endif
2509
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002513}
2514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002516/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002517 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002519int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2520 mbedtls_md_type_t md_alg,
2521 unsigned int hashlen,
2522 const unsigned char *hash,
2523 mbedtls_md_type_t mgf1_hash_id,
2524 int expected_salt_len,
2525 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002526{
Janos Follath24eed8d2019-11-22 13:21:35 +00002527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002528 size_t siglen;
2529 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002530 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002531 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002532 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002533 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002537 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002539
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 siglen = ctx->len;
2541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 if (siglen < 16 || siglen > sizeof(buf)) {
2543 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2544 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 if (ret != 0) {
2549 return ret;
2550 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002551
2552 p = buf;
2553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 if (buf[siglen - 1] != 0xBC) {
2555 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002556 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (md_alg != MBEDTLS_MD_NONE) {
2559 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002560 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 if (exp_hashlen == 0) {
2562 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2563 }
2564
2565 if (hashlen != exp_hashlen) {
2566 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2567 }
2568 }
2569
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002570 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (hlen == 0) {
2572 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2573 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002574
Simon Butcher02037452016-03-01 21:19:12 +00002575 /*
2576 * Note: EMSA-PSS verification is over the length of N - 1 bits
2577 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 if (buf[0] >> (8 - siglen * 8 + msb)) {
2581 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2582 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002583
Simon Butcher02037452016-03-01 21:19:12 +00002584 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002586 p++;
2587 siglen -= 1;
2588 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (siglen < hlen + 2) {
2591 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2592 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002593 hash_start = p + siglen - hlen - 1;
2594
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2596 if (ret != 0) {
2597 return ret;
2598 }
Paul Bakker02303e82013-01-03 11:08:31 +01002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002603 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (*p++ != 0x01) {
2607 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2608 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002609
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002610 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2613 observed_salt_len != (size_t) expected_salt_len) {
2614 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002615 }
2616
Simon Butcher02037452016-03-01 21:19:12 +00002617 /*
2618 * Generate H = Hash( M' )
2619 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2621 result, mgf1_hash_id);
2622 if (ret != 0) {
2623 return ret;
2624 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (memcmp(hash_start, result, hlen) != 0) {
2627 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2628 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002631}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002632
2633/*
2634 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2635 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002636int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2637 mbedtls_md_type_t md_alg,
2638 unsigned int hashlen,
2639 const unsigned char *hash,
2640 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002641{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002642 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002644 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002646
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002649 : md_alg;
2650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2652 md_alg, hashlen, hash,
2653 mgf1_hash_id,
2654 MBEDTLS_RSA_SALT_LEN_ANY,
2655 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002656
2657}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002661/*
2662 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2663 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002664int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2665 mbedtls_md_type_t md_alg,
2666 unsigned int hashlen,
2667 const unsigned char *hash,
2668 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002669{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002670 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002671 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002672 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002675 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002677
2678 sig_len = ctx->len;
2679
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002680 /*
2681 * Prepare expected PKCS1 v1.5 encoding of hash.
2682 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2685 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002686 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2687 goto cleanup;
2688 }
2689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2691 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002692 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002694
2695 /*
2696 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2697 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 ret = mbedtls_rsa_public(ctx, sig, encoded);
2700 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002701 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002703
Simon Butcher02037452016-03-01 21:19:12 +00002704 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002705 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002706 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2709 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002710 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2711 goto cleanup;
2712 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002713
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002714cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002717 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002718 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002721 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002722 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
2728/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002729 * Do an RSA operation and check the message digest
2730 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002731int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2732 mbedtls_md_type_t md_alg,
2733 unsigned int hashlen,
2734 const unsigned char *hash,
2735 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002736{
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002738 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742#if defined(MBEDTLS_PKCS1_V15)
2743 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2745 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002746#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748#if defined(MBEDTLS_PKCS1_V21)
2749 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2751 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002752#endif
2753
2754 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002756 }
2757}
2758
2759/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002760 * Copy the components of an RSA key
2761 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002762int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002763{
Janos Follath24eed8d2019-11-22 13:21:35 +00002764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002765
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002766 dst->len = src->len;
2767
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2769 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2772 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2773 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002774
2775#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2777 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2778 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2779 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2780 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002781#endif
2782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2786 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002787
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002788 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002789 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002790
2791cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 if (ret != 0) {
2793 mbedtls_rsa_free(dst);
2794 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002795
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002797}
2798
2799/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 * Free the components of an RSA key
2801 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002802void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002803{
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002805 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002807
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 mbedtls_mpi_free(&ctx->Vi);
2809 mbedtls_mpi_free(&ctx->Vf);
2810 mbedtls_mpi_free(&ctx->RN);
2811 mbedtls_mpi_free(&ctx->D);
2812 mbedtls_mpi_free(&ctx->Q);
2813 mbedtls_mpi_free(&ctx->P);
2814 mbedtls_mpi_free(&ctx->E);
2815 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002816
Hanno Becker33c30a02017-08-23 07:00:22 +01002817#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 mbedtls_mpi_free(&ctx->RQ);
2819 mbedtls_mpi_free(&ctx->RP);
2820 mbedtls_mpi_free(&ctx->QP);
2821 mbedtls_mpi_free(&ctx->DQ);
2822 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002823#endif /* MBEDTLS_RSA_NO_CRT */
2824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002826 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 if (ctx->ver != 0) {
2828 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002829 ctx->ver = 0;
2830 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002831#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002832}
2833
Hanno Beckerab377312017-08-23 16:24:51 +01002834#endif /* !MBEDTLS_RSA_ALT */
2835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
2839/*
2840 * Example RSA-1024 keypair, for test purposes
2841 */
2842#define KEY_LEN 128
2843
2844#define RSA_N "9292758453063D803DD603D5E777D788" \
2845 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2846 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2847 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2848 "93A89813FBF3C4F8066D2D800F7C38A8" \
2849 "1AE31942917403FF4946B0A83D3D3E05" \
2850 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2851 "5E94BB77B07507233A0BC7BAC8F90F79"
2852
2853#define RSA_E "10001"
2854
2855#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2856 "66CA472BC44D253102F8B4A9D3BFA750" \
2857 "91386C0077937FE33FA3252D28855837" \
2858 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2859 "DF79C5CE07EE72C7F123142198164234" \
2860 "CABB724CF78B8173B9F880FC86322407" \
2861 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2862 "071513A1E85B5DFA031F21ECAE91A34D"
2863
2864#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2865 "2C01CAD19EA484A87EA4377637E75500" \
2866 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2867 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2868
2869#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2870 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2871 "910E4168387E3C30AA1E00C339A79508" \
2872 "8452DD96A9A5EA5D9DCA68DA636032AF"
2873
Paul Bakker5121ce52009-01-03 21:22:43 +00002874#define PT_LEN 24
2875#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2876 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002879static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002880{
gufe44c2620da2020-08-03 17:56:50 +02002881#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002882 size_t i;
2883
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002885 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 }
Paul Bakker545570e2010-07-18 09:00:25 +00002887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002889 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002891#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002893 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002895
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002897#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002898
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002900}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002902
Paul Bakker5121ce52009-01-03 21:22:43 +00002903/*
2904 * Checkup routine
2905 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002907{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002908 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002910 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002912 unsigned char rsa_plaintext[PT_LEN];
2913 unsigned char rsa_decrypted[PT_LEN];
2914 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002915#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002916 unsigned char sha1sum[20];
2917#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Hanno Becker3a701162017-08-22 13:52:43 +01002919 mbedtls_mpi K;
2920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 mbedtls_mpi_init(&K);
2922 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2925 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2926 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2927 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2928 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2929 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2930 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2931 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2932 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2933 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 if (verbose != 0) {
2938 mbedtls_printf(" RSA key validation: ");
2939 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2942 mbedtls_rsa_check_privkey(&rsa) != 0) {
2943 if (verbose != 0) {
2944 mbedtls_printf("failed\n");
2945 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
Hanno Becker5bc87292017-05-03 15:09:31 +01002947 ret = 1;
2948 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 }
2950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 if (verbose != 0) {
2952 mbedtls_printf("passed\n PKCS#1 encryption : ");
2953 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2958 PT_LEN, rsa_plaintext,
2959 rsa_ciphertext) != 0) {
2960 if (verbose != 0) {
2961 mbedtls_printf("failed\n");
2962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Hanno Becker5bc87292017-05-03 15:09:31 +01002964 ret = 1;
2965 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 }
2967
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 if (verbose != 0) {
2969 mbedtls_printf("passed\n PKCS#1 decryption : ");
2970 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2973 &len, rsa_ciphertext, rsa_decrypted,
2974 sizeof(rsa_decrypted)) != 0) {
2975 if (verbose != 0) {
2976 mbedtls_printf("failed\n");
2977 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
Hanno Becker5bc87292017-05-03 15:09:31 +01002979 ret = 1;
2980 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002981 }
2982
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2984 if (verbose != 0) {
2985 mbedtls_printf("failed\n");
2986 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002987
Hanno Becker5bc87292017-05-03 15:09:31 +01002988 ret = 1;
2989 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002990 }
2991
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 if (verbose != 0) {
2993 mbedtls_printf("passed\n");
2994 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002995
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002996#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 if (verbose != 0) {
2998 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002999 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003000
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01003001 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
3002 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 if (verbose != 0) {
3004 mbedtls_printf("failed\n");
3005 }
3006
3007 return 1;
3008 }
3009
3010 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
3011 MBEDTLS_MD_SHA1, 20,
3012 sha1sum, rsa_ciphertext) != 0) {
3013 if (verbose != 0) {
3014 mbedtls_printf("failed\n");
3015 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Hanno Becker5bc87292017-05-03 15:09:31 +01003017 ret = 1;
3018 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003019 }
3020
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 if (verbose != 0) {
3022 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
3023 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
3026 sha1sum, rsa_ciphertext) != 0) {
3027 if (verbose != 0) {
3028 mbedtls_printf("failed\n");
3029 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Hanno Becker5bc87292017-05-03 15:09:31 +01003031 ret = 1;
3032 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003033 }
3034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 if (verbose != 0) {
3036 mbedtls_printf("passed\n");
3037 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003038#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 if (verbose != 0) {
3041 mbedtls_printf("\n");
3042 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003043
Paul Bakker3d8fb632014-04-17 12:42:41 +02003044cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 mbedtls_mpi_free(&K);
3046 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02003048 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003051}
3052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#endif /* MBEDTLS_RSA_C */