blob: 2b9f85b7394e2f2911c260ea5c810336089a1f4a [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 Rodgman16799db2023-11-02 19:47:20 +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"
Tomi Fontanilles573dc232023-12-10 14:57:51 +020032#include "rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050034#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000035#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020036#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020037#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020038#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000039
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
gufe44c2620da2020-08-03 17:56:50 +020042#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000043#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010047
Dave Rodgman19e8cd02023-05-09 11:10:21 +010048
49#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
50
51/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
52 * operation (EME-PKCS1-v1_5 decoding).
53 *
54 * \note The return value from this function is a sensitive value
55 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
56 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
57 * is often a situation that an attacker can provoke and leaking which
58 * one is the result is precisely the information the attacker wants.
59 *
60 * \param input The input buffer which is the payload inside PKCS#1v1.5
61 * encryption padding, called the "encoded message EM"
62 * by the terminology.
63 * \param ilen The length of the payload in the \p input buffer.
64 * \param output The buffer for the payload, called "message M" by the
65 * PKCS#1 terminology. This must be a writable buffer of
66 * length \p output_max_len bytes.
67 * \param olen The address at which to store the length of
68 * the payload. This must not be \c NULL.
69 * \param output_max_len The length in bytes of the output buffer \p output.
70 *
71 * \return \c 0 on success.
72 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
73 * The output buffer is too small for the unpadded payload.
74 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
75 * The input doesn't contain properly formatted padding.
76 */
77static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
78 size_t ilen,
79 unsigned char *output,
80 size_t output_max_len,
81 size_t *olen)
82{
83 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
84 size_t i, plaintext_max_size;
85
86 /* The following variables take sensitive values: their value must
87 * not leak into the observable behavior of the function other than
88 * the designated outputs (output, olen, return value). Otherwise
89 * this would open the execution of the function to
90 * side-channel-based variants of the Bleichenbacher padding oracle
91 * attack. Potential side channels include overall timing, memory
92 * access patterns (especially visible to an adversary who has access
93 * to a shared memory cache), and branches (especially visible to
94 * an adversary who has access to a shared code cache or to a shared
95 * branch predictor). */
96 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +010097 mbedtls_ct_condition_t bad;
98 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +010099 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100100 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100101
102 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
103 : output_max_len;
104
105 /* Check and get padding length in constant time and constant
106 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100107 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100108
109
110 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
111 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100112 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100113
114 /* Read the whole buffer. Set pad_done to nonzero if we find
115 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100116 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100117 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100118 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100119 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100120 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100121 }
122
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100123 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100124 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100125
126 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100127 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100128
129 /* If the padding is valid, set plaintext_size to the number of
130 * remaining bytes after stripping the padding. If the padding
131 * is invalid, avoid leaking this fact through the size of the
132 * output: use the maximum message size that fits in the output
133 * buffer. Do it without branches to avoid leaking the padding
134 * validity through timing. RSA keys are small enough that all the
135 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100136 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100137 bad, (unsigned) plaintext_max_size,
138 (unsigned) (ilen - pad_count - 3));
139
140 /* Set output_too_large to 0 if the plaintext fits in the output
141 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100142 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100143 plaintext_max_size);
144
145 /* Set ret without branches to avoid timing attacks. Return:
146 * - INVALID_PADDING if the padding is bad (bad != 0).
147 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
148 * plaintext does not fit in the output buffer.
149 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100150 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100151 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100152 MBEDTLS_ERR_RSA_INVALID_PADDING,
153 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100154 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100155
156 /* If the padding is bad or the plaintext is too large, zero the
157 * data that we're about to copy to the output buffer.
158 * We need to copy the same amount of data
159 * from the same buffer whether the padding is good or not to
160 * avoid leaking the padding validity through overall timing or
161 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100162 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100163
164 /* If the plaintext is too large, truncate it to the buffer size.
165 * Copy anyway to avoid revealing the length through timing, because
166 * revealing the length is as bad as revealing the padding validity
167 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100168 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100169 (unsigned) plaintext_max_size,
170 (unsigned) plaintext_size);
171
172 /* Move the plaintext to the leftmost position where it can start in
173 * the working buffer, i.e. make it start plaintext_max_size from
174 * the end of the buffer. Do this with a memory access trace that
175 * does not depend on the plaintext size. After this move, the
176 * starting location of the plaintext is no longer sensitive
177 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100178 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
179 plaintext_max_size,
180 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100181
182 /* Finally copy the decrypted plaintext plus trailing zeros into the output
183 * buffer. If output_max_len is 0, then output may be an invalid pointer
184 * and the result of memcpy() would be undefined; prevent undefined
185 * behavior making sure to depend only on output_max_len (the size of the
186 * user-provided output buffer), which is independent from plaintext
187 * length, validity of padding, success of the decryption, and other
188 * secrets. */
189 if (output_max_len != 0) {
190 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
191 }
192
193 /* Report the amount of data we copied to the output buffer. In case
194 * of errors (bad padding or output too large), the value of *olen
195 * when this function returns is not specified. Making it equivalent
196 * to the good case limits the risks of leaking the padding validity. */
197 *olen = plaintext_size;
198
199 return ret;
200}
201
202#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
203
Hanno Beckera565f542017-10-11 11:00:19 +0100204#if !defined(MBEDTLS_RSA_ALT)
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
207 const mbedtls_mpi *N,
208 const mbedtls_mpi *P, const mbedtls_mpi *Q,
209 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100210{
Janos Follath24eed8d2019-11-22 13:21:35 +0000211 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
214 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
215 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
216 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
217 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
218 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100219 }
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if (N != NULL) {
222 ctx->len = mbedtls_mpi_size(&ctx->N);
223 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100226}
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
229 unsigned char const *N, size_t N_len,
230 unsigned char const *P, size_t P_len,
231 unsigned char const *Q, size_t Q_len,
232 unsigned char const *D, size_t D_len,
233 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100234{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000235 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (N != NULL) {
238 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
239 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100240 }
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (P != NULL) {
243 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
244 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if (Q != NULL) {
247 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
248 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (D != NULL) {
251 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
252 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (E != NULL) {
255 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
256 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100257
258cleanup:
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (ret != 0) {
261 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
262 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100265}
266
Hanno Becker705fc682017-10-10 17:57:02 +0100267/*
268 * Checks whether the context fields are set in such a way
269 * that the RSA primitives will be able to execute without error.
270 * It does *not* make guarantees for consistency of the parameters.
271 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
273 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100274{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100275#if !defined(MBEDTLS_RSA_NO_CRT)
276 /* blinding_needed is only used for NO_CRT to decide whether
277 * P,Q need to be present or not. */
278 ((void) blinding_needed);
279#endif
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
282 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
283 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000284 }
Hanno Becker705fc682017-10-10 17:57:02 +0100285
286 /*
287 * 1. Modular exponentiation needs positive, odd moduli.
288 */
289
290 /* Modular exponentiation wrt. N is always used for
291 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
293 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
294 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100295 }
296
297#if !defined(MBEDTLS_RSA_NO_CRT)
298 /* Modular exponentiation for P and Q is only
299 * used for private key operations and if CRT
300 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (is_priv &&
302 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
303 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
304 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
305 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
306 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100307 }
308#endif /* !MBEDTLS_RSA_NO_CRT */
309
310 /*
311 * 2. Exponents must be positive
312 */
313
314 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
316 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
317 }
Hanno Becker705fc682017-10-10 17:57:02 +0100318
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100319#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100320 /* For private key operations, use D or DP & DQ
321 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
323 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
324 }
Hanno Becker705fc682017-10-10 17:57:02 +0100325#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (is_priv &&
327 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
328 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
329 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100330 }
331#endif /* MBEDTLS_RSA_NO_CRT */
332
333 /* Blinding shouldn't make exponents negative either,
334 * so check that P, Q >= 1 if that hasn't yet been
335 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100336#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (is_priv && blinding_needed &&
338 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
339 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
340 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100341 }
342#endif
343
344 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100345 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100346#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (is_priv &&
348 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
349 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100350 }
351#endif
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100354}
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100357{
358 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500359 int have_N, have_P, have_Q, have_D, have_E;
360#if !defined(MBEDTLS_RSA_NO_CRT)
361 int have_DP, have_DQ, have_QP;
362#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500363 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
366 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
367 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
368 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
369 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500370
371#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
373 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
374 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500375#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100376
Hanno Becker617c1ae2017-08-23 14:11:24 +0100377 /*
378 * Check whether provided parameters are enough
379 * to deduce all others. The following incomplete
380 * parameter sets for private keys are supported:
381 *
382 * (1) P, Q missing.
383 * (2) D and potentially N missing.
384 *
385 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100386
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500387 n_missing = have_P && have_Q && have_D && have_E;
388 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
389 d_missing = have_P && have_Q && !have_D && have_E;
390 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100391
392 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500393 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (!is_priv && !is_pub) {
396 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
397 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100398
399 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100400 * Step 1: Deduce N if P, Q are provided.
401 */
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (!have_N && have_P && have_Q) {
404 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
405 &ctx->Q)) != 0) {
406 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100407 }
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100410 }
411
412 /*
413 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100414 */
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (pq_missing) {
417 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
418 &ctx->P, &ctx->Q);
419 if (ret != 0) {
420 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
421 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 } else if (d_missing) {
424 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
425 &ctx->Q,
426 &ctx->E,
427 &ctx->D)) != 0) {
428 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100429 }
430 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100431
Hanno Becker617c1ae2017-08-23 14:11:24 +0100432 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100433 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100434 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100435 */
436
Hanno Becker23344b52017-08-23 07:43:27 +0100437#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (is_priv && !(have_DP && have_DQ && have_QP)) {
439 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
440 &ctx->DP, &ctx->DQ, &ctx->QP);
441 if (ret != 0) {
442 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
443 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444 }
Hanno Becker23344b52017-08-23 07:43:27 +0100445#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100446
447 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100448 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100449 */
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100452}
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
455 unsigned char *N, size_t N_len,
456 unsigned char *P, size_t P_len,
457 unsigned char *Q, size_t Q_len,
458 unsigned char *D, size_t D_len,
459 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100460{
461 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500462 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100463
464 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500465 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
467 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
468 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
469 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
470 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100473 /* If we're trying to export private parameters for a public key,
474 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (P != NULL || Q != NULL || D != NULL) {
476 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
477 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100478
479 }
480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (N != NULL) {
482 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
483 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (P != NULL) {
486 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
487 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (Q != NULL) {
490 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
491 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (D != NULL) {
494 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
495 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (E != NULL) {
498 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
499 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100500
501cleanup:
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100504}
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
507 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
508 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100509{
Janos Follath24eed8d2019-11-22 13:21:35 +0000510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500511 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100512
513 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500514 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
516 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
517 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
518 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
519 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100522 /* If we're trying to export private parameters for a public key,
523 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (P != NULL || Q != NULL || D != NULL) {
525 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
526 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100527
528 }
529
530 /* Export all requested core parameters. */
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
533 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
534 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
535 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
536 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
537 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100541}
542
543/*
544 * Export CRT parameters
545 * This must also be implemented if CRT is not used, for being able to
546 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
547 * can be used in this case.
548 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
550 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100551{
Janos Follath24eed8d2019-11-22 13:21:35 +0000552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500553 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100554
555 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500556 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
558 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
559 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
560 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
561 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (!is_priv) {
564 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
565 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100566
Hanno Beckerdc95c892017-08-23 06:57:02 +0100567#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100568 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
570 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
571 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
572 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100573 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100574#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
576 DP, DQ, QP)) != 0) {
577 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100578 }
579#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100582}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100583
Paul Bakker5121ce52009-01-03 21:22:43 +0000584/*
585 * Initialize an RSA context
586 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100587void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000588{
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Ronald Cronc1905a12021-06-05 11:11:14 +0200591 ctx->padding = MBEDTLS_RSA_PKCS_V15;
592 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100595 /* Set ctx->ver to nonzero to indicate that the mutex has been
596 * initialized and will need to be freed. */
597 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200599#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000600}
601
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100602/*
603 * Set padding for an existing RSA context
604 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100605int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
606 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100607{
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200609#if defined(MBEDTLS_PKCS1_V15)
610 case MBEDTLS_RSA_PKCS_V15:
611 break;
612#endif
613
614#if defined(MBEDTLS_PKCS1_V21)
615 case MBEDTLS_RSA_PKCS_V21:
616 break;
617#endif
618 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200620 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200621
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200622#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
624 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200625 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200626 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return MBEDTLS_ERR_RSA_INVALID_PADDING;
628 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200629 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200630#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500631
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100632 ctx->padding = padding;
633 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100636}
637
Hanno Becker617c1ae2017-08-23 14:11:24 +0100638/*
Yanray Wang83548b52023-03-15 16:46:34 +0800639 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800640 */
641int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
642{
Yanray Wang644b9012023-03-15 16:50:31 +0800643 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +0800644}
645
646/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800647 * Get hash identifier of mbedtls_md_type_t type
648 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800649int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +0800650{
Yanray Wang644b9012023-03-15 16:50:31 +0800651 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +0800652}
653
654/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100655 * Get length in bytes of RSA modulus
656 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100657size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100658{
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100660}
661
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000664
665/*
666 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800667 *
668 * This generation method follows the RSA key pair generation procedure of
669 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
672 int (*f_rng)(void *, unsigned char *, size_t),
673 void *p_rng,
674 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000675{
Janos Follath24eed8d2019-11-22 13:21:35 +0000676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800677 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100678 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
Janos Follathb8fc1b02018-09-03 15:37:01 +0100680 /*
681 * If the modulus is 1024 bit long or shorter, then the security strength of
682 * the RSA algorithm is less than or equal to 80 bits and therefore an error
683 * rate of 2^-80 is sufficient.
684 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100686 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 mbedtls_mpi_init(&H);
690 mbedtls_mpi_init(&G);
691 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000693 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100694 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
695 goto cleanup;
696 }
697
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000698 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
700 goto cleanup;
701 }
702
703 /*
704 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800705 * 1. |P-Q| > 2^( nbits / 2 - 100 )
706 * 2. GCD( E, (P-1)*(Q-1) ) == 1
707 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 do {
712 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
713 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
716 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800718 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
720 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800724 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (H.s < 0) {
726 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
727 }
Janos Follathef441782016-09-21 13:18:12 +0100728
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100729 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
731 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
732 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800733
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800734 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
736 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800737 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800739
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800740 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
742 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
743 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (mbedtls_mpi_bitlen(&ctx->D) <= ((nbits + 1) / 2)) { // (FIPS 186-4 §B.3.1 criterion 3(a))
Jethro Beekman97f95c92018-02-13 15:50:36 -0800746 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800748
749 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100752 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
754 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100759
Jethro Beekman97f95c92018-02-13 15:50:36 -0800760#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 * DP = D mod (P - 1)
763 * DQ = D mod (Q - 1)
764 * QP = Q^-1 mod P
765 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
767 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100768#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Hanno Becker83aad1f2017-08-23 06:45:10 +0100770 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
773cleanup:
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 mbedtls_mpi_free(&H);
776 mbedtls_mpi_free(&G);
777 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (ret != 0) {
780 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((-ret & ~0x7f) == 0) {
783 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
784 }
785 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 }
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000789}
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
793/*
794 * Check a public RSA key
795 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000797{
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
799 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100800 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
803 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100804 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
807 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
808 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
809 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
810 }
811
812 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000813}
814
815/*
Hanno Becker705fc682017-10-10 17:57:02 +0100816 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000817 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000819{
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
821 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
822 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 }
Paul Bakker48377d92013-08-30 12:06:24 +0200824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
826 &ctx->D, &ctx->E, NULL, NULL) != 0) {
827 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000828 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000829
Hanno Beckerb269a852017-08-25 08:03:21 +0100830#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
832 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
833 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100834 }
835#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000838}
839
840/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100841 * Check if contexts holding a public and private key match
842 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
844 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100845{
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
847 mbedtls_rsa_check_privkey(prv) != 0) {
848 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100849 }
850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
852 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
853 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100854 }
855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100857}
858
859/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 * Do an RSA public key operation
861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
863 const unsigned char *input,
864 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000865{
Janos Follath24eed8d2019-11-22 13:21:35 +0000866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000867 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
871 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
872 }
Hanno Becker705fc682017-10-10 17:57:02 +0100873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000875
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200876#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
878 return ret;
879 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200880#endif
881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200885 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
886 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 }
888
889 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
891 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
893cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
896 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
897 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100898#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (ret != 0) {
903 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
904 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000907}
908
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200909/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200910 * Generate or update blinding values, see section 10 of:
911 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200912 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200913 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200914 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
916 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200917{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200918 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200919 mbedtls_mpi R;
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200924 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
926 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
927 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
928 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200929
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200930 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200931 }
932
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200933 /* Unblinding value: Vf = random number, invertible mod N */
934 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200936 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
937 goto cleanup;
938 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&ctx->Vf, ctx->len - 1, f_rng, p_rng));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200941
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200942 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
944 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
945 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200946
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200947 /* At this point, Vi is invertible mod N if and only if both Vf and R
948 * are invertible mod N. If one of them isn't, we don't need to know
949 * which one, we just loop and choose new values for both of them.
950 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
952 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200953 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500957
958 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
960 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200961
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200962 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200963 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200965
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200966
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200967cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200971}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200972
Paul Bakker5121ce52009-01-03 21:22:43 +0000973/*
Janos Follathe81102e2017-03-22 13:38:28 +0000974 * Exponent blinding supposed to prevent side-channel attacks using multiple
975 * traces of measurements to recover the RSA key. The more collisions are there,
976 * the more bits of the key can be recovered. See [3].
977 *
978 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800979 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000980 *
981 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800982 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000983 *
984 * (With the currently (as of 2017 April) known best algorithms breaking 2048
985 * bit RSA requires approximately as much time as trying out 2^112 random keys.
986 * Thus in this sense with 28 byte blinding the security is not reduced by
987 * side-channel attacks like the one in [3])
988 *
989 * This countermeasure does not help if the key recovery is possible with a
990 * single trace.
991 */
992#define RSA_EXPONENT_BLINDING 28
993
994/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 * Do an RSA private key operation
996 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
998 int (*f_rng)(void *, unsigned char *, size_t),
999 void *p_rng,
1000 const unsigned char *input,
1001 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001002{
Janos Follath24eed8d2019-11-22 13:21:35 +00001003 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001004 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001005
1006 /* Temporary holding the result */
1007 mbedtls_mpi T;
1008
1009 /* Temporaries holding P-1, Q-1 and the
1010 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001011 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001012
1013#if !defined(MBEDTLS_RSA_NO_CRT)
1014 /* Temporaries holding the results mod p resp. mod q. */
1015 mbedtls_mpi TP, TQ;
1016
1017 /* Temporaries holding the blinded exponents for
1018 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001019 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001020
1021 /* Pointers to actual exponents to be used - either the unblinded
1022 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +00001023 mbedtls_mpi *DP = &ctx->DP;
1024 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +01001025#else
1026 /* Temporary holding the blinded exponent (if used). */
1027 mbedtls_mpi D_blind;
1028
1029 /* Pointer to actual exponent to be used - either the unblinded
1030 * or the blinded one, depending on the presence of a PRNG. */
1031 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +01001032#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001033
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001034 /* Temporaries holding the initial input and the double
1035 * checked result; should be the same in the end. */
1036 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if (f_rng == NULL) {
1039 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1040 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (rsa_check_context(ctx, 1 /* private key checks */,
1043 1 /* blinding on */) != 0) {
1044 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001045 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001046
Hanno Becker06811ce2017-05-03 15:10:34 +01001047#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1049 return ret;
1050 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001051#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001052
Hanno Becker06811ce2017-05-03 15:10:34 +01001053 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 mbedtls_mpi_init(&P1);
1057 mbedtls_mpi_init(&Q1);
1058 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001059
Janos Follathe81102e2017-03-22 13:38:28 +00001060#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001062#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 mbedtls_mpi_init(&DP_blind);
1064 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001065#endif
1066
Hanno Becker06811ce2017-05-03 15:10:34 +01001067#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001069#endif
1070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 mbedtls_mpi_init(&I);
1072 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001073
1074 /* End of MPI initialization */
1075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1077 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001078 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1079 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 }
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +01001083
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001084 /*
1085 * Blinding
1086 * T = T * Vi mod N
1087 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1089 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1090 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001091
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001092 /*
1093 * Exponent blinding
1094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1096 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001097
Janos Follathf9203b42017-03-22 15:13:15 +00001098#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001099 /*
1100 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1103 f_rng, p_rng));
1104 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1105 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1106 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +00001107
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001108 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001109#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001110 /*
1111 * DP_blind = ( P - 1 ) * R + DP
1112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1114 f_rng, p_rng));
1115 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1116 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1117 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001118
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001119 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001120
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001121 /*
1122 * DQ_blind = ( Q - 1 ) * R + DQ
1123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1125 f_rng, p_rng));
1126 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1127 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1128 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001129
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001130 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001131#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001135#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001136 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001137 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001138 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001139 * TP = input ^ dP mod P
1140 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001141 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1144 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001145
1146 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001147 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1150 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1151 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
1153 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001154 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1157 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001159
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001160 /*
1161 * Unblind
1162 * T = T * Vf mod N
1163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1165 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
Hanno Becker2dec5e82017-10-03 07:49:52 +01001167 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1169 &ctx->N, &ctx->RN));
1170 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001171 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1172 goto cleanup;
1173 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001174
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
1178cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1181 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1182 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001183#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 mbedtls_mpi_free(&P1);
1186 mbedtls_mpi_free(&Q1);
1187 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001188
Janos Follathe81102e2017-03-22 13:38:28 +00001189#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001191#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 mbedtls_mpi_free(&DP_blind);
1193 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001194#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001197
1198#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001200#endif
1201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 mbedtls_mpi_free(&C);
1203 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (ret != 0 && ret >= -0x007f) {
1206 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001210}
1211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001213/**
1214 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1215 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001216 * \param dst buffer to mask
1217 * \param dlen length of destination buffer
1218 * \param src source of the mask generation
1219 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001220 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1223 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001224{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001225 unsigned char counter[4];
1226 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001227 unsigned int hlen;
1228 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001229 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001230 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001231 const mbedtls_md_info_t *md_info;
1232 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 mbedtls_md_init(&md_ctx);
1235 md_info = mbedtls_md_info_from_type(md_alg);
1236 if (md_info == NULL) {
1237 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1238 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 mbedtls_md_init(&md_ctx);
1241 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001242 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 memset(mask, 0, sizeof(mask));
1248 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001249
Simon Butcher02037452016-03-01 21:19:12 +00001250 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001251 p = dst;
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001254 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001256 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001260 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 }
1262 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001263 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 }
1265 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001266 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 }
1268 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001269 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001273 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001275
1276 counter[3]++;
1277
1278 dlen -= use_len;
1279 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001280
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001281exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001286}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001287
1288/**
1289 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1290 *
1291 * \param hash the input hash
1292 * \param hlen length of the input hash
1293 * \param salt the input salt
1294 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001295 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001296 * \param md_alg message digest to use
1297 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001298static int hash_mprime(const unsigned char *hash, size_t hlen,
1299 const unsigned char *salt, size_t slen,
1300 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001301{
1302 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001303
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001304 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1308 if (md_info == NULL) {
1309 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1310 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 mbedtls_md_init(&md_ctx);
1313 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001314 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 }
1316 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001317 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 }
1319 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001320 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 }
1322 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001323 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 }
1325 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001326 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 }
1328 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001329 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001331
1332exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001336}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001337
1338/**
1339 * Compute a hash.
1340 *
1341 * \param md_alg algorithm to use
1342 * \param input input message to hash
1343 * \param ilen input length
1344 * \param output the output buffer - must be large enough for \p md_alg
1345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346static int compute_hash(mbedtls_md_type_t md_alg,
1347 const unsigned char *input, size_t ilen,
1348 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001349{
1350 const mbedtls_md_info_t *md_info;
1351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 md_info = mbedtls_md_info_from_type(md_alg);
1353 if (md_info == NULL) {
1354 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1355 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001358}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001362/*
1363 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1364 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001365int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1366 int (*f_rng)(void *, unsigned char *, size_t),
1367 void *p_rng,
1368 const unsigned char *label, size_t label_len,
1369 size_t ilen,
1370 const unsigned char *input,
1371 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001372{
1373 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001375 unsigned char *p = output;
1376 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if (f_rng == NULL) {
1379 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1380 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001381
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001382 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if (hlen == 0) {
1384 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1385 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001386
1387 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001388
Simon Butcher02037452016-03-01 21:19:12 +00001389 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1391 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1392 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001395
1396 *p++ = 0;
1397
Simon Butcher02037452016-03-01 21:19:12 +00001398 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1400 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1401 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001402
1403 p += hlen;
1404
Simon Butcher02037452016-03-01 21:19:12 +00001405 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1407 if (ret != 0) {
1408 return ret;
1409 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001410 p += hlen;
1411 p += olen - 2 * hlen - 2 - ilen;
1412 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (ilen != 0) {
1414 memcpy(p, input, ilen);
1415 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001416
Simon Butcher02037452016-03-01 21:19:12 +00001417 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001419 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 return ret;
1421 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001422
Simon Butcher02037452016-03-01 21:19:12 +00001423 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001425 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 return ret;
1427 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001434/*
1435 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1436 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001437int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1438 int (*f_rng)(void *, unsigned char *, size_t),
1439 void *p_rng, size_t ilen,
1440 const unsigned char *input,
1441 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001442{
1443 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001445 unsigned char *p = output;
1446
Paul Bakkerb3869132013-02-28 17:21:01 +01001447 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001448
Simon Butcher02037452016-03-01 21:19:12 +00001449 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if (ilen + 11 < ilen || olen < ilen + 11) {
1451 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1452 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001453
1454 nb_pad = olen - 3 - ilen;
1455
1456 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (f_rng == NULL) {
1459 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1460 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001461
1462 *p++ = MBEDTLS_RSA_CRYPT;
1463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001465 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001466
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001467 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 ret = f_rng(p_rng, p, 1);
1469 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001470
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001471 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (rng_dl == 0 || ret != 0) {
1473 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1474 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001475
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001476 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001477 }
1478
1479 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if (ilen != 0) {
1481 memcpy(p, input, ilen);
1482 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001487
Paul Bakker5121ce52009-01-03 21:22:43 +00001488/*
1489 * Add the message padding, then do an RSA operation
1490 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1492 int (*f_rng)(void *, unsigned char *, size_t),
1493 void *p_rng,
1494 size_t ilen,
1495 const unsigned char *input,
1496 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001497{
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#if defined(MBEDTLS_PKCS1_V15)
1500 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1502 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001503#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_PKCS1_V21)
1506 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1508 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
1511 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001513 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001514}
1515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001517/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001518 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1521 int (*f_rng)(void *, unsigned char *, size_t),
1522 void *p_rng,
1523 const unsigned char *label, size_t label_len,
1524 size_t *olen,
1525 const unsigned char *input,
1526 unsigned char *output,
1527 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001528{
Janos Follath24eed8d2019-11-22 13:21:35 +00001529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001530 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001531 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001532 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001534 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001535 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001536
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001537 /*
1538 * Parameters sanity checks
1539 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1541 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001543
1544 ilen = ctx->len;
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 if (ilen < 16 || ilen > sizeof(buf)) {
1547 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001549
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001550 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 if (hlen == 0) {
1552 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1553 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001554
Janos Follathc17cda12016-02-11 11:08:18 +00001555 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (2 * hlen + 2 > ilen) {
1557 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1558 }
Janos Follathc17cda12016-02-11 11:08:18 +00001559
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001560 /*
1561 * RSA operation
1562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001566 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001569 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001570 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001571 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001572 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001574 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 /* DB: Apply dbMask to maskedDB */
1576 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001577 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001578 goto cleanup;
1579 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001580
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001581 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1583 label, label_len, lhash);
1584 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001585 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001587
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001588 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001589 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001590 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001591 p = buf;
1592
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001593 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001594
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001595 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001596
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001597 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001598 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001599 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001600
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001601 /* Get zero-padding len, but always read till end of buffer
1602 * (minus one, for the 01 byte) */
1603 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001604 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001606 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1607 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001608 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001609
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001610 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001611 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001612
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001613 /*
1614 * The only information "leaked" is whether the padding was correct or not
1615 * (eg, no data is copied if it was not correct). This meets the
1616 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1617 * the different error conditions.
1618 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001619 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001620 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1621 goto cleanup;
1622 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001623
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001624 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001625 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1626 goto cleanup;
1627 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001628
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001629 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 if (*olen != 0) {
1631 memcpy(output, p, *olen);
1632 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001633 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001634
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001635cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 mbedtls_platform_zeroize(buf, sizeof(buf));
1637 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001640}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001644/*
1645 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1646 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001647int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1648 int (*f_rng)(void *, unsigned char *, size_t),
1649 void *p_rng,
1650 size_t *olen,
1651 const unsigned char *input,
1652 unsigned char *output,
1653 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001654{
1655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1656 size_t ilen;
1657 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1658
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001659 ilen = ctx->len;
1660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1662 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1663 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (ilen < 16 || ilen > sizeof(buf)) {
1666 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1667 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001672 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1676 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001677
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001678cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
1685/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001686 * Do an RSA operation, then remove the message padding
1687 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001688int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1689 int (*f_rng)(void *, unsigned char *, size_t),
1690 void *p_rng,
1691 size_t *olen,
1692 const unsigned char *input,
1693 unsigned char *output,
1694 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001695{
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#if defined(MBEDTLS_PKCS1_V15)
1698 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1700 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001701#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#if defined(MBEDTLS_PKCS1_V21)
1704 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1706 olen, input, output,
1707 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001708#endif
1709
1710 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001712 }
1713}
1714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001716static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
1717 int (*f_rng)(void *, unsigned char *, size_t),
1718 void *p_rng,
1719 mbedtls_md_type_t md_alg,
1720 unsigned int hashlen,
1721 const unsigned char *hash,
1722 int saltlen,
1723 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001724{
1725 size_t olen;
1726 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001727 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001728 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001729 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001730 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001731 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001734 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001736
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 if (f_rng == NULL) {
1738 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1739 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001740
1741 olen = ctx->len;
1742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001744 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001745 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (exp_hashlen == 0) {
1747 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1748 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 if (hashlen != exp_hashlen) {
1751 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1752 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001753 }
1754
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001755 hash_id = (mbedtls_md_type_t) ctx->hash_id;
1756 if (hash_id == MBEDTLS_MD_NONE) {
1757 hash_id = md_alg;
1758 }
1759 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 if (hlen == 0) {
1761 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1762 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001763
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1765 /* Calculate the largest possible salt length, up to the hash size.
1766 * Normally this is the hash length, which is the maximum salt length
1767 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1768 * enough room, use the maximum salt length that fits. The constraint is
1769 * that the hash length plus the salt length plus 2 bytes must be at most
1770 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1771 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001772 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (olen < hlen + min_slen + 2) {
1774 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1775 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001776 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001778 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 }
1780 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1781 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1782 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001783 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001784 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001787
Simon Butcher02037452016-03-01 21:19:12 +00001788 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001790 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001791 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001792
1793 /* Generate salt of length slen in place in the encoded message */
1794 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1796 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1797 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001798
Paul Bakkerb3869132013-02-28 17:21:01 +01001799 p += slen;
1800
Simon Butcher02037452016-03-01 21:19:12 +00001801 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001802 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (ret != 0) {
1804 return ret;
1805 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001806
Simon Butcher02037452016-03-01 21:19:12 +00001807 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001809 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001811
Simon Butcher02037452016-03-01 21:19:12 +00001812 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001813 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 if (ret != 0) {
1815 return ret;
1816 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1819 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001820
1821 p += hlen;
1822 *p++ = 0xBC;
1823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001825}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001826
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001827static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1828 int (*f_rng)(void *, unsigned char *, size_t),
1829 void *p_rng,
1830 mbedtls_md_type_t md_alg,
1831 unsigned int hashlen,
1832 const unsigned char *hash,
1833 int saltlen,
1834 unsigned char *sig)
1835{
1836 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1837 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1838 }
1839 if (ctx->hash_id == MBEDTLS_MD_NONE) {
1840 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1841 }
1842 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
1843 sig);
1844}
1845
1846int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
1847 int (*f_rng)(void *, unsigned char *, size_t),
1848 void *p_rng,
1849 mbedtls_md_type_t md_alg,
1850 unsigned int hashlen,
1851 const unsigned char *hash,
1852 unsigned char *sig)
1853{
1854 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
1855 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
1856}
1857
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001858/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001859 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1860 * the option to pass in the salt length.
1861 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001862int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1863 int (*f_rng)(void *, unsigned char *, size_t),
1864 void *p_rng,
1865 mbedtls_md_type_t md_alg,
1866 unsigned int hashlen,
1867 const unsigned char *hash,
1868 int saltlen,
1869 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001870{
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1872 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001873}
1874
Cédric Meuterf3fab332020-04-25 11:30:45 +02001875/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001876 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1877 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001878int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1879 int (*f_rng)(void *, unsigned char *, size_t),
1880 void *p_rng,
1881 mbedtls_md_type_t md_alg,
1882 unsigned int hashlen,
1883 const unsigned char *hash,
1884 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001885{
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1887 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001888}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001892/*
1893 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1894 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001895
1896/* Construct a PKCS v1.5 encoding of a hashed message
1897 *
1898 * This is used both for signature generation and verification.
1899 *
1900 * Parameters:
1901 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001902 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001903 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001904 * - hash: Buffer containing the hashed message or the raw data.
1905 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001906 * - dst: Buffer to hold the encoded message.
1907 *
1908 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001909 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001910 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001911 *
1912 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001913static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1914 unsigned int hashlen,
1915 const unsigned char *hash,
1916 size_t dst_len,
1917 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001918{
1919 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001920 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001921 unsigned char *p = dst;
1922 const char *oid = NULL;
1923
1924 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001926 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 if (md_size == 0) {
1928 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1929 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1932 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1933 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001934
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 if (hashlen != md_size) {
1936 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1937 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001938
1939 /* Double-check that 8 + hashlen + oid_size can be used as a
1940 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001942 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 10 + hashlen + oid_size < 10 + hashlen) {
1944 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1945 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001946
1947 /*
1948 * Static bounds check:
1949 * - Need 10 bytes for five tag-length pairs.
1950 * (Insist on 1-byte length encodings to protect against variants of
1951 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1952 * - Need hashlen bytes for hash
1953 * - Need oid_size bytes for hash alg OID.
1954 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (nb_pad < 10 + hashlen + oid_size) {
1956 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1957 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001958 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 } else {
1960 if (nb_pad < hashlen) {
1961 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1962 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001963
1964 nb_pad -= hashlen;
1965 }
1966
Hanno Becker2b2f8982017-09-27 17:10:03 +01001967 /* Need space for signature header and padding delimiter (3 bytes),
1968 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (nb_pad < 3 + 8) {
1970 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1971 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001972 nb_pad -= 3;
1973
1974 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001975 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001976
1977 /* Write signature header and padding */
1978 *p++ = 0;
1979 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001981 p += nb_pad;
1982 *p++ = 0;
1983
1984 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if (md_alg == MBEDTLS_MD_NONE) {
1986 memcpy(p, hash, hashlen);
1987 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001988 }
1989
1990 /* Signing hashed data, add corresponding ASN.1 structure
1991 *
1992 * DigestInfo ::= SEQUENCE {
1993 * digestAlgorithm DigestAlgorithmIdentifier,
1994 * digest Digest }
1995 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1996 * Digest ::= OCTET STRING
1997 *
1998 * Schematic:
1999 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2000 * TAG-NULL + LEN [ NULL ] ]
2001 * TAG-OCTET + LEN [ HASH ] ]
2002 */
2003 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002005 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002007 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002008 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002010 p += oid_size;
2011 *p++ = MBEDTLS_ASN1_NULL;
2012 *p++ = 0x00;
2013 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002014 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002016 p += hashlen;
2017
2018 /* Just a sanity-check, should be automatic
2019 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 if (p != dst + dst_len) {
2021 mbedtls_platform_zeroize(dst, dst_len);
2022 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002023 }
2024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002026}
2027
Paul Bakkerb3869132013-02-28 17:21:01 +01002028/*
2029 * Do an RSA operation to sign the message digest
2030 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002031int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2032 int (*f_rng)(void *, unsigned char *, size_t),
2033 void *p_rng,
2034 mbedtls_md_type_t md_alg,
2035 unsigned int hashlen,
2036 const unsigned char *hash,
2037 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002038{
Janos Follath24eed8d2019-11-22 13:21:35 +00002039 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002040 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002043 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2047 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2048 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002049
Hanno Beckerfdf38032017-09-06 12:35:55 +01002050 /*
2051 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2052 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2055 ctx->len, sig)) != 0) {
2056 return ret;
2057 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002058
Hanno Beckerfdf38032017-09-06 12:35:55 +01002059 /* Private key operation
2060 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002061 * In order to prevent Lenstra's attack, make the signature in a
2062 * temporary buffer and check it before returning it.
2063 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 sig_try = mbedtls_calloc(1, ctx->len);
2066 if (sig_try == NULL) {
2067 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002068 }
2069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 verif = mbedtls_calloc(1, ctx->len);
2071 if (verif == NULL) {
2072 mbedtls_free(sig_try);
2073 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2074 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2077 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2078
2079 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002080 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2081 goto cleanup;
2082 }
2083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002085
2086cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002087 mbedtls_zeroize_and_free(sig_try, ctx->len);
2088 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if (ret != 0) {
2091 memset(sig, '!', ctx->len);
2092 }
2093 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002094}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002096
2097/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 * Do an RSA operation to sign the message digest
2099 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002100int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2101 int (*f_rng)(void *, unsigned char *, size_t),
2102 void *p_rng,
2103 mbedtls_md_type_t md_alg,
2104 unsigned int hashlen,
2105 const unsigned char *hash,
2106 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002107{
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002109 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#if defined(MBEDTLS_PKCS1_V15)
2114 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2116 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002117#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119#if defined(MBEDTLS_PKCS1_V21)
2120 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2122 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002123#endif
2124
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002128}
2129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002131/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002132 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002134int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2135 mbedtls_md_type_t md_alg,
2136 unsigned int hashlen,
2137 const unsigned char *hash,
2138 mbedtls_md_type_t mgf1_hash_id,
2139 int expected_salt_len,
2140 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002141{
Janos Follath24eed8d2019-11-22 13:21:35 +00002142 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002143 size_t siglen;
2144 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002145 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002146 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002147 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002148 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002152 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002154
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 siglen = ctx->len;
2156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 if (siglen < 16 || siglen > sizeof(buf)) {
2158 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2159 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (ret != 0) {
2164 return ret;
2165 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002166
2167 p = buf;
2168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (buf[siglen - 1] != 0xBC) {
2170 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002171 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 if (md_alg != MBEDTLS_MD_NONE) {
2174 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002175 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (exp_hashlen == 0) {
2177 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2178 }
2179
2180 if (hashlen != exp_hashlen) {
2181 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2182 }
2183 }
2184
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002185 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (hlen == 0) {
2187 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2188 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002189
Simon Butcher02037452016-03-01 21:19:12 +00002190 /*
2191 * Note: EMSA-PSS verification is over the length of N - 1 bits
2192 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 if (buf[0] >> (8 - siglen * 8 + msb)) {
2196 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2197 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002198
Simon Butcher02037452016-03-01 21:19:12 +00002199 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002201 p++;
2202 siglen -= 1;
2203 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if (siglen < hlen + 2) {
2206 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2207 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002208 hash_start = p + siglen - hlen - 1;
2209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2211 if (ret != 0) {
2212 return ret;
2213 }
Paul Bakker02303e82013-01-03 11:08:31 +01002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002218 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 if (*p++ != 0x01) {
2222 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2223 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002224
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002225 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2228 observed_salt_len != (size_t) expected_salt_len) {
2229 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002230 }
2231
Simon Butcher02037452016-03-01 21:19:12 +00002232 /*
2233 * Generate H = Hash( M' )
2234 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2236 result, mgf1_hash_id);
2237 if (ret != 0) {
2238 return ret;
2239 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if (memcmp(hash_start, result, hlen) != 0) {
2242 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2243 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002246}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002247
2248/*
2249 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2250 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002251int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2252 mbedtls_md_type_t md_alg,
2253 unsigned int hashlen,
2254 const unsigned char *hash,
2255 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002256{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002257 mbedtls_md_type_t mgf1_hash_id;
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
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002264 : md_alg;
2265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2267 md_alg, hashlen, hash,
2268 mgf1_hash_id,
2269 MBEDTLS_RSA_SALT_LEN_ANY,
2270 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002271
2272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002276/*
2277 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002279int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2280 mbedtls_md_type_t md_alg,
2281 unsigned int hashlen,
2282 const unsigned char *hash,
2283 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002284{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002285 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002286 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002287 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002290 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002292
2293 sig_len = ctx->len;
2294
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002295 /*
2296 * Prepare expected PKCS1 v1.5 encoding of hash.
2297 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2300 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002301 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2302 goto cleanup;
2303 }
2304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2306 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002307 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002309
2310 /*
2311 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2312 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 ret = mbedtls_rsa_public(ctx, sig, encoded);
2315 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002316 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002318
Simon Butcher02037452016-03-01 21:19:12 +00002319 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002320 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002321 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2324 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002325 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2326 goto cleanup;
2327 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002328
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002329cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002332 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002333 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002336 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002337 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002340}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002342
2343/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002344 * Do an RSA operation and check the message digest
2345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002346int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2347 mbedtls_md_type_t md_alg,
2348 unsigned int hashlen,
2349 const unsigned char *hash,
2350 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002351{
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002353 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357#if defined(MBEDTLS_PKCS1_V15)
2358 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2360 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002361#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363#if defined(MBEDTLS_PKCS1_V21)
2364 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2366 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002367#endif
2368
2369 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002371 }
2372}
2373
2374/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002375 * Copy the components of an RSA key
2376 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002377int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002378{
Janos Follath24eed8d2019-11-22 13:21:35 +00002379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002380
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002381 dst->len = src->len;
2382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2384 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2387 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2388 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002389
2390#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2392 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2393 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2394 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2395 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002396#endif
2397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2401 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002402
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002403 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002404 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002405
2406cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (ret != 0) {
2408 mbedtls_rsa_free(dst);
2409 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002412}
2413
2414/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 * Free the components of an RSA key
2416 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002417void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002418{
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002420 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 mbedtls_mpi_free(&ctx->Vi);
2424 mbedtls_mpi_free(&ctx->Vf);
2425 mbedtls_mpi_free(&ctx->RN);
2426 mbedtls_mpi_free(&ctx->D);
2427 mbedtls_mpi_free(&ctx->Q);
2428 mbedtls_mpi_free(&ctx->P);
2429 mbedtls_mpi_free(&ctx->E);
2430 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002431
Hanno Becker33c30a02017-08-23 07:00:22 +01002432#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 mbedtls_mpi_free(&ctx->RQ);
2434 mbedtls_mpi_free(&ctx->RP);
2435 mbedtls_mpi_free(&ctx->QP);
2436 mbedtls_mpi_free(&ctx->DQ);
2437 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002438#endif /* MBEDTLS_RSA_NO_CRT */
2439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002441 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (ctx->ver != 0) {
2443 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002444 ctx->ver = 0;
2445 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002446#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002447}
2448
Hanno Beckerab377312017-08-23 16:24:51 +01002449#endif /* !MBEDTLS_RSA_ALT */
2450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
2454/*
2455 * Example RSA-1024 keypair, for test purposes
2456 */
2457#define KEY_LEN 128
2458
2459#define RSA_N "9292758453063D803DD603D5E777D788" \
2460 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2461 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2462 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2463 "93A89813FBF3C4F8066D2D800F7C38A8" \
2464 "1AE31942917403FF4946B0A83D3D3E05" \
2465 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2466 "5E94BB77B07507233A0BC7BAC8F90F79"
2467
2468#define RSA_E "10001"
2469
2470#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2471 "66CA472BC44D253102F8B4A9D3BFA750" \
2472 "91386C0077937FE33FA3252D28855837" \
2473 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2474 "DF79C5CE07EE72C7F123142198164234" \
2475 "CABB724CF78B8173B9F880FC86322407" \
2476 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2477 "071513A1E85B5DFA031F21ECAE91A34D"
2478
2479#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2480 "2C01CAD19EA484A87EA4377637E75500" \
2481 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2482 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2483
2484#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2485 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2486 "910E4168387E3C30AA1E00C339A79508" \
2487 "8452DD96A9A5EA5D9DCA68DA636032AF"
2488
Paul Bakker5121ce52009-01-03 21:22:43 +00002489#define PT_LEN 24
2490#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2491 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002494static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002495{
gufe44c2620da2020-08-03 17:56:50 +02002496#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002497 size_t i;
2498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002500 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 }
Paul Bakker545570e2010-07-18 09:00:25 +00002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002504 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002506#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002508 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002512#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002515}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002517
Paul Bakker5121ce52009-01-03 21:22:43 +00002518/*
2519 * Checkup routine
2520 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002521int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002522{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002523 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002525 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 unsigned char rsa_plaintext[PT_LEN];
2528 unsigned char rsa_decrypted[PT_LEN];
2529 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002530#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002531 unsigned char sha1sum[20];
2532#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Hanno Becker3a701162017-08-22 13:52:43 +01002534 mbedtls_mpi K;
2535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 mbedtls_mpi_init(&K);
2537 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2540 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2541 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2542 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2543 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2544 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2545 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2546 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2547 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2548 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 if (verbose != 0) {
2553 mbedtls_printf(" RSA key validation: ");
2554 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2557 mbedtls_rsa_check_privkey(&rsa) != 0) {
2558 if (verbose != 0) {
2559 mbedtls_printf("failed\n");
2560 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
Hanno Becker5bc87292017-05-03 15:09:31 +01002562 ret = 1;
2563 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 }
2565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 if (verbose != 0) {
2567 mbedtls_printf("passed\n PKCS#1 encryption : ");
2568 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2573 PT_LEN, rsa_plaintext,
2574 rsa_ciphertext) != 0) {
2575 if (verbose != 0) {
2576 mbedtls_printf("failed\n");
2577 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Hanno Becker5bc87292017-05-03 15:09:31 +01002579 ret = 1;
2580 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 }
2582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 if (verbose != 0) {
2584 mbedtls_printf("passed\n PKCS#1 decryption : ");
2585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2588 &len, rsa_ciphertext, rsa_decrypted,
2589 sizeof(rsa_decrypted)) != 0) {
2590 if (verbose != 0) {
2591 mbedtls_printf("failed\n");
2592 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
Hanno Becker5bc87292017-05-03 15:09:31 +01002594 ret = 1;
2595 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 }
2597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2599 if (verbose != 0) {
2600 mbedtls_printf("failed\n");
2601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Hanno Becker5bc87292017-05-03 15:09:31 +01002603 ret = 1;
2604 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 }
2606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 if (verbose != 0) {
2608 mbedtls_printf("passed\n");
2609 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002610
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002611#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (verbose != 0) {
2613 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002616 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2617 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (verbose != 0) {
2619 mbedtls_printf("failed\n");
2620 }
2621
2622 return 1;
2623 }
2624
2625 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2626 MBEDTLS_MD_SHA1, 20,
2627 sha1sum, rsa_ciphertext) != 0) {
2628 if (verbose != 0) {
2629 mbedtls_printf("failed\n");
2630 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Hanno Becker5bc87292017-05-03 15:09:31 +01002632 ret = 1;
2633 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 }
2635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 if (verbose != 0) {
2637 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2638 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2641 sha1sum, rsa_ciphertext) != 0) {
2642 if (verbose != 0) {
2643 mbedtls_printf("failed\n");
2644 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Hanno Becker5bc87292017-05-03 15:09:31 +01002646 ret = 1;
2647 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002648 }
2649
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 if (verbose != 0) {
2651 mbedtls_printf("passed\n");
2652 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002653#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 if (verbose != 0) {
2656 mbedtls_printf("\n");
2657 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002658
Paul Bakker3d8fb632014-04-17 12:42:41 +02002659cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 mbedtls_mpi_free(&K);
2661 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002663 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002666}
2667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670#endif /* MBEDTLS_RSA_C */