blob: 3095fec88361c3375b21a5952054bb6caf7e286c [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
Hanno Becker74716312017-10-02 10:00:37 +010019
Paul Bakker5121ce52009-01-03 21:22:43 +000020/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000021 * The following sources were referenced in the design of this implementation
22 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000023 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000024 * [1] A method for obtaining digital signatures and public-key cryptosystems
25 * R Rivest, A Shamir, and L Adleman
26 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
27 *
28 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
29 * Menezes, van Oorschot and Vanstone
30 *
Janos Follathe81102e2017-03-22 13:38:28 +000031 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
32 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
33 * Stefan Mangard
34 * https://arxiv.org/abs/1702.08719v2
35 *
Paul Bakker5121ce52009-01-03 21:22:43 +000036 */
37
Gilles Peskinedb09ef62020-06-03 01:43:33 +020038#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/rsa.h"
Janos Follath10f83662023-11-21 09:33:54 +000043#include "bignum_core.h"
Chris Jones66a4cd42021-03-09 16:04:12 +000044#include "rsa_alt_helpers.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050046#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000047#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020048#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020049#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020050#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
gufe44c2620da2020-08-03 17:56:50 +020054#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010059
Dave Rodgman19e8cd02023-05-09 11:10:21 +010060
61#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
62
63/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
64 * operation (EME-PKCS1-v1_5 decoding).
65 *
66 * \note The return value from this function is a sensitive value
67 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
68 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
69 * is often a situation that an attacker can provoke and leaking which
70 * one is the result is precisely the information the attacker wants.
71 *
72 * \param input The input buffer which is the payload inside PKCS#1v1.5
73 * encryption padding, called the "encoded message EM"
74 * by the terminology.
75 * \param ilen The length of the payload in the \p input buffer.
76 * \param output The buffer for the payload, called "message M" by the
77 * PKCS#1 terminology. This must be a writable buffer of
78 * length \p output_max_len bytes.
79 * \param olen The address at which to store the length of
80 * the payload. This must not be \c NULL.
81 * \param output_max_len The length in bytes of the output buffer \p output.
82 *
83 * \return \c 0 on success.
84 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
85 * The output buffer is too small for the unpadded payload.
86 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
87 * The input doesn't contain properly formatted padding.
88 */
89static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
90 size_t ilen,
91 unsigned char *output,
92 size_t output_max_len,
93 size_t *olen)
94{
95 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
96 size_t i, plaintext_max_size;
97
98 /* The following variables take sensitive values: their value must
99 * not leak into the observable behavior of the function other than
100 * the designated outputs (output, olen, return value). Otherwise
101 * this would open the execution of the function to
102 * side-channel-based variants of the Bleichenbacher padding oracle
103 * attack. Potential side channels include overall timing, memory
104 * access patterns (especially visible to an adversary who has access
105 * to a shared memory cache), and branches (especially visible to
106 * an adversary who has access to a shared code cache or to a shared
107 * branch predictor). */
108 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100109 mbedtls_ct_condition_t bad;
110 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100111 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100112 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100113
114 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
115 : output_max_len;
116
117 /* Check and get padding length in constant time and constant
118 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100119 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100120
121
122 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
123 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100124 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100125
126 /* Read the whole buffer. Set pad_done to nonzero if we find
127 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100128 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100129 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100130 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100131 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100132 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100133 }
134
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100135 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100136 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100137
138 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100139 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100140
141 /* If the padding is valid, set plaintext_size to the number of
142 * remaining bytes after stripping the padding. If the padding
143 * is invalid, avoid leaking this fact through the size of the
144 * output: use the maximum message size that fits in the output
145 * buffer. Do it without branches to avoid leaking the padding
146 * validity through timing. RSA keys are small enough that all the
147 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100148 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100149 bad, (unsigned) plaintext_max_size,
150 (unsigned) (ilen - pad_count - 3));
151
152 /* Set output_too_large to 0 if the plaintext fits in the output
153 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100154 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100155 plaintext_max_size);
156
157 /* Set ret without branches to avoid timing attacks. Return:
158 * - INVALID_PADDING if the padding is bad (bad != 0).
159 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
160 * plaintext does not fit in the output buffer.
161 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100162 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100163 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100164 MBEDTLS_ERR_RSA_INVALID_PADDING,
165 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100166 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100167
168 /* If the padding is bad or the plaintext is too large, zero the
169 * data that we're about to copy to the output buffer.
170 * We need to copy the same amount of data
171 * from the same buffer whether the padding is good or not to
172 * avoid leaking the padding validity through overall timing or
173 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100174 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100175
176 /* If the plaintext is too large, truncate it to the buffer size.
177 * Copy anyway to avoid revealing the length through timing, because
178 * revealing the length is as bad as revealing the padding validity
179 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100180 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100181 (unsigned) plaintext_max_size,
182 (unsigned) plaintext_size);
183
184 /* Move the plaintext to the leftmost position where it can start in
185 * the working buffer, i.e. make it start plaintext_max_size from
186 * the end of the buffer. Do this with a memory access trace that
187 * does not depend on the plaintext size. After this move, the
188 * starting location of the plaintext is no longer sensitive
189 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100190 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
191 plaintext_max_size,
192 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100193
194 /* Finally copy the decrypted plaintext plus trailing zeros into the output
195 * buffer. If output_max_len is 0, then output may be an invalid pointer
196 * and the result of memcpy() would be undefined; prevent undefined
197 * behavior making sure to depend only on output_max_len (the size of the
198 * user-provided output buffer), which is independent from plaintext
199 * length, validity of padding, success of the decryption, and other
200 * secrets. */
201 if (output_max_len != 0) {
202 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
203 }
204
205 /* Report the amount of data we copied to the output buffer. In case
206 * of errors (bad padding or output too large), the value of *olen
207 * when this function returns is not specified. Making it equivalent
208 * to the good case limits the risks of leaking the padding validity. */
209 *olen = plaintext_size;
210
211 return ret;
212}
213
214#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
215
Hanno Beckera565f542017-10-11 11:00:19 +0100216#if !defined(MBEDTLS_RSA_ALT)
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
219 const mbedtls_mpi *N,
220 const mbedtls_mpi *P, const mbedtls_mpi *Q,
221 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100222{
Janos Follath24eed8d2019-11-22 13:21:35 +0000223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
226 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
227 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
228 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
229 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
230 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100231 }
232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 if (N != NULL) {
234 ctx->len = mbedtls_mpi_size(&ctx->N);
235 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100238}
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
241 unsigned char const *N, size_t N_len,
242 unsigned char const *P, size_t P_len,
243 unsigned char const *Q, size_t Q_len,
244 unsigned char const *D, size_t D_len,
245 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100246{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000247 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (N != NULL) {
250 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
251 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100252 }
253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (P != NULL) {
255 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
256 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (Q != NULL) {
259 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
260 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (D != NULL) {
263 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
264 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if (E != NULL) {
267 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
268 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100269
270cleanup:
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (ret != 0) {
273 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
274 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100277}
278
Hanno Becker705fc682017-10-10 17:57:02 +0100279/*
280 * Checks whether the context fields are set in such a way
281 * that the RSA primitives will be able to execute without error.
282 * It does *not* make guarantees for consistency of the parameters.
283 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
285 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100286{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100287#if !defined(MBEDTLS_RSA_NO_CRT)
288 /* blinding_needed is only used for NO_CRT to decide whether
289 * P,Q need to be present or not. */
290 ((void) blinding_needed);
291#endif
292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
294 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
295 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000296 }
Hanno Becker705fc682017-10-10 17:57:02 +0100297
298 /*
299 * 1. Modular exponentiation needs positive, odd moduli.
300 */
301
302 /* Modular exponentiation wrt. N is always used for
303 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
305 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
306 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100307 }
308
309#if !defined(MBEDTLS_RSA_NO_CRT)
310 /* Modular exponentiation for P and Q is only
311 * used for private key operations and if CRT
312 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if (is_priv &&
314 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
315 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
316 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
317 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
318 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100319 }
320#endif /* !MBEDTLS_RSA_NO_CRT */
321
322 /*
323 * 2. Exponents must be positive
324 */
325
326 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
328 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
329 }
Hanno Becker705fc682017-10-10 17:57:02 +0100330
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100331#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100332 /* For private key operations, use D or DP & DQ
333 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
335 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
336 }
Hanno Becker705fc682017-10-10 17:57:02 +0100337#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (is_priv &&
339 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
340 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
341 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100342 }
343#endif /* MBEDTLS_RSA_NO_CRT */
344
345 /* Blinding shouldn't make exponents negative either,
346 * so check that P, Q >= 1 if that hasn't yet been
347 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100348#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (is_priv && blinding_needed &&
350 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
351 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
352 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100353 }
354#endif
355
356 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100357 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100358#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (is_priv &&
360 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
361 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100362 }
363#endif
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100366}
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100369{
370 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500371 int have_N, have_P, have_Q, have_D, have_E;
372#if !defined(MBEDTLS_RSA_NO_CRT)
373 int have_DP, have_DQ, have_QP;
374#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500375 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
378 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
379 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
380 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
381 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500382
383#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
385 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
386 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500387#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100388
Hanno Becker617c1ae2017-08-23 14:11:24 +0100389 /*
390 * Check whether provided parameters are enough
391 * to deduce all others. The following incomplete
392 * parameter sets for private keys are supported:
393 *
394 * (1) P, Q missing.
395 * (2) D and potentially N missing.
396 *
397 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100398
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500399 n_missing = have_P && have_Q && have_D && have_E;
400 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
401 d_missing = have_P && have_Q && !have_D && have_E;
402 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100403
404 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500405 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (!is_priv && !is_pub) {
408 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
409 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100410
411 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100412 * Step 1: Deduce N if P, Q are provided.
413 */
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (!have_N && have_P && have_Q) {
416 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
417 &ctx->Q)) != 0) {
418 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100419 }
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100422 }
423
424 /*
425 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100426 */
427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (pq_missing) {
429 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
430 &ctx->P, &ctx->Q);
431 if (ret != 0) {
432 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
433 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 } else if (d_missing) {
436 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
437 &ctx->Q,
438 &ctx->E,
439 &ctx->D)) != 0) {
440 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100441 }
442 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100443
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100445 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100446 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100447 */
448
Hanno Becker23344b52017-08-23 07:43:27 +0100449#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (is_priv && !(have_DP && have_DQ && have_QP)) {
451 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
452 &ctx->DP, &ctx->DQ, &ctx->QP);
453 if (ret != 0) {
454 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
455 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100456 }
Hanno Becker23344b52017-08-23 07:43:27 +0100457#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100458
459 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100460 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100461 */
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100464}
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
467 unsigned char *N, size_t N_len,
468 unsigned char *P, size_t P_len,
469 unsigned char *Q, size_t Q_len,
470 unsigned char *D, size_t D_len,
471 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100472{
473 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500474 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100475
476 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500477 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
479 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
480 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
481 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
482 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100485 /* If we're trying to export private parameters for a public key,
486 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (P != NULL || Q != NULL || D != NULL) {
488 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
489 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100490
491 }
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (N != NULL) {
494 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
495 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (P != NULL) {
498 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
499 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 if (Q != NULL) {
502 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
503 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (D != NULL) {
506 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
507 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (E != NULL) {
510 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
511 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100512
513cleanup:
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100516}
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
519 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
520 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100521{
Janos Follath24eed8d2019-11-22 13:21:35 +0000522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500523 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100524
525 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500526 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
528 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
529 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
530 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
531 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100534 /* If we're trying to export private parameters for a public key,
535 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (P != NULL || Q != NULL || D != NULL) {
537 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
538 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100539
540 }
541
542 /* Export all requested core parameters. */
543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
545 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
546 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
547 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
548 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
549 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100550 }
551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100553}
554
555/*
556 * Export CRT parameters
557 * This must also be implemented if CRT is not used, for being able to
558 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
559 * can be used in this case.
560 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
562 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100563{
Janos Follath24eed8d2019-11-22 13:21:35 +0000564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500565 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100566
567 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500568 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
570 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
571 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
572 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
573 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (!is_priv) {
576 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
577 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100578
Hanno Beckerdc95c892017-08-23 06:57:02 +0100579#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100580 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
582 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
583 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
584 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100585 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100586#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
588 DP, DQ, QP)) != 0) {
589 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100590 }
591#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100594}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100595
Paul Bakker5121ce52009-01-03 21:22:43 +0000596/*
597 * Initialize an RSA context
598 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100599void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000600{
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Ronald Cronc1905a12021-06-05 11:11:14 +0200603 ctx->padding = MBEDTLS_RSA_PKCS_V15;
604 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100607 /* Set ctx->ver to nonzero to indicate that the mutex has been
608 * initialized and will need to be freed. */
609 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200611#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000612}
613
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100614/*
615 * Set padding for an existing RSA context
616 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100617int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
618 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100619{
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200621#if defined(MBEDTLS_PKCS1_V15)
622 case MBEDTLS_RSA_PKCS_V15:
623 break;
624#endif
625
626#if defined(MBEDTLS_PKCS1_V21)
627 case MBEDTLS_RSA_PKCS_V21:
628 break;
629#endif
630 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200632 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200633
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200634#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
636 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200637 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200638 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return MBEDTLS_ERR_RSA_INVALID_PADDING;
640 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200641 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200642#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500643
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100644 ctx->padding = padding;
645 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100648}
649
Hanno Becker617c1ae2017-08-23 14:11:24 +0100650/*
Yanray Wang83548b52023-03-15 16:46:34 +0800651 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800652 */
653int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
654{
Yanray Wang644b9012023-03-15 16:50:31 +0800655 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +0800656}
657
658/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800659 * Get hash identifier of mbedtls_md_type_t type
660 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800661int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +0800662{
Yanray Wang644b9012023-03-15 16:50:31 +0800663 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +0800664}
665
666/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100667 * Get length in bytes of RSA modulus
668 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100672}
673
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
677/*
678 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800679 *
680 * This generation method follows the RSA key pair generation procedure of
681 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100683int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
684 int (*f_rng)(void *, unsigned char *, size_t),
685 void *p_rng,
686 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000687{
Janos Follath24eed8d2019-11-22 13:21:35 +0000688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800689 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100690 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000691
Janos Follathb8fc1b02018-09-03 15:37:01 +0100692 /*
693 * If the modulus is 1024 bit long or shorter, then the security strength of
694 * the RSA algorithm is less than or equal to 80 bits and therefore an error
695 * rate of 2^-80 is sufficient.
696 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100698 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 mbedtls_mpi_init(&H);
702 mbedtls_mpi_init(&G);
703 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000705 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100706 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
707 goto cleanup;
708 }
709
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000710 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
712 goto cleanup;
713 }
714
715 /*
716 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800717 * 1. |P-Q| > 2^( nbits / 2 - 100 )
718 * 2. GCD( E, (P-1)*(Q-1) ) == 1
719 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 do {
724 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
725 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
728 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800730 /* 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 +0100731 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
732 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000735
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800736 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (H.s < 0) {
738 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
739 }
Janos Follathef441782016-09-21 13:18:12 +0100740
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100741 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
743 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
744 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800745
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800746 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
748 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800749 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800751
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800752 /* 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 +0100753 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
754 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
755 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 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 -0800758 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800760
761 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100764 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
766 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100771
Jethro Beekman97f95c92018-02-13 15:50:36 -0800772#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 * DP = D mod (P - 1)
775 * DQ = D mod (Q - 1)
776 * QP = Q^-1 mod P
777 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
779 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100780#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
Hanno Becker83aad1f2017-08-23 06:45:10 +0100782 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
785cleanup:
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 mbedtls_mpi_free(&H);
788 mbedtls_mpi_free(&G);
789 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (ret != 0) {
792 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if ((-ret & ~0x7f) == 0) {
795 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
796 }
797 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000798 }
799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000801}
802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000804
805/*
806 * Check a public RSA key
807 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100808int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000809{
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
811 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100812 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
815 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100816 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
819 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
820 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
821 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
822 }
823
824 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000825}
826
827/*
Hanno Becker705fc682017-10-10 17:57:02 +0100828 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000831{
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
833 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
834 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 }
Paul Bakker48377d92013-08-30 12:06:24 +0200836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
838 &ctx->D, &ctx->E, NULL, NULL) != 0) {
839 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000840 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000841
Hanno Beckerb269a852017-08-25 08:03:21 +0100842#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
844 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
845 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100846 }
847#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000850}
851
852/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100853 * Check if contexts holding a public and private key match
854 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
856 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100857{
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
859 mbedtls_rsa_check_privkey(prv) != 0) {
860 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100861 }
862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
864 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
865 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100866 }
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100869}
870
871/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 * Do an RSA public key operation
873 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100874int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
875 const unsigned char *input,
876 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000877{
Janos Follath24eed8d2019-11-22 13:21:35 +0000878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000879 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
883 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
884 }
Hanno Becker705fc682017-10-10 17:57:02 +0100885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000887
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200888#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
890 return ret;
891 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200892#endif
893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200897 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
898 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000899 }
900
901 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
903 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000904
905cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
908 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
909 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100910#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if (ret != 0) {
915 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
916 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000919}
920
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200921/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200922 * Generate or update blinding values, see section 10 of:
923 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200924 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200925 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200926 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100927static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
928 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200929{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200930 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200931 mbedtls_mpi R;
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200936 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
938 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
939 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
940 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200941
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200942 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200943 }
944
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200945 /* Unblinding value: Vf = random number, invertible mod N */
946 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200948 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
949 goto cleanup;
950 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 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 +0200953
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200954 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
956 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
957 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200958
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200959 /* At this point, Vi is invertible mod N if and only if both Vf and R
960 * are invertible mod N. If one of them isn't, we don't need to know
961 * which one, we just loop and choose new values for both of them.
962 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
964 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200965 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500969
970 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
972 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200973
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200974 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200975 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 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 +0200977
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200978
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200979cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200983}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200984
Paul Bakker5121ce52009-01-03 21:22:43 +0000985/*
Janos Follath10f83662023-11-21 09:33:54 +0000986 * Unblind
987 * T = T * Vf mod N
988 */
Janos Follath8209ff32023-11-21 12:48:52 +0000989static int rsa_unblind(mbedtls_mpi* T, mbedtls_mpi* Vf, mbedtls_mpi* N)
Janos Follath10f83662023-11-21 09:33:54 +0000990{
991 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
992 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
993 const size_t nlimbs = N->n;
994 const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
995 mbedtls_mpi RR, M_T;
996
997 mbedtls_mpi_init(&RR);
998 mbedtls_mpi_init(&M_T);
999
1000 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
1001 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
1002
1003 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
1004 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
1005
1006 // T = T * R mod N
1007 mbedtls_mpi_core_to_mont_rep(T->p, T->p, N->p, nlimbs, mm, RR.p, M_T.p);
1008 // T = T * Vf mod N
1009 mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1010
1011cleanup:
1012
1013 mbedtls_mpi_free(&RR);
1014 mbedtls_mpi_free(&M_T);
1015
1016 return ret;
1017}
1018
1019/*
Janos Follathe81102e2017-03-22 13:38:28 +00001020 * Exponent blinding supposed to prevent side-channel attacks using multiple
1021 * traces of measurements to recover the RSA key. The more collisions are there,
1022 * the more bits of the key can be recovered. See [3].
1023 *
1024 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001025 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001026 *
1027 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001028 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001029 *
1030 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1031 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1032 * Thus in this sense with 28 byte blinding the security is not reduced by
1033 * side-channel attacks like the one in [3])
1034 *
1035 * This countermeasure does not help if the key recovery is possible with a
1036 * single trace.
1037 */
1038#define RSA_EXPONENT_BLINDING 28
1039
1040/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 * Do an RSA private key operation
1042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1044 int (*f_rng)(void *, unsigned char *, size_t),
1045 void *p_rng,
1046 const unsigned char *input,
1047 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001048{
Janos Follath24eed8d2019-11-22 13:21:35 +00001049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001050 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001051
1052 /* Temporary holding the result */
1053 mbedtls_mpi T;
1054
1055 /* Temporaries holding P-1, Q-1 and the
1056 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001057 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001058
1059#if !defined(MBEDTLS_RSA_NO_CRT)
1060 /* Temporaries holding the results mod p resp. mod q. */
1061 mbedtls_mpi TP, TQ;
1062
1063 /* Temporaries holding the blinded exponents for
1064 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001065 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001066
1067 /* Pointers to actual exponents to be used - either the unblinded
1068 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +00001069 mbedtls_mpi *DP = &ctx->DP;
1070 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +01001071#else
1072 /* Temporary holding the blinded exponent (if used). */
1073 mbedtls_mpi D_blind;
1074
1075 /* Pointer to actual exponent to be used - either the unblinded
1076 * or the blinded one, depending on the presence of a PRNG. */
1077 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +01001078#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001079
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001080 /* Temporaries holding the initial input and the double
1081 * checked result; should be the same in the end. */
1082 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if (f_rng == NULL) {
1085 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1086 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 if (rsa_check_context(ctx, 1 /* private key checks */,
1089 1 /* blinding on */) != 0) {
1090 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001091 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001092
Hanno Becker06811ce2017-05-03 15:10:34 +01001093#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1095 return ret;
1096 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001097#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001098
Hanno Becker06811ce2017-05-03 15:10:34 +01001099 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 mbedtls_mpi_init(&P1);
1103 mbedtls_mpi_init(&Q1);
1104 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001105
Janos Follathe81102e2017-03-22 13:38:28 +00001106#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001108#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 mbedtls_mpi_init(&DP_blind);
1110 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001111#endif
1112
Hanno Becker06811ce2017-05-03 15:10:34 +01001113#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001115#endif
1116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 mbedtls_mpi_init(&I);
1118 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001119
1120 /* End of MPI initialization */
1121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1123 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001124 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1125 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 }
1127
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001128 /*
1129 * Blinding
1130 * T = T * Vi mod N
1131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1133 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1134 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001135
Janos Follath2d8624d2023-11-21 09:46:43 +00001136 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
1137
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001138 /*
1139 * Exponent blinding
1140 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1142 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001143
Janos Follathf9203b42017-03-22 15:13:15 +00001144#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001145 /*
1146 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1149 f_rng, p_rng));
1150 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1151 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1152 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +00001153
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001154 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001155#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001156 /*
1157 * DP_blind = ( P - 1 ) * R + DP
1158 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1160 f_rng, p_rng));
1161 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1162 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1163 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001164
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001165 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001166
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001167 /*
1168 * DQ_blind = ( Q - 1 ) * R + DQ
1169 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1171 f_rng, p_rng));
1172 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1173 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1174 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001175
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001176 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001177#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001181#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001182 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001183 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001185 * TP = input ^ dP mod P
1186 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1190 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001191
1192 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001193 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1196 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1197 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001198
1199 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001200 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1203 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001205
Hanno Becker2dec5e82017-10-03 07:49:52 +01001206 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1208 &ctx->N, &ctx->RN));
1209 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001210 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1211 goto cleanup;
1212 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001213
Janos Follath2d8624d2023-11-21 09:46:43 +00001214 /*
1215 * Unblind
1216 * T = T * Vf mod N
1217 */
1218 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1219
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001222
1223cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1226 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1227 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001228#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 mbedtls_mpi_free(&P1);
1231 mbedtls_mpi_free(&Q1);
1232 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001233
Janos Follathe81102e2017-03-22 13:38:28 +00001234#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001236#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 mbedtls_mpi_free(&DP_blind);
1238 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001239#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001242
1243#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001245#endif
1246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 mbedtls_mpi_free(&C);
1248 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (ret != 0 && ret >= -0x007f) {
1251 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1252 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001255}
1256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001258/**
1259 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1260 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001261 * \param dst buffer to mask
1262 * \param dlen length of destination buffer
1263 * \param src source of the mask generation
1264 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001265 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001266 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001267static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1268 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001269{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001270 unsigned char counter[4];
1271 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001272 unsigned int hlen;
1273 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001274 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001275 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001276 const mbedtls_md_info_t *md_info;
1277 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 mbedtls_md_init(&md_ctx);
1280 md_info = mbedtls_md_info_from_type(md_alg);
1281 if (md_info == NULL) {
1282 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1283 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 mbedtls_md_init(&md_ctx);
1286 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001287 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 memset(mask, 0, sizeof(mask));
1293 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001294
Simon Butcher02037452016-03-01 21:19:12 +00001295 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001296 p = dst;
1297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001299 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001301 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001305 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 }
1307 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001308 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 }
1310 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001311 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 }
1313 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001314 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001318 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001320
1321 counter[3]++;
1322
1323 dlen -= use_len;
1324 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001325
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001331}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001332
1333/**
1334 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1335 *
1336 * \param hash the input hash
1337 * \param hlen length of the input hash
1338 * \param salt the input salt
1339 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001340 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001341 * \param md_alg message digest to use
1342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001343static int hash_mprime(const unsigned char *hash, size_t hlen,
1344 const unsigned char *salt, size_t slen,
1345 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001346{
1347 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001348
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001349 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1353 if (md_info == NULL) {
1354 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1355 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 mbedtls_md_init(&md_ctx);
1358 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001359 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 }
1361 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001362 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 }
1364 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001365 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 }
1367 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001368 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 }
1370 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001371 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 }
1373 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001374 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001376
1377exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001381}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001382
1383/**
1384 * Compute a hash.
1385 *
1386 * \param md_alg algorithm to use
1387 * \param input input message to hash
1388 * \param ilen input length
1389 * \param output the output buffer - must be large enough for \p md_alg
1390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001391static int compute_hash(mbedtls_md_type_t md_alg,
1392 const unsigned char *input, size_t ilen,
1393 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001394{
1395 const mbedtls_md_info_t *md_info;
1396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 md_info = mbedtls_md_info_from_type(md_alg);
1398 if (md_info == NULL) {
1399 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1400 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001407/*
1408 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1409 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001410int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1411 int (*f_rng)(void *, unsigned char *, size_t),
1412 void *p_rng,
1413 const unsigned char *label, size_t label_len,
1414 size_t ilen,
1415 const unsigned char *input,
1416 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001417{
1418 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001419 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001420 unsigned char *p = output;
1421 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if (f_rng == NULL) {
1424 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1425 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001426
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001427 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (hlen == 0) {
1429 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1430 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001431
1432 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001433
Simon Butcher02037452016-03-01 21:19:12 +00001434 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1436 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1437 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001440
1441 *p++ = 0;
1442
Simon Butcher02037452016-03-01 21:19:12 +00001443 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1445 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1446 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001447
1448 p += hlen;
1449
Simon Butcher02037452016-03-01 21:19:12 +00001450 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1452 if (ret != 0) {
1453 return ret;
1454 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001455 p += hlen;
1456 p += olen - 2 * hlen - 2 - ilen;
1457 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (ilen != 0) {
1459 memcpy(p, input, ilen);
1460 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001461
Simon Butcher02037452016-03-01 21:19:12 +00001462 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001464 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 return ret;
1466 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001467
Simon Butcher02037452016-03-01 21:19:12 +00001468 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001470 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 return ret;
1472 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001475}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001479/*
1480 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1481 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001482int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1483 int (*f_rng)(void *, unsigned char *, size_t),
1484 void *p_rng, size_t ilen,
1485 const unsigned char *input,
1486 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001487{
1488 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001490 unsigned char *p = output;
1491
Paul Bakkerb3869132013-02-28 17:21:01 +01001492 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001493
Simon Butcher02037452016-03-01 21:19:12 +00001494 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (ilen + 11 < ilen || olen < ilen + 11) {
1496 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1497 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001498
1499 nb_pad = olen - 3 - ilen;
1500
1501 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (f_rng == NULL) {
1504 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1505 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001506
1507 *p++ = MBEDTLS_RSA_CRYPT;
1508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001510 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001511
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001512 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 ret = f_rng(p_rng, p, 1);
1514 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001515
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001516 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if (rng_dl == 0 || ret != 0) {
1518 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1519 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001520
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001521 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001522 }
1523
1524 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if (ilen != 0) {
1526 memcpy(p, input, ilen);
1527 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001530}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001532
Paul Bakker5121ce52009-01-03 21:22:43 +00001533/*
1534 * Add the message padding, then do an RSA operation
1535 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001536int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1537 int (*f_rng)(void *, unsigned char *, size_t),
1538 void *p_rng,
1539 size_t ilen,
1540 const unsigned char *input,
1541 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001542{
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_PKCS1_V15)
1545 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1547 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001548#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#if defined(MBEDTLS_PKCS1_V21)
1551 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1553 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001554#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001555
1556 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001559}
1560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001562/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001563 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001565int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1566 int (*f_rng)(void *, unsigned char *, size_t),
1567 void *p_rng,
1568 const unsigned char *label, size_t label_len,
1569 size_t *olen,
1570 const unsigned char *input,
1571 unsigned char *output,
1572 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001573{
Janos Follath24eed8d2019-11-22 13:21:35 +00001574 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001575 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001576 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001577 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001579 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001580 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001581
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001582 /*
1583 * Parameters sanity checks
1584 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1586 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
1589 ilen = ctx->len;
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (ilen < 16 || ilen > sizeof(buf)) {
1592 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001595 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (hlen == 0) {
1597 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1598 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001599
Janos Follathc17cda12016-02-11 11:08:18 +00001600 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if (2 * hlen + 2 > ilen) {
1602 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1603 }
Janos Follathc17cda12016-02-11 11:08:18 +00001604
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001605 /*
1606 * RSA operation
1607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001611 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001614 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001615 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001616 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001617 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001619 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 /* DB: Apply dbMask to maskedDB */
1621 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001622 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001623 goto cleanup;
1624 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001625
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001626 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1628 label, label_len, lhash);
1629 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001630 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001632
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001633 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001634 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001635 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001636 p = buf;
1637
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001638 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001639
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001640 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001641
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001642 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001643 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001644 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001645
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001646 /* Get zero-padding len, but always read till end of buffer
1647 * (minus one, for the 01 byte) */
1648 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001649 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001651 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1652 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001653 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001654
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001655 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001656 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001657
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001658 /*
1659 * The only information "leaked" is whether the padding was correct or not
1660 * (eg, no data is copied if it was not correct). This meets the
1661 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1662 * the different error conditions.
1663 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001664 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001665 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1666 goto cleanup;
1667 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001670 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1671 goto cleanup;
1672 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001673
1674 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 if (*olen != 0) {
1676 memcpy(output, p, *olen);
1677 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001678 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001679
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001680cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 mbedtls_platform_zeroize(buf, sizeof(buf));
1682 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001689/*
1690 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1691 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001692int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1693 int (*f_rng)(void *, unsigned char *, size_t),
1694 void *p_rng,
1695 size_t *olen,
1696 const unsigned char *input,
1697 unsigned char *output,
1698 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001699{
1700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1701 size_t ilen;
1702 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1703
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001704 ilen = ctx->len;
1705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1707 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1708 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 if (ilen < 16 || ilen > sizeof(buf)) {
1711 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1712 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001717 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1721 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001722
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001723cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001727}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001729
1730/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001731 * Do an RSA operation, then remove the message padding
1732 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001733int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1734 int (*f_rng)(void *, unsigned char *, size_t),
1735 void *p_rng,
1736 size_t *olen,
1737 const unsigned char *input,
1738 unsigned char *output,
1739 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001740{
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#if defined(MBEDTLS_PKCS1_V15)
1743 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1745 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001746#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748#if defined(MBEDTLS_PKCS1_V21)
1749 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1751 olen, input, output,
1752 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001753#endif
1754
1755 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001757 }
1758}
1759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001761static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1762 int (*f_rng)(void *, unsigned char *, size_t),
1763 void *p_rng,
1764 mbedtls_md_type_t md_alg,
1765 unsigned int hashlen,
1766 const unsigned char *hash,
1767 int saltlen,
1768 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001769{
1770 size_t olen;
1771 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001772 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001773 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001775 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001778 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1782 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1783 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (f_rng == NULL) {
1786 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1787 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001788
1789 olen = ctx->len;
1790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001792 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001793 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if (exp_hashlen == 0) {
1795 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1796 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 if (hashlen != exp_hashlen) {
1799 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1800 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001801 }
1802
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001803 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 if (hlen == 0) {
1805 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1806 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1809 /* Calculate the largest possible salt length, up to the hash size.
1810 * Normally this is the hash length, which is the maximum salt length
1811 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1812 * enough room, use the maximum salt length that fits. The constraint is
1813 * that the hash length plus the salt length plus 2 bytes must be at most
1814 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1815 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001816 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 if (olen < hlen + min_slen + 2) {
1818 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1819 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001820 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001822 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 }
1824 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1825 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1826 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001827 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001828 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001831
Simon Butcher02037452016-03-01 21:19:12 +00001832 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001834 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001835 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001836
1837 /* Generate salt of length slen in place in the encoded message */
1838 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1840 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1841 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001842
Paul Bakkerb3869132013-02-28 17:21:01 +01001843 p += slen;
1844
Simon Butcher02037452016-03-01 21:19:12 +00001845 /* Generate H = Hash( M' ) */
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001846 ret = hash_mprime(hash, hashlen, salt, slen, p, (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (ret != 0) {
1848 return ret;
1849 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001850
Simon Butcher02037452016-03-01 21:19:12 +00001851 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001853 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001855
Simon Butcher02037452016-03-01 21:19:12 +00001856 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001858 (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 if (ret != 0) {
1860 return ret;
1861 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1864 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001865
1866 p += hlen;
1867 *p++ = 0xBC;
1868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001870}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001871
1872/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001873 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1874 * the option to pass in the salt length.
1875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001876int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1877 int (*f_rng)(void *, unsigned char *, size_t),
1878 void *p_rng,
1879 mbedtls_md_type_t md_alg,
1880 unsigned int hashlen,
1881 const unsigned char *hash,
1882 int saltlen,
1883 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001884{
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1886 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001887}
1888
1889
1890/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001891 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1892 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001893int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1894 int (*f_rng)(void *, unsigned char *, size_t),
1895 void *p_rng,
1896 mbedtls_md_type_t md_alg,
1897 unsigned int hashlen,
1898 const unsigned char *hash,
1899 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001900{
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1902 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001907/*
1908 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1909 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001910
1911/* Construct a PKCS v1.5 encoding of a hashed message
1912 *
1913 * This is used both for signature generation and verification.
1914 *
1915 * Parameters:
1916 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001917 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001918 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001919 * - hash: Buffer containing the hashed message or the raw data.
1920 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001921 * - dst: Buffer to hold the encoded message.
1922 *
1923 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001924 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001925 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001926 *
1927 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001928static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1929 unsigned int hashlen,
1930 const unsigned char *hash,
1931 size_t dst_len,
1932 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001933{
1934 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001935 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001936 unsigned char *p = dst;
1937 const char *oid = NULL;
1938
1939 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001941 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (md_size == 0) {
1943 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1944 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1947 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1948 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (hashlen != md_size) {
1951 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1952 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001953
1954 /* Double-check that 8 + hashlen + oid_size can be used as a
1955 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001957 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 10 + hashlen + oid_size < 10 + hashlen) {
1959 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1960 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001961
1962 /*
1963 * Static bounds check:
1964 * - Need 10 bytes for five tag-length pairs.
1965 * (Insist on 1-byte length encodings to protect against variants of
1966 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1967 * - Need hashlen bytes for hash
1968 * - Need oid_size bytes for hash alg OID.
1969 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 if (nb_pad < 10 + hashlen + oid_size) {
1971 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1972 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001973 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 } else {
1975 if (nb_pad < hashlen) {
1976 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1977 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001978
1979 nb_pad -= hashlen;
1980 }
1981
Hanno Becker2b2f8982017-09-27 17:10:03 +01001982 /* Need space for signature header and padding delimiter (3 bytes),
1983 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (nb_pad < 3 + 8) {
1985 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1986 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001987 nb_pad -= 3;
1988
1989 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001990 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001991
1992 /* Write signature header and padding */
1993 *p++ = 0;
1994 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001996 p += nb_pad;
1997 *p++ = 0;
1998
1999 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 if (md_alg == MBEDTLS_MD_NONE) {
2001 memcpy(p, hash, hashlen);
2002 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002003 }
2004
2005 /* Signing hashed data, add corresponding ASN.1 structure
2006 *
2007 * DigestInfo ::= SEQUENCE {
2008 * digestAlgorithm DigestAlgorithmIdentifier,
2009 * digest Digest }
2010 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2011 * Digest ::= OCTET STRING
2012 *
2013 * Schematic:
2014 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2015 * TAG-NULL + LEN [ NULL ] ]
2016 * TAG-OCTET + LEN [ HASH ] ]
2017 */
2018 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002020 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002022 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002023 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002025 p += oid_size;
2026 *p++ = MBEDTLS_ASN1_NULL;
2027 *p++ = 0x00;
2028 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002029 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002031 p += hashlen;
2032
2033 /* Just a sanity-check, should be automatic
2034 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 if (p != dst + dst_len) {
2036 mbedtls_platform_zeroize(dst, dst_len);
2037 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002038 }
2039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002041}
2042
Paul Bakkerb3869132013-02-28 17:21:01 +01002043/*
2044 * Do an RSA operation to sign the message digest
2045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002046int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2047 int (*f_rng)(void *, unsigned char *, size_t),
2048 void *p_rng,
2049 mbedtls_md_type_t md_alg,
2050 unsigned int hashlen,
2051 const unsigned char *hash,
2052 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002053{
Janos Follath24eed8d2019-11-22 13:21:35 +00002054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002055 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002058 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2062 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2063 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002064
Hanno Beckerfdf38032017-09-06 12:35:55 +01002065 /*
2066 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2067 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2070 ctx->len, sig)) != 0) {
2071 return ret;
2072 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002073
Hanno Beckerfdf38032017-09-06 12:35:55 +01002074 /* Private key operation
2075 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002076 * In order to prevent Lenstra's attack, make the signature in a
2077 * temporary buffer and check it before returning it.
2078 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 sig_try = mbedtls_calloc(1, ctx->len);
2081 if (sig_try == NULL) {
2082 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002083 }
2084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 verif = mbedtls_calloc(1, ctx->len);
2086 if (verif == NULL) {
2087 mbedtls_free(sig_try);
2088 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2089 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002090
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2092 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2093
2094 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002095 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2096 goto cleanup;
2097 }
2098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002100
2101cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002102 mbedtls_zeroize_and_free(sig_try, ctx->len);
2103 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002104
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 if (ret != 0) {
2106 memset(sig, '!', ctx->len);
2107 }
2108 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002111
2112/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 * Do an RSA operation to sign the message digest
2114 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002115int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2116 int (*f_rng)(void *, unsigned char *, size_t),
2117 void *p_rng,
2118 mbedtls_md_type_t md_alg,
2119 unsigned int hashlen,
2120 const unsigned char *hash,
2121 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002122{
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002124 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128#if defined(MBEDTLS_PKCS1_V15)
2129 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2131 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002132#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134#if defined(MBEDTLS_PKCS1_V21)
2135 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2137 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002138#endif
2139
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002143}
2144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002146/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002147 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002149int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2150 mbedtls_md_type_t md_alg,
2151 unsigned int hashlen,
2152 const unsigned char *hash,
2153 mbedtls_md_type_t mgf1_hash_id,
2154 int expected_salt_len,
2155 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002156{
Janos Follath24eed8d2019-11-22 13:21:35 +00002157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002158 size_t siglen;
2159 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002160 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002161 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002162 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002163 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002167 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002169
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 siglen = ctx->len;
2171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if (siglen < 16 || siglen > sizeof(buf)) {
2173 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2174 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if (ret != 0) {
2179 return ret;
2180 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002181
2182 p = buf;
2183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (buf[siglen - 1] != 0xBC) {
2185 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002186 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (md_alg != MBEDTLS_MD_NONE) {
2189 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002190 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (exp_hashlen == 0) {
2192 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2193 }
2194
2195 if (hashlen != exp_hashlen) {
2196 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2197 }
2198 }
2199
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002200 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (hlen == 0) {
2202 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2203 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002204
Simon Butcher02037452016-03-01 21:19:12 +00002205 /*
2206 * Note: EMSA-PSS verification is over the length of N - 1 bits
2207 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (buf[0] >> (8 - siglen * 8 + msb)) {
2211 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2212 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002213
Simon Butcher02037452016-03-01 21:19:12 +00002214 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002216 p++;
2217 siglen -= 1;
2218 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if (siglen < hlen + 2) {
2221 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2222 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002223 hash_start = p + siglen - hlen - 1;
2224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2226 if (ret != 0) {
2227 return ret;
2228 }
Paul Bakker02303e82013-01-03 11:08:31 +01002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002233 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 if (*p++ != 0x01) {
2237 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2238 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002239
Gilles Peskine6a54b022017-10-17 19:02:13 +02002240 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002241
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2243 observed_salt_len != (size_t) expected_salt_len) {
2244 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002245 }
2246
Simon Butcher02037452016-03-01 21:19:12 +00002247 /*
2248 * Generate H = Hash( M' )
2249 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2251 result, mgf1_hash_id);
2252 if (ret != 0) {
2253 return ret;
2254 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 if (memcmp(hash_start, result, hlen) != 0) {
2257 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2258 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002261}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002262
2263/*
2264 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002266int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2267 mbedtls_md_type_t md_alg,
2268 unsigned int hashlen,
2269 const unsigned char *hash,
2270 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002271{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002272 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002274 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002279 : md_alg;
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2282 md_alg, hashlen, hash,
2283 mgf1_hash_id,
2284 MBEDTLS_RSA_SALT_LEN_ANY,
2285 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002286
2287}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002291/*
2292 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2293 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002294int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2295 mbedtls_md_type_t md_alg,
2296 unsigned int hashlen,
2297 const unsigned char *hash,
2298 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002299{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002300 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002301 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002302 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002305 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002307
2308 sig_len = ctx->len;
2309
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002310 /*
2311 * Prepare expected PKCS1 v1.5 encoding of hash.
2312 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2315 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002316 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2317 goto cleanup;
2318 }
2319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2321 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002322 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002324
2325 /*
2326 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2327 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 ret = mbedtls_rsa_public(ctx, sig, encoded);
2330 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002331 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002333
Simon Butcher02037452016-03-01 21:19:12 +00002334 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002335 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002336 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2339 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002340 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2341 goto cleanup;
2342 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002343
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002344cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002347 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002348 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002351 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002352 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002357
2358/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002359 * Do an RSA operation and check the message digest
2360 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002361int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2362 mbedtls_md_type_t md_alg,
2363 unsigned int hashlen,
2364 const unsigned char *hash,
2365 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002366{
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002368 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372#if defined(MBEDTLS_PKCS1_V15)
2373 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2375 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002376#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378#if defined(MBEDTLS_PKCS1_V21)
2379 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2381 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002382#endif
2383
2384 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002386 }
2387}
2388
2389/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002390 * Copy the components of an RSA key
2391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002393{
Janos Follath24eed8d2019-11-22 13:21:35 +00002394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002395
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002396 dst->len = src->len;
2397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2399 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2402 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2403 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002404
2405#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2407 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2408 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2409 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2410 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002411#endif
2412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2416 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002417
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002418 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002419 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002420
2421cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 if (ret != 0) {
2423 mbedtls_rsa_free(dst);
2424 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002427}
2428
2429/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 * Free the components of an RSA key
2431 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002432void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002433{
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002435 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 mbedtls_mpi_free(&ctx->Vi);
2439 mbedtls_mpi_free(&ctx->Vf);
2440 mbedtls_mpi_free(&ctx->RN);
2441 mbedtls_mpi_free(&ctx->D);
2442 mbedtls_mpi_free(&ctx->Q);
2443 mbedtls_mpi_free(&ctx->P);
2444 mbedtls_mpi_free(&ctx->E);
2445 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002446
Hanno Becker33c30a02017-08-23 07:00:22 +01002447#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 mbedtls_mpi_free(&ctx->RQ);
2449 mbedtls_mpi_free(&ctx->RP);
2450 mbedtls_mpi_free(&ctx->QP);
2451 mbedtls_mpi_free(&ctx->DQ);
2452 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002453#endif /* MBEDTLS_RSA_NO_CRT */
2454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002456 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 if (ctx->ver != 0) {
2458 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002459 ctx->ver = 0;
2460 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002461#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002462}
2463
Hanno Beckerab377312017-08-23 16:24:51 +01002464#endif /* !MBEDTLS_RSA_ALT */
2465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002467
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002468#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
2470/*
2471 * Example RSA-1024 keypair, for test purposes
2472 */
2473#define KEY_LEN 128
2474
2475#define RSA_N "9292758453063D803DD603D5E777D788" \
2476 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2477 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2478 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2479 "93A89813FBF3C4F8066D2D800F7C38A8" \
2480 "1AE31942917403FF4946B0A83D3D3E05" \
2481 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2482 "5E94BB77B07507233A0BC7BAC8F90F79"
2483
2484#define RSA_E "10001"
2485
2486#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2487 "66CA472BC44D253102F8B4A9D3BFA750" \
2488 "91386C0077937FE33FA3252D28855837" \
2489 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2490 "DF79C5CE07EE72C7F123142198164234" \
2491 "CABB724CF78B8173B9F880FC86322407" \
2492 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2493 "071513A1E85B5DFA031F21ECAE91A34D"
2494
2495#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2496 "2C01CAD19EA484A87EA4377637E75500" \
2497 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2498 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2499
2500#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2501 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2502 "910E4168387E3C30AA1E00C339A79508" \
2503 "8452DD96A9A5EA5D9DCA68DA636032AF"
2504
Paul Bakker5121ce52009-01-03 21:22:43 +00002505#define PT_LEN 24
2506#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2507 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002510static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002511{
gufe44c2620da2020-08-03 17:56:50 +02002512#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002513 size_t i;
2514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002516 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 }
Paul Bakker545570e2010-07-18 09:00:25 +00002518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002520 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002522#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002524 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002528#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002531}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002533
Paul Bakker5121ce52009-01-03 21:22:43 +00002534/*
2535 * Checkup routine
2536 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002537int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002538{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002539 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002541 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 unsigned char rsa_plaintext[PT_LEN];
2544 unsigned char rsa_decrypted[PT_LEN];
2545 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002546#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002547 unsigned char sha1sum[20];
2548#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Hanno Becker3a701162017-08-22 13:52:43 +01002550 mbedtls_mpi K;
2551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 mbedtls_mpi_init(&K);
2553 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2556 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2557 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2558 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2559 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2560 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2561 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2562 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2563 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2564 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (verbose != 0) {
2569 mbedtls_printf(" RSA key validation: ");
2570 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2573 mbedtls_rsa_check_privkey(&rsa) != 0) {
2574 if (verbose != 0) {
2575 mbedtls_printf("failed\n");
2576 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Hanno Becker5bc87292017-05-03 15:09:31 +01002578 ret = 1;
2579 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 }
2581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (verbose != 0) {
2583 mbedtls_printf("passed\n PKCS#1 encryption : ");
2584 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2589 PT_LEN, rsa_plaintext,
2590 rsa_ciphertext) != 0) {
2591 if (verbose != 0) {
2592 mbedtls_printf("failed\n");
2593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
Hanno Becker5bc87292017-05-03 15:09:31 +01002595 ret = 1;
2596 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 }
2598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (verbose != 0) {
2600 mbedtls_printf("passed\n PKCS#1 decryption : ");
2601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2604 &len, rsa_ciphertext, rsa_decrypted,
2605 sizeof(rsa_decrypted)) != 0) {
2606 if (verbose != 0) {
2607 mbedtls_printf("failed\n");
2608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
Hanno Becker5bc87292017-05-03 15:09:31 +01002610 ret = 1;
2611 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 }
2613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2615 if (verbose != 0) {
2616 mbedtls_printf("failed\n");
2617 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
Hanno Becker5bc87292017-05-03 15:09:31 +01002619 ret = 1;
2620 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 }
2622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (verbose != 0) {
2624 mbedtls_printf("passed\n");
2625 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002626
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002627#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (verbose != 0) {
2629 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002630 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002632 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2633 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (verbose != 0) {
2635 mbedtls_printf("failed\n");
2636 }
2637
2638 return 1;
2639 }
2640
2641 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2642 MBEDTLS_MD_SHA1, 20,
2643 sha1sum, rsa_ciphertext) != 0) {
2644 if (verbose != 0) {
2645 mbedtls_printf("failed\n");
2646 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Hanno Becker5bc87292017-05-03 15:09:31 +01002648 ret = 1;
2649 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 }
2651
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 if (verbose != 0) {
2653 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2654 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2657 sha1sum, rsa_ciphertext) != 0) {
2658 if (verbose != 0) {
2659 mbedtls_printf("failed\n");
2660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Hanno Becker5bc87292017-05-03 15:09:31 +01002662 ret = 1;
2663 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 }
2665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 if (verbose != 0) {
2667 mbedtls_printf("passed\n");
2668 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002669#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 if (verbose != 0) {
2672 mbedtls_printf("\n");
2673 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002674
Paul Bakker3d8fb632014-04-17 12:42:41 +02002675cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 mbedtls_mpi_free(&K);
2677 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002679 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002682}
2683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#endif /* MBEDTLS_RSA_C */