blob: 01d0eb09d2e28c0a8fa4c8c7edf9a4de3b615e72 [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"
Hanno Beckera565f542017-10-11 11:00:19 +010043#include "mbedtls/rsa_internal.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 Mezeic0ae1cf2021-10-20 12:09:35 +020047#include "constant_time_internal.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020048#include "mbedtls/constant_time.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
gufe44c2620da2020-08-03 17:56:50 +020056#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010061
Hanno Beckera565f542017-10-11 11:00:19 +010062#if !defined(MBEDTLS_RSA_ALT)
63
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050064/* Parameter validation macros */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065#define RSA_VALIDATE_RET(cond) \
66 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA)
67#define RSA_VALIDATE(cond) \
68 MBEDTLS_INTERNAL_VALIDATE(cond)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +010077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
79 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
80 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
81 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
82 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
83 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +010084 }
85
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 if (N != NULL) {
87 ctx->len = mbedtls_mpi_size(&ctx->N);
88 }
Hanno Becker617c1ae2017-08-23 14:11:24 +010089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +010091}
92
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
94 unsigned char const *N, size_t N_len,
95 unsigned char const *P, size_t P_len,
96 unsigned char const *Q, size_t Q_len,
97 unsigned char const *D, size_t D_len,
98 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +010099{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000100 int ret = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 if (N != NULL) {
104 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
105 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100106 }
107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 if (P != NULL) {
109 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
110 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 if (Q != NULL) {
113 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
114 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 if (D != NULL) {
117 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
118 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 if (E != NULL) {
121 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
122 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100123
124cleanup:
125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 if (ret != 0) {
127 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
128 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100131}
132
Hanno Becker705fc682017-10-10 17:57:02 +0100133/*
134 * Checks whether the context fields are set in such a way
135 * that the RSA primitives will be able to execute without error.
136 * It does *not* make guarantees for consistency of the parameters.
137 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
139 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100140{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100141#if !defined(MBEDTLS_RSA_NO_CRT)
142 /* blinding_needed is only used for NO_CRT to decide whether
143 * P,Q need to be present or not. */
144 ((void) blinding_needed);
145#endif
146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
148 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
149 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000150 }
Hanno Becker705fc682017-10-10 17:57:02 +0100151
152 /*
153 * 1. Modular exponentiation needs positive, odd moduli.
154 */
155
156 /* Modular exponentiation wrt. N is always used for
157 * RSA public key operations. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
159 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
160 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100161 }
162
163#if !defined(MBEDTLS_RSA_NO_CRT)
164 /* Modular exponentiation for P and Q is only
165 * used for private key operations and if CRT
166 * is used. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 if (is_priv &&
168 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
169 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
170 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
171 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
172 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100173 }
174#endif /* !MBEDTLS_RSA_NO_CRT */
175
176 /*
177 * 2. Exponents must be positive
178 */
179
180 /* Always need E for public key operations */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
182 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
183 }
Hanno Becker705fc682017-10-10 17:57:02 +0100184
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100185#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100186 /* For private key operations, use D or DP & DQ
187 * as (unblinded) exponents. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
189 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
190 }
Hanno Becker705fc682017-10-10 17:57:02 +0100191#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 if (is_priv &&
193 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
194 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
195 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100196 }
197#endif /* MBEDTLS_RSA_NO_CRT */
198
199 /* Blinding shouldn't make exponents negative either,
200 * so check that P, Q >= 1 if that hasn't yet been
201 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100202#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 if (is_priv && blinding_needed &&
204 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
205 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
206 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100207 }
208#endif
209
210 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100211 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100212#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 if (is_priv &&
214 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
215 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100216 }
217#endif
218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100220}
221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100223{
224 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500225 int have_N, have_P, have_Q, have_D, have_E;
226#if !defined(MBEDTLS_RSA_NO_CRT)
227 int have_DP, have_DQ, have_QP;
228#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500229 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 RSA_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
234 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
235 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
236 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
237 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500238
239#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
241 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
242 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500243#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100244
Hanno Becker617c1ae2017-08-23 14:11:24 +0100245 /*
246 * Check whether provided parameters are enough
247 * to deduce all others. The following incomplete
248 * parameter sets for private keys are supported:
249 *
250 * (1) P, Q missing.
251 * (2) D and potentially N missing.
252 *
253 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100254
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 n_missing = have_P && have_Q && have_D && have_E;
256 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
257 d_missing = have_P && have_Q && !have_D && have_E;
258 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100259
260 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500261 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 if (!is_priv && !is_pub) {
264 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
265 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100266
267 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100268 * Step 1: Deduce N if P, Q are provided.
269 */
270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 if (!have_N && have_P && have_Q) {
272 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
273 &ctx->Q)) != 0) {
274 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100275 }
276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100278 }
279
280 /*
281 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100282 */
283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 if (pq_missing) {
285 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
286 &ctx->P, &ctx->Q);
287 if (ret != 0) {
288 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
289 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 } else if (d_missing) {
292 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
293 &ctx->Q,
294 &ctx->E,
295 &ctx->D)) != 0) {
296 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100297 }
298 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100299
Hanno Becker617c1ae2017-08-23 14:11:24 +0100300 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100301 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100302 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100303 */
304
Hanno Becker23344b52017-08-23 07:43:27 +0100305#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 if (is_priv && !(have_DP && have_DQ && have_QP)) {
307 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
308 &ctx->DP, &ctx->DQ, &ctx->QP);
309 if (ret != 0) {
310 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
311 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100312 }
Hanno Becker23344b52017-08-23 07:43:27 +0100313#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100314
315 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100316 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100317 */
318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100320}
321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
323 unsigned char *N, size_t N_len,
324 unsigned char *P, size_t P_len,
325 unsigned char *Q, size_t Q_len,
326 unsigned char *D, size_t D_len,
327 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100328{
329 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500330 int is_priv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100332
333 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500334 is_priv =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
336 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
337 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
338 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
339 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100342 /* If we're trying to export private parameters for a public key,
343 * something must be wrong. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 if (P != NULL || Q != NULL || D != NULL) {
345 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
346 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100347
348 }
349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 if (N != NULL) {
351 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
352 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 if (P != NULL) {
355 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
356 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 if (Q != NULL) {
359 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
360 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 if (D != NULL) {
363 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
364 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 if (E != NULL) {
367 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
368 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100369
370cleanup:
371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100373}
374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
376 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
377 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100378{
Janos Follath24eed8d2019-11-22 13:21:35 +0000379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500380 int is_priv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100381 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100382
383 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500384 is_priv =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
386 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
387 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
388 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
389 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100392 /* If we're trying to export private parameters for a public key,
393 * something must be wrong. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100394 if (P != NULL || Q != NULL || D != NULL) {
395 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
396 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100397
398 }
399
400 /* Export all requested core parameters. */
401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
403 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
404 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
405 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
406 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
407 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100408 }
409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100411}
412
413/*
414 * Export CRT parameters
415 * This must also be implemented if CRT is not used, for being able to
416 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
417 * can be used in this case.
418 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
420 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100421{
Janos Follath24eed8d2019-11-22 13:21:35 +0000422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500423 int is_priv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100424 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100425
426 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500427 is_priv =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
429 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
430 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
431 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
432 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 if (!is_priv) {
435 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
436 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100437
Hanno Beckerdc95c892017-08-23 06:57:02 +0100438#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100439 /* Export all requested blinding parameters. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
441 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
442 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
443 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100445#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
447 DP, DQ, QP)) != 0) {
448 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100449 }
450#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100453}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100454
Paul Bakker5121ce52009-01-03 21:22:43 +0000455/*
456 * Initialize an RSA context
457 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100458void mbedtls_rsa_init(mbedtls_rsa_context *ctx,
459 int padding,
460 int hash_id)
Paul Bakker5121ce52009-01-03 21:22:43 +0000461{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462 RSA_VALIDATE(ctx != NULL);
463 RSA_VALIDATE(padding == MBEDTLS_RSA_PKCS_V15 ||
464 padding == MBEDTLS_RSA_PKCS_V21);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 mbedtls_rsa_set_padding(ctx, padding, hash_id);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100471 /* Set ctx->ver to nonzero to indicate that the mutex has been
472 * initialized and will need to be freed. */
473 ctx->ver = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200475#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000476}
477
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100478/*
479 * Set padding for an existing RSA context
480 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100481void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
482 int hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100483{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 RSA_VALIDATE(ctx != NULL);
485 RSA_VALIDATE(padding == MBEDTLS_RSA_PKCS_V15 ||
486 padding == MBEDTLS_RSA_PKCS_V21);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500487
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100488 ctx->padding = padding;
489 ctx->hash_id = hash_id;
490}
491
Hanno Becker617c1ae2017-08-23 14:11:24 +0100492/*
493 * Get length in bytes of RSA modulus
494 */
495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100496size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100497{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100499}
500
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504/*
505 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800506 *
507 * This generation method follows the RSA key pair generation procedure of
508 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
511 int (*f_rng)(void *, unsigned char *, size_t),
512 void *p_rng,
513 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000514{
Janos Follath24eed8d2019-11-22 13:21:35 +0000515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800516 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100517 int prime_quality = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100518 RSA_VALIDATE_RET(ctx != NULL);
519 RSA_VALIDATE_RET(f_rng != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
Janos Follathb8fc1b02018-09-03 15:37:01 +0100521 /*
522 * If the modulus is 1024 bit long or shorter, then the security strength of
523 * the RSA algorithm is less than or equal to 80 bits and therefore an error
524 * rate of 2^-80 is sufficient.
525 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100527 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 mbedtls_mpi_init(&H);
531 mbedtls_mpi_init(&G);
532 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 if (nbits < 128 || exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100535 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
536 goto cleanup;
537 }
538
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 /*
540 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800541 * 1. |P-Q| > 2^( nbits / 2 - 100 )
542 * 2. GCD( E, (P-1)*(Q-1) ) == 1
543 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547 do {
548 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
549 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100551 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
552 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800554 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
556 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100558 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800560 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100561 if (H.s < 0) {
562 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
563 }
Janos Follathef441782016-09-21 13:18:12 +0100564
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100565 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100566 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
567 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
568 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800569
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800570 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
572 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800573 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800575
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800576 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100577 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
578 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
579 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100581 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 -0800582 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100583 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800584
585 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100588 /* Restore P,Q */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100589 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
590 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100592 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100594 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100595
Jethro Beekman97f95c92018-02-13 15:50:36 -0800596#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 * DP = D mod (P - 1)
599 * DQ = D mod (Q - 1)
600 * QP = Q^-1 mod P
601 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100602 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
603 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100604#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
Hanno Becker83aad1f2017-08-23 06:45:10 +0100606 /* Double-check */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100607 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
609cleanup:
610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100611 mbedtls_mpi_free(&H);
612 mbedtls_mpi_free(&G);
613 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100615 if (ret != 0) {
616 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100618 if ((-ret & ~0x7f) == 0) {
619 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
620 }
621 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 }
623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000625}
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
629/*
630 * Check a public RSA key
631 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100632int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000633{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100634 RSA_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
637 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100638 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
641 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100642 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
645 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
646 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
647 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
648 }
649
650 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000651}
652
653/*
Hanno Becker705fc682017-10-10 17:57:02 +0100654 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100656int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000657{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100658 RSA_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
661 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
662 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 }
Paul Bakker48377d92013-08-30 12:06:24 +0200664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
666 &ctx->D, &ctx->E, NULL, NULL) != 0) {
667 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000669
Hanno Beckerb269a852017-08-25 08:03:21 +0100670#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100671 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
672 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
673 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100674 }
675#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100677 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000678}
679
680/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100681 * Check if contexts holding a public and private key match
682 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
684 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100685{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100686 RSA_VALIDATE_RET(pub != NULL);
687 RSA_VALIDATE_RET(prv != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
690 mbedtls_rsa_check_privkey(prv) != 0) {
691 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100692 }
693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100694 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
695 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
696 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100697 }
698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100700}
701
702/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000703 * Do an RSA public key operation
704 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
706 const unsigned char *input,
707 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000708{
Janos Follath24eed8d2019-11-22 13:21:35 +0000709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000710 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_mpi T;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100712 RSA_VALIDATE_RET(ctx != NULL);
713 RSA_VALIDATE_RET(input != NULL);
714 RSA_VALIDATE_RET(output != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100716 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
717 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
718 }
Hanno Becker705fc682017-10-10 17:57:02 +0100719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100720 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000721
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200722#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
724 return ret;
725 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200726#endif
727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100728 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100730 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200731 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
732 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 }
734
735 olen = ctx->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100736 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
737 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000738
739cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100741 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
742 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
743 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100744#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100746 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100748 if (ret != 0) {
749 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
750 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100752 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000753}
754
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200755/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200756 * Generate or update blinding values, see section 10 of:
757 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200758 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200759 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200760 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100761static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
762 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200763{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200764 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200765 mbedtls_mpi R;
766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100767 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100769 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200770 /* We already have blinding values, just update them by squaring */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100771 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
772 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
773 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
774 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200775
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200776 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200777 }
778
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200779 /* Unblinding value: Vf = random number, invertible mod N */
780 do {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100781 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200782 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
783 goto cleanup;
784 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100786 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 +0200787
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200788 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100789 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
790 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
791 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200792
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200793 /* At this point, Vi is invertible mod N if and only if both Vf and R
794 * are invertible mod N. If one of them isn't, we don't need to know
795 * which one, we just loop and choose new values for both of them.
796 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100797 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
798 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200799 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100800 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100802 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500803
804 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100805 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
806 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200807
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200808 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200809 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100810 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 +0200811
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200812
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200813cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100814 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100816 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200817}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200818
Paul Bakker5121ce52009-01-03 21:22:43 +0000819/*
Janos Follathe81102e2017-03-22 13:38:28 +0000820 * Exponent blinding supposed to prevent side-channel attacks using multiple
821 * traces of measurements to recover the RSA key. The more collisions are there,
822 * the more bits of the key can be recovered. See [3].
823 *
824 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case0e7791f2021-12-20 21:14:10 -0800825 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000826 *
827 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case0e7791f2021-12-20 21:14:10 -0800828 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000829 *
830 * (With the currently (as of 2017 April) known best algorithms breaking 2048
831 * bit RSA requires approximately as much time as trying out 2^112 random keys.
832 * Thus in this sense with 28 byte blinding the security is not reduced by
833 * side-channel attacks like the one in [3])
834 *
835 * This countermeasure does not help if the key recovery is possible with a
836 * single trace.
837 */
838#define RSA_EXPONENT_BLINDING 28
839
840/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 * Do an RSA private key operation
842 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100843int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
844 int (*f_rng)(void *, unsigned char *, size_t),
845 void *p_rng,
846 const unsigned char *input,
847 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000848{
Janos Follath24eed8d2019-11-22 13:21:35 +0000849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000850 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100851
852 /* Temporary holding the result */
853 mbedtls_mpi T;
854
855 /* Temporaries holding P-1, Q-1 and the
856 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000857 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100858
859#if !defined(MBEDTLS_RSA_NO_CRT)
860 /* Temporaries holding the results mod p resp. mod q. */
861 mbedtls_mpi TP, TQ;
862
863 /* Temporaries holding the blinded exponents for
864 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000865 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100866
867 /* Pointers to actual exponents to be used - either the unblinded
868 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000869 mbedtls_mpi *DP = &ctx->DP;
870 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100871#else
872 /* Temporary holding the blinded exponent (if used). */
873 mbedtls_mpi D_blind;
874
875 /* Pointer to actual exponent to be used - either the unblinded
876 * or the blinded one, depending on the presence of a PRNG. */
877 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100878#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100879
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100880 /* Temporaries holding the initial input and the double
881 * checked result; should be the same in the end. */
882 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100884 RSA_VALIDATE_RET(ctx != NULL);
885 RSA_VALIDATE_RET(input != NULL);
886 RSA_VALIDATE_RET(output != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888 if (rsa_check_context(ctx, 1 /* private key checks */,
889 f_rng != NULL /* blinding y/n */) != 0) {
890 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +0100891 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100892
Hanno Becker06811ce2017-05-03 15:10:34 +0100893#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
895 return ret;
896 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100897#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000898
Hanno Becker06811ce2017-05-03 15:10:34 +0100899 /* MPI Initialization */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100900 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +0100901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100902 mbedtls_mpi_init(&P1);
903 mbedtls_mpi_init(&Q1);
904 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +0000905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100906 if (f_rng != NULL) {
Janos Follathe81102e2017-03-22 13:38:28 +0000907#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100908 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +0000909#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100910 mbedtls_mpi_init(&DP_blind);
911 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +0000912#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000913 }
Janos Follathe81102e2017-03-22 13:38:28 +0000914
Hanno Becker06811ce2017-05-03 15:10:34 +0100915#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100916 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200917#endif
918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100919 mbedtls_mpi_init(&I);
920 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +0100921
922 /* End of MPI initialization */
923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100924 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
925 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200926 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
927 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 }
929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +0100931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100932 if (f_rng != NULL) {
Paul Bakkerf451bac2013-08-30 15:37:02 +0200933 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200934 * Blinding
935 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200936 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100937 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
938 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
939 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +0000940
Janos Follathe81102e2017-03-22 13:38:28 +0000941 /*
942 * Exponent blinding
943 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100944 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
945 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +0000946
Janos Follathf9203b42017-03-22 15:13:15 +0000947#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000948 /*
949 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
950 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100951 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
952 f_rng, p_rng));
953 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
954 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
955 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +0000956
957 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000958#else
959 /*
960 * DP_blind = ( P - 1 ) * R + DP
961 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100962 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
963 f_rng, p_rng));
964 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
965 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
966 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +0000967
968 DP = &DP_blind;
969
970 /*
971 * DQ_blind = ( Q - 1 ) * R + DQ
972 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100973 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
974 f_rng, p_rng));
975 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
976 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
977 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +0000978
979 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000980#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200981 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100984 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100985#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200986 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000987 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100989 * TP = input ^ dP mod P
990 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100993 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
994 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
996 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100997 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100999 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1000 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1001 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
1003 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001004 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001005 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001006 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1007 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010 if (f_rng != NULL) {
Paul Bakkerf451bac2013-08-30 15:37:02 +02001011 /*
1012 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001013 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001014 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001015 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1016 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakkerf451bac2013-08-30 15:37:02 +02001017 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Hanno Becker2dec5e82017-10-03 07:49:52 +01001019 /* Verify the result to prevent glitching attacks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001020 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1021 &ctx->N, &ctx->RN));
1022 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001023 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1024 goto cleanup;
1025 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001026
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 olen = ctx->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001028 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
1030cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001032 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1033 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1034 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001035#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001037 mbedtls_mpi_free(&P1);
1038 mbedtls_mpi_free(&Q1);
1039 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041 if (f_rng != NULL) {
Janos Follathe81102e2017-03-22 13:38:28 +00001042#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001043 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001044#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001045 mbedtls_mpi_free(&DP_blind);
1046 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001047#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001048 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001050 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001051
1052#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001053 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001054#endif
1055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001056 mbedtls_mpi_free(&C);
1057 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001059 if (ret != 0 && ret >= -0x007f) {
1060 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1061 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001063 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001064}
1065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001067/**
1068 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1069 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001070 * \param dst buffer to mask
1071 * \param dlen length of destination buffer
1072 * \param src source of the mask generation
1073 * \param slen length of the source buffer
1074 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001075 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001076static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1077 size_t slen, mbedtls_md_context_t *md_ctx)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001078{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001080 unsigned char counter[4];
1081 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001082 unsigned int hlen;
1083 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001084 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086 memset(mask, 0, MBEDTLS_MD_MAX_SIZE);
1087 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001089 hlen = mbedtls_md_get_size(md_ctx->md_info);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001090
Simon Butcher02037452016-03-01 21:19:12 +00001091 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001092 p = dst;
1093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001094 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001095 use_len = hlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001096 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001097 use_len = dlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001098 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001100 if ((ret = mbedtls_md_starts(md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001101 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001102 }
1103 if ((ret = mbedtls_md_update(md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001104 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001105 }
1106 if ((ret = mbedtls_md_update(md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001107 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001108 }
1109 if ((ret = mbedtls_md_finish(md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001110 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001111 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001113 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114 *p++ ^= mask[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001115 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001116
1117 counter[3]++;
1118
1119 dlen -= use_len;
1120 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001121
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001122exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 mbedtls_platform_zeroize(mask, sizeof(mask));
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001125 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001130/*
1131 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1132 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001133int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1134 int (*f_rng)(void *, unsigned char *, size_t),
1135 void *p_rng,
1136 int mode,
1137 const unsigned char *label, size_t label_len,
1138 size_t ilen,
1139 const unsigned char *input,
1140 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001141{
1142 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001144 unsigned char *p = output;
1145 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 const mbedtls_md_info_t *md_info;
1147 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001149 RSA_VALIDATE_RET(ctx != NULL);
1150 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1151 mode == MBEDTLS_RSA_PUBLIC);
1152 RSA_VALIDATE_RET(output != NULL);
1153 RSA_VALIDATE_RET(ilen == 0 || input != NULL);
1154 RSA_VALIDATE_RET(label_len == 0 || label != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001156 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1157 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1158 }
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001160 if (f_rng == NULL) {
1161 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1162 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ctx->hash_id);
1165 if (md_info == NULL) {
1166 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1167 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001168
1169 olen = ctx->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001170 hlen = mbedtls_md_get_size(md_info);
Paul Bakkerb3869132013-02-28 17:21:01 +01001171
Simon Butcher02037452016-03-01 21:19:12 +00001172 /* first comparison checks for overflow */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001173 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1174 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1175 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001177 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001178
1179 *p++ = 0;
1180
Simon Butcher02037452016-03-01 21:19:12 +00001181 /* Generate a random octet string seed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001182 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1183 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1184 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001185
1186 p += hlen;
1187
Simon Butcher02037452016-03-01 21:19:12 +00001188 /* Construct DB */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001189 if ((ret = mbedtls_md(md_info, label, label_len, p)) != 0) {
1190 return ret;
1191 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001192 p += hlen;
1193 p += olen - 2 * hlen - 2 - ilen;
1194 *p++ = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001195 if (ilen != 0) {
1196 memcpy(p, input, ilen);
1197 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001199 mbedtls_md_init(&md_ctx);
1200 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001201 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001202 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001203
Simon Butcher02037452016-03-01 21:19:12 +00001204 /* maskedDB: Apply dbMask to DB */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001205 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1206 &md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001207 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001208 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001209
Simon Butcher02037452016-03-01 21:19:12 +00001210 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001211 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1212 &md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001213 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001214 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001215
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001216exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001217 mbedtls_md_free(&md_ctx);
Paul Bakkerb3869132013-02-28 17:21:01 +01001218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001219 if (ret != 0) {
1220 return ret;
1221 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001223 return (mode == MBEDTLS_RSA_PUBLIC)
1224 ? mbedtls_rsa_public(ctx, output, output)
1225 : mbedtls_rsa_private(ctx, f_rng, p_rng, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001230/*
1231 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1232 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001233int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1234 int (*f_rng)(void *, unsigned char *, size_t),
1235 void *p_rng,
1236 int mode, size_t ilen,
1237 const unsigned char *input,
1238 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001239{
1240 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001242 unsigned char *p = output;
1243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001244 RSA_VALIDATE_RET(ctx != NULL);
1245 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1246 mode == MBEDTLS_RSA_PUBLIC);
1247 RSA_VALIDATE_RET(output != NULL);
1248 RSA_VALIDATE_RET(ilen == 0 || input != NULL);
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001250 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1251 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1252 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001253
1254 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001255
Simon Butcher02037452016-03-01 21:19:12 +00001256 /* first comparison checks for overflow */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001257 if (ilen + 11 < ilen || olen < ilen + 11) {
1258 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1259 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001260
1261 nb_pad = olen - 3 - ilen;
1262
1263 *p++ = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001264 if (mode == MBEDTLS_RSA_PUBLIC) {
1265 if (f_rng == NULL) {
1266 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1267 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001271 while (nb_pad-- > 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001272 int rng_dl = 100;
1273
1274 do {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001275 ret = f_rng(p_rng, p, 1);
1276 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001277
Simon Butcher02037452016-03-01 21:19:12 +00001278 /* Check if RNG failed to generate data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001279 if (rng_dl == 0 || ret != 0) {
1280 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1281 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001282
1283 p++;
1284 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001285 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001288 while (nb_pad-- > 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001289 *p++ = 0xFF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001290 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001291 }
1292
1293 *p++ = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001294 if (ilen != 0) {
1295 memcpy(p, input, ilen);
1296 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001298 return (mode == MBEDTLS_RSA_PUBLIC)
1299 ? mbedtls_rsa_public(ctx, output, output)
1300 : mbedtls_rsa_private(ctx, f_rng, p_rng, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001303
Paul Bakker5121ce52009-01-03 21:22:43 +00001304/*
1305 * Add the message padding, then do an RSA operation
1306 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001307int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1308 int (*f_rng)(void *, unsigned char *, size_t),
1309 void *p_rng,
1310 int mode, size_t ilen,
1311 const unsigned char *input,
1312 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001313{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 RSA_VALIDATE_RET(ctx != NULL);
1315 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1316 mode == MBEDTLS_RSA_PUBLIC);
1317 RSA_VALIDATE_RET(output != NULL);
1318 RSA_VALIDATE_RET(ilen == 0 || input != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001320 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#if defined(MBEDTLS_PKCS1_V15)
1322 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001323 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng, mode, ilen,
1324 input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001325#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_PKCS1_V21)
1328 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, mode, NULL, 0,
1330 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001331#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
1333 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001334 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001336}
1337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001339/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001340 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001342int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1343 int (*f_rng)(void *, unsigned char *, size_t),
1344 void *p_rng,
1345 int mode,
1346 const unsigned char *label, size_t label_len,
1347 size_t *olen,
1348 const unsigned char *input,
1349 unsigned char *output,
1350 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001351{
Janos Follath24eed8d2019-11-22 13:21:35 +00001352 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001353 size_t ilen, i, pad_len;
Dave Rodgman954a2da2023-09-20 14:10:35 +01001354 unsigned char *p, pad_done;
1355 int bad;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1357 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001358 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 const mbedtls_md_info_t *md_info;
1360 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001362 RSA_VALIDATE_RET(ctx != NULL);
1363 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1364 mode == MBEDTLS_RSA_PUBLIC);
1365 RSA_VALIDATE_RET(output_max_len == 0 || output != NULL);
1366 RSA_VALIDATE_RET(label_len == 0 || label != NULL);
1367 RSA_VALIDATE_RET(input != NULL);
1368 RSA_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001369
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001370 /*
1371 * Parameters sanity checks
1372 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001373 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1374 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1375 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
1377 ilen = ctx->len;
1378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001379 if (ilen < 16 || ilen > sizeof(buf)) {
1380 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001383 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ctx->hash_id);
1384 if (md_info == NULL) {
1385 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1386 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001388 hlen = mbedtls_md_get_size(md_info);
Janos Follathc17cda12016-02-11 11:08:18 +00001389
1390 // checking for integer underflow
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001391 if (2 * hlen + 2 > ilen) {
1392 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1393 }
Janos Follathc17cda12016-02-11 11:08:18 +00001394
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001395 /*
1396 * RSA operation
1397 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001398 ret = (mode == MBEDTLS_RSA_PUBLIC)
1399 ? mbedtls_rsa_public(ctx, input, buf)
1400 : mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001402 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001403 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001405
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001406 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001407 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001408 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001409 mbedtls_md_init(&md_ctx);
1410 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
1411 mbedtls_md_free(&md_ctx);
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001412 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001413 }
1414
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001415 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001416 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1417 &md_ctx)) != 0 ||
1418 /* DB: Apply dbMask to maskedDB */
1419 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1420 &md_ctx)) != 0) {
1421 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001422 goto cleanup;
1423 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001425 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001426
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001427 /* Generate lHash */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001428 if ((ret = mbedtls_md(md_info, label, label_len, lhash)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001429 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001430 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001431
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001432 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001433 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001434 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001436 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001438 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001439
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001440 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001441
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001442 /* Check lHash */
Dave Rodgman954a2da2023-09-20 14:10:35 +01001443 bad |= mbedtls_ct_memcmp(lhash, p, hlen);
1444 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001446 /* Get zero-padding len, but always read till end of buffer
1447 * (minus one, for the 01 byte) */
1448 pad_len = 0;
1449 pad_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001450 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001451 pad_done |= p[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001452 pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001453 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001454
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001455 p += pad_len;
1456 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001457
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001458 /*
1459 * The only information "leaked" is whether the padding was correct or not
1460 * (eg, no data is copied if it was not correct). This meets the
1461 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1462 * the different error conditions.
1463 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001465 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1466 goto cleanup;
1467 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001469 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001470 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1471 goto cleanup;
1472 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001473
1474 *olen = ilen - (p - buf);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001475 if (*olen != 0) {
1476 memcpy(output, p, *olen);
1477 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001478 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001479
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001480cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001481 mbedtls_platform_zeroize(buf, sizeof(buf));
1482 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001484 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001489/*
1490 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1491 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001492int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1493 int (*f_rng)(void *, unsigned char *, size_t),
1494 void *p_rng,
1495 int mode,
1496 size_t *olen,
1497 const unsigned char *input,
1498 unsigned char *output,
1499 size_t output_max_len)
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001500{
1501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1502 size_t ilen;
1503 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001505 RSA_VALIDATE_RET(ctx != NULL);
1506 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1507 mode == MBEDTLS_RSA_PUBLIC);
1508 RSA_VALIDATE_RET(output_max_len == 0 || output != NULL);
1509 RSA_VALIDATE_RET(input != NULL);
1510 RSA_VALIDATE_RET(olen != NULL);
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001511
1512 ilen = ctx->len;
1513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001514 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1515 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1516 }
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001518 if (ilen < 16 || ilen > sizeof(buf)) {
1519 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1520 }
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 ret = (mode == MBEDTLS_RSA_PUBLIC)
1523 ? mbedtls_rsa_public(ctx, input, buf)
1524 : mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001526 if (ret != 0) {
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001527 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001528 }
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(mode, buf, ilen,
1531 output, output_max_len, olen);
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001532
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001533cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001534 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001536 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
1540/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001541 * Do an RSA operation, then remove the message padding
1542 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001543int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1544 int (*f_rng)(void *, unsigned char *, size_t),
1545 void *p_rng,
1546 int mode, size_t *olen,
1547 const unsigned char *input,
1548 unsigned char *output,
1549 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001550{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001551 RSA_VALIDATE_RET(ctx != NULL);
1552 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1553 mode == MBEDTLS_RSA_PUBLIC);
1554 RSA_VALIDATE_RET(output_max_len == 0 || output != NULL);
1555 RSA_VALIDATE_RET(input != NULL);
1556 RSA_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001558 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#if defined(MBEDTLS_PKCS1_V15)
1560 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001561 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, mode, olen,
1562 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001563#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#if defined(MBEDTLS_PKCS1_V21)
1566 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001567 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, mode, NULL, 0,
1568 olen, input, output,
1569 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001570#endif
1571
1572 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001573 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001574 }
1575}
1576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001578static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1579 int (*f_rng)(void *, unsigned char *, size_t),
1580 void *p_rng,
1581 int mode,
1582 mbedtls_md_type_t md_alg,
1583 unsigned int hashlen,
1584 const unsigned char *hash,
1585 int saltlen,
1586 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001587{
1588 size_t olen;
1589 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001590 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001591 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001593 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 const mbedtls_md_info_t *md_info;
1595 mbedtls_md_context_t md_ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001596 RSA_VALIDATE_RET(ctx != NULL);
1597 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1598 mode == MBEDTLS_RSA_PUBLIC);
1599 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
1600 hashlen == 0) ||
1601 hash != NULL);
1602 RSA_VALIDATE_RET(sig != NULL);
Paul Bakkerb3869132013-02-28 17:21:01 +01001603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001604 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1605 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1606 }
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001608 if (f_rng == NULL) {
1609 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1610 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001611
1612 olen = ctx->len;
1613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001614 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001615 /* Gather length of hash to sign */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001616 md_info = mbedtls_md_info_from_type(md_alg);
1617 if (md_info == NULL) {
1618 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1619 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621 hashlen = mbedtls_md_get_size(md_info);
Paul Bakkerb3869132013-02-28 17:21:01 +01001622 }
1623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001624 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ctx->hash_id);
1625 if (md_info == NULL) {
1626 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1627 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001629 hlen = mbedtls_md_get_size(md_info);
Paul Bakkerb3869132013-02-28 17:21:01 +01001630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001631 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1632 /* Calculate the largest possible salt length, up to the hash size.
1633 * Normally this is the hash length, which is the maximum salt length
1634 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1635 * enough room, use the maximum salt length that fits. The constraint is
1636 * that the hash length plus the salt length plus 2 bytes must be at most
1637 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1638 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001639 min_slen = hlen - 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001640 if (olen < hlen + min_slen + 2) {
1641 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1642 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001643 slen = hlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001644 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001645 slen = olen - hlen - 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001646 }
1647 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1648 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1649 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001650 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001651 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001653 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001654
Simon Butcher02037452016-03-01 21:19:12 +00001655 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001656 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001657 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001658 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001659
1660 /* Generate salt of length slen in place in the encoded message */
1661 salt = p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001662 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1663 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1664 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001665
Paul Bakkerb3869132013-02-28 17:21:01 +01001666 p += slen;
1667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001668 mbedtls_md_init(&md_ctx);
1669 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001670 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001671 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001672
Simon Butcher02037452016-03-01 21:19:12 +00001673 /* Generate H = Hash( M' ) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001674 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001675 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001676 }
1677 if ((ret = mbedtls_md_update(&md_ctx, p, 8)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001678 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001679 }
1680 if ((ret = mbedtls_md_update(&md_ctx, hash, hashlen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001681 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001682 }
1683 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001684 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001685 }
1686 if ((ret = mbedtls_md_finish(&md_ctx, p)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001687 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001688 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001689
Simon Butcher02037452016-03-01 21:19:12 +00001690 /* Compensate for boundary condition when applying mask */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001691 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001692 offset = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001693 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001694
Simon Butcher02037452016-03-01 21:19:12 +00001695 /* maskedDB: Apply dbMask to DB */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001696 if ((ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
1697 &md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001698 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001699 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001701 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1702 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001703
1704 p += hlen;
1705 *p++ = 0xBC;
1706
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001707exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001708 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001710 if (ret != 0) {
1711 return ret;
1712 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001714 return (mode == MBEDTLS_RSA_PUBLIC)
1715 ? mbedtls_rsa_public(ctx, sig, sig)
1716 : mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001717}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001718
1719/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001720 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1721 * the option to pass in the salt length.
1722 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001723int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1724 int (*f_rng)(void *, unsigned char *, size_t),
1725 void *p_rng,
1726 mbedtls_md_type_t md_alg,
1727 unsigned int hashlen,
1728 const unsigned char *hash,
1729 int saltlen,
1730 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001731{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001732 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, md_alg,
1733 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001734}
1735
1736
1737/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001738 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1739 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001740int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1741 int (*f_rng)(void *, unsigned char *, size_t),
1742 void *p_rng,
1743 int mode,
1744 mbedtls_md_type_t md_alg,
1745 unsigned int hashlen,
1746 const unsigned char *hash,
1747 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001748{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001749 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, mode, md_alg,
1750 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001751}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001755/*
1756 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1757 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001758
1759/* Construct a PKCS v1.5 encoding of a hashed message
1760 *
1761 * This is used both for signature generation and verification.
1762 *
1763 * Parameters:
1764 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001765 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001766 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001767 * - hash: Buffer containing the hashed message or the raw data.
1768 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001769 * - dst: Buffer to hold the encoded message.
1770 *
1771 * Assumptions:
1772 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1773 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001774 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001775 *
1776 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001777static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1778 unsigned int hashlen,
1779 const unsigned char *hash,
1780 size_t dst_len,
1781 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001782{
1783 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001784 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001785 unsigned char *p = dst;
1786 const char *oid = NULL;
1787
1788 /* Are we signing hashed or raw data? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001789 if (md_alg != MBEDTLS_MD_NONE) {
1790 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1791 if (md_info == NULL) {
1792 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1793 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001795 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1796 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1797 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001799 hashlen = mbedtls_md_get_size(md_info);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001800
1801 /* Double-check that 8 + hashlen + oid_size can be used as a
1802 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001803 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001804 10 + hashlen < hashlen ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001805 10 + hashlen + oid_size < 10 + hashlen) {
1806 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1807 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001808
1809 /*
1810 * Static bounds check:
1811 * - Need 10 bytes for five tag-length pairs.
1812 * (Insist on 1-byte length encodings to protect against variants of
1813 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1814 * - Need hashlen bytes for hash
1815 * - Need oid_size bytes for hash alg OID.
1816 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001817 if (nb_pad < 10 + hashlen + oid_size) {
1818 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1819 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001820 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001821 } else {
1822 if (nb_pad < hashlen) {
1823 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1824 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001825
1826 nb_pad -= hashlen;
1827 }
1828
Hanno Becker2b2f8982017-09-27 17:10:03 +01001829 /* Need space for signature header and padding delimiter (3 bytes),
1830 * and 8 bytes for the minimal padding */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001831 if (nb_pad < 3 + 8) {
1832 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1833 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001834 nb_pad -= 3;
1835
1836 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001837 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001838
1839 /* Write signature header and padding */
1840 *p++ = 0;
1841 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001842 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001843 p += nb_pad;
1844 *p++ = 0;
1845
1846 /* Are we signing raw data? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001847 if (md_alg == MBEDTLS_MD_NONE) {
1848 memcpy(p, hash, hashlen);
1849 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001850 }
1851
1852 /* Signing hashed data, add corresponding ASN.1 structure
1853 *
1854 * DigestInfo ::= SEQUENCE {
1855 * digestAlgorithm DigestAlgorithmIdentifier,
1856 * digest Digest }
1857 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1858 * Digest ::= OCTET STRING
1859 *
1860 * Schematic:
1861 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1862 * TAG-NULL + LEN [ NULL ] ]
1863 * TAG-OCTET + LEN [ HASH ] ]
1864 */
1865 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001866 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001867 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001868 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001869 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001870 *p++ = (unsigned char) oid_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001871 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001872 p += oid_size;
1873 *p++ = MBEDTLS_ASN1_NULL;
1874 *p++ = 0x00;
1875 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001876 *p++ = (unsigned char) hashlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001877 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001878 p += hashlen;
1879
1880 /* Just a sanity-check, should be automatic
1881 * after the initial bounds check. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001882 if (p != dst + dst_len) {
1883 mbedtls_platform_zeroize(dst, dst_len);
1884 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001885 }
1886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001887 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001888}
1889
Paul Bakkerb3869132013-02-28 17:21:01 +01001890/*
1891 * Do an RSA operation to sign the message digest
1892 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001893int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
1894 int (*f_rng)(void *, unsigned char *, size_t),
1895 void *p_rng,
1896 int mode,
1897 mbedtls_md_type_t md_alg,
1898 unsigned int hashlen,
1899 const unsigned char *hash,
1900 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001901{
Janos Follath24eed8d2019-11-22 13:21:35 +00001902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001903 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001905 RSA_VALIDATE_RET(ctx != NULL);
1906 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1907 mode == MBEDTLS_RSA_PUBLIC);
1908 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
1909 hashlen == 0) ||
1910 hash != NULL);
1911 RSA_VALIDATE_RET(sig != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001912
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001913 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1914 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1915 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001916
Hanno Beckerfdf38032017-09-06 12:35:55 +01001917 /*
1918 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1919 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001921 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
1922 ctx->len, sig)) != 0) {
1923 return ret;
1924 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001925
1926 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001927 * Call respective RSA primitive
1928 */
1929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001930 if (mode == MBEDTLS_RSA_PUBLIC) {
Hanno Beckerfdf38032017-09-06 12:35:55 +01001931 /* Skip verification on a public key operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001932 return mbedtls_rsa_public(ctx, sig, sig);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001933 }
1934
1935 /* Private key operation
1936 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001937 * In order to prevent Lenstra's attack, make the signature in a
1938 * temporary buffer and check it before returning it.
1939 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001941 sig_try = mbedtls_calloc(1, ctx->len);
1942 if (sig_try == NULL) {
1943 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00001944 }
1945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001946 verif = mbedtls_calloc(1, ctx->len);
1947 if (verif == NULL) {
1948 mbedtls_free(sig_try);
1949 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
1950 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001952 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
1953 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
1954
1955 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001956 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1957 goto cleanup;
1958 }
1959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001960 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001961
1962cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001963 mbedtls_platform_zeroize(sig_try, ctx->len);
1964 mbedtls_platform_zeroize(verif, ctx->len);
1965 mbedtls_free(sig_try);
1966 mbedtls_free(verif);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001968 if (ret != 0) {
1969 memset(sig, '!', ctx->len);
1970 }
1971 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001972}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001974
1975/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 * Do an RSA operation to sign the message digest
1977 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001978int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
1979 int (*f_rng)(void *, unsigned char *, size_t),
1980 void *p_rng,
1981 int mode,
1982 mbedtls_md_type_t md_alg,
1983 unsigned int hashlen,
1984 const unsigned char *hash,
1985 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00001986{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001987 RSA_VALIDATE_RET(ctx != NULL);
1988 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1989 mode == MBEDTLS_RSA_PUBLIC);
1990 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
1991 hashlen == 0) ||
1992 hash != NULL);
1993 RSA_VALIDATE_RET(sig != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996#if defined(MBEDTLS_PKCS1_V15)
1997 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001998 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng, mode, md_alg,
1999 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002000#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#if defined(MBEDTLS_PKCS1_V21)
2003 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002004 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, mode, md_alg,
2005 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002006#endif
2007
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002009 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002011}
2012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002014/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002015 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002016 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002017int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2018 int (*f_rng)(void *, unsigned char *, size_t),
2019 void *p_rng,
2020 int mode,
2021 mbedtls_md_type_t md_alg,
2022 unsigned int hashlen,
2023 const unsigned char *hash,
2024 mbedtls_md_type_t mgf1_hash_id,
2025 int expected_salt_len,
2026 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002027{
Janos Follath24eed8d2019-11-22 13:21:35 +00002028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002029 size_t siglen;
2030 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002031 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002033 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002034 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002035 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 const mbedtls_md_info_t *md_info;
2037 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002038 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002040 RSA_VALIDATE_RET(ctx != NULL);
2041 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2042 mode == MBEDTLS_RSA_PUBLIC);
2043 RSA_VALIDATE_RET(sig != NULL);
2044 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2045 hashlen == 0) ||
2046 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002048 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2049 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2050 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002051
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 siglen = ctx->len;
2053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002054 if (siglen < 16 || siglen > sizeof(buf)) {
2055 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002058 ret = (mode == MBEDTLS_RSA_PUBLIC)
2059 ? mbedtls_rsa_public(ctx, sig, buf)
2060 : mbedtls_rsa_private(ctx, f_rng, p_rng, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002062 if (ret != 0) {
2063 return ret;
2064 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002065
2066 p = buf;
2067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002068 if (buf[siglen - 1] != 0xBC) {
2069 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002070 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002072 if (md_alg != MBEDTLS_MD_NONE) {
2073 /* Gather length of hash to sign */
2074 md_info = mbedtls_md_info_from_type(md_alg);
2075 if (md_info == NULL) {
2076 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2077 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002079 hashlen = mbedtls_md_get_size(md_info);
2080 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002082 md_info = mbedtls_md_info_from_type(mgf1_hash_id);
2083 if (md_info == NULL) {
2084 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2085 }
2086
2087 hlen = mbedtls_md_get_size(md_info);
2088
2089 memset(zeros, 0, 8);
Paul Bakker53019ae2011-03-25 13:58:48 +00002090
Simon Butcher02037452016-03-01 21:19:12 +00002091 /*
2092 * Note: EMSA-PSS verification is over the length of N - 1 bits
2093 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002096 if (buf[0] >> (8 - siglen * 8 + msb)) {
2097 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2098 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002099
Simon Butcher02037452016-03-01 21:19:12 +00002100 /* Compensate for boundary condition when applying mask */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002102 p++;
2103 siglen -= 1;
2104 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002106 if (siglen < hlen + 2) {
2107 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2108 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002109 hash_start = p + siglen - hlen - 1;
2110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002111 mbedtls_md_init(&md_ctx);
2112 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002113 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002114 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002116 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, &md_ctx);
2117 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002118 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002119 }
Paul Bakker02303e82013-01-03 11:08:31 +01002120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002121 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002123 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002124 p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002125 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002127 if (*p++ != 0x01) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002128 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2129 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002130 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002131
Gilles Peskine6a54b022017-10-17 19:02:13 +02002132 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002134 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2135 observed_salt_len != (size_t) expected_salt_len) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002136 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2137 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002138 }
2139
Simon Butcher02037452016-03-01 21:19:12 +00002140 /*
2141 * Generate H = Hash( M' )
2142 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 ret = mbedtls_md_starts(&md_ctx);
2144 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002145 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002146 }
2147 ret = mbedtls_md_update(&md_ctx, zeros, 8);
2148 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002149 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002150 }
2151 ret = mbedtls_md_update(&md_ctx, hash, hashlen);
2152 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002153 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002154 }
2155 ret = mbedtls_md_update(&md_ctx, p, observed_salt_len);
2156 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002157 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158 }
2159 ret = mbedtls_md_finish(&md_ctx, result);
2160 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002161 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002162 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002164 if (memcmp(hash_start, result, hlen) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002165 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002166 goto exit;
2167 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002168
2169exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002170 mbedtls_md_free(&md_ctx);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002172 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002173}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002174
2175/*
2176 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2177 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002178int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2179 int (*f_rng)(void *, unsigned char *, size_t),
2180 void *p_rng,
2181 int mode,
2182 mbedtls_md_type_t md_alg,
2183 unsigned int hashlen,
2184 const unsigned char *hash,
2185 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002186{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002187 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188 RSA_VALIDATE_RET(ctx != NULL);
2189 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2190 mode == MBEDTLS_RSA_PUBLIC);
2191 RSA_VALIDATE_RET(sig != NULL);
2192 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2193 hashlen == 0) ||
2194 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002196 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002198 : md_alg;
2199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002200 return mbedtls_rsa_rsassa_pss_verify_ext(ctx, f_rng, p_rng, mode,
2201 md_alg, hashlen, hash,
2202 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
2203 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002204
2205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002209/*
2210 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2211 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002212int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2213 int (*f_rng)(void *, unsigned char *, size_t),
2214 void *p_rng,
2215 int mode,
2216 mbedtls_md_type_t md_alg,
2217 unsigned int hashlen,
2218 const unsigned char *hash,
2219 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002220{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002221 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002222 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002223 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002225 RSA_VALIDATE_RET(ctx != NULL);
2226 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2227 mode == MBEDTLS_RSA_PUBLIC);
2228 RSA_VALIDATE_RET(sig != NULL);
2229 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2230 hashlen == 0) ||
2231 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002232
2233 sig_len = ctx->len;
2234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002235 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2236 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2237 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002238
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002239 /*
2240 * Prepare expected PKCS1 v1.5 encoding of hash.
2241 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002243 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2244 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002245 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2246 goto cleanup;
2247 }
2248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002249 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2250 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002251 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002252 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002253
2254 /*
2255 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2256 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002258 ret = (mode == MBEDTLS_RSA_PUBLIC)
2259 ? mbedtls_rsa_public(ctx, sig, encoded)
2260 : mbedtls_rsa_private(ctx, f_rng, p_rng, sig, encoded);
2261 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002262 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002263 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002264
Simon Butcher02037452016-03-01 21:19:12 +00002265 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002266 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002267 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002269 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2270 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002271 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2272 goto cleanup;
2273 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002274
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002275cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002277 if (encoded != NULL) {
2278 mbedtls_platform_zeroize(encoded, sig_len);
2279 mbedtls_free(encoded);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002280 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002282 if (encoded_expected != NULL) {
2283 mbedtls_platform_zeroize(encoded_expected, sig_len);
2284 mbedtls_free(encoded_expected);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002285 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002287 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002288}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002290
2291/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002292 * Do an RSA operation and check the message digest
2293 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002294int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2295 int (*f_rng)(void *, unsigned char *, size_t),
2296 void *p_rng,
2297 int mode,
2298 mbedtls_md_type_t md_alg,
2299 unsigned int hashlen,
2300 const unsigned char *hash,
2301 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002302{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002303 RSA_VALIDATE_RET(ctx != NULL);
2304 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2305 mode == MBEDTLS_RSA_PUBLIC);
2306 RSA_VALIDATE_RET(sig != NULL);
2307 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2308 hashlen == 0) ||
2309 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002311 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312#if defined(MBEDTLS_PKCS1_V15)
2313 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002314 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, f_rng, p_rng, mode, md_alg,
2315 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002316#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318#if defined(MBEDTLS_PKCS1_V21)
2319 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002320 return mbedtls_rsa_rsassa_pss_verify(ctx, f_rng, p_rng, mode, md_alg,
2321 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002322#endif
2323
2324 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002325 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002326 }
2327}
2328
2329/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002330 * Copy the components of an RSA key
2331 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002332int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002333{
Janos Follath24eed8d2019-11-22 13:21:35 +00002334 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002335 RSA_VALIDATE_RET(dst != NULL);
2336 RSA_VALIDATE_RET(src != NULL);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002337
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002338 dst->len = src->len;
2339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002340 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2341 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002343 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2344 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2345 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002346
2347#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002348 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2349 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2350 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2351 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2352 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002353#endif
2354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002355 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002357 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2358 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002359
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002360 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002361 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002362
2363cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 if (ret != 0) {
2365 mbedtls_rsa_free(dst);
2366 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002368 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002369}
2370
2371/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 * Free the components of an RSA key
2373 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002374void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002375{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002376 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002377 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002378 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002380 mbedtls_mpi_free(&ctx->Vi);
2381 mbedtls_mpi_free(&ctx->Vf);
2382 mbedtls_mpi_free(&ctx->RN);
2383 mbedtls_mpi_free(&ctx->D);
2384 mbedtls_mpi_free(&ctx->Q);
2385 mbedtls_mpi_free(&ctx->P);
2386 mbedtls_mpi_free(&ctx->E);
2387 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002388
Hanno Becker33c30a02017-08-23 07:00:22 +01002389#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002390 mbedtls_mpi_free(&ctx->RQ);
2391 mbedtls_mpi_free(&ctx->RP);
2392 mbedtls_mpi_free(&ctx->QP);
2393 mbedtls_mpi_free(&ctx->DQ);
2394 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002395#endif /* MBEDTLS_RSA_NO_CRT */
2396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002398 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002399 if (ctx->ver != 0) {
2400 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002401 ctx->ver = 0;
2402 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002403#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002404}
2405
Hanno Beckerab377312017-08-23 16:24:51 +01002406#endif /* !MBEDTLS_RSA_ALT */
2407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002410#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002411
2412/*
2413 * Example RSA-1024 keypair, for test purposes
2414 */
2415#define KEY_LEN 128
2416
2417#define RSA_N "9292758453063D803DD603D5E777D788" \
2418 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2419 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2420 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2421 "93A89813FBF3C4F8066D2D800F7C38A8" \
2422 "1AE31942917403FF4946B0A83D3D3E05" \
2423 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2424 "5E94BB77B07507233A0BC7BAC8F90F79"
2425
2426#define RSA_E "10001"
2427
2428#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2429 "66CA472BC44D253102F8B4A9D3BFA750" \
2430 "91386C0077937FE33FA3252D28855837" \
2431 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2432 "DF79C5CE07EE72C7F123142198164234" \
2433 "CABB724CF78B8173B9F880FC86322407" \
2434 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2435 "071513A1E85B5DFA031F21ECAE91A34D"
2436
2437#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2438 "2C01CAD19EA484A87EA4377637E75500" \
2439 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2440 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2441
2442#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2443 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2444 "910E4168387E3C30AA1E00C339A79508" \
2445 "8452DD96A9A5EA5D9DCA68DA636032AF"
2446
Paul Bakker5121ce52009-01-03 21:22:43 +00002447#define PT_LEN 24
2448#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2449 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002452static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002453{
gufe44c2620da2020-08-03 17:56:50 +02002454#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002455 size_t i;
2456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002457 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002458 rng_state = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459 }
Paul Bakker545570e2010-07-18 09:00:25 +00002460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002461 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002462 output[i] = rand();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002463 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002464#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002465 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002466 rng_state = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002467 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002469 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002470#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002472 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002473}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002475
Paul Bakker5121ce52009-01-03 21:22:43 +00002476/*
2477 * Checkup routine
2478 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002479int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002480{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002481 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002483 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 unsigned char rsa_plaintext[PT_LEN];
2486 unsigned char rsa_decrypted[PT_LEN];
2487 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002489 unsigned char sha1sum[20];
2490#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Hanno Becker3a701162017-08-22 13:52:43 +01002492 mbedtls_mpi K;
2493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002494 mbedtls_mpi_init(&K);
2495 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002497 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2498 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2499 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2500 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2501 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2502 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2503 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2504 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2505 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2506 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002508 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002510 if (verbose != 0) {
2511 mbedtls_printf(" RSA key validation: ");
2512 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002514 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2515 mbedtls_rsa_check_privkey(&rsa) != 0) {
2516 if (verbose != 0) {
2517 mbedtls_printf("failed\n");
2518 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Hanno Becker5bc87292017-05-03 15:09:31 +01002520 ret = 1;
2521 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 }
2523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002524 if (verbose != 0) {
2525 mbedtls_printf("passed\n PKCS#1 encryption : ");
2526 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002528 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002530 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2531 PT_LEN, rsa_plaintext,
2532 rsa_ciphertext) != 0) {
2533 if (verbose != 0) {
2534 mbedtls_printf("failed\n");
2535 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Hanno Becker5bc87292017-05-03 15:09:31 +01002537 ret = 1;
2538 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 }
2540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002541 if (verbose != 0) {
2542 mbedtls_printf("passed\n PKCS#1 decryption : ");
2543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002545 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2546 &len, rsa_ciphertext, rsa_decrypted,
2547 sizeof(rsa_decrypted)) != 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 Peskine1b6c09a2023-01-11 14:52:35 +01002556 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2557 if (verbose != 0) {
2558 mbedtls_printf("failed\n");
2559 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Hanno Becker5bc87292017-05-03 15:09:31 +01002561 ret = 1;
2562 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 }
2564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002565 if (verbose != 0) {
2566 mbedtls_printf("passed\n");
2567 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002570 if (verbose != 0) {
2571 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002572 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002574 if (mbedtls_sha1_ret(rsa_plaintext, PT_LEN, sha1sum) != 0) {
2575 if (verbose != 0) {
2576 mbedtls_printf("failed\n");
2577 }
2578
2579 return 1;
2580 }
2581
2582 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2583 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2584 sha1sum, rsa_ciphertext) != 0) {
2585 if (verbose != 0) {
2586 mbedtls_printf("failed\n");
2587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Hanno Becker5bc87292017-05-03 15:09:31 +01002589 ret = 1;
2590 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 }
2592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002593 if (verbose != 0) {
2594 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002597 if (mbedtls_rsa_pkcs1_verify(&rsa, NULL, NULL,
2598 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2599 sha1sum, rsa_ciphertext) != 0) {
2600 if (verbose != 0) {
2601 mbedtls_printf("failed\n");
2602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Hanno Becker5bc87292017-05-03 15:09:31 +01002604 ret = 1;
2605 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 }
2607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002608 if (verbose != 0) {
2609 mbedtls_printf("passed\n");
2610 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002613 if (verbose != 0) {
2614 mbedtls_printf("\n");
2615 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002616
Paul Bakker3d8fb632014-04-17 12:42:41 +02002617cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002618 mbedtls_mpi_free(&K);
2619 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002621 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002623 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002624}
2625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628#endif /* MBEDTLS_RSA_C */