blob: 63102d7a0e170afc13285cfd73de2246684462e2 [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é-Gonnard077ba842022-07-27 10:42:31 +020057/* We use MD first if it's available (for compatibility reasons)
58 * and "fall back" to PSA otherwise (which needs psa_crypto_init()). */
59#if defined(MBEDTLS_PKCS1_V21)
Przemek Stekiel40afdd22022-09-06 13:08:28 +020060#if !defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +020061#include "psa/crypto.h"
62#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +020063#endif /* MBEDTLS_MD_C */
64#endif /* MBEDTLS_PKCS1_V21 */
65
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010067
Hanno Beckera565f542017-10-11 11:00:19 +010068#if !defined(MBEDTLS_RSA_ALT)
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
71 const mbedtls_mpi *N,
72 const mbedtls_mpi *P, const mbedtls_mpi *Q,
73 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +010074{
Janos Follath24eed8d2019-11-22 13:21:35 +000075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +010076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
78 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
79 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
80 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
81 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
82 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +010083 }
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if (N != NULL) {
86 ctx->len = mbedtls_mpi_size(&ctx->N);
87 }
Hanno Becker617c1ae2017-08-23 14:11:24 +010088
Gilles Peskine449bd832023-01-11 14:50:10 +010089 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +010090}
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
93 unsigned char const *N, size_t N_len,
94 unsigned char const *P, size_t P_len,
95 unsigned char const *Q, size_t Q_len,
96 unsigned char const *D, size_t D_len,
97 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +010098{
Hanno Beckerd4d60572018-01-10 07:12:01 +000099 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (N != NULL) {
102 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
103 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100104 }
105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (P != NULL) {
107 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
108 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (Q != NULL) {
111 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
112 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (D != NULL) {
115 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
116 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (E != NULL) {
119 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
120 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100121
122cleanup:
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (ret != 0) {
125 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
126 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100129}
130
Hanno Becker705fc682017-10-10 17:57:02 +0100131/*
132 * Checks whether the context fields are set in such a way
133 * that the RSA primitives will be able to execute without error.
134 * It does *not* make guarantees for consistency of the parameters.
135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
137 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100138{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100139#if !defined(MBEDTLS_RSA_NO_CRT)
140 /* blinding_needed is only used for NO_CRT to decide whether
141 * P,Q need to be present or not. */
142 ((void) blinding_needed);
143#endif
144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
146 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
147 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000148 }
Hanno Becker705fc682017-10-10 17:57:02 +0100149
150 /*
151 * 1. Modular exponentiation needs positive, odd moduli.
152 */
153
154 /* Modular exponentiation wrt. N is always used for
155 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
157 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
158 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100159 }
160
161#if !defined(MBEDTLS_RSA_NO_CRT)
162 /* Modular exponentiation for P and Q is only
163 * used for private key operations and if CRT
164 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (is_priv &&
166 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
167 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
168 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
169 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
170 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100171 }
172#endif /* !MBEDTLS_RSA_NO_CRT */
173
174 /*
175 * 2. Exponents must be positive
176 */
177
178 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
180 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
181 }
Hanno Becker705fc682017-10-10 17:57:02 +0100182
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100183#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100184 /* For private key operations, use D or DP & DQ
185 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
187 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
188 }
Hanno Becker705fc682017-10-10 17:57:02 +0100189#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (is_priv &&
191 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
192 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
193 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100194 }
195#endif /* MBEDTLS_RSA_NO_CRT */
196
197 /* Blinding shouldn't make exponents negative either,
198 * so check that P, Q >= 1 if that hasn't yet been
199 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100200#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (is_priv && blinding_needed &&
202 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
203 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
204 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100205 }
206#endif
207
208 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100209 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100210#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (is_priv &&
212 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
213 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100214 }
215#endif
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100218}
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100221{
222 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500223 int have_N, have_P, have_Q, have_D, have_E;
224#if !defined(MBEDTLS_RSA_NO_CRT)
225 int have_DP, have_DQ, have_QP;
226#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
230 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
231 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
232 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
233 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500234
235#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
237 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
238 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500239#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100240
Hanno Becker617c1ae2017-08-23 14:11:24 +0100241 /*
242 * Check whether provided parameters are enough
243 * to deduce all others. The following incomplete
244 * parameter sets for private keys are supported:
245 *
246 * (1) P, Q missing.
247 * (2) D and potentially N missing.
248 *
249 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100250
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500251 n_missing = have_P && have_Q && have_D && have_E;
252 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
253 d_missing = have_P && have_Q && !have_D && have_E;
254 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100255
256 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500257 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (!is_priv && !is_pub) {
260 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
261 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100262
263 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100264 * Step 1: Deduce N if P, Q are provided.
265 */
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (!have_N && have_P && have_Q) {
268 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
269 &ctx->Q)) != 0) {
270 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100271 }
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100274 }
275
276 /*
277 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100278 */
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (pq_missing) {
281 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
282 &ctx->P, &ctx->Q);
283 if (ret != 0) {
284 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
285 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 } else if (d_missing) {
288 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
289 &ctx->Q,
290 &ctx->E,
291 &ctx->D)) != 0) {
292 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100293 }
294 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100295
Hanno Becker617c1ae2017-08-23 14:11:24 +0100296 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100297 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100298 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100299 */
300
Hanno Becker23344b52017-08-23 07:43:27 +0100301#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (is_priv && !(have_DP && have_DQ && have_QP)) {
303 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
304 &ctx->DP, &ctx->DQ, &ctx->QP);
305 if (ret != 0) {
306 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
307 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100308 }
Hanno Becker23344b52017-08-23 07:43:27 +0100309#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100310
311 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100312 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100313 */
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100316}
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
319 unsigned char *N, size_t N_len,
320 unsigned char *P, size_t P_len,
321 unsigned char *Q, size_t Q_len,
322 unsigned char *D, size_t D_len,
323 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324{
325 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500326 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327
328 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500329 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
331 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
332 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
333 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
334 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100337 /* If we're trying to export private parameters for a public key,
338 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (P != NULL || Q != NULL || D != NULL) {
340 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
341 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100342
343 }
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (N != NULL) {
346 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
347 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (P != NULL) {
350 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
351 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (Q != NULL) {
354 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
355 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (D != NULL) {
358 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
359 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (E != NULL) {
362 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
363 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100364
365cleanup:
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100368}
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
371 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
372 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100373{
Janos Follath24eed8d2019-11-22 13:21:35 +0000374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500375 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100376
377 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500378 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
380 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
381 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
382 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
383 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100386 /* If we're trying to export private parameters for a public key,
387 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (P != NULL || Q != NULL || D != NULL) {
389 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
390 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100391
392 }
393
394 /* Export all requested core parameters. */
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
397 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
398 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
399 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
400 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
401 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100402 }
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100405}
406
407/*
408 * Export CRT parameters
409 * This must also be implemented if CRT is not used, for being able to
410 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
411 * can be used in this case.
412 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
414 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100415{
Janos Follath24eed8d2019-11-22 13:21:35 +0000416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500417 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100418
419 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500420 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
422 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
423 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
424 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
425 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (!is_priv) {
428 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
429 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100430
Hanno Beckerdc95c892017-08-23 06:57:02 +0100431#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100432 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
434 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
435 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
436 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100437 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100438#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
440 DP, DQ, QP)) != 0) {
441 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100442 }
443#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100446}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100447
Paul Bakker5121ce52009-01-03 21:22:43 +0000448/*
449 * Initialize an RSA context
450 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000452{
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000454
Ronald Cronc1905a12021-06-05 11:11:14 +0200455 ctx->padding = MBEDTLS_RSA_PKCS_V15;
456 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100459 /* Set ctx->ver to nonzero to indicate that the mutex has been
460 * initialized and will need to be freed. */
461 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200463#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000464}
465
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100466/*
467 * Set padding for an existing RSA context
468 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
470 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100471{
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200473#if defined(MBEDTLS_PKCS1_V15)
474 case MBEDTLS_RSA_PKCS_V15:
475 break;
476#endif
477
478#if defined(MBEDTLS_PKCS1_V21)
479 case MBEDTLS_RSA_PKCS_V21:
480 break;
481#endif
482 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200484 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200485
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200486#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
488 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200489 /* Just make sure this hash is supported in this build. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (mbedtls_hash_info_psa_from_md(hash_id) == PSA_ALG_NONE) {
491 return MBEDTLS_ERR_RSA_INVALID_PADDING;
492 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200493 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200494#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500495
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100496 ctx->padding = padding;
497 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100500}
501
Hanno Becker617c1ae2017-08-23 14:11:24 +0100502/*
Yanray Wang83548b52023-03-15 16:46:34 +0800503 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800504 */
505int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
506{
507 return ctx->MBEDTLS_PRIVATE(padding);
508}
509
510/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800511 * Get hash identifier of mbedtls_md_type_t type
512 */
513int mbedtls_rsa_get_hash_id(const mbedtls_rsa_context *ctx)
514{
515 return ctx->MBEDTLS_PRIVATE(hash_id);
516}
517
518/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100519 * Get length in bytes of RSA modulus
520 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100522{
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100524}
525
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529/*
530 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800531 *
532 * This generation method follows the RSA key pair generation procedure of
533 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100535int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
536 int (*f_rng)(void *, unsigned char *, size_t),
537 void *p_rng,
538 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000539{
Janos Follath24eed8d2019-11-22 13:21:35 +0000540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800541 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100542 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Janos Follathb8fc1b02018-09-03 15:37:01 +0100544 /*
545 * If the modulus is 1024 bit long or shorter, then the security strength of
546 * the RSA algorithm is less than or equal to 80 bits and therefore an error
547 * rate of 2^-80 is sufficient.
548 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100550 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 mbedtls_mpi_init(&H);
554 mbedtls_mpi_init(&G);
555 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (nbits < 128 || exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100558 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
559 goto cleanup;
560 }
561
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 /*
563 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800564 * 1. |P-Q| > 2^( nbits / 2 - 100 )
565 * 2. GCD( E, (P-1)*(Q-1) ) == 1
566 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 do {
571 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
572 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
575 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800577 /* 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 +0100578 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
579 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800583 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (H.s < 0) {
585 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
586 }
Janos Follathef441782016-09-21 13:18:12 +0100587
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100588 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
590 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
591 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800592
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800593 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
595 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800596 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800598
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800599 /* 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 +0100600 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
601 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
602 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 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 -0800605 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800607
608 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000610
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100611 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
613 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100618
Jethro Beekman97f95c92018-02-13 15:50:36 -0800619#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 * DP = D mod (P - 1)
622 * DQ = D mod (Q - 1)
623 * QP = Q^-1 mod P
624 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
626 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100627#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
Hanno Becker83aad1f2017-08-23 06:45:10 +0100629 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
632cleanup:
633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 mbedtls_mpi_free(&H);
635 mbedtls_mpi_free(&G);
636 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (ret != 0) {
639 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if ((-ret & ~0x7f) == 0) {
642 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
643 }
644 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 }
646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000648}
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
652/*
653 * Check a public RSA key
654 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000656{
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
658 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100659 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
662 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100663 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
666 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
667 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
668 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
669 }
670
671 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000672}
673
674/*
Hanno Becker705fc682017-10-10 17:57:02 +0100675 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100677int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000678{
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
680 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
681 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 }
Paul Bakker48377d92013-08-30 12:06:24 +0200683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
685 &ctx->D, &ctx->E, NULL, NULL) != 0) {
686 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000688
Hanno Beckerb269a852017-08-25 08:03:21 +0100689#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
691 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
692 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100693 }
694#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000697}
698
699/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100700 * Check if contexts holding a public and private key match
701 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100702int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
703 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100704{
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
706 mbedtls_rsa_check_privkey(prv) != 0) {
707 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708 }
709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
711 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
712 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100713 }
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100716}
717
718/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 * Do an RSA public key operation
720 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
722 const unsigned char *input,
723 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000724{
Janos Follath24eed8d2019-11-22 13:21:35 +0000725 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000726 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
730 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
731 }
Hanno Becker705fc682017-10-10 17:57:02 +0100732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200735#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
737 return ret;
738 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200739#endif
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200744 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
745 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000746 }
747
748 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
750 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
752cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
755 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
756 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100757#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (ret != 0) {
762 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
763 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000766}
767
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200768/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200769 * Generate or update blinding values, see section 10 of:
770 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200771 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200772 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200773 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
775 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200776{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200777 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200778 mbedtls_mpi R;
779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200783 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
785 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
786 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
787 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200788
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200789 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200790 }
791
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200792 /* Unblinding value: Vf = random number, invertible mod N */
793 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200795 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
796 goto cleanup;
797 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 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 +0200800
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200801 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
803 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
804 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200805
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200806 /* At this point, Vi is invertible mod N if and only if both Vf and R
807 * are invertible mod N. If one of them isn't, we don't need to know
808 * which one, we just loop and choose new values for both of them.
809 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
811 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200812 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500816
817 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
819 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200820
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200821 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200822 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 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 +0200824
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200825
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200826cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200830}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200831
Paul Bakker5121ce52009-01-03 21:22:43 +0000832/*
Janos Follathe81102e2017-03-22 13:38:28 +0000833 * Exponent blinding supposed to prevent side-channel attacks using multiple
834 * traces of measurements to recover the RSA key. The more collisions are there,
835 * the more bits of the key can be recovered. See [3].
836 *
837 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800838 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000839 *
840 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800841 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000842 *
843 * (With the currently (as of 2017 April) known best algorithms breaking 2048
844 * bit RSA requires approximately as much time as trying out 2^112 random keys.
845 * Thus in this sense with 28 byte blinding the security is not reduced by
846 * side-channel attacks like the one in [3])
847 *
848 * This countermeasure does not help if the key recovery is possible with a
849 * single trace.
850 */
851#define RSA_EXPONENT_BLINDING 28
852
853/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000854 * Do an RSA private key operation
855 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
857 int (*f_rng)(void *, unsigned char *, size_t),
858 void *p_rng,
859 const unsigned char *input,
860 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000861{
Janos Follath24eed8d2019-11-22 13:21:35 +0000862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000863 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100864
865 /* Temporary holding the result */
866 mbedtls_mpi T;
867
868 /* Temporaries holding P-1, Q-1 and the
869 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000870 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100871
872#if !defined(MBEDTLS_RSA_NO_CRT)
873 /* Temporaries holding the results mod p resp. mod q. */
874 mbedtls_mpi TP, TQ;
875
876 /* Temporaries holding the blinded exponents for
877 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000878 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100879
880 /* Pointers to actual exponents to be used - either the unblinded
881 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000882 mbedtls_mpi *DP = &ctx->DP;
883 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100884#else
885 /* Temporary holding the blinded exponent (if used). */
886 mbedtls_mpi D_blind;
887
888 /* Pointer to actual exponent to be used - either the unblinded
889 * or the blinded one, depending on the presence of a PRNG. */
890 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100891#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100892
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100893 /* Temporaries holding the initial input and the double
894 * checked result; should be the same in the end. */
895 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (f_rng == NULL) {
898 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
899 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (rsa_check_context(ctx, 1 /* private key checks */,
902 1 /* blinding on */) != 0) {
903 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +0100904 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100905
Hanno Becker06811ce2017-05-03 15:10:34 +0100906#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
908 return ret;
909 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100910#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000911
Hanno Becker06811ce2017-05-03 15:10:34 +0100912 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +0100914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 mbedtls_mpi_init(&P1);
916 mbedtls_mpi_init(&Q1);
917 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +0000918
Janos Follathe81102e2017-03-22 13:38:28 +0000919#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +0000921#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 mbedtls_mpi_init(&DP_blind);
923 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +0000924#endif
925
Hanno Becker06811ce2017-05-03 15:10:34 +0100926#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200928#endif
929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 mbedtls_mpi_init(&I);
931 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +0100932
933 /* End of MPI initialization */
934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
936 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200937 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
938 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 }
940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +0100942
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200943 /*
944 * Blinding
945 * T = T * Vi mod N
946 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
948 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
949 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +0000950
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200951 /*
952 * Exponent blinding
953 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
955 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +0000956
Janos Follathf9203b42017-03-22 15:13:15 +0000957#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200958 /*
959 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
960 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
962 f_rng, p_rng));
963 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
964 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
965 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +0000966
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200967 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000968#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200969 /*
970 * DP_blind = ( P - 1 ) * R + DP
971 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
973 f_rng, p_rng));
974 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
975 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
976 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +0000977
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200978 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000979
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200980 /*
981 * DQ_blind = ( Q - 1 ) * R + DQ
982 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
984 f_rng, p_rng));
985 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
986 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
987 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +0000988
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200989 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000990#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100994#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200995 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000996 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000997 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100998 * TP = input ^ dP mod P
999 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1003 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
1005 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001006 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001007 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1009 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1010 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001011
1012 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001013 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1016 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001018
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001019 /*
1020 * Unblind
1021 * T = T * Vf mod N
1022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1024 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
Hanno Becker2dec5e82017-10-03 07:49:52 +01001026 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1028 &ctx->N, &ctx->RN));
1029 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001030 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1031 goto cleanup;
1032 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001033
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
1037cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1040 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1041 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001042#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 mbedtls_mpi_free(&P1);
1045 mbedtls_mpi_free(&Q1);
1046 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001047
Janos Follathe81102e2017-03-22 13:38:28 +00001048#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001050#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 mbedtls_mpi_free(&DP_blind);
1052 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001056
1057#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001059#endif
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 mbedtls_mpi_free(&C);
1062 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if (ret != 0 && ret >= -0x007f) {
1065 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1066 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001069}
1070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001072/**
1073 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1074 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001075 * \param dst buffer to mask
1076 * \param dlen length of destination buffer
1077 * \param src source of the mask generation
1078 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001079 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1082 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001083{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001084 unsigned char counter[4];
1085 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001086 unsigned int hlen;
1087 size_t i, use_len;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001088 unsigned char mask[MBEDTLS_HASH_MAX_SIZE];
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001089#if defined(MBEDTLS_MD_C)
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001090 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001091 const mbedtls_md_info_t *md_info;
1092 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 mbedtls_md_init(&md_ctx);
1095 md_info = mbedtls_md_info_from_type(md_alg);
1096 if (md_info == NULL) {
1097 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1098 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 mbedtls_md_init(&md_ctx);
1101 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001106#else
1107 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 psa_algorithm_t alg = mbedtls_psa_translate_md(md_alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001109 psa_status_t status = PSA_SUCCESS;
1110 size_t out_len;
1111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 hlen = PSA_HASH_LENGTH(alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001113#endif
1114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 memset(mask, 0, sizeof(mask));
1116 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001117
Simon Butcher02037452016-03-01 21:19:12 +00001118 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001119 p = dst;
1120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001122 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001124 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001126
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001127#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 }
1131 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 }
1134 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 }
1137 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 }
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001140#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if ((status = psa_hash_setup(&op, alg)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001142 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 }
1144 if ((status = psa_hash_update(&op, src, slen)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001145 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 }
1147 if ((status = psa_hash_update(&op, counter, 4)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001148 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 }
1150 status = psa_hash_finish(&op, mask, sizeof(mask), &out_len);
1151 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 }
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001154#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001157 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001159
1160 counter[3]++;
1161
1162 dlen -= use_len;
1163 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001164
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001165exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 mbedtls_platform_zeroize(mask, sizeof(mask));
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001167#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 return ret;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001171#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 psa_hash_abort(&op);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001175#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001176}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001177
1178/**
1179 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1180 *
1181 * \param hash the input hash
1182 * \param hlen length of the input hash
1183 * \param salt the input salt
1184 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001185 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001186 * \param md_alg message digest to use
1187 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001188static int hash_mprime(const unsigned char *hash, size_t hlen,
1189 const unsigned char *salt, size_t slen,
1190 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001191{
1192 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001193
1194#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001195 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001196 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1199 if (md_info == NULL) {
1200 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1201 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 mbedtls_md_init(&md_ctx);
1204 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001205 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 }
1207 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001208 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 }
1210 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001211 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 }
1213 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001214 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 }
1216 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001217 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 }
1219 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001222
1223exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 return ret;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001227#else
1228 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 psa_algorithm_t alg = mbedtls_psa_translate_md(md_alg);
1230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1231 size_t out_size = PSA_HASH_LENGTH(alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001232 size_t out_len;
1233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if ((status = psa_hash_setup(&op, alg)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001235 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 }
1237 if ((status = psa_hash_update(&op, zeros, sizeof(zeros))) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001238 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 }
1240 if ((status = psa_hash_update(&op, hash, hlen)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001241 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 }
1243 if ((status = psa_hash_update(&op, salt, slen)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001244 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 }
1246 status = psa_hash_finish(&op, out, out_size, &out_len);
1247 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001248 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 }
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001250
1251exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 psa_hash_abort(&op);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001255#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001256}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001257
1258/**
1259 * Compute a hash.
1260 *
1261 * \param md_alg algorithm to use
1262 * \param input input message to hash
1263 * \param ilen input length
1264 * \param output the output buffer - must be large enough for \p md_alg
1265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001266static int compute_hash(mbedtls_md_type_t md_alg,
1267 const unsigned char *input, size_t ilen,
1268 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001269{
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001270#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001271 const mbedtls_md_info_t *md_info;
1272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 md_info = mbedtls_md_info_from_type(md_alg);
1274 if (md_info == NULL) {
1275 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1276 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001279#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 psa_algorithm_t alg = mbedtls_psa_translate_md(md_alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001281 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 size_t out_size = PSA_HASH_LENGTH(alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001283 size_t out_len;
1284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 status = psa_hash_compute(alg, input, ilen, output, out_size, &out_len);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001288#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001293/*
1294 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1297 int (*f_rng)(void *, unsigned char *, size_t),
1298 void *p_rng,
1299 const unsigned char *label, size_t label_len,
1300 size_t ilen,
1301 const unsigned char *input,
1302 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001303{
1304 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001306 unsigned char *p = output;
1307 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (f_rng == NULL) {
1310 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1311 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1314 if (hlen == 0) {
1315 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1316 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001317
1318 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001319
Simon Butcher02037452016-03-01 21:19:12 +00001320 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1322 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1323 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001326
1327 *p++ = 0;
1328
Simon Butcher02037452016-03-01 21:19:12 +00001329 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1331 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1332 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001333
1334 p += hlen;
1335
Simon Butcher02037452016-03-01 21:19:12 +00001336 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1338 if (ret != 0) {
1339 return ret;
1340 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001341 p += hlen;
1342 p += olen - 2 * hlen - 2 - ilen;
1343 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 if (ilen != 0) {
1345 memcpy(p, input, ilen);
1346 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001347
Simon Butcher02037452016-03-01 21:19:12 +00001348 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1350 ctx->hash_id)) != 0) {
1351 return ret;
1352 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001353
Simon Butcher02037452016-03-01 21:19:12 +00001354 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1356 ctx->hash_id)) != 0) {
1357 return ret;
1358 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001365/*
1366 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001368int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1369 int (*f_rng)(void *, unsigned char *, size_t),
1370 void *p_rng, size_t ilen,
1371 const unsigned char *input,
1372 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001373{
1374 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001376 unsigned char *p = output;
1377
Paul Bakkerb3869132013-02-28 17:21:01 +01001378 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001379
Simon Butcher02037452016-03-01 21:19:12 +00001380 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if (ilen + 11 < ilen || olen < ilen + 11) {
1382 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1383 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001384
1385 nb_pad = olen - 3 - ilen;
1386
1387 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 if (f_rng == NULL) {
1390 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1391 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001392
1393 *p++ = MBEDTLS_RSA_CRYPT;
1394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001396 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001397
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001398 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 ret = f_rng(p_rng, p, 1);
1400 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001401
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001402 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (rng_dl == 0 || ret != 0) {
1404 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1405 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001406
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001407 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001408 }
1409
1410 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (ilen != 0) {
1412 memcpy(p, input, ilen);
1413 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001418
Paul Bakker5121ce52009-01-03 21:22:43 +00001419/*
1420 * Add the message padding, then do an RSA operation
1421 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001422int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1423 int (*f_rng)(void *, unsigned char *, size_t),
1424 void *p_rng,
1425 size_t ilen,
1426 const unsigned char *input,
1427 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001428{
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_PKCS1_V15)
1431 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1433 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001434#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_PKCS1_V21)
1437 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1439 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001440#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001441
1442 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001445}
1446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001448/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001449 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1452 int (*f_rng)(void *, unsigned char *, size_t),
1453 void *p_rng,
1454 const unsigned char *label, size_t label_len,
1455 size_t *olen,
1456 const unsigned char *input,
1457 unsigned char *output,
1458 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001459{
Janos Follath24eed8d2019-11-22 13:21:35 +00001460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001461 size_t ilen, i, pad_len;
1462 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001464 unsigned char lhash[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001465 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001466
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001467 /*
1468 * Parameters sanity checks
1469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1471 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
1474 ilen = ctx->len;
1475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (ilen < 16 || ilen > sizeof(buf)) {
1477 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1478 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1481 if (hlen == 0) {
1482 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1483 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001484
Janos Follathc17cda12016-02-11 11:08:18 +00001485 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (2 * hlen + 2 > ilen) {
1487 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1488 }
Janos Follathc17cda12016-02-11 11:08:18 +00001489
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001490 /*
1491 * RSA operation
1492 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001496 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001498
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001499 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001500 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001501 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001502 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1504 ctx->hash_id)) != 0 ||
1505 /* DB: Apply dbMask to maskedDB */
1506 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1507 ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001508 goto cleanup;
1509 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001510
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001511 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1513 label, label_len, lhash);
1514 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001515 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001517
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001518 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001519 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001520 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001522 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001523
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001524 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001525
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001526 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001527
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001528 /* Check lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 for (i = 0; i < hlen; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001530 bad |= lhash[i] ^ *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001532
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001533 /* Get zero-padding len, but always read till end of buffer
1534 * (minus one, for the 01 byte) */
1535 pad_len = 0;
1536 pad_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001538 pad_done |= p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001540 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001541
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001542 p += pad_len;
1543 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001544
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001545 /*
1546 * The only information "leaked" is whether the padding was correct or not
1547 * (eg, no data is copied if it was not correct). This meets the
1548 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1549 * the different error conditions.
1550 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001552 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1553 goto cleanup;
1554 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001557 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1558 goto cleanup;
1559 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001560
1561 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (*olen != 0) {
1563 memcpy(output, p, *olen);
1564 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001565 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001566
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001567cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 mbedtls_platform_zeroize(buf, sizeof(buf));
1569 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001576/*
1577 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1578 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1580 int (*f_rng)(void *, unsigned char *, size_t),
1581 void *p_rng,
1582 size_t *olen,
1583 const unsigned char *input,
1584 unsigned char *output,
1585 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001586{
1587 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1588 size_t ilen;
1589 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1590
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001591 ilen = ctx->len;
1592
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1594 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1595 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 if (ilen < 16 || ilen > sizeof(buf)) {
1598 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1599 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001604 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1608 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001609
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001610cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001612
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001614}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
1617/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001618 * Do an RSA operation, then remove the message padding
1619 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001620int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1621 int (*f_rng)(void *, unsigned char *, size_t),
1622 void *p_rng,
1623 size_t *olen,
1624 const unsigned char *input,
1625 unsigned char *output,
1626 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001627{
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#if defined(MBEDTLS_PKCS1_V15)
1630 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1632 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001633#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_PKCS1_V21)
1636 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1638 olen, input, output,
1639 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001640#endif
1641
1642 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001644 }
1645}
1646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001648static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1649 int (*f_rng)(void *, unsigned char *, size_t),
1650 void *p_rng,
1651 mbedtls_md_type_t md_alg,
1652 unsigned int hashlen,
1653 const unsigned char *hash,
1654 int saltlen,
1655 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001656{
1657 size_t olen;
1658 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001659 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001660 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001661 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001662 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001665 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1669 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1670 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 if (f_rng == NULL) {
1673 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1674 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001675
1676 olen = ctx->len;
1677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001679 /* Gather length of hash to sign */
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 size_t exp_hashlen = mbedtls_hash_info_get_size(md_alg);
1681 if (exp_hashlen == 0) {
1682 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1683 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 if (hashlen != exp_hashlen) {
1686 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1687 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001688 }
1689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1691 if (hlen == 0) {
1692 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1693 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1696 /* Calculate the largest possible salt length, up to the hash size.
1697 * Normally this is the hash length, which is the maximum salt length
1698 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1699 * enough room, use the maximum salt length that fits. The constraint is
1700 * that the hash length plus the salt length plus 2 bytes must be at most
1701 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1702 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001703 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 if (olen < hlen + min_slen + 2) {
1705 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1706 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001707 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001709 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 }
1711 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1712 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1713 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001714 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001715 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001718
Simon Butcher02037452016-03-01 21:19:12 +00001719 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001721 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001722 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001723
1724 /* Generate salt of length slen in place in the encoded message */
1725 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1727 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1728 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001729
Paul Bakkerb3869132013-02-28 17:21:01 +01001730 p += slen;
1731
Simon Butcher02037452016-03-01 21:19:12 +00001732 /* Generate H = Hash( M' ) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 ret = hash_mprime(hash, hashlen, salt, slen, p, ctx->hash_id);
1734 if (ret != 0) {
1735 return ret;
1736 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001737
Simon Butcher02037452016-03-01 21:19:12 +00001738 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001740 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001742
Simon Butcher02037452016-03-01 21:19:12 +00001743 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
1745 ctx->hash_id);
1746 if (ret != 0) {
1747 return ret;
1748 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1751 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001752
1753 p += hlen;
1754 *p++ = 0xBC;
1755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001757}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001758
1759/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001760 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1761 * the option to pass in the salt length.
1762 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001763int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1764 int (*f_rng)(void *, unsigned char *, size_t),
1765 void *p_rng,
1766 mbedtls_md_type_t md_alg,
1767 unsigned int hashlen,
1768 const unsigned char *hash,
1769 int saltlen,
1770 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001771{
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1773 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001774}
1775
1776
1777/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001778 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1779 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001780int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1781 int (*f_rng)(void *, unsigned char *, size_t),
1782 void *p_rng,
1783 mbedtls_md_type_t md_alg,
1784 unsigned int hashlen,
1785 const unsigned char *hash,
1786 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001787{
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1789 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001790}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001794/*
1795 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1796 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001797
1798/* Construct a PKCS v1.5 encoding of a hashed message
1799 *
1800 * This is used both for signature generation and verification.
1801 *
1802 * Parameters:
1803 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001804 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001805 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001806 * - hash: Buffer containing the hashed message or the raw data.
1807 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001808 * - dst: Buffer to hold the encoded message.
1809 *
1810 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001811 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001812 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001813 *
1814 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001815static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1816 unsigned int hashlen,
1817 const unsigned char *hash,
1818 size_t dst_len,
1819 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001820{
1821 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001822 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001823 unsigned char *p = dst;
1824 const char *oid = NULL;
1825
1826 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if (md_alg != MBEDTLS_MD_NONE) {
1828 unsigned char md_size = mbedtls_hash_info_get_size(md_alg);
1829 if (md_size == 0) {
1830 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1831 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1834 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1835 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 if (hashlen != md_size) {
1838 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1839 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001840
1841 /* Double-check that 8 + hashlen + oid_size can be used as a
1842 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001844 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 10 + hashlen + oid_size < 10 + hashlen) {
1846 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1847 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001848
1849 /*
1850 * Static bounds check:
1851 * - Need 10 bytes for five tag-length pairs.
1852 * (Insist on 1-byte length encodings to protect against variants of
1853 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1854 * - Need hashlen bytes for hash
1855 * - Need oid_size bytes for hash alg OID.
1856 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 if (nb_pad < 10 + hashlen + oid_size) {
1858 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1859 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001860 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 } else {
1862 if (nb_pad < hashlen) {
1863 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1864 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001865
1866 nb_pad -= hashlen;
1867 }
1868
Hanno Becker2b2f8982017-09-27 17:10:03 +01001869 /* Need space for signature header and padding delimiter (3 bytes),
1870 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (nb_pad < 3 + 8) {
1872 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1873 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001874 nb_pad -= 3;
1875
1876 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001877 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001878
1879 /* Write signature header and padding */
1880 *p++ = 0;
1881 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001883 p += nb_pad;
1884 *p++ = 0;
1885
1886 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 if (md_alg == MBEDTLS_MD_NONE) {
1888 memcpy(p, hash, hashlen);
1889 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001890 }
1891
1892 /* Signing hashed data, add corresponding ASN.1 structure
1893 *
1894 * DigestInfo ::= SEQUENCE {
1895 * digestAlgorithm DigestAlgorithmIdentifier,
1896 * digest Digest }
1897 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1898 * Digest ::= OCTET STRING
1899 *
1900 * Schematic:
1901 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1902 * TAG-NULL + LEN [ NULL ] ]
1903 * TAG-OCTET + LEN [ HASH ] ]
1904 */
1905 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001907 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001909 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001910 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001912 p += oid_size;
1913 *p++ = MBEDTLS_ASN1_NULL;
1914 *p++ = 0x00;
1915 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001916 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001918 p += hashlen;
1919
1920 /* Just a sanity-check, should be automatic
1921 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 if (p != dst + dst_len) {
1923 mbedtls_platform_zeroize(dst, dst_len);
1924 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001925 }
1926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001928}
1929
Paul Bakkerb3869132013-02-28 17:21:01 +01001930/*
1931 * Do an RSA operation to sign the message digest
1932 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001933int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
1934 int (*f_rng)(void *, unsigned char *, size_t),
1935 void *p_rng,
1936 mbedtls_md_type_t md_alg,
1937 unsigned int hashlen,
1938 const unsigned char *hash,
1939 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001940{
Janos Follath24eed8d2019-11-22 13:21:35 +00001941 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001942 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001945 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1949 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1950 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001951
Hanno Beckerfdf38032017-09-06 12:35:55 +01001952 /*
1953 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1954 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
1957 ctx->len, sig)) != 0) {
1958 return ret;
1959 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001960
Hanno Beckerfdf38032017-09-06 12:35:55 +01001961 /* Private key operation
1962 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001963 * In order to prevent Lenstra's attack, make the signature in a
1964 * temporary buffer and check it before returning it.
1965 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 sig_try = mbedtls_calloc(1, ctx->len);
1968 if (sig_try == NULL) {
1969 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00001970 }
1971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 verif = mbedtls_calloc(1, ctx->len);
1973 if (verif == NULL) {
1974 mbedtls_free(sig_try);
1975 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
1976 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
1979 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
1980
1981 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001982 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1983 goto cleanup;
1984 }
1985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001987
1988cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 mbedtls_platform_zeroize(sig_try, ctx->len);
1990 mbedtls_platform_zeroize(verif, ctx->len);
1991 mbedtls_free(sig_try);
1992 mbedtls_free(verif);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 if (ret != 0) {
1995 memset(sig, '!', ctx->len);
1996 }
1997 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001998}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002000
2001/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 * Do an RSA operation to sign the message digest
2003 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002004int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2005 int (*f_rng)(void *, unsigned char *, size_t),
2006 void *p_rng,
2007 mbedtls_md_type_t md_alg,
2008 unsigned int hashlen,
2009 const unsigned char *hash,
2010 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002011{
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002013 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002015
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017#if defined(MBEDTLS_PKCS1_V15)
2018 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2020 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002021#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023#if defined(MBEDTLS_PKCS1_V21)
2024 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2026 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002027#endif
2028
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002032}
2033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002035/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002036 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2039 mbedtls_md_type_t md_alg,
2040 unsigned int hashlen,
2041 const unsigned char *hash,
2042 mbedtls_md_type_t mgf1_hash_id,
2043 int expected_salt_len,
2044 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002045{
Janos Follath24eed8d2019-11-22 13:21:35 +00002046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002047 size_t siglen;
2048 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002049 unsigned char *hash_start;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002050 unsigned char result[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002051 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002052 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002056 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002058
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 siglen = ctx->len;
2060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (siglen < 16 || siglen > sizeof(buf)) {
2062 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2063 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (ret != 0) {
2068 return ret;
2069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
2071 p = buf;
2072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 if (buf[siglen - 1] != 0xBC) {
2074 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002075 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (md_alg != MBEDTLS_MD_NONE) {
2078 /* Gather length of hash to sign */
2079 size_t exp_hashlen = mbedtls_hash_info_get_size(md_alg);
2080 if (exp_hashlen == 0) {
2081 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2082 }
2083
2084 if (hashlen != exp_hashlen) {
2085 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2086 }
2087 }
2088
2089 hlen = mbedtls_hash_info_get_size(mgf1_hash_id);
2090 if (hlen == 0) {
2091 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2092 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002093
Simon Butcher02037452016-03-01 21:19:12 +00002094 /*
2095 * Note: EMSA-PSS verification is over the length of N - 1 bits
2096 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (buf[0] >> (8 - siglen * 8 + msb)) {
2100 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2101 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002102
Simon Butcher02037452016-03-01 21:19:12 +00002103 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002105 p++;
2106 siglen -= 1;
2107 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if (siglen < hlen + 2) {
2110 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2111 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002112 hash_start = p + siglen - hlen - 1;
2113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2115 if (ret != 0) {
2116 return ret;
2117 }
Paul Bakker02303e82013-01-03 11:08:31 +01002118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002120
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002122 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (*p++ != 0x01) {
2126 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2127 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002128
Gilles Peskine6a54b022017-10-17 19:02:13 +02002129 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2132 observed_salt_len != (size_t) expected_salt_len) {
2133 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002134 }
2135
Simon Butcher02037452016-03-01 21:19:12 +00002136 /*
2137 * Generate H = Hash( M' )
2138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2140 result, mgf1_hash_id);
2141 if (ret != 0) {
2142 return ret;
2143 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (memcmp(hash_start, result, hlen) != 0) {
2146 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2147 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002150}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002151
2152/*
2153 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2154 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002155int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2156 mbedtls_md_type_t md_alg,
2157 unsigned int hashlen,
2158 const unsigned char *hash,
2159 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002160{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002161 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002163 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002168 : md_alg;
2169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2171 md_alg, hashlen, hash,
2172 mgf1_hash_id,
2173 MBEDTLS_RSA_SALT_LEN_ANY,
2174 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002175
2176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002180/*
2181 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2182 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002183int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2184 mbedtls_md_type_t md_alg,
2185 unsigned int hashlen,
2186 const unsigned char *hash,
2187 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002188{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002189 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002190 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002191 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002194 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002196
2197 sig_len = ctx->len;
2198
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002199 /*
2200 * Prepare expected PKCS1 v1.5 encoding of hash.
2201 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2204 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002205 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2206 goto cleanup;
2207 }
2208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2210 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002211 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002213
2214 /*
2215 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2216 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 ret = mbedtls_rsa_public(ctx, sig, encoded);
2219 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002220 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002222
Simon Butcher02037452016-03-01 21:19:12 +00002223 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002224 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002225 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2228 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002229 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2230 goto cleanup;
2231 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002232
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002233cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 if (encoded != NULL) {
2236 mbedtls_platform_zeroize(encoded, sig_len);
2237 mbedtls_free(encoded);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002238 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002239
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 if (encoded_expected != NULL) {
2241 mbedtls_platform_zeroize(encoded_expected, sig_len);
2242 mbedtls_free(encoded_expected);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002243 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002246}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
2249/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002250 * Do an RSA operation and check the message digest
2251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002252int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2253 mbedtls_md_type_t md_alg,
2254 unsigned int hashlen,
2255 const unsigned char *hash,
2256 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002257{
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002259 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263#if defined(MBEDTLS_PKCS1_V15)
2264 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2266 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002267#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#if defined(MBEDTLS_PKCS1_V21)
2270 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2272 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002273#endif
2274
2275 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002277 }
2278}
2279
2280/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002281 * Copy the components of an RSA key
2282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002283int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002284{
Janos Follath24eed8d2019-11-22 13:21:35 +00002285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002286
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002287 dst->len = src->len;
2288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2290 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2293 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2294 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002295
2296#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2298 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2299 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2300 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2301 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002302#endif
2303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2307 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002308
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002309 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002310 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002311
2312cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 if (ret != 0) {
2314 mbedtls_rsa_free(dst);
2315 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002318}
2319
2320/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 * Free the components of an RSA key
2322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002323void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002324{
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002326 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 mbedtls_mpi_free(&ctx->Vi);
2330 mbedtls_mpi_free(&ctx->Vf);
2331 mbedtls_mpi_free(&ctx->RN);
2332 mbedtls_mpi_free(&ctx->D);
2333 mbedtls_mpi_free(&ctx->Q);
2334 mbedtls_mpi_free(&ctx->P);
2335 mbedtls_mpi_free(&ctx->E);
2336 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002337
Hanno Becker33c30a02017-08-23 07:00:22 +01002338#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 mbedtls_mpi_free(&ctx->RQ);
2340 mbedtls_mpi_free(&ctx->RP);
2341 mbedtls_mpi_free(&ctx->QP);
2342 mbedtls_mpi_free(&ctx->DQ);
2343 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002344#endif /* MBEDTLS_RSA_NO_CRT */
2345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002347 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 if (ctx->ver != 0) {
2349 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002350 ctx->ver = 0;
2351 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002352#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002353}
2354
Hanno Beckerab377312017-08-23 16:24:51 +01002355#endif /* !MBEDTLS_RSA_ALT */
2356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002358
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002359#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
2361/*
2362 * Example RSA-1024 keypair, for test purposes
2363 */
2364#define KEY_LEN 128
2365
2366#define RSA_N "9292758453063D803DD603D5E777D788" \
2367 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2368 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2369 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2370 "93A89813FBF3C4F8066D2D800F7C38A8" \
2371 "1AE31942917403FF4946B0A83D3D3E05" \
2372 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2373 "5E94BB77B07507233A0BC7BAC8F90F79"
2374
2375#define RSA_E "10001"
2376
2377#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2378 "66CA472BC44D253102F8B4A9D3BFA750" \
2379 "91386C0077937FE33FA3252D28855837" \
2380 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2381 "DF79C5CE07EE72C7F123142198164234" \
2382 "CABB724CF78B8173B9F880FC86322407" \
2383 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2384 "071513A1E85B5DFA031F21ECAE91A34D"
2385
2386#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2387 "2C01CAD19EA484A87EA4377637E75500" \
2388 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2389 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2390
2391#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2392 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2393 "910E4168387E3C30AA1E00C339A79508" \
2394 "8452DD96A9A5EA5D9DCA68DA636032AF"
2395
Paul Bakker5121ce52009-01-03 21:22:43 +00002396#define PT_LEN 24
2397#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2398 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002401static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002402{
gufe44c2620da2020-08-03 17:56:50 +02002403#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002404 size_t i;
2405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002407 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 }
Paul Bakker545570e2010-07-18 09:00:25 +00002409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002411 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002413#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002415 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002419#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002424
Paul Bakker5121ce52009-01-03 21:22:43 +00002425/*
2426 * Checkup routine
2427 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002428int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002429{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002430 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002432 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 unsigned char rsa_plaintext[PT_LEN];
2435 unsigned char rsa_decrypted[PT_LEN];
2436 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002438 unsigned char sha1sum[20];
2439#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
Hanno Becker3a701162017-08-22 13:52:43 +01002441 mbedtls_mpi K;
2442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 mbedtls_mpi_init(&K);
2444 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2447 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2448 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2449 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2450 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2451 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2452 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2453 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2454 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2455 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 if (verbose != 0) {
2460 mbedtls_printf(" RSA key validation: ");
2461 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2464 mbedtls_rsa_check_privkey(&rsa) != 0) {
2465 if (verbose != 0) {
2466 mbedtls_printf("failed\n");
2467 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Hanno Becker5bc87292017-05-03 15:09:31 +01002469 ret = 1;
2470 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 }
2472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 if (verbose != 0) {
2474 mbedtls_printf("passed\n PKCS#1 encryption : ");
2475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2480 PT_LEN, rsa_plaintext,
2481 rsa_ciphertext) != 0) {
2482 if (verbose != 0) {
2483 mbedtls_printf("failed\n");
2484 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Hanno Becker5bc87292017-05-03 15:09:31 +01002486 ret = 1;
2487 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 }
2489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 if (verbose != 0) {
2491 mbedtls_printf("passed\n PKCS#1 decryption : ");
2492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2495 &len, rsa_ciphertext, rsa_decrypted,
2496 sizeof(rsa_decrypted)) != 0) {
2497 if (verbose != 0) {
2498 mbedtls_printf("failed\n");
2499 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Hanno Becker5bc87292017-05-03 15:09:31 +01002501 ret = 1;
2502 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 }
2504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2506 if (verbose != 0) {
2507 mbedtls_printf("failed\n");
2508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Hanno Becker5bc87292017-05-03 15:09:31 +01002510 ret = 1;
2511 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 }
2513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if (verbose != 0) {
2515 mbedtls_printf("passed\n");
2516 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 if (verbose != 0) {
2520 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002521 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (mbedtls_sha1(rsa_plaintext, PT_LEN, sha1sum) != 0) {
2524 if (verbose != 0) {
2525 mbedtls_printf("failed\n");
2526 }
2527
2528 return 1;
2529 }
2530
2531 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2532 MBEDTLS_MD_SHA1, 20,
2533 sha1sum, rsa_ciphertext) != 0) {
2534 if (verbose != 0) {
2535 mbedtls_printf("failed\n");
2536 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
Hanno Becker5bc87292017-05-03 15:09:31 +01002538 ret = 1;
2539 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 }
2541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 if (verbose != 0) {
2543 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2544 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2547 sha1sum, rsa_ciphertext) != 0) {
2548 if (verbose != 0) {
2549 mbedtls_printf("failed\n");
2550 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002551
Hanno Becker5bc87292017-05-03 15:09:31 +01002552 ret = 1;
2553 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (verbose != 0) {
2557 mbedtls_printf("passed\n");
2558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 if (verbose != 0) {
2562 mbedtls_printf("\n");
2563 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002564
Paul Bakker3d8fb632014-04-17 12:42:41 +02002565cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 mbedtls_mpi_free(&K);
2567 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002569 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002572}
2573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#endif /* MBEDTLS_RSA_C */