blob: 44ff3d2c0a11926d5f994b90cf9b4d2bd9dd7d2c [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é-Gonnard47728842022-07-18 13:00:40 +020049#include "hash_info.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 Rodgman9f9c3b82023-05-17 12:28:51 +0100123 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_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 Rodgman9f9c3b82023-05-17 12:28:51 +0100129 mbedtls_ct_condition_t found = mbedtls_ct_bool_eq(input[i], 0);
130 pad_done = mbedtls_ct_bool_or(pad_done, found);
131 pad_count += mbedtls_ct_uint_if0(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 Rodgman9f9c3b82023-05-17 12:28:51 +0100138 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_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 Rodgman9f9c3b82023-05-17 12:28:51 +0100153 output_too_large = mbedtls_ct_bool_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)),
164 mbedtls_ct_uint_if0(
165 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. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (mbedtls_hash_info_psa_from_md(hash_id) == PSA_ALG_NONE) {
640 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (nbits < 128 || 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
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 /*
712 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800713 * 1. |P-Q| > 2^( nbits / 2 - 100 )
714 * 2. GCD( E, (P-1)*(Q-1) ) == 1
715 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000716 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 do {
720 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
721 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
724 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800726 /* 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 +0100727 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
728 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800732 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (H.s < 0) {
734 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
735 }
Janos Follathef441782016-09-21 13:18:12 +0100736
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100737 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
739 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
740 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800741
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800742 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
744 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800745 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800747
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800748 /* 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 +0100749 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
750 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
751 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 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 -0800754 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800756
757 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100760 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
762 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100767
Jethro Beekman97f95c92018-02-13 15:50:36 -0800768#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000770 * DP = D mod (P - 1)
771 * DQ = D mod (Q - 1)
772 * QP = Q^-1 mod P
773 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
775 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100776#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000777
Hanno Becker83aad1f2017-08-23 06:45:10 +0100778 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000780
781cleanup:
782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 mbedtls_mpi_free(&H);
784 mbedtls_mpi_free(&G);
785 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (ret != 0) {
788 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if ((-ret & ~0x7f) == 0) {
791 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
792 }
793 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000794 }
795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000797}
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000800
801/*
802 * Check a public RSA key
803 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000805{
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
807 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100808 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
811 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100812 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
815 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
816 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
817 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
818 }
819
820 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000821}
822
823/*
Hanno Becker705fc682017-10-10 17:57:02 +0100824 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000827{
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
829 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
830 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000831 }
Paul Bakker48377d92013-08-30 12:06:24 +0200832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
834 &ctx->D, &ctx->E, NULL, NULL) != 0) {
835 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000836 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000837
Hanno Beckerb269a852017-08-25 08:03:21 +0100838#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
840 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
841 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100842 }
843#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000846}
847
848/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100849 * Check if contexts holding a public and private key match
850 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
852 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100853{
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
855 mbedtls_rsa_check_privkey(prv) != 0) {
856 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100857 }
858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
860 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 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 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100865}
866
867/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000868 * Do an RSA public key operation
869 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
871 const unsigned char *input,
872 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000873{
Janos Follath24eed8d2019-11-22 13:21:35 +0000874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000875 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
879 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
880 }
Hanno Becker705fc682017-10-10 17:57:02 +0100881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200884#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
886 return ret;
887 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200888#endif
889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200893 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
894 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000895 }
896
897 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
899 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
901cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
904 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
905 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100906#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (ret != 0) {
911 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
912 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000915}
916
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200917/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200918 * Generate or update blinding values, see section 10 of:
919 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200920 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200921 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200922 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100923static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
924 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200925{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200926 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200927 mbedtls_mpi R;
928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200932 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
934 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
935 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
936 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200937
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200938 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200939 }
940
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200941 /* Unblinding value: Vf = random number, invertible mod N */
942 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200944 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
945 goto cleanup;
946 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 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 +0200949
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200950 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
952 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
953 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200954
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200955 /* At this point, Vi is invertible mod N if and only if both Vf and R
956 * are invertible mod N. If one of them isn't, we don't need to know
957 * which one, we just loop and choose new values for both of them.
958 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
960 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200961 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500965
966 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
968 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200969
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200970 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200971 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 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 +0200973
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200974
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200975cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200979}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200980
Paul Bakker5121ce52009-01-03 21:22:43 +0000981/*
Janos Follathe81102e2017-03-22 13:38:28 +0000982 * Exponent blinding supposed to prevent side-channel attacks using multiple
983 * traces of measurements to recover the RSA key. The more collisions are there,
984 * the more bits of the key can be recovered. See [3].
985 *
986 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800987 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000988 *
989 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800990 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000991 *
992 * (With the currently (as of 2017 April) known best algorithms breaking 2048
993 * bit RSA requires approximately as much time as trying out 2^112 random keys.
994 * Thus in this sense with 28 byte blinding the security is not reduced by
995 * side-channel attacks like the one in [3])
996 *
997 * This countermeasure does not help if the key recovery is possible with a
998 * single trace.
999 */
1000#define RSA_EXPONENT_BLINDING 28
1001
1002/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 * Do an RSA private key operation
1004 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001005int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1006 int (*f_rng)(void *, unsigned char *, size_t),
1007 void *p_rng,
1008 const unsigned char *input,
1009 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001010{
Janos Follath24eed8d2019-11-22 13:21:35 +00001011 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001012 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001013
1014 /* Temporary holding the result */
1015 mbedtls_mpi T;
1016
1017 /* Temporaries holding P-1, Q-1 and the
1018 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001019 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001020
1021#if !defined(MBEDTLS_RSA_NO_CRT)
1022 /* Temporaries holding the results mod p resp. mod q. */
1023 mbedtls_mpi TP, TQ;
1024
1025 /* Temporaries holding the blinded exponents for
1026 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001027 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001028
1029 /* Pointers to actual exponents to be used - either the unblinded
1030 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +00001031 mbedtls_mpi *DP = &ctx->DP;
1032 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +01001033#else
1034 /* Temporary holding the blinded exponent (if used). */
1035 mbedtls_mpi D_blind;
1036
1037 /* Pointer to actual exponent to be used - either the unblinded
1038 * or the blinded one, depending on the presence of a PRNG. */
1039 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +01001040#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001041
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001042 /* Temporaries holding the initial input and the double
1043 * checked result; should be the same in the end. */
1044 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (f_rng == NULL) {
1047 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1048 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 if (rsa_check_context(ctx, 1 /* private key checks */,
1051 1 /* blinding on */) != 0) {
1052 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001053 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001054
Hanno Becker06811ce2017-05-03 15:10:34 +01001055#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1057 return ret;
1058 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001059#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001060
Hanno Becker06811ce2017-05-03 15:10:34 +01001061 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 mbedtls_mpi_init(&P1);
1065 mbedtls_mpi_init(&Q1);
1066 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001067
Janos Follathe81102e2017-03-22 13:38:28 +00001068#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001070#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 mbedtls_mpi_init(&DP_blind);
1072 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001073#endif
1074
Hanno Becker06811ce2017-05-03 15:10:34 +01001075#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001077#endif
1078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 mbedtls_mpi_init(&I);
1080 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001081
1082 /* End of MPI initialization */
1083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1085 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001086 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1087 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 }
1089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +01001091
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001092 /*
1093 * Blinding
1094 * T = T * Vi mod N
1095 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1097 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1098 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001099
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001100 /*
1101 * Exponent blinding
1102 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1104 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001105
Janos Follathf9203b42017-03-22 15:13:15 +00001106#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001107 /*
1108 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1109 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1111 f_rng, p_rng));
1112 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1113 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1114 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +00001115
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001116 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001117#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001118 /*
1119 * DP_blind = ( P - 1 ) * R + DP
1120 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1122 f_rng, p_rng));
1123 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1124 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1125 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001126
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001127 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001128
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001129 /*
1130 * DQ_blind = ( Q - 1 ) * R + DQ
1131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1133 f_rng, p_rng));
1134 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1135 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1136 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001137
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001138 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001139#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001143#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001144 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001145 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001147 * TP = input ^ dP mod P
1148 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1152 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
1154 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001155 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1158 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1159 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
1161 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001162 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1165 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001167
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001168 /*
1169 * Unblind
1170 * T = T * Vf mod N
1171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1173 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001174
Hanno Becker2dec5e82017-10-03 07:49:52 +01001175 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1177 &ctx->N, &ctx->RN));
1178 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001179 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1180 goto cleanup;
1181 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001182
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001185
1186cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1189 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1190 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001191#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 mbedtls_mpi_free(&P1);
1194 mbedtls_mpi_free(&Q1);
1195 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001196
Janos Follathe81102e2017-03-22 13:38:28 +00001197#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001199#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 mbedtls_mpi_free(&DP_blind);
1201 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001202#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001205
1206#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001208#endif
1209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 mbedtls_mpi_free(&C);
1211 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 if (ret != 0 && ret >= -0x007f) {
1214 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1215 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001218}
1219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001221/**
1222 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1223 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001224 * \param dst buffer to mask
1225 * \param dlen length of destination buffer
1226 * \param src source of the mask generation
1227 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001228 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001230static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1231 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001232{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001233 unsigned char counter[4];
1234 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001235 unsigned int hlen;
1236 size_t i, use_len;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001237 unsigned char mask[MBEDTLS_HASH_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001238 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001239 const mbedtls_md_info_t *md_info;
1240 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 mbedtls_md_init(&md_ctx);
1243 md_info = mbedtls_md_info_from_type(md_alg);
1244 if (md_info == NULL) {
1245 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1246 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 mbedtls_md_init(&md_ctx);
1249 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001250 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 memset(mask, 0, sizeof(mask));
1256 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001257
Simon Butcher02037452016-03-01 21:19:12 +00001258 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001259 p = dst;
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001262 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001264 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001268 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 }
1270 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001271 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 }
1273 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001274 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 }
1276 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001277 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001281 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001283
1284 counter[3]++;
1285
1286 dlen -= use_len;
1287 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001288
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001289exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001294}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001295
1296/**
1297 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1298 *
1299 * \param hash the input hash
1300 * \param hlen length of the input hash
1301 * \param salt the input salt
1302 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001303 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001304 * \param md_alg message digest to use
1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001306static int hash_mprime(const unsigned char *hash, size_t hlen,
1307 const unsigned char *salt, size_t slen,
1308 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001309{
1310 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001311
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001312 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1316 if (md_info == NULL) {
1317 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1318 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 mbedtls_md_init(&md_ctx);
1321 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001322 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 }
1324 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001325 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 }
1327 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001328 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 }
1330 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001331 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 }
1333 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001334 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 }
1336 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001337 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001339
1340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001344}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001345
1346/**
1347 * Compute a hash.
1348 *
1349 * \param md_alg algorithm to use
1350 * \param input input message to hash
1351 * \param ilen input length
1352 * \param output the output buffer - must be large enough for \p md_alg
1353 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001354static int compute_hash(mbedtls_md_type_t md_alg,
1355 const unsigned char *input, size_t ilen,
1356 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001357{
1358 const mbedtls_md_info_t *md_info;
1359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 md_info = mbedtls_md_info_from_type(md_alg);
1361 if (md_info == NULL) {
1362 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1363 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001366}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001370/*
1371 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1372 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001373int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1374 int (*f_rng)(void *, unsigned char *, size_t),
1375 void *p_rng,
1376 const unsigned char *label, size_t label_len,
1377 size_t ilen,
1378 const unsigned char *input,
1379 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001380{
1381 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001382 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001383 unsigned char *p = output;
1384 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 if (f_rng == NULL) {
1387 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1388 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1391 if (hlen == 0) {
1392 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1393 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001394
1395 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001396
Simon Butcher02037452016-03-01 21:19:12 +00001397 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1399 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1400 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001403
1404 *p++ = 0;
1405
Simon Butcher02037452016-03-01 21:19:12 +00001406 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1408 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1409 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001410
1411 p += hlen;
1412
Simon Butcher02037452016-03-01 21:19:12 +00001413 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1415 if (ret != 0) {
1416 return ret;
1417 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001418 p += hlen;
1419 p += olen - 2 * hlen - 2 - ilen;
1420 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if (ilen != 0) {
1422 memcpy(p, input, ilen);
1423 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001424
Simon Butcher02037452016-03-01 21:19:12 +00001425 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1427 ctx->hash_id)) != 0) {
1428 return ret;
1429 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001430
Simon Butcher02037452016-03-01 21:19:12 +00001431 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1433 ctx->hash_id)) != 0) {
1434 return ret;
1435 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001442/*
1443 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1444 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001445int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1446 int (*f_rng)(void *, unsigned char *, size_t),
1447 void *p_rng, size_t ilen,
1448 const unsigned char *input,
1449 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001450{
1451 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001453 unsigned char *p = output;
1454
Paul Bakkerb3869132013-02-28 17:21:01 +01001455 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001456
Simon Butcher02037452016-03-01 21:19:12 +00001457 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (ilen + 11 < ilen || olen < ilen + 11) {
1459 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1460 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001461
1462 nb_pad = olen - 3 - ilen;
1463
1464 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 if (f_rng == NULL) {
1467 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1468 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001469
1470 *p++ = MBEDTLS_RSA_CRYPT;
1471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001473 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001474
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001475 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 ret = f_rng(p_rng, p, 1);
1477 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001478
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001479 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if (rng_dl == 0 || ret != 0) {
1481 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1482 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001483
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001484 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001485 }
1486
1487 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (ilen != 0) {
1489 memcpy(p, input, ilen);
1490 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001493}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001495
Paul Bakker5121ce52009-01-03 21:22:43 +00001496/*
1497 * Add the message padding, then do an RSA operation
1498 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001499int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1500 int (*f_rng)(void *, unsigned char *, size_t),
1501 void *p_rng,
1502 size_t ilen,
1503 const unsigned char *input,
1504 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001505{
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#if defined(MBEDTLS_PKCS1_V15)
1508 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1510 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001511#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_PKCS1_V21)
1514 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1516 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001517#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
1519 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001522}
1523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001525/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001526 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001528int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1529 int (*f_rng)(void *, unsigned char *, size_t),
1530 void *p_rng,
1531 const unsigned char *label, size_t label_len,
1532 size_t *olen,
1533 const unsigned char *input,
1534 unsigned char *output,
1535 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001536{
Janos Follath24eed8d2019-11-22 13:21:35 +00001537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001538 size_t ilen, i, pad_len;
1539 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001541 unsigned char lhash[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001542 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001543
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001544 /*
1545 * Parameters sanity checks
1546 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1548 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1549 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001550
1551 ilen = ctx->len;
1552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 if (ilen < 16 || ilen > sizeof(buf)) {
1554 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1558 if (hlen == 0) {
1559 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1560 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001561
Janos Follathc17cda12016-02-11 11:08:18 +00001562 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 if (2 * hlen + 2 > ilen) {
1564 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1565 }
Janos Follathc17cda12016-02-11 11:08:18 +00001566
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001567 /*
1568 * RSA operation
1569 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001573 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001575
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001576 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001577 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001578 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001579 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1581 ctx->hash_id)) != 0 ||
1582 /* DB: Apply dbMask to maskedDB */
1583 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1584 ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001585 goto cleanup;
1586 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001587
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001588 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1590 label, label_len, lhash);
1591 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001592 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001594
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001595 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001596 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001597 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001599 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001600
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001601 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001602
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001603 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001604
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001605 /* Check lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 for (i = 0; i < hlen; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001607 bad |= lhash[i] ^ *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001609
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001610 /* Get zero-padding len, but always read till end of buffer
1611 * (minus one, for the 01 byte) */
1612 pad_len = 0;
1613 pad_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001615 pad_done |= p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001617 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001618
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001619 p += pad_len;
1620 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001621
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001622 /*
1623 * The only information "leaked" is whether the padding was correct or not
1624 * (eg, no data is copied if it was not correct). This meets the
1625 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1626 * the different error conditions.
1627 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001629 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1630 goto cleanup;
1631 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001634 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1635 goto cleanup;
1636 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001637
1638 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 if (*olen != 0) {
1640 memcpy(output, p, *olen);
1641 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001642 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001643
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001644cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 mbedtls_platform_zeroize(buf, sizeof(buf));
1646 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001653/*
1654 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1655 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001656int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1657 int (*f_rng)(void *, unsigned char *, size_t),
1658 void *p_rng,
1659 size_t *olen,
1660 const unsigned char *input,
1661 unsigned char *output,
1662 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001663{
1664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1665 size_t ilen;
1666 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1667
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001668 ilen = ctx->len;
1669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1671 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1672 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 if (ilen < 16 || ilen > sizeof(buf)) {
1675 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1676 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001681 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1685 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001686
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001687cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001691}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001693
1694/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001695 * Do an RSA operation, then remove the message padding
1696 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001697int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1698 int (*f_rng)(void *, unsigned char *, size_t),
1699 void *p_rng,
1700 size_t *olen,
1701 const unsigned char *input,
1702 unsigned char *output,
1703 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001704{
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_PKCS1_V15)
1707 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1709 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001710#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#if defined(MBEDTLS_PKCS1_V21)
1713 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1715 olen, input, output,
1716 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001717#endif
1718
1719 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001721 }
1722}
1723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001725static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1726 int (*f_rng)(void *, unsigned char *, size_t),
1727 void *p_rng,
1728 mbedtls_md_type_t md_alg,
1729 unsigned int hashlen,
1730 const unsigned char *hash,
1731 int saltlen,
1732 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001733{
1734 size_t olen;
1735 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001736 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001737 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001738 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001739 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001742 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1746 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1747 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if (f_rng == NULL) {
1750 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1751 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001752
1753 olen = ctx->len;
1754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001756 /* Gather length of hash to sign */
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 size_t exp_hashlen = mbedtls_hash_info_get_size(md_alg);
1758 if (exp_hashlen == 0) {
1759 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1760 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (hashlen != exp_hashlen) {
1763 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1764 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001765 }
1766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1768 if (hlen == 0) {
1769 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1770 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1773 /* Calculate the largest possible salt length, up to the hash size.
1774 * Normally this is the hash length, which is the maximum salt length
1775 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1776 * enough room, use the maximum salt length that fits. The constraint is
1777 * that the hash length plus the salt length plus 2 bytes must be at most
1778 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1779 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001780 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (olen < hlen + min_slen + 2) {
1782 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1783 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001784 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001786 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 }
1788 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1789 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1790 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001791 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001792 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001795
Simon Butcher02037452016-03-01 21:19:12 +00001796 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001798 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001799 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001800
1801 /* Generate salt of length slen in place in the encoded message */
1802 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1804 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1805 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001806
Paul Bakkerb3869132013-02-28 17:21:01 +01001807 p += slen;
1808
Simon Butcher02037452016-03-01 21:19:12 +00001809 /* Generate H = Hash( M' ) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 ret = hash_mprime(hash, hashlen, salt, slen, p, ctx->hash_id);
1811 if (ret != 0) {
1812 return ret;
1813 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001814
Simon Butcher02037452016-03-01 21:19:12 +00001815 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001817 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001819
Simon Butcher02037452016-03-01 21:19:12 +00001820 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
1822 ctx->hash_id);
1823 if (ret != 0) {
1824 return ret;
1825 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1828 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001829
1830 p += hlen;
1831 *p++ = 0xBC;
1832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001834}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001835
1836/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001837 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1838 * the option to pass in the salt length.
1839 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001840int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1841 int (*f_rng)(void *, unsigned char *, size_t),
1842 void *p_rng,
1843 mbedtls_md_type_t md_alg,
1844 unsigned int hashlen,
1845 const unsigned char *hash,
1846 int saltlen,
1847 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001848{
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1850 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001851}
1852
1853
1854/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001855 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1856 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1858 int (*f_rng)(void *, unsigned char *, size_t),
1859 void *p_rng,
1860 mbedtls_md_type_t md_alg,
1861 unsigned int hashlen,
1862 const unsigned char *hash,
1863 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001864{
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1866 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001867}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001871/*
1872 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1873 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001874
1875/* Construct a PKCS v1.5 encoding of a hashed message
1876 *
1877 * This is used both for signature generation and verification.
1878 *
1879 * Parameters:
1880 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001881 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001882 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001883 * - hash: Buffer containing the hashed message or the raw data.
1884 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001885 * - dst: Buffer to hold the encoded message.
1886 *
1887 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001888 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001889 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001890 *
1891 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001892static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1893 unsigned int hashlen,
1894 const unsigned char *hash,
1895 size_t dst_len,
1896 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001897{
1898 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001899 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001900 unsigned char *p = dst;
1901 const char *oid = NULL;
1902
1903 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 if (md_alg != MBEDTLS_MD_NONE) {
1905 unsigned char md_size = mbedtls_hash_info_get_size(md_alg);
1906 if (md_size == 0) {
1907 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1908 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1911 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1912 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 if (hashlen != md_size) {
1915 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1916 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001917
1918 /* Double-check that 8 + hashlen + oid_size can be used as a
1919 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001921 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 10 + hashlen + oid_size < 10 + hashlen) {
1923 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1924 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001925
1926 /*
1927 * Static bounds check:
1928 * - Need 10 bytes for five tag-length pairs.
1929 * (Insist on 1-byte length encodings to protect against variants of
1930 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1931 * - Need hashlen bytes for hash
1932 * - Need oid_size bytes for hash alg OID.
1933 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 if (nb_pad < 10 + hashlen + oid_size) {
1935 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1936 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001937 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 } else {
1939 if (nb_pad < hashlen) {
1940 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1941 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001942
1943 nb_pad -= hashlen;
1944 }
1945
Hanno Becker2b2f8982017-09-27 17:10:03 +01001946 /* Need space for signature header and padding delimiter (3 bytes),
1947 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 if (nb_pad < 3 + 8) {
1949 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1950 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001951 nb_pad -= 3;
1952
1953 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001954 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001955
1956 /* Write signature header and padding */
1957 *p++ = 0;
1958 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001960 p += nb_pad;
1961 *p++ = 0;
1962
1963 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 if (md_alg == MBEDTLS_MD_NONE) {
1965 memcpy(p, hash, hashlen);
1966 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001967 }
1968
1969 /* Signing hashed data, add corresponding ASN.1 structure
1970 *
1971 * DigestInfo ::= SEQUENCE {
1972 * digestAlgorithm DigestAlgorithmIdentifier,
1973 * digest Digest }
1974 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1975 * Digest ::= OCTET STRING
1976 *
1977 * Schematic:
1978 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1979 * TAG-NULL + LEN [ NULL ] ]
1980 * TAG-OCTET + LEN [ HASH ] ]
1981 */
1982 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001984 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001986 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001987 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001989 p += oid_size;
1990 *p++ = MBEDTLS_ASN1_NULL;
1991 *p++ = 0x00;
1992 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001993 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001995 p += hashlen;
1996
1997 /* Just a sanity-check, should be automatic
1998 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 if (p != dst + dst_len) {
2000 mbedtls_platform_zeroize(dst, dst_len);
2001 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002002 }
2003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002005}
2006
Paul Bakkerb3869132013-02-28 17:21:01 +01002007/*
2008 * Do an RSA operation to sign the message digest
2009 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002010int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2011 int (*f_rng)(void *, unsigned char *, size_t),
2012 void *p_rng,
2013 mbedtls_md_type_t md_alg,
2014 unsigned int hashlen,
2015 const unsigned char *hash,
2016 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002017{
Janos Follath24eed8d2019-11-22 13:21:35 +00002018 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002019 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002022 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2026 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2027 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002028
Hanno Beckerfdf38032017-09-06 12:35:55 +01002029 /*
2030 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2031 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2034 ctx->len, sig)) != 0) {
2035 return ret;
2036 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002037
Hanno Beckerfdf38032017-09-06 12:35:55 +01002038 /* Private key operation
2039 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002040 * In order to prevent Lenstra's attack, make the signature in a
2041 * temporary buffer and check it before returning it.
2042 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 sig_try = mbedtls_calloc(1, ctx->len);
2045 if (sig_try == NULL) {
2046 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002047 }
2048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 verif = mbedtls_calloc(1, ctx->len);
2050 if (verif == NULL) {
2051 mbedtls_free(sig_try);
2052 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2053 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2056 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2057
2058 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002059 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2060 goto cleanup;
2061 }
2062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002064
2065cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 mbedtls_platform_zeroize(sig_try, ctx->len);
2067 mbedtls_platform_zeroize(verif, ctx->len);
2068 mbedtls_free(sig_try);
2069 mbedtls_free(verif);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 if (ret != 0) {
2072 memset(sig, '!', ctx->len);
2073 }
2074 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002077
2078/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 * Do an RSA operation to sign the message digest
2080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002081int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2082 int (*f_rng)(void *, unsigned char *, size_t),
2083 void *p_rng,
2084 mbedtls_md_type_t md_alg,
2085 unsigned int hashlen,
2086 const unsigned char *hash,
2087 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002088{
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002090 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002092
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094#if defined(MBEDTLS_PKCS1_V15)
2095 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2097 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002098#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#if defined(MBEDTLS_PKCS1_V21)
2101 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2103 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002104#endif
2105
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002109}
2110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002112/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002113 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002115int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2116 mbedtls_md_type_t md_alg,
2117 unsigned int hashlen,
2118 const unsigned char *hash,
2119 mbedtls_md_type_t mgf1_hash_id,
2120 int expected_salt_len,
2121 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002122{
Janos Follath24eed8d2019-11-22 13:21:35 +00002123 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002124 size_t siglen;
2125 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002126 unsigned char *hash_start;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002127 unsigned char result[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002128 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002129 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002133 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002135
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 siglen = ctx->len;
2137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if (siglen < 16 || siglen > sizeof(buf)) {
2139 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2140 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 if (ret != 0) {
2145 return ret;
2146 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
2148 p = buf;
2149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if (buf[siglen - 1] != 0xBC) {
2151 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002152 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (md_alg != MBEDTLS_MD_NONE) {
2155 /* Gather length of hash to sign */
2156 size_t exp_hashlen = mbedtls_hash_info_get_size(md_alg);
2157 if (exp_hashlen == 0) {
2158 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2159 }
2160
2161 if (hashlen != exp_hashlen) {
2162 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2163 }
2164 }
2165
2166 hlen = mbedtls_hash_info_get_size(mgf1_hash_id);
2167 if (hlen == 0) {
2168 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2169 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002170
Simon Butcher02037452016-03-01 21:19:12 +00002171 /*
2172 * Note: EMSA-PSS verification is over the length of N - 1 bits
2173 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (buf[0] >> (8 - siglen * 8 + msb)) {
2177 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2178 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002179
Simon Butcher02037452016-03-01 21:19:12 +00002180 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002182 p++;
2183 siglen -= 1;
2184 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002185
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (siglen < hlen + 2) {
2187 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2188 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002189 hash_start = p + siglen - hlen - 1;
2190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2192 if (ret != 0) {
2193 return ret;
2194 }
Paul Bakker02303e82013-01-03 11:08:31 +01002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002199 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (*p++ != 0x01) {
2203 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2204 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002205
Gilles Peskine6a54b022017-10-17 19:02:13 +02002206 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2209 observed_salt_len != (size_t) expected_salt_len) {
2210 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002211 }
2212
Simon Butcher02037452016-03-01 21:19:12 +00002213 /*
2214 * Generate H = Hash( M' )
2215 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2217 result, mgf1_hash_id);
2218 if (ret != 0) {
2219 return ret;
2220 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 if (memcmp(hash_start, result, hlen) != 0) {
2223 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2224 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002227}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002228
2229/*
2230 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002232int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2233 mbedtls_md_type_t md_alg,
2234 unsigned int hashlen,
2235 const unsigned char *hash,
2236 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002237{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002238 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002240 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002245 : md_alg;
2246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2248 md_alg, hashlen, hash,
2249 mgf1_hash_id,
2250 MBEDTLS_RSA_SALT_LEN_ANY,
2251 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002252
2253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002257/*
2258 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2259 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002260int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2261 mbedtls_md_type_t md_alg,
2262 unsigned int hashlen,
2263 const unsigned char *hash,
2264 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002265{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002266 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002267 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002268 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002271 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002273
2274 sig_len = ctx->len;
2275
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002276 /*
2277 * Prepare expected PKCS1 v1.5 encoding of hash.
2278 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2281 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002282 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2283 goto cleanup;
2284 }
2285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2287 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002288 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002290
2291 /*
2292 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2293 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 ret = mbedtls_rsa_public(ctx, sig, encoded);
2296 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002297 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002299
Simon Butcher02037452016-03-01 21:19:12 +00002300 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002301 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002302 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2305 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002306 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2307 goto cleanup;
2308 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002309
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002310cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (encoded != NULL) {
2313 mbedtls_platform_zeroize(encoded, sig_len);
2314 mbedtls_free(encoded);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002315 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 if (encoded_expected != NULL) {
2318 mbedtls_platform_zeroize(encoded_expected, sig_len);
2319 mbedtls_free(encoded_expected);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002320 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002323}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
2326/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002327 * Do an RSA operation and check the message digest
2328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002329int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2330 mbedtls_md_type_t md_alg,
2331 unsigned int hashlen,
2332 const unsigned char *hash,
2333 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002334{
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002336 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340#if defined(MBEDTLS_PKCS1_V15)
2341 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2343 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002344#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#if defined(MBEDTLS_PKCS1_V21)
2347 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2349 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002350#endif
2351
2352 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002354 }
2355}
2356
2357/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002358 * Copy the components of an RSA key
2359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002360int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002361{
Janos Follath24eed8d2019-11-22 13:21:35 +00002362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002363
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002364 dst->len = src->len;
2365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2367 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2370 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2371 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002372
2373#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2375 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2376 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2377 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2378 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002379#endif
2380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2384 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002385
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002386 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002387 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002388
2389cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (ret != 0) {
2391 mbedtls_rsa_free(dst);
2392 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002395}
2396
2397/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 * Free the components of an RSA key
2399 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002400void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002401{
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002403 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 mbedtls_mpi_free(&ctx->Vi);
2407 mbedtls_mpi_free(&ctx->Vf);
2408 mbedtls_mpi_free(&ctx->RN);
2409 mbedtls_mpi_free(&ctx->D);
2410 mbedtls_mpi_free(&ctx->Q);
2411 mbedtls_mpi_free(&ctx->P);
2412 mbedtls_mpi_free(&ctx->E);
2413 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002414
Hanno Becker33c30a02017-08-23 07:00:22 +01002415#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 mbedtls_mpi_free(&ctx->RQ);
2417 mbedtls_mpi_free(&ctx->RP);
2418 mbedtls_mpi_free(&ctx->QP);
2419 mbedtls_mpi_free(&ctx->DQ);
2420 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002421#endif /* MBEDTLS_RSA_NO_CRT */
2422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002424 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (ctx->ver != 0) {
2426 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002427 ctx->ver = 0;
2428 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002429#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002430}
2431
Hanno Beckerab377312017-08-23 16:24:51 +01002432#endif /* !MBEDTLS_RSA_ALT */
2433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002435
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002436#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002437
2438/*
2439 * Example RSA-1024 keypair, for test purposes
2440 */
2441#define KEY_LEN 128
2442
2443#define RSA_N "9292758453063D803DD603D5E777D788" \
2444 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2445 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2446 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2447 "93A89813FBF3C4F8066D2D800F7C38A8" \
2448 "1AE31942917403FF4946B0A83D3D3E05" \
2449 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2450 "5E94BB77B07507233A0BC7BAC8F90F79"
2451
2452#define RSA_E "10001"
2453
2454#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2455 "66CA472BC44D253102F8B4A9D3BFA750" \
2456 "91386C0077937FE33FA3252D28855837" \
2457 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2458 "DF79C5CE07EE72C7F123142198164234" \
2459 "CABB724CF78B8173B9F880FC86322407" \
2460 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2461 "071513A1E85B5DFA031F21ECAE91A34D"
2462
2463#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2464 "2C01CAD19EA484A87EA4377637E75500" \
2465 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2466 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2467
2468#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2469 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2470 "910E4168387E3C30AA1E00C339A79508" \
2471 "8452DD96A9A5EA5D9DCA68DA636032AF"
2472
Paul Bakker5121ce52009-01-03 21:22:43 +00002473#define PT_LEN 24
2474#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2475 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002478static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002479{
gufe44c2620da2020-08-03 17:56:50 +02002480#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002481 size_t i;
2482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002484 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 }
Paul Bakker545570e2010-07-18 09:00:25 +00002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002488 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002490#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002492 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002496#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002501
Paul Bakker5121ce52009-01-03 21:22:43 +00002502/*
2503 * Checkup routine
2504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002505int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002506{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002507 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002509 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 unsigned char rsa_plaintext[PT_LEN];
2512 unsigned char rsa_decrypted[PT_LEN];
2513 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002514#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002515 unsigned char sha1sum[20];
2516#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Hanno Becker3a701162017-08-22 13:52:43 +01002518 mbedtls_mpi K;
2519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 mbedtls_mpi_init(&K);
2521 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2524 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2525 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2526 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2527 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2528 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2529 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2530 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2531 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2532 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (verbose != 0) {
2537 mbedtls_printf(" RSA key validation: ");
2538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2541 mbedtls_rsa_check_privkey(&rsa) != 0) {
2542 if (verbose != 0) {
2543 mbedtls_printf("failed\n");
2544 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Hanno Becker5bc87292017-05-03 15:09:31 +01002546 ret = 1;
2547 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 }
2549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (verbose != 0) {
2551 mbedtls_printf("passed\n PKCS#1 encryption : ");
2552 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2557 PT_LEN, rsa_plaintext,
2558 rsa_ciphertext) != 0) {
2559 if (verbose != 0) {
2560 mbedtls_printf("failed\n");
2561 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
Hanno Becker5bc87292017-05-03 15:09:31 +01002563 ret = 1;
2564 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 }
2566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (verbose != 0) {
2568 mbedtls_printf("passed\n PKCS#1 decryption : ");
2569 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2572 &len, rsa_ciphertext, rsa_decrypted,
2573 sizeof(rsa_decrypted)) != 0) {
2574 if (verbose != 0) {
2575 mbedtls_printf("failed\n");
2576 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Hanno Becker5bc87292017-05-03 15:09:31 +01002578 ret = 1;
2579 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 }
2581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2583 if (verbose != 0) {
2584 mbedtls_printf("failed\n");
2585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Hanno Becker5bc87292017-05-03 15:09:31 +01002587 ret = 1;
2588 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 }
2590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if (verbose != 0) {
2592 mbedtls_printf("passed\n");
2593 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002594
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002595#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (verbose != 0) {
2597 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002600 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2601 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 if (verbose != 0) {
2603 mbedtls_printf("failed\n");
2604 }
2605
2606 return 1;
2607 }
2608
2609 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2610 MBEDTLS_MD_SHA1, 20,
2611 sha1sum, rsa_ciphertext) != 0) {
2612 if (verbose != 0) {
2613 mbedtls_printf("failed\n");
2614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Hanno Becker5bc87292017-05-03 15:09:31 +01002616 ret = 1;
2617 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
2619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (verbose != 0) {
2621 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2622 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2625 sha1sum, rsa_ciphertext) != 0) {
2626 if (verbose != 0) {
2627 mbedtls_printf("failed\n");
2628 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002629
Hanno Becker5bc87292017-05-03 15:09:31 +01002630 ret = 1;
2631 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 }
2633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (verbose != 0) {
2635 mbedtls_printf("passed\n");
2636 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002637#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 if (verbose != 0) {
2640 mbedtls_printf("\n");
2641 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002642
Paul Bakker3d8fb632014-04-17 12:42:41 +02002643cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 mbedtls_mpi_free(&K);
2645 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002647 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650}
2651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654#endif /* MBEDTLS_RSA_C */