blob: f57909b71bac39cf4c908cd220e14a1f700ef51d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgmane3c05852023-11-03 12:21:36 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
Hanno Becker74716312017-10-02 10:00:37 +01007
Paul Bakker5121ce52009-01-03 21:22:43 +00008/*
Simon Butcherbdae02c2016-01-20 00:44:42 +00009 * The following sources were referenced in the design of this implementation
10 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000011 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000012 * [1] A method for obtaining digital signatures and public-key cryptosystems
13 * R Rivest, A Shamir, and L Adleman
14 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
15 *
16 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
17 * Menezes, van Oorschot and Vanstone
18 *
Janos Follathe81102e2017-03-22 13:38:28 +000019 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
20 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
21 * Stefan Mangard
22 * https://arxiv.org/abs/1702.08719v2
23 *
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/rsa.h"
Janos Follathd6b09652023-11-21 09:33:54 +000031#include "bignum_core.h"
Chris Jones66a4cd42021-03-09 16:04:12 +000032#include "rsa_alt_helpers.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050034#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000035#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020036#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020037#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020038#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000039
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
gufe44c2620da2020-08-03 17:56:50 +020042#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000043#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010047
Dave Rodgman19e8cd02023-05-09 11:10:21 +010048
49#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
50
51/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
52 * operation (EME-PKCS1-v1_5 decoding).
53 *
54 * \note The return value from this function is a sensitive value
55 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
56 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
57 * is often a situation that an attacker can provoke and leaking which
58 * one is the result is precisely the information the attacker wants.
59 *
60 * \param input The input buffer which is the payload inside PKCS#1v1.5
61 * encryption padding, called the "encoded message EM"
62 * by the terminology.
63 * \param ilen The length of the payload in the \p input buffer.
64 * \param output The buffer for the payload, called "message M" by the
65 * PKCS#1 terminology. This must be a writable buffer of
66 * length \p output_max_len bytes.
67 * \param olen The address at which to store the length of
68 * the payload. This must not be \c NULL.
69 * \param output_max_len The length in bytes of the output buffer \p output.
70 *
71 * \return \c 0 on success.
72 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
73 * The output buffer is too small for the unpadded payload.
74 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
75 * The input doesn't contain properly formatted padding.
76 */
77static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
78 size_t ilen,
79 unsigned char *output,
80 size_t output_max_len,
81 size_t *olen)
82{
83 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
84 size_t i, plaintext_max_size;
85
86 /* The following variables take sensitive values: their value must
87 * not leak into the observable behavior of the function other than
88 * the designated outputs (output, olen, return value). Otherwise
89 * this would open the execution of the function to
90 * side-channel-based variants of the Bleichenbacher padding oracle
91 * attack. Potential side channels include overall timing, memory
92 * access patterns (especially visible to an adversary who has access
93 * to a shared memory cache), and branches (especially visible to
94 * an adversary who has access to a shared code cache or to a shared
95 * branch predictor). */
96 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +010097 mbedtls_ct_condition_t bad;
98 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +010099 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100100 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100101
102 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
103 : output_max_len;
104
105 /* Check and get padding length in constant time and constant
106 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100107 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100108
109
110 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
111 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100112 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100113
114 /* Read the whole buffer. Set pad_done to nonzero if we find
115 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100116 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100117 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100118 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100119 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100120 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100121 }
122
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100123 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100124 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100125
126 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100127 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100128
129 /* If the padding is valid, set plaintext_size to the number of
130 * remaining bytes after stripping the padding. If the padding
131 * is invalid, avoid leaking this fact through the size of the
132 * output: use the maximum message size that fits in the output
133 * buffer. Do it without branches to avoid leaking the padding
134 * validity through timing. RSA keys are small enough that all the
135 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100136 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100137 bad, (unsigned) plaintext_max_size,
138 (unsigned) (ilen - pad_count - 3));
139
140 /* Set output_too_large to 0 if the plaintext fits in the output
141 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100142 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100143 plaintext_max_size);
144
145 /* Set ret without branches to avoid timing attacks. Return:
146 * - INVALID_PADDING if the padding is bad (bad != 0).
147 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
148 * plaintext does not fit in the output buffer.
149 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100150 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100151 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100152 MBEDTLS_ERR_RSA_INVALID_PADDING,
153 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100154 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100155
156 /* If the padding is bad or the plaintext is too large, zero the
157 * data that we're about to copy to the output buffer.
158 * We need to copy the same amount of data
159 * from the same buffer whether the padding is good or not to
160 * avoid leaking the padding validity through overall timing or
161 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100162 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100163
164 /* If the plaintext is too large, truncate it to the buffer size.
165 * Copy anyway to avoid revealing the length through timing, because
166 * revealing the length is as bad as revealing the padding validity
167 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100168 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100169 (unsigned) plaintext_max_size,
170 (unsigned) plaintext_size);
171
172 /* Move the plaintext to the leftmost position where it can start in
173 * the working buffer, i.e. make it start plaintext_max_size from
174 * the end of the buffer. Do this with a memory access trace that
175 * does not depend on the plaintext size. After this move, the
176 * starting location of the plaintext is no longer sensitive
177 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100178 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
179 plaintext_max_size,
180 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100181
182 /* Finally copy the decrypted plaintext plus trailing zeros into the output
183 * buffer. If output_max_len is 0, then output may be an invalid pointer
184 * and the result of memcpy() would be undefined; prevent undefined
185 * behavior making sure to depend only on output_max_len (the size of the
186 * user-provided output buffer), which is independent from plaintext
187 * length, validity of padding, success of the decryption, and other
188 * secrets. */
189 if (output_max_len != 0) {
190 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
191 }
192
193 /* Report the amount of data we copied to the output buffer. In case
194 * of errors (bad padding or output too large), the value of *olen
195 * when this function returns is not specified. Making it equivalent
196 * to the good case limits the risks of leaking the padding validity. */
197 *olen = plaintext_size;
198
199 return ret;
200}
201
202#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
203
Hanno Beckera565f542017-10-11 11:00:19 +0100204#if !defined(MBEDTLS_RSA_ALT)
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
207 const mbedtls_mpi *N,
208 const mbedtls_mpi *P, const mbedtls_mpi *Q,
209 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100210{
Janos Follath24eed8d2019-11-22 13:21:35 +0000211 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
214 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
215 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
216 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
217 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
218 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100219 }
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if (N != NULL) {
222 ctx->len = mbedtls_mpi_size(&ctx->N);
223 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100226}
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
229 unsigned char const *N, size_t N_len,
230 unsigned char const *P, size_t P_len,
231 unsigned char const *Q, size_t Q_len,
232 unsigned char const *D, size_t D_len,
233 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100234{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000235 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (N != NULL) {
238 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
239 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100240 }
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (P != NULL) {
243 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
244 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if (Q != NULL) {
247 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
248 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (D != NULL) {
251 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
252 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (E != NULL) {
255 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
256 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100257
258cleanup:
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (ret != 0) {
261 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
262 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100265}
266
Hanno Becker705fc682017-10-10 17:57:02 +0100267/*
268 * Checks whether the context fields are set in such a way
269 * that the RSA primitives will be able to execute without error.
270 * It does *not* make guarantees for consistency of the parameters.
271 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
273 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100274{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100275#if !defined(MBEDTLS_RSA_NO_CRT)
276 /* blinding_needed is only used for NO_CRT to decide whether
277 * P,Q need to be present or not. */
278 ((void) blinding_needed);
279#endif
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
282 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
283 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000284 }
Hanno Becker705fc682017-10-10 17:57:02 +0100285
286 /*
287 * 1. Modular exponentiation needs positive, odd moduli.
288 */
289
290 /* Modular exponentiation wrt. N is always used for
291 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
293 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
294 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100295 }
296
297#if !defined(MBEDTLS_RSA_NO_CRT)
298 /* Modular exponentiation for P and Q is only
299 * used for private key operations and if CRT
300 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (is_priv &&
302 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
303 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
304 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
305 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
306 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100307 }
308#endif /* !MBEDTLS_RSA_NO_CRT */
309
310 /*
311 * 2. Exponents must be positive
312 */
313
314 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
316 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
317 }
Hanno Becker705fc682017-10-10 17:57:02 +0100318
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100319#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100320 /* For private key operations, use D or DP & DQ
321 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
323 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
324 }
Hanno Becker705fc682017-10-10 17:57:02 +0100325#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (is_priv &&
327 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
328 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
329 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100330 }
331#endif /* MBEDTLS_RSA_NO_CRT */
332
333 /* Blinding shouldn't make exponents negative either,
334 * so check that P, Q >= 1 if that hasn't yet been
335 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100336#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (is_priv && blinding_needed &&
338 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
339 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
340 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100341 }
342#endif
343
344 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100345 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100346#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (is_priv &&
348 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
349 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100350 }
351#endif
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100354}
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100357{
358 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500359 int have_N, have_P, have_Q, have_D, have_E;
360#if !defined(MBEDTLS_RSA_NO_CRT)
361 int have_DP, have_DQ, have_QP;
362#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500363 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
366 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
367 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
368 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
369 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500370
371#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
373 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
374 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500375#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100376
Hanno Becker617c1ae2017-08-23 14:11:24 +0100377 /*
378 * Check whether provided parameters are enough
379 * to deduce all others. The following incomplete
380 * parameter sets for private keys are supported:
381 *
382 * (1) P, Q missing.
383 * (2) D and potentially N missing.
384 *
385 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100386
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500387 n_missing = have_P && have_Q && have_D && have_E;
388 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
389 d_missing = have_P && have_Q && !have_D && have_E;
390 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100391
392 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500393 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (!is_priv && !is_pub) {
396 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
397 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100398
399 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100400 * Step 1: Deduce N if P, Q are provided.
401 */
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (!have_N && have_P && have_Q) {
404 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
405 &ctx->Q)) != 0) {
406 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100407 }
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100410 }
411
412 /*
413 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100414 */
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (pq_missing) {
417 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
418 &ctx->P, &ctx->Q);
419 if (ret != 0) {
420 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
421 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 } else if (d_missing) {
424 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
425 &ctx->Q,
426 &ctx->E,
427 &ctx->D)) != 0) {
428 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100429 }
430 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100431
Hanno Becker617c1ae2017-08-23 14:11:24 +0100432 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100433 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100434 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100435 */
436
Hanno Becker23344b52017-08-23 07:43:27 +0100437#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (is_priv && !(have_DP && have_DQ && have_QP)) {
439 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
440 &ctx->DP, &ctx->DQ, &ctx->QP);
441 if (ret != 0) {
442 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
443 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444 }
Hanno Becker23344b52017-08-23 07:43:27 +0100445#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100446
447 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100448 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100449 */
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100452}
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
455 unsigned char *N, size_t N_len,
456 unsigned char *P, size_t P_len,
457 unsigned char *Q, size_t Q_len,
458 unsigned char *D, size_t D_len,
459 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100460{
461 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500462 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100463
464 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500465 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
467 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
468 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
469 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
470 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100473 /* If we're trying to export private parameters for a public key,
474 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (P != NULL || Q != NULL || D != NULL) {
476 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
477 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100478
479 }
480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (N != NULL) {
482 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
483 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (P != NULL) {
486 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
487 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (Q != NULL) {
490 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
491 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (D != NULL) {
494 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
495 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (E != NULL) {
498 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
499 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100500
501cleanup:
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100504}
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
507 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
508 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100509{
Janos Follath24eed8d2019-11-22 13:21:35 +0000510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500511 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100512
513 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500514 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
516 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
517 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
518 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
519 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100522 /* If we're trying to export private parameters for a public key,
523 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (P != NULL || Q != NULL || D != NULL) {
525 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
526 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100527
528 }
529
530 /* Export all requested core parameters. */
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
533 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
534 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
535 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
536 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
537 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100541}
542
543/*
544 * Export CRT parameters
545 * This must also be implemented if CRT is not used, for being able to
546 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
547 * can be used in this case.
548 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
550 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100551{
Janos Follath24eed8d2019-11-22 13:21:35 +0000552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500553 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100554
555 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500556 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
558 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
559 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
560 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
561 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (!is_priv) {
564 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
565 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100566
Hanno Beckerdc95c892017-08-23 06:57:02 +0100567#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100568 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
570 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
571 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
572 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100573 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100574#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
576 DP, DQ, QP)) != 0) {
577 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100578 }
579#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100582}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100583
Paul Bakker5121ce52009-01-03 21:22:43 +0000584/*
585 * Initialize an RSA context
586 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100587void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000588{
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Ronald Cronc1905a12021-06-05 11:11:14 +0200591 ctx->padding = MBEDTLS_RSA_PKCS_V15;
592 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100595 /* Set ctx->ver to nonzero to indicate that the mutex has been
596 * initialized and will need to be freed. */
597 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200599#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000600}
601
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100602/*
603 * Set padding for an existing RSA context
604 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100605int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
606 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100607{
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200609#if defined(MBEDTLS_PKCS1_V15)
610 case MBEDTLS_RSA_PKCS_V15:
611 break;
612#endif
613
614#if defined(MBEDTLS_PKCS1_V21)
615 case MBEDTLS_RSA_PKCS_V21:
616 break;
617#endif
618 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200620 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200621
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200622#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
624 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200625 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200626 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return MBEDTLS_ERR_RSA_INVALID_PADDING;
628 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200629 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200630#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500631
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100632 ctx->padding = padding;
633 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100636}
637
Hanno Becker617c1ae2017-08-23 14:11:24 +0100638/*
Yanray Wang83548b52023-03-15 16:46:34 +0800639 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800640 */
641int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
642{
Yanray Wang644b9012023-03-15 16:50:31 +0800643 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +0800644}
645
646/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800647 * Get hash identifier of mbedtls_md_type_t type
648 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800649int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +0800650{
Yanray Wang644b9012023-03-15 16:50:31 +0800651 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +0800652}
653
654/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100655 * Get length in bytes of RSA modulus
656 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100657size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100658{
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100660}
661
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000664
665/*
666 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800667 *
668 * This generation method follows the RSA key pair generation procedure of
669 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
672 int (*f_rng)(void *, unsigned char *, size_t),
673 void *p_rng,
674 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000675{
Janos Follath24eed8d2019-11-22 13:21:35 +0000676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800677 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100678 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
Janos Follathb8fc1b02018-09-03 15:37:01 +0100680 /*
681 * If the modulus is 1024 bit long or shorter, then the security strength of
682 * the RSA algorithm is less than or equal to 80 bits and therefore an error
683 * rate of 2^-80 is sufficient.
684 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100686 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 mbedtls_mpi_init(&H);
690 mbedtls_mpi_init(&G);
691 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000693 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100694 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
695 goto cleanup;
696 }
697
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000698 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
700 goto cleanup;
701 }
702
703 /*
704 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800705 * 1. |P-Q| > 2^( nbits / 2 - 100 )
706 * 2. GCD( E, (P-1)*(Q-1) ) == 1
707 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 do {
712 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
713 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
716 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800718 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
720 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800724 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (H.s < 0) {
726 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
727 }
Janos Follathef441782016-09-21 13:18:12 +0100728
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100729 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
731 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
732 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800733
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800734 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
736 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800737 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800739
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800740 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
742 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
743 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (mbedtls_mpi_bitlen(&ctx->D) <= ((nbits + 1) / 2)) { // (FIPS 186-4 §B.3.1 criterion 3(a))
Jethro Beekman97f95c92018-02-13 15:50:36 -0800746 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800748
749 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100752 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
754 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100759
Jethro Beekman97f95c92018-02-13 15:50:36 -0800760#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 * DP = D mod (P - 1)
763 * DQ = D mod (Q - 1)
764 * QP = Q^-1 mod P
765 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
767 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100768#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Hanno Becker83aad1f2017-08-23 06:45:10 +0100770 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
773cleanup:
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 mbedtls_mpi_free(&H);
776 mbedtls_mpi_free(&G);
777 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (ret != 0) {
780 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((-ret & ~0x7f) == 0) {
783 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
784 }
785 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 }
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000789}
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
793/*
794 * Check a public RSA key
795 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000797{
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
799 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100800 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
803 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100804 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
807 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
808 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
809 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
810 }
811
812 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000813}
814
815/*
Hanno Becker705fc682017-10-10 17:57:02 +0100816 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000817 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000819{
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
821 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
822 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 }
Paul Bakker48377d92013-08-30 12:06:24 +0200824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
826 &ctx->D, &ctx->E, NULL, NULL) != 0) {
827 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000828 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000829
Hanno Beckerb269a852017-08-25 08:03:21 +0100830#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
832 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
833 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100834 }
835#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000838}
839
840/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100841 * Check if contexts holding a public and private key match
842 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
844 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100845{
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
847 mbedtls_rsa_check_privkey(prv) != 0) {
848 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100849 }
850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
852 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
853 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100854 }
855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100857}
858
859/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 * Do an RSA public key operation
861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
863 const unsigned char *input,
864 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000865{
Janos Follath24eed8d2019-11-22 13:21:35 +0000866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000867 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
871 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
872 }
Hanno Becker705fc682017-10-10 17:57:02 +0100873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000875
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200876#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
878 return ret;
879 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200880#endif
881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200885 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
886 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 }
888
889 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
891 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
893cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
896 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
897 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100898#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (ret != 0) {
903 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
904 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000907}
908
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200909/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200910 * Generate or update blinding values, see section 10 of:
911 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200912 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200913 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200914 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
916 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200917{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200918 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200919 mbedtls_mpi R;
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200924 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
926 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
927 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
928 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200929
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200930 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200931 }
932
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200933 /* Unblinding value: Vf = random number, invertible mod N */
934 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200936 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
937 goto cleanup;
938 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&ctx->Vf, ctx->len - 1, f_rng, p_rng));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200941
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200942 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
944 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
945 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200946
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200947 /* At this point, Vi is invertible mod N if and only if both Vf and R
948 * are invertible mod N. If one of them isn't, we don't need to know
949 * which one, we just loop and choose new values for both of them.
950 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
952 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200953 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500957
958 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
960 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200961
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200962 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200963 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200965
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200966
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200967cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200971}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200972
Paul Bakker5121ce52009-01-03 21:22:43 +0000973/*
Janos Follathd6b09652023-11-21 09:33:54 +0000974 * Unblind
975 * T = T * Vf mod N
976 */
Janos Follatha62a5542023-11-21 14:20:08 +0000977static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, mbedtls_mpi *N)
Janos Follathd6b09652023-11-21 09:33:54 +0000978{
979 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
980 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
981 const size_t nlimbs = N->n;
982 const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
983 mbedtls_mpi RR, M_T;
984
985 mbedtls_mpi_init(&RR);
986 mbedtls_mpi_init(&M_T);
987
988 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
989 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
990
991 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
992 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
993
Janos Follathe6750b22023-12-27 10:22:59 +0000994 /* T = T * Vf mod N
995 * Reminder: montmul(A, B, N) = A * B * R^-1 mod N
996 * Usually both operands are multiplied by R mod N beforehand (by calling
997 * `to_mont_rep()` on them), yielding a result that's also * R mod N (aka
998 * "in the Montgomery domain"). Here we only multiply one operand by R mod
999 * N, so the result is directly what we want - no need to call
1000 * `from_mont_rep()` on it. */
Janos Follathd6b09652023-11-21 09:33:54 +00001001 mbedtls_mpi_core_to_mont_rep(T->p, T->p, N->p, nlimbs, mm, RR.p, M_T.p);
Janos Follathd6b09652023-11-21 09:33:54 +00001002 mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1003
1004cleanup:
1005
1006 mbedtls_mpi_free(&RR);
1007 mbedtls_mpi_free(&M_T);
1008
1009 return ret;
1010}
1011
1012/*
Janos Follathe81102e2017-03-22 13:38:28 +00001013 * Exponent blinding supposed to prevent side-channel attacks using multiple
1014 * traces of measurements to recover the RSA key. The more collisions are there,
1015 * the more bits of the key can be recovered. See [3].
1016 *
1017 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001018 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001019 *
1020 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001021 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001022 *
1023 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1024 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1025 * Thus in this sense with 28 byte blinding the security is not reduced by
1026 * side-channel attacks like the one in [3])
1027 *
1028 * This countermeasure does not help if the key recovery is possible with a
1029 * single trace.
1030 */
1031#define RSA_EXPONENT_BLINDING 28
1032
1033/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 * Do an RSA private key operation
1035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1037 int (*f_rng)(void *, unsigned char *, size_t),
1038 void *p_rng,
1039 const unsigned char *input,
1040 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001041{
Janos Follath24eed8d2019-11-22 13:21:35 +00001042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001043 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001044
1045 /* Temporary holding the result */
1046 mbedtls_mpi T;
1047
1048 /* Temporaries holding P-1, Q-1 and the
1049 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001050 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001051
1052#if !defined(MBEDTLS_RSA_NO_CRT)
1053 /* Temporaries holding the results mod p resp. mod q. */
1054 mbedtls_mpi TP, TQ;
1055
1056 /* Temporaries holding the blinded exponents for
1057 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001058 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001059
1060 /* Pointers to actual exponents to be used - either the unblinded
1061 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +00001062 mbedtls_mpi *DP = &ctx->DP;
1063 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +01001064#else
1065 /* Temporary holding the blinded exponent (if used). */
1066 mbedtls_mpi D_blind;
1067
1068 /* Pointer to actual exponent to be used - either the unblinded
1069 * or the blinded one, depending on the presence of a PRNG. */
1070 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +01001071#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001072
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001073 /* Temporaries holding the initial input and the double
1074 * checked result; should be the same in the end. */
1075 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if (f_rng == NULL) {
1078 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1079 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if (rsa_check_context(ctx, 1 /* private key checks */,
1082 1 /* blinding on */) != 0) {
1083 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001084 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001085
Hanno Becker06811ce2017-05-03 15:10:34 +01001086#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1088 return ret;
1089 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001090#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001091
Hanno Becker06811ce2017-05-03 15:10:34 +01001092 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 mbedtls_mpi_init(&P1);
1096 mbedtls_mpi_init(&Q1);
1097 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001098
Janos Follathe81102e2017-03-22 13:38:28 +00001099#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001101#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 mbedtls_mpi_init(&DP_blind);
1103 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001104#endif
1105
Hanno Becker06811ce2017-05-03 15:10:34 +01001106#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001108#endif
1109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 mbedtls_mpi_init(&I);
1111 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001112
1113 /* End of MPI initialization */
1114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1116 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001117 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1118 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 }
1120
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001121 /*
1122 * Blinding
1123 * T = T * Vi mod N
1124 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1126 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1127 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001128
Janos Follath6bcbc922023-11-21 09:46:43 +00001129 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
1130
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001131 /*
1132 * Exponent blinding
1133 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1135 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001136
Janos Follathf9203b42017-03-22 15:13:15 +00001137#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001138 /*
1139 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1140 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1142 f_rng, p_rng));
1143 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1144 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1145 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +00001146
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001147 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001148#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001149 /*
1150 * DP_blind = ( P - 1 ) * R + DP
1151 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1153 f_rng, p_rng));
1154 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1155 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1156 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001157
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001158 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001159
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001160 /*
1161 * DQ_blind = ( Q - 1 ) * R + DQ
1162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1164 f_rng, p_rng));
1165 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1166 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1167 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001168
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001169 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001170#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001174#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001175 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001176 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001178 * TP = input ^ dP mod P
1179 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1183 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001184
1185 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001186 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1189 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1190 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001191
1192 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001193 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1196 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001198
Hanno Becker2dec5e82017-10-03 07:49:52 +01001199 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1201 &ctx->N, &ctx->RN));
1202 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001203 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1204 goto cleanup;
1205 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001206
Janos Follath6bcbc922023-11-21 09:46:43 +00001207 /*
1208 * Unblind
1209 * T = T * Vf mod N
1210 */
1211 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1212
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001215
1216cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1219 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1220 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001221#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 mbedtls_mpi_free(&P1);
1224 mbedtls_mpi_free(&Q1);
1225 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001226
Janos Follathe81102e2017-03-22 13:38:28 +00001227#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001229#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 mbedtls_mpi_free(&DP_blind);
1231 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001232#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001235
1236#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001238#endif
1239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 mbedtls_mpi_free(&C);
1241 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (ret != 0 && ret >= -0x007f) {
1244 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1245 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001248}
1249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001251/**
1252 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1253 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001254 * \param dst buffer to mask
1255 * \param dlen length of destination buffer
1256 * \param src source of the mask generation
1257 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001258 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001259 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001260static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1261 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001262{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001263 unsigned char counter[4];
1264 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001265 unsigned int hlen;
1266 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001267 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001268 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001269 const mbedtls_md_info_t *md_info;
1270 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 mbedtls_md_init(&md_ctx);
1273 md_info = mbedtls_md_info_from_type(md_alg);
1274 if (md_info == NULL) {
1275 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1276 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 mbedtls_md_init(&md_ctx);
1279 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001280 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 memset(mask, 0, sizeof(mask));
1286 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001287
Simon Butcher02037452016-03-01 21:19:12 +00001288 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001289 p = dst;
1290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001292 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001294 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001298 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 }
1300 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001301 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 }
1303 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001304 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 }
1306 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001307 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001311 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001313
1314 counter[3]++;
1315
1316 dlen -= use_len;
1317 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001318
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001324}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001325
1326/**
1327 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1328 *
1329 * \param hash the input hash
1330 * \param hlen length of the input hash
1331 * \param salt the input salt
1332 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001333 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001334 * \param md_alg message digest to use
1335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001336static int hash_mprime(const unsigned char *hash, size_t hlen,
1337 const unsigned char *salt, size_t slen,
1338 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001339{
1340 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001341
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001342 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1346 if (md_info == NULL) {
1347 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1348 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 mbedtls_md_init(&md_ctx);
1351 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001352 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 }
1354 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001355 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 }
1357 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001358 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 }
1360 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001361 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 }
1363 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001364 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 }
1366 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001367 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001369
1370exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001374}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001375
1376/**
1377 * Compute a hash.
1378 *
1379 * \param md_alg algorithm to use
1380 * \param input input message to hash
1381 * \param ilen input length
1382 * \param output the output buffer - must be large enough for \p md_alg
1383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001384static int compute_hash(mbedtls_md_type_t md_alg,
1385 const unsigned char *input, size_t ilen,
1386 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001387{
1388 const mbedtls_md_info_t *md_info;
1389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 md_info = mbedtls_md_info_from_type(md_alg);
1391 if (md_info == NULL) {
1392 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1393 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001400/*
1401 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1404 int (*f_rng)(void *, unsigned char *, size_t),
1405 void *p_rng,
1406 const unsigned char *label, size_t label_len,
1407 size_t ilen,
1408 const unsigned char *input,
1409 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001410{
1411 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001413 unsigned char *p = output;
1414 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001415
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 if (f_rng == NULL) {
1417 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1418 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001419
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001420 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if (hlen == 0) {
1422 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1423 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001424
1425 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001426
Simon Butcher02037452016-03-01 21:19:12 +00001427 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1429 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1430 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001433
1434 *p++ = 0;
1435
Simon Butcher02037452016-03-01 21:19:12 +00001436 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1438 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1439 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001440
1441 p += hlen;
1442
Simon Butcher02037452016-03-01 21:19:12 +00001443 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1445 if (ret != 0) {
1446 return ret;
1447 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001448 p += hlen;
1449 p += olen - 2 * hlen - 2 - ilen;
1450 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 if (ilen != 0) {
1452 memcpy(p, input, ilen);
1453 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001454
Simon Butcher02037452016-03-01 21:19:12 +00001455 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001457 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 return ret;
1459 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001460
Simon Butcher02037452016-03-01 21:19:12 +00001461 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001463 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 return ret;
1465 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001468}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001472/*
1473 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1476 int (*f_rng)(void *, unsigned char *, size_t),
1477 void *p_rng, size_t ilen,
1478 const unsigned char *input,
1479 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001480{
1481 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001483 unsigned char *p = output;
1484
Paul Bakkerb3869132013-02-28 17:21:01 +01001485 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001486
Simon Butcher02037452016-03-01 21:19:12 +00001487 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (ilen + 11 < ilen || olen < ilen + 11) {
1489 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1490 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001491
1492 nb_pad = olen - 3 - ilen;
1493
1494 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (f_rng == NULL) {
1497 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1498 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001499
1500 *p++ = MBEDTLS_RSA_CRYPT;
1501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001503 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001504
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001505 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 ret = f_rng(p_rng, p, 1);
1507 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001508
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001509 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if (rng_dl == 0 || ret != 0) {
1511 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1512 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001513
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001514 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001515 }
1516
1517 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 if (ilen != 0) {
1519 memcpy(p, input, ilen);
1520 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001525
Paul Bakker5121ce52009-01-03 21:22:43 +00001526/*
1527 * Add the message padding, then do an RSA operation
1528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1530 int (*f_rng)(void *, unsigned char *, size_t),
1531 void *p_rng,
1532 size_t ilen,
1533 const unsigned char *input,
1534 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001535{
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_PKCS1_V15)
1538 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1540 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001541#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_PKCS1_V21)
1544 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1546 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001547#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001548
1549 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001552}
1553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001555/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001556 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001557 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001558int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1559 int (*f_rng)(void *, unsigned char *, size_t),
1560 void *p_rng,
1561 const unsigned char *label, size_t label_len,
1562 size_t *olen,
1563 const unsigned char *input,
1564 unsigned char *output,
1565 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001566{
Janos Follath24eed8d2019-11-22 13:21:35 +00001567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001568 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001569 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001570 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001572 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001573 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001574
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001575 /*
1576 * Parameters sanity checks
1577 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1579 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1580 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001581
1582 ilen = ctx->len;
1583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if (ilen < 16 || ilen > sizeof(buf)) {
1585 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001587
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001588 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 if (hlen == 0) {
1590 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1591 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001592
Janos Follathc17cda12016-02-11 11:08:18 +00001593 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 if (2 * hlen + 2 > ilen) {
1595 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1596 }
Janos Follathc17cda12016-02-11 11:08:18 +00001597
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001598 /*
1599 * RSA operation
1600 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001604 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001606
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001607 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001608 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001609 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001610 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001612 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 /* DB: Apply dbMask to maskedDB */
1614 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001615 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001616 goto cleanup;
1617 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001618
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001619 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1621 label, label_len, lhash);
1622 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001623 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001625
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001626 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001627 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001628 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 p = buf;
1630
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001631 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001632
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001633 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001634
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001635 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001636 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001637 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001638
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001639 /* Get zero-padding len, but always read till end of buffer
1640 * (minus one, for the 01 byte) */
1641 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001642 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001644 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1645 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001646 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001647
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001648 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001649 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001650
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001651 /*
1652 * The only information "leaked" is whether the padding was correct or not
1653 * (eg, no data is copied if it was not correct). This meets the
1654 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1655 * the different error conditions.
1656 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001657 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001658 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1659 goto cleanup;
1660 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001663 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1664 goto cleanup;
1665 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001666
1667 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 if (*olen != 0) {
1669 memcpy(output, p, *olen);
1670 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001671 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001672
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001673cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 mbedtls_platform_zeroize(buf, sizeof(buf));
1675 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001682/*
1683 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1684 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001685int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1686 int (*f_rng)(void *, unsigned char *, size_t),
1687 void *p_rng,
1688 size_t *olen,
1689 const unsigned char *input,
1690 unsigned char *output,
1691 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001692{
1693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1694 size_t ilen;
1695 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1696
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001697 ilen = ctx->len;
1698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1700 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1701 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 if (ilen < 16 || ilen > sizeof(buf)) {
1704 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1705 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001710 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001712
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1714 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001715
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001716cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001720}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
1723/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001724 * Do an RSA operation, then remove the message padding
1725 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1727 int (*f_rng)(void *, unsigned char *, size_t),
1728 void *p_rng,
1729 size_t *olen,
1730 const unsigned char *input,
1731 unsigned char *output,
1732 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001733{
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#if defined(MBEDTLS_PKCS1_V15)
1736 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1738 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001739#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741#if defined(MBEDTLS_PKCS1_V21)
1742 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1744 olen, input, output,
1745 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001746#endif
1747
1748 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001750 }
1751}
1752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001754static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1755 int (*f_rng)(void *, unsigned char *, size_t),
1756 void *p_rng,
1757 mbedtls_md_type_t md_alg,
1758 unsigned int hashlen,
1759 const unsigned char *hash,
1760 int saltlen,
1761 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001762{
1763 size_t olen;
1764 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001765 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001766 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001768 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001769
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001771 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1775 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1776 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 if (f_rng == NULL) {
1779 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1780 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001781
1782 olen = ctx->len;
1783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001785 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001786 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 if (exp_hashlen == 0) {
1788 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1789 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 if (hashlen != exp_hashlen) {
1792 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1793 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001794 }
1795
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001796 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 if (hlen == 0) {
1798 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1799 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1802 /* Calculate the largest possible salt length, up to the hash size.
1803 * Normally this is the hash length, which is the maximum salt length
1804 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1805 * enough room, use the maximum salt length that fits. The constraint is
1806 * that the hash length plus the salt length plus 2 bytes must be at most
1807 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1808 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001809 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 if (olen < hlen + min_slen + 2) {
1811 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1812 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001813 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001815 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 }
1817 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1818 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1819 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001820 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001821 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001824
Simon Butcher02037452016-03-01 21:19:12 +00001825 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001827 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001828 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001829
1830 /* Generate salt of length slen in place in the encoded message */
1831 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1833 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1834 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001835
Paul Bakkerb3869132013-02-28 17:21:01 +01001836 p += slen;
1837
Simon Butcher02037452016-03-01 21:19:12 +00001838 /* Generate H = Hash( M' ) */
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001839 ret = hash_mprime(hash, hashlen, salt, slen, p, (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if (ret != 0) {
1841 return ret;
1842 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001843
Simon Butcher02037452016-03-01 21:19:12 +00001844 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001846 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001848
Simon Butcher02037452016-03-01 21:19:12 +00001849 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001851 (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 if (ret != 0) {
1853 return ret;
1854 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1857 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001858
1859 p += hlen;
1860 *p++ = 0xBC;
1861
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001863}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001864
1865/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001866 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1867 * the option to pass in the salt length.
1868 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001869int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1870 int (*f_rng)(void *, unsigned char *, size_t),
1871 void *p_rng,
1872 mbedtls_md_type_t md_alg,
1873 unsigned int hashlen,
1874 const unsigned char *hash,
1875 int saltlen,
1876 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001877{
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1879 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001880}
1881
1882
1883/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001884 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1885 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001886int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1887 int (*f_rng)(void *, unsigned char *, size_t),
1888 void *p_rng,
1889 mbedtls_md_type_t md_alg,
1890 unsigned int hashlen,
1891 const unsigned char *hash,
1892 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001893{
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1895 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001896}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001900/*
1901 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1902 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001903
1904/* Construct a PKCS v1.5 encoding of a hashed message
1905 *
1906 * This is used both for signature generation and verification.
1907 *
1908 * Parameters:
1909 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001910 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001911 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001912 * - hash: Buffer containing the hashed message or the raw data.
1913 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001914 * - dst: Buffer to hold the encoded message.
1915 *
1916 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001917 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001918 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001919 *
1920 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001921static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1922 unsigned int hashlen,
1923 const unsigned char *hash,
1924 size_t dst_len,
1925 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001926{
1927 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001928 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001929 unsigned char *p = dst;
1930 const char *oid = NULL;
1931
1932 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001934 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 if (md_size == 0) {
1936 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1937 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1940 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1941 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 if (hashlen != md_size) {
1944 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1945 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001946
1947 /* Double-check that 8 + hashlen + oid_size can be used as a
1948 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001950 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 10 + hashlen + oid_size < 10 + hashlen) {
1952 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1953 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001954
1955 /*
1956 * Static bounds check:
1957 * - Need 10 bytes for five tag-length pairs.
1958 * (Insist on 1-byte length encodings to protect against variants of
1959 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1960 * - Need hashlen bytes for hash
1961 * - Need oid_size bytes for hash alg OID.
1962 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 if (nb_pad < 10 + hashlen + oid_size) {
1964 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1965 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001966 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 } else {
1968 if (nb_pad < hashlen) {
1969 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1970 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001971
1972 nb_pad -= hashlen;
1973 }
1974
Hanno Becker2b2f8982017-09-27 17:10:03 +01001975 /* Need space for signature header and padding delimiter (3 bytes),
1976 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 if (nb_pad < 3 + 8) {
1978 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1979 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001980 nb_pad -= 3;
1981
1982 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001983 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001984
1985 /* Write signature header and padding */
1986 *p++ = 0;
1987 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001989 p += nb_pad;
1990 *p++ = 0;
1991
1992 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 if (md_alg == MBEDTLS_MD_NONE) {
1994 memcpy(p, hash, hashlen);
1995 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001996 }
1997
1998 /* Signing hashed data, add corresponding ASN.1 structure
1999 *
2000 * DigestInfo ::= SEQUENCE {
2001 * digestAlgorithm DigestAlgorithmIdentifier,
2002 * digest Digest }
2003 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2004 * Digest ::= OCTET STRING
2005 *
2006 * Schematic:
2007 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2008 * TAG-NULL + LEN [ NULL ] ]
2009 * TAG-OCTET + LEN [ HASH ] ]
2010 */
2011 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002013 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002015 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002016 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002018 p += oid_size;
2019 *p++ = MBEDTLS_ASN1_NULL;
2020 *p++ = 0x00;
2021 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002022 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002024 p += hashlen;
2025
2026 /* Just a sanity-check, should be automatic
2027 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (p != dst + dst_len) {
2029 mbedtls_platform_zeroize(dst, dst_len);
2030 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002031 }
2032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002034}
2035
Paul Bakkerb3869132013-02-28 17:21:01 +01002036/*
2037 * Do an RSA operation to sign the message digest
2038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2040 int (*f_rng)(void *, unsigned char *, size_t),
2041 void *p_rng,
2042 mbedtls_md_type_t md_alg,
2043 unsigned int hashlen,
2044 const unsigned char *hash,
2045 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002046{
Janos Follath24eed8d2019-11-22 13:21:35 +00002047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002048 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002051 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2055 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2056 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002057
Hanno Beckerfdf38032017-09-06 12:35:55 +01002058 /*
2059 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2060 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2063 ctx->len, sig)) != 0) {
2064 return ret;
2065 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002066
Hanno Beckerfdf38032017-09-06 12:35:55 +01002067 /* Private key operation
2068 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002069 * In order to prevent Lenstra's attack, make the signature in a
2070 * temporary buffer and check it before returning it.
2071 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 sig_try = mbedtls_calloc(1, ctx->len);
2074 if (sig_try == NULL) {
2075 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002076 }
2077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 verif = mbedtls_calloc(1, ctx->len);
2079 if (verif == NULL) {
2080 mbedtls_free(sig_try);
2081 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2082 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2085 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2086
2087 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002088 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2089 goto cleanup;
2090 }
2091
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002093
2094cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002095 mbedtls_zeroize_and_free(sig_try, ctx->len);
2096 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002097
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (ret != 0) {
2099 memset(sig, '!', ctx->len);
2100 }
2101 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002104
2105/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 * Do an RSA operation to sign the message digest
2107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002108int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2109 int (*f_rng)(void *, unsigned char *, size_t),
2110 void *p_rng,
2111 mbedtls_md_type_t md_alg,
2112 unsigned int hashlen,
2113 const unsigned char *hash,
2114 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002115{
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002117 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121#if defined(MBEDTLS_PKCS1_V15)
2122 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2124 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002125#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127#if defined(MBEDTLS_PKCS1_V21)
2128 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2130 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002131#endif
2132
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002136}
2137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002139/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002140 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002142int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2143 mbedtls_md_type_t md_alg,
2144 unsigned int hashlen,
2145 const unsigned char *hash,
2146 mbedtls_md_type_t mgf1_hash_id,
2147 int expected_salt_len,
2148 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002149{
Janos Follath24eed8d2019-11-22 13:21:35 +00002150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002151 size_t siglen;
2152 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002153 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002154 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002155 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002156 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002160 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002162
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 siglen = ctx->len;
2164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 if (siglen < 16 || siglen > sizeof(buf)) {
2166 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2167 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 if (ret != 0) {
2172 return ret;
2173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002174
2175 p = buf;
2176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (buf[siglen - 1] != 0xBC) {
2178 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002179 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (md_alg != MBEDTLS_MD_NONE) {
2182 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002183 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (exp_hashlen == 0) {
2185 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2186 }
2187
2188 if (hashlen != exp_hashlen) {
2189 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2190 }
2191 }
2192
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002193 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (hlen == 0) {
2195 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2196 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002197
Simon Butcher02037452016-03-01 21:19:12 +00002198 /*
2199 * Note: EMSA-PSS verification is over the length of N - 1 bits
2200 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (buf[0] >> (8 - siglen * 8 + msb)) {
2204 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2205 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002206
Simon Butcher02037452016-03-01 21:19:12 +00002207 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002209 p++;
2210 siglen -= 1;
2211 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (siglen < hlen + 2) {
2214 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2215 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002216 hash_start = p + siglen - hlen - 1;
2217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2219 if (ret != 0) {
2220 return ret;
2221 }
Paul Bakker02303e82013-01-03 11:08:31 +01002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002226 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 if (*p++ != 0x01) {
2230 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2231 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002232
Gilles Peskine6a54b022017-10-17 19:02:13 +02002233 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2236 observed_salt_len != (size_t) expected_salt_len) {
2237 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002238 }
2239
Simon Butcher02037452016-03-01 21:19:12 +00002240 /*
2241 * Generate H = Hash( M' )
2242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2244 result, mgf1_hash_id);
2245 if (ret != 0) {
2246 return ret;
2247 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (memcmp(hash_start, result, hlen) != 0) {
2250 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2251 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002254}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002255
2256/*
2257 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002259int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2260 mbedtls_md_type_t md_alg,
2261 unsigned int hashlen,
2262 const unsigned char *hash,
2263 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002264{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002265 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002267 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002272 : md_alg;
2273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2275 md_alg, hashlen, hash,
2276 mgf1_hash_id,
2277 MBEDTLS_RSA_SALT_LEN_ANY,
2278 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002279
2280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002284/*
2285 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002287int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2288 mbedtls_md_type_t md_alg,
2289 unsigned int hashlen,
2290 const unsigned char *hash,
2291 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002292{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002293 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002294 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002295 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002298 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002300
2301 sig_len = ctx->len;
2302
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002303 /*
2304 * Prepare expected PKCS1 v1.5 encoding of hash.
2305 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2308 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002309 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2310 goto cleanup;
2311 }
2312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2314 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002315 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002317
2318 /*
2319 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2320 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 ret = mbedtls_rsa_public(ctx, sig, encoded);
2323 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002324 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002326
Simon Butcher02037452016-03-01 21:19:12 +00002327 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002328 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002329 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2332 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002333 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2334 goto cleanup;
2335 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002336
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002337cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002340 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002341 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002344 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002345 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002348}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002350
2351/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002352 * Do an RSA operation and check the message digest
2353 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002354int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2355 mbedtls_md_type_t md_alg,
2356 unsigned int hashlen,
2357 const unsigned char *hash,
2358 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002359{
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002361 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365#if defined(MBEDTLS_PKCS1_V15)
2366 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2368 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002369#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371#if defined(MBEDTLS_PKCS1_V21)
2372 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2374 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002375#endif
2376
2377 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002379 }
2380}
2381
2382/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002383 * Copy the components of an RSA key
2384 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002385int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002386{
Janos Follath24eed8d2019-11-22 13:21:35 +00002387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002388
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002389 dst->len = src->len;
2390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2392 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2395 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2396 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002397
2398#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2400 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2401 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2402 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2403 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002404#endif
2405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2409 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002410
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002411 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002412 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002413
2414cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if (ret != 0) {
2416 mbedtls_rsa_free(dst);
2417 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002420}
2421
2422/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 * Free the components of an RSA key
2424 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002426{
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002428 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 mbedtls_mpi_free(&ctx->Vi);
2432 mbedtls_mpi_free(&ctx->Vf);
2433 mbedtls_mpi_free(&ctx->RN);
2434 mbedtls_mpi_free(&ctx->D);
2435 mbedtls_mpi_free(&ctx->Q);
2436 mbedtls_mpi_free(&ctx->P);
2437 mbedtls_mpi_free(&ctx->E);
2438 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002439
Hanno Becker33c30a02017-08-23 07:00:22 +01002440#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 mbedtls_mpi_free(&ctx->RQ);
2442 mbedtls_mpi_free(&ctx->RP);
2443 mbedtls_mpi_free(&ctx->QP);
2444 mbedtls_mpi_free(&ctx->DQ);
2445 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002446#endif /* MBEDTLS_RSA_NO_CRT */
2447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002449 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (ctx->ver != 0) {
2451 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002452 ctx->ver = 0;
2453 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002454#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002455}
2456
Hanno Beckerab377312017-08-23 16:24:51 +01002457#endif /* !MBEDTLS_RSA_ALT */
2458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002461#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
2463/*
2464 * Example RSA-1024 keypair, for test purposes
2465 */
2466#define KEY_LEN 128
2467
2468#define RSA_N "9292758453063D803DD603D5E777D788" \
2469 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2470 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2471 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2472 "93A89813FBF3C4F8066D2D800F7C38A8" \
2473 "1AE31942917403FF4946B0A83D3D3E05" \
2474 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2475 "5E94BB77B07507233A0BC7BAC8F90F79"
2476
2477#define RSA_E "10001"
2478
2479#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2480 "66CA472BC44D253102F8B4A9D3BFA750" \
2481 "91386C0077937FE33FA3252D28855837" \
2482 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2483 "DF79C5CE07EE72C7F123142198164234" \
2484 "CABB724CF78B8173B9F880FC86322407" \
2485 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2486 "071513A1E85B5DFA031F21ECAE91A34D"
2487
2488#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2489 "2C01CAD19EA484A87EA4377637E75500" \
2490 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2491 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2492
2493#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2494 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2495 "910E4168387E3C30AA1E00C339A79508" \
2496 "8452DD96A9A5EA5D9DCA68DA636032AF"
2497
Paul Bakker5121ce52009-01-03 21:22:43 +00002498#define PT_LEN 24
2499#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2500 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002503static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002504{
gufe44c2620da2020-08-03 17:56:50 +02002505#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002506 size_t i;
2507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002509 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 }
Paul Bakker545570e2010-07-18 09:00:25 +00002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002513 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002515#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002517 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002521#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002524}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002526
Paul Bakker5121ce52009-01-03 21:22:43 +00002527/*
2528 * Checkup routine
2529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002530int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002531{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002532 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002534 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 unsigned char rsa_plaintext[PT_LEN];
2537 unsigned char rsa_decrypted[PT_LEN];
2538 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002539#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002540 unsigned char sha1sum[20];
2541#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Hanno Becker3a701162017-08-22 13:52:43 +01002543 mbedtls_mpi K;
2544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 mbedtls_mpi_init(&K);
2546 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2549 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2550 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2551 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2552 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2553 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2554 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2555 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2556 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2557 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 if (verbose != 0) {
2562 mbedtls_printf(" RSA key validation: ");
2563 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2566 mbedtls_rsa_check_privkey(&rsa) != 0) {
2567 if (verbose != 0) {
2568 mbedtls_printf("failed\n");
2569 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Hanno Becker5bc87292017-05-03 15:09:31 +01002571 ret = 1;
2572 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 }
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 if (verbose != 0) {
2576 mbedtls_printf("passed\n PKCS#1 encryption : ");
2577 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2582 PT_LEN, rsa_plaintext,
2583 rsa_ciphertext) != 0) {
2584 if (verbose != 0) {
2585 mbedtls_printf("failed\n");
2586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Hanno Becker5bc87292017-05-03 15:09:31 +01002588 ret = 1;
2589 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 }
2591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (verbose != 0) {
2593 mbedtls_printf("passed\n PKCS#1 decryption : ");
2594 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2597 &len, rsa_ciphertext, rsa_decrypted,
2598 sizeof(rsa_decrypted)) != 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 (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2608 if (verbose != 0) {
2609 mbedtls_printf("failed\n");
2610 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Hanno Becker5bc87292017-05-03 15:09:31 +01002612 ret = 1;
2613 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 }
2615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (verbose != 0) {
2617 mbedtls_printf("passed\n");
2618 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002619
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002620#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (verbose != 0) {
2622 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002623 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002625 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2626 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 if (verbose != 0) {
2628 mbedtls_printf("failed\n");
2629 }
2630
2631 return 1;
2632 }
2633
2634 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2635 MBEDTLS_MD_SHA1, 20,
2636 sha1sum, rsa_ciphertext) != 0) {
2637 if (verbose != 0) {
2638 mbedtls_printf("failed\n");
2639 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Hanno Becker5bc87292017-05-03 15:09:31 +01002641 ret = 1;
2642 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002643 }
2644
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 if (verbose != 0) {
2646 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2647 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2650 sha1sum, rsa_ciphertext) != 0) {
2651 if (verbose != 0) {
2652 mbedtls_printf("failed\n");
2653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Hanno Becker5bc87292017-05-03 15:09:31 +01002655 ret = 1;
2656 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 }
2658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 if (verbose != 0) {
2660 mbedtls_printf("passed\n");
2661 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002662#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 if (verbose != 0) {
2665 mbedtls_printf("\n");
2666 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002667
Paul Bakker3d8fb632014-04-17 12:42:41 +02002668cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 mbedtls_mpi_free(&K);
2670 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002672 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002675}
2676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679#endif /* MBEDTLS_RSA_C */