blob: 2c338691200e9168cc7dfca6d52b8de408f44a09 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgmane3c05852023-11-03 12:21:36 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
Hanno Becker74716312017-10-02 10:00:37 +01007
Paul Bakker5121ce52009-01-03 21:22:43 +00008/*
Simon Butcherbdae02c2016-01-20 00:44:42 +00009 * The following sources were referenced in the design of this implementation
10 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000011 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000012 * [1] A method for obtaining digital signatures and public-key cryptosystems
13 * R Rivest, A Shamir, and L Adleman
14 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
15 *
16 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
17 * Menezes, van Oorschot and Vanstone
18 *
Janos Follathe81102e2017-03-22 13:38:28 +000019 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
20 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
21 * Stefan Mangard
22 * https://arxiv.org/abs/1702.08719v2
23 *
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/rsa.h"
Janos Follathd6b09652023-11-21 09:33:54 +000031#include "bignum_core.h"
Chris Jones66a4cd42021-03-09 16:04:12 +000032#include "rsa_alt_helpers.h"
Tomi Fontanilles573dc232023-12-10 14:57:51 +020033#include "rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/oid.h"
Valerio Settib328c442024-01-23 10:48:45 +010035#include "mbedtls/asn1write.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000037#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020038#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020039#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020040#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000041
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
gufe44c2620da2020-08-03 17:56:50 +020044#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000045#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010049
Valerio Setti201e6432024-02-01 17:19:37 +010050/*
51 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
52 *
53 * The value zero is:
54 * - never a valid value for an RSA parameter
55 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
56 *
57 * Since values can't be omitted in PKCS#1, passing a zero value to
58 * rsa_complete() would be incorrect, so reject zero values early.
59 */
60static int asn1_get_nonzero_mpi(unsigned char **p,
61 const unsigned char *end,
62 mbedtls_mpi *X)
63{
64 int ret;
65
66 ret = mbedtls_asn1_get_mpi(p, end, X);
67 if (ret != 0) {
68 return ret;
69 }
70
71 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
72 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
73 }
74
75 return 0;
76}
77
Valerio Setti135ebde2024-02-01 17:00:29 +010078int mbedtls_rsa_parse_key(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +010079{
80 int ret, version;
81 size_t len;
82 unsigned char *p, *end;
83
84 mbedtls_mpi T;
85 mbedtls_mpi_init(&T);
86
87 p = (unsigned char *) key;
88 end = p + keylen;
89
90 /*
91 * This function parses the RSAPrivateKey (PKCS#1)
92 *
93 * RSAPrivateKey ::= SEQUENCE {
94 * version Version,
95 * modulus INTEGER, -- n
96 * publicExponent INTEGER, -- e
97 * privateExponent INTEGER, -- d
98 * prime1 INTEGER, -- p
99 * prime2 INTEGER, -- q
100 * exponent1 INTEGER, -- d mod (p-1)
101 * exponent2 INTEGER, -- d mod (q-1)
102 * coefficient INTEGER, -- (inverse of q) mod p
103 * otherPrimeInfos OtherPrimeInfos OPTIONAL
104 * }
105 */
106 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
107 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
108 return ret;
109 }
110
Valerio Setti9de84bd2024-02-08 17:40:27 +0100111 if (end != p + len) {
112 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
113 }
114
Valerio Setti44ff9502024-02-01 16:51:05 +0100115 end = p + len;
116
117 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
118 return ret;
119 }
120
121 if (version != 0) {
122 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
123 }
124
125 /* Import N */
126 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
127 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
128 NULL, NULL)) != 0) {
129 goto cleanup;
130 }
131
132 /* Import E */
133 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
134 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
135 NULL, &T)) != 0) {
136 goto cleanup;
137 }
138
139 /* Import D */
140 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
141 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
142 &T, NULL)) != 0) {
143 goto cleanup;
144 }
145
146 /* Import P */
147 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
148 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
149 NULL, NULL)) != 0) {
150 goto cleanup;
151 }
152
153 /* Import Q */
154 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
155 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
156 NULL, NULL)) != 0) {
157 goto cleanup;
158 }
159
160#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
161 /*
162 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
163 * that they can be easily recomputed from D, P and Q. However by
164 * parsing them from the PKCS1 structure it is possible to avoid
165 * recalculating them which both reduces the overhead of loading
166 * RSA private keys into memory and also avoids side channels which
167 * can arise when computing those values, since all of D, P, and Q
168 * are secret. See https://eprint.iacr.org/2020/055 for a
169 * description of one such attack.
170 */
171
172 /* Import DP */
173 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
174 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
175 goto cleanup;
176 }
177
178 /* Import DQ */
179 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
180 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
181 goto cleanup;
182 }
183
184 /* Import QP */
185 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
186 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
187 goto cleanup;
188 }
189
190#else
191 /* Verify existence of the CRT params */
192 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
193 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
194 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
195 goto cleanup;
196 }
197#endif
198
199 /* rsa_complete() doesn't complete anything with the default
200 * implementation but is still called:
201 * - for the benefit of alternative implementation that may want to
202 * pre-compute stuff beyond what's provided (eg Montgomery factors)
203 * - as is also sanity-checks the key
204 *
205 * Furthermore, we also check the public part for consistency with
206 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
207 */
208 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
209 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
210 goto cleanup;
211 }
212
213 if (p != end) {
214 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
215 }
216
217cleanup:
218
219 mbedtls_mpi_free(&T);
220
221 if (ret != 0) {
222 mbedtls_rsa_free(rsa);
223 }
224
225 return ret;
226}
227
Valerio Setti201e6432024-02-01 17:19:37 +0100228int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +0100229{
Valerio Setti201e6432024-02-01 17:19:37 +0100230 unsigned char *p = (unsigned char *) key;
231 unsigned char *end = (unsigned char *) (key + keylen);
Valerio Setti44ff9502024-02-01 16:51:05 +0100232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
233 size_t len;
234
235 /*
236 * RSAPublicKey ::= SEQUENCE {
237 * modulus INTEGER, -- n
238 * publicExponent INTEGER -- e
239 * }
240 */
241
Valerio Setti201e6432024-02-01 17:19:37 +0100242 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
Valerio Setti44ff9502024-02-01 16:51:05 +0100243 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
244 return ret;
245 }
246
Valerio Setti9de84bd2024-02-08 17:40:27 +0100247 if (end != p + len) {
248 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
249 }
250
Valerio Settife329ce2024-02-06 08:00:18 +0100251 end = p + len;
252
Valerio Setti44ff9502024-02-01 16:51:05 +0100253 /* Import N */
Valerio Setti201e6432024-02-01 17:19:37 +0100254 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100255 return ret;
256 }
257
Valerio Setti201e6432024-02-01 17:19:37 +0100258 if ((ret = mbedtls_rsa_import_raw(rsa, p, len, NULL, 0, NULL, 0,
Valerio Setti44ff9502024-02-01 16:51:05 +0100259 NULL, 0, NULL, 0)) != 0) {
260 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
261 }
262
Valerio Setti201e6432024-02-01 17:19:37 +0100263 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100264
265 /* Import E */
Valerio Setti201e6432024-02-01 17:19:37 +0100266 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100267 return ret;
268 }
269
270 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
Valerio Setti201e6432024-02-01 17:19:37 +0100271 NULL, 0, p, len)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100272 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
273 }
274
Valerio Setti201e6432024-02-01 17:19:37 +0100275 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100276
277 if (mbedtls_rsa_complete(rsa) != 0 ||
278 mbedtls_rsa_check_pubkey(rsa) != 0) {
279 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
280 }
281
Valerio Setti201e6432024-02-01 17:19:37 +0100282 if (p != end) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100283 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
284 }
285
286 return 0;
287}
288
Valerio Setti135ebde2024-02-01 17:00:29 +0100289int mbedtls_rsa_write_key(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100290 unsigned char **p)
291{
292 size_t len = 0;
293 int ret;
294
295 mbedtls_mpi T; /* Temporary holding the exported parameters */
296
297 /*
298 * Export the parameters one after another to avoid simultaneous copies.
299 */
300
301 mbedtls_mpi_init(&T);
302
303 /* Export QP */
304 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
305 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
306 goto end_of_export;
307 }
308 len += ret;
309
310 /* Export DQ */
311 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
312 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
313 goto end_of_export;
314 }
315 len += ret;
316
317 /* Export DP */
318 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
319 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
320 goto end_of_export;
321 }
322 len += ret;
323
324 /* Export Q */
325 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, &T, NULL, NULL)) != 0 ||
326 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
327 goto end_of_export;
328 }
329 len += ret;
330
331 /* Export P */
332 if ((ret = mbedtls_rsa_export(rsa, NULL, &T, NULL, NULL, NULL)) != 0 ||
333 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
334 goto end_of_export;
335 }
336 len += ret;
337
338 /* Export D */
339 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, &T, NULL)) != 0 ||
340 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
341 goto end_of_export;
342 }
343 len += ret;
344
345 /* Export E */
346 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
347 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
348 goto end_of_export;
349 }
350 len += ret;
351
352 /* Export N */
353 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
354 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
355 goto end_of_export;
356 }
357 len += ret;
358
359end_of_export:
360
361 mbedtls_mpi_free(&T);
362 if (ret < 0) {
363 return ret;
364 }
365
366 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
367 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
368 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
369 MBEDTLS_ASN1_CONSTRUCTED |
370 MBEDTLS_ASN1_SEQUENCE));
371
372 return (int) len;
373}
374
375/*
376 * RSAPublicKey ::= SEQUENCE {
377 * modulus INTEGER, -- n
378 * publicExponent INTEGER -- e
379 * }
380 */
Valerio Setti135ebde2024-02-01 17:00:29 +0100381int mbedtls_rsa_write_pubkey(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100382 unsigned char **p)
383{
384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
385 size_t len = 0;
386 mbedtls_mpi T;
387
388 mbedtls_mpi_init(&T);
389
390 /* Export E */
391 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
392 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
393 goto end_of_export;
394 }
395 len += ret;
396
397 /* Export N */
398 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
399 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
400 goto end_of_export;
401 }
402 len += ret;
403
404end_of_export:
405
406 mbedtls_mpi_free(&T);
407 if (ret < 0) {
408 return ret;
409 }
410
411 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
412 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
413 MBEDTLS_ASN1_SEQUENCE));
414
415 return (int) len;
416}
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100417
418#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
419
420/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
421 * operation (EME-PKCS1-v1_5 decoding).
422 *
423 * \note The return value from this function is a sensitive value
424 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
425 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
426 * is often a situation that an attacker can provoke and leaking which
427 * one is the result is precisely the information the attacker wants.
428 *
429 * \param input The input buffer which is the payload inside PKCS#1v1.5
430 * encryption padding, called the "encoded message EM"
431 * by the terminology.
432 * \param ilen The length of the payload in the \p input buffer.
433 * \param output The buffer for the payload, called "message M" by the
434 * PKCS#1 terminology. This must be a writable buffer of
435 * length \p output_max_len bytes.
436 * \param olen The address at which to store the length of
437 * the payload. This must not be \c NULL.
438 * \param output_max_len The length in bytes of the output buffer \p output.
439 *
440 * \return \c 0 on success.
441 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
442 * The output buffer is too small for the unpadded payload.
443 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
444 * The input doesn't contain properly formatted padding.
445 */
446static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
447 size_t ilen,
448 unsigned char *output,
449 size_t output_max_len,
450 size_t *olen)
451{
452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
453 size_t i, plaintext_max_size;
454
455 /* The following variables take sensitive values: their value must
456 * not leak into the observable behavior of the function other than
457 * the designated outputs (output, olen, return value). Otherwise
458 * this would open the execution of the function to
459 * side-channel-based variants of the Bleichenbacher padding oracle
460 * attack. Potential side channels include overall timing, memory
461 * access patterns (especially visible to an adversary who has access
462 * to a shared memory cache), and branches (especially visible to
463 * an adversary who has access to a shared code cache or to a shared
464 * branch predictor). */
465 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100466 mbedtls_ct_condition_t bad;
467 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100468 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100469 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100470
471 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
472 : output_max_len;
473
474 /* Check and get padding length in constant time and constant
475 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100476 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100477
478
479 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
480 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100481 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100482
483 /* Read the whole buffer. Set pad_done to nonzero if we find
484 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100485 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100486 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100487 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100488 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100489 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100490 }
491
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100492 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100493 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100494
495 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100496 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100497
498 /* If the padding is valid, set plaintext_size to the number of
499 * remaining bytes after stripping the padding. If the padding
500 * is invalid, avoid leaking this fact through the size of the
501 * output: use the maximum message size that fits in the output
502 * buffer. Do it without branches to avoid leaking the padding
503 * validity through timing. RSA keys are small enough that all the
504 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100505 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100506 bad, (unsigned) plaintext_max_size,
507 (unsigned) (ilen - pad_count - 3));
508
509 /* Set output_too_large to 0 if the plaintext fits in the output
510 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100511 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100512 plaintext_max_size);
513
514 /* Set ret without branches to avoid timing attacks. Return:
515 * - INVALID_PADDING if the padding is bad (bad != 0).
516 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
517 * plaintext does not fit in the output buffer.
518 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100519 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100520 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100521 MBEDTLS_ERR_RSA_INVALID_PADDING,
522 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100523 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100524
525 /* If the padding is bad or the plaintext is too large, zero the
526 * data that we're about to copy to the output buffer.
527 * We need to copy the same amount of data
528 * from the same buffer whether the padding is good or not to
529 * avoid leaking the padding validity through overall timing or
530 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100531 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100532
533 /* If the plaintext is too large, truncate it to the buffer size.
534 * Copy anyway to avoid revealing the length through timing, because
535 * revealing the length is as bad as revealing the padding validity
536 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100537 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100538 (unsigned) plaintext_max_size,
539 (unsigned) plaintext_size);
540
541 /* Move the plaintext to the leftmost position where it can start in
542 * the working buffer, i.e. make it start plaintext_max_size from
543 * the end of the buffer. Do this with a memory access trace that
544 * does not depend on the plaintext size. After this move, the
545 * starting location of the plaintext is no longer sensitive
546 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100547 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
548 plaintext_max_size,
549 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100550
551 /* Finally copy the decrypted plaintext plus trailing zeros into the output
552 * buffer. If output_max_len is 0, then output may be an invalid pointer
553 * and the result of memcpy() would be undefined; prevent undefined
554 * behavior making sure to depend only on output_max_len (the size of the
555 * user-provided output buffer), which is independent from plaintext
556 * length, validity of padding, success of the decryption, and other
557 * secrets. */
558 if (output_max_len != 0) {
559 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
560 }
561
562 /* Report the amount of data we copied to the output buffer. In case
563 * of errors (bad padding or output too large), the value of *olen
564 * when this function returns is not specified. Making it equivalent
565 * to the good case limits the risks of leaking the padding validity. */
566 *olen = plaintext_size;
567
568 return ret;
569}
570
571#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
572
Hanno Beckera565f542017-10-11 11:00:19 +0100573#if !defined(MBEDTLS_RSA_ALT)
574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
576 const mbedtls_mpi *N,
577 const mbedtls_mpi *P, const mbedtls_mpi *Q,
578 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100579{
Janos Follath24eed8d2019-11-22 13:21:35 +0000580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
583 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
584 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
585 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
586 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
587 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100588 }
589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (N != NULL) {
591 ctx->len = mbedtls_mpi_size(&ctx->N);
592 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100595}
596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
598 unsigned char const *N, size_t N_len,
599 unsigned char const *P, size_t P_len,
600 unsigned char const *Q, size_t Q_len,
601 unsigned char const *D, size_t D_len,
602 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100603{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000604 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if (N != NULL) {
607 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
608 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100609 }
610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (P != NULL) {
612 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
613 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (Q != NULL) {
616 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
617 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (D != NULL) {
620 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
621 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if (E != NULL) {
624 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
625 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100626
627cleanup:
628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (ret != 0) {
630 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
631 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100634}
635
Hanno Becker705fc682017-10-10 17:57:02 +0100636/*
637 * Checks whether the context fields are set in such a way
638 * that the RSA primitives will be able to execute without error.
639 * It does *not* make guarantees for consistency of the parameters.
640 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100641static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
642 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100643{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100644#if !defined(MBEDTLS_RSA_NO_CRT)
645 /* blinding_needed is only used for NO_CRT to decide whether
646 * P,Q need to be present or not. */
647 ((void) blinding_needed);
648#endif
649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
651 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
652 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000653 }
Hanno Becker705fc682017-10-10 17:57:02 +0100654
655 /*
656 * 1. Modular exponentiation needs positive, odd moduli.
657 */
658
659 /* Modular exponentiation wrt. N is always used for
660 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
662 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
663 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100664 }
665
666#if !defined(MBEDTLS_RSA_NO_CRT)
667 /* Modular exponentiation for P and Q is only
668 * used for private key operations and if CRT
669 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (is_priv &&
671 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
672 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
673 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
674 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
675 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100676 }
677#endif /* !MBEDTLS_RSA_NO_CRT */
678
679 /*
680 * 2. Exponents must be positive
681 */
682
683 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
685 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
686 }
Hanno Becker705fc682017-10-10 17:57:02 +0100687
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100688#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100689 /* For private key operations, use D or DP & DQ
690 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
692 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
693 }
Hanno Becker705fc682017-10-10 17:57:02 +0100694#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (is_priv &&
696 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
697 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
698 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100699 }
700#endif /* MBEDTLS_RSA_NO_CRT */
701
702 /* Blinding shouldn't make exponents negative either,
703 * so check that P, Q >= 1 if that hasn't yet been
704 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100705#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (is_priv && blinding_needed &&
707 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
708 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
709 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100710 }
711#endif
712
713 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100714 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100715#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (is_priv &&
717 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
718 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100719 }
720#endif
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100723}
724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100726{
727 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500728 int have_N, have_P, have_Q, have_D, have_E;
729#if !defined(MBEDTLS_RSA_NO_CRT)
730 int have_DP, have_DQ, have_QP;
731#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500732 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
735 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
736 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
737 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
738 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500739
740#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
742 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
743 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500744#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100745
Hanno Becker617c1ae2017-08-23 14:11:24 +0100746 /*
747 * Check whether provided parameters are enough
748 * to deduce all others. The following incomplete
749 * parameter sets for private keys are supported:
750 *
751 * (1) P, Q missing.
752 * (2) D and potentially N missing.
753 *
754 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100755
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500756 n_missing = have_P && have_Q && have_D && have_E;
757 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
758 d_missing = have_P && have_Q && !have_D && have_E;
759 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100760
761 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500762 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 if (!is_priv && !is_pub) {
765 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
766 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100767
768 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100769 * Step 1: Deduce N if P, Q are provided.
770 */
771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (!have_N && have_P && have_Q) {
773 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
774 &ctx->Q)) != 0) {
775 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100776 }
777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100779 }
780
781 /*
782 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100783 */
784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (pq_missing) {
786 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
787 &ctx->P, &ctx->Q);
788 if (ret != 0) {
789 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
790 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 } else if (d_missing) {
793 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
794 &ctx->Q,
795 &ctx->E,
796 &ctx->D)) != 0) {
797 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100798 }
799 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100800
Hanno Becker617c1ae2017-08-23 14:11:24 +0100801 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100802 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100803 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100804 */
805
Hanno Becker23344b52017-08-23 07:43:27 +0100806#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (is_priv && !(have_DP && have_DQ && have_QP)) {
808 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
809 &ctx->DP, &ctx->DQ, &ctx->QP);
810 if (ret != 0) {
811 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
812 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100813 }
Hanno Becker23344b52017-08-23 07:43:27 +0100814#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100815
816 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100817 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100818 */
819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100821}
822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
824 unsigned char *N, size_t N_len,
825 unsigned char *P, size_t P_len,
826 unsigned char *Q, size_t Q_len,
827 unsigned char *D, size_t D_len,
828 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100829{
830 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500831 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100832
833 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500834 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
836 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
837 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
838 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
839 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100842 /* If we're trying to export private parameters for a public key,
843 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (P != NULL || Q != NULL || D != NULL) {
845 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
846 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100847
848 }
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (N != NULL) {
851 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
852 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (P != NULL) {
855 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
856 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (Q != NULL) {
859 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
860 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (D != NULL) {
863 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
864 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (E != NULL) {
867 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
868 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100869
870cleanup:
871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100873}
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
876 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
877 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100878{
Janos Follath24eed8d2019-11-22 13:21:35 +0000879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500880 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100881
882 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500883 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
885 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
886 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
887 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
888 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100891 /* If we're trying to export private parameters for a public key,
892 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (P != NULL || Q != NULL || D != NULL) {
894 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
895 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100896
897 }
898
899 /* Export all requested core parameters. */
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
902 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
903 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
904 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
905 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
906 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100907 }
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100910}
911
912/*
913 * Export CRT parameters
914 * This must also be implemented if CRT is not used, for being able to
915 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
916 * can be used in this case.
917 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
919 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100920{
Janos Follath24eed8d2019-11-22 13:21:35 +0000921 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500922 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100923
924 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500925 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
927 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
928 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
929 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
930 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (!is_priv) {
933 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
934 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100935
Hanno Beckerdc95c892017-08-23 06:57:02 +0100936#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100937 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
939 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
940 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
941 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100942 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100943#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
945 DP, DQ, QP)) != 0) {
946 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100947 }
948#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100951}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100952
Paul Bakker5121ce52009-01-03 21:22:43 +0000953/*
954 * Initialize an RSA context
955 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000957{
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Ronald Cronc1905a12021-06-05 11:11:14 +0200960 ctx->padding = MBEDTLS_RSA_PKCS_V15;
961 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100964 /* Set ctx->ver to nonzero to indicate that the mutex has been
965 * initialized and will need to be freed. */
966 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200968#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000969}
970
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100971/*
972 * Set padding for an existing RSA context
973 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100974int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
975 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100976{
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200978#if defined(MBEDTLS_PKCS1_V15)
979 case MBEDTLS_RSA_PKCS_V15:
980 break;
981#endif
982
983#if defined(MBEDTLS_PKCS1_V21)
984 case MBEDTLS_RSA_PKCS_V21:
985 break;
986#endif
987 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200989 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200990
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200991#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
993 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200994 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200995 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 return MBEDTLS_ERR_RSA_INVALID_PADDING;
997 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200998 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200999#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001000
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +01001001 ctx->padding = padding;
1002 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +02001003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +01001005}
1006
Hanno Becker617c1ae2017-08-23 14:11:24 +01001007/*
Yanray Wang83548b52023-03-15 16:46:34 +08001008 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +08001009 */
1010int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
1011{
Yanray Wang644b9012023-03-15 16:50:31 +08001012 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +08001013}
1014
1015/*
Yanray Wang12cb3962023-03-01 10:20:02 +08001016 * Get hash identifier of mbedtls_md_type_t type
1017 */
Yanray Wangd41684e2023-03-17 18:54:22 +08001018int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +08001019{
Yanray Wang644b9012023-03-15 16:50:31 +08001020 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +08001021}
1022
1023/*
Hanno Becker617c1ae2017-08-23 14:11:24 +01001024 * Get length in bytes of RSA modulus
1025 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +01001027{
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +01001029}
1030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
1033/*
1034 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001035 *
1036 * This generation method follows the RSA key pair generation procedure of
1037 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
1040 int (*f_rng)(void *, unsigned char *, size_t),
1041 void *p_rng,
1042 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +00001043{
Janos Follath24eed8d2019-11-22 13:21:35 +00001044 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -08001045 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +01001046 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
Janos Follathb8fc1b02018-09-03 15:37:01 +01001048 /*
1049 * If the modulus is 1024 bit long or shorter, then the security strength of
1050 * the RSA algorithm is less than or equal to 80 bits and therefore an error
1051 * rate of 2^-80 is sufficient.
1052 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +01001054 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 }
Janos Follathb8fc1b02018-09-03 15:37:01 +01001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 mbedtls_mpi_init(&H);
1058 mbedtls_mpi_init(&G);
1059 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001061 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +01001062 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1063 goto cleanup;
1064 }
1065
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001066 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001067 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1068 goto cleanup;
1069 }
1070
1071 /*
1072 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001073 * 1. |P-Q| > 2^( nbits / 2 - 100 )
1074 * 2. GCD( E, (P-1)*(Q-1) ) == 1
1075 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 do {
1080 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
1081 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
1084 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001086 /* 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 +01001087 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
1088 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001092 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if (H.s < 0) {
1094 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
1095 }
Janos Follathef441782016-09-21 13:18:12 +01001096
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001097 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
1099 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
1100 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001101
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001102 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
1104 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -08001105 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001107
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001108 /* 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 +01001109 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
1110 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
1111 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 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 -08001114 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001116
1117 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001119
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001120 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
1122 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001127
Jethro Beekman97f95c92018-02-13 15:50:36 -08001128#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 * DP = D mod (P - 1)
1131 * DQ = D mod (Q - 1)
1132 * QP = Q^-1 mod P
1133 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
1135 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001136#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Hanno Becker83aad1f2017-08-23 06:45:10 +01001138 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
1141cleanup:
1142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 mbedtls_mpi_free(&H);
1144 mbedtls_mpi_free(&G);
1145 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if (ret != 0) {
1148 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +01001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if ((-ret & ~0x7f) == 0) {
1151 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
1152 }
1153 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 }
1155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001157}
1158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
1161/*
1162 * Check a public RSA key
1163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001165{
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
1167 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001168 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
1171 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001172 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
1175 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
1176 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
1177 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1178 }
1179
1180 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001181}
1182
1183/*
Hanno Becker705fc682017-10-10 17:57:02 +01001184 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001186int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001187{
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
1189 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
1190 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 }
Paul Bakker48377d92013-08-30 12:06:24 +02001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
1194 &ctx->D, &ctx->E, NULL, NULL) != 0) {
1195 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001197
Hanno Beckerb269a852017-08-25 08:03:21 +01001198#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
1200 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
1201 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +01001202 }
1203#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001206}
1207
1208/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001209 * Check if contexts holding a public and private key match
1210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001211int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
1212 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001213{
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
1215 mbedtls_rsa_check_privkey(prv) != 0) {
1216 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001217 }
1218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
1220 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
1221 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001222 }
1223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001225}
1226
1227/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001228 * Do an RSA public key operation
1229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001230int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
1231 const unsigned char *input,
1232 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001233{
Janos Follath24eed8d2019-11-22 13:21:35 +00001234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001235 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
1239 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1240 }
Hanno Becker705fc682017-10-10 17:57:02 +01001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001244#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1246 return ret;
1247 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001248#endif
1249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +00001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001253 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1254 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 }
1256
1257 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
1259 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
1261cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1264 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1265 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001266#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if (ret != 0) {
1271 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
1272 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001275}
1276
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001277/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001278 * Generate or update blinding values, see section 10 of:
1279 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001280 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001281 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1284 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001285{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001286 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001287 mbedtls_mpi R;
1288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001292 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1294 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1295 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1296 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001297
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001298 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001299 }
1300
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001301 /* Unblinding value: Vf = random number, invertible mod N */
1302 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +02001304 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
1305 goto cleanup;
1306 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 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 +02001309
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001310 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
1312 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
1313 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001314
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001315 /* At this point, Vi is invertible mod N if and only if both Vf and R
1316 * are invertible mod N. If one of them isn't, we don't need to know
1317 * which one, we just loop and choose new values for both of them.
1318 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
1320 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001321 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -05001325
1326 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
1328 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001329
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001330 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001331 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 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 +02001333
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001334
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001335cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001339}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001340
Paul Bakker5121ce52009-01-03 21:22:43 +00001341/*
Janos Follathd6b09652023-11-21 09:33:54 +00001342 * Unblind
1343 * T = T * Vf mod N
1344 */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001345static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, const mbedtls_mpi *N)
Janos Follathd6b09652023-11-21 09:33:54 +00001346{
1347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1348 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
1349 const size_t nlimbs = N->n;
1350 const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
1351 mbedtls_mpi RR, M_T;
1352
1353 mbedtls_mpi_init(&RR);
1354 mbedtls_mpi_init(&M_T);
1355
1356 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
1357 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
1358
1359 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
1360 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
1361
Janos Follathe6750b22023-12-27 10:22:59 +00001362 /* T = T * Vf mod N
1363 * Reminder: montmul(A, B, N) = A * B * R^-1 mod N
1364 * Usually both operands are multiplied by R mod N beforehand (by calling
1365 * `to_mont_rep()` on them), yielding a result that's also * R mod N (aka
1366 * "in the Montgomery domain"). Here we only multiply one operand by R mod
1367 * N, so the result is directly what we want - no need to call
1368 * `from_mont_rep()` on it. */
Janos Follathd6b09652023-11-21 09:33:54 +00001369 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 +00001370 mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1371
1372cleanup:
1373
1374 mbedtls_mpi_free(&RR);
1375 mbedtls_mpi_free(&M_T);
1376
1377 return ret;
1378}
1379
1380/*
Janos Follathe81102e2017-03-22 13:38:28 +00001381 * Exponent blinding supposed to prevent side-channel attacks using multiple
1382 * traces of measurements to recover the RSA key. The more collisions are there,
1383 * the more bits of the key can be recovered. See [3].
1384 *
1385 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001386 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001387 *
1388 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001389 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001390 *
1391 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1392 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1393 * Thus in this sense with 28 byte blinding the security is not reduced by
1394 * side-channel attacks like the one in [3])
1395 *
1396 * This countermeasure does not help if the key recovery is possible with a
1397 * single trace.
1398 */
1399#define RSA_EXPONENT_BLINDING 28
1400
1401/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 * Do an RSA private key operation
1403 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001404int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1405 int (*f_rng)(void *, unsigned char *, size_t),
1406 void *p_rng,
1407 const unsigned char *input,
1408 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001409{
Janos Follath24eed8d2019-11-22 13:21:35 +00001410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001411 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001412
1413 /* Temporary holding the result */
1414 mbedtls_mpi T;
1415
1416 /* Temporaries holding P-1, Q-1 and the
1417 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001418 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001419
1420#if !defined(MBEDTLS_RSA_NO_CRT)
1421 /* Temporaries holding the results mod p resp. mod q. */
1422 mbedtls_mpi TP, TQ;
1423
1424 /* Temporaries holding the blinded exponents for
1425 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001426 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001427#else
1428 /* Temporary holding the blinded exponent (if used). */
1429 mbedtls_mpi D_blind;
Hanno Becker43f94722017-08-25 11:50:00 +01001430#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001431
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001432 /* Temporaries holding the initial input and the double
1433 * checked result; should be the same in the end. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001434 mbedtls_mpi input_blinded, check_result_blinded;
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 if (f_rng == NULL) {
1437 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1438 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (rsa_check_context(ctx, 1 /* private key checks */,
1441 1 /* blinding on */) != 0) {
1442 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001443 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001444
Hanno Becker06811ce2017-05-03 15:10:34 +01001445#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1447 return ret;
1448 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001449#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001450
Hanno Becker06811ce2017-05-03 15:10:34 +01001451 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 mbedtls_mpi_init(&P1);
1455 mbedtls_mpi_init(&Q1);
1456 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001457
Janos Follathe81102e2017-03-22 13:38:28 +00001458#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001460#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 mbedtls_mpi_init(&DP_blind);
1462 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001463#endif
1464
Hanno Becker06811ce2017-05-03 15:10:34 +01001465#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001467#endif
1468
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001469 mbedtls_mpi_init(&input_blinded);
1470 mbedtls_mpi_init(&check_result_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001471
1472 /* End of MPI initialization */
1473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1475 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001476 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1477 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 }
1479
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001480 /*
1481 * Blinding
1482 * T = T * Vi mod N
1483 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1485 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1486 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001487
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001488 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&input_blinded, &T));
Janos Follath6bcbc922023-11-21 09:46:43 +00001489
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001490 /*
1491 * Exponent blinding
1492 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1494 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001495
Janos Follathf9203b42017-03-22 15:13:15 +00001496#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001497 /*
1498 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1499 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1501 f_rng, p_rng));
1502 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1503 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1504 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathf9203b42017-03-22 15:13:15 +00001505#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001506 /*
1507 * DP_blind = ( P - 1 ) * R + DP
1508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1510 f_rng, p_rng));
1511 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1512 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1513 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001514
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001515 /*
1516 * DQ_blind = ( Q - 1 ) * R + DQ
1517 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1519 f_rng, p_rng));
1520 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1521 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1522 &ctx->DQ));
Janos Follathe81102e2017-03-22 13:38:28 +00001523#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath47ee7702023-12-27 10:33:00 +00001526 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &D_blind, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001527#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001528 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001529 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001530 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001531 * TP = input ^ dP mod P
1532 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001534
Janos Follath47ee7702023-12-27 10:33:00 +00001535 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, &DP_blind, &ctx->P, &ctx->RP));
1536 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, &DQ_blind, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
1538 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001539 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1542 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1543 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
1545 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001546 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1549 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001551
Hanno Becker2dec5e82017-10-03 07:49:52 +01001552 /* Verify the result to prevent glitching attacks. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001553 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&check_result_blinded, &T, &ctx->E,
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 &ctx->N, &ctx->RN));
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001555 if (mbedtls_mpi_cmp_mpi(&check_result_blinded, &input_blinded) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001556 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1557 goto cleanup;
1558 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001559
Janos Follath6bcbc922023-11-21 09:46:43 +00001560 /*
1561 * Unblind
1562 * T = T * Vf mod N
1563 */
1564 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1565
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
1569cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1572 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1573 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001574#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 mbedtls_mpi_free(&P1);
1577 mbedtls_mpi_free(&Q1);
1578 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001579
Janos Follathe81102e2017-03-22 13:38:28 +00001580#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001582#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 mbedtls_mpi_free(&DP_blind);
1584 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001585#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001588
1589#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001591#endif
1592
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001593 mbedtls_mpi_free(&check_result_blinded);
1594 mbedtls_mpi_free(&input_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (ret != 0 && ret >= -0x007f) {
1597 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001601}
1602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001604/**
1605 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1606 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001607 * \param dst buffer to mask
1608 * \param dlen length of destination buffer
1609 * \param src source of the mask generation
1610 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001611 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001612 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001613static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1614 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001615{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001616 unsigned char counter[4];
1617 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001618 unsigned int hlen;
1619 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001620 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001621 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001622 const mbedtls_md_info_t *md_info;
1623 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 mbedtls_md_init(&md_ctx);
1626 md_info = mbedtls_md_info_from_type(md_alg);
1627 if (md_info == NULL) {
1628 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1629 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 mbedtls_md_init(&md_ctx);
1632 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001633 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 memset(mask, 0, sizeof(mask));
1639 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001640
Simon Butcher02037452016-03-01 21:19:12 +00001641 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001642 p = dst;
1643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001645 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001647 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001651 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 }
1653 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001654 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 }
1656 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001657 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 }
1659 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001660 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001664 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001666
1667 counter[3]++;
1668
1669 dlen -= use_len;
1670 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001671
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001672exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001677}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001678
1679/**
1680 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1681 *
1682 * \param hash the input hash
1683 * \param hlen length of the input hash
1684 * \param salt the input salt
1685 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001686 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001687 * \param md_alg message digest to use
1688 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001689static int hash_mprime(const unsigned char *hash, size_t hlen,
1690 const unsigned char *salt, size_t slen,
1691 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001692{
1693 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001694
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001695 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001696 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1699 if (md_info == NULL) {
1700 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1701 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 mbedtls_md_init(&md_ctx);
1704 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001705 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 }
1707 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001708 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 }
1710 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001711 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 }
1713 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001714 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 }
1716 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001717 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 }
1719 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001720 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001722
1723exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001727}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001728
1729/**
1730 * Compute a hash.
1731 *
1732 * \param md_alg algorithm to use
1733 * \param input input message to hash
1734 * \param ilen input length
1735 * \param output the output buffer - must be large enough for \p md_alg
1736 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001737static int compute_hash(mbedtls_md_type_t md_alg,
1738 const unsigned char *input, size_t ilen,
1739 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001740{
1741 const mbedtls_md_info_t *md_info;
1742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 md_info = mbedtls_md_info_from_type(md_alg);
1744 if (md_info == NULL) {
1745 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1746 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001749}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001753/*
1754 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1755 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001756int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1757 int (*f_rng)(void *, unsigned char *, size_t),
1758 void *p_rng,
1759 const unsigned char *label, size_t label_len,
1760 size_t ilen,
1761 const unsigned char *input,
1762 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001763{
1764 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001766 unsigned char *p = output;
1767 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if (f_rng == NULL) {
1770 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1771 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001772
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001773 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if (hlen == 0) {
1775 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1776 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001777
1778 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001779
Simon Butcher02037452016-03-01 21:19:12 +00001780 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1782 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1783 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001786
1787 *p++ = 0;
1788
Simon Butcher02037452016-03-01 21:19:12 +00001789 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1791 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1792 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001793
1794 p += hlen;
1795
Simon Butcher02037452016-03-01 21:19:12 +00001796 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1798 if (ret != 0) {
1799 return ret;
1800 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001801 p += hlen;
1802 p += olen - 2 * hlen - 2 - ilen;
1803 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 if (ilen != 0) {
1805 memcpy(p, input, ilen);
1806 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001807
Simon Butcher02037452016-03-01 21:19:12 +00001808 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001810 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 return ret;
1812 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001813
Simon Butcher02037452016-03-01 21:19:12 +00001814 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001816 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 return ret;
1818 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001825/*
1826 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1827 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001828int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1829 int (*f_rng)(void *, unsigned char *, size_t),
1830 void *p_rng, size_t ilen,
1831 const unsigned char *input,
1832 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001833{
1834 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001836 unsigned char *p = output;
1837
Paul Bakkerb3869132013-02-28 17:21:01 +01001838 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001839
Simon Butcher02037452016-03-01 21:19:12 +00001840 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 if (ilen + 11 < ilen || olen < ilen + 11) {
1842 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1843 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001844
1845 nb_pad = olen - 3 - ilen;
1846
1847 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if (f_rng == NULL) {
1850 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1851 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001852
1853 *p++ = MBEDTLS_RSA_CRYPT;
1854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001856 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001857
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001858 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 ret = f_rng(p_rng, p, 1);
1860 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001861
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001862 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if (rng_dl == 0 || ret != 0) {
1864 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1865 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001866
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001867 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001868 }
1869
1870 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (ilen != 0) {
1872 memcpy(p, input, ilen);
1873 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001878
Paul Bakker5121ce52009-01-03 21:22:43 +00001879/*
1880 * Add the message padding, then do an RSA operation
1881 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001882int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1883 int (*f_rng)(void *, unsigned char *, size_t),
1884 void *p_rng,
1885 size_t ilen,
1886 const unsigned char *input,
1887 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001888{
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890#if defined(MBEDTLS_PKCS1_V15)
1891 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1893 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001894#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896#if defined(MBEDTLS_PKCS1_V21)
1897 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1899 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001900#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001901
1902 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001905}
1906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001908/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001909 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001911int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1912 int (*f_rng)(void *, unsigned char *, size_t),
1913 void *p_rng,
1914 const unsigned char *label, size_t label_len,
1915 size_t *olen,
1916 const unsigned char *input,
1917 unsigned char *output,
1918 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001919{
Janos Follath24eed8d2019-11-22 13:21:35 +00001920 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001921 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001922 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001923 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001925 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001926 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001927
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001928 /*
1929 * Parameters sanity checks
1930 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1932 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001934
1935 ilen = ctx->len;
1936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 if (ilen < 16 || ilen > sizeof(buf)) {
1938 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1939 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001940
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001941 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (hlen == 0) {
1943 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1944 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001945
Janos Follathc17cda12016-02-11 11:08:18 +00001946 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (2 * hlen + 2 > ilen) {
1948 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1949 }
Janos Follathc17cda12016-02-11 11:08:18 +00001950
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001951 /*
1952 * RSA operation
1953 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001957 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001959
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001960 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001961 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001962 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001963 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001965 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 /* DB: Apply dbMask to maskedDB */
1967 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001968 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001969 goto cleanup;
1970 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001971
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001972 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1974 label, label_len, lhash);
1975 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001976 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001978
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001979 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001980 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001981 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 p = buf;
1983
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001984 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001985
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001986 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001987
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001988 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001989 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001990 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001991
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001992 /* Get zero-padding len, but always read till end of buffer
1993 * (minus one, for the 01 byte) */
1994 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001995 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001997 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1998 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001999 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002000
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002001 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002002 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01002003
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01002004 /*
2005 * The only information "leaked" is whether the padding was correct or not
2006 * (eg, no data is copied if it was not correct). This meets the
2007 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
2008 * the different error conditions.
2009 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01002010 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002011 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2012 goto cleanup;
2013 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002014
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002015 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002016 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
2017 goto cleanup;
2018 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002019
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002020 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if (*olen != 0) {
2022 memcpy(output, p, *olen);
2023 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002024 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002025
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002026cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 mbedtls_platform_zeroize(buf, sizeof(buf));
2028 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002031}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002035/*
2036 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
2037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
2039 int (*f_rng)(void *, unsigned char *, size_t),
2040 void *p_rng,
2041 size_t *olen,
2042 const unsigned char *input,
2043 unsigned char *output,
2044 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002045{
2046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2047 size_t ilen;
2048 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
2049
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002050 ilen = ctx->len;
2051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2053 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2054 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (ilen < 16 || ilen > sizeof(buf)) {
2057 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2058 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002063 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
2067 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002068
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002069cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002073}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002075
2076/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002077 * Do an RSA operation, then remove the message padding
2078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002079int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
2080 int (*f_rng)(void *, unsigned char *, size_t),
2081 void *p_rng,
2082 size_t *olen,
2083 const unsigned char *input,
2084 unsigned char *output,
2085 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01002086{
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#if defined(MBEDTLS_PKCS1_V15)
2089 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
2091 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02002092#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094#if defined(MBEDTLS_PKCS1_V21)
2095 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
2097 olen, input, output,
2098 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01002099#endif
2100
2101 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002103 }
2104}
2105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002107static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2108 int (*f_rng)(void *, unsigned char *, size_t),
2109 void *p_rng,
2110 mbedtls_md_type_t md_alg,
2111 unsigned int hashlen,
2112 const unsigned char *hash,
2113 int saltlen,
2114 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002115{
2116 size_t olen;
2117 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002118 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002119 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00002120 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002121 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002122 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002125 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if (f_rng == NULL) {
2129 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2130 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002131
2132 olen = ctx->len;
2133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002135 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002136 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (exp_hashlen == 0) {
2138 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2139 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (hashlen != exp_hashlen) {
2142 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2143 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002144 }
2145
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002146 hash_id = (mbedtls_md_type_t) ctx->hash_id;
2147 if (hash_id == MBEDTLS_MD_NONE) {
2148 hash_id = md_alg;
2149 }
2150 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (hlen == 0) {
2152 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2153 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
2156 /* Calculate the largest possible salt length, up to the hash size.
2157 * Normally this is the hash length, which is the maximum salt length
2158 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
2159 * enough room, use the maximum salt length that fits. The constraint is
2160 * that the hash length plus the salt length plus 2 bytes must be at most
2161 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
2162 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002163 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (olen < hlen + min_slen + 2) {
2165 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2166 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002167 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002169 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 }
2171 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
2172 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2173 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02002174 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002175 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01002178
Simon Butcher02037452016-03-01 21:19:12 +00002179 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002181 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01002182 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002183
2184 /* Generate salt of length slen in place in the encoded message */
2185 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
2187 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
2188 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02002189
Paul Bakkerb3869132013-02-28 17:21:01 +01002190 p += slen;
2191
Simon Butcher02037452016-03-01 21:19:12 +00002192 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002193 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (ret != 0) {
2195 return ret;
2196 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002197
Simon Butcher02037452016-03-01 21:19:12 +00002198 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002200 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002202
Simon Butcher02037452016-03-01 21:19:12 +00002203 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002204 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if (ret != 0) {
2206 return ret;
2207 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2210 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01002211
2212 p += hlen;
2213 *p++ = 0xBC;
2214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002216}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002217
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002218static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2219 int (*f_rng)(void *, unsigned char *, size_t),
2220 void *p_rng,
2221 mbedtls_md_type_t md_alg,
2222 unsigned int hashlen,
2223 const unsigned char *hash,
2224 int saltlen,
2225 unsigned char *sig)
2226{
2227 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2228 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2229 }
2230 if (ctx->hash_id == MBEDTLS_MD_NONE) {
2231 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2232 }
2233 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
2234 sig);
2235}
2236
2237int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2238 int (*f_rng)(void *, unsigned char *, size_t),
2239 void *p_rng,
2240 mbedtls_md_type_t md_alg,
2241 unsigned int hashlen,
2242 const unsigned char *hash,
2243 unsigned char *sig)
2244{
2245 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
2246 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2247}
2248
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002249/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02002250 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
2251 * the option to pass in the salt length.
2252 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002253int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
2254 int (*f_rng)(void *, unsigned char *, size_t),
2255 void *p_rng,
2256 mbedtls_md_type_t md_alg,
2257 unsigned int hashlen,
2258 const unsigned char *hash,
2259 int saltlen,
2260 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02002261{
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2263 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02002264}
2265
Cédric Meuterf3fab332020-04-25 11:30:45 +02002266/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002267 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
2268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002269int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2270 int (*f_rng)(void *, unsigned char *, size_t),
2271 void *p_rng,
2272 mbedtls_md_type_t md_alg,
2273 unsigned int hashlen,
2274 const unsigned char *hash,
2275 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002276{
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2278 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002283/*
2284 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
2285 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002286
2287/* Construct a PKCS v1.5 encoding of a hashed message
2288 *
2289 * This is used both for signature generation and verification.
2290 *
2291 * Parameters:
2292 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002293 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002294 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002295 * - hash: Buffer containing the hashed message or the raw data.
2296 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002297 * - dst: Buffer to hold the encoded message.
2298 *
2299 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002300 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002301 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002302 *
2303 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002304static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
2305 unsigned int hashlen,
2306 const unsigned char *hash,
2307 size_t dst_len,
2308 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01002309{
2310 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002311 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002312 unsigned char *p = dst;
2313 const char *oid = NULL;
2314
2315 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002317 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 if (md_size == 0) {
2319 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2320 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2323 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2324 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (hashlen != md_size) {
2327 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2328 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002329
2330 /* Double-check that 8 + hashlen + oid_size can be used as a
2331 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01002333 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 10 + hashlen + oid_size < 10 + hashlen) {
2335 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2336 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002337
2338 /*
2339 * Static bounds check:
2340 * - Need 10 bytes for five tag-length pairs.
2341 * (Insist on 1-byte length encodings to protect against variants of
2342 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2343 * - Need hashlen bytes for hash
2344 * - Need oid_size bytes for hash alg OID.
2345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 if (nb_pad < 10 + hashlen + oid_size) {
2347 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2348 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002349 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 } else {
2351 if (nb_pad < hashlen) {
2352 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2353 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002354
2355 nb_pad -= hashlen;
2356 }
2357
Hanno Becker2b2f8982017-09-27 17:10:03 +01002358 /* Need space for signature header and padding delimiter (3 bytes),
2359 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (nb_pad < 3 + 8) {
2361 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2362 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002363 nb_pad -= 3;
2364
2365 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002366 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002367
2368 /* Write signature header and padding */
2369 *p++ = 0;
2370 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002372 p += nb_pad;
2373 *p++ = 0;
2374
2375 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 if (md_alg == MBEDTLS_MD_NONE) {
2377 memcpy(p, hash, hashlen);
2378 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002379 }
2380
2381 /* Signing hashed data, add corresponding ASN.1 structure
2382 *
2383 * DigestInfo ::= SEQUENCE {
2384 * digestAlgorithm DigestAlgorithmIdentifier,
2385 * digest Digest }
2386 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2387 * Digest ::= OCTET STRING
2388 *
2389 * Schematic:
2390 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2391 * TAG-NULL + LEN [ NULL ] ]
2392 * TAG-OCTET + LEN [ HASH ] ]
2393 */
2394 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002396 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002398 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002399 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002401 p += oid_size;
2402 *p++ = MBEDTLS_ASN1_NULL;
2403 *p++ = 0x00;
2404 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002405 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002407 p += hashlen;
2408
2409 /* Just a sanity-check, should be automatic
2410 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (p != dst + dst_len) {
2412 mbedtls_platform_zeroize(dst, dst_len);
2413 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002414 }
2415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002417}
2418
Paul Bakkerb3869132013-02-28 17:21:01 +01002419/*
2420 * Do an RSA operation to sign the message digest
2421 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002422int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2423 int (*f_rng)(void *, unsigned char *, size_t),
2424 void *p_rng,
2425 mbedtls_md_type_t md_alg,
2426 unsigned int hashlen,
2427 const unsigned char *hash,
2428 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002429{
Janos Follath24eed8d2019-11-22 13:21:35 +00002430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002431 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002434 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2438 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2439 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002440
Hanno Beckerfdf38032017-09-06 12:35:55 +01002441 /*
2442 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2443 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2446 ctx->len, sig)) != 0) {
2447 return ret;
2448 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002449
Hanno Beckerfdf38032017-09-06 12:35:55 +01002450 /* Private key operation
2451 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002452 * In order to prevent Lenstra's attack, make the signature in a
2453 * temporary buffer and check it before returning it.
2454 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 sig_try = mbedtls_calloc(1, ctx->len);
2457 if (sig_try == NULL) {
2458 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002459 }
2460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 verif = mbedtls_calloc(1, ctx->len);
2462 if (verif == NULL) {
2463 mbedtls_free(sig_try);
2464 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2465 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2468 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2469
2470 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002471 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2472 goto cleanup;
2473 }
2474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002476
2477cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002478 mbedtls_zeroize_and_free(sig_try, ctx->len);
2479 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (ret != 0) {
2482 memset(sig, '!', ctx->len);
2483 }
2484 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002487
2488/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 * Do an RSA operation to sign the message digest
2490 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2492 int (*f_rng)(void *, unsigned char *, size_t),
2493 void *p_rng,
2494 mbedtls_md_type_t md_alg,
2495 unsigned int hashlen,
2496 const unsigned char *hash,
2497 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002498{
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002500 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504#if defined(MBEDTLS_PKCS1_V15)
2505 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2507 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002508#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510#if defined(MBEDTLS_PKCS1_V21)
2511 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2513 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002514#endif
2515
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002519}
2520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002522/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002523 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002525int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2526 mbedtls_md_type_t md_alg,
2527 unsigned int hashlen,
2528 const unsigned char *hash,
2529 mbedtls_md_type_t mgf1_hash_id,
2530 int expected_salt_len,
2531 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002532{
Janos Follath24eed8d2019-11-22 13:21:35 +00002533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002534 size_t siglen;
2535 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002536 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002537 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002538 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002539 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002543 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002545
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 siglen = ctx->len;
2547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 if (siglen < 16 || siglen > sizeof(buf)) {
2549 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2550 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 if (ret != 0) {
2555 return ret;
2556 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
2558 p = buf;
2559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (buf[siglen - 1] != 0xBC) {
2561 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002562 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (md_alg != MBEDTLS_MD_NONE) {
2565 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002566 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (exp_hashlen == 0) {
2568 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2569 }
2570
2571 if (hashlen != exp_hashlen) {
2572 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2573 }
2574 }
2575
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002576 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (hlen == 0) {
2578 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2579 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002580
Simon Butcher02037452016-03-01 21:19:12 +00002581 /*
2582 * Note: EMSA-PSS verification is over the length of N - 1 bits
2583 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 if (buf[0] >> (8 - siglen * 8 + msb)) {
2587 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2588 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002589
Simon Butcher02037452016-03-01 21:19:12 +00002590 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002592 p++;
2593 siglen -= 1;
2594 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (siglen < hlen + 2) {
2597 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2598 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002599 hash_start = p + siglen - hlen - 1;
2600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2602 if (ret != 0) {
2603 return ret;
2604 }
Paul Bakker02303e82013-01-03 11:08:31 +01002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002609 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (*p++ != 0x01) {
2613 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2614 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002615
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002616 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2619 observed_salt_len != (size_t) expected_salt_len) {
2620 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002621 }
2622
Simon Butcher02037452016-03-01 21:19:12 +00002623 /*
2624 * Generate H = Hash( M' )
2625 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2627 result, mgf1_hash_id);
2628 if (ret != 0) {
2629 return ret;
2630 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (memcmp(hash_start, result, hlen) != 0) {
2633 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2634 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002637}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002638
2639/*
2640 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2641 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2643 mbedtls_md_type_t md_alg,
2644 unsigned int hashlen,
2645 const unsigned char *hash,
2646 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002647{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002648 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002650 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002655 : md_alg;
2656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2658 md_alg, hashlen, hash,
2659 mgf1_hash_id,
2660 MBEDTLS_RSA_SALT_LEN_ANY,
2661 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002662
2663}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002667/*
2668 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2669 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002670int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2671 mbedtls_md_type_t md_alg,
2672 unsigned int hashlen,
2673 const unsigned char *hash,
2674 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002675{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002676 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002677 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002678 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002681 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002683
2684 sig_len = ctx->len;
2685
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002686 /*
2687 * Prepare expected PKCS1 v1.5 encoding of hash.
2688 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2691 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002692 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2693 goto cleanup;
2694 }
2695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2697 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002698 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002700
2701 /*
2702 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2703 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002704
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 ret = mbedtls_rsa_public(ctx, sig, encoded);
2706 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002707 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002709
Simon Butcher02037452016-03-01 21:19:12 +00002710 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002711 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002712 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2715 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002716 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2717 goto cleanup;
2718 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002719
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002720cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002723 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002724 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002727 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002728 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002731}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
2734/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002735 * Do an RSA operation and check the message digest
2736 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002737int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2738 mbedtls_md_type_t md_alg,
2739 unsigned int hashlen,
2740 const unsigned char *hash,
2741 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002742{
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002744 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748#if defined(MBEDTLS_PKCS1_V15)
2749 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2751 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002752#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#if defined(MBEDTLS_PKCS1_V21)
2755 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2757 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002758#endif
2759
2760 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002762 }
2763}
2764
2765/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002766 * Copy the components of an RSA key
2767 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002768int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002769{
Janos Follath24eed8d2019-11-22 13:21:35 +00002770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002771
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002772 dst->len = src->len;
2773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2775 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2778 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2779 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002780
2781#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2783 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2784 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2785 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2786 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002787#endif
2788
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002790
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2792 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002793
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002794 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002795 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002796
2797cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 if (ret != 0) {
2799 mbedtls_rsa_free(dst);
2800 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002803}
2804
2805/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 * Free the components of an RSA key
2807 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002808void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002809{
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002811 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 mbedtls_mpi_free(&ctx->Vi);
2815 mbedtls_mpi_free(&ctx->Vf);
2816 mbedtls_mpi_free(&ctx->RN);
2817 mbedtls_mpi_free(&ctx->D);
2818 mbedtls_mpi_free(&ctx->Q);
2819 mbedtls_mpi_free(&ctx->P);
2820 mbedtls_mpi_free(&ctx->E);
2821 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002822
Hanno Becker33c30a02017-08-23 07:00:22 +01002823#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 mbedtls_mpi_free(&ctx->RQ);
2825 mbedtls_mpi_free(&ctx->RP);
2826 mbedtls_mpi_free(&ctx->QP);
2827 mbedtls_mpi_free(&ctx->DQ);
2828 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002829#endif /* MBEDTLS_RSA_NO_CRT */
2830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002832 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 if (ctx->ver != 0) {
2834 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002835 ctx->ver = 0;
2836 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002837#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002838}
2839
Hanno Beckerab377312017-08-23 16:24:51 +01002840#endif /* !MBEDTLS_RSA_ALT */
2841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
2845/*
2846 * Example RSA-1024 keypair, for test purposes
2847 */
2848#define KEY_LEN 128
2849
2850#define RSA_N "9292758453063D803DD603D5E777D788" \
2851 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2852 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2853 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2854 "93A89813FBF3C4F8066D2D800F7C38A8" \
2855 "1AE31942917403FF4946B0A83D3D3E05" \
2856 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2857 "5E94BB77B07507233A0BC7BAC8F90F79"
2858
2859#define RSA_E "10001"
2860
2861#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2862 "66CA472BC44D253102F8B4A9D3BFA750" \
2863 "91386C0077937FE33FA3252D28855837" \
2864 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2865 "DF79C5CE07EE72C7F123142198164234" \
2866 "CABB724CF78B8173B9F880FC86322407" \
2867 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2868 "071513A1E85B5DFA031F21ECAE91A34D"
2869
2870#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2871 "2C01CAD19EA484A87EA4377637E75500" \
2872 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2873 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2874
2875#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2876 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2877 "910E4168387E3C30AA1E00C339A79508" \
2878 "8452DD96A9A5EA5D9DCA68DA636032AF"
2879
Paul Bakker5121ce52009-01-03 21:22:43 +00002880#define PT_LEN 24
2881#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2882 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002885static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002886{
gufe44c2620da2020-08-03 17:56:50 +02002887#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002888 size_t i;
2889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002891 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 }
Paul Bakker545570e2010-07-18 09:00:25 +00002893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002895 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002897#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002899 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002903#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002906}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002908
Paul Bakker5121ce52009-01-03 21:22:43 +00002909/*
2910 * Checkup routine
2911 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002912int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002913{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002914 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002916 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002918 unsigned char rsa_plaintext[PT_LEN];
2919 unsigned char rsa_decrypted[PT_LEN];
2920 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002921#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002922 unsigned char sha1sum[20];
2923#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
Hanno Becker3a701162017-08-22 13:52:43 +01002925 mbedtls_mpi K;
2926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 mbedtls_mpi_init(&K);
2928 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002929
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2931 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2932 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2933 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2934 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2935 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2936 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2937 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2938 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2939 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 if (verbose != 0) {
2944 mbedtls_printf(" RSA key validation: ");
2945 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2948 mbedtls_rsa_check_privkey(&rsa) != 0) {
2949 if (verbose != 0) {
2950 mbedtls_printf("failed\n");
2951 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002952
Hanno Becker5bc87292017-05-03 15:09:31 +01002953 ret = 1;
2954 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002955 }
2956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (verbose != 0) {
2958 mbedtls_printf("passed\n PKCS#1 encryption : ");
2959 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2964 PT_LEN, rsa_plaintext,
2965 rsa_ciphertext) != 0) {
2966 if (verbose != 0) {
2967 mbedtls_printf("failed\n");
2968 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
Hanno Becker5bc87292017-05-03 15:09:31 +01002970 ret = 1;
2971 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002972 }
2973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 if (verbose != 0) {
2975 mbedtls_printf("passed\n PKCS#1 decryption : ");
2976 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002977
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2979 &len, rsa_ciphertext, rsa_decrypted,
2980 sizeof(rsa_decrypted)) != 0) {
2981 if (verbose != 0) {
2982 mbedtls_printf("failed\n");
2983 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
Hanno Becker5bc87292017-05-03 15:09:31 +01002985 ret = 1;
2986 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 }
2988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2990 if (verbose != 0) {
2991 mbedtls_printf("failed\n");
2992 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002993
Hanno Becker5bc87292017-05-03 15:09:31 +01002994 ret = 1;
2995 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002996 }
2997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 if (verbose != 0) {
2999 mbedtls_printf("passed\n");
3000 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003001
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003002#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 if (verbose != 0) {
3004 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01003005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01003007 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
3008 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 if (verbose != 0) {
3010 mbedtls_printf("failed\n");
3011 }
3012
3013 return 1;
3014 }
3015
3016 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
3017 MBEDTLS_MD_SHA1, 20,
3018 sha1sum, rsa_ciphertext) != 0) {
3019 if (verbose != 0) {
3020 mbedtls_printf("failed\n");
3021 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003022
Hanno Becker5bc87292017-05-03 15:09:31 +01003023 ret = 1;
3024 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 }
3026
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 if (verbose != 0) {
3028 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
3029 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
3032 sha1sum, rsa_ciphertext) != 0) {
3033 if (verbose != 0) {
3034 mbedtls_printf("failed\n");
3035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
Hanno Becker5bc87292017-05-03 15:09:31 +01003037 ret = 1;
3038 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003039 }
3040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 if (verbose != 0) {
3042 mbedtls_printf("passed\n");
3043 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003044#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 if (verbose != 0) {
3047 mbedtls_printf("\n");
3048 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003049
Paul Bakker3d8fb632014-04-17 12:42:41 +02003050cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 mbedtls_mpi_free(&K);
3052 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02003054 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003057}
3058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061#endif /* MBEDTLS_RSA_C */