blob: db0b0f74f1880c678be07b597aee69b396e5d4e8 [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"
Chris Jones66a4cd42021-03-09 16:04:12 +000031#include "rsa_alt_helpers.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000034#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020035#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020036#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020037#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000038
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <string.h>
40
gufe44c2620da2020-08-03 17:56:50 +020041#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000042#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010046
Dave Rodgman19e8cd02023-05-09 11:10:21 +010047
48#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
49
50/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
51 * operation (EME-PKCS1-v1_5 decoding).
52 *
53 * \note The return value from this function is a sensitive value
54 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
55 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
56 * is often a situation that an attacker can provoke and leaking which
57 * one is the result is precisely the information the attacker wants.
58 *
59 * \param input The input buffer which is the payload inside PKCS#1v1.5
60 * encryption padding, called the "encoded message EM"
61 * by the terminology.
62 * \param ilen The length of the payload in the \p input buffer.
63 * \param output The buffer for the payload, called "message M" by the
64 * PKCS#1 terminology. This must be a writable buffer of
65 * length \p output_max_len bytes.
66 * \param olen The address at which to store the length of
67 * the payload. This must not be \c NULL.
68 * \param output_max_len The length in bytes of the output buffer \p output.
69 *
70 * \return \c 0 on success.
71 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
72 * The output buffer is too small for the unpadded payload.
73 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
74 * The input doesn't contain properly formatted padding.
75 */
76static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
77 size_t ilen,
78 unsigned char *output,
79 size_t output_max_len,
80 size_t *olen)
81{
82 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
83 size_t i, plaintext_max_size;
84
85 /* The following variables take sensitive values: their value must
86 * not leak into the observable behavior of the function other than
87 * the designated outputs (output, olen, return value). Otherwise
88 * this would open the execution of the function to
89 * side-channel-based variants of the Bleichenbacher padding oracle
90 * attack. Potential side channels include overall timing, memory
91 * access patterns (especially visible to an adversary who has access
92 * to a shared memory cache), and branches (especially visible to
93 * an adversary who has access to a shared code cache or to a shared
94 * branch predictor). */
95 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +010096 mbedtls_ct_condition_t bad;
97 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +010098 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +010099 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100100
101 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
102 : output_max_len;
103
104 /* Check and get padding length in constant time and constant
105 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100106 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100107
108
109 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
110 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100111 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100112
113 /* Read the whole buffer. Set pad_done to nonzero if we find
114 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100115 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100116 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100117 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100118 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100119 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100120 }
121
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100122 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100123 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100124
125 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100126 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100127
128 /* If the padding is valid, set plaintext_size to the number of
129 * remaining bytes after stripping the padding. If the padding
130 * is invalid, avoid leaking this fact through the size of the
131 * output: use the maximum message size that fits in the output
132 * buffer. Do it without branches to avoid leaking the padding
133 * validity through timing. RSA keys are small enough that all the
134 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100135 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100136 bad, (unsigned) plaintext_max_size,
137 (unsigned) (ilen - pad_count - 3));
138
139 /* Set output_too_large to 0 if the plaintext fits in the output
140 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100141 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100142 plaintext_max_size);
143
144 /* Set ret without branches to avoid timing attacks. Return:
145 * - INVALID_PADDING if the padding is bad (bad != 0).
146 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
147 * plaintext does not fit in the output buffer.
148 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100149 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100150 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100151 MBEDTLS_ERR_RSA_INVALID_PADDING,
152 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100153 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100154
155 /* If the padding is bad or the plaintext is too large, zero the
156 * data that we're about to copy to the output buffer.
157 * We need to copy the same amount of data
158 * from the same buffer whether the padding is good or not to
159 * avoid leaking the padding validity through overall timing or
160 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100161 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100162
163 /* If the plaintext is too large, truncate it to the buffer size.
164 * Copy anyway to avoid revealing the length through timing, because
165 * revealing the length is as bad as revealing the padding validity
166 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100167 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100168 (unsigned) plaintext_max_size,
169 (unsigned) plaintext_size);
170
171 /* Move the plaintext to the leftmost position where it can start in
172 * the working buffer, i.e. make it start plaintext_max_size from
173 * the end of the buffer. Do this with a memory access trace that
174 * does not depend on the plaintext size. After this move, the
175 * starting location of the plaintext is no longer sensitive
176 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100177 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
178 plaintext_max_size,
179 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100180
181 /* Finally copy the decrypted plaintext plus trailing zeros into the output
182 * buffer. If output_max_len is 0, then output may be an invalid pointer
183 * and the result of memcpy() would be undefined; prevent undefined
184 * behavior making sure to depend only on output_max_len (the size of the
185 * user-provided output buffer), which is independent from plaintext
186 * length, validity of padding, success of the decryption, and other
187 * secrets. */
188 if (output_max_len != 0) {
189 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
190 }
191
192 /* Report the amount of data we copied to the output buffer. In case
193 * of errors (bad padding or output too large), the value of *olen
194 * when this function returns is not specified. Making it equivalent
195 * to the good case limits the risks of leaking the padding validity. */
196 *olen = plaintext_size;
197
198 return ret;
199}
200
201#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
202
Hanno Beckera565f542017-10-11 11:00:19 +0100203#if !defined(MBEDTLS_RSA_ALT)
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
206 const mbedtls_mpi *N,
207 const mbedtls_mpi *P, const mbedtls_mpi *Q,
208 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100209{
Janos Follath24eed8d2019-11-22 13:21:35 +0000210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
213 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
214 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
215 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
216 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
217 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100218 }
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (N != NULL) {
221 ctx->len = mbedtls_mpi_size(&ctx->N);
222 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100225}
226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
228 unsigned char const *N, size_t N_len,
229 unsigned char const *P, size_t P_len,
230 unsigned char const *Q, size_t Q_len,
231 unsigned char const *D, size_t D_len,
232 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100233{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000234 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if (N != NULL) {
237 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
238 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100239 }
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if (P != NULL) {
242 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
243 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (Q != NULL) {
246 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
247 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (D != NULL) {
250 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
251 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (E != NULL) {
254 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
255 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100256
257cleanup:
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (ret != 0) {
260 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
261 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100264}
265
Hanno Becker705fc682017-10-10 17:57:02 +0100266/*
267 * Checks whether the context fields are set in such a way
268 * that the RSA primitives will be able to execute without error.
269 * It does *not* make guarantees for consistency of the parameters.
270 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100271static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
272 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100273{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100274#if !defined(MBEDTLS_RSA_NO_CRT)
275 /* blinding_needed is only used for NO_CRT to decide whether
276 * P,Q need to be present or not. */
277 ((void) blinding_needed);
278#endif
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
281 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
282 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000283 }
Hanno Becker705fc682017-10-10 17:57:02 +0100284
285 /*
286 * 1. Modular exponentiation needs positive, odd moduli.
287 */
288
289 /* Modular exponentiation wrt. N is always used for
290 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
292 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
293 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100294 }
295
296#if !defined(MBEDTLS_RSA_NO_CRT)
297 /* Modular exponentiation for P and Q is only
298 * used for private key operations and if CRT
299 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (is_priv &&
301 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
302 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
303 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
304 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
305 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100306 }
307#endif /* !MBEDTLS_RSA_NO_CRT */
308
309 /*
310 * 2. Exponents must be positive
311 */
312
313 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
315 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
316 }
Hanno Becker705fc682017-10-10 17:57:02 +0100317
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100318#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100319 /* For private key operations, use D or DP & DQ
320 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
322 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
323 }
Hanno Becker705fc682017-10-10 17:57:02 +0100324#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (is_priv &&
326 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
327 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
328 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100329 }
330#endif /* MBEDTLS_RSA_NO_CRT */
331
332 /* Blinding shouldn't make exponents negative either,
333 * so check that P, Q >= 1 if that hasn't yet been
334 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100335#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (is_priv && blinding_needed &&
337 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
338 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
339 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100340 }
341#endif
342
343 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100344 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100345#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (is_priv &&
347 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
348 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100349 }
350#endif
351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100353}
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100356{
357 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500358 int have_N, have_P, have_Q, have_D, have_E;
359#if !defined(MBEDTLS_RSA_NO_CRT)
360 int have_DP, have_DQ, have_QP;
361#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500362 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
365 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
366 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
367 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
368 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500369
370#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
372 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
373 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500374#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100375
Hanno Becker617c1ae2017-08-23 14:11:24 +0100376 /*
377 * Check whether provided parameters are enough
378 * to deduce all others. The following incomplete
379 * parameter sets for private keys are supported:
380 *
381 * (1) P, Q missing.
382 * (2) D and potentially N missing.
383 *
384 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100385
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500386 n_missing = have_P && have_Q && have_D && have_E;
387 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
388 d_missing = have_P && have_Q && !have_D && have_E;
389 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100390
391 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500392 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if (!is_priv && !is_pub) {
395 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
396 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100397
398 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100399 * Step 1: Deduce N if P, Q are provided.
400 */
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (!have_N && have_P && have_Q) {
403 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
404 &ctx->Q)) != 0) {
405 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100406 }
407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100409 }
410
411 /*
412 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100413 */
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (pq_missing) {
416 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
417 &ctx->P, &ctx->Q);
418 if (ret != 0) {
419 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
420 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 } else if (d_missing) {
423 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
424 &ctx->Q,
425 &ctx->E,
426 &ctx->D)) != 0) {
427 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100428 }
429 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100430
Hanno Becker617c1ae2017-08-23 14:11:24 +0100431 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100432 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100433 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100434 */
435
Hanno Becker23344b52017-08-23 07:43:27 +0100436#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (is_priv && !(have_DP && have_DQ && have_QP)) {
438 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
439 &ctx->DP, &ctx->DQ, &ctx->QP);
440 if (ret != 0) {
441 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
442 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100443 }
Hanno Becker23344b52017-08-23 07:43:27 +0100444#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100445
446 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100447 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100448 */
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100451}
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
454 unsigned char *N, size_t N_len,
455 unsigned char *P, size_t P_len,
456 unsigned char *Q, size_t Q_len,
457 unsigned char *D, size_t D_len,
458 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100459{
460 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500461 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100462
463 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500464 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
466 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
467 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
468 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
469 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100472 /* If we're trying to export private parameters for a public key,
473 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if (P != NULL || Q != NULL || D != NULL) {
475 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
476 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100477
478 }
479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (N != NULL) {
481 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
482 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (P != NULL) {
485 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
486 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (Q != NULL) {
489 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
490 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if (D != NULL) {
493 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
494 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (E != NULL) {
497 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
498 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100499
500cleanup:
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100503}
504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
506 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
507 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100508{
Janos Follath24eed8d2019-11-22 13:21:35 +0000509 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500510 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100511
512 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500513 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
515 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
516 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
517 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
518 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100521 /* If we're trying to export private parameters for a public key,
522 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (P != NULL || Q != NULL || D != NULL) {
524 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
525 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100526
527 }
528
529 /* Export all requested core parameters. */
530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
532 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
533 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
534 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
535 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
536 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100537 }
538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100540}
541
542/*
543 * Export CRT parameters
544 * This must also be implemented if CRT is not used, for being able to
545 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
546 * can be used in this case.
547 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100548int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
549 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100550{
Janos Follath24eed8d2019-11-22 13:21:35 +0000551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500552 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100553
554 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500555 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
557 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
558 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
559 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
560 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (!is_priv) {
563 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
564 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100565
Hanno Beckerdc95c892017-08-23 06:57:02 +0100566#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100567 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
569 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
570 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
571 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100572 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100573#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
575 DP, DQ, QP)) != 0) {
576 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100577 }
578#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100581}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100582
Paul Bakker5121ce52009-01-03 21:22:43 +0000583/*
584 * Initialize an RSA context
585 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100586void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000587{
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
Ronald Cronc1905a12021-06-05 11:11:14 +0200590 ctx->padding = MBEDTLS_RSA_PKCS_V15;
591 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100594 /* Set ctx->ver to nonzero to indicate that the mutex has been
595 * initialized and will need to be freed. */
596 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200598#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000599}
600
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100601/*
602 * Set padding for an existing RSA context
603 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
605 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100606{
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200608#if defined(MBEDTLS_PKCS1_V15)
609 case MBEDTLS_RSA_PKCS_V15:
610 break;
611#endif
612
613#if defined(MBEDTLS_PKCS1_V21)
614 case MBEDTLS_RSA_PKCS_V21:
615 break;
616#endif
617 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200619 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200620
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200621#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
623 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200624 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200625 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 return MBEDTLS_ERR_RSA_INVALID_PADDING;
627 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200628 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200629#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500630
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100631 ctx->padding = padding;
632 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100635}
636
Hanno Becker617c1ae2017-08-23 14:11:24 +0100637/*
Yanray Wang83548b52023-03-15 16:46:34 +0800638 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800639 */
640int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
641{
Yanray Wang644b9012023-03-15 16:50:31 +0800642 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +0800643}
644
645/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800646 * Get hash identifier of mbedtls_md_type_t type
647 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800648int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +0800649{
Yanray Wang644b9012023-03-15 16:50:31 +0800650 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +0800651}
652
653/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100654 * Get length in bytes of RSA modulus
655 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100657{
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100659}
660
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
664/*
665 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800666 *
667 * This generation method follows the RSA key pair generation procedure of
668 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
671 int (*f_rng)(void *, unsigned char *, size_t),
672 void *p_rng,
673 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000674{
Janos Follath24eed8d2019-11-22 13:21:35 +0000675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800676 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100677 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000678
Janos Follathb8fc1b02018-09-03 15:37:01 +0100679 /*
680 * If the modulus is 1024 bit long or shorter, then the security strength of
681 * the RSA algorithm is less than or equal to 80 bits and therefore an error
682 * rate of 2^-80 is sufficient.
683 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100685 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 mbedtls_mpi_init(&H);
689 mbedtls_mpi_init(&G);
690 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000691
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000692 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100693 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
694 goto cleanup;
695 }
696
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000697 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
699 goto cleanup;
700 }
701
702 /*
703 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800704 * 1. |P-Q| > 2^( nbits / 2 - 100 )
705 * 2. GCD( E, (P-1)*(Q-1) ) == 1
706 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 do {
711 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
712 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
715 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800717 /* 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 +0100718 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
719 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800723 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (H.s < 0) {
725 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
726 }
Janos Follathef441782016-09-21 13:18:12 +0100727
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100728 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
730 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
731 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800732
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800733 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
735 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800736 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800738
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800739 /* 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 +0100740 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
741 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
742 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 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 -0800745 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800747
748 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100751 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
753 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100758
Jethro Beekman97f95c92018-02-13 15:50:36 -0800759#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 * DP = D mod (P - 1)
762 * DQ = D mod (Q - 1)
763 * QP = Q^-1 mod P
764 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
766 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100767#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
Hanno Becker83aad1f2017-08-23 06:45:10 +0100769 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
772cleanup:
773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 mbedtls_mpi_free(&H);
775 mbedtls_mpi_free(&G);
776 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (ret != 0) {
779 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if ((-ret & ~0x7f) == 0) {
782 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
783 }
784 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000785 }
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000788}
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000791
792/*
793 * Check a public RSA key
794 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000796{
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
798 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100799 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
802 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100803 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
806 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
807 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
808 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
809 }
810
811 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000812}
813
814/*
Hanno Becker705fc682017-10-10 17:57:02 +0100815 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000818{
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
820 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
821 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 }
Paul Bakker48377d92013-08-30 12:06:24 +0200823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
825 &ctx->D, &ctx->E, NULL, NULL) != 0) {
826 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000827 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000828
Hanno Beckerb269a852017-08-25 08:03:21 +0100829#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
831 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
832 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100833 }
834#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000837}
838
839/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100840 * Check if contexts holding a public and private key match
841 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
843 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100844{
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
846 mbedtls_rsa_check_privkey(prv) != 0) {
847 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100848 }
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
851 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
852 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100853 }
854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100856}
857
858/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000859 * Do an RSA public key operation
860 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
862 const unsigned char *input,
863 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000864{
Janos Follath24eed8d2019-11-22 13:21:35 +0000865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000866 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
870 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
871 }
Hanno Becker705fc682017-10-10 17:57:02 +0100872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000874
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200875#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
877 return ret;
878 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200879#endif
880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200884 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
885 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000886 }
887
888 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
890 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
892cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
895 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
896 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100897#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (ret != 0) {
902 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
903 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000906}
907
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200908/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200909 * Generate or update blinding values, see section 10 of:
910 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200911 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200912 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200913 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100914static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
915 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200916{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200917 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200918 mbedtls_mpi R;
919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200923 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
925 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
926 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
927 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200928
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200929 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200930 }
931
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200932 /* Unblinding value: Vf = random number, invertible mod N */
933 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200935 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
936 goto cleanup;
937 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 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 +0200940
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200941 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
943 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
944 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200945
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200946 /* At this point, Vi is invertible mod N if and only if both Vf and R
947 * are invertible mod N. If one of them isn't, we don't need to know
948 * which one, we just loop and choose new values for both of them.
949 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
951 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200952 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200954
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500956
957 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
959 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200960
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200961 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200962 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 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 +0200964
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200965
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200966cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200970}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200971
Paul Bakker5121ce52009-01-03 21:22:43 +0000972/*
Janos Follathe81102e2017-03-22 13:38:28 +0000973 * Exponent blinding supposed to prevent side-channel attacks using multiple
974 * traces of measurements to recover the RSA key. The more collisions are there,
975 * the more bits of the key can be recovered. See [3].
976 *
977 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800978 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000979 *
980 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800981 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000982 *
983 * (With the currently (as of 2017 April) known best algorithms breaking 2048
984 * bit RSA requires approximately as much time as trying out 2^112 random keys.
985 * Thus in this sense with 28 byte blinding the security is not reduced by
986 * side-channel attacks like the one in [3])
987 *
988 * This countermeasure does not help if the key recovery is possible with a
989 * single trace.
990 */
991#define RSA_EXPONENT_BLINDING 28
992
993/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 * Do an RSA private key operation
995 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
997 int (*f_rng)(void *, unsigned char *, size_t),
998 void *p_rng,
999 const unsigned char *input,
1000 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001001{
Janos Follath24eed8d2019-11-22 13:21:35 +00001002 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001003 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001004
1005 /* Temporary holding the result */
1006 mbedtls_mpi T;
1007
1008 /* Temporaries holding P-1, Q-1 and the
1009 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001010 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001011
1012#if !defined(MBEDTLS_RSA_NO_CRT)
1013 /* Temporaries holding the results mod p resp. mod q. */
1014 mbedtls_mpi TP, TQ;
1015
1016 /* Temporaries holding the blinded exponents for
1017 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001018 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001019
1020 /* Pointers to actual exponents to be used - either the unblinded
1021 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +00001022 mbedtls_mpi *DP = &ctx->DP;
1023 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +01001024#else
1025 /* Temporary holding the blinded exponent (if used). */
1026 mbedtls_mpi D_blind;
1027
1028 /* Pointer to actual exponent to be used - either the unblinded
1029 * or the blinded one, depending on the presence of a PRNG. */
1030 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +01001031#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001032
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001033 /* Temporaries holding the initial input and the double
1034 * checked result; should be the same in the end. */
1035 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if (f_rng == NULL) {
1038 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1039 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if (rsa_check_context(ctx, 1 /* private key checks */,
1042 1 /* blinding on */) != 0) {
1043 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001044 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001045
Hanno Becker06811ce2017-05-03 15:10:34 +01001046#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1048 return ret;
1049 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001050#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001051
Hanno Becker06811ce2017-05-03 15:10:34 +01001052 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 mbedtls_mpi_init(&P1);
1056 mbedtls_mpi_init(&Q1);
1057 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001058
Janos Follathe81102e2017-03-22 13:38:28 +00001059#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001061#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 mbedtls_mpi_init(&DP_blind);
1063 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001064#endif
1065
Hanno Becker06811ce2017-05-03 15:10:34 +01001066#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001068#endif
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 mbedtls_mpi_init(&I);
1071 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001072
1073 /* End of MPI initialization */
1074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1076 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001077 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1078 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 }
1080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +01001082
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001083 /*
1084 * Blinding
1085 * T = T * Vi mod N
1086 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1088 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1089 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001090
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001091 /*
1092 * Exponent blinding
1093 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1095 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001096
Janos Follathf9203b42017-03-22 15:13:15 +00001097#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001098 /*
1099 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1100 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1102 f_rng, p_rng));
1103 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1104 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1105 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +00001106
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001107 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001108#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001109 /*
1110 * DP_blind = ( P - 1 ) * R + DP
1111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1113 f_rng, p_rng));
1114 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1115 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1116 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001117
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001118 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001119
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001120 /*
1121 * DQ_blind = ( Q - 1 ) * R + DQ
1122 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1124 f_rng, p_rng));
1125 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1126 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1127 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001128
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001129 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001130#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001134#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001135 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001136 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001138 * TP = input ^ dP mod P
1139 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001140 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1143 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
1145 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001146 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1149 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1150 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
1152 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001153 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1156 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001158
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001159 /*
1160 * Unblind
1161 * T = T * Vf mod N
1162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1164 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
Hanno Becker2dec5e82017-10-03 07:49:52 +01001166 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1168 &ctx->N, &ctx->RN));
1169 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001170 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1171 goto cleanup;
1172 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001173
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001176
1177cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1180 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1181 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001182#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 mbedtls_mpi_free(&P1);
1185 mbedtls_mpi_free(&Q1);
1186 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001187
Janos Follathe81102e2017-03-22 13:38:28 +00001188#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001190#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 mbedtls_mpi_free(&DP_blind);
1192 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001193#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001196
1197#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001199#endif
1200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 mbedtls_mpi_free(&C);
1202 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 if (ret != 0 && ret >= -0x007f) {
1205 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1206 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001209}
1210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001212/**
1213 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1214 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001215 * \param dst buffer to mask
1216 * \param dlen length of destination buffer
1217 * \param src source of the mask generation
1218 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001219 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001220 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001221static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1222 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001223{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001224 unsigned char counter[4];
1225 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001226 unsigned int hlen;
1227 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001228 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001229 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001230 const mbedtls_md_info_t *md_info;
1231 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 mbedtls_md_init(&md_ctx);
1234 md_info = mbedtls_md_info_from_type(md_alg);
1235 if (md_info == NULL) {
1236 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1237 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 mbedtls_md_init(&md_ctx);
1240 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001241 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 memset(mask, 0, sizeof(mask));
1247 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001248
Simon Butcher02037452016-03-01 21:19:12 +00001249 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001250 p = dst;
1251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001253 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001255 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001259 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 }
1261 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001262 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 }
1264 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001265 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 }
1267 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001268 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001272 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001274
1275 counter[3]++;
1276
1277 dlen -= use_len;
1278 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001279
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001280exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001285}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001286
1287/**
1288 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1289 *
1290 * \param hash the input hash
1291 * \param hlen length of the input hash
1292 * \param salt the input salt
1293 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001294 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001295 * \param md_alg message digest to use
1296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001297static int hash_mprime(const unsigned char *hash, size_t hlen,
1298 const unsigned char *salt, size_t slen,
1299 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001300{
1301 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001302
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001303 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1307 if (md_info == NULL) {
1308 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1309 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 mbedtls_md_init(&md_ctx);
1312 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001313 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 }
1315 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001316 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 }
1318 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001319 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 }
1321 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001322 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 }
1324 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001325 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 }
1327 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001328 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001330
1331exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001335}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001336
1337/**
1338 * Compute a hash.
1339 *
1340 * \param md_alg algorithm to use
1341 * \param input input message to hash
1342 * \param ilen input length
1343 * \param output the output buffer - must be large enough for \p md_alg
1344 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001345static int compute_hash(mbedtls_md_type_t md_alg,
1346 const unsigned char *input, size_t ilen,
1347 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001348{
1349 const mbedtls_md_info_t *md_info;
1350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 md_info = mbedtls_md_info_from_type(md_alg);
1352 if (md_info == NULL) {
1353 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1354 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001361/*
1362 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1363 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001364int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1365 int (*f_rng)(void *, unsigned char *, size_t),
1366 void *p_rng,
1367 const unsigned char *label, size_t label_len,
1368 size_t ilen,
1369 const unsigned char *input,
1370 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001371{
1372 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001374 unsigned char *p = output;
1375 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 if (f_rng == NULL) {
1378 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1379 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001380
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001381 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 if (hlen == 0) {
1383 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1384 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001385
1386 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001387
Simon Butcher02037452016-03-01 21:19:12 +00001388 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1390 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1391 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001394
1395 *p++ = 0;
1396
Simon Butcher02037452016-03-01 21:19:12 +00001397 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1399 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1400 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001401
1402 p += hlen;
1403
Simon Butcher02037452016-03-01 21:19:12 +00001404 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1406 if (ret != 0) {
1407 return ret;
1408 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001409 p += hlen;
1410 p += olen - 2 * hlen - 2 - ilen;
1411 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 if (ilen != 0) {
1413 memcpy(p, input, ilen);
1414 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001415
Simon Butcher02037452016-03-01 21:19:12 +00001416 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001418 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 return ret;
1420 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001421
Simon Butcher02037452016-03-01 21:19:12 +00001422 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001424 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 return ret;
1426 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001433/*
1434 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1435 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001436int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1437 int (*f_rng)(void *, unsigned char *, size_t),
1438 void *p_rng, size_t ilen,
1439 const unsigned char *input,
1440 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001441{
1442 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001444 unsigned char *p = output;
1445
Paul Bakkerb3869132013-02-28 17:21:01 +01001446 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001447
Simon Butcher02037452016-03-01 21:19:12 +00001448 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if (ilen + 11 < ilen || olen < ilen + 11) {
1450 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1451 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001452
1453 nb_pad = olen - 3 - ilen;
1454
1455 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (f_rng == NULL) {
1458 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1459 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001460
1461 *p++ = MBEDTLS_RSA_CRYPT;
1462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001464 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001465
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001466 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 ret = f_rng(p_rng, p, 1);
1468 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001469
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001470 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (rng_dl == 0 || ret != 0) {
1472 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1473 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001474
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001475 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001476 }
1477
1478 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (ilen != 0) {
1480 memcpy(p, input, ilen);
1481 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001486
Paul Bakker5121ce52009-01-03 21:22:43 +00001487/*
1488 * Add the message padding, then do an RSA operation
1489 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001490int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1491 int (*f_rng)(void *, unsigned char *, size_t),
1492 void *p_rng,
1493 size_t ilen,
1494 const unsigned char *input,
1495 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001496{
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#if defined(MBEDTLS_PKCS1_V15)
1499 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1501 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001502#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_PKCS1_V21)
1505 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1507 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001508#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
1510 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001513}
1514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001516/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001517 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001518 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001519int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1520 int (*f_rng)(void *, unsigned char *, size_t),
1521 void *p_rng,
1522 const unsigned char *label, size_t label_len,
1523 size_t *olen,
1524 const unsigned char *input,
1525 unsigned char *output,
1526 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001527{
Janos Follath24eed8d2019-11-22 13:21:35 +00001528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001529 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001530 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001531 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001533 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001534 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001535
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001536 /*
1537 * Parameters sanity checks
1538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1540 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1541 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001542
1543 ilen = ctx->len;
1544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if (ilen < 16 || ilen > sizeof(buf)) {
1546 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1547 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001548
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001549 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 if (hlen == 0) {
1551 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1552 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001553
Janos Follathc17cda12016-02-11 11:08:18 +00001554 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 if (2 * hlen + 2 > ilen) {
1556 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1557 }
Janos Follathc17cda12016-02-11 11:08:18 +00001558
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001559 /*
1560 * RSA operation
1561 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001565 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001567
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001568 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001569 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001570 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001571 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001573 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 /* DB: Apply dbMask to maskedDB */
1575 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001576 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001577 goto cleanup;
1578 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001579
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001580 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1582 label, label_len, lhash);
1583 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001584 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001586
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001587 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001588 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001589 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 p = buf;
1591
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001592 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001593
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001594 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001595
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001596 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001597 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001598 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001599
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001600 /* Get zero-padding len, but always read till end of buffer
1601 * (minus one, for the 01 byte) */
1602 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001603 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001605 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1606 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001607 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001608
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001609 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001610 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001611
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001612 /*
1613 * The only information "leaked" is whether the padding was correct or not
1614 * (eg, no data is copied if it was not correct). This meets the
1615 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1616 * the different error conditions.
1617 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001618 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001619 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1620 goto cleanup;
1621 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001624 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1625 goto cleanup;
1626 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001627
1628 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 if (*olen != 0) {
1630 memcpy(output, p, *olen);
1631 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001632 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001633
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001634cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 mbedtls_platform_zeroize(buf, sizeof(buf));
1636 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001639}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001643/*
1644 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1645 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001646int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1647 int (*f_rng)(void *, unsigned char *, size_t),
1648 void *p_rng,
1649 size_t *olen,
1650 const unsigned char *input,
1651 unsigned char *output,
1652 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001653{
1654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1655 size_t ilen;
1656 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1657
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001658 ilen = ctx->len;
1659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1661 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1662 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if (ilen < 16 || ilen > sizeof(buf)) {
1665 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1666 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001671 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1675 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001676
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001677cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001681}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
1684/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001685 * Do an RSA operation, then remove the message padding
1686 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001687int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1688 int (*f_rng)(void *, unsigned char *, size_t),
1689 void *p_rng,
1690 size_t *olen,
1691 const unsigned char *input,
1692 unsigned char *output,
1693 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001694{
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#if defined(MBEDTLS_PKCS1_V15)
1697 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1699 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001700#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#if defined(MBEDTLS_PKCS1_V21)
1703 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1705 olen, input, output,
1706 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001707#endif
1708
1709 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001711 }
1712}
1713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001715static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1716 int (*f_rng)(void *, unsigned char *, size_t),
1717 void *p_rng,
1718 mbedtls_md_type_t md_alg,
1719 unsigned int hashlen,
1720 const unsigned char *hash,
1721 int saltlen,
1722 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001723{
1724 size_t olen;
1725 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001726 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001727 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001729 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001730
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001732 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1736 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1737 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (f_rng == NULL) {
1740 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1741 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001742
1743 olen = ctx->len;
1744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001746 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001747 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 if (exp_hashlen == 0) {
1749 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1750 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 if (hashlen != exp_hashlen) {
1753 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1754 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001755 }
1756
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001757 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (hlen == 0) {
1759 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1760 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1763 /* Calculate the largest possible salt length, up to the hash size.
1764 * Normally this is the hash length, which is the maximum salt length
1765 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1766 * enough room, use the maximum salt length that fits. The constraint is
1767 * that the hash length plus the salt length plus 2 bytes must be at most
1768 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1769 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001770 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (olen < hlen + min_slen + 2) {
1772 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1773 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001774 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001776 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 }
1778 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1779 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1780 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001781 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001782 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001785
Simon Butcher02037452016-03-01 21:19:12 +00001786 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001788 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001789 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001790
1791 /* Generate salt of length slen in place in the encoded message */
1792 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1794 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1795 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001796
Paul Bakkerb3869132013-02-28 17:21:01 +01001797 p += slen;
1798
Simon Butcher02037452016-03-01 21:19:12 +00001799 /* Generate H = Hash( M' ) */
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001800 ret = hash_mprime(hash, hashlen, salt, slen, p, (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 if (ret != 0) {
1802 return ret;
1803 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001804
Simon Butcher02037452016-03-01 21:19:12 +00001805 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001807 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001809
Simon Butcher02037452016-03-01 21:19:12 +00001810 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001812 (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 if (ret != 0) {
1814 return ret;
1815 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1818 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001819
1820 p += hlen;
1821 *p++ = 0xBC;
1822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001824}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001825
1826/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001827 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1828 * the option to pass in the salt length.
1829 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001830int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1831 int (*f_rng)(void *, unsigned char *, size_t),
1832 void *p_rng,
1833 mbedtls_md_type_t md_alg,
1834 unsigned int hashlen,
1835 const unsigned char *hash,
1836 int saltlen,
1837 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001838{
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1840 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001841}
1842
1843
1844/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001845 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1846 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001847int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1848 int (*f_rng)(void *, unsigned char *, size_t),
1849 void *p_rng,
1850 mbedtls_md_type_t md_alg,
1851 unsigned int hashlen,
1852 const unsigned char *hash,
1853 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001854{
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1856 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001857}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001861/*
1862 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1863 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001864
1865/* Construct a PKCS v1.5 encoding of a hashed message
1866 *
1867 * This is used both for signature generation and verification.
1868 *
1869 * Parameters:
1870 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001871 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001872 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001873 * - hash: Buffer containing the hashed message or the raw data.
1874 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001875 * - dst: Buffer to hold the encoded message.
1876 *
1877 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001878 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001879 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001880 *
1881 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001882static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1883 unsigned int hashlen,
1884 const unsigned char *hash,
1885 size_t dst_len,
1886 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001887{
1888 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001889 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001890 unsigned char *p = dst;
1891 const char *oid = NULL;
1892
1893 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001895 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 if (md_size == 0) {
1897 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1898 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1901 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1902 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 if (hashlen != md_size) {
1905 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1906 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001907
1908 /* Double-check that 8 + hashlen + oid_size can be used as a
1909 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001911 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 10 + hashlen + oid_size < 10 + hashlen) {
1913 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1914 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001915
1916 /*
1917 * Static bounds check:
1918 * - Need 10 bytes for five tag-length pairs.
1919 * (Insist on 1-byte length encodings to protect against variants of
1920 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1921 * - Need hashlen bytes for hash
1922 * - Need oid_size bytes for hash alg OID.
1923 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 if (nb_pad < 10 + hashlen + oid_size) {
1925 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1926 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001927 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 } else {
1929 if (nb_pad < hashlen) {
1930 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1931 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001932
1933 nb_pad -= hashlen;
1934 }
1935
Hanno Becker2b2f8982017-09-27 17:10:03 +01001936 /* Need space for signature header and padding delimiter (3 bytes),
1937 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (nb_pad < 3 + 8) {
1939 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1940 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001941 nb_pad -= 3;
1942
1943 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001944 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001945
1946 /* Write signature header and padding */
1947 *p++ = 0;
1948 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001950 p += nb_pad;
1951 *p++ = 0;
1952
1953 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 if (md_alg == MBEDTLS_MD_NONE) {
1955 memcpy(p, hash, hashlen);
1956 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001957 }
1958
1959 /* Signing hashed data, add corresponding ASN.1 structure
1960 *
1961 * DigestInfo ::= SEQUENCE {
1962 * digestAlgorithm DigestAlgorithmIdentifier,
1963 * digest Digest }
1964 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1965 * Digest ::= OCTET STRING
1966 *
1967 * Schematic:
1968 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1969 * TAG-NULL + LEN [ NULL ] ]
1970 * TAG-OCTET + LEN [ HASH ] ]
1971 */
1972 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001974 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001976 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001977 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001979 p += oid_size;
1980 *p++ = MBEDTLS_ASN1_NULL;
1981 *p++ = 0x00;
1982 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001983 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001985 p += hashlen;
1986
1987 /* Just a sanity-check, should be automatic
1988 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (p != dst + dst_len) {
1990 mbedtls_platform_zeroize(dst, dst_len);
1991 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001992 }
1993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001995}
1996
Paul Bakkerb3869132013-02-28 17:21:01 +01001997/*
1998 * Do an RSA operation to sign the message digest
1999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002000int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2001 int (*f_rng)(void *, unsigned char *, size_t),
2002 void *p_rng,
2003 mbedtls_md_type_t md_alg,
2004 unsigned int hashlen,
2005 const unsigned char *hash,
2006 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002007{
Janos Follath24eed8d2019-11-22 13:21:35 +00002008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002009 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002012 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2016 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2017 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002018
Hanno Beckerfdf38032017-09-06 12:35:55 +01002019 /*
2020 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2021 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2024 ctx->len, sig)) != 0) {
2025 return ret;
2026 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002027
Hanno Beckerfdf38032017-09-06 12:35:55 +01002028 /* Private key operation
2029 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002030 * In order to prevent Lenstra's attack, make the signature in a
2031 * temporary buffer and check it before returning it.
2032 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 sig_try = mbedtls_calloc(1, ctx->len);
2035 if (sig_try == NULL) {
2036 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002037 }
2038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 verif = mbedtls_calloc(1, ctx->len);
2040 if (verif == NULL) {
2041 mbedtls_free(sig_try);
2042 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2043 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2046 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2047
2048 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002049 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2050 goto cleanup;
2051 }
2052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002054
2055cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002056 mbedtls_zeroize_and_free(sig_try, ctx->len);
2057 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (ret != 0) {
2060 memset(sig, '!', ctx->len);
2061 }
2062 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002065
2066/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 * Do an RSA operation to sign the message digest
2068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002069int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2070 int (*f_rng)(void *, unsigned char *, size_t),
2071 void *p_rng,
2072 mbedtls_md_type_t md_alg,
2073 unsigned int hashlen,
2074 const unsigned char *hash,
2075 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002076{
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002078 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#if defined(MBEDTLS_PKCS1_V15)
2083 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2085 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002086#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#if defined(MBEDTLS_PKCS1_V21)
2089 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2091 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002092#endif
2093
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002097}
2098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002100/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002101 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2104 mbedtls_md_type_t md_alg,
2105 unsigned int hashlen,
2106 const unsigned char *hash,
2107 mbedtls_md_type_t mgf1_hash_id,
2108 int expected_salt_len,
2109 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002110{
Janos Follath24eed8d2019-11-22 13:21:35 +00002111 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002112 size_t siglen;
2113 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002114 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002115 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002116 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002117 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002121 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002123
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 siglen = ctx->len;
2125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 if (siglen < 16 || siglen > sizeof(buf)) {
2127 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2128 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (ret != 0) {
2133 return ret;
2134 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002135
2136 p = buf;
2137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if (buf[siglen - 1] != 0xBC) {
2139 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002140 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 if (md_alg != MBEDTLS_MD_NONE) {
2143 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002144 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (exp_hashlen == 0) {
2146 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2147 }
2148
2149 if (hashlen != exp_hashlen) {
2150 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2151 }
2152 }
2153
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002154 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (hlen == 0) {
2156 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2157 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002158
Simon Butcher02037452016-03-01 21:19:12 +00002159 /*
2160 * Note: EMSA-PSS verification is over the length of N - 1 bits
2161 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (buf[0] >> (8 - siglen * 8 + msb)) {
2165 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2166 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002167
Simon Butcher02037452016-03-01 21:19:12 +00002168 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002170 p++;
2171 siglen -= 1;
2172 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 if (siglen < hlen + 2) {
2175 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2176 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002177 hash_start = p + siglen - hlen - 1;
2178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2180 if (ret != 0) {
2181 return ret;
2182 }
Paul Bakker02303e82013-01-03 11:08:31 +01002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002185
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002187 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 if (*p++ != 0x01) {
2191 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2192 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002193
Gilles Peskine6a54b022017-10-17 19:02:13 +02002194 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2197 observed_salt_len != (size_t) expected_salt_len) {
2198 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002199 }
2200
Simon Butcher02037452016-03-01 21:19:12 +00002201 /*
2202 * Generate H = Hash( M' )
2203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2205 result, mgf1_hash_id);
2206 if (ret != 0) {
2207 return ret;
2208 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (memcmp(hash_start, result, hlen) != 0) {
2211 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2212 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002215}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002216
2217/*
2218 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002220int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2221 mbedtls_md_type_t md_alg,
2222 unsigned int hashlen,
2223 const unsigned char *hash,
2224 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002225{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002226 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002228 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002233 : md_alg;
2234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2236 md_alg, hashlen, hash,
2237 mgf1_hash_id,
2238 MBEDTLS_RSA_SALT_LEN_ANY,
2239 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002240
2241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002245/*
2246 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002248int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2249 mbedtls_md_type_t md_alg,
2250 unsigned int hashlen,
2251 const unsigned char *hash,
2252 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002253{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002254 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002255 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002256 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002259 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002261
2262 sig_len = ctx->len;
2263
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002264 /*
2265 * Prepare expected PKCS1 v1.5 encoding of hash.
2266 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002267
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2269 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002270 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2271 goto cleanup;
2272 }
2273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2275 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002276 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002278
2279 /*
2280 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2281 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 ret = mbedtls_rsa_public(ctx, sig, encoded);
2284 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002285 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002287
Simon Butcher02037452016-03-01 21:19:12 +00002288 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002289 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002290 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2293 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002294 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2295 goto cleanup;
2296 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002297
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002298cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002301 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002302 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002305 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002306 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002309}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002311
2312/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002313 * Do an RSA operation and check the message digest
2314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002315int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2316 mbedtls_md_type_t md_alg,
2317 unsigned int hashlen,
2318 const unsigned char *hash,
2319 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002320{
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002322 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326#if defined(MBEDTLS_PKCS1_V15)
2327 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2329 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002330#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332#if defined(MBEDTLS_PKCS1_V21)
2333 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2335 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002336#endif
2337
2338 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002340 }
2341}
2342
2343/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002344 * Copy the components of an RSA key
2345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002346int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002347{
Janos Follath24eed8d2019-11-22 13:21:35 +00002348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002349
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002350 dst->len = src->len;
2351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2353 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2356 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2357 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002358
2359#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2361 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2362 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2363 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2364 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002365#endif
2366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2370 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002371
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002372 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002373 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002374
2375cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 if (ret != 0) {
2377 mbedtls_rsa_free(dst);
2378 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002381}
2382
2383/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 * Free the components of an RSA key
2385 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002386void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002387{
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002389 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 mbedtls_mpi_free(&ctx->Vi);
2393 mbedtls_mpi_free(&ctx->Vf);
2394 mbedtls_mpi_free(&ctx->RN);
2395 mbedtls_mpi_free(&ctx->D);
2396 mbedtls_mpi_free(&ctx->Q);
2397 mbedtls_mpi_free(&ctx->P);
2398 mbedtls_mpi_free(&ctx->E);
2399 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002400
Hanno Becker33c30a02017-08-23 07:00:22 +01002401#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 mbedtls_mpi_free(&ctx->RQ);
2403 mbedtls_mpi_free(&ctx->RP);
2404 mbedtls_mpi_free(&ctx->QP);
2405 mbedtls_mpi_free(&ctx->DQ);
2406 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002407#endif /* MBEDTLS_RSA_NO_CRT */
2408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002410 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (ctx->ver != 0) {
2412 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002413 ctx->ver = 0;
2414 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002415#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002416}
2417
Hanno Beckerab377312017-08-23 16:24:51 +01002418#endif /* !MBEDTLS_RSA_ALT */
2419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002422#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002423
2424/*
2425 * Example RSA-1024 keypair, for test purposes
2426 */
2427#define KEY_LEN 128
2428
2429#define RSA_N "9292758453063D803DD603D5E777D788" \
2430 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2431 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2432 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2433 "93A89813FBF3C4F8066D2D800F7C38A8" \
2434 "1AE31942917403FF4946B0A83D3D3E05" \
2435 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2436 "5E94BB77B07507233A0BC7BAC8F90F79"
2437
2438#define RSA_E "10001"
2439
2440#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2441 "66CA472BC44D253102F8B4A9D3BFA750" \
2442 "91386C0077937FE33FA3252D28855837" \
2443 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2444 "DF79C5CE07EE72C7F123142198164234" \
2445 "CABB724CF78B8173B9F880FC86322407" \
2446 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2447 "071513A1E85B5DFA031F21ECAE91A34D"
2448
2449#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2450 "2C01CAD19EA484A87EA4377637E75500" \
2451 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2452 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2453
2454#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2455 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2456 "910E4168387E3C30AA1E00C339A79508" \
2457 "8452DD96A9A5EA5D9DCA68DA636032AF"
2458
Paul Bakker5121ce52009-01-03 21:22:43 +00002459#define PT_LEN 24
2460#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2461 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002464static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002465{
gufe44c2620da2020-08-03 17:56:50 +02002466#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002467 size_t i;
2468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002470 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 }
Paul Bakker545570e2010-07-18 09:00:25 +00002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002474 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002476#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002478 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002482#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002487
Paul Bakker5121ce52009-01-03 21:22:43 +00002488/*
2489 * Checkup routine
2490 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002492{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002493 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002495 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 unsigned char rsa_plaintext[PT_LEN];
2498 unsigned char rsa_decrypted[PT_LEN];
2499 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002500#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002501 unsigned char sha1sum[20];
2502#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Hanno Becker3a701162017-08-22 13:52:43 +01002504 mbedtls_mpi K;
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 mbedtls_mpi_init(&K);
2507 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2510 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2511 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2512 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2513 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2514 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2515 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2516 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2517 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2518 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 if (verbose != 0) {
2523 mbedtls_printf(" RSA key validation: ");
2524 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2527 mbedtls_rsa_check_privkey(&rsa) != 0) {
2528 if (verbose != 0) {
2529 mbedtls_printf("failed\n");
2530 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Hanno Becker5bc87292017-05-03 15:09:31 +01002532 ret = 1;
2533 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 }
2535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (verbose != 0) {
2537 mbedtls_printf("passed\n PKCS#1 encryption : ");
2538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2543 PT_LEN, rsa_plaintext,
2544 rsa_ciphertext) != 0) {
2545 if (verbose != 0) {
2546 mbedtls_printf("failed\n");
2547 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
Hanno Becker5bc87292017-05-03 15:09:31 +01002549 ret = 1;
2550 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 }
2552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (verbose != 0) {
2554 mbedtls_printf("passed\n PKCS#1 decryption : ");
2555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2558 &len, rsa_ciphertext, rsa_decrypted,
2559 sizeof(rsa_decrypted)) != 0) {
2560 if (verbose != 0) {
2561 mbedtls_printf("failed\n");
2562 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Hanno Becker5bc87292017-05-03 15:09:31 +01002564 ret = 1;
2565 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
2567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2569 if (verbose != 0) {
2570 mbedtls_printf("failed\n");
2571 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Hanno Becker5bc87292017-05-03 15:09:31 +01002573 ret = 1;
2574 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
2576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (verbose != 0) {
2578 mbedtls_printf("passed\n");
2579 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002580
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002581#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (verbose != 0) {
2583 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002584 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002586 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2587 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (verbose != 0) {
2589 mbedtls_printf("failed\n");
2590 }
2591
2592 return 1;
2593 }
2594
2595 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2596 MBEDTLS_MD_SHA1, 20,
2597 sha1sum, rsa_ciphertext) != 0) {
2598 if (verbose != 0) {
2599 mbedtls_printf("failed\n");
2600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Hanno Becker5bc87292017-05-03 15:09:31 +01002602 ret = 1;
2603 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 }
2605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (verbose != 0) {
2607 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2611 sha1sum, rsa_ciphertext) != 0) {
2612 if (verbose != 0) {
2613 mbedtls_printf("failed\n");
2614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Hanno Becker5bc87292017-05-03 15:09:31 +01002616 ret = 1;
2617 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
2619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (verbose != 0) {
2621 mbedtls_printf("passed\n");
2622 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002623#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (verbose != 0) {
2626 mbedtls_printf("\n");
2627 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002628
Paul Bakker3d8fb632014-04-17 12:42:41 +02002629cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 mbedtls_mpi_free(&K);
2631 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002633 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002636}
2637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640#endif /* MBEDTLS_RSA_C */