blob: 835146368ea4b806ab0625670649495c209b6b91 [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/*
Gilles Peskine19f1adf2024-02-01 22:17:44 +01001018 * Get length in bits of RSA modulus
1019 */
1020size_t mbedtls_rsa_get_bitlen(const mbedtls_rsa_context *ctx)
1021{
1022 return mbedtls_mpi_bitlen(&ctx->N);
1023}
1024
1025/*
Hanno Becker617c1ae2017-08-23 14:11:24 +01001026 * Get length in bytes of RSA modulus
1027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +01001029{
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +01001031}
1032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
1035/*
1036 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001037 *
1038 * This generation method follows the RSA key pair generation procedure of
1039 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
1042 int (*f_rng)(void *, unsigned char *, size_t),
1043 void *p_rng,
1044 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +00001045{
Janos Follath24eed8d2019-11-22 13:21:35 +00001046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -08001047 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +01001048 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Janos Follathb8fc1b02018-09-03 15:37:01 +01001050 /*
1051 * If the modulus is 1024 bit long or shorter, then the security strength of
1052 * the RSA algorithm is less than or equal to 80 bits and therefore an error
1053 * rate of 2^-80 is sufficient.
1054 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +01001056 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 }
Janos Follathb8fc1b02018-09-03 15:37:01 +01001058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 mbedtls_mpi_init(&H);
1060 mbedtls_mpi_init(&G);
1061 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001063 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +01001064 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1065 goto cleanup;
1066 }
1067
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001068 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1070 goto cleanup;
1071 }
1072
1073 /*
1074 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001075 * 1. |P-Q| > 2^( nbits / 2 - 100 )
1076 * 2. GCD( E, (P-1)*(Q-1) ) == 1
1077 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +00001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 do {
1082 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
1083 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
1086 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001088 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
1090 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001093
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001094 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (H.s < 0) {
1096 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
1097 }
Janos Follathef441782016-09-21 13:18:12 +01001098
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001099 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
1101 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
1102 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001103
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001104 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
1106 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -08001107 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001109
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001110 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
1112 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
1113 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if (mbedtls_mpi_bitlen(&ctx->D) <= ((nbits + 1) / 2)) { // (FIPS 186-4 §B.3.1 criterion 3(a))
Jethro Beekman97f95c92018-02-13 15:50:36 -08001116 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001118
1119 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001122 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
1124 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001129
Jethro Beekman97f95c92018-02-13 15:50:36 -08001130#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 * DP = D mod (P - 1)
1133 * DQ = D mod (Q - 1)
1134 * QP = Q^-1 mod P
1135 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
1137 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001138#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Hanno Becker83aad1f2017-08-23 06:45:10 +01001140 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
1143cleanup:
1144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 mbedtls_mpi_free(&H);
1146 mbedtls_mpi_free(&G);
1147 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (ret != 0) {
1150 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +01001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if ((-ret & ~0x7f) == 0) {
1153 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
1154 }
1155 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 }
1157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001159}
1160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
1163/*
1164 * Check a public RSA key
1165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001166int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001167{
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
1169 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001170 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
1173 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001174 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
1177 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
1178 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
1179 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1180 }
1181
1182 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001183}
1184
1185/*
Hanno Becker705fc682017-10-10 17:57:02 +01001186 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001188int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001189{
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
1191 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
1192 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 }
Paul Bakker48377d92013-08-30 12:06:24 +02001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
1196 &ctx->D, &ctx->E, NULL, NULL) != 0) {
1197 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001199
Hanno Beckerb269a852017-08-25 08:03:21 +01001200#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
1202 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
1203 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +01001204 }
1205#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001208}
1209
1210/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001211 * Check if contexts holding a public and private key match
1212 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001213int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
1214 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001215{
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
1217 mbedtls_rsa_check_privkey(prv) != 0) {
1218 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001219 }
1220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
1222 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
1223 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001224 }
1225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001227}
1228
1229/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 * Do an RSA public key operation
1231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001232int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
1233 const unsigned char *input,
1234 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001235{
Janos Follath24eed8d2019-11-22 13:21:35 +00001236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001237 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
1241 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1242 }
Hanno Becker705fc682017-10-10 17:57:02 +01001243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001246#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1248 return ret;
1249 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001250#endif
1251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001255 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1256 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 }
1258
1259 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
1261 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
1263cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1266 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1267 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001268#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if (ret != 0) {
1273 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
1274 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001277}
1278
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001279/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001280 * Generate or update blinding values, see section 10 of:
1281 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001282 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001283 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001284 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001285static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1286 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001287{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001288 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001289 mbedtls_mpi R;
1290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001294 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1296 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1297 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1298 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001299
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001300 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001301 }
1302
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001303 /* Unblinding value: Vf = random number, invertible mod N */
1304 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +02001306 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
1307 goto cleanup;
1308 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 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 +02001311
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001312 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
1314 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
1315 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001316
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001317 /* At this point, Vi is invertible mod N if and only if both Vf and R
1318 * are invertible mod N. If one of them isn't, we don't need to know
1319 * which one, we just loop and choose new values for both of them.
1320 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
1322 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001323 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -05001327
1328 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
1330 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001331
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001332 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001333 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 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 +02001335
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001336
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001337cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001341}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001342
Paul Bakker5121ce52009-01-03 21:22:43 +00001343/*
Janos Follathd6b09652023-11-21 09:33:54 +00001344 * Unblind
1345 * T = T * Vf mod N
1346 */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001347static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, const mbedtls_mpi *N)
Janos Follathd6b09652023-11-21 09:33:54 +00001348{
1349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1350 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
1351 const size_t nlimbs = N->n;
1352 const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
1353 mbedtls_mpi RR, M_T;
1354
1355 mbedtls_mpi_init(&RR);
1356 mbedtls_mpi_init(&M_T);
1357
1358 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
1359 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
1360
1361 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
1362 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
1363
Janos Follathe6750b22023-12-27 10:22:59 +00001364 /* T = T * Vf mod N
1365 * Reminder: montmul(A, B, N) = A * B * R^-1 mod N
1366 * Usually both operands are multiplied by R mod N beforehand (by calling
1367 * `to_mont_rep()` on them), yielding a result that's also * R mod N (aka
1368 * "in the Montgomery domain"). Here we only multiply one operand by R mod
1369 * N, so the result is directly what we want - no need to call
1370 * `from_mont_rep()` on it. */
Janos Follathd6b09652023-11-21 09:33:54 +00001371 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 +00001372 mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1373
1374cleanup:
1375
1376 mbedtls_mpi_free(&RR);
1377 mbedtls_mpi_free(&M_T);
1378
1379 return ret;
1380}
1381
1382/*
Janos Follathe81102e2017-03-22 13:38:28 +00001383 * Exponent blinding supposed to prevent side-channel attacks using multiple
1384 * traces of measurements to recover the RSA key. The more collisions are there,
1385 * the more bits of the key can be recovered. See [3].
1386 *
1387 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001388 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001389 *
1390 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001391 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001392 *
1393 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1394 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1395 * Thus in this sense with 28 byte blinding the security is not reduced by
1396 * side-channel attacks like the one in [3])
1397 *
1398 * This countermeasure does not help if the key recovery is possible with a
1399 * single trace.
1400 */
1401#define RSA_EXPONENT_BLINDING 28
1402
1403/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 * Do an RSA private key operation
1405 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001406int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1407 int (*f_rng)(void *, unsigned char *, size_t),
1408 void *p_rng,
1409 const unsigned char *input,
1410 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001411{
Janos Follath24eed8d2019-11-22 13:21:35 +00001412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001413 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001414
1415 /* Temporary holding the result */
1416 mbedtls_mpi T;
1417
1418 /* Temporaries holding P-1, Q-1 and the
1419 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001420 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001421
1422#if !defined(MBEDTLS_RSA_NO_CRT)
1423 /* Temporaries holding the results mod p resp. mod q. */
1424 mbedtls_mpi TP, TQ;
1425
1426 /* Temporaries holding the blinded exponents for
1427 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001428 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001429#else
1430 /* Temporary holding the blinded exponent (if used). */
1431 mbedtls_mpi D_blind;
Hanno Becker43f94722017-08-25 11:50:00 +01001432#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001433
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001434 /* Temporaries holding the initial input and the double
1435 * checked result; should be the same in the end. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001436 mbedtls_mpi input_blinded, check_result_blinded;
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if (f_rng == NULL) {
1439 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1440 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if (rsa_check_context(ctx, 1 /* private key checks */,
1443 1 /* blinding on */) != 0) {
1444 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001445 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001446
Hanno Becker06811ce2017-05-03 15:10:34 +01001447#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1449 return ret;
1450 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001451#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001452
Hanno Becker06811ce2017-05-03 15:10:34 +01001453 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 mbedtls_mpi_init(&P1);
1457 mbedtls_mpi_init(&Q1);
1458 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001459
Janos Follathe81102e2017-03-22 13:38:28 +00001460#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001462#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 mbedtls_mpi_init(&DP_blind);
1464 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001465#endif
1466
Hanno Becker06811ce2017-05-03 15:10:34 +01001467#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001469#endif
1470
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001471 mbedtls_mpi_init(&input_blinded);
1472 mbedtls_mpi_init(&check_result_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001473
1474 /* End of MPI initialization */
1475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1477 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001478 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1479 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 }
1481
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001482 /*
1483 * Blinding
1484 * T = T * Vi mod N
1485 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1487 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1488 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001489
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001490 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&input_blinded, &T));
Janos Follath6bcbc922023-11-21 09:46:43 +00001491
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001492 /*
1493 * Exponent blinding
1494 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1496 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001497
Janos Follathf9203b42017-03-22 15:13:15 +00001498#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001499 /*
1500 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1501 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1503 f_rng, p_rng));
1504 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1505 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1506 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathf9203b42017-03-22 15:13:15 +00001507#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001508 /*
1509 * DP_blind = ( P - 1 ) * R + DP
1510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1512 f_rng, p_rng));
1513 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1514 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1515 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001516
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001517 /*
1518 * DQ_blind = ( Q - 1 ) * R + DQ
1519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1521 f_rng, p_rng));
1522 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1523 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1524 &ctx->DQ));
Janos Follathe81102e2017-03-22 13:38:28 +00001525#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath47ee7702023-12-27 10:33:00 +00001528 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &D_blind, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001529#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001530 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001531 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001533 * TP = input ^ dP mod P
1534 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001536
Janos Follath47ee7702023-12-27 10:33:00 +00001537 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, &DP_blind, &ctx->P, &ctx->RP));
1538 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, &DQ_blind, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
1540 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001541 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1544 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1545 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001546
1547 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001548 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001549 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1551 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001553
Hanno Becker2dec5e82017-10-03 07:49:52 +01001554 /* Verify the result to prevent glitching attacks. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001555 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&check_result_blinded, &T, &ctx->E,
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 &ctx->N, &ctx->RN));
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001557 if (mbedtls_mpi_cmp_mpi(&check_result_blinded, &input_blinded) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001558 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1559 goto cleanup;
1560 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001561
Janos Follath6bcbc922023-11-21 09:46:43 +00001562 /*
1563 * Unblind
1564 * T = T * Vf mod N
1565 */
1566 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1567
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001570
1571cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1574 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1575 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001576#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 mbedtls_mpi_free(&P1);
1579 mbedtls_mpi_free(&Q1);
1580 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001581
Janos Follathe81102e2017-03-22 13:38:28 +00001582#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001584#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 mbedtls_mpi_free(&DP_blind);
1586 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001587#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001590
1591#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001593#endif
1594
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001595 mbedtls_mpi_free(&check_result_blinded);
1596 mbedtls_mpi_free(&input_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (ret != 0 && ret >= -0x007f) {
1599 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001603}
1604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001606/**
1607 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1608 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001609 * \param dst buffer to mask
1610 * \param dlen length of destination buffer
1611 * \param src source of the mask generation
1612 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001613 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001614 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001615static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1616 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001617{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001618 unsigned char counter[4];
1619 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001620 unsigned int hlen;
1621 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001622 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001623 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001624 const mbedtls_md_info_t *md_info;
1625 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 mbedtls_md_init(&md_ctx);
1628 md_info = mbedtls_md_info_from_type(md_alg);
1629 if (md_info == NULL) {
1630 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1631 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 mbedtls_md_init(&md_ctx);
1634 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001635 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 memset(mask, 0, sizeof(mask));
1641 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001642
Simon Butcher02037452016-03-01 21:19:12 +00001643 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001644 p = dst;
1645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001647 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001649 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001653 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 }
1655 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001656 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 }
1658 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001659 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 }
1661 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001662 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001666 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001668
1669 counter[3]++;
1670
1671 dlen -= use_len;
1672 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001673
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001674exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001679}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001680
1681/**
1682 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1683 *
1684 * \param hash the input hash
1685 * \param hlen length of the input hash
1686 * \param salt the input salt
1687 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001688 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001689 * \param md_alg message digest to use
1690 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001691static int hash_mprime(const unsigned char *hash, size_t hlen,
1692 const unsigned char *salt, size_t slen,
1693 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001694{
1695 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001696
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001697 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1701 if (md_info == NULL) {
1702 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1703 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 mbedtls_md_init(&md_ctx);
1706 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001707 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 }
1709 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001710 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 }
1712 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001713 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 }
1715 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001716 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 }
1718 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001719 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 }
1721 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001722 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001724
1725exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001729}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001730
1731/**
1732 * Compute a hash.
1733 *
1734 * \param md_alg algorithm to use
1735 * \param input input message to hash
1736 * \param ilen input length
1737 * \param output the output buffer - must be large enough for \p md_alg
1738 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001739static int compute_hash(mbedtls_md_type_t md_alg,
1740 const unsigned char *input, size_t ilen,
1741 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001742{
1743 const mbedtls_md_info_t *md_info;
1744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 md_info = mbedtls_md_info_from_type(md_alg);
1746 if (md_info == NULL) {
1747 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1748 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001751}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001755/*
1756 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1757 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001758int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1759 int (*f_rng)(void *, unsigned char *, size_t),
1760 void *p_rng,
1761 const unsigned char *label, size_t label_len,
1762 size_t ilen,
1763 const unsigned char *input,
1764 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001765{
1766 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001768 unsigned char *p = output;
1769 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (f_rng == NULL) {
1772 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1773 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001774
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001775 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if (hlen == 0) {
1777 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1778 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001779
1780 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001781
Simon Butcher02037452016-03-01 21:19:12 +00001782 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1784 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1785 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001788
1789 *p++ = 0;
1790
Simon Butcher02037452016-03-01 21:19:12 +00001791 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1793 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1794 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001795
1796 p += hlen;
1797
Simon Butcher02037452016-03-01 21:19:12 +00001798 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1800 if (ret != 0) {
1801 return ret;
1802 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001803 p += hlen;
1804 p += olen - 2 * hlen - 2 - ilen;
1805 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (ilen != 0) {
1807 memcpy(p, input, ilen);
1808 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001809
Simon Butcher02037452016-03-01 21:19:12 +00001810 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001812 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 return ret;
1814 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001815
Simon Butcher02037452016-03-01 21:19:12 +00001816 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001818 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 return ret;
1820 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001821
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001823}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001827/*
1828 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1829 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001830int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1831 int (*f_rng)(void *, unsigned char *, size_t),
1832 void *p_rng, size_t ilen,
1833 const unsigned char *input,
1834 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001835{
1836 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001838 unsigned char *p = output;
1839
Paul Bakkerb3869132013-02-28 17:21:01 +01001840 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001841
Simon Butcher02037452016-03-01 21:19:12 +00001842 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (ilen + 11 < ilen || olen < ilen + 11) {
1844 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1845 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001846
1847 nb_pad = olen - 3 - ilen;
1848
1849 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 if (f_rng == NULL) {
1852 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1853 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001854
1855 *p++ = MBEDTLS_RSA_CRYPT;
1856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001858 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001859
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001860 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 ret = f_rng(p_rng, p, 1);
1862 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001863
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001864 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 if (rng_dl == 0 || ret != 0) {
1866 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1867 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001868
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001869 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001870 }
1871
1872 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 if (ilen != 0) {
1874 memcpy(p, input, ilen);
1875 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001878}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001880
Paul Bakker5121ce52009-01-03 21:22:43 +00001881/*
1882 * Add the message padding, then do an RSA operation
1883 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001884int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1885 int (*f_rng)(void *, unsigned char *, size_t),
1886 void *p_rng,
1887 size_t ilen,
1888 const unsigned char *input,
1889 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001890{
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#if defined(MBEDTLS_PKCS1_V15)
1893 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1895 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001896#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#if defined(MBEDTLS_PKCS1_V21)
1899 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1901 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001902#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001903
1904 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001907}
1908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001910/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001911 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001912 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001913int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1914 int (*f_rng)(void *, unsigned char *, size_t),
1915 void *p_rng,
1916 const unsigned char *label, size_t label_len,
1917 size_t *olen,
1918 const unsigned char *input,
1919 unsigned char *output,
1920 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001921{
Janos Follath24eed8d2019-11-22 13:21:35 +00001922 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001923 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001924 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001925 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001927 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001928 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001929
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001930 /*
1931 * Parameters sanity checks
1932 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1934 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001936
1937 ilen = ctx->len;
1938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 if (ilen < 16 || ilen > sizeof(buf)) {
1940 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1941 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001943 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 if (hlen == 0) {
1945 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1946 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001947
Janos Follathc17cda12016-02-11 11:08:18 +00001948 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (2 * hlen + 2 > ilen) {
1950 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1951 }
Janos Follathc17cda12016-02-11 11:08:18 +00001952
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001953 /*
1954 * RSA operation
1955 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001959 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001961
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001962 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001963 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001964 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001965 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001967 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 /* DB: Apply dbMask to maskedDB */
1969 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001970 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001971 goto cleanup;
1972 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001973
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001974 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1976 label, label_len, lhash);
1977 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001978 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001980
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001981 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001982 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001983 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 p = buf;
1985
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001986 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001987
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001988 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001989
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001990 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001991 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001992 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001993
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001994 /* Get zero-padding len, but always read till end of buffer
1995 * (minus one, for the 01 byte) */
1996 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001997 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001999 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
2000 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002001 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002002
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002003 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002004 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01002005
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002006 /*
2007 * The only information "leaked" is whether the padding was correct or not
2008 * (eg, no data is copied if it was not correct). This meets the
2009 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
2010 * the different error conditions.
2011 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002012 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002013 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2014 goto cleanup;
2015 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002016
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002017 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002018 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
2019 goto cleanup;
2020 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002021
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002022 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 if (*olen != 0) {
2024 memcpy(output, p, *olen);
2025 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002026 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002027
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002028cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 mbedtls_platform_zeroize(buf, sizeof(buf));
2030 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002033}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002037/*
2038 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
2039 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002040int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
2041 int (*f_rng)(void *, unsigned char *, size_t),
2042 void *p_rng,
2043 size_t *olen,
2044 const unsigned char *input,
2045 unsigned char *output,
2046 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002047{
2048 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2049 size_t ilen;
2050 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
2051
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002052 ilen = ctx->len;
2053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2055 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2056 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 if (ilen < 16 || ilen > sizeof(buf)) {
2059 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2060 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002065 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
2069 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002070
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002071cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
2078/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002079 * Do an RSA operation, then remove the message padding
2080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002081int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
2082 int (*f_rng)(void *, unsigned char *, size_t),
2083 void *p_rng,
2084 size_t *olen,
2085 const unsigned char *input,
2086 unsigned char *output,
2087 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01002088{
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090#if defined(MBEDTLS_PKCS1_V15)
2091 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
2093 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02002094#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096#if defined(MBEDTLS_PKCS1_V21)
2097 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
2099 olen, input, output,
2100 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01002101#endif
2102
2103 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002105 }
2106}
2107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002109static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2110 int (*f_rng)(void *, unsigned char *, size_t),
2111 void *p_rng,
2112 mbedtls_md_type_t md_alg,
2113 unsigned int hashlen,
2114 const unsigned char *hash,
2115 int saltlen,
2116 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002117{
2118 size_t olen;
2119 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002120 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002121 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00002122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002123 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002124 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002127 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if (f_rng == NULL) {
2131 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2132 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002133
2134 olen = ctx->len;
2135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002137 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002138 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (exp_hashlen == 0) {
2140 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2141 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 if (hashlen != exp_hashlen) {
2144 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2145 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002146 }
2147
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002148 hash_id = (mbedtls_md_type_t) ctx->hash_id;
2149 if (hash_id == MBEDTLS_MD_NONE) {
2150 hash_id = md_alg;
2151 }
2152 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (hlen == 0) {
2154 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2155 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
2158 /* Calculate the largest possible salt length, up to the hash size.
2159 * Normally this is the hash length, which is the maximum salt length
2160 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
2161 * enough room, use the maximum salt length that fits. The constraint is
2162 * that the hash length plus the salt length plus 2 bytes must be at most
2163 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
2164 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002165 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (olen < hlen + min_slen + 2) {
2167 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2168 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002169 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002171 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 }
2173 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
2174 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2175 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02002176 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002177 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01002180
Simon Butcher02037452016-03-01 21:19:12 +00002181 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002183 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01002184 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002185
2186 /* Generate salt of length slen in place in the encoded message */
2187 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
2189 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
2190 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02002191
Paul Bakkerb3869132013-02-28 17:21:01 +01002192 p += slen;
2193
Simon Butcher02037452016-03-01 21:19:12 +00002194 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002195 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (ret != 0) {
2197 return ret;
2198 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002199
Simon Butcher02037452016-03-01 21:19:12 +00002200 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002202 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002204
Simon Butcher02037452016-03-01 21:19:12 +00002205 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002206 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (ret != 0) {
2208 return ret;
2209 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2212 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01002213
2214 p += hlen;
2215 *p++ = 0xBC;
2216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002218}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002219
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002220static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2221 int (*f_rng)(void *, unsigned char *, size_t),
2222 void *p_rng,
2223 mbedtls_md_type_t md_alg,
2224 unsigned int hashlen,
2225 const unsigned char *hash,
2226 int saltlen,
2227 unsigned char *sig)
2228{
2229 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2230 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2231 }
2232 if (ctx->hash_id == MBEDTLS_MD_NONE) {
2233 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2234 }
2235 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
2236 sig);
2237}
2238
2239int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2240 int (*f_rng)(void *, unsigned char *, size_t),
2241 void *p_rng,
2242 mbedtls_md_type_t md_alg,
2243 unsigned int hashlen,
2244 const unsigned char *hash,
2245 unsigned char *sig)
2246{
2247 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
2248 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2249}
2250
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002251/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02002252 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
2253 * the option to pass in the salt length.
2254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002255int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
2256 int (*f_rng)(void *, unsigned char *, size_t),
2257 void *p_rng,
2258 mbedtls_md_type_t md_alg,
2259 unsigned int hashlen,
2260 const unsigned char *hash,
2261 int saltlen,
2262 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02002263{
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2265 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02002266}
2267
Cédric Meuterf3fab332020-04-25 11:30:45 +02002268/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002269 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
2270 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002271int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2272 int (*f_rng)(void *, unsigned char *, size_t),
2273 void *p_rng,
2274 mbedtls_md_type_t md_alg,
2275 unsigned int hashlen,
2276 const unsigned char *hash,
2277 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002278{
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2280 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002281}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002285/*
2286 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
2287 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002288
2289/* Construct a PKCS v1.5 encoding of a hashed message
2290 *
2291 * This is used both for signature generation and verification.
2292 *
2293 * Parameters:
2294 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002295 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002296 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002297 * - hash: Buffer containing the hashed message or the raw data.
2298 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002299 * - dst: Buffer to hold the encoded message.
2300 *
2301 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002302 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002303 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002304 *
2305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002306static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
2307 unsigned int hashlen,
2308 const unsigned char *hash,
2309 size_t dst_len,
2310 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01002311{
2312 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002313 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002314 unsigned char *p = dst;
2315 const char *oid = NULL;
2316
2317 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002319 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 if (md_size == 0) {
2321 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2322 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2325 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2326 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (hashlen != md_size) {
2329 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2330 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002331
2332 /* Double-check that 8 + hashlen + oid_size can be used as a
2333 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01002335 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 10 + hashlen + oid_size < 10 + hashlen) {
2337 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2338 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002339
2340 /*
2341 * Static bounds check:
2342 * - Need 10 bytes for five tag-length pairs.
2343 * (Insist on 1-byte length encodings to protect against variants of
2344 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2345 * - Need hashlen bytes for hash
2346 * - Need oid_size bytes for hash alg OID.
2347 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 if (nb_pad < 10 + hashlen + oid_size) {
2349 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2350 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002351 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 } else {
2353 if (nb_pad < hashlen) {
2354 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2355 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002356
2357 nb_pad -= hashlen;
2358 }
2359
Hanno Becker2b2f8982017-09-27 17:10:03 +01002360 /* Need space for signature header and padding delimiter (3 bytes),
2361 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (nb_pad < 3 + 8) {
2363 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2364 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002365 nb_pad -= 3;
2366
2367 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002368 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002369
2370 /* Write signature header and padding */
2371 *p++ = 0;
2372 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002374 p += nb_pad;
2375 *p++ = 0;
2376
2377 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (md_alg == MBEDTLS_MD_NONE) {
2379 memcpy(p, hash, hashlen);
2380 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002381 }
2382
2383 /* Signing hashed data, add corresponding ASN.1 structure
2384 *
2385 * DigestInfo ::= SEQUENCE {
2386 * digestAlgorithm DigestAlgorithmIdentifier,
2387 * digest Digest }
2388 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2389 * Digest ::= OCTET STRING
2390 *
2391 * Schematic:
2392 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2393 * TAG-NULL + LEN [ NULL ] ]
2394 * TAG-OCTET + LEN [ HASH ] ]
2395 */
2396 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002398 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002400 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002401 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002403 p += oid_size;
2404 *p++ = MBEDTLS_ASN1_NULL;
2405 *p++ = 0x00;
2406 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002407 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002409 p += hashlen;
2410
2411 /* Just a sanity-check, should be automatic
2412 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 if (p != dst + dst_len) {
2414 mbedtls_platform_zeroize(dst, dst_len);
2415 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002416 }
2417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002419}
2420
Paul Bakkerb3869132013-02-28 17:21:01 +01002421/*
2422 * Do an RSA operation to sign the message digest
2423 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002424int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2425 int (*f_rng)(void *, unsigned char *, size_t),
2426 void *p_rng,
2427 mbedtls_md_type_t md_alg,
2428 unsigned int hashlen,
2429 const unsigned char *hash,
2430 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002431{
Janos Follath24eed8d2019-11-22 13:21:35 +00002432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002433 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002436 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2440 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2441 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002442
Hanno Beckerfdf38032017-09-06 12:35:55 +01002443 /*
2444 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2445 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2448 ctx->len, sig)) != 0) {
2449 return ret;
2450 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002451
Hanno Beckerfdf38032017-09-06 12:35:55 +01002452 /* Private key operation
2453 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002454 * In order to prevent Lenstra's attack, make the signature in a
2455 * temporary buffer and check it before returning it.
2456 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 sig_try = mbedtls_calloc(1, ctx->len);
2459 if (sig_try == NULL) {
2460 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002461 }
2462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 verif = mbedtls_calloc(1, ctx->len);
2464 if (verif == NULL) {
2465 mbedtls_free(sig_try);
2466 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2467 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2470 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2471
2472 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002473 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2474 goto cleanup;
2475 }
2476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002478
2479cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002480 mbedtls_zeroize_and_free(sig_try, ctx->len);
2481 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 if (ret != 0) {
2484 memset(sig, '!', ctx->len);
2485 }
2486 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002489
2490/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 * Do an RSA operation to sign the message digest
2492 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002493int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2494 int (*f_rng)(void *, unsigned char *, size_t),
2495 void *p_rng,
2496 mbedtls_md_type_t md_alg,
2497 unsigned int hashlen,
2498 const unsigned char *hash,
2499 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002500{
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002502 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#if defined(MBEDTLS_PKCS1_V15)
2507 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2509 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002510#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512#if defined(MBEDTLS_PKCS1_V21)
2513 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2515 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002516#endif
2517
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002521}
2522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002524/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002525 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002527int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2528 mbedtls_md_type_t md_alg,
2529 unsigned int hashlen,
2530 const unsigned char *hash,
2531 mbedtls_md_type_t mgf1_hash_id,
2532 int expected_salt_len,
2533 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002534{
Janos Follath24eed8d2019-11-22 13:21:35 +00002535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002536 size_t siglen;
2537 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002538 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002539 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002540 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002541 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002545 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002547
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 siglen = ctx->len;
2549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (siglen < 16 || siglen > sizeof(buf)) {
2551 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2552 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (ret != 0) {
2557 return ret;
2558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
2560 p = buf;
2561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (buf[siglen - 1] != 0xBC) {
2563 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002564 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 if (md_alg != MBEDTLS_MD_NONE) {
2567 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002568 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 if (exp_hashlen == 0) {
2570 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2571 }
2572
2573 if (hashlen != exp_hashlen) {
2574 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2575 }
2576 }
2577
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002578 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (hlen == 0) {
2580 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2581 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002582
Simon Butcher02037452016-03-01 21:19:12 +00002583 /*
2584 * Note: EMSA-PSS verification is over the length of N - 1 bits
2585 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (buf[0] >> (8 - siglen * 8 + msb)) {
2589 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2590 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002591
Simon Butcher02037452016-03-01 21:19:12 +00002592 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002594 p++;
2595 siglen -= 1;
2596 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if (siglen < hlen + 2) {
2599 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2600 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002601 hash_start = p + siglen - hlen - 1;
2602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2604 if (ret != 0) {
2605 return ret;
2606 }
Paul Bakker02303e82013-01-03 11:08:31 +01002607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002611 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (*p++ != 0x01) {
2615 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2616 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002617
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002618 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2621 observed_salt_len != (size_t) expected_salt_len) {
2622 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002623 }
2624
Simon Butcher02037452016-03-01 21:19:12 +00002625 /*
2626 * Generate H = Hash( M' )
2627 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2629 result, mgf1_hash_id);
2630 if (ret != 0) {
2631 return ret;
2632 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (memcmp(hash_start, result, hlen) != 0) {
2635 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2636 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002639}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002640
2641/*
2642 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2643 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002644int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2645 mbedtls_md_type_t md_alg,
2646 unsigned int hashlen,
2647 const unsigned char *hash,
2648 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002649{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002650 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002652 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002657 : md_alg;
2658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2660 md_alg, hashlen, hash,
2661 mgf1_hash_id,
2662 MBEDTLS_RSA_SALT_LEN_ANY,
2663 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002664
2665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002669/*
2670 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2671 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002672int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2673 mbedtls_md_type_t md_alg,
2674 unsigned int hashlen,
2675 const unsigned char *hash,
2676 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002677{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002678 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002679 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002680 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002683 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002685
2686 sig_len = ctx->len;
2687
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002688 /*
2689 * Prepare expected PKCS1 v1.5 encoding of hash.
2690 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2693 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002694 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2695 goto cleanup;
2696 }
2697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2699 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002700 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002702
2703 /*
2704 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2705 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 ret = mbedtls_rsa_public(ctx, sig, encoded);
2708 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002709 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002711
Simon Butcher02037452016-03-01 21:19:12 +00002712 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002713 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002714 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2717 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002718 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2719 goto cleanup;
2720 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002721
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002722cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002725 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002726 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002729 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002730 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002733}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002735
2736/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002737 * Do an RSA operation and check the message digest
2738 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002739int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2740 mbedtls_md_type_t md_alg,
2741 unsigned int hashlen,
2742 const unsigned char *hash,
2743 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002744{
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002746 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750#if defined(MBEDTLS_PKCS1_V15)
2751 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2753 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002754#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756#if defined(MBEDTLS_PKCS1_V21)
2757 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2759 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002760#endif
2761
2762 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002764 }
2765}
2766
2767/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002768 * Copy the components of an RSA key
2769 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002770int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002771{
Janos Follath24eed8d2019-11-22 13:21:35 +00002772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002773
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002774 dst->len = src->len;
2775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2777 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2780 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2781 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002782
2783#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2785 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2786 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2787 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2788 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002789#endif
2790
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2794 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002795
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002796 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002797 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002798
2799cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 if (ret != 0) {
2801 mbedtls_rsa_free(dst);
2802 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002805}
2806
2807/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 * Free the components of an RSA key
2809 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002810void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002811{
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002813 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 mbedtls_mpi_free(&ctx->Vi);
2817 mbedtls_mpi_free(&ctx->Vf);
2818 mbedtls_mpi_free(&ctx->RN);
2819 mbedtls_mpi_free(&ctx->D);
2820 mbedtls_mpi_free(&ctx->Q);
2821 mbedtls_mpi_free(&ctx->P);
2822 mbedtls_mpi_free(&ctx->E);
2823 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002824
Hanno Becker33c30a02017-08-23 07:00:22 +01002825#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 mbedtls_mpi_free(&ctx->RQ);
2827 mbedtls_mpi_free(&ctx->RP);
2828 mbedtls_mpi_free(&ctx->QP);
2829 mbedtls_mpi_free(&ctx->DQ);
2830 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002831#endif /* MBEDTLS_RSA_NO_CRT */
2832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002834 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 if (ctx->ver != 0) {
2836 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002837 ctx->ver = 0;
2838 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002839#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002840}
2841
Hanno Beckerab377312017-08-23 16:24:51 +01002842#endif /* !MBEDTLS_RSA_ALT */
2843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Paul Bakker5121ce52009-01-03 21:22:43 +00002846
2847/*
2848 * Example RSA-1024 keypair, for test purposes
2849 */
2850#define KEY_LEN 128
2851
2852#define RSA_N "9292758453063D803DD603D5E777D788" \
2853 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2854 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2855 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2856 "93A89813FBF3C4F8066D2D800F7C38A8" \
2857 "1AE31942917403FF4946B0A83D3D3E05" \
2858 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2859 "5E94BB77B07507233A0BC7BAC8F90F79"
2860
2861#define RSA_E "10001"
2862
2863#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2864 "66CA472BC44D253102F8B4A9D3BFA750" \
2865 "91386C0077937FE33FA3252D28855837" \
2866 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2867 "DF79C5CE07EE72C7F123142198164234" \
2868 "CABB724CF78B8173B9F880FC86322407" \
2869 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2870 "071513A1E85B5DFA031F21ECAE91A34D"
2871
2872#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2873 "2C01CAD19EA484A87EA4377637E75500" \
2874 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2875 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2876
2877#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2878 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2879 "910E4168387E3C30AA1E00C339A79508" \
2880 "8452DD96A9A5EA5D9DCA68DA636032AF"
2881
Paul Bakker5121ce52009-01-03 21:22:43 +00002882#define PT_LEN 24
2883#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2884 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002887static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002888{
gufe44c2620da2020-08-03 17:56:50 +02002889#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002890 size_t i;
2891
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002893 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 }
Paul Bakker545570e2010-07-18 09:00:25 +00002895
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002897 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002899#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002901 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002905#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002908}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002910
Paul Bakker5121ce52009-01-03 21:22:43 +00002911/*
2912 * Checkup routine
2913 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002914int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002915{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002916 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002918 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002920 unsigned char rsa_plaintext[PT_LEN];
2921 unsigned char rsa_decrypted[PT_LEN];
2922 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002923#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002924 unsigned char sha1sum[20];
2925#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Hanno Becker3a701162017-08-22 13:52:43 +01002927 mbedtls_mpi K;
2928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 mbedtls_mpi_init(&K);
2930 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2933 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2934 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2935 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2936 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2937 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2938 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2939 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2940 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2941 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 if (verbose != 0) {
2946 mbedtls_printf(" RSA key validation: ");
2947 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2950 mbedtls_rsa_check_privkey(&rsa) != 0) {
2951 if (verbose != 0) {
2952 mbedtls_printf("failed\n");
2953 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
Hanno Becker5bc87292017-05-03 15:09:31 +01002955 ret = 1;
2956 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002957 }
2958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 if (verbose != 0) {
2960 mbedtls_printf("passed\n PKCS#1 encryption : ");
2961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2966 PT_LEN, rsa_plaintext,
2967 rsa_ciphertext) != 0) {
2968 if (verbose != 0) {
2969 mbedtls_printf("failed\n");
2970 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
Hanno Becker5bc87292017-05-03 15:09:31 +01002972 ret = 1;
2973 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002974 }
2975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 if (verbose != 0) {
2977 mbedtls_printf("passed\n PKCS#1 decryption : ");
2978 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2981 &len, rsa_ciphertext, rsa_decrypted,
2982 sizeof(rsa_decrypted)) != 0) {
2983 if (verbose != 0) {
2984 mbedtls_printf("failed\n");
2985 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002986
Hanno Becker5bc87292017-05-03 15:09:31 +01002987 ret = 1;
2988 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002989 }
2990
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2992 if (verbose != 0) {
2993 mbedtls_printf("failed\n");
2994 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002995
Hanno Becker5bc87292017-05-03 15:09:31 +01002996 ret = 1;
2997 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002998 }
2999
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 if (verbose != 0) {
3001 mbedtls_printf("passed\n");
3002 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003003
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003004#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 if (verbose != 0) {
3006 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01003007 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01003009 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
3010 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 if (verbose != 0) {
3012 mbedtls_printf("failed\n");
3013 }
3014
3015 return 1;
3016 }
3017
3018 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
3019 MBEDTLS_MD_SHA1, 20,
3020 sha1sum, rsa_ciphertext) != 0) {
3021 if (verbose != 0) {
3022 mbedtls_printf("failed\n");
3023 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003024
Hanno Becker5bc87292017-05-03 15:09:31 +01003025 ret = 1;
3026 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 }
3028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 if (verbose != 0) {
3030 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
3031 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
3034 sha1sum, rsa_ciphertext) != 0) {
3035 if (verbose != 0) {
3036 mbedtls_printf("failed\n");
3037 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Hanno Becker5bc87292017-05-03 15:09:31 +01003039 ret = 1;
3040 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003041 }
3042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 if (verbose != 0) {
3044 mbedtls_printf("passed\n");
3045 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003046#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 if (verbose != 0) {
3049 mbedtls_printf("\n");
3050 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003051
Paul Bakker3d8fb632014-04-17 12:42:41 +02003052cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 mbedtls_mpi_free(&K);
3054 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02003056 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003059}
3060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063#endif /* MBEDTLS_RSA_C */