blob: 32a26500ed9f894582eefeea496785e656528798 [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"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050034#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000035#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020036#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020037#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020038#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000039
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
gufe44c2620da2020-08-03 17:56:50 +020042#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000043#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010047
Dave Rodgman19e8cd02023-05-09 11:10:21 +010048
49#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
50
51/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
52 * operation (EME-PKCS1-v1_5 decoding).
53 *
54 * \note The return value from this function is a sensitive value
55 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
56 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
57 * is often a situation that an attacker can provoke and leaking which
58 * one is the result is precisely the information the attacker wants.
59 *
60 * \param input The input buffer which is the payload inside PKCS#1v1.5
61 * encryption padding, called the "encoded message EM"
62 * by the terminology.
63 * \param ilen The length of the payload in the \p input buffer.
64 * \param output The buffer for the payload, called "message M" by the
65 * PKCS#1 terminology. This must be a writable buffer of
66 * length \p output_max_len bytes.
67 * \param olen The address at which to store the length of
68 * the payload. This must not be \c NULL.
69 * \param output_max_len The length in bytes of the output buffer \p output.
70 *
71 * \return \c 0 on success.
72 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
73 * The output buffer is too small for the unpadded payload.
74 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
75 * The input doesn't contain properly formatted padding.
76 */
77static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
78 size_t ilen,
79 unsigned char *output,
80 size_t output_max_len,
81 size_t *olen)
82{
83 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
84 size_t i, plaintext_max_size;
85
86 /* The following variables take sensitive values: their value must
87 * not leak into the observable behavior of the function other than
88 * the designated outputs (output, olen, return value). Otherwise
89 * this would open the execution of the function to
90 * side-channel-based variants of the Bleichenbacher padding oracle
91 * attack. Potential side channels include overall timing, memory
92 * access patterns (especially visible to an adversary who has access
93 * to a shared memory cache), and branches (especially visible to
94 * an adversary who has access to a shared code cache or to a shared
95 * branch predictor). */
96 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +010097 mbedtls_ct_condition_t bad;
98 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +010099 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100100 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100101
102 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
103 : output_max_len;
104
105 /* Check and get padding length in constant time and constant
106 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100107 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100108
109
110 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
111 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100112 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100113
114 /* Read the whole buffer. Set pad_done to nonzero if we find
115 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100116 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100117 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100118 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100119 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100120 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100121 }
122
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100123 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100124 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100125
126 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100127 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100128
129 /* If the padding is valid, set plaintext_size to the number of
130 * remaining bytes after stripping the padding. If the padding
131 * is invalid, avoid leaking this fact through the size of the
132 * output: use the maximum message size that fits in the output
133 * buffer. Do it without branches to avoid leaking the padding
134 * validity through timing. RSA keys are small enough that all the
135 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100136 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100137 bad, (unsigned) plaintext_max_size,
138 (unsigned) (ilen - pad_count - 3));
139
140 /* Set output_too_large to 0 if the plaintext fits in the output
141 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100142 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100143 plaintext_max_size);
144
145 /* Set ret without branches to avoid timing attacks. Return:
146 * - INVALID_PADDING if the padding is bad (bad != 0).
147 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
148 * plaintext does not fit in the output buffer.
149 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100150 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100151 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100152 MBEDTLS_ERR_RSA_INVALID_PADDING,
153 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100154 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100155
156 /* If the padding is bad or the plaintext is too large, zero the
157 * data that we're about to copy to the output buffer.
158 * We need to copy the same amount of data
159 * from the same buffer whether the padding is good or not to
160 * avoid leaking the padding validity through overall timing or
161 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100162 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100163
164 /* If the plaintext is too large, truncate it to the buffer size.
165 * Copy anyway to avoid revealing the length through timing, because
166 * revealing the length is as bad as revealing the padding validity
167 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100168 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100169 (unsigned) plaintext_max_size,
170 (unsigned) plaintext_size);
171
172 /* Move the plaintext to the leftmost position where it can start in
173 * the working buffer, i.e. make it start plaintext_max_size from
174 * the end of the buffer. Do this with a memory access trace that
175 * does not depend on the plaintext size. After this move, the
176 * starting location of the plaintext is no longer sensitive
177 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100178 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
179 plaintext_max_size,
180 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100181
182 /* Finally copy the decrypted plaintext plus trailing zeros into the output
183 * buffer. If output_max_len is 0, then output may be an invalid pointer
184 * and the result of memcpy() would be undefined; prevent undefined
185 * behavior making sure to depend only on output_max_len (the size of the
186 * user-provided output buffer), which is independent from plaintext
187 * length, validity of padding, success of the decryption, and other
188 * secrets. */
189 if (output_max_len != 0) {
190 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
191 }
192
193 /* Report the amount of data we copied to the output buffer. In case
194 * of errors (bad padding or output too large), the value of *olen
195 * when this function returns is not specified. Making it equivalent
196 * to the good case limits the risks of leaking the padding validity. */
197 *olen = plaintext_size;
198
199 return ret;
200}
201
202#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
203
Hanno Beckera565f542017-10-11 11:00:19 +0100204#if !defined(MBEDTLS_RSA_ALT)
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
207 const mbedtls_mpi *N,
208 const mbedtls_mpi *P, const mbedtls_mpi *Q,
209 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100210{
Janos Follath24eed8d2019-11-22 13:21:35 +0000211 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
214 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
215 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
216 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
217 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
218 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100219 }
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if (N != NULL) {
222 ctx->len = mbedtls_mpi_size(&ctx->N);
223 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100226}
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
229 unsigned char const *N, size_t N_len,
230 unsigned char const *P, size_t P_len,
231 unsigned char const *Q, size_t Q_len,
232 unsigned char const *D, size_t D_len,
233 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100234{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000235 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (N != NULL) {
238 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
239 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100240 }
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (P != NULL) {
243 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
244 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if (Q != NULL) {
247 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
248 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (D != NULL) {
251 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
252 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (E != NULL) {
255 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
256 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100257
258cleanup:
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (ret != 0) {
261 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
262 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100265}
266
Hanno Becker705fc682017-10-10 17:57:02 +0100267/*
268 * Checks whether the context fields are set in such a way
269 * that the RSA primitives will be able to execute without error.
270 * It does *not* make guarantees for consistency of the parameters.
271 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
273 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100274{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100275#if !defined(MBEDTLS_RSA_NO_CRT)
276 /* blinding_needed is only used for NO_CRT to decide whether
277 * P,Q need to be present or not. */
278 ((void) blinding_needed);
279#endif
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
282 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
283 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000284 }
Hanno Becker705fc682017-10-10 17:57:02 +0100285
286 /*
287 * 1. Modular exponentiation needs positive, odd moduli.
288 */
289
290 /* Modular exponentiation wrt. N is always used for
291 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
293 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
294 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100295 }
296
297#if !defined(MBEDTLS_RSA_NO_CRT)
298 /* Modular exponentiation for P and Q is only
299 * used for private key operations and if CRT
300 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (is_priv &&
302 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
303 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
304 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
305 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
306 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100307 }
308#endif /* !MBEDTLS_RSA_NO_CRT */
309
310 /*
311 * 2. Exponents must be positive
312 */
313
314 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
316 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
317 }
Hanno Becker705fc682017-10-10 17:57:02 +0100318
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100319#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100320 /* For private key operations, use D or DP & DQ
321 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
323 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
324 }
Hanno Becker705fc682017-10-10 17:57:02 +0100325#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (is_priv &&
327 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
328 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
329 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100330 }
331#endif /* MBEDTLS_RSA_NO_CRT */
332
333 /* Blinding shouldn't make exponents negative either,
334 * so check that P, Q >= 1 if that hasn't yet been
335 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100336#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (is_priv && blinding_needed &&
338 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
339 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
340 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100341 }
342#endif
343
344 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100345 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100346#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (is_priv &&
348 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
349 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100350 }
351#endif
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100354}
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100357{
358 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500359 int have_N, have_P, have_Q, have_D, have_E;
360#if !defined(MBEDTLS_RSA_NO_CRT)
361 int have_DP, have_DQ, have_QP;
362#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500363 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
366 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
367 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
368 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
369 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500370
371#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
373 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
374 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500375#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100376
Hanno Becker617c1ae2017-08-23 14:11:24 +0100377 /*
378 * Check whether provided parameters are enough
379 * to deduce all others. The following incomplete
380 * parameter sets for private keys are supported:
381 *
382 * (1) P, Q missing.
383 * (2) D and potentially N missing.
384 *
385 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100386
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500387 n_missing = have_P && have_Q && have_D && have_E;
388 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
389 d_missing = have_P && have_Q && !have_D && have_E;
390 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100391
392 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500393 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (!is_priv && !is_pub) {
396 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
397 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100398
399 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100400 * Step 1: Deduce N if P, Q are provided.
401 */
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (!have_N && have_P && have_Q) {
404 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
405 &ctx->Q)) != 0) {
406 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100407 }
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100410 }
411
412 /*
413 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100414 */
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (pq_missing) {
417 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
418 &ctx->P, &ctx->Q);
419 if (ret != 0) {
420 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
421 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 } else if (d_missing) {
424 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
425 &ctx->Q,
426 &ctx->E,
427 &ctx->D)) != 0) {
428 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100429 }
430 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100431
Hanno Becker617c1ae2017-08-23 14:11:24 +0100432 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100433 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100434 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100435 */
436
Hanno Becker23344b52017-08-23 07:43:27 +0100437#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (is_priv && !(have_DP && have_DQ && have_QP)) {
439 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
440 &ctx->DP, &ctx->DQ, &ctx->QP);
441 if (ret != 0) {
442 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
443 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444 }
Hanno Becker23344b52017-08-23 07:43:27 +0100445#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100446
447 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100448 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100449 */
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100452}
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
455 unsigned char *N, size_t N_len,
456 unsigned char *P, size_t P_len,
457 unsigned char *Q, size_t Q_len,
458 unsigned char *D, size_t D_len,
459 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100460{
461 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500462 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100463
464 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500465 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
467 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
468 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
469 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
470 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100473 /* If we're trying to export private parameters for a public key,
474 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (P != NULL || Q != NULL || D != NULL) {
476 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
477 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100478
479 }
480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (N != NULL) {
482 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
483 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (P != NULL) {
486 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
487 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (Q != NULL) {
490 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
491 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (D != NULL) {
494 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
495 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (E != NULL) {
498 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
499 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100500
501cleanup:
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100504}
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
507 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
508 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100509{
Janos Follath24eed8d2019-11-22 13:21:35 +0000510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500511 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100512
513 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500514 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
516 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
517 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
518 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
519 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100522 /* If we're trying to export private parameters for a public key,
523 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (P != NULL || Q != NULL || D != NULL) {
525 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
526 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100527
528 }
529
530 /* Export all requested core parameters. */
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
533 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
534 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
535 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
536 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
537 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100541}
542
543/*
544 * Export CRT parameters
545 * This must also be implemented if CRT is not used, for being able to
546 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
547 * can be used in this case.
548 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
550 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100551{
Janos Follath24eed8d2019-11-22 13:21:35 +0000552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500553 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100554
555 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500556 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
558 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
559 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
560 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
561 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (!is_priv) {
564 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
565 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100566
Hanno Beckerdc95c892017-08-23 06:57:02 +0100567#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100568 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
570 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
571 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
572 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100573 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100574#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
576 DP, DQ, QP)) != 0) {
577 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100578 }
579#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100582}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100583
Paul Bakker5121ce52009-01-03 21:22:43 +0000584/*
585 * Initialize an RSA context
586 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100587void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000588{
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Ronald Cronc1905a12021-06-05 11:11:14 +0200591 ctx->padding = MBEDTLS_RSA_PKCS_V15;
592 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100595 /* Set ctx->ver to nonzero to indicate that the mutex has been
596 * initialized and will need to be freed. */
597 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200599#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000600}
601
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100602/*
603 * Set padding for an existing RSA context
604 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100605int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
606 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100607{
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200609#if defined(MBEDTLS_PKCS1_V15)
610 case MBEDTLS_RSA_PKCS_V15:
611 break;
612#endif
613
614#if defined(MBEDTLS_PKCS1_V21)
615 case MBEDTLS_RSA_PKCS_V21:
616 break;
617#endif
618 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200620 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200621
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200622#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
624 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200625 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200626 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return MBEDTLS_ERR_RSA_INVALID_PADDING;
628 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200629 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200630#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500631
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100632 ctx->padding = padding;
633 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100636}
637
Hanno Becker617c1ae2017-08-23 14:11:24 +0100638/*
Yanray Wang83548b52023-03-15 16:46:34 +0800639 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800640 */
641int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
642{
Yanray Wang644b9012023-03-15 16:50:31 +0800643 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +0800644}
645
646/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800647 * Get hash identifier of mbedtls_md_type_t type
648 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800649int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +0800650{
Yanray Wang644b9012023-03-15 16:50:31 +0800651 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +0800652}
653
654/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100655 * Get length in bytes of RSA modulus
656 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100657size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100658{
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100660}
661
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000664
665/*
666 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800667 *
668 * This generation method follows the RSA key pair generation procedure of
669 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
672 int (*f_rng)(void *, unsigned char *, size_t),
673 void *p_rng,
674 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000675{
Janos Follath24eed8d2019-11-22 13:21:35 +0000676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800677 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100678 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
Janos Follathb8fc1b02018-09-03 15:37:01 +0100680 /*
681 * If the modulus is 1024 bit long or shorter, then the security strength of
682 * the RSA algorithm is less than or equal to 80 bits and therefore an error
683 * rate of 2^-80 is sufficient.
684 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100686 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 mbedtls_mpi_init(&H);
690 mbedtls_mpi_init(&G);
691 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000693 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100694 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
695 goto cleanup;
696 }
697
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000698 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
700 goto cleanup;
701 }
702
703 /*
704 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800705 * 1. |P-Q| > 2^( nbits / 2 - 100 )
706 * 2. GCD( E, (P-1)*(Q-1) ) == 1
707 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 do {
712 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
713 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
716 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800718 /* 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 +0100719 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
720 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800724 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (H.s < 0) {
726 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
727 }
Janos Follathef441782016-09-21 13:18:12 +0100728
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100729 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
731 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
732 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800733
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800734 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
736 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800737 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800739
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800740 /* 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 +0100741 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
742 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
743 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 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 -0800746 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800748
749 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100752 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
754 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100759
Jethro Beekman97f95c92018-02-13 15:50:36 -0800760#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 * DP = D mod (P - 1)
763 * DQ = D mod (Q - 1)
764 * QP = Q^-1 mod P
765 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
767 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100768#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Hanno Becker83aad1f2017-08-23 06:45:10 +0100770 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
773cleanup:
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 mbedtls_mpi_free(&H);
776 mbedtls_mpi_free(&G);
777 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (ret != 0) {
780 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((-ret & ~0x7f) == 0) {
783 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
784 }
785 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 }
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000789}
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
793/*
794 * Check a public RSA key
795 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000797{
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
799 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100800 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
803 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100804 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
807 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
808 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
809 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
810 }
811
812 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000813}
814
815/*
Hanno Becker705fc682017-10-10 17:57:02 +0100816 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000817 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000819{
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
821 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
822 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 }
Paul Bakker48377d92013-08-30 12:06:24 +0200824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
826 &ctx->D, &ctx->E, NULL, NULL) != 0) {
827 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000828 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000829
Hanno Beckerb269a852017-08-25 08:03:21 +0100830#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
832 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
833 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100834 }
835#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000838}
839
840/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100841 * Check if contexts holding a public and private key match
842 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
844 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100845{
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
847 mbedtls_rsa_check_privkey(prv) != 0) {
848 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100849 }
850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
852 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
853 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100854 }
855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100857}
858
859/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 * Do an RSA public key operation
861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
863 const unsigned char *input,
864 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000865{
Janos Follath24eed8d2019-11-22 13:21:35 +0000866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000867 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
871 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
872 }
Hanno Becker705fc682017-10-10 17:57:02 +0100873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000875
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200876#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
878 return ret;
879 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200880#endif
881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200885 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
886 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 }
888
889 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
891 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
893cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
896 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
897 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100898#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (ret != 0) {
903 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
904 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000907}
908
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200909/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200910 * Generate or update blinding values, see section 10 of:
911 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200912 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200913 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200914 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
916 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200917{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200918 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200919 mbedtls_mpi R;
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200924 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
926 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
927 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
928 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200929
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200930 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200931 }
932
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200933 /* Unblinding value: Vf = random number, invertible mod N */
934 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200936 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
937 goto cleanup;
938 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 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 +0200941
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200942 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
944 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
945 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200946
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200947 /* At this point, Vi is invertible mod N if and only if both Vf and R
948 * are invertible mod N. If one of them isn't, we don't need to know
949 * which one, we just loop and choose new values for both of them.
950 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
952 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200953 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500957
958 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
960 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200961
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200962 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200963 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 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 +0200965
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200966
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200967cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200971}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200972
Paul Bakker5121ce52009-01-03 21:22:43 +0000973/*
Janos Follathd6b09652023-11-21 09:33:54 +0000974 * Unblind
975 * T = T * Vf mod N
976 */
977int rsa_unblind(mbedtls_mpi* T, mbedtls_mpi* Vf, mbedtls_mpi* N)
978{
979 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
980 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
981 const size_t nlimbs = N->n;
982 const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
983 mbedtls_mpi RR, M_T;
984
985 mbedtls_mpi_init(&RR);
986 mbedtls_mpi_init(&M_T);
987
988 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
989 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
990
991 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
992 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
993
994 // T = T * R mod N
995 mbedtls_mpi_core_to_mont_rep(T->p, T->p, N->p, nlimbs, mm, RR.p, M_T.p);
996 // T = T * Vf mod N
997 mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
998
999cleanup:
1000
1001 mbedtls_mpi_free(&RR);
1002 mbedtls_mpi_free(&M_T);
1003
1004 return ret;
1005}
1006
1007/*
Janos Follathe81102e2017-03-22 13:38:28 +00001008 * Exponent blinding supposed to prevent side-channel attacks using multiple
1009 * traces of measurements to recover the RSA key. The more collisions are there,
1010 * the more bits of the key can be recovered. See [3].
1011 *
1012 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001013 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001014 *
1015 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001016 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001017 *
1018 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1019 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1020 * Thus in this sense with 28 byte blinding the security is not reduced by
1021 * side-channel attacks like the one in [3])
1022 *
1023 * This countermeasure does not help if the key recovery is possible with a
1024 * single trace.
1025 */
1026#define RSA_EXPONENT_BLINDING 28
1027
1028/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 * Do an RSA private key operation
1030 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001031int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1032 int (*f_rng)(void *, unsigned char *, size_t),
1033 void *p_rng,
1034 const unsigned char *input,
1035 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001036{
Janos Follath24eed8d2019-11-22 13:21:35 +00001037 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001038 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001039
1040 /* Temporary holding the result */
1041 mbedtls_mpi T;
1042
1043 /* Temporaries holding P-1, Q-1 and the
1044 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001045 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001046
1047#if !defined(MBEDTLS_RSA_NO_CRT)
1048 /* Temporaries holding the results mod p resp. mod q. */
1049 mbedtls_mpi TP, TQ;
1050
1051 /* Temporaries holding the blinded exponents for
1052 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001053 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001054
1055 /* Pointers to actual exponents to be used - either the unblinded
1056 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +00001057 mbedtls_mpi *DP = &ctx->DP;
1058 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +01001059#else
1060 /* Temporary holding the blinded exponent (if used). */
1061 mbedtls_mpi D_blind;
1062
1063 /* Pointer to actual exponent to be used - either the unblinded
1064 * or the blinded one, depending on the presence of a PRNG. */
1065 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +01001066#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001067
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001068 /* Temporaries holding the initial input and the double
1069 * checked result; should be the same in the end. */
1070 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (f_rng == NULL) {
1073 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1074 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if (rsa_check_context(ctx, 1 /* private key checks */,
1077 1 /* blinding on */) != 0) {
1078 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001079 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001080
Hanno Becker06811ce2017-05-03 15:10:34 +01001081#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1083 return ret;
1084 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001085#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001086
Hanno Becker06811ce2017-05-03 15:10:34 +01001087 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 mbedtls_mpi_init(&P1);
1091 mbedtls_mpi_init(&Q1);
1092 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001093
Janos Follathe81102e2017-03-22 13:38:28 +00001094#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001096#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 mbedtls_mpi_init(&DP_blind);
1098 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001099#endif
1100
Hanno Becker06811ce2017-05-03 15:10:34 +01001101#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001103#endif
1104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 mbedtls_mpi_init(&I);
1106 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001107
1108 /* End of MPI initialization */
1109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1111 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001112 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1113 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 }
1115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +01001117
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001118 /*
1119 * Blinding
1120 * T = T * Vi mod N
1121 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1123 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1124 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001125
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001126 /*
1127 * Exponent blinding
1128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1130 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001131
Janos Follathf9203b42017-03-22 15:13:15 +00001132#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001133 /*
1134 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1135 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1137 f_rng, p_rng));
1138 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1139 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1140 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +00001141
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001142 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001143#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001144 /*
1145 * DP_blind = ( P - 1 ) * R + DP
1146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1148 f_rng, p_rng));
1149 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1150 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1151 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001152
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001153 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001154
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001155 /*
1156 * DQ_blind = ( Q - 1 ) * R + DQ
1157 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1159 f_rng, p_rng));
1160 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1161 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1162 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001163
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001164 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001165#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001169#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001170 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001171 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001173 * TP = input ^ dP mod P
1174 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1178 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001179
1180 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001181 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1184 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1185 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001186
1187 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001188 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1191 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001193
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001194 /*
1195 * Unblind
1196 * T = T * Vf mod N
1197 */
Janos Follathd6b09652023-11-21 09:33:54 +00001198 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
Hanno Becker2dec5e82017-10-03 07:49:52 +01001200 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1202 &ctx->N, &ctx->RN));
1203 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001204 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1205 goto cleanup;
1206 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001207
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001210
1211cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1214 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1215 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001216#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 mbedtls_mpi_free(&P1);
1219 mbedtls_mpi_free(&Q1);
1220 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001221
Janos Follathe81102e2017-03-22 13:38:28 +00001222#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001224#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 mbedtls_mpi_free(&DP_blind);
1226 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001227#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001230
1231#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001233#endif
1234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_mpi_free(&C);
1236 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (ret != 0 && ret >= -0x007f) {
1239 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001243}
1244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001246/**
1247 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1248 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001249 * \param dst buffer to mask
1250 * \param dlen length of destination buffer
1251 * \param src source of the mask generation
1252 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001253 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001255static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1256 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001257{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001258 unsigned char counter[4];
1259 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001260 unsigned int hlen;
1261 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001262 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001263 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001264 const mbedtls_md_info_t *md_info;
1265 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 mbedtls_md_init(&md_ctx);
1268 md_info = mbedtls_md_info_from_type(md_alg);
1269 if (md_info == NULL) {
1270 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1271 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 mbedtls_md_init(&md_ctx);
1274 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001275 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 memset(mask, 0, sizeof(mask));
1281 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001282
Simon Butcher02037452016-03-01 21:19:12 +00001283 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001284 p = dst;
1285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001287 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001289 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001293 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 }
1295 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001296 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 }
1298 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001299 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 }
1301 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001302 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001306 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001308
1309 counter[3]++;
1310
1311 dlen -= use_len;
1312 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001313
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001314exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001319}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001320
1321/**
1322 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1323 *
1324 * \param hash the input hash
1325 * \param hlen length of the input hash
1326 * \param salt the input salt
1327 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001328 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001329 * \param md_alg message digest to use
1330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331static int hash_mprime(const unsigned char *hash, size_t hlen,
1332 const unsigned char *salt, size_t slen,
1333 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001334{
1335 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001336
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001337 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1341 if (md_info == NULL) {
1342 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1343 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 mbedtls_md_init(&md_ctx);
1346 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001347 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 }
1349 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001350 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 }
1352 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001353 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 }
1355 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001356 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 }
1358 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001359 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 }
1361 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001362 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001364
1365exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001369}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001370
1371/**
1372 * Compute a hash.
1373 *
1374 * \param md_alg algorithm to use
1375 * \param input input message to hash
1376 * \param ilen input length
1377 * \param output the output buffer - must be large enough for \p md_alg
1378 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001379static int compute_hash(mbedtls_md_type_t md_alg,
1380 const unsigned char *input, size_t ilen,
1381 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001382{
1383 const mbedtls_md_info_t *md_info;
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 md_info = mbedtls_md_info_from_type(md_alg);
1386 if (md_info == NULL) {
1387 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1388 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001395/*
1396 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1399 int (*f_rng)(void *, unsigned char *, size_t),
1400 void *p_rng,
1401 const unsigned char *label, size_t label_len,
1402 size_t ilen,
1403 const unsigned char *input,
1404 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001405{
1406 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001408 unsigned char *p = output;
1409 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (f_rng == NULL) {
1412 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1413 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001414
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001415 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 if (hlen == 0) {
1417 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1418 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001419
1420 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001421
Simon Butcher02037452016-03-01 21:19:12 +00001422 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1424 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1425 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001428
1429 *p++ = 0;
1430
Simon Butcher02037452016-03-01 21:19:12 +00001431 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1433 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1434 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001435
1436 p += hlen;
1437
Simon Butcher02037452016-03-01 21:19:12 +00001438 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1440 if (ret != 0) {
1441 return ret;
1442 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001443 p += hlen;
1444 p += olen - 2 * hlen - 2 - ilen;
1445 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 if (ilen != 0) {
1447 memcpy(p, input, ilen);
1448 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001449
Simon Butcher02037452016-03-01 21:19:12 +00001450 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001452 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 return ret;
1454 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001455
Simon Butcher02037452016-03-01 21:19:12 +00001456 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001458 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 return ret;
1460 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001463}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001467/*
1468 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1471 int (*f_rng)(void *, unsigned char *, size_t),
1472 void *p_rng, size_t ilen,
1473 const unsigned char *input,
1474 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001475{
1476 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001478 unsigned char *p = output;
1479
Paul Bakkerb3869132013-02-28 17:21:01 +01001480 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001481
Simon Butcher02037452016-03-01 21:19:12 +00001482 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (ilen + 11 < ilen || olen < ilen + 11) {
1484 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1485 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001486
1487 nb_pad = olen - 3 - ilen;
1488
1489 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (f_rng == NULL) {
1492 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1493 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001494
1495 *p++ = MBEDTLS_RSA_CRYPT;
1496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001498 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001499
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001500 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 ret = f_rng(p_rng, p, 1);
1502 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001503
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001504 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if (rng_dl == 0 || ret != 0) {
1506 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1507 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001508
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001509 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001510 }
1511
1512 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (ilen != 0) {
1514 memcpy(p, input, ilen);
1515 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001518}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001520
Paul Bakker5121ce52009-01-03 21:22:43 +00001521/*
1522 * Add the message padding, then do an RSA operation
1523 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1525 int (*f_rng)(void *, unsigned char *, size_t),
1526 void *p_rng,
1527 size_t ilen,
1528 const unsigned char *input,
1529 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001530{
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_PKCS1_V15)
1533 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1535 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001536#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#if defined(MBEDTLS_PKCS1_V21)
1539 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1541 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001542#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001543
1544 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001546 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001547}
1548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001550/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001551 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1554 int (*f_rng)(void *, unsigned char *, size_t),
1555 void *p_rng,
1556 const unsigned char *label, size_t label_len,
1557 size_t *olen,
1558 const unsigned char *input,
1559 unsigned char *output,
1560 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001561{
Janos Follath24eed8d2019-11-22 13:21:35 +00001562 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001563 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001564 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001565 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001567 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001568 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001569
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001570 /*
1571 * Parameters sanity checks
1572 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1574 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1575 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001576
1577 ilen = ctx->len;
1578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (ilen < 16 || ilen > sizeof(buf)) {
1580 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001582
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001583 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if (hlen == 0) {
1585 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1586 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001587
Janos Follathc17cda12016-02-11 11:08:18 +00001588 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 if (2 * hlen + 2 > ilen) {
1590 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1591 }
Janos Follathc17cda12016-02-11 11:08:18 +00001592
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001593 /*
1594 * RSA operation
1595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001599 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001601
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001602 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001603 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001604 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001605 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001607 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 /* DB: Apply dbMask to maskedDB */
1609 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001610 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001611 goto cleanup;
1612 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001613
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001614 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1616 label, label_len, lhash);
1617 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001618 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001620
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001621 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001622 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001623 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001624 p = buf;
1625
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001626 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001627
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001628 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001629
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001630 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001631 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001632 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001633
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001634 /* Get zero-padding len, but always read till end of buffer
1635 * (minus one, for the 01 byte) */
1636 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001637 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001639 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1640 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001641 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001642
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001643 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001644 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001645
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001646 /*
1647 * The only information "leaked" is whether the padding was correct or not
1648 * (eg, no data is copied if it was not correct). This meets the
1649 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1650 * the different error conditions.
1651 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001652 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001653 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1654 goto cleanup;
1655 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001658 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1659 goto cleanup;
1660 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001661
1662 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (*olen != 0) {
1664 memcpy(output, p, *olen);
1665 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001666 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001667
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001668cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 mbedtls_platform_zeroize(buf, sizeof(buf));
1670 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001673}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001677/*
1678 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1679 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001680int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1681 int (*f_rng)(void *, unsigned char *, size_t),
1682 void *p_rng,
1683 size_t *olen,
1684 const unsigned char *input,
1685 unsigned char *output,
1686 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001687{
1688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1689 size_t ilen;
1690 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1691
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001692 ilen = ctx->len;
1693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1695 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1696 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 if (ilen < 16 || ilen > sizeof(buf)) {
1699 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1700 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001705 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1709 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001710
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001711cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001715}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001717
1718/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001719 * Do an RSA operation, then remove the message padding
1720 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001721int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1722 int (*f_rng)(void *, unsigned char *, size_t),
1723 void *p_rng,
1724 size_t *olen,
1725 const unsigned char *input,
1726 unsigned char *output,
1727 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001728{
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#if defined(MBEDTLS_PKCS1_V15)
1731 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1733 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001734#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#if defined(MBEDTLS_PKCS1_V21)
1737 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1739 olen, input, output,
1740 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001741#endif
1742
1743 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001745 }
1746}
1747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001749static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1750 int (*f_rng)(void *, unsigned char *, size_t),
1751 void *p_rng,
1752 mbedtls_md_type_t md_alg,
1753 unsigned int hashlen,
1754 const unsigned char *hash,
1755 int saltlen,
1756 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001757{
1758 size_t olen;
1759 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001760 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001761 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001762 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001763 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001764
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001766 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1770 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1771 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (f_rng == NULL) {
1774 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1775 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001776
1777 olen = ctx->len;
1778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001780 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001781 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 if (exp_hashlen == 0) {
1783 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1784 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 if (hashlen != exp_hashlen) {
1787 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1788 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001789 }
1790
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001791 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 if (hlen == 0) {
1793 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1794 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1797 /* Calculate the largest possible salt length, up to the hash size.
1798 * Normally this is the hash length, which is the maximum salt length
1799 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1800 * enough room, use the maximum salt length that fits. The constraint is
1801 * that the hash length plus the salt length plus 2 bytes must be at most
1802 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1803 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001804 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 if (olen < hlen + min_slen + 2) {
1806 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1807 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001808 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001810 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 }
1812 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1813 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1814 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001815 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001816 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001819
Simon Butcher02037452016-03-01 21:19:12 +00001820 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001822 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001823 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001824
1825 /* Generate salt of length slen in place in the encoded message */
1826 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1828 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1829 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001830
Paul Bakkerb3869132013-02-28 17:21:01 +01001831 p += slen;
1832
Simon Butcher02037452016-03-01 21:19:12 +00001833 /* Generate H = Hash( M' ) */
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001834 ret = hash_mprime(hash, hashlen, salt, slen, p, (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 if (ret != 0) {
1836 return ret;
1837 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001838
Simon Butcher02037452016-03-01 21:19:12 +00001839 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001841 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001843
Simon Butcher02037452016-03-01 21:19:12 +00001844 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001846 (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (ret != 0) {
1848 return ret;
1849 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1852 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001853
1854 p += hlen;
1855 *p++ = 0xBC;
1856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001858}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001859
1860/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001861 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1862 * the option to pass in the salt length.
1863 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001864int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1865 int (*f_rng)(void *, unsigned char *, size_t),
1866 void *p_rng,
1867 mbedtls_md_type_t md_alg,
1868 unsigned int hashlen,
1869 const unsigned char *hash,
1870 int saltlen,
1871 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001872{
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1874 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001875}
1876
1877
1878/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001879 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1880 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001881int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1882 int (*f_rng)(void *, unsigned char *, size_t),
1883 void *p_rng,
1884 mbedtls_md_type_t md_alg,
1885 unsigned int hashlen,
1886 const unsigned char *hash,
1887 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001888{
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1890 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001891}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001895/*
1896 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1897 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001898
1899/* Construct a PKCS v1.5 encoding of a hashed message
1900 *
1901 * This is used both for signature generation and verification.
1902 *
1903 * Parameters:
1904 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001905 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001906 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001907 * - hash: Buffer containing the hashed message or the raw data.
1908 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001909 * - dst: Buffer to hold the encoded message.
1910 *
1911 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001912 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001913 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001914 *
1915 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001916static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1917 unsigned int hashlen,
1918 const unsigned char *hash,
1919 size_t dst_len,
1920 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001921{
1922 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001923 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001924 unsigned char *p = dst;
1925 const char *oid = NULL;
1926
1927 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001929 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 if (md_size == 0) {
1931 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1932 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1935 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1936 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (hashlen != md_size) {
1939 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1940 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001941
1942 /* Double-check that 8 + hashlen + oid_size can be used as a
1943 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001945 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 10 + hashlen + oid_size < 10 + hashlen) {
1947 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1948 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001949
1950 /*
1951 * Static bounds check:
1952 * - Need 10 bytes for five tag-length pairs.
1953 * (Insist on 1-byte length encodings to protect against variants of
1954 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1955 * - Need hashlen bytes for hash
1956 * - Need oid_size bytes for hash alg OID.
1957 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if (nb_pad < 10 + hashlen + oid_size) {
1959 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1960 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001961 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 } else {
1963 if (nb_pad < hashlen) {
1964 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1965 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001966
1967 nb_pad -= hashlen;
1968 }
1969
Hanno Becker2b2f8982017-09-27 17:10:03 +01001970 /* Need space for signature header and padding delimiter (3 bytes),
1971 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 if (nb_pad < 3 + 8) {
1973 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1974 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001975 nb_pad -= 3;
1976
1977 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001978 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001979
1980 /* Write signature header and padding */
1981 *p++ = 0;
1982 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001984 p += nb_pad;
1985 *p++ = 0;
1986
1987 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 if (md_alg == MBEDTLS_MD_NONE) {
1989 memcpy(p, hash, hashlen);
1990 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001991 }
1992
1993 /* Signing hashed data, add corresponding ASN.1 structure
1994 *
1995 * DigestInfo ::= SEQUENCE {
1996 * digestAlgorithm DigestAlgorithmIdentifier,
1997 * digest Digest }
1998 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1999 * Digest ::= OCTET STRING
2000 *
2001 * Schematic:
2002 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2003 * TAG-NULL + LEN [ NULL ] ]
2004 * TAG-OCTET + LEN [ HASH ] ]
2005 */
2006 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002008 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002010 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002011 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002013 p += oid_size;
2014 *p++ = MBEDTLS_ASN1_NULL;
2015 *p++ = 0x00;
2016 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002017 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002019 p += hashlen;
2020
2021 /* Just a sanity-check, should be automatic
2022 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 if (p != dst + dst_len) {
2024 mbedtls_platform_zeroize(dst, dst_len);
2025 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002026 }
2027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002029}
2030
Paul Bakkerb3869132013-02-28 17:21:01 +01002031/*
2032 * Do an RSA operation to sign the message digest
2033 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002034int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2035 int (*f_rng)(void *, unsigned char *, size_t),
2036 void *p_rng,
2037 mbedtls_md_type_t md_alg,
2038 unsigned int hashlen,
2039 const unsigned char *hash,
2040 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002041{
Janos Follath24eed8d2019-11-22 13:21:35 +00002042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002043 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002046 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2050 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2051 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002052
Hanno Beckerfdf38032017-09-06 12:35:55 +01002053 /*
2054 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2055 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2058 ctx->len, sig)) != 0) {
2059 return ret;
2060 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002061
Hanno Beckerfdf38032017-09-06 12:35:55 +01002062 /* Private key operation
2063 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002064 * In order to prevent Lenstra's attack, make the signature in a
2065 * temporary buffer and check it before returning it.
2066 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 sig_try = mbedtls_calloc(1, ctx->len);
2069 if (sig_try == NULL) {
2070 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002071 }
2072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 verif = mbedtls_calloc(1, ctx->len);
2074 if (verif == NULL) {
2075 mbedtls_free(sig_try);
2076 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2077 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2080 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2081
2082 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002083 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2084 goto cleanup;
2085 }
2086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002088
2089cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002090 mbedtls_zeroize_and_free(sig_try, ctx->len);
2091 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002092
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 if (ret != 0) {
2094 memset(sig, '!', ctx->len);
2095 }
2096 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002099
2100/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 * Do an RSA operation to sign the message digest
2102 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2104 int (*f_rng)(void *, unsigned char *, size_t),
2105 void *p_rng,
2106 mbedtls_md_type_t md_alg,
2107 unsigned int hashlen,
2108 const unsigned char *hash,
2109 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002110{
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002112 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#if defined(MBEDTLS_PKCS1_V15)
2117 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2119 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002120#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#if defined(MBEDTLS_PKCS1_V21)
2123 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2125 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002126#endif
2127
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002131}
2132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002134/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002135 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002137int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2138 mbedtls_md_type_t md_alg,
2139 unsigned int hashlen,
2140 const unsigned char *hash,
2141 mbedtls_md_type_t mgf1_hash_id,
2142 int expected_salt_len,
2143 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002144{
Janos Follath24eed8d2019-11-22 13:21:35 +00002145 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002146 size_t siglen;
2147 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002148 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002149 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002150 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002151 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002155 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002157
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 siglen = ctx->len;
2159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (siglen < 16 || siglen > sizeof(buf)) {
2161 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2162 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (ret != 0) {
2167 return ret;
2168 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
2170 p = buf;
2171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if (buf[siglen - 1] != 0xBC) {
2173 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002174 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (md_alg != MBEDTLS_MD_NONE) {
2177 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002178 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (exp_hashlen == 0) {
2180 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2181 }
2182
2183 if (hashlen != exp_hashlen) {
2184 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2185 }
2186 }
2187
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002188 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (hlen == 0) {
2190 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2191 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002192
Simon Butcher02037452016-03-01 21:19:12 +00002193 /*
2194 * Note: EMSA-PSS verification is over the length of N - 1 bits
2195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (buf[0] >> (8 - siglen * 8 + msb)) {
2199 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2200 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002201
Simon Butcher02037452016-03-01 21:19:12 +00002202 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002204 p++;
2205 siglen -= 1;
2206 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 if (siglen < hlen + 2) {
2209 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2210 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002211 hash_start = p + siglen - hlen - 1;
2212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2214 if (ret != 0) {
2215 return ret;
2216 }
Paul Bakker02303e82013-01-03 11:08:31 +01002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002221 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (*p++ != 0x01) {
2225 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2226 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002227
Gilles Peskine6a54b022017-10-17 19:02:13 +02002228 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2231 observed_salt_len != (size_t) expected_salt_len) {
2232 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002233 }
2234
Simon Butcher02037452016-03-01 21:19:12 +00002235 /*
2236 * Generate H = Hash( M' )
2237 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2239 result, mgf1_hash_id);
2240 if (ret != 0) {
2241 return ret;
2242 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 if (memcmp(hash_start, result, hlen) != 0) {
2245 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2246 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002249}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002250
2251/*
2252 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2253 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002254int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2255 mbedtls_md_type_t md_alg,
2256 unsigned int hashlen,
2257 const unsigned char *hash,
2258 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002259{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002260 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002262 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002267 : md_alg;
2268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2270 md_alg, hashlen, hash,
2271 mgf1_hash_id,
2272 MBEDTLS_RSA_SALT_LEN_ANY,
2273 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002274
2275}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002279/*
2280 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002282int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2283 mbedtls_md_type_t md_alg,
2284 unsigned int hashlen,
2285 const unsigned char *hash,
2286 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002287{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002288 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002289 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002290 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002293 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002295
2296 sig_len = ctx->len;
2297
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002298 /*
2299 * Prepare expected PKCS1 v1.5 encoding of hash.
2300 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2303 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002304 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2305 goto cleanup;
2306 }
2307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2309 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002310 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002312
2313 /*
2314 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2315 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 ret = mbedtls_rsa_public(ctx, sig, encoded);
2318 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002319 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002321
Simon Butcher02037452016-03-01 21:19:12 +00002322 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002323 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002324 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2327 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002328 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2329 goto cleanup;
2330 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002331
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002332cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002335 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002336 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002339 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002340 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002343}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002345
2346/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002347 * Do an RSA operation and check the message digest
2348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002349int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2350 mbedtls_md_type_t md_alg,
2351 unsigned int hashlen,
2352 const unsigned char *hash,
2353 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002354{
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002356 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360#if defined(MBEDTLS_PKCS1_V15)
2361 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2363 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002364#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366#if defined(MBEDTLS_PKCS1_V21)
2367 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2369 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002370#endif
2371
2372 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002374 }
2375}
2376
2377/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002378 * Copy the components of an RSA key
2379 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002380int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002381{
Janos Follath24eed8d2019-11-22 13:21:35 +00002382 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002383
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002384 dst->len = src->len;
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2387 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2390 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2391 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002392
2393#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2395 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2396 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2397 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2398 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002399#endif
2400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2404 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002405
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002406 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002407 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002408
2409cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (ret != 0) {
2411 mbedtls_rsa_free(dst);
2412 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002415}
2416
2417/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 * Free the components of an RSA key
2419 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002421{
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002423 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 mbedtls_mpi_free(&ctx->Vi);
2427 mbedtls_mpi_free(&ctx->Vf);
2428 mbedtls_mpi_free(&ctx->RN);
2429 mbedtls_mpi_free(&ctx->D);
2430 mbedtls_mpi_free(&ctx->Q);
2431 mbedtls_mpi_free(&ctx->P);
2432 mbedtls_mpi_free(&ctx->E);
2433 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002434
Hanno Becker33c30a02017-08-23 07:00:22 +01002435#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 mbedtls_mpi_free(&ctx->RQ);
2437 mbedtls_mpi_free(&ctx->RP);
2438 mbedtls_mpi_free(&ctx->QP);
2439 mbedtls_mpi_free(&ctx->DQ);
2440 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002441#endif /* MBEDTLS_RSA_NO_CRT */
2442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002444 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (ctx->ver != 0) {
2446 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002447 ctx->ver = 0;
2448 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002449#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002450}
2451
Hanno Beckerab377312017-08-23 16:24:51 +01002452#endif /* !MBEDTLS_RSA_ALT */
2453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002455
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002456#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
2458/*
2459 * Example RSA-1024 keypair, for test purposes
2460 */
2461#define KEY_LEN 128
2462
2463#define RSA_N "9292758453063D803DD603D5E777D788" \
2464 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2465 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2466 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2467 "93A89813FBF3C4F8066D2D800F7C38A8" \
2468 "1AE31942917403FF4946B0A83D3D3E05" \
2469 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2470 "5E94BB77B07507233A0BC7BAC8F90F79"
2471
2472#define RSA_E "10001"
2473
2474#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2475 "66CA472BC44D253102F8B4A9D3BFA750" \
2476 "91386C0077937FE33FA3252D28855837" \
2477 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2478 "DF79C5CE07EE72C7F123142198164234" \
2479 "CABB724CF78B8173B9F880FC86322407" \
2480 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2481 "071513A1E85B5DFA031F21ECAE91A34D"
2482
2483#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2484 "2C01CAD19EA484A87EA4377637E75500" \
2485 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2486 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2487
2488#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2489 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2490 "910E4168387E3C30AA1E00C339A79508" \
2491 "8452DD96A9A5EA5D9DCA68DA636032AF"
2492
Paul Bakker5121ce52009-01-03 21:22:43 +00002493#define PT_LEN 24
2494#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2495 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002498static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002499{
gufe44c2620da2020-08-03 17:56:50 +02002500#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002501 size_t i;
2502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002504 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 }
Paul Bakker545570e2010-07-18 09:00:25 +00002506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002508 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002510#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002512 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002516#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002519}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002521
Paul Bakker5121ce52009-01-03 21:22:43 +00002522/*
2523 * Checkup routine
2524 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002525int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002526{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002527 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002529 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 unsigned char rsa_plaintext[PT_LEN];
2532 unsigned char rsa_decrypted[PT_LEN];
2533 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002534#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002535 unsigned char sha1sum[20];
2536#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
Hanno Becker3a701162017-08-22 13:52:43 +01002538 mbedtls_mpi K;
2539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 mbedtls_mpi_init(&K);
2541 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2544 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2545 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2546 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2547 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2548 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2549 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2550 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2551 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2552 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (verbose != 0) {
2557 mbedtls_printf(" RSA key validation: ");
2558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2561 mbedtls_rsa_check_privkey(&rsa) != 0) {
2562 if (verbose != 0) {
2563 mbedtls_printf("failed\n");
2564 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Hanno Becker5bc87292017-05-03 15:09:31 +01002566 ret = 1;
2567 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 }
2569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (verbose != 0) {
2571 mbedtls_printf("passed\n PKCS#1 encryption : ");
2572 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2577 PT_LEN, rsa_plaintext,
2578 rsa_ciphertext) != 0) {
2579 if (verbose != 0) {
2580 mbedtls_printf("failed\n");
2581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Hanno Becker5bc87292017-05-03 15:09:31 +01002583 ret = 1;
2584 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 }
2586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 if (verbose != 0) {
2588 mbedtls_printf("passed\n PKCS#1 decryption : ");
2589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2592 &len, rsa_ciphertext, rsa_decrypted,
2593 sizeof(rsa_decrypted)) != 0) {
2594 if (verbose != 0) {
2595 mbedtls_printf("failed\n");
2596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
Hanno Becker5bc87292017-05-03 15:09:31 +01002598 ret = 1;
2599 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 }
2601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2603 if (verbose != 0) {
2604 mbedtls_printf("failed\n");
2605 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
Hanno Becker5bc87292017-05-03 15:09:31 +01002607 ret = 1;
2608 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 }
2610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (verbose != 0) {
2612 mbedtls_printf("passed\n");
2613 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002614
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002615#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (verbose != 0) {
2617 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002618 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002620 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2621 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (verbose != 0) {
2623 mbedtls_printf("failed\n");
2624 }
2625
2626 return 1;
2627 }
2628
2629 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2630 MBEDTLS_MD_SHA1, 20,
2631 sha1sum, rsa_ciphertext) != 0) {
2632 if (verbose != 0) {
2633 mbedtls_printf("failed\n");
2634 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Hanno Becker5bc87292017-05-03 15:09:31 +01002636 ret = 1;
2637 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 }
2639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 if (verbose != 0) {
2641 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2642 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2645 sha1sum, rsa_ciphertext) != 0) {
2646 if (verbose != 0) {
2647 mbedtls_printf("failed\n");
2648 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Hanno Becker5bc87292017-05-03 15:09:31 +01002650 ret = 1;
2651 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 }
2653
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 if (verbose != 0) {
2655 mbedtls_printf("passed\n");
2656 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002657#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 if (verbose != 0) {
2660 mbedtls_printf("\n");
2661 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002662
Paul Bakker3d8fb632014-04-17 12:42:41 +02002663cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 mbedtls_mpi_free(&K);
2665 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002667 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002670}
2671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674#endif /* MBEDTLS_RSA_C */