blob: 7c0ba0d2cdfd4d20299869d9c7f3174b720ce4cf [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"
Chris Jones66a4cd42021-03-09 16:04:12 +000043#include "rsa_alt_helpers.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000046#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020047#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020048#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020049#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
gufe44c2620da2020-08-03 17:56:50 +020053#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000054#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000055#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010058
Dave Rodgman19e8cd02023-05-09 11:10:21 +010059
60#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
61
62/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
63 * operation (EME-PKCS1-v1_5 decoding).
64 *
65 * \note The return value from this function is a sensitive value
66 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
67 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
68 * is often a situation that an attacker can provoke and leaking which
69 * one is the result is precisely the information the attacker wants.
70 *
71 * \param input The input buffer which is the payload inside PKCS#1v1.5
72 * encryption padding, called the "encoded message EM"
73 * by the terminology.
74 * \param ilen The length of the payload in the \p input buffer.
75 * \param output The buffer for the payload, called "message M" by the
76 * PKCS#1 terminology. This must be a writable buffer of
77 * length \p output_max_len bytes.
78 * \param olen The address at which to store the length of
79 * the payload. This must not be \c NULL.
80 * \param output_max_len The length in bytes of the output buffer \p output.
81 *
82 * \return \c 0 on success.
83 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
84 * The output buffer is too small for the unpadded payload.
85 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
86 * The input doesn't contain properly formatted padding.
87 */
88static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
89 size_t ilen,
90 unsigned char *output,
91 size_t output_max_len,
92 size_t *olen)
93{
94 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
95 size_t i, plaintext_max_size;
96
97 /* The following variables take sensitive values: their value must
98 * not leak into the observable behavior of the function other than
99 * the designated outputs (output, olen, return value). Otherwise
100 * this would open the execution of the function to
101 * side-channel-based variants of the Bleichenbacher padding oracle
102 * attack. Potential side channels include overall timing, memory
103 * access patterns (especially visible to an adversary who has access
104 * to a shared memory cache), and branches (especially visible to
105 * an adversary who has access to a shared code cache or to a shared
106 * branch predictor). */
107 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100108 mbedtls_ct_condition_t bad;
109 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100110 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100111 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100112
113 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
114 : output_max_len;
115
116 /* Check and get padding length in constant time and constant
117 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100118 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100119
120
121 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
122 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100123 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100124
125 /* Read the whole buffer. Set pad_done to nonzero if we find
126 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100127 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100128 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100129 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100130 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100131 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100132 }
133
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100134 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100135 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100136
137 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100138 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100139
140 /* If the padding is valid, set plaintext_size to the number of
141 * remaining bytes after stripping the padding. If the padding
142 * is invalid, avoid leaking this fact through the size of the
143 * output: use the maximum message size that fits in the output
144 * buffer. Do it without branches to avoid leaking the padding
145 * validity through timing. RSA keys are small enough that all the
146 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100147 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100148 bad, (unsigned) plaintext_max_size,
149 (unsigned) (ilen - pad_count - 3));
150
151 /* Set output_too_large to 0 if the plaintext fits in the output
152 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100153 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100154 plaintext_max_size);
155
156 /* Set ret without branches to avoid timing attacks. Return:
157 * - INVALID_PADDING if the padding is bad (bad != 0).
158 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
159 * plaintext does not fit in the output buffer.
160 * - 0 if the padding is correct. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100161 ret = -(int) mbedtls_ct_uint_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100162 bad,
163 (unsigned) (-(MBEDTLS_ERR_RSA_INVALID_PADDING)),
Dave Rodgman98ddc012023-08-10 12:11:31 +0100164 mbedtls_ct_uint_if_else_0(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100165 output_too_large,
166 (unsigned) (-(MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)))
167 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100168
169 /* If the padding is bad or the plaintext is too large, zero the
170 * data that we're about to copy to the output buffer.
171 * We need to copy the same amount of data
172 * from the same buffer whether the padding is good or not to
173 * avoid leaking the padding validity through overall timing or
174 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100175 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100176
177 /* If the plaintext is too large, truncate it to the buffer size.
178 * Copy anyway to avoid revealing the length through timing, because
179 * revealing the length is as bad as revealing the padding validity
180 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100181 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100182 (unsigned) plaintext_max_size,
183 (unsigned) plaintext_size);
184
185 /* Move the plaintext to the leftmost position where it can start in
186 * the working buffer, i.e. make it start plaintext_max_size from
187 * the end of the buffer. Do this with a memory access trace that
188 * does not depend on the plaintext size. After this move, the
189 * starting location of the plaintext is no longer sensitive
190 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100191 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
192 plaintext_max_size,
193 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100194
195 /* Finally copy the decrypted plaintext plus trailing zeros into the output
196 * buffer. If output_max_len is 0, then output may be an invalid pointer
197 * and the result of memcpy() would be undefined; prevent undefined
198 * behavior making sure to depend only on output_max_len (the size of the
199 * user-provided output buffer), which is independent from plaintext
200 * length, validity of padding, success of the decryption, and other
201 * secrets. */
202 if (output_max_len != 0) {
203 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
204 }
205
206 /* Report the amount of data we copied to the output buffer. In case
207 * of errors (bad padding or output too large), the value of *olen
208 * when this function returns is not specified. Making it equivalent
209 * to the good case limits the risks of leaking the padding validity. */
210 *olen = plaintext_size;
211
212 return ret;
213}
214
215#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
216
Hanno Beckera565f542017-10-11 11:00:19 +0100217#if !defined(MBEDTLS_RSA_ALT)
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
220 const mbedtls_mpi *N,
221 const mbedtls_mpi *P, const mbedtls_mpi *Q,
222 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100223{
Janos Follath24eed8d2019-11-22 13:21:35 +0000224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
227 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
228 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
229 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
230 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
231 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100232 }
233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if (N != NULL) {
235 ctx->len = mbedtls_mpi_size(&ctx->N);
236 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100239}
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
242 unsigned char const *N, size_t N_len,
243 unsigned char const *P, size_t P_len,
244 unsigned char const *Q, size_t Q_len,
245 unsigned char const *D, size_t D_len,
246 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100247{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000248 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (N != NULL) {
251 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
252 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100253 }
254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (P != NULL) {
256 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
257 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (Q != NULL) {
260 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
261 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (D != NULL) {
264 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
265 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (E != NULL) {
268 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
269 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100270
271cleanup:
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (ret != 0) {
274 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
275 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100278}
279
Hanno Becker705fc682017-10-10 17:57:02 +0100280/*
281 * Checks whether the context fields are set in such a way
282 * that the RSA primitives will be able to execute without error.
283 * It does *not* make guarantees for consistency of the parameters.
284 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
286 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100287{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100288#if !defined(MBEDTLS_RSA_NO_CRT)
289 /* blinding_needed is only used for NO_CRT to decide whether
290 * P,Q need to be present or not. */
291 ((void) blinding_needed);
292#endif
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
295 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
296 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000297 }
Hanno Becker705fc682017-10-10 17:57:02 +0100298
299 /*
300 * 1. Modular exponentiation needs positive, odd moduli.
301 */
302
303 /* Modular exponentiation wrt. N is always used for
304 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
306 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
307 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100308 }
309
310#if !defined(MBEDTLS_RSA_NO_CRT)
311 /* Modular exponentiation for P and Q is only
312 * used for private key operations and if CRT
313 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if (is_priv &&
315 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
316 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
317 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
318 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
319 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100320 }
321#endif /* !MBEDTLS_RSA_NO_CRT */
322
323 /*
324 * 2. Exponents must be positive
325 */
326
327 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
329 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
330 }
Hanno Becker705fc682017-10-10 17:57:02 +0100331
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100332#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100333 /* For private key operations, use D or DP & DQ
334 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
336 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
337 }
Hanno Becker705fc682017-10-10 17:57:02 +0100338#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (is_priv &&
340 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
341 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
342 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100343 }
344#endif /* MBEDTLS_RSA_NO_CRT */
345
346 /* Blinding shouldn't make exponents negative either,
347 * so check that P, Q >= 1 if that hasn't yet been
348 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100349#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (is_priv && blinding_needed &&
351 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
352 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
353 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100354 }
355#endif
356
357 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100358 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100359#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (is_priv &&
361 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
362 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100363 }
364#endif
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100367}
368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100370{
371 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500372 int have_N, have_P, have_Q, have_D, have_E;
373#if !defined(MBEDTLS_RSA_NO_CRT)
374 int have_DP, have_DQ, have_QP;
375#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500376 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
379 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
380 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
381 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
382 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500383
384#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
386 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
387 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500388#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100389
Hanno Becker617c1ae2017-08-23 14:11:24 +0100390 /*
391 * Check whether provided parameters are enough
392 * to deduce all others. The following incomplete
393 * parameter sets for private keys are supported:
394 *
395 * (1) P, Q missing.
396 * (2) D and potentially N missing.
397 *
398 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100399
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500400 n_missing = have_P && have_Q && have_D && have_E;
401 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
402 d_missing = have_P && have_Q && !have_D && have_E;
403 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100404
405 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500406 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (!is_priv && !is_pub) {
409 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
410 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100411
412 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100413 * Step 1: Deduce N if P, Q are provided.
414 */
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (!have_N && have_P && have_Q) {
417 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
418 &ctx->Q)) != 0) {
419 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100420 }
421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100423 }
424
425 /*
426 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100427 */
428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (pq_missing) {
430 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
431 &ctx->P, &ctx->Q);
432 if (ret != 0) {
433 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
434 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 } else if (d_missing) {
437 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
438 &ctx->Q,
439 &ctx->E,
440 &ctx->D)) != 0) {
441 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100442 }
443 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444
Hanno Becker617c1ae2017-08-23 14:11:24 +0100445 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100446 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100447 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100448 */
449
Hanno Becker23344b52017-08-23 07:43:27 +0100450#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (is_priv && !(have_DP && have_DQ && have_QP)) {
452 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
453 &ctx->DP, &ctx->DQ, &ctx->QP);
454 if (ret != 0) {
455 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
456 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100457 }
Hanno Becker23344b52017-08-23 07:43:27 +0100458#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100459
460 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100461 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100462 */
463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100465}
466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
468 unsigned char *N, size_t N_len,
469 unsigned char *P, size_t P_len,
470 unsigned char *Q, size_t Q_len,
471 unsigned char *D, size_t D_len,
472 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100473{
474 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500475 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100476
477 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500478 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
480 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
481 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
482 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
483 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100486 /* If we're trying to export private parameters for a public key,
487 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (P != NULL || Q != NULL || D != NULL) {
489 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
490 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100491
492 }
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (N != NULL) {
495 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
496 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (P != NULL) {
499 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
500 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (Q != NULL) {
503 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
504 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (D != NULL) {
507 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
508 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (E != NULL) {
511 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
512 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100513
514cleanup:
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100517}
518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
520 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
521 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100522{
Janos Follath24eed8d2019-11-22 13:21:35 +0000523 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500524 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100525
526 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500527 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
529 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
530 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
531 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
532 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100535 /* If we're trying to export private parameters for a public key,
536 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (P != NULL || Q != NULL || D != NULL) {
538 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
539 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100540
541 }
542
543 /* Export all requested core parameters. */
544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
546 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
547 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
548 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
549 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
550 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100551 }
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100554}
555
556/*
557 * Export CRT parameters
558 * This must also be implemented if CRT is not used, for being able to
559 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
560 * can be used in this case.
561 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
563 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100564{
Janos Follath24eed8d2019-11-22 13:21:35 +0000565 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500566 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100567
568 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500569 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
571 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
572 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
573 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
574 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (!is_priv) {
577 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
578 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100579
Hanno Beckerdc95c892017-08-23 06:57:02 +0100580#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100581 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
583 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
584 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
585 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100586 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100587#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
589 DP, DQ, QP)) != 0) {
590 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100591 }
592#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100595}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100596
Paul Bakker5121ce52009-01-03 21:22:43 +0000597/*
598 * Initialize an RSA context
599 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100600void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000601{
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
Ronald Cronc1905a12021-06-05 11:11:14 +0200604 ctx->padding = MBEDTLS_RSA_PKCS_V15;
605 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100608 /* Set ctx->ver to nonzero to indicate that the mutex has been
609 * initialized and will need to be freed. */
610 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200612#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000613}
614
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100615/*
616 * Set padding for an existing RSA context
617 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
619 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100620{
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200622#if defined(MBEDTLS_PKCS1_V15)
623 case MBEDTLS_RSA_PKCS_V15:
624 break;
625#endif
626
627#if defined(MBEDTLS_PKCS1_V21)
628 case MBEDTLS_RSA_PKCS_V21:
629 break;
630#endif
631 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200633 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200634
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200635#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
637 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200638 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200639 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 return MBEDTLS_ERR_RSA_INVALID_PADDING;
641 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200642 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200643#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500644
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100645 ctx->padding = padding;
646 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100649}
650
Hanno Becker617c1ae2017-08-23 14:11:24 +0100651/*
Yanray Wang83548b52023-03-15 16:46:34 +0800652 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800653 */
654int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
655{
Yanray Wang644b9012023-03-15 16:50:31 +0800656 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +0800657}
658
659/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800660 * Get hash identifier of mbedtls_md_type_t type
661 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800662int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +0800663{
Yanray Wang644b9012023-03-15 16:50:31 +0800664 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +0800665}
666
667/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100668 * Get length in bytes of RSA modulus
669 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100671{
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100673}
674
675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000677
678/*
679 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800680 *
681 * This generation method follows the RSA key pair generation procedure of
682 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
685 int (*f_rng)(void *, unsigned char *, size_t),
686 void *p_rng,
687 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000688{
Janos Follath24eed8d2019-11-22 13:21:35 +0000689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800690 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100691 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Janos Follathb8fc1b02018-09-03 15:37:01 +0100693 /*
694 * If the modulus is 1024 bit long or shorter, then the security strength of
695 * the RSA algorithm is less than or equal to 80 bits and therefore an error
696 * rate of 2^-80 is sufficient.
697 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100699 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 mbedtls_mpi_init(&H);
703 mbedtls_mpi_init(&G);
704 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000706 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100707 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
708 goto cleanup;
709 }
710
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000711 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
713 goto cleanup;
714 }
715
716 /*
717 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800718 * 1. |P-Q| > 2^( nbits / 2 - 100 )
719 * 2. GCD( E, (P-1)*(Q-1) ) == 1
720 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 do {
725 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
726 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
729 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800731 /* 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 +0100732 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
733 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800737 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if (H.s < 0) {
739 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
740 }
Janos Follathef441782016-09-21 13:18:12 +0100741
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100742 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
744 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
745 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800746
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800747 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
749 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800750 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800752
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800753 /* 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 +0100754 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
755 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
756 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 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 -0800759 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800761
762 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000764
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100765 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
767 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100772
Jethro Beekman97f95c92018-02-13 15:50:36 -0800773#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 * DP = D mod (P - 1)
776 * DQ = D mod (Q - 1)
777 * QP = Q^-1 mod P
778 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
780 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100781#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
Hanno Becker83aad1f2017-08-23 06:45:10 +0100783 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000785
786cleanup:
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 mbedtls_mpi_free(&H);
789 mbedtls_mpi_free(&G);
790 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (ret != 0) {
793 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if ((-ret & ~0x7f) == 0) {
796 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
797 }
798 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000799 }
800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000802}
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000805
806/*
807 * Check a public RSA key
808 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000810{
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
812 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100813 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
816 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100817 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
820 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
821 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
822 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
823 }
824
825 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000826}
827
828/*
Hanno Becker705fc682017-10-10 17:57:02 +0100829 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000830 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000832{
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
834 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
835 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000836 }
Paul Bakker48377d92013-08-30 12:06:24 +0200837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
839 &ctx->D, &ctx->E, NULL, NULL) != 0) {
840 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000842
Hanno Beckerb269a852017-08-25 08:03:21 +0100843#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
845 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
846 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100847 }
848#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000851}
852
853/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100854 * Check if contexts holding a public and private key match
855 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
857 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100858{
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
860 mbedtls_rsa_check_privkey(prv) != 0) {
861 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100862 }
863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
865 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
866 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100867 }
868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100870}
871
872/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 * Do an RSA public key operation
874 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
876 const unsigned char *input,
877 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000878{
Janos Follath24eed8d2019-11-22 13:21:35 +0000879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000880 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
884 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
885 }
Hanno Becker705fc682017-10-10 17:57:02 +0100886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000888
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200889#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
891 return ret;
892 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200893#endif
894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200898 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
899 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000900 }
901
902 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
904 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
906cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
909 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
910 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100911#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if (ret != 0) {
916 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
917 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000920}
921
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200922/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200923 * Generate or update blinding values, see section 10 of:
924 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200925 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200926 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200927 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100928static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
929 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200930{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200931 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200932 mbedtls_mpi R;
933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200937 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
939 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
940 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
941 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200942
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200943 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200944 }
945
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200946 /* Unblinding value: Vf = random number, invertible mod N */
947 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200949 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
950 goto cleanup;
951 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 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 +0200954
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200955 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
957 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
958 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200959
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200960 /* At this point, Vi is invertible mod N if and only if both Vf and R
961 * are invertible mod N. If one of them isn't, we don't need to know
962 * which one, we just loop and choose new values for both of them.
963 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
965 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200966 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500970
971 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
973 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200974
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200975 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200976 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 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 +0200978
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200979
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200980cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200984}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200985
Paul Bakker5121ce52009-01-03 21:22:43 +0000986/*
Janos Follathe81102e2017-03-22 13:38:28 +0000987 * Exponent blinding supposed to prevent side-channel attacks using multiple
988 * traces of measurements to recover the RSA key. The more collisions are there,
989 * the more bits of the key can be recovered. See [3].
990 *
991 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800992 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000993 *
994 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800995 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000996 *
997 * (With the currently (as of 2017 April) known best algorithms breaking 2048
998 * bit RSA requires approximately as much time as trying out 2^112 random keys.
999 * Thus in this sense with 28 byte blinding the security is not reduced by
1000 * side-channel attacks like the one in [3])
1001 *
1002 * This countermeasure does not help if the key recovery is possible with a
1003 * single trace.
1004 */
1005#define RSA_EXPONENT_BLINDING 28
1006
1007/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001008 * Do an RSA private key operation
1009 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001010int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1011 int (*f_rng)(void *, unsigned char *, size_t),
1012 void *p_rng,
1013 const unsigned char *input,
1014 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001015{
Janos Follath24eed8d2019-11-22 13:21:35 +00001016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001017 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001018
1019 /* Temporary holding the result */
1020 mbedtls_mpi T;
1021
1022 /* Temporaries holding P-1, Q-1 and the
1023 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001024 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001025
1026#if !defined(MBEDTLS_RSA_NO_CRT)
1027 /* Temporaries holding the results mod p resp. mod q. */
1028 mbedtls_mpi TP, TQ;
1029
1030 /* Temporaries holding the blinded exponents for
1031 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001032 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001033
1034 /* Pointers to actual exponents to be used - either the unblinded
1035 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +00001036 mbedtls_mpi *DP = &ctx->DP;
1037 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +01001038#else
1039 /* Temporary holding the blinded exponent (if used). */
1040 mbedtls_mpi D_blind;
1041
1042 /* Pointer to actual exponent to be used - either the unblinded
1043 * or the blinded one, depending on the presence of a PRNG. */
1044 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +01001045#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001046
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001047 /* Temporaries holding the initial input and the double
1048 * checked result; should be the same in the end. */
1049 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (f_rng == NULL) {
1052 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1053 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (rsa_check_context(ctx, 1 /* private key checks */,
1056 1 /* blinding on */) != 0) {
1057 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001058 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001059
Hanno Becker06811ce2017-05-03 15:10:34 +01001060#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1062 return ret;
1063 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001064#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001065
Hanno Becker06811ce2017-05-03 15:10:34 +01001066 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 mbedtls_mpi_init(&P1);
1070 mbedtls_mpi_init(&Q1);
1071 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001072
Janos Follathe81102e2017-03-22 13:38:28 +00001073#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001075#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 mbedtls_mpi_init(&DP_blind);
1077 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001078#endif
1079
Hanno Becker06811ce2017-05-03 15:10:34 +01001080#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001082#endif
1083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 mbedtls_mpi_init(&I);
1085 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001086
1087 /* End of MPI initialization */
1088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1090 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001091 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1092 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001093 }
1094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +01001096
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001097 /*
1098 * Blinding
1099 * T = T * Vi mod N
1100 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1102 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1103 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001104
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001105 /*
1106 * Exponent blinding
1107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1109 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001110
Janos Follathf9203b42017-03-22 15:13:15 +00001111#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001112 /*
1113 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1114 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1116 f_rng, p_rng));
1117 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1118 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1119 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +00001120
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001121 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001122#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001123 /*
1124 * DP_blind = ( P - 1 ) * R + DP
1125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1127 f_rng, p_rng));
1128 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1129 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1130 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001131
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001132 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001133
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001134 /*
1135 * DQ_blind = ( Q - 1 ) * R + DQ
1136 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1138 f_rng, p_rng));
1139 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1140 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1141 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001142
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001143 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001144#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001148#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001149 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001150 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001152 * TP = input ^ dP mod P
1153 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1157 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001158
1159 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001160 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1163 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1164 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
1166 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001167 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1170 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001172
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001173 /*
1174 * Unblind
1175 * T = T * Vf mod N
1176 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1178 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001179
Hanno Becker2dec5e82017-10-03 07:49:52 +01001180 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1182 &ctx->N, &ctx->RN));
1183 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001184 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1185 goto cleanup;
1186 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001187
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001190
1191cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1194 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1195 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001196#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 mbedtls_mpi_free(&P1);
1199 mbedtls_mpi_free(&Q1);
1200 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001201
Janos Follathe81102e2017-03-22 13:38:28 +00001202#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001204#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 mbedtls_mpi_free(&DP_blind);
1206 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001207#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001210
1211#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001213#endif
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 mbedtls_mpi_free(&C);
1216 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 if (ret != 0 && ret >= -0x007f) {
1219 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1220 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001223}
1224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001226/**
1227 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1228 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001229 * \param dst buffer to mask
1230 * \param dlen length of destination buffer
1231 * \param src source of the mask generation
1232 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001233 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001234 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001235static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1236 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001237{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001238 unsigned char counter[4];
1239 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001240 unsigned int hlen;
1241 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001242 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001243 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001244 const mbedtls_md_info_t *md_info;
1245 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 mbedtls_md_init(&md_ctx);
1248 md_info = mbedtls_md_info_from_type(md_alg);
1249 if (md_info == NULL) {
1250 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1251 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 mbedtls_md_init(&md_ctx);
1254 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001255 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 memset(mask, 0, sizeof(mask));
1261 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001262
Simon Butcher02037452016-03-01 21:19:12 +00001263 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001264 p = dst;
1265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001267 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001269 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001273 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 }
1275 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001276 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 }
1278 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001279 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 }
1281 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001282 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001286 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001288
1289 counter[3]++;
1290
1291 dlen -= use_len;
1292 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001293
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001294exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001299}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001300
1301/**
1302 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1303 *
1304 * \param hash the input hash
1305 * \param hlen length of the input hash
1306 * \param salt the input salt
1307 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001308 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001309 * \param md_alg message digest to use
1310 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001311static int hash_mprime(const unsigned char *hash, size_t hlen,
1312 const unsigned char *salt, size_t slen,
1313 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001314{
1315 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001316
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001317 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1321 if (md_info == NULL) {
1322 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1323 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 mbedtls_md_init(&md_ctx);
1326 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001327 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 }
1329 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001330 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 }
1332 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001333 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 }
1335 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001336 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 }
1338 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001339 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 }
1341 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001342 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001344
1345exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001349}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001350
1351/**
1352 * Compute a hash.
1353 *
1354 * \param md_alg algorithm to use
1355 * \param input input message to hash
1356 * \param ilen input length
1357 * \param output the output buffer - must be large enough for \p md_alg
1358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001359static int compute_hash(mbedtls_md_type_t md_alg,
1360 const unsigned char *input, size_t ilen,
1361 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001362{
1363 const mbedtls_md_info_t *md_info;
1364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 md_info = mbedtls_md_info_from_type(md_alg);
1366 if (md_info == NULL) {
1367 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1368 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001375/*
1376 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1379 int (*f_rng)(void *, unsigned char *, size_t),
1380 void *p_rng,
1381 const unsigned char *label, size_t label_len,
1382 size_t ilen,
1383 const unsigned char *input,
1384 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001385{
1386 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001388 unsigned char *p = output;
1389 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if (f_rng == NULL) {
1392 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1393 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001394
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001395 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (hlen == 0) {
1397 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1398 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001399
1400 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001401
Simon Butcher02037452016-03-01 21:19:12 +00001402 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1404 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1405 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001408
1409 *p++ = 0;
1410
Simon Butcher02037452016-03-01 21:19:12 +00001411 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1413 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1414 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001415
1416 p += hlen;
1417
Simon Butcher02037452016-03-01 21:19:12 +00001418 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1420 if (ret != 0) {
1421 return ret;
1422 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001423 p += hlen;
1424 p += olen - 2 * hlen - 2 - ilen;
1425 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (ilen != 0) {
1427 memcpy(p, input, ilen);
1428 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001429
Simon Butcher02037452016-03-01 21:19:12 +00001430 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001432 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 return ret;
1434 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001435
Simon Butcher02037452016-03-01 21:19:12 +00001436 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001438 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 return ret;
1440 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001443}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001447/*
1448 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1449 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1451 int (*f_rng)(void *, unsigned char *, size_t),
1452 void *p_rng, size_t ilen,
1453 const unsigned char *input,
1454 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001455{
1456 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001458 unsigned char *p = output;
1459
Paul Bakkerb3869132013-02-28 17:21:01 +01001460 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001461
Simon Butcher02037452016-03-01 21:19:12 +00001462 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if (ilen + 11 < ilen || olen < ilen + 11) {
1464 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1465 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001466
1467 nb_pad = olen - 3 - ilen;
1468
1469 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (f_rng == NULL) {
1472 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1473 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001474
1475 *p++ = MBEDTLS_RSA_CRYPT;
1476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001478 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001479
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001480 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 ret = f_rng(p_rng, p, 1);
1482 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001483
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001484 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if (rng_dl == 0 || ret != 0) {
1486 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1487 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001488
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001489 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001490 }
1491
1492 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (ilen != 0) {
1494 memcpy(p, input, ilen);
1495 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001500
Paul Bakker5121ce52009-01-03 21:22:43 +00001501/*
1502 * Add the message padding, then do an RSA operation
1503 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1505 int (*f_rng)(void *, unsigned char *, size_t),
1506 void *p_rng,
1507 size_t ilen,
1508 const unsigned char *input,
1509 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001510{
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_PKCS1_V15)
1513 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1515 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001516#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#if defined(MBEDTLS_PKCS1_V21)
1519 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1521 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001522#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001523
1524 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001527}
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001530/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001531 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001533int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1534 int (*f_rng)(void *, unsigned char *, size_t),
1535 void *p_rng,
1536 const unsigned char *label, size_t label_len,
1537 size_t *olen,
1538 const unsigned char *input,
1539 unsigned char *output,
1540 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001541{
Janos Follath24eed8d2019-11-22 13:21:35 +00001542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001543 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001544 unsigned char *p;
1545 mbedtls_ct_condition_t bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001547 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001548 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001549
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001550 /*
1551 * Parameters sanity checks
1552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1554 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001556
1557 ilen = ctx->len;
1558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (ilen < 16 || ilen > sizeof(buf)) {
1560 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1561 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001562
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001563 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 if (hlen == 0) {
1565 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1566 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001567
Janos Follathc17cda12016-02-11 11:08:18 +00001568 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 if (2 * hlen + 2 > ilen) {
1570 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1571 }
Janos Follathc17cda12016-02-11 11:08:18 +00001572
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001573 /*
1574 * RSA operation
1575 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001579 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001581
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001582 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001583 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001584 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001585 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001587 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 /* DB: Apply dbMask to maskedDB */
1589 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001590 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001591 goto cleanup;
1592 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001593
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001594 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1596 label, label_len, lhash);
1597 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001598 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001600
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001601 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001602 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001603 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001604 p = buf;
1605
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001606 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001607
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001608 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001609
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001610 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001611 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001612 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001613
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001614 /* Get zero-padding len, but always read till end of buffer
1615 * (minus one, for the 01 byte) */
1616 pad_len = 0;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001617 pad_done = MBEDTLS_CT_FALSE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001619 pad_done = mbedtls_ct_bool_or(pad_done, mbedtls_ct_uint_ne(p[i], 0));
Dave Rodgmane94cd0b2023-09-20 19:05:35 +01001620 pad_len += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001621 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001622
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001623 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001624 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001625
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001626 /*
1627 * The only information "leaked" is whether the padding was correct or not
1628 * (eg, no data is copied if it was not correct). This meets the
1629 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1630 * the different error conditions.
1631 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001632 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001633 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1634 goto cleanup;
1635 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001638 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1639 goto cleanup;
1640 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001641
1642 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 if (*olen != 0) {
1644 memcpy(output, p, *olen);
1645 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001646 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001647
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001648cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 mbedtls_platform_zeroize(buf, sizeof(buf));
1650 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001657/*
1658 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1659 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001660int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1661 int (*f_rng)(void *, unsigned char *, size_t),
1662 void *p_rng,
1663 size_t *olen,
1664 const unsigned char *input,
1665 unsigned char *output,
1666 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001667{
1668 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1669 size_t ilen;
1670 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1671
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001672 ilen = ctx->len;
1673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1675 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1676 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 if (ilen < 16 || ilen > sizeof(buf)) {
1679 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1680 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001685 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1689 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001690
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001691cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001695}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
1698/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001699 * Do an RSA operation, then remove the message padding
1700 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001701int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1702 int (*f_rng)(void *, unsigned char *, size_t),
1703 void *p_rng,
1704 size_t *olen,
1705 const unsigned char *input,
1706 unsigned char *output,
1707 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001708{
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#if defined(MBEDTLS_PKCS1_V15)
1711 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1713 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001714#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#if defined(MBEDTLS_PKCS1_V21)
1717 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1719 olen, input, output,
1720 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001721#endif
1722
1723 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001725 }
1726}
1727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001729static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1730 int (*f_rng)(void *, unsigned char *, size_t),
1731 void *p_rng,
1732 mbedtls_md_type_t md_alg,
1733 unsigned int hashlen,
1734 const unsigned char *hash,
1735 int saltlen,
1736 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001737{
1738 size_t olen;
1739 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001740 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001741 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001743 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001746 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1750 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1751 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (f_rng == NULL) {
1754 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1755 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001756
1757 olen = ctx->len;
1758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001760 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001761 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (exp_hashlen == 0) {
1763 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1764 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 if (hashlen != exp_hashlen) {
1767 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1768 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001769 }
1770
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001771 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 if (hlen == 0) {
1773 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1774 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1777 /* Calculate the largest possible salt length, up to the hash size.
1778 * Normally this is the hash length, which is the maximum salt length
1779 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1780 * enough room, use the maximum salt length that fits. The constraint is
1781 * that the hash length plus the salt length plus 2 bytes must be at most
1782 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1783 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001784 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (olen < hlen + min_slen + 2) {
1786 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1787 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001788 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001790 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 }
1792 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1793 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1794 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001795 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001796 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001799
Simon Butcher02037452016-03-01 21:19:12 +00001800 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001802 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001803 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001804
1805 /* Generate salt of length slen in place in the encoded message */
1806 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1808 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1809 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001810
Paul Bakkerb3869132013-02-28 17:21:01 +01001811 p += slen;
1812
Simon Butcher02037452016-03-01 21:19:12 +00001813 /* Generate H = Hash( M' ) */
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001814 ret = hash_mprime(hash, hashlen, salt, slen, p, (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 if (ret != 0) {
1816 return ret;
1817 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001818
Simon Butcher02037452016-03-01 21:19:12 +00001819 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001821 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001823
Simon Butcher02037452016-03-01 21:19:12 +00001824 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001826 (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if (ret != 0) {
1828 return ret;
1829 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001830
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1832 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001833
1834 p += hlen;
1835 *p++ = 0xBC;
1836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001838}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001839
1840/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001841 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1842 * the option to pass in the salt length.
1843 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001844int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1845 int (*f_rng)(void *, unsigned char *, size_t),
1846 void *p_rng,
1847 mbedtls_md_type_t md_alg,
1848 unsigned int hashlen,
1849 const unsigned char *hash,
1850 int saltlen,
1851 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001852{
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1854 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001855}
1856
1857
1858/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001859 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1860 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001861int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1862 int (*f_rng)(void *, unsigned char *, size_t),
1863 void *p_rng,
1864 mbedtls_md_type_t md_alg,
1865 unsigned int hashlen,
1866 const unsigned char *hash,
1867 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001868{
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1870 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001871}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001875/*
1876 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1877 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001878
1879/* Construct a PKCS v1.5 encoding of a hashed message
1880 *
1881 * This is used both for signature generation and verification.
1882 *
1883 * Parameters:
1884 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001885 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001886 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001887 * - hash: Buffer containing the hashed message or the raw data.
1888 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001889 * - dst: Buffer to hold the encoded message.
1890 *
1891 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001892 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001893 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001894 *
1895 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001896static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1897 unsigned int hashlen,
1898 const unsigned char *hash,
1899 size_t dst_len,
1900 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001901{
1902 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001903 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001904 unsigned char *p = dst;
1905 const char *oid = NULL;
1906
1907 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001909 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 if (md_size == 0) {
1911 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1912 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1915 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1916 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 if (hashlen != md_size) {
1919 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1920 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001921
1922 /* Double-check that 8 + hashlen + oid_size can be used as a
1923 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001925 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 10 + hashlen + oid_size < 10 + hashlen) {
1927 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1928 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001929
1930 /*
1931 * Static bounds check:
1932 * - Need 10 bytes for five tag-length pairs.
1933 * (Insist on 1-byte length encodings to protect against variants of
1934 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1935 * - Need hashlen bytes for hash
1936 * - Need oid_size bytes for hash alg OID.
1937 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (nb_pad < 10 + hashlen + oid_size) {
1939 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1940 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001941 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 } else {
1943 if (nb_pad < hashlen) {
1944 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1945 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001946
1947 nb_pad -= hashlen;
1948 }
1949
Hanno Becker2b2f8982017-09-27 17:10:03 +01001950 /* Need space for signature header and padding delimiter (3 bytes),
1951 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 if (nb_pad < 3 + 8) {
1953 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1954 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001955 nb_pad -= 3;
1956
1957 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001958 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001959
1960 /* Write signature header and padding */
1961 *p++ = 0;
1962 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001964 p += nb_pad;
1965 *p++ = 0;
1966
1967 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 if (md_alg == MBEDTLS_MD_NONE) {
1969 memcpy(p, hash, hashlen);
1970 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001971 }
1972
1973 /* Signing hashed data, add corresponding ASN.1 structure
1974 *
1975 * DigestInfo ::= SEQUENCE {
1976 * digestAlgorithm DigestAlgorithmIdentifier,
1977 * digest Digest }
1978 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1979 * Digest ::= OCTET STRING
1980 *
1981 * Schematic:
1982 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1983 * TAG-NULL + LEN [ NULL ] ]
1984 * TAG-OCTET + LEN [ HASH ] ]
1985 */
1986 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001988 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001990 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001991 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001993 p += oid_size;
1994 *p++ = MBEDTLS_ASN1_NULL;
1995 *p++ = 0x00;
1996 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001997 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001999 p += hashlen;
2000
2001 /* Just a sanity-check, should be automatic
2002 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 if (p != dst + dst_len) {
2004 mbedtls_platform_zeroize(dst, dst_len);
2005 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002006 }
2007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002009}
2010
Paul Bakkerb3869132013-02-28 17:21:01 +01002011/*
2012 * Do an RSA operation to sign the message digest
2013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002014int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2015 int (*f_rng)(void *, unsigned char *, size_t),
2016 void *p_rng,
2017 mbedtls_md_type_t md_alg,
2018 unsigned int hashlen,
2019 const unsigned char *hash,
2020 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002021{
Janos Follath24eed8d2019-11-22 13:21:35 +00002022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002023 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002026 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2030 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2031 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002032
Hanno Beckerfdf38032017-09-06 12:35:55 +01002033 /*
2034 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2035 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2038 ctx->len, sig)) != 0) {
2039 return ret;
2040 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002041
Hanno Beckerfdf38032017-09-06 12:35:55 +01002042 /* Private key operation
2043 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002044 * In order to prevent Lenstra's attack, make the signature in a
2045 * temporary buffer and check it before returning it.
2046 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 sig_try = mbedtls_calloc(1, ctx->len);
2049 if (sig_try == NULL) {
2050 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002051 }
2052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 verif = mbedtls_calloc(1, ctx->len);
2054 if (verif == NULL) {
2055 mbedtls_free(sig_try);
2056 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2057 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2060 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2061
2062 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002063 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2064 goto cleanup;
2065 }
2066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002068
2069cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002070 mbedtls_zeroize_and_free(sig_try, ctx->len);
2071 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 if (ret != 0) {
2074 memset(sig, '!', ctx->len);
2075 }
2076 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002077}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002079
2080/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 * Do an RSA operation to sign the message digest
2082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002083int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2084 int (*f_rng)(void *, unsigned char *, size_t),
2085 void *p_rng,
2086 mbedtls_md_type_t md_alg,
2087 unsigned int hashlen,
2088 const unsigned char *hash,
2089 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002090{
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002092 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096#if defined(MBEDTLS_PKCS1_V15)
2097 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2099 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102#if defined(MBEDTLS_PKCS1_V21)
2103 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2105 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002106#endif
2107
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002111}
2112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002114/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002115 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002117int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2118 mbedtls_md_type_t md_alg,
2119 unsigned int hashlen,
2120 const unsigned char *hash,
2121 mbedtls_md_type_t mgf1_hash_id,
2122 int expected_salt_len,
2123 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002124{
Janos Follath24eed8d2019-11-22 13:21:35 +00002125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002126 size_t siglen;
2127 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002128 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002129 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002130 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002131 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002135 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002137
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 siglen = ctx->len;
2139
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (siglen < 16 || siglen > sizeof(buf)) {
2141 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2142 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (ret != 0) {
2147 return ret;
2148 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002149
2150 p = buf;
2151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 if (buf[siglen - 1] != 0xBC) {
2153 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002154 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if (md_alg != MBEDTLS_MD_NONE) {
2157 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002158 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (exp_hashlen == 0) {
2160 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2161 }
2162
2163 if (hashlen != exp_hashlen) {
2164 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2165 }
2166 }
2167
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002168 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (hlen == 0) {
2170 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2171 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002172
Simon Butcher02037452016-03-01 21:19:12 +00002173 /*
2174 * Note: EMSA-PSS verification is over the length of N - 1 bits
2175 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if (buf[0] >> (8 - siglen * 8 + msb)) {
2179 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2180 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002181
Simon Butcher02037452016-03-01 21:19:12 +00002182 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002184 p++;
2185 siglen -= 1;
2186 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (siglen < hlen + 2) {
2189 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2190 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002191 hash_start = p + siglen - hlen - 1;
2192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2194 if (ret != 0) {
2195 return ret;
2196 }
Paul Bakker02303e82013-01-03 11:08:31 +01002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002201 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (*p++ != 0x01) {
2205 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2206 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002207
Gilles Peskine6a54b022017-10-17 19:02:13 +02002208 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2211 observed_salt_len != (size_t) expected_salt_len) {
2212 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002213 }
2214
Simon Butcher02037452016-03-01 21:19:12 +00002215 /*
2216 * Generate H = Hash( M' )
2217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2219 result, mgf1_hash_id);
2220 if (ret != 0) {
2221 return ret;
2222 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (memcmp(hash_start, result, hlen) != 0) {
2225 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2226 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002229}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002230
2231/*
2232 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2233 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002234int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2235 mbedtls_md_type_t md_alg,
2236 unsigned int hashlen,
2237 const unsigned char *hash,
2238 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002239{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002240 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002242 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002247 : md_alg;
2248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2250 md_alg, hashlen, hash,
2251 mgf1_hash_id,
2252 MBEDTLS_RSA_SALT_LEN_ANY,
2253 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002254
2255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002259/*
2260 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2261 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002262int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2263 mbedtls_md_type_t md_alg,
2264 unsigned int hashlen,
2265 const unsigned char *hash,
2266 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002267{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002268 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002269 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002270 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002273 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002275
2276 sig_len = ctx->len;
2277
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002278 /*
2279 * Prepare expected PKCS1 v1.5 encoding of hash.
2280 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2283 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002284 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2285 goto cleanup;
2286 }
2287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2289 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002290 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002292
2293 /*
2294 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2295 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 ret = mbedtls_rsa_public(ctx, sig, encoded);
2298 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002299 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002301
Simon Butcher02037452016-03-01 21:19:12 +00002302 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002303 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002304 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2307 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002308 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2309 goto cleanup;
2310 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002311
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002312cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002315 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002316 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002319 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002320 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002323}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
2326/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002327 * Do an RSA operation and check the message digest
2328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002329int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2330 mbedtls_md_type_t md_alg,
2331 unsigned int hashlen,
2332 const unsigned char *hash,
2333 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002334{
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002336 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340#if defined(MBEDTLS_PKCS1_V15)
2341 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2343 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002344#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#if defined(MBEDTLS_PKCS1_V21)
2347 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2349 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002350#endif
2351
2352 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002354 }
2355}
2356
2357/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002358 * Copy the components of an RSA key
2359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002360int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002361{
Janos Follath24eed8d2019-11-22 13:21:35 +00002362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002363
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002364 dst->len = src->len;
2365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2367 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2370 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2371 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002372
2373#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2375 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2376 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2377 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2378 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002379#endif
2380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2384 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002385
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002386 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002387 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002388
2389cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (ret != 0) {
2391 mbedtls_rsa_free(dst);
2392 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002395}
2396
2397/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 * Free the components of an RSA key
2399 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002400void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002401{
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002403 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 mbedtls_mpi_free(&ctx->Vi);
2407 mbedtls_mpi_free(&ctx->Vf);
2408 mbedtls_mpi_free(&ctx->RN);
2409 mbedtls_mpi_free(&ctx->D);
2410 mbedtls_mpi_free(&ctx->Q);
2411 mbedtls_mpi_free(&ctx->P);
2412 mbedtls_mpi_free(&ctx->E);
2413 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002414
Hanno Becker33c30a02017-08-23 07:00:22 +01002415#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 mbedtls_mpi_free(&ctx->RQ);
2417 mbedtls_mpi_free(&ctx->RP);
2418 mbedtls_mpi_free(&ctx->QP);
2419 mbedtls_mpi_free(&ctx->DQ);
2420 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002421#endif /* MBEDTLS_RSA_NO_CRT */
2422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002424 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (ctx->ver != 0) {
2426 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002427 ctx->ver = 0;
2428 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002429#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002430}
2431
Hanno Beckerab377312017-08-23 16:24:51 +01002432#endif /* !MBEDTLS_RSA_ALT */
2433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002435
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002436#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002437
2438/*
2439 * Example RSA-1024 keypair, for test purposes
2440 */
2441#define KEY_LEN 128
2442
2443#define RSA_N "9292758453063D803DD603D5E777D788" \
2444 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2445 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2446 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2447 "93A89813FBF3C4F8066D2D800F7C38A8" \
2448 "1AE31942917403FF4946B0A83D3D3E05" \
2449 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2450 "5E94BB77B07507233A0BC7BAC8F90F79"
2451
2452#define RSA_E "10001"
2453
2454#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2455 "66CA472BC44D253102F8B4A9D3BFA750" \
2456 "91386C0077937FE33FA3252D28855837" \
2457 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2458 "DF79C5CE07EE72C7F123142198164234" \
2459 "CABB724CF78B8173B9F880FC86322407" \
2460 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2461 "071513A1E85B5DFA031F21ECAE91A34D"
2462
2463#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2464 "2C01CAD19EA484A87EA4377637E75500" \
2465 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2466 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2467
2468#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2469 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2470 "910E4168387E3C30AA1E00C339A79508" \
2471 "8452DD96A9A5EA5D9DCA68DA636032AF"
2472
Paul Bakker5121ce52009-01-03 21:22:43 +00002473#define PT_LEN 24
2474#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2475 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002478static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002479{
gufe44c2620da2020-08-03 17:56:50 +02002480#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002481 size_t i;
2482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002484 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 }
Paul Bakker545570e2010-07-18 09:00:25 +00002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002488 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002490#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002492 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002496#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002501
Paul Bakker5121ce52009-01-03 21:22:43 +00002502/*
2503 * Checkup routine
2504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002505int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002506{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002507 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002509 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 unsigned char rsa_plaintext[PT_LEN];
2512 unsigned char rsa_decrypted[PT_LEN];
2513 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002514#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002515 unsigned char sha1sum[20];
2516#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Hanno Becker3a701162017-08-22 13:52:43 +01002518 mbedtls_mpi K;
2519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 mbedtls_mpi_init(&K);
2521 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2524 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2525 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2526 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2527 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2528 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2529 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2530 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2531 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2532 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (verbose != 0) {
2537 mbedtls_printf(" RSA key validation: ");
2538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2541 mbedtls_rsa_check_privkey(&rsa) != 0) {
2542 if (verbose != 0) {
2543 mbedtls_printf("failed\n");
2544 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Hanno Becker5bc87292017-05-03 15:09:31 +01002546 ret = 1;
2547 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 }
2549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (verbose != 0) {
2551 mbedtls_printf("passed\n PKCS#1 encryption : ");
2552 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2557 PT_LEN, rsa_plaintext,
2558 rsa_ciphertext) != 0) {
2559 if (verbose != 0) {
2560 mbedtls_printf("failed\n");
2561 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
Hanno Becker5bc87292017-05-03 15:09:31 +01002563 ret = 1;
2564 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 }
2566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (verbose != 0) {
2568 mbedtls_printf("passed\n PKCS#1 decryption : ");
2569 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2572 &len, rsa_ciphertext, rsa_decrypted,
2573 sizeof(rsa_decrypted)) != 0) {
2574 if (verbose != 0) {
2575 mbedtls_printf("failed\n");
2576 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Hanno Becker5bc87292017-05-03 15:09:31 +01002578 ret = 1;
2579 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 }
2581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2583 if (verbose != 0) {
2584 mbedtls_printf("failed\n");
2585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Hanno Becker5bc87292017-05-03 15:09:31 +01002587 ret = 1;
2588 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 }
2590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if (verbose != 0) {
2592 mbedtls_printf("passed\n");
2593 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002594
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002595#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (verbose != 0) {
2597 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002600 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2601 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 if (verbose != 0) {
2603 mbedtls_printf("failed\n");
2604 }
2605
2606 return 1;
2607 }
2608
2609 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2610 MBEDTLS_MD_SHA1, 20,
2611 sha1sum, rsa_ciphertext) != 0) {
2612 if (verbose != 0) {
2613 mbedtls_printf("failed\n");
2614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Hanno Becker5bc87292017-05-03 15:09:31 +01002616 ret = 1;
2617 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
2619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (verbose != 0) {
2621 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2622 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2625 sha1sum, rsa_ciphertext) != 0) {
2626 if (verbose != 0) {
2627 mbedtls_printf("failed\n");
2628 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002629
Hanno Becker5bc87292017-05-03 15:09:31 +01002630 ret = 1;
2631 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 }
2633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (verbose != 0) {
2635 mbedtls_printf("passed\n");
2636 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002637#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 if (verbose != 0) {
2640 mbedtls_printf("\n");
2641 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002642
Paul Bakker3d8fb632014-04-17 12:42:41 +02002643cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 mbedtls_mpi_free(&K);
2645 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002647 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650}
2651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654#endif /* MBEDTLS_RSA_C */