blob: d0782f53c77a101a041fb185af26a7f0f9ec7d1b [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;
1544 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001546 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001547 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001548
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001549 /*
1550 * Parameters sanity checks
1551 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1553 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1554 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001555
1556 ilen = ctx->len;
1557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 if (ilen < 16 || ilen > sizeof(buf)) {
1559 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1560 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001561
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001562 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 if (hlen == 0) {
1564 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1565 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001566
Janos Follathc17cda12016-02-11 11:08:18 +00001567 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 if (2 * hlen + 2 > ilen) {
1569 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1570 }
Janos Follathc17cda12016-02-11 11:08:18 +00001571
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001572 /*
1573 * RSA operation
1574 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001578 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001580
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001581 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001582 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001583 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001584 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001586 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 /* DB: Apply dbMask to maskedDB */
1588 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001589 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001590 goto cleanup;
1591 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001592
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001593 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1595 label, label_len, lhash);
1596 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001597 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001599
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001600 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001601 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001602 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001604 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001606 bad |= *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 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 for (i = 0; i < hlen; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001612 bad |= lhash[i] ^ *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001614
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001615 /* Get zero-padding len, but always read till end of buffer
1616 * (minus one, for the 01 byte) */
1617 pad_len = 0;
1618 pad_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001620 pad_done |= p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001622 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001623
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001624 p += pad_len;
1625 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001626
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001627 /*
1628 * The only information "leaked" is whether the padding was correct or not
1629 * (eg, no data is copied if it was not correct). This meets the
1630 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1631 * the different error conditions.
1632 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001634 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1635 goto cleanup;
1636 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001639 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1640 goto cleanup;
1641 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001642
1643 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 if (*olen != 0) {
1645 memcpy(output, p, *olen);
1646 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001647 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001648
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001649cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 mbedtls_platform_zeroize(buf, sizeof(buf));
1651 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001658/*
1659 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1660 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001661int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1662 int (*f_rng)(void *, unsigned char *, size_t),
1663 void *p_rng,
1664 size_t *olen,
1665 const unsigned char *input,
1666 unsigned char *output,
1667 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001668{
1669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1670 size_t ilen;
1671 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1672
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001673 ilen = ctx->len;
1674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1676 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1677 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 if (ilen < 16 || ilen > sizeof(buf)) {
1680 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1681 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001686 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1690 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001691
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001692cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001696}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001698
1699/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001700 * Do an RSA operation, then remove the message padding
1701 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001702int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1703 int (*f_rng)(void *, unsigned char *, size_t),
1704 void *p_rng,
1705 size_t *olen,
1706 const unsigned char *input,
1707 unsigned char *output,
1708 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001709{
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#if defined(MBEDTLS_PKCS1_V15)
1712 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1714 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001715#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#if defined(MBEDTLS_PKCS1_V21)
1718 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1720 olen, input, output,
1721 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001722#endif
1723
1724 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001726 }
1727}
1728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001730static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1731 int (*f_rng)(void *, unsigned char *, size_t),
1732 void *p_rng,
1733 mbedtls_md_type_t md_alg,
1734 unsigned int hashlen,
1735 const unsigned char *hash,
1736 int saltlen,
1737 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001738{
1739 size_t olen;
1740 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001741 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001742 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001743 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001744 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001747 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1751 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1752 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001753
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 if (f_rng == NULL) {
1755 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1756 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001757
1758 olen = ctx->len;
1759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001761 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001762 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 if (exp_hashlen == 0) {
1764 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1765 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 if (hashlen != exp_hashlen) {
1768 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1769 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001770 }
1771
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001772 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (hlen == 0) {
1774 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1775 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1778 /* Calculate the largest possible salt length, up to the hash size.
1779 * Normally this is the hash length, which is the maximum salt length
1780 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1781 * enough room, use the maximum salt length that fits. The constraint is
1782 * that the hash length plus the salt length plus 2 bytes must be at most
1783 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1784 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001785 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 if (olen < hlen + min_slen + 2) {
1787 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1788 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001789 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001791 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 }
1793 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1794 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1795 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001796 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001797 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001800
Simon Butcher02037452016-03-01 21:19:12 +00001801 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001803 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001804 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001805
1806 /* Generate salt of length slen in place in the encoded message */
1807 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1809 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1810 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001811
Paul Bakkerb3869132013-02-28 17:21:01 +01001812 p += slen;
1813
Simon Butcher02037452016-03-01 21:19:12 +00001814 /* Generate H = Hash( M' ) */
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001815 ret = hash_mprime(hash, hashlen, salt, slen, p, (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 if (ret != 0) {
1817 return ret;
1818 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001819
Simon Butcher02037452016-03-01 21:19:12 +00001820 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001822 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001824
Simon Butcher02037452016-03-01 21:19:12 +00001825 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001827 (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 if (ret != 0) {
1829 return ret;
1830 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1833 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001834
1835 p += hlen;
1836 *p++ = 0xBC;
1837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001839}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001840
1841/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001842 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1843 * the option to pass in the salt length.
1844 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1846 int (*f_rng)(void *, unsigned char *, size_t),
1847 void *p_rng,
1848 mbedtls_md_type_t md_alg,
1849 unsigned int hashlen,
1850 const unsigned char *hash,
1851 int saltlen,
1852 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001853{
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1855 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001856}
1857
1858
1859/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001860 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1861 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001862int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1863 int (*f_rng)(void *, unsigned char *, size_t),
1864 void *p_rng,
1865 mbedtls_md_type_t md_alg,
1866 unsigned int hashlen,
1867 const unsigned char *hash,
1868 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001869{
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1871 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001872}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001876/*
1877 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1878 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001879
1880/* Construct a PKCS v1.5 encoding of a hashed message
1881 *
1882 * This is used both for signature generation and verification.
1883 *
1884 * Parameters:
1885 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001886 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001887 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001888 * - hash: Buffer containing the hashed message or the raw data.
1889 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001890 * - dst: Buffer to hold the encoded message.
1891 *
1892 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001893 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001894 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001895 *
1896 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001897static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1898 unsigned int hashlen,
1899 const unsigned char *hash,
1900 size_t dst_len,
1901 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001902{
1903 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001904 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001905 unsigned char *p = dst;
1906 const char *oid = NULL;
1907
1908 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001910 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 if (md_size == 0) {
1912 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1913 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1916 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1917 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 if (hashlen != md_size) {
1920 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1921 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001922
1923 /* Double-check that 8 + hashlen + oid_size can be used as a
1924 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001926 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 10 + hashlen + oid_size < 10 + hashlen) {
1928 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1929 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001930
1931 /*
1932 * Static bounds check:
1933 * - Need 10 bytes for five tag-length pairs.
1934 * (Insist on 1-byte length encodings to protect against variants of
1935 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1936 * - Need hashlen bytes for hash
1937 * - Need oid_size bytes for hash alg OID.
1938 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 if (nb_pad < 10 + hashlen + oid_size) {
1940 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1941 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001942 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 } else {
1944 if (nb_pad < hashlen) {
1945 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1946 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001947
1948 nb_pad -= hashlen;
1949 }
1950
Hanno Becker2b2f8982017-09-27 17:10:03 +01001951 /* Need space for signature header and padding delimiter (3 bytes),
1952 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 if (nb_pad < 3 + 8) {
1954 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1955 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001956 nb_pad -= 3;
1957
1958 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001959 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001960
1961 /* Write signature header and padding */
1962 *p++ = 0;
1963 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001965 p += nb_pad;
1966 *p++ = 0;
1967
1968 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (md_alg == MBEDTLS_MD_NONE) {
1970 memcpy(p, hash, hashlen);
1971 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001972 }
1973
1974 /* Signing hashed data, add corresponding ASN.1 structure
1975 *
1976 * DigestInfo ::= SEQUENCE {
1977 * digestAlgorithm DigestAlgorithmIdentifier,
1978 * digest Digest }
1979 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1980 * Digest ::= OCTET STRING
1981 *
1982 * Schematic:
1983 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1984 * TAG-NULL + LEN [ NULL ] ]
1985 * TAG-OCTET + LEN [ HASH ] ]
1986 */
1987 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001989 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001991 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001992 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001994 p += oid_size;
1995 *p++ = MBEDTLS_ASN1_NULL;
1996 *p++ = 0x00;
1997 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001998 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002000 p += hashlen;
2001
2002 /* Just a sanity-check, should be automatic
2003 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 if (p != dst + dst_len) {
2005 mbedtls_platform_zeroize(dst, dst_len);
2006 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002007 }
2008
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002010}
2011
Paul Bakkerb3869132013-02-28 17:21:01 +01002012/*
2013 * Do an RSA operation to sign the message digest
2014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002015int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2016 int (*f_rng)(void *, unsigned char *, size_t),
2017 void *p_rng,
2018 mbedtls_md_type_t md_alg,
2019 unsigned int hashlen,
2020 const unsigned char *hash,
2021 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002022{
Janos Follath24eed8d2019-11-22 13:21:35 +00002023 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002024 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002027 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2031 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2032 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002033
Hanno Beckerfdf38032017-09-06 12:35:55 +01002034 /*
2035 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2036 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2039 ctx->len, sig)) != 0) {
2040 return ret;
2041 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002042
Hanno Beckerfdf38032017-09-06 12:35:55 +01002043 /* Private key operation
2044 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002045 * In order to prevent Lenstra's attack, make the signature in a
2046 * temporary buffer and check it before returning it.
2047 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 sig_try = mbedtls_calloc(1, ctx->len);
2050 if (sig_try == NULL) {
2051 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002052 }
2053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 verif = mbedtls_calloc(1, ctx->len);
2055 if (verif == NULL) {
2056 mbedtls_free(sig_try);
2057 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2058 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2061 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2062
2063 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002064 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2065 goto cleanup;
2066 }
2067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002069
2070cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002071 mbedtls_zeroize_and_free(sig_try, ctx->len);
2072 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 if (ret != 0) {
2075 memset(sig, '!', ctx->len);
2076 }
2077 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002078}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002080
2081/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 * Do an RSA operation to sign the message digest
2083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002084int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2085 int (*f_rng)(void *, unsigned char *, size_t),
2086 void *p_rng,
2087 mbedtls_md_type_t md_alg,
2088 unsigned int hashlen,
2089 const unsigned char *hash,
2090 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002091{
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002093 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#if defined(MBEDTLS_PKCS1_V15)
2098 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2100 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002101#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#if defined(MBEDTLS_PKCS1_V21)
2104 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2106 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002107#endif
2108
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002112}
2113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002115/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002116 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002118int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2119 mbedtls_md_type_t md_alg,
2120 unsigned int hashlen,
2121 const unsigned char *hash,
2122 mbedtls_md_type_t mgf1_hash_id,
2123 int expected_salt_len,
2124 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002125{
Janos Follath24eed8d2019-11-22 13:21:35 +00002126 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002127 size_t siglen;
2128 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002129 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002130 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002131 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002132 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002136 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002138
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 siglen = ctx->len;
2140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (siglen < 16 || siglen > sizeof(buf)) {
2142 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2143 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 if (ret != 0) {
2148 return ret;
2149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
2151 p = buf;
2152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (buf[siglen - 1] != 0xBC) {
2154 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002155 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 if (md_alg != MBEDTLS_MD_NONE) {
2158 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002159 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (exp_hashlen == 0) {
2161 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2162 }
2163
2164 if (hashlen != exp_hashlen) {
2165 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2166 }
2167 }
2168
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002169 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (hlen == 0) {
2171 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2172 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002173
Simon Butcher02037452016-03-01 21:19:12 +00002174 /*
2175 * Note: EMSA-PSS verification is over the length of N - 1 bits
2176 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (buf[0] >> (8 - siglen * 8 + msb)) {
2180 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2181 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002182
Simon Butcher02037452016-03-01 21:19:12 +00002183 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002185 p++;
2186 siglen -= 1;
2187 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (siglen < hlen + 2) {
2190 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2191 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002192 hash_start = p + siglen - hlen - 1;
2193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2195 if (ret != 0) {
2196 return ret;
2197 }
Paul Bakker02303e82013-01-03 11:08:31 +01002198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002202 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if (*p++ != 0x01) {
2206 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2207 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002208
Gilles Peskine6a54b022017-10-17 19:02:13 +02002209 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2212 observed_salt_len != (size_t) expected_salt_len) {
2213 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002214 }
2215
Simon Butcher02037452016-03-01 21:19:12 +00002216 /*
2217 * Generate H = Hash( M' )
2218 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2220 result, mgf1_hash_id);
2221 if (ret != 0) {
2222 return ret;
2223 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (memcmp(hash_start, result, hlen) != 0) {
2226 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2227 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002230}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002231
2232/*
2233 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2234 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002235int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2236 mbedtls_md_type_t md_alg,
2237 unsigned int hashlen,
2238 const unsigned char *hash,
2239 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002240{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002241 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002243 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002248 : md_alg;
2249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2251 md_alg, hashlen, hash,
2252 mgf1_hash_id,
2253 MBEDTLS_RSA_SALT_LEN_ANY,
2254 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002255
2256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002260/*
2261 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002263int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2264 mbedtls_md_type_t md_alg,
2265 unsigned int hashlen,
2266 const unsigned char *hash,
2267 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002268{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002269 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002270 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002271 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002274 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002276
2277 sig_len = ctx->len;
2278
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002279 /*
2280 * Prepare expected PKCS1 v1.5 encoding of hash.
2281 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2284 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002285 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2286 goto cleanup;
2287 }
2288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2290 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002291 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002293
2294 /*
2295 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2296 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 ret = mbedtls_rsa_public(ctx, sig, encoded);
2299 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002300 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002302
Simon Butcher02037452016-03-01 21:19:12 +00002303 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002304 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002305 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2308 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002309 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2310 goto cleanup;
2311 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002312
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002313cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002316 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002317 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002320 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002321 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002326
2327/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002328 * Do an RSA operation and check the message digest
2329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002330int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2331 mbedtls_md_type_t md_alg,
2332 unsigned int hashlen,
2333 const unsigned char *hash,
2334 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002335{
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002337 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341#if defined(MBEDTLS_PKCS1_V15)
2342 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2344 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002345#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#if defined(MBEDTLS_PKCS1_V21)
2348 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2350 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002351#endif
2352
2353 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002355 }
2356}
2357
2358/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002359 * Copy the components of an RSA key
2360 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002361int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002362{
Janos Follath24eed8d2019-11-22 13:21:35 +00002363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002364
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002365 dst->len = src->len;
2366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2368 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2371 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2372 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002373
2374#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2376 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2377 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2378 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2379 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002380#endif
2381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2385 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002386
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002387 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002388 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002389
2390cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if (ret != 0) {
2392 mbedtls_rsa_free(dst);
2393 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002396}
2397
2398/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 * Free the components of an RSA key
2400 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002401void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002402{
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002404 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 mbedtls_mpi_free(&ctx->Vi);
2408 mbedtls_mpi_free(&ctx->Vf);
2409 mbedtls_mpi_free(&ctx->RN);
2410 mbedtls_mpi_free(&ctx->D);
2411 mbedtls_mpi_free(&ctx->Q);
2412 mbedtls_mpi_free(&ctx->P);
2413 mbedtls_mpi_free(&ctx->E);
2414 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002415
Hanno Becker33c30a02017-08-23 07:00:22 +01002416#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 mbedtls_mpi_free(&ctx->RQ);
2418 mbedtls_mpi_free(&ctx->RP);
2419 mbedtls_mpi_free(&ctx->QP);
2420 mbedtls_mpi_free(&ctx->DQ);
2421 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002422#endif /* MBEDTLS_RSA_NO_CRT */
2423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002425 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 if (ctx->ver != 0) {
2427 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002428 ctx->ver = 0;
2429 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002430#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002431}
2432
Hanno Beckerab377312017-08-23 16:24:51 +01002433#endif /* !MBEDTLS_RSA_ALT */
2434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002437#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
2439/*
2440 * Example RSA-1024 keypair, for test purposes
2441 */
2442#define KEY_LEN 128
2443
2444#define RSA_N "9292758453063D803DD603D5E777D788" \
2445 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2446 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2447 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2448 "93A89813FBF3C4F8066D2D800F7C38A8" \
2449 "1AE31942917403FF4946B0A83D3D3E05" \
2450 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2451 "5E94BB77B07507233A0BC7BAC8F90F79"
2452
2453#define RSA_E "10001"
2454
2455#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2456 "66CA472BC44D253102F8B4A9D3BFA750" \
2457 "91386C0077937FE33FA3252D28855837" \
2458 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2459 "DF79C5CE07EE72C7F123142198164234" \
2460 "CABB724CF78B8173B9F880FC86322407" \
2461 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2462 "071513A1E85B5DFA031F21ECAE91A34D"
2463
2464#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2465 "2C01CAD19EA484A87EA4377637E75500" \
2466 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2467 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2468
2469#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2470 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2471 "910E4168387E3C30AA1E00C339A79508" \
2472 "8452DD96A9A5EA5D9DCA68DA636032AF"
2473
Paul Bakker5121ce52009-01-03 21:22:43 +00002474#define PT_LEN 24
2475#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2476 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002479static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002480{
gufe44c2620da2020-08-03 17:56:50 +02002481#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002482 size_t i;
2483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002485 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 }
Paul Bakker545570e2010-07-18 09:00:25 +00002487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002489 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002491#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002493 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002497#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002500}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002502
Paul Bakker5121ce52009-01-03 21:22:43 +00002503/*
2504 * Checkup routine
2505 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002506int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002507{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002508 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002510 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 unsigned char rsa_plaintext[PT_LEN];
2513 unsigned char rsa_decrypted[PT_LEN];
2514 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002515#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002516 unsigned char sha1sum[20];
2517#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
Hanno Becker3a701162017-08-22 13:52:43 +01002519 mbedtls_mpi K;
2520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 mbedtls_mpi_init(&K);
2522 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2525 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2526 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2527 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2528 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2529 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2530 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2531 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2532 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2533 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 if (verbose != 0) {
2538 mbedtls_printf(" RSA key validation: ");
2539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2542 mbedtls_rsa_check_privkey(&rsa) != 0) {
2543 if (verbose != 0) {
2544 mbedtls_printf("failed\n");
2545 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
Hanno Becker5bc87292017-05-03 15:09:31 +01002547 ret = 1;
2548 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 }
2550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 if (verbose != 0) {
2552 mbedtls_printf("passed\n PKCS#1 encryption : ");
2553 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2558 PT_LEN, rsa_plaintext,
2559 rsa_ciphertext) != 0) {
2560 if (verbose != 0) {
2561 mbedtls_printf("failed\n");
2562 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Hanno Becker5bc87292017-05-03 15:09:31 +01002564 ret = 1;
2565 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
2567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (verbose != 0) {
2569 mbedtls_printf("passed\n PKCS#1 decryption : ");
2570 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2573 &len, rsa_ciphertext, rsa_decrypted,
2574 sizeof(rsa_decrypted)) != 0) {
2575 if (verbose != 0) {
2576 mbedtls_printf("failed\n");
2577 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Hanno Becker5bc87292017-05-03 15:09:31 +01002579 ret = 1;
2580 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 }
2582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2584 if (verbose != 0) {
2585 mbedtls_printf("failed\n");
2586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Hanno Becker5bc87292017-05-03 15:09:31 +01002588 ret = 1;
2589 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 }
2591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (verbose != 0) {
2593 mbedtls_printf("passed\n");
2594 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002595
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002596#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 if (verbose != 0) {
2598 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002599 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002601 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2602 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (verbose != 0) {
2604 mbedtls_printf("failed\n");
2605 }
2606
2607 return 1;
2608 }
2609
2610 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2611 MBEDTLS_MD_SHA1, 20,
2612 sha1sum, rsa_ciphertext) != 0) {
2613 if (verbose != 0) {
2614 mbedtls_printf("failed\n");
2615 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
Hanno Becker5bc87292017-05-03 15:09:31 +01002617 ret = 1;
2618 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (verbose != 0) {
2622 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2623 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2626 sha1sum, rsa_ciphertext) != 0) {
2627 if (verbose != 0) {
2628 mbedtls_printf("failed\n");
2629 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
Hanno Becker5bc87292017-05-03 15:09:31 +01002631 ret = 1;
2632 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 }
2634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 if (verbose != 0) {
2636 mbedtls_printf("passed\n");
2637 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002638#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 if (verbose != 0) {
2641 mbedtls_printf("\n");
2642 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002643
Paul Bakker3d8fb632014-04-17 12:42:41 +02002644cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 mbedtls_mpi_free(&K);
2646 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002648 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002651}
2652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655#endif /* MBEDTLS_RSA_C */