blob: 1357e1bc02df82ca8a122ad3bad7bd7b8ca4e874 [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 Follathf7f88d62023-11-21 14:20:08 +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
Janos Follathdad6d662023-12-27 10:22:59 +00001006 /* T = T * Vf mod N
1007 * Reminder: montmul(A, B, N) = A * B * R^-1 mod N
1008 * Usually both operands are multiplied by R mod N beforehand (by calling
1009 * `to_mont_rep()` on them), yielding a result that's also * R mod N (aka
1010 * "in the Montgomery domain"). Here we only multiply one operand by R mod
1011 * N, so the result is directly what we want - no need to call
1012 * `from_mont_rep()` on it. */
Janos Follath10f83662023-11-21 09:33:54 +00001013 mbedtls_mpi_core_to_mont_rep(T->p, T->p, N->p, nlimbs, mm, RR.p, M_T.p);
Janos Follath10f83662023-11-21 09:33:54 +00001014 mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1015
1016cleanup:
1017
1018 mbedtls_mpi_free(&RR);
1019 mbedtls_mpi_free(&M_T);
1020
1021 return ret;
1022}
1023
1024/*
Janos Follathe81102e2017-03-22 13:38:28 +00001025 * Exponent blinding supposed to prevent side-channel attacks using multiple
1026 * traces of measurements to recover the RSA key. The more collisions are there,
1027 * the more bits of the key can be recovered. See [3].
1028 *
1029 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001030 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001031 *
1032 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001033 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001034 *
1035 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1036 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1037 * Thus in this sense with 28 byte blinding the security is not reduced by
1038 * side-channel attacks like the one in [3])
1039 *
1040 * This countermeasure does not help if the key recovery is possible with a
1041 * single trace.
1042 */
1043#define RSA_EXPONENT_BLINDING 28
1044
1045/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 * Do an RSA private key operation
1047 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001048int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1049 int (*f_rng)(void *, unsigned char *, size_t),
1050 void *p_rng,
1051 const unsigned char *input,
1052 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001053{
Janos Follath24eed8d2019-11-22 13:21:35 +00001054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001055 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001056
1057 /* Temporary holding the result */
1058 mbedtls_mpi T;
1059
1060 /* Temporaries holding P-1, Q-1 and the
1061 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001062 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001063
1064#if !defined(MBEDTLS_RSA_NO_CRT)
1065 /* Temporaries holding the results mod p resp. mod q. */
1066 mbedtls_mpi TP, TQ;
1067
1068 /* Temporaries holding the blinded exponents for
1069 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001070 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001071#else
1072 /* Temporary holding the blinded exponent (if used). */
1073 mbedtls_mpi D_blind;
Hanno Becker43f94722017-08-25 11:50:00 +01001074#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001075
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001076 /* Temporaries holding the initial input and the double
1077 * checked result; should be the same in the end. */
1078 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if (f_rng == NULL) {
1081 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1082 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if (rsa_check_context(ctx, 1 /* private key checks */,
1085 1 /* blinding on */) != 0) {
1086 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001087 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001088
Hanno Becker06811ce2017-05-03 15:10:34 +01001089#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1091 return ret;
1092 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001093#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001094
Hanno Becker06811ce2017-05-03 15:10:34 +01001095 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 mbedtls_mpi_init(&P1);
1099 mbedtls_mpi_init(&Q1);
1100 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001101
Janos Follathe81102e2017-03-22 13:38:28 +00001102#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001104#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 mbedtls_mpi_init(&DP_blind);
1106 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001107#endif
1108
Hanno Becker06811ce2017-05-03 15:10:34 +01001109#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001111#endif
1112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 mbedtls_mpi_init(&I);
1114 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001115
1116 /* End of MPI initialization */
1117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1119 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001120 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1121 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001122 }
1123
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001124 /*
1125 * Blinding
1126 * T = T * Vi mod N
1127 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1129 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1130 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001131
Janos Follath2d8624d2023-11-21 09:46:43 +00001132 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
1133
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001134 /*
1135 * Exponent blinding
1136 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1138 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001139
Janos Follathf9203b42017-03-22 15:13:15 +00001140#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001141 /*
1142 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1143 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1145 f_rng, p_rng));
1146 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1147 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1148 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathf9203b42017-03-22 15:13:15 +00001149#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001150 /*
1151 * DP_blind = ( P - 1 ) * R + DP
1152 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1154 f_rng, p_rng));
1155 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1156 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1157 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001158
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001159 /*
1160 * DQ_blind = ( Q - 1 ) * R + DQ
1161 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1163 f_rng, p_rng));
1164 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1165 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1166 &ctx->DQ));
Janos Follathe81102e2017-03-22 13:38:28 +00001167#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathc7625212023-12-27 10:33:00 +00001170 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &D_blind, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001171#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001172 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001173 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001175 * TP = input ^ dP mod P
1176 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001178
Janos Follathc7625212023-12-27 10:33:00 +00001179 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, &DP_blind, &ctx->P, &ctx->RP));
1180 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, &DQ_blind, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001181
1182 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001183 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1186 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1187 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001188
1189 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001190 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1193 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001195
Hanno Becker2dec5e82017-10-03 07:49:52 +01001196 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1198 &ctx->N, &ctx->RN));
1199 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001200 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1201 goto cleanup;
1202 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001203
Janos Follath2d8624d2023-11-21 09:46:43 +00001204 /*
1205 * Unblind
1206 * T = T * Vf mod N
1207 */
1208 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1209
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001212
1213cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1216 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1217 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001218#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 mbedtls_mpi_free(&P1);
1221 mbedtls_mpi_free(&Q1);
1222 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001223
Janos Follathe81102e2017-03-22 13:38:28 +00001224#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001226#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 mbedtls_mpi_free(&DP_blind);
1228 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001229#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001232
1233#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001235#endif
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 mbedtls_mpi_free(&C);
1238 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if (ret != 0 && ret >= -0x007f) {
1241 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001245}
1246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001248/**
1249 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1250 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001251 * \param dst buffer to mask
1252 * \param dlen length of destination buffer
1253 * \param src source of the mask generation
1254 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001255 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001257static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1258 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001259{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001260 unsigned char counter[4];
1261 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001262 unsigned int hlen;
1263 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001264 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001265 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001266 const mbedtls_md_info_t *md_info;
1267 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 mbedtls_md_init(&md_ctx);
1270 md_info = mbedtls_md_info_from_type(md_alg);
1271 if (md_info == NULL) {
1272 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1273 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 mbedtls_md_init(&md_ctx);
1276 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001277 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 memset(mask, 0, sizeof(mask));
1283 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001284
Simon Butcher02037452016-03-01 21:19:12 +00001285 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001286 p = dst;
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001289 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001291 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001295 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 }
1297 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001298 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 }
1300 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001301 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 }
1303 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001304 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001308 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001310
1311 counter[3]++;
1312
1313 dlen -= use_len;
1314 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001315
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001321}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001322
1323/**
1324 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1325 *
1326 * \param hash the input hash
1327 * \param hlen length of the input hash
1328 * \param salt the input salt
1329 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001330 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001331 * \param md_alg message digest to use
1332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333static int hash_mprime(const unsigned char *hash, size_t hlen,
1334 const unsigned char *salt, size_t slen,
1335 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001336{
1337 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001338
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001339 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1343 if (md_info == NULL) {
1344 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1345 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 mbedtls_md_init(&md_ctx);
1348 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001349 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 }
1351 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001352 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 }
1354 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001355 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 }
1357 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001358 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 }
1360 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001361 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 }
1363 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001364 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001366
1367exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001371}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001372
1373/**
1374 * Compute a hash.
1375 *
1376 * \param md_alg algorithm to use
1377 * \param input input message to hash
1378 * \param ilen input length
1379 * \param output the output buffer - must be large enough for \p md_alg
1380 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001381static int compute_hash(mbedtls_md_type_t md_alg,
1382 const unsigned char *input, size_t ilen,
1383 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001384{
1385 const mbedtls_md_info_t *md_info;
1386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 md_info = mbedtls_md_info_from_type(md_alg);
1388 if (md_info == NULL) {
1389 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1390 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001393}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001397/*
1398 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1399 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001400int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1401 int (*f_rng)(void *, unsigned char *, size_t),
1402 void *p_rng,
1403 const unsigned char *label, size_t label_len,
1404 size_t ilen,
1405 const unsigned char *input,
1406 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001407{
1408 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001410 unsigned char *p = output;
1411 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (f_rng == NULL) {
1414 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1415 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001416
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001417 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if (hlen == 0) {
1419 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1420 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001421
1422 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001423
Simon Butcher02037452016-03-01 21:19:12 +00001424 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1426 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1427 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001430
1431 *p++ = 0;
1432
Simon Butcher02037452016-03-01 21:19:12 +00001433 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1435 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1436 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001437
1438 p += hlen;
1439
Simon Butcher02037452016-03-01 21:19:12 +00001440 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1442 if (ret != 0) {
1443 return ret;
1444 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001445 p += hlen;
1446 p += olen - 2 * hlen - 2 - ilen;
1447 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if (ilen != 0) {
1449 memcpy(p, input, ilen);
1450 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001451
Simon Butcher02037452016-03-01 21:19:12 +00001452 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001454 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 return ret;
1456 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001457
Simon Butcher02037452016-03-01 21:19:12 +00001458 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001460 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return ret;
1462 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001465}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001469/*
1470 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1471 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1473 int (*f_rng)(void *, unsigned char *, size_t),
1474 void *p_rng, size_t ilen,
1475 const unsigned char *input,
1476 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001477{
1478 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001480 unsigned char *p = output;
1481
Paul Bakkerb3869132013-02-28 17:21:01 +01001482 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001483
Simon Butcher02037452016-03-01 21:19:12 +00001484 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if (ilen + 11 < ilen || olen < ilen + 11) {
1486 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1487 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001488
1489 nb_pad = olen - 3 - ilen;
1490
1491 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (f_rng == NULL) {
1494 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1495 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001496
1497 *p++ = MBEDTLS_RSA_CRYPT;
1498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001500 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001501
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001502 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 ret = f_rng(p_rng, p, 1);
1504 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001505
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001506 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (rng_dl == 0 || ret != 0) {
1508 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1509 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001510
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001511 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001512 }
1513
1514 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if (ilen != 0) {
1516 memcpy(p, input, ilen);
1517 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001522
Paul Bakker5121ce52009-01-03 21:22:43 +00001523/*
1524 * Add the message padding, then do an RSA operation
1525 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001526int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1527 int (*f_rng)(void *, unsigned char *, size_t),
1528 void *p_rng,
1529 size_t ilen,
1530 const unsigned char *input,
1531 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001532{
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#if defined(MBEDTLS_PKCS1_V15)
1535 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1537 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001538#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#if defined(MBEDTLS_PKCS1_V21)
1541 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1543 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001544#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001545
1546 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001549}
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001552/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001553 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001555int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1556 int (*f_rng)(void *, unsigned char *, size_t),
1557 void *p_rng,
1558 const unsigned char *label, size_t label_len,
1559 size_t *olen,
1560 const unsigned char *input,
1561 unsigned char *output,
1562 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001563{
Janos Follath24eed8d2019-11-22 13:21:35 +00001564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001565 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001566 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001567 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001569 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001570 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001571
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001572 /*
1573 * Parameters sanity checks
1574 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1576 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1577 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001578
1579 ilen = ctx->len;
1580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if (ilen < 16 || ilen > sizeof(buf)) {
1582 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001584
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001585 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if (hlen == 0) {
1587 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1588 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001589
Janos Follathc17cda12016-02-11 11:08:18 +00001590 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (2 * hlen + 2 > ilen) {
1592 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1593 }
Janos Follathc17cda12016-02-11 11:08:18 +00001594
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001595 /*
1596 * RSA operation
1597 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001601 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001604 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001605 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001606 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001607 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001609 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 /* DB: Apply dbMask to maskedDB */
1611 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001612 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001613 goto cleanup;
1614 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001615
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001616 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1618 label, label_len, lhash);
1619 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001620 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001622
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001623 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001624 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001625 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001626 p = buf;
1627
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001628 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001629
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001630 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001631
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001632 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001633 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001634 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001635
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001636 /* Get zero-padding len, but always read till end of buffer
1637 * (minus one, for the 01 byte) */
1638 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001639 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001641 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1642 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001643 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001644
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001645 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001646 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001647
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001648 /*
1649 * The only information "leaked" is whether the padding was correct or not
1650 * (eg, no data is copied if it was not correct). This meets the
1651 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1652 * the different error conditions.
1653 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001654 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001655 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1656 goto cleanup;
1657 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001660 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1661 goto cleanup;
1662 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001663
1664 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (*olen != 0) {
1666 memcpy(output, p, *olen);
1667 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001668 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001669
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001670cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 mbedtls_platform_zeroize(buf, sizeof(buf));
1672 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001675}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001679/*
1680 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1681 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001682int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1683 int (*f_rng)(void *, unsigned char *, size_t),
1684 void *p_rng,
1685 size_t *olen,
1686 const unsigned char *input,
1687 unsigned char *output,
1688 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001689{
1690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1691 size_t ilen;
1692 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1693
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001694 ilen = ctx->len;
1695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1697 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1698 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 if (ilen < 16 || ilen > sizeof(buf)) {
1701 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1702 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001707 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1711 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001712
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001713cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001717}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001719
1720/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001721 * Do an RSA operation, then remove the message padding
1722 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001723int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1724 int (*f_rng)(void *, unsigned char *, size_t),
1725 void *p_rng,
1726 size_t *olen,
1727 const unsigned char *input,
1728 unsigned char *output,
1729 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001730{
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732#if defined(MBEDTLS_PKCS1_V15)
1733 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1735 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001736#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738#if defined(MBEDTLS_PKCS1_V21)
1739 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1741 olen, input, output,
1742 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001743#endif
1744
1745 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001747 }
1748}
1749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001751static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1752 int (*f_rng)(void *, unsigned char *, size_t),
1753 void *p_rng,
1754 mbedtls_md_type_t md_alg,
1755 unsigned int hashlen,
1756 const unsigned char *hash,
1757 int saltlen,
1758 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001759{
1760 size_t olen;
1761 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001762 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001763 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001765 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001768 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1772 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1773 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 if (f_rng == NULL) {
1776 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1777 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001778
1779 olen = ctx->len;
1780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001782 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001783 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if (exp_hashlen == 0) {
1785 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1786 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 if (hashlen != exp_hashlen) {
1789 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1790 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001791 }
1792
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001793 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 if (hlen == 0) {
1795 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1796 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1799 /* Calculate the largest possible salt length, up to the hash size.
1800 * Normally this is the hash length, which is the maximum salt length
1801 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1802 * enough room, use the maximum salt length that fits. The constraint is
1803 * that the hash length plus the salt length plus 2 bytes must be at most
1804 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1805 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001806 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 if (olen < hlen + min_slen + 2) {
1808 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1809 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001810 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001812 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 }
1814 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1815 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1816 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001817 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001818 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001821
Simon Butcher02037452016-03-01 21:19:12 +00001822 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001824 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001825 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001826
1827 /* Generate salt of length slen in place in the encoded message */
1828 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1830 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1831 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001832
Paul Bakkerb3869132013-02-28 17:21:01 +01001833 p += slen;
1834
Simon Butcher02037452016-03-01 21:19:12 +00001835 /* Generate H = Hash( M' ) */
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001836 ret = hash_mprime(hash, hashlen, salt, slen, p, (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 if (ret != 0) {
1838 return ret;
1839 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001840
Simon Butcher02037452016-03-01 21:19:12 +00001841 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001843 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001845
Simon Butcher02037452016-03-01 21:19:12 +00001846 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001848 (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if (ret != 0) {
1850 return ret;
1851 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1854 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001855
1856 p += hlen;
1857 *p++ = 0xBC;
1858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001860}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001861
1862/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001863 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1864 * the option to pass in the salt length.
1865 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001866int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1867 int (*f_rng)(void *, unsigned char *, size_t),
1868 void *p_rng,
1869 mbedtls_md_type_t md_alg,
1870 unsigned int hashlen,
1871 const unsigned char *hash,
1872 int saltlen,
1873 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001874{
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1876 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001877}
1878
1879
1880/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001881 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1882 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001883int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1884 int (*f_rng)(void *, unsigned char *, size_t),
1885 void *p_rng,
1886 mbedtls_md_type_t md_alg,
1887 unsigned int hashlen,
1888 const unsigned char *hash,
1889 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001890{
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1892 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001893}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001897/*
1898 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1899 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001900
1901/* Construct a PKCS v1.5 encoding of a hashed message
1902 *
1903 * This is used both for signature generation and verification.
1904 *
1905 * Parameters:
1906 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001907 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001908 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001909 * - hash: Buffer containing the hashed message or the raw data.
1910 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001911 * - dst: Buffer to hold the encoded message.
1912 *
1913 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001914 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001915 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001916 *
1917 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001918static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1919 unsigned int hashlen,
1920 const unsigned char *hash,
1921 size_t dst_len,
1922 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001923{
1924 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001925 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001926 unsigned char *p = dst;
1927 const char *oid = NULL;
1928
1929 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001931 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 if (md_size == 0) {
1933 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1934 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1937 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1938 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if (hashlen != md_size) {
1941 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1942 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001943
1944 /* Double-check that 8 + hashlen + oid_size can be used as a
1945 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001947 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 10 + hashlen + oid_size < 10 + hashlen) {
1949 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1950 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001951
1952 /*
1953 * Static bounds check:
1954 * - Need 10 bytes for five tag-length pairs.
1955 * (Insist on 1-byte length encodings to protect against variants of
1956 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1957 * - Need hashlen bytes for hash
1958 * - Need oid_size bytes for hash alg OID.
1959 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (nb_pad < 10 + hashlen + oid_size) {
1961 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1962 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001963 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 } else {
1965 if (nb_pad < hashlen) {
1966 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1967 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001968
1969 nb_pad -= hashlen;
1970 }
1971
Hanno Becker2b2f8982017-09-27 17:10:03 +01001972 /* Need space for signature header and padding delimiter (3 bytes),
1973 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 if (nb_pad < 3 + 8) {
1975 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1976 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001977 nb_pad -= 3;
1978
1979 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001980 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001981
1982 /* Write signature header and padding */
1983 *p++ = 0;
1984 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001986 p += nb_pad;
1987 *p++ = 0;
1988
1989 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 if (md_alg == MBEDTLS_MD_NONE) {
1991 memcpy(p, hash, hashlen);
1992 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001993 }
1994
1995 /* Signing hashed data, add corresponding ASN.1 structure
1996 *
1997 * DigestInfo ::= SEQUENCE {
1998 * digestAlgorithm DigestAlgorithmIdentifier,
1999 * digest Digest }
2000 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2001 * Digest ::= OCTET STRING
2002 *
2003 * Schematic:
2004 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2005 * TAG-NULL + LEN [ NULL ] ]
2006 * TAG-OCTET + LEN [ HASH ] ]
2007 */
2008 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002010 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002012 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002013 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002015 p += oid_size;
2016 *p++ = MBEDTLS_ASN1_NULL;
2017 *p++ = 0x00;
2018 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002019 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002021 p += hashlen;
2022
2023 /* Just a sanity-check, should be automatic
2024 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 if (p != dst + dst_len) {
2026 mbedtls_platform_zeroize(dst, dst_len);
2027 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002028 }
2029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002031}
2032
Paul Bakkerb3869132013-02-28 17:21:01 +01002033/*
2034 * Do an RSA operation to sign the message digest
2035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2037 int (*f_rng)(void *, unsigned char *, size_t),
2038 void *p_rng,
2039 mbedtls_md_type_t md_alg,
2040 unsigned int hashlen,
2041 const unsigned char *hash,
2042 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002043{
Janos Follath24eed8d2019-11-22 13:21:35 +00002044 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002045 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002048 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2052 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2053 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002054
Hanno Beckerfdf38032017-09-06 12:35:55 +01002055 /*
2056 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2057 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2060 ctx->len, sig)) != 0) {
2061 return ret;
2062 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002063
Hanno Beckerfdf38032017-09-06 12:35:55 +01002064 /* Private key operation
2065 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002066 * In order to prevent Lenstra's attack, make the signature in a
2067 * temporary buffer and check it before returning it.
2068 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 sig_try = mbedtls_calloc(1, ctx->len);
2071 if (sig_try == NULL) {
2072 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002073 }
2074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 verif = mbedtls_calloc(1, ctx->len);
2076 if (verif == NULL) {
2077 mbedtls_free(sig_try);
2078 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2079 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2082 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2083
2084 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002085 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2086 goto cleanup;
2087 }
2088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002090
2091cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002092 mbedtls_zeroize_and_free(sig_try, ctx->len);
2093 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (ret != 0) {
2096 memset(sig, '!', ctx->len);
2097 }
2098 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002101
2102/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 * Do an RSA operation to sign the message digest
2104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002105int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2106 int (*f_rng)(void *, unsigned char *, size_t),
2107 void *p_rng,
2108 mbedtls_md_type_t md_alg,
2109 unsigned int hashlen,
2110 const unsigned char *hash,
2111 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002112{
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002114 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#if defined(MBEDTLS_PKCS1_V15)
2119 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2121 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002122#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#if defined(MBEDTLS_PKCS1_V21)
2125 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2127 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002128#endif
2129
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002133}
2134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002136/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002137 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2140 mbedtls_md_type_t md_alg,
2141 unsigned int hashlen,
2142 const unsigned char *hash,
2143 mbedtls_md_type_t mgf1_hash_id,
2144 int expected_salt_len,
2145 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002146{
Janos Follath24eed8d2019-11-22 13:21:35 +00002147 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002148 size_t siglen;
2149 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002150 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002151 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002152 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002153 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002157 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002159
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 siglen = ctx->len;
2161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 if (siglen < 16 || siglen > sizeof(buf)) {
2163 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if (ret != 0) {
2169 return ret;
2170 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002171
2172 p = buf;
2173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 if (buf[siglen - 1] != 0xBC) {
2175 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002176 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if (md_alg != MBEDTLS_MD_NONE) {
2179 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002180 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (exp_hashlen == 0) {
2182 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2183 }
2184
2185 if (hashlen != exp_hashlen) {
2186 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2187 }
2188 }
2189
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002190 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (hlen == 0) {
2192 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2193 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002194
Simon Butcher02037452016-03-01 21:19:12 +00002195 /*
2196 * Note: EMSA-PSS verification is over the length of N - 1 bits
2197 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (buf[0] >> (8 - siglen * 8 + msb)) {
2201 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2202 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002203
Simon Butcher02037452016-03-01 21:19:12 +00002204 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002206 p++;
2207 siglen -= 1;
2208 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (siglen < hlen + 2) {
2211 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2212 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002213 hash_start = p + siglen - hlen - 1;
2214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2216 if (ret != 0) {
2217 return ret;
2218 }
Paul Bakker02303e82013-01-03 11:08:31 +01002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002223 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (*p++ != 0x01) {
2227 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2228 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002229
Gilles Peskine6a54b022017-10-17 19:02:13 +02002230 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2233 observed_salt_len != (size_t) expected_salt_len) {
2234 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002235 }
2236
Simon Butcher02037452016-03-01 21:19:12 +00002237 /*
2238 * Generate H = Hash( M' )
2239 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2241 result, mgf1_hash_id);
2242 if (ret != 0) {
2243 return ret;
2244 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if (memcmp(hash_start, result, hlen) != 0) {
2247 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2248 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002251}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002252
2253/*
2254 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002256int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2257 mbedtls_md_type_t md_alg,
2258 unsigned int hashlen,
2259 const unsigned char *hash,
2260 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002261{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002262 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002264 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002269 : md_alg;
2270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2272 md_alg, hashlen, hash,
2273 mgf1_hash_id,
2274 MBEDTLS_RSA_SALT_LEN_ANY,
2275 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002276
2277}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002281/*
2282 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2283 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002284int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2285 mbedtls_md_type_t md_alg,
2286 unsigned int hashlen,
2287 const unsigned char *hash,
2288 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002289{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002290 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002291 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002292 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002295 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002297
2298 sig_len = ctx->len;
2299
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002300 /*
2301 * Prepare expected PKCS1 v1.5 encoding of hash.
2302 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2305 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002306 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2307 goto cleanup;
2308 }
2309
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2311 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002312 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002314
2315 /*
2316 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2317 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 ret = mbedtls_rsa_public(ctx, sig, encoded);
2320 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002321 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002323
Simon Butcher02037452016-03-01 21:19:12 +00002324 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002325 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002326 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2329 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002330 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2331 goto cleanup;
2332 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002333
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002334cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002337 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002338 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002341 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002342 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002345}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002347
2348/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002349 * Do an RSA operation and check the message digest
2350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002351int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2352 mbedtls_md_type_t md_alg,
2353 unsigned int hashlen,
2354 const unsigned char *hash,
2355 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002356{
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002358 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362#if defined(MBEDTLS_PKCS1_V15)
2363 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2365 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002366#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368#if defined(MBEDTLS_PKCS1_V21)
2369 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2371 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002372#endif
2373
2374 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002376 }
2377}
2378
2379/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002380 * Copy the components of an RSA key
2381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002382int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002383{
Janos Follath24eed8d2019-11-22 13:21:35 +00002384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002385
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002386 dst->len = src->len;
2387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2389 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2392 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2393 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002394
2395#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2397 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2398 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2399 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2400 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002401#endif
2402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2406 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002407
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002408 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002409 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002410
2411cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 if (ret != 0) {
2413 mbedtls_rsa_free(dst);
2414 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002417}
2418
2419/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 * Free the components of an RSA key
2421 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002422void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002423{
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002425 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 mbedtls_mpi_free(&ctx->Vi);
2429 mbedtls_mpi_free(&ctx->Vf);
2430 mbedtls_mpi_free(&ctx->RN);
2431 mbedtls_mpi_free(&ctx->D);
2432 mbedtls_mpi_free(&ctx->Q);
2433 mbedtls_mpi_free(&ctx->P);
2434 mbedtls_mpi_free(&ctx->E);
2435 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002436
Hanno Becker33c30a02017-08-23 07:00:22 +01002437#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 mbedtls_mpi_free(&ctx->RQ);
2439 mbedtls_mpi_free(&ctx->RP);
2440 mbedtls_mpi_free(&ctx->QP);
2441 mbedtls_mpi_free(&ctx->DQ);
2442 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002443#endif /* MBEDTLS_RSA_NO_CRT */
2444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002446 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (ctx->ver != 0) {
2448 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002449 ctx->ver = 0;
2450 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002451#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002452}
2453
Hanno Beckerab377312017-08-23 16:24:51 +01002454#endif /* !MBEDTLS_RSA_ALT */
2455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002458#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002459
2460/*
2461 * Example RSA-1024 keypair, for test purposes
2462 */
2463#define KEY_LEN 128
2464
2465#define RSA_N "9292758453063D803DD603D5E777D788" \
2466 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2467 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2468 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2469 "93A89813FBF3C4F8066D2D800F7C38A8" \
2470 "1AE31942917403FF4946B0A83D3D3E05" \
2471 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2472 "5E94BB77B07507233A0BC7BAC8F90F79"
2473
2474#define RSA_E "10001"
2475
2476#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2477 "66CA472BC44D253102F8B4A9D3BFA750" \
2478 "91386C0077937FE33FA3252D28855837" \
2479 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2480 "DF79C5CE07EE72C7F123142198164234" \
2481 "CABB724CF78B8173B9F880FC86322407" \
2482 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2483 "071513A1E85B5DFA031F21ECAE91A34D"
2484
2485#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2486 "2C01CAD19EA484A87EA4377637E75500" \
2487 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2488 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2489
2490#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2491 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2492 "910E4168387E3C30AA1E00C339A79508" \
2493 "8452DD96A9A5EA5D9DCA68DA636032AF"
2494
Paul Bakker5121ce52009-01-03 21:22:43 +00002495#define PT_LEN 24
2496#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2497 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002500static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002501{
gufe44c2620da2020-08-03 17:56:50 +02002502#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002503 size_t i;
2504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002506 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 }
Paul Bakker545570e2010-07-18 09:00:25 +00002508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002510 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002512#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002514 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002518#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002523
Paul Bakker5121ce52009-01-03 21:22:43 +00002524/*
2525 * Checkup routine
2526 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002527int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002528{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002529 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002531 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 unsigned char rsa_plaintext[PT_LEN];
2534 unsigned char rsa_decrypted[PT_LEN];
2535 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002536#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002537 unsigned char sha1sum[20];
2538#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Hanno Becker3a701162017-08-22 13:52:43 +01002540 mbedtls_mpi K;
2541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 mbedtls_mpi_init(&K);
2543 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2546 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2547 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2548 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2549 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2550 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2551 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2552 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2553 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2554 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (verbose != 0) {
2559 mbedtls_printf(" RSA key validation: ");
2560 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2563 mbedtls_rsa_check_privkey(&rsa) != 0) {
2564 if (verbose != 0) {
2565 mbedtls_printf("failed\n");
2566 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Hanno Becker5bc87292017-05-03 15:09:31 +01002568 ret = 1;
2569 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 }
2571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (verbose != 0) {
2573 mbedtls_printf("passed\n PKCS#1 encryption : ");
2574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2579 PT_LEN, rsa_plaintext,
2580 rsa_ciphertext) != 0) {
2581 if (verbose != 0) {
2582 mbedtls_printf("failed\n");
2583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Hanno Becker5bc87292017-05-03 15:09:31 +01002585 ret = 1;
2586 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 }
2588
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 if (verbose != 0) {
2590 mbedtls_printf("passed\n PKCS#1 decryption : ");
2591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2594 &len, rsa_ciphertext, rsa_decrypted,
2595 sizeof(rsa_decrypted)) != 0) {
2596 if (verbose != 0) {
2597 mbedtls_printf("failed\n");
2598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Hanno Becker5bc87292017-05-03 15:09:31 +01002600 ret = 1;
2601 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 }
2603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2605 if (verbose != 0) {
2606 mbedtls_printf("failed\n");
2607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002608
Hanno Becker5bc87292017-05-03 15:09:31 +01002609 ret = 1;
2610 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 }
2612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 if (verbose != 0) {
2614 mbedtls_printf("passed\n");
2615 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002616
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002617#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (verbose != 0) {
2619 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002620 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002621
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002622 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2623 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (verbose != 0) {
2625 mbedtls_printf("failed\n");
2626 }
2627
2628 return 1;
2629 }
2630
2631 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2632 MBEDTLS_MD_SHA1, 20,
2633 sha1sum, rsa_ciphertext) != 0) {
2634 if (verbose != 0) {
2635 mbedtls_printf("failed\n");
2636 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Hanno Becker5bc87292017-05-03 15:09:31 +01002638 ret = 1;
2639 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 }
2641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (verbose != 0) {
2643 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2644 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2647 sha1sum, rsa_ciphertext) != 0) {
2648 if (verbose != 0) {
2649 mbedtls_printf("failed\n");
2650 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Hanno Becker5bc87292017-05-03 15:09:31 +01002652 ret = 1;
2653 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 }
2655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 if (verbose != 0) {
2657 mbedtls_printf("passed\n");
2658 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002659#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 if (verbose != 0) {
2662 mbedtls_printf("\n");
2663 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002664
Paul Bakker3d8fb632014-04-17 12:42:41 +02002665cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 mbedtls_mpi_free(&K);
2667 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002669 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002672}
2673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#endif /* MBEDTLS_RSA_C */