blob: 937d4aacdbf5a7b77abcf4170dbee65c13b97c59 [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
Dave Rodgman0f2971a2023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
Hanno Becker74716312017-10-02 10:00:37 +01007
Paul Bakker5121ce52009-01-03 21:22:43 +00008/*
Simon Butcherbdae02c2016-01-20 00:44:42 +00009 * The following sources were referenced in the design of this implementation
10 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000011 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000012 * [1] A method for obtaining digital signatures and public-key cryptosystems
13 * R Rivest, A Shamir, and L Adleman
14 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
15 *
16 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
17 * Menezes, van Oorschot and Vanstone
18 *
Janos Follathe81102e2017-03-22 13:38:28 +000019 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
20 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
21 * Stefan Mangard
22 * https://arxiv.org/abs/1702.08719v2
23 *
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010031#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000034#include "mbedtls/error.h"
Gabor Mezeic0ae1cf2021-10-20 12:09:35 +020035#include "constant_time_internal.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020036#include "mbedtls/constant_time.h"
Janos Follathaa6760d2024-01-08 15:09:34 +000037#include "bignum_internal.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000038
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <string.h>
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
gufe44c2620da2020-08-03 17:56:50 +020045#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000046#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010050
Hanno Beckera565f542017-10-11 11:00:19 +010051#if !defined(MBEDTLS_RSA_ALT)
52
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050053/* Parameter validation macros */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054#define RSA_VALIDATE_RET(cond) \
55 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA)
56#define RSA_VALIDATE(cond) \
57 MBEDTLS_INTERNAL_VALIDATE(cond)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
60 const mbedtls_mpi *N,
61 const mbedtls_mpi *P, const mbedtls_mpi *Q,
62 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +010063{
Janos Follath24eed8d2019-11-22 13:21:35 +000064 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +010066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
68 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
69 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
70 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
71 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
72 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +010073 }
74
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 if (N != NULL) {
76 ctx->len = mbedtls_mpi_size(&ctx->N);
77 }
Hanno Becker617c1ae2017-08-23 14:11:24 +010078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +010080}
81
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
83 unsigned char const *N, size_t N_len,
84 unsigned char const *P, size_t P_len,
85 unsigned char const *Q, size_t Q_len,
86 unsigned char const *D, size_t D_len,
87 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +010088{
Hanno Beckerd4d60572018-01-10 07:12:01 +000089 int ret = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +010091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 if (N != NULL) {
93 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
94 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +010095 }
96
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 if (P != NULL) {
98 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
99 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 if (Q != NULL) {
102 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
103 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 if (D != NULL) {
106 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
107 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 if (E != NULL) {
110 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
111 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100112
113cleanup:
114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 if (ret != 0) {
116 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
117 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100120}
121
Hanno Becker705fc682017-10-10 17:57:02 +0100122/*
123 * Checks whether the context fields are set in such a way
124 * that the RSA primitives will be able to execute without error.
125 * It does *not* make guarantees for consistency of the parameters.
126 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
128 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100129{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100130#if !defined(MBEDTLS_RSA_NO_CRT)
131 /* blinding_needed is only used for NO_CRT to decide whether
132 * P,Q need to be present or not. */
133 ((void) blinding_needed);
134#endif
135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
137 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
138 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000139 }
Hanno Becker705fc682017-10-10 17:57:02 +0100140
141 /*
142 * 1. Modular exponentiation needs positive, odd moduli.
143 */
144
145 /* Modular exponentiation wrt. N is always used for
146 * RSA public key operations. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
148 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
149 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100150 }
151
152#if !defined(MBEDTLS_RSA_NO_CRT)
153 /* Modular exponentiation for P and Q is only
154 * used for private key operations and if CRT
155 * is used. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 if (is_priv &&
157 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
158 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
159 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
160 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
161 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100162 }
163#endif /* !MBEDTLS_RSA_NO_CRT */
164
165 /*
166 * 2. Exponents must be positive
167 */
168
169 /* Always need E for public key operations */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
171 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
172 }
Hanno Becker705fc682017-10-10 17:57:02 +0100173
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100174#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100175 /* For private key operations, use D or DP & DQ
176 * as (unblinded) exponents. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
178 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
179 }
Hanno Becker705fc682017-10-10 17:57:02 +0100180#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 if (is_priv &&
182 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
183 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
184 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100185 }
186#endif /* MBEDTLS_RSA_NO_CRT */
187
188 /* Blinding shouldn't make exponents negative either,
189 * so check that P, Q >= 1 if that hasn't yet been
190 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100191#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 if (is_priv && blinding_needed &&
193 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
194 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
195 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100196 }
197#endif
198
199 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100200 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100201#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 if (is_priv &&
203 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
204 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100205 }
206#endif
207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100209}
210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100212{
213 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500214 int have_N, have_P, have_Q, have_D, have_E;
215#if !defined(MBEDTLS_RSA_NO_CRT)
216 int have_DP, have_DQ, have_QP;
217#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 RSA_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
223 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
224 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
225 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
226 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500227
228#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
230 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
231 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500232#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100233
Hanno Becker617c1ae2017-08-23 14:11:24 +0100234 /*
235 * Check whether provided parameters are enough
236 * to deduce all others. The following incomplete
237 * parameter sets for private keys are supported:
238 *
239 * (1) P, Q missing.
240 * (2) D and potentially N missing.
241 *
242 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100243
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500244 n_missing = have_P && have_Q && have_D && have_E;
245 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
246 d_missing = have_P && have_Q && !have_D && have_E;
247 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100248
249 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500250 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 if (!is_priv && !is_pub) {
253 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
254 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100255
256 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100257 * Step 1: Deduce N if P, Q are provided.
258 */
259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 if (!have_N && have_P && have_Q) {
261 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
262 &ctx->Q)) != 0) {
263 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100264 }
265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100267 }
268
269 /*
270 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100271 */
272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 if (pq_missing) {
274 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
275 &ctx->P, &ctx->Q);
276 if (ret != 0) {
277 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
278 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 } else if (d_missing) {
281 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
282 &ctx->Q,
283 &ctx->E,
284 &ctx->D)) != 0) {
285 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100286 }
287 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100288
Hanno Becker617c1ae2017-08-23 14:11:24 +0100289 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100290 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100291 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100292 */
293
Hanno Becker23344b52017-08-23 07:43:27 +0100294#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 if (is_priv && !(have_DP && have_DQ && have_QP)) {
296 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
297 &ctx->DP, &ctx->DQ, &ctx->QP);
298 if (ret != 0) {
299 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
300 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100301 }
Hanno Becker23344b52017-08-23 07:43:27 +0100302#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100303
304 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100305 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100306 */
307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100309}
310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
312 unsigned char *N, size_t N_len,
313 unsigned char *P, size_t P_len,
314 unsigned char *Q, size_t Q_len,
315 unsigned char *D, size_t D_len,
316 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100317{
318 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500319 int is_priv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100321
322 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500323 is_priv =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
325 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
326 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
327 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
328 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100331 /* If we're trying to export private parameters for a public key,
332 * something must be wrong. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 if (P != NULL || Q != NULL || D != NULL) {
334 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
335 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100336
337 }
338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 if (N != NULL) {
340 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
341 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 if (P != NULL) {
344 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
345 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 if (Q != NULL) {
348 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
349 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 if (D != NULL) {
352 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
353 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 if (E != NULL) {
356 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
357 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100358
359cleanup:
360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100362}
363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
365 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
366 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100367{
Janos Follath24eed8d2019-11-22 13:21:35 +0000368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500369 int is_priv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100371
372 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500373 is_priv =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
375 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
376 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
377 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
378 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100381 /* If we're trying to export private parameters for a public key,
382 * something must be wrong. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 if (P != NULL || Q != NULL || D != NULL) {
384 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
385 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100386
387 }
388
389 /* Export all requested core parameters. */
390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
392 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
393 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
394 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
395 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
396 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100397 }
398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100400}
401
402/*
403 * Export CRT parameters
404 * This must also be implemented if CRT is not used, for being able to
405 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
406 * can be used in this case.
407 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
409 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100410{
Janos Follath24eed8d2019-11-22 13:21:35 +0000411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500412 int is_priv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100414
415 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500416 is_priv =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
418 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
419 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
420 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
421 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 if (!is_priv) {
424 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
425 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100426
Hanno Beckerdc95c892017-08-23 06:57:02 +0100427#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100428 /* Export all requested blinding parameters. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
430 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
431 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
432 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100433 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100434#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
436 DP, DQ, QP)) != 0) {
437 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100438 }
439#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100442}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100443
Paul Bakker5121ce52009-01-03 21:22:43 +0000444/*
445 * Initialize an RSA context
446 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447void mbedtls_rsa_init(mbedtls_rsa_context *ctx,
448 int padding,
449 int hash_id)
Paul Bakker5121ce52009-01-03 21:22:43 +0000450{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 RSA_VALIDATE(ctx != NULL);
452 RSA_VALIDATE(padding == MBEDTLS_RSA_PKCS_V15 ||
453 padding == MBEDTLS_RSA_PKCS_V21);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 mbedtls_rsa_set_padding(ctx, padding, hash_id);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100460 /* Set ctx->ver to nonzero to indicate that the mutex has been
461 * initialized and will need to be freed. */
462 ctx->ver = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200464#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000465}
466
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100467/*
468 * Set padding for an existing RSA context
469 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100470void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
471 int hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100472{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 RSA_VALIDATE(ctx != NULL);
474 RSA_VALIDATE(padding == MBEDTLS_RSA_PKCS_V15 ||
475 padding == MBEDTLS_RSA_PKCS_V21);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500476
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100477 ctx->padding = padding;
478 ctx->hash_id = hash_id;
479}
480
Hanno Becker617c1ae2017-08-23 14:11:24 +0100481/*
482 * Get length in bytes of RSA modulus
483 */
484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100486{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100488}
489
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493/*
494 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800495 *
496 * This generation method follows the RSA key pair generation procedure of
497 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100499int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
500 int (*f_rng)(void *, unsigned char *, size_t),
501 void *p_rng,
502 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000503{
Janos Follath24eed8d2019-11-22 13:21:35 +0000504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800505 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100506 int prime_quality = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 RSA_VALIDATE_RET(ctx != NULL);
508 RSA_VALIDATE_RET(f_rng != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
Janos Follathb8fc1b02018-09-03 15:37:01 +0100510 /*
511 * If the modulus is 1024 bit long or shorter, then the security strength of
512 * the RSA algorithm is less than or equal to 80 bits and therefore an error
513 * rate of 2^-80 is sufficient.
514 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100515 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100516 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100517 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 mbedtls_mpi_init(&H);
520 mbedtls_mpi_init(&G);
521 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523 if (nbits < 128 || exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100524 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
525 goto cleanup;
526 }
527
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 /*
529 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800530 * 1. |P-Q| > 2^( nbits / 2 - 100 )
531 * 2. GCD( E, (P-1)*(Q-1) ) == 1
532 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100536 do {
537 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
538 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
541 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800543 /* 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 +0100544 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
545 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800549 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 if (H.s < 0) {
551 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
552 }
Janos Follathef441782016-09-21 13:18:12 +0100553
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100554 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
556 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
557 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800558
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800559 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100560 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
561 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800562 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800564
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800565 /* 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 +0100566 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
567 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
568 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100570 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 -0800571 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100572 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800573
574 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100577 /* Restore P,Q */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
579 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100581 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100583 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100584
Jethro Beekman97f95c92018-02-13 15:50:36 -0800585#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 * DP = D mod (P - 1)
588 * DQ = D mod (Q - 1)
589 * QP = Q^-1 mod P
590 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100591 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
592 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100593#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Hanno Becker83aad1f2017-08-23 06:45:10 +0100595 /* Double-check */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
598cleanup:
599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100600 mbedtls_mpi_free(&H);
601 mbedtls_mpi_free(&G);
602 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100604 if (ret != 0) {
605 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100607 if ((-ret & ~0x7f) == 0) {
608 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
609 }
610 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 }
612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100613 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000614}
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
618/*
619 * Check a public RSA key
620 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000622{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100623 RSA_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500624
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100625 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
626 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100627 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100629 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
630 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100631 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
634 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
635 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
636 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
637 }
638
639 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000640}
641
642/*
Hanno Becker705fc682017-10-10 17:57:02 +0100643 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100645int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000646{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647 RSA_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100649 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
650 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
651 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 }
Paul Bakker48377d92013-08-30 12:06:24 +0200653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100654 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
655 &ctx->D, &ctx->E, NULL, NULL) != 0) {
656 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000658
Hanno Beckerb269a852017-08-25 08:03:21 +0100659#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
661 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
662 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100663 }
664#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100666 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000667}
668
669/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100670 * Check if contexts holding a public and private key match
671 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100672int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
673 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100674{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100675 RSA_VALIDATE_RET(pub != NULL);
676 RSA_VALIDATE_RET(prv != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
679 mbedtls_rsa_check_privkey(prv) != 0) {
680 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100681 }
682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
684 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
685 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100686 }
687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100689}
690
691/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000692 * Do an RSA public key operation
693 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100694int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
695 const unsigned char *input,
696 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000697{
Janos Follath24eed8d2019-11-22 13:21:35 +0000698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000699 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_mpi T;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100701 RSA_VALIDATE_RET(ctx != NULL);
702 RSA_VALIDATE_RET(input != NULL);
703 RSA_VALIDATE_RET(output != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
706 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
707 }
Hanno Becker705fc682017-10-10 17:57:02 +0100708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200711#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100712 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
713 return ret;
714 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200715#endif
716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100719 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200720 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
721 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 }
723
724 olen = ctx->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100725 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
726 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000727
728cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100730 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
731 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
732 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100733#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100735 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100737 if (ret != 0) {
738 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
739 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100741 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000742}
743
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200744/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200745 * Generate or update blinding values, see section 10 of:
746 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200747 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200748 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200749 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100750static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
751 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200752{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200753 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200754 mbedtls_mpi R;
755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100756 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200759 /* We already have blinding values, just update them by squaring */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100760 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
761 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
762 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
763 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200764
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200765 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200766 }
767
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200768 /* Unblinding value: Vf = random number, invertible mod N */
769 do {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100770 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200771 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
772 goto cleanup;
773 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100775 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 +0200776
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200777 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100778 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
779 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
780 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200781
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200782 /* At this point, Vi is invertible mod N if and only if both Vf and R
783 * are invertible mod N. If one of them isn't, we don't need to know
784 * which one, we just loop and choose new values for both of them.
785 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100786 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
787 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200788 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100789 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200790
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500792
793 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
795 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200796
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200797 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200798 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100799 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 +0200800
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200801
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200802cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100803 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100805 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200806}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200807
Paul Bakker5121ce52009-01-03 21:22:43 +0000808/*
Janos Follathaa6760d2024-01-08 15:09:34 +0000809 * Unblind
810 * T = T * Vf mod N
811 */
812static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, const mbedtls_mpi *N)
813{
814 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
815 const size_t nlimbs = N->n;
816 const size_t tlimbs = 2 * (nlimbs + 1);
817
818 mbedtls_mpi_uint mm;
819 mbedtls_mpi_montg_init(&mm, N);
820
821 mbedtls_mpi RR, M_T;
822
823 mbedtls_mpi_init(&RR);
824 mbedtls_mpi_init(&M_T);
825
826 MBEDTLS_MPI_CHK(mbedtls_mpi_get_mont_r2_unsafe(&RR, N));
827 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
828
829 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
830 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
831
832 /* T = T * Vf mod N
833 * Reminder: montmul(A, B, N) = A * B * R^-1 mod N
834 * Usually both operands are multiplied by R mod N beforehand, yielding a
835 * result that's also * R mod N (aka "in the Montgomery domain"). Here we
836 * only multiply one operand by R mod N, so the result is directly what we
837 * want - no need to call `mpi_montred()` on it. */
838 mbedtls_mpi_montmul(T, &RR, N, mm, &M_T);
839 mbedtls_mpi_montmul(T, Vf, N, mm, &M_T);
840
841cleanup:
842
843 mbedtls_mpi_free(&RR);
844 mbedtls_mpi_free(&M_T);
845
846 return ret;
847}
848
849/*
Janos Follathe81102e2017-03-22 13:38:28 +0000850 * Exponent blinding supposed to prevent side-channel attacks using multiple
851 * traces of measurements to recover the RSA key. The more collisions are there,
852 * the more bits of the key can be recovered. See [3].
853 *
854 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case0e7791f2021-12-20 21:14:10 -0800855 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000856 *
857 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case0e7791f2021-12-20 21:14:10 -0800858 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000859 *
860 * (With the currently (as of 2017 April) known best algorithms breaking 2048
861 * bit RSA requires approximately as much time as trying out 2^112 random keys.
862 * Thus in this sense with 28 byte blinding the security is not reduced by
863 * side-channel attacks like the one in [3])
864 *
865 * This countermeasure does not help if the key recovery is possible with a
866 * single trace.
867 */
868#define RSA_EXPONENT_BLINDING 28
869
870/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 * Do an RSA private key operation
872 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100873int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
874 int (*f_rng)(void *, unsigned char *, size_t),
875 void *p_rng,
876 const unsigned char *input,
877 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000878{
Janos Follath24eed8d2019-11-22 13:21:35 +0000879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000880 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100881
882 /* Temporary holding the result */
883 mbedtls_mpi T;
884
885 /* Temporaries holding P-1, Q-1 and the
886 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000887 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100888
889#if !defined(MBEDTLS_RSA_NO_CRT)
890 /* Temporaries holding the results mod p resp. mod q. */
891 mbedtls_mpi TP, TQ;
892
893 /* Temporaries holding the blinded exponents for
894 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000895 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100896
897 /* Pointers to actual exponents to be used - either the unblinded
898 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000899 mbedtls_mpi *DP = &ctx->DP;
900 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100901#else
902 /* Temporary holding the blinded exponent (if used). */
903 mbedtls_mpi D_blind;
904
905 /* Pointer to actual exponent to be used - either the unblinded
906 * or the blinded one, depending on the presence of a PRNG. */
907 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100908#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100909
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100910 /* Temporaries holding the initial input and the double
911 * checked result; should be the same in the end. */
912 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100914 RSA_VALIDATE_RET(ctx != NULL);
915 RSA_VALIDATE_RET(input != NULL);
916 RSA_VALIDATE_RET(output != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 if (rsa_check_context(ctx, 1 /* private key checks */,
919 f_rng != NULL /* blinding y/n */) != 0) {
920 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +0100921 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100922
Hanno Becker06811ce2017-05-03 15:10:34 +0100923#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100924 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
925 return ret;
926 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100927#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000928
Hanno Becker06811ce2017-05-03 15:10:34 +0100929 /* MPI Initialization */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +0100931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100932 mbedtls_mpi_init(&P1);
933 mbedtls_mpi_init(&Q1);
934 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +0000935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100936 if (f_rng != NULL) {
Janos Follathe81102e2017-03-22 13:38:28 +0000937#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100938 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +0000939#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100940 mbedtls_mpi_init(&DP_blind);
941 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +0000942#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000943 }
Janos Follathe81102e2017-03-22 13:38:28 +0000944
Hanno Becker06811ce2017-05-03 15:10:34 +0100945#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100946 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200947#endif
948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100949 mbedtls_mpi_init(&I);
950 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +0100951
952 /* End of MPI initialization */
953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100954 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
955 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200956 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
957 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000958 }
959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100960 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +0100961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100962 if (f_rng != NULL) {
Paul Bakkerf451bac2013-08-30 15:37:02 +0200963 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200964 * Blinding
965 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200966 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100967 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
968 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
969 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +0000970
Janos Follathe81102e2017-03-22 13:38:28 +0000971 /*
972 * Exponent blinding
973 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100974 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
975 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +0000976
Janos Follathf9203b42017-03-22 15:13:15 +0000977#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000978 /*
979 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
980 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
982 f_rng, p_rng));
983 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
984 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
985 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +0000986
987 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000988#else
989 /*
990 * DP_blind = ( P - 1 ) * R + DP
991 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100992 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
993 f_rng, p_rng));
994 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
995 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
996 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +0000997
998 DP = &DP_blind;
999
1000 /*
1001 * DQ_blind = ( Q - 1 ) * R + DQ
1002 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001003 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1004 f_rng, p_rng));
1005 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1006 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1007 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001008
1009 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001010#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001011 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001014 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001015#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001016 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001017 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001019 * TP = input ^ dP mod P
1020 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001021 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001022
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001023 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1024 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
1026 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001027 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001029 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1030 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1031 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
1033 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001034 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001036 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1037 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001040 if (f_rng != NULL) {
Paul Bakkerf451bac2013-08-30 15:37:02 +02001041 /*
1042 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001043 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001044 */
Janos Follathaa6760d2024-01-08 15:09:34 +00001045 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
Paul Bakkerf451bac2013-08-30 15:37:02 +02001046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
Hanno Becker2dec5e82017-10-03 07:49:52 +01001048 /* Verify the result to prevent glitching attacks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001049 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1050 &ctx->N, &ctx->RN));
1051 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001052 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1053 goto cleanup;
1054 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001055
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 olen = ctx->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001057 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
1059cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001061 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1062 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1063 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001064#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001066 mbedtls_mpi_free(&P1);
1067 mbedtls_mpi_free(&Q1);
1068 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001070 if (f_rng != NULL) {
Janos Follathe81102e2017-03-22 13:38:28 +00001071#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001072 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001073#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001074 mbedtls_mpi_free(&DP_blind);
1075 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001076#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001077 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001079 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001080
1081#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001082 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001083#endif
1084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001085 mbedtls_mpi_free(&C);
1086 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001088 if (ret != 0 && ret >= -0x007f) {
1089 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1090 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001092 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001093}
1094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001096/**
1097 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1098 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001099 * \param dst buffer to mask
1100 * \param dlen length of destination buffer
1101 * \param src source of the mask generation
1102 * \param slen length of the source buffer
1103 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001105static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1106 size_t slen, mbedtls_md_context_t *md_ctx)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001109 unsigned char counter[4];
1110 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001111 unsigned int hlen;
1112 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001113 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001115 memset(mask, 0, MBEDTLS_MD_MAX_SIZE);
1116 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 hlen = mbedtls_md_get_size(md_ctx->md_info);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001119
Simon Butcher02037452016-03-01 21:19:12 +00001120 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001121 p = dst;
1122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001124 use_len = hlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001125 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001126 use_len = dlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001127 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001129 if ((ret = mbedtls_md_starts(md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001130 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001131 }
1132 if ((ret = mbedtls_md_update(md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001133 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001134 }
1135 if ((ret = mbedtls_md_update(md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001136 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001137 }
1138 if ((ret = mbedtls_md_finish(md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001139 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001140 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001143 *p++ ^= mask[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001144 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001145
1146 counter[3]++;
1147
1148 dlen -= use_len;
1149 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001150
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001151exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001152 mbedtls_platform_zeroize(mask, sizeof(mask));
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001154 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001159/*
1160 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1161 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001162int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1163 int (*f_rng)(void *, unsigned char *, size_t),
1164 void *p_rng,
1165 int mode,
1166 const unsigned char *label, size_t label_len,
1167 size_t ilen,
1168 const unsigned char *input,
1169 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001170{
1171 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001173 unsigned char *p = output;
1174 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 const mbedtls_md_info_t *md_info;
1176 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 RSA_VALIDATE_RET(ctx != NULL);
1179 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1180 mode == MBEDTLS_RSA_PUBLIC);
1181 RSA_VALIDATE_RET(output != NULL);
1182 RSA_VALIDATE_RET(ilen == 0 || input != NULL);
1183 RSA_VALIDATE_RET(label_len == 0 || label != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1186 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1187 }
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001189 if (f_rng == NULL) {
1190 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1191 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001193 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ctx->hash_id);
1194 if (md_info == NULL) {
1195 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1196 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001197
1198 olen = ctx->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001199 hlen = mbedtls_md_get_size(md_info);
Paul Bakkerb3869132013-02-28 17:21:01 +01001200
Simon Butcher02037452016-03-01 21:19:12 +00001201 /* first comparison checks for overflow */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001202 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1203 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1204 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001206 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001207
1208 *p++ = 0;
1209
Simon Butcher02037452016-03-01 21:19:12 +00001210 /* Generate a random octet string seed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001211 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1212 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1213 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001214
1215 p += hlen;
1216
Simon Butcher02037452016-03-01 21:19:12 +00001217 /* Construct DB */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001218 if ((ret = mbedtls_md(md_info, label, label_len, p)) != 0) {
1219 return ret;
1220 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001221 p += hlen;
1222 p += olen - 2 * hlen - 2 - ilen;
1223 *p++ = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001224 if (ilen != 0) {
1225 memcpy(p, input, ilen);
1226 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001228 mbedtls_md_init(&md_ctx);
1229 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001230 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001231 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001232
Simon Butcher02037452016-03-01 21:19:12 +00001233 /* maskedDB: Apply dbMask to DB */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001234 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1235 &md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001236 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001237 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001238
Simon Butcher02037452016-03-01 21:19:12 +00001239 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001240 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1241 &md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001242 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001243 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001244
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001245exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001246 mbedtls_md_free(&md_ctx);
Paul Bakkerb3869132013-02-28 17:21:01 +01001247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001248 if (ret != 0) {
1249 return ret;
1250 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001252 return (mode == MBEDTLS_RSA_PUBLIC)
1253 ? mbedtls_rsa_public(ctx, output, output)
1254 : mbedtls_rsa_private(ctx, f_rng, p_rng, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001259/*
1260 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1261 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001262int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1263 int (*f_rng)(void *, unsigned char *, size_t),
1264 void *p_rng,
1265 int mode, size_t ilen,
1266 const unsigned char *input,
1267 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001268{
1269 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001271 unsigned char *p = output;
1272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001273 RSA_VALIDATE_RET(ctx != NULL);
1274 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1275 mode == MBEDTLS_RSA_PUBLIC);
1276 RSA_VALIDATE_RET(output != NULL);
1277 RSA_VALIDATE_RET(ilen == 0 || input != NULL);
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001279 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1280 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1281 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001282
1283 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001284
Simon Butcher02037452016-03-01 21:19:12 +00001285 /* first comparison checks for overflow */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001286 if (ilen + 11 < ilen || olen < ilen + 11) {
1287 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1288 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001289
1290 nb_pad = olen - 3 - ilen;
1291
1292 *p++ = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001293 if (mode == MBEDTLS_RSA_PUBLIC) {
1294 if (f_rng == NULL) {
1295 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1296 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001300 while (nb_pad-- > 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001301 int rng_dl = 100;
1302
1303 do {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001304 ret = f_rng(p_rng, p, 1);
1305 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001306
Simon Butcher02037452016-03-01 21:19:12 +00001307 /* Check if RNG failed to generate data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001308 if (rng_dl == 0 || ret != 0) {
1309 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1310 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001311
1312 p++;
1313 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 while (nb_pad-- > 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001318 *p++ = 0xFF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001319 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001320 }
1321
1322 *p++ = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001323 if (ilen != 0) {
1324 memcpy(p, input, ilen);
1325 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001327 return (mode == MBEDTLS_RSA_PUBLIC)
1328 ? mbedtls_rsa_public(ctx, output, output)
1329 : mbedtls_rsa_private(ctx, f_rng, p_rng, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001330}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001332
Paul Bakker5121ce52009-01-03 21:22:43 +00001333/*
1334 * Add the message padding, then do an RSA operation
1335 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001336int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1337 int (*f_rng)(void *, unsigned char *, size_t),
1338 void *p_rng,
1339 int mode, size_t ilen,
1340 const unsigned char *input,
1341 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001342{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001343 RSA_VALIDATE_RET(ctx != NULL);
1344 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1345 mode == MBEDTLS_RSA_PUBLIC);
1346 RSA_VALIDATE_RET(output != NULL);
1347 RSA_VALIDATE_RET(ilen == 0 || input != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001349 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#if defined(MBEDTLS_PKCS1_V15)
1351 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001352 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng, mode, ilen,
1353 input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001354#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#if defined(MBEDTLS_PKCS1_V21)
1357 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001358 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, mode, NULL, 0,
1359 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001360#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
1362 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001363 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001365}
1366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001368/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001369 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001371int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1372 int (*f_rng)(void *, unsigned char *, size_t),
1373 void *p_rng,
1374 int mode,
1375 const unsigned char *label, size_t label_len,
1376 size_t *olen,
1377 const unsigned char *input,
1378 unsigned char *output,
1379 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001380{
Janos Follath24eed8d2019-11-22 13:21:35 +00001381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001382 size_t ilen, i, pad_len;
Dave Rodgman954a2da2023-09-20 14:10:35 +01001383 unsigned char *p, pad_done;
1384 int bad;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1386 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001387 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 const mbedtls_md_info_t *md_info;
1389 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001391 RSA_VALIDATE_RET(ctx != NULL);
1392 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1393 mode == MBEDTLS_RSA_PUBLIC);
1394 RSA_VALIDATE_RET(output_max_len == 0 || output != NULL);
1395 RSA_VALIDATE_RET(label_len == 0 || label != NULL);
1396 RSA_VALIDATE_RET(input != NULL);
1397 RSA_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001398
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001399 /*
1400 * Parameters sanity checks
1401 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001402 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1403 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001405
1406 ilen = ctx->len;
1407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001408 if (ilen < 16 || ilen > sizeof(buf)) {
1409 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001412 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ctx->hash_id);
1413 if (md_info == NULL) {
1414 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1415 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001417 hlen = mbedtls_md_get_size(md_info);
Janos Follathc17cda12016-02-11 11:08:18 +00001418
1419 // checking for integer underflow
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001420 if (2 * hlen + 2 > ilen) {
1421 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1422 }
Janos Follathc17cda12016-02-11 11:08:18 +00001423
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001424 /*
1425 * RSA operation
1426 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001427 ret = (mode == MBEDTLS_RSA_PUBLIC)
1428 ? mbedtls_rsa_public(ctx, input, buf)
1429 : mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001431 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001432 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001433 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001435 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001436 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001437 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001438 mbedtls_md_init(&md_ctx);
1439 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
1440 mbedtls_md_free(&md_ctx);
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001441 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001442 }
1443
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001444 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001445 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1446 &md_ctx)) != 0 ||
1447 /* DB: Apply dbMask to maskedDB */
1448 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1449 &md_ctx)) != 0) {
1450 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001451 goto cleanup;
1452 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001454 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001455
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001456 /* Generate lHash */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001457 if ((ret = mbedtls_md(md_info, label, label_len, lhash)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001458 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001459 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001460
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001461 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001462 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001463 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001465 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001466
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001467 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001468
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001469 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001470
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001471 /* Check lHash */
Dave Rodgman954a2da2023-09-20 14:10:35 +01001472 bad |= mbedtls_ct_memcmp(lhash, p, hlen);
1473 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001474
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001475 /* Get zero-padding len, but always read till end of buffer
1476 * (minus one, for the 01 byte) */
1477 pad_len = 0;
1478 pad_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001479 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001480 pad_done |= p[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001481 pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001482 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001483
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001484 p += pad_len;
1485 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001486
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001487 /*
1488 * The only information "leaked" is whether the padding was correct or not
1489 * (eg, no data is copied if it was not correct). This meets the
1490 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1491 * the different error conditions.
1492 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001493 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001494 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1495 goto cleanup;
1496 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001498 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001499 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1500 goto cleanup;
1501 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001502
1503 *olen = ilen - (p - buf);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001504 if (*olen != 0) {
1505 memcpy(output, p, *olen);
1506 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001507 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001508
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001509cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001510 mbedtls_platform_zeroize(buf, sizeof(buf));
1511 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001513 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001518/*
1519 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1520 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001521int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1522 int (*f_rng)(void *, unsigned char *, size_t),
1523 void *p_rng,
1524 int mode,
1525 size_t *olen,
1526 const unsigned char *input,
1527 unsigned char *output,
1528 size_t output_max_len)
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001529{
1530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1531 size_t ilen;
1532 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001534 RSA_VALIDATE_RET(ctx != NULL);
1535 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1536 mode == MBEDTLS_RSA_PUBLIC);
1537 RSA_VALIDATE_RET(output_max_len == 0 || output != NULL);
1538 RSA_VALIDATE_RET(input != NULL);
1539 RSA_VALIDATE_RET(olen != NULL);
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001540
1541 ilen = ctx->len;
1542
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001543 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1544 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1545 }
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001547 if (ilen < 16 || ilen > sizeof(buf)) {
1548 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1549 }
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001551 ret = (mode == MBEDTLS_RSA_PUBLIC)
1552 ? mbedtls_rsa_public(ctx, input, buf)
1553 : mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001555 if (ret != 0) {
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001556 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001557 }
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001559 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(mode, buf, ilen,
1560 output, output_max_len, olen);
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001561
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001562cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001563 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001565 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
1569/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001570 * Do an RSA operation, then remove the message padding
1571 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001572int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1573 int (*f_rng)(void *, unsigned char *, size_t),
1574 void *p_rng,
1575 int mode, size_t *olen,
1576 const unsigned char *input,
1577 unsigned char *output,
1578 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001579{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001580 RSA_VALIDATE_RET(ctx != NULL);
1581 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1582 mode == MBEDTLS_RSA_PUBLIC);
1583 RSA_VALIDATE_RET(output_max_len == 0 || output != NULL);
1584 RSA_VALIDATE_RET(input != NULL);
1585 RSA_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001587 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#if defined(MBEDTLS_PKCS1_V15)
1589 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001590 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, mode, olen,
1591 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001592#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#if defined(MBEDTLS_PKCS1_V21)
1595 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001596 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, mode, NULL, 0,
1597 olen, input, output,
1598 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001599#endif
1600
1601 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001602 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001603 }
1604}
1605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001607static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1608 int (*f_rng)(void *, unsigned char *, size_t),
1609 void *p_rng,
1610 int mode,
1611 mbedtls_md_type_t md_alg,
1612 unsigned int hashlen,
1613 const unsigned char *hash,
1614 int saltlen,
1615 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001616{
1617 size_t olen;
1618 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001619 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001620 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001621 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001622 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 const mbedtls_md_info_t *md_info;
1624 mbedtls_md_context_t md_ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001625 RSA_VALIDATE_RET(ctx != NULL);
1626 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1627 mode == MBEDTLS_RSA_PUBLIC);
1628 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
1629 hashlen == 0) ||
1630 hash != NULL);
1631 RSA_VALIDATE_RET(sig != NULL);
Paul Bakkerb3869132013-02-28 17:21:01 +01001632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001633 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1634 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1635 }
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001637 if (f_rng == NULL) {
1638 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1639 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001640
1641 olen = ctx->len;
1642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001643 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001644 /* Gather length of hash to sign */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001645 md_info = mbedtls_md_info_from_type(md_alg);
1646 if (md_info == NULL) {
1647 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1648 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001650 hashlen = mbedtls_md_get_size(md_info);
Paul Bakkerb3869132013-02-28 17:21:01 +01001651 }
1652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001653 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ctx->hash_id);
1654 if (md_info == NULL) {
1655 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1656 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001658 hlen = mbedtls_md_get_size(md_info);
Paul Bakkerb3869132013-02-28 17:21:01 +01001659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001660 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1661 /* Calculate the largest possible salt length, up to the hash size.
1662 * Normally this is the hash length, which is the maximum salt length
1663 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1664 * enough room, use the maximum salt length that fits. The constraint is
1665 * that the hash length plus the salt length plus 2 bytes must be at most
1666 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1667 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001668 min_slen = hlen - 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001669 if (olen < hlen + min_slen + 2) {
1670 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1671 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001672 slen = hlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001673 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001674 slen = olen - hlen - 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001675 }
1676 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1677 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1678 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001679 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001680 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001682 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001683
Simon Butcher02037452016-03-01 21:19:12 +00001684 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001685 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001686 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001687 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001688
1689 /* Generate salt of length slen in place in the encoded message */
1690 salt = p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001691 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1692 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1693 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001694
Paul Bakkerb3869132013-02-28 17:21:01 +01001695 p += slen;
1696
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001697 mbedtls_md_init(&md_ctx);
1698 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001699 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001700 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001701
Simon Butcher02037452016-03-01 21:19:12 +00001702 /* Generate H = Hash( M' ) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001703 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001704 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001705 }
1706 if ((ret = mbedtls_md_update(&md_ctx, p, 8)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001707 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001708 }
1709 if ((ret = mbedtls_md_update(&md_ctx, hash, hashlen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001710 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001711 }
1712 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001713 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001714 }
1715 if ((ret = mbedtls_md_finish(&md_ctx, p)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001716 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001717 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001718
Simon Butcher02037452016-03-01 21:19:12 +00001719 /* Compensate for boundary condition when applying mask */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001720 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001721 offset = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001722 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001723
Simon Butcher02037452016-03-01 21:19:12 +00001724 /* maskedDB: Apply dbMask to DB */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001725 if ((ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
1726 &md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001727 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001728 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001729
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001730 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1731 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001732
1733 p += hlen;
1734 *p++ = 0xBC;
1735
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001736exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001737 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001739 if (ret != 0) {
1740 return ret;
1741 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001743 return (mode == MBEDTLS_RSA_PUBLIC)
1744 ? mbedtls_rsa_public(ctx, sig, sig)
1745 : mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001746}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001747
1748/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001749 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1750 * the option to pass in the salt length.
1751 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001752int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1753 int (*f_rng)(void *, unsigned char *, size_t),
1754 void *p_rng,
1755 mbedtls_md_type_t md_alg,
1756 unsigned int hashlen,
1757 const unsigned char *hash,
1758 int saltlen,
1759 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001760{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001761 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, md_alg,
1762 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001763}
1764
1765
1766/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001767 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1768 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001769int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1770 int (*f_rng)(void *, unsigned char *, size_t),
1771 void *p_rng,
1772 int mode,
1773 mbedtls_md_type_t md_alg,
1774 unsigned int hashlen,
1775 const unsigned char *hash,
1776 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001777{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001778 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, mode, md_alg,
1779 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001780}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001784/*
1785 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1786 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001787
1788/* Construct a PKCS v1.5 encoding of a hashed message
1789 *
1790 * This is used both for signature generation and verification.
1791 *
1792 * Parameters:
1793 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001794 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001795 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001796 * - hash: Buffer containing the hashed message or the raw data.
1797 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001798 * - dst: Buffer to hold the encoded message.
1799 *
1800 * Assumptions:
1801 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1802 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001803 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001804 *
1805 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001806static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1807 unsigned int hashlen,
1808 const unsigned char *hash,
1809 size_t dst_len,
1810 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001811{
1812 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001813 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001814 unsigned char *p = dst;
1815 const char *oid = NULL;
1816
1817 /* Are we signing hashed or raw data? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001818 if (md_alg != MBEDTLS_MD_NONE) {
1819 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1820 if (md_info == NULL) {
1821 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1822 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001823
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001824 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1825 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1826 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001828 hashlen = mbedtls_md_get_size(md_info);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001829
1830 /* Double-check that 8 + hashlen + oid_size can be used as a
1831 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001832 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001833 10 + hashlen < hashlen ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001834 10 + hashlen + oid_size < 10 + hashlen) {
1835 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1836 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001837
1838 /*
1839 * Static bounds check:
1840 * - Need 10 bytes for five tag-length pairs.
1841 * (Insist on 1-byte length encodings to protect against variants of
1842 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1843 * - Need hashlen bytes for hash
1844 * - Need oid_size bytes for hash alg OID.
1845 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001846 if (nb_pad < 10 + hashlen + oid_size) {
1847 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1848 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001849 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001850 } else {
1851 if (nb_pad < hashlen) {
1852 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1853 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001854
1855 nb_pad -= hashlen;
1856 }
1857
Hanno Becker2b2f8982017-09-27 17:10:03 +01001858 /* Need space for signature header and padding delimiter (3 bytes),
1859 * and 8 bytes for the minimal padding */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001860 if (nb_pad < 3 + 8) {
1861 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1862 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001863 nb_pad -= 3;
1864
1865 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001866 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001867
1868 /* Write signature header and padding */
1869 *p++ = 0;
1870 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001871 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001872 p += nb_pad;
1873 *p++ = 0;
1874
1875 /* Are we signing raw data? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001876 if (md_alg == MBEDTLS_MD_NONE) {
1877 memcpy(p, hash, hashlen);
1878 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001879 }
1880
1881 /* Signing hashed data, add corresponding ASN.1 structure
1882 *
1883 * DigestInfo ::= SEQUENCE {
1884 * digestAlgorithm DigestAlgorithmIdentifier,
1885 * digest Digest }
1886 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1887 * Digest ::= OCTET STRING
1888 *
1889 * Schematic:
1890 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1891 * TAG-NULL + LEN [ NULL ] ]
1892 * TAG-OCTET + LEN [ HASH ] ]
1893 */
1894 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001895 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001896 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001897 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001898 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001899 *p++ = (unsigned char) oid_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001900 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001901 p += oid_size;
1902 *p++ = MBEDTLS_ASN1_NULL;
1903 *p++ = 0x00;
1904 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001905 *p++ = (unsigned char) hashlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001906 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001907 p += hashlen;
1908
1909 /* Just a sanity-check, should be automatic
1910 * after the initial bounds check. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001911 if (p != dst + dst_len) {
1912 mbedtls_platform_zeroize(dst, dst_len);
1913 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001914 }
1915
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001916 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001917}
1918
Paul Bakkerb3869132013-02-28 17:21:01 +01001919/*
1920 * Do an RSA operation to sign the message digest
1921 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001922int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
1923 int (*f_rng)(void *, unsigned char *, size_t),
1924 void *p_rng,
1925 int mode,
1926 mbedtls_md_type_t md_alg,
1927 unsigned int hashlen,
1928 const unsigned char *hash,
1929 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001930{
Janos Follath24eed8d2019-11-22 13:21:35 +00001931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001932 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001934 RSA_VALIDATE_RET(ctx != NULL);
1935 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1936 mode == MBEDTLS_RSA_PUBLIC);
1937 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
1938 hashlen == 0) ||
1939 hash != NULL);
1940 RSA_VALIDATE_RET(sig != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001942 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1943 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1944 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001945
Hanno Beckerfdf38032017-09-06 12:35:55 +01001946 /*
1947 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1948 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001950 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
1951 ctx->len, sig)) != 0) {
1952 return ret;
1953 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001954
1955 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001956 * Call respective RSA primitive
1957 */
1958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001959 if (mode == MBEDTLS_RSA_PUBLIC) {
Hanno Beckerfdf38032017-09-06 12:35:55 +01001960 /* Skip verification on a public key operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001961 return mbedtls_rsa_public(ctx, sig, sig);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001962 }
1963
1964 /* Private key operation
1965 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001966 * In order to prevent Lenstra's attack, make the signature in a
1967 * temporary buffer and check it before returning it.
1968 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001970 sig_try = mbedtls_calloc(1, ctx->len);
1971 if (sig_try == NULL) {
1972 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00001973 }
1974
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001975 verif = mbedtls_calloc(1, ctx->len);
1976 if (verif == NULL) {
1977 mbedtls_free(sig_try);
1978 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
1979 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001981 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
1982 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
1983
1984 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001985 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1986 goto cleanup;
1987 }
1988
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001989 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001990
1991cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001992 mbedtls_platform_zeroize(sig_try, ctx->len);
1993 mbedtls_platform_zeroize(verif, ctx->len);
1994 mbedtls_free(sig_try);
1995 mbedtls_free(verif);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 if (ret != 0) {
1998 memset(sig, '!', ctx->len);
1999 }
2000 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002003
2004/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 * Do an RSA operation to sign the message digest
2006 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002007int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2008 int (*f_rng)(void *, unsigned char *, size_t),
2009 void *p_rng,
2010 int mode,
2011 mbedtls_md_type_t md_alg,
2012 unsigned int hashlen,
2013 const unsigned char *hash,
2014 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002015{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002016 RSA_VALIDATE_RET(ctx != NULL);
2017 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2018 mode == MBEDTLS_RSA_PUBLIC);
2019 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2020 hashlen == 0) ||
2021 hash != NULL);
2022 RSA_VALIDATE_RET(sig != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002023
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002024 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025#if defined(MBEDTLS_PKCS1_V15)
2026 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002027 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng, mode, md_alg,
2028 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002029#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#if defined(MBEDTLS_PKCS1_V21)
2032 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002033 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, mode, md_alg,
2034 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002035#endif
2036
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002038 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002040}
2041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002043/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002044 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002046int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2047 int (*f_rng)(void *, unsigned char *, size_t),
2048 void *p_rng,
2049 int mode,
2050 mbedtls_md_type_t md_alg,
2051 unsigned int hashlen,
2052 const unsigned char *hash,
2053 mbedtls_md_type_t mgf1_hash_id,
2054 int expected_salt_len,
2055 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002056{
Janos Follath24eed8d2019-11-22 13:21:35 +00002057 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002058 size_t siglen;
2059 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002060 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002062 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002063 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002064 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 const mbedtls_md_info_t *md_info;
2066 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002067 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002069 RSA_VALIDATE_RET(ctx != NULL);
2070 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2071 mode == MBEDTLS_RSA_PUBLIC);
2072 RSA_VALIDATE_RET(sig != NULL);
2073 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2074 hashlen == 0) ||
2075 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002077 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2078 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2079 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002080
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 siglen = ctx->len;
2082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002083 if (siglen < 16 || siglen > sizeof(buf)) {
2084 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2085 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002087 ret = (mode == MBEDTLS_RSA_PUBLIC)
2088 ? mbedtls_rsa_public(ctx, sig, buf)
2089 : mbedtls_rsa_private(ctx, f_rng, p_rng, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002091 if (ret != 0) {
2092 return ret;
2093 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002094
2095 p = buf;
2096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002097 if (buf[siglen - 1] != 0xBC) {
2098 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002099 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 if (md_alg != MBEDTLS_MD_NONE) {
2102 /* Gather length of hash to sign */
2103 md_info = mbedtls_md_info_from_type(md_alg);
2104 if (md_info == NULL) {
2105 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2106 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002108 hashlen = mbedtls_md_get_size(md_info);
2109 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002111 md_info = mbedtls_md_info_from_type(mgf1_hash_id);
2112 if (md_info == NULL) {
2113 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2114 }
2115
2116 hlen = mbedtls_md_get_size(md_info);
2117
2118 memset(zeros, 0, 8);
Paul Bakker53019ae2011-03-25 13:58:48 +00002119
Simon Butcher02037452016-03-01 21:19:12 +00002120 /*
2121 * Note: EMSA-PSS verification is over the length of N - 1 bits
2122 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002123 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002125 if (buf[0] >> (8 - siglen * 8 + msb)) {
2126 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2127 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002128
Simon Butcher02037452016-03-01 21:19:12 +00002129 /* Compensate for boundary condition when applying mask */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002130 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002131 p++;
2132 siglen -= 1;
2133 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002135 if (siglen < hlen + 2) {
2136 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2137 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002138 hash_start = p + siglen - hlen - 1;
2139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002140 mbedtls_md_init(&md_ctx);
2141 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002142 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002145 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, &md_ctx);
2146 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002147 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002148 }
Paul Bakker02303e82013-01-03 11:08:31 +01002149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002150 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002153 p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002154 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002156 if (*p++ != 0x01) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002157 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2158 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002159 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002160
Gilles Peskine6a54b022017-10-17 19:02:13 +02002161 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002163 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2164 observed_salt_len != (size_t) expected_salt_len) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002165 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2166 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002167 }
2168
Simon Butcher02037452016-03-01 21:19:12 +00002169 /*
2170 * Generate H = Hash( M' )
2171 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002172 ret = mbedtls_md_starts(&md_ctx);
2173 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002174 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002175 }
2176 ret = mbedtls_md_update(&md_ctx, zeros, 8);
2177 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002178 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002179 }
2180 ret = mbedtls_md_update(&md_ctx, hash, hashlen);
2181 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002182 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002183 }
2184 ret = mbedtls_md_update(&md_ctx, p, observed_salt_len);
2185 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002186 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002187 }
2188 ret = mbedtls_md_finish(&md_ctx, result);
2189 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002190 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002191 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002193 if (memcmp(hash_start, result, hlen) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002194 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002195 goto exit;
2196 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002197
2198exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002199 mbedtls_md_free(&md_ctx);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002201 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002202}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002203
2204/*
2205 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2206 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002207int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2208 int (*f_rng)(void *, unsigned char *, size_t),
2209 void *p_rng,
2210 int mode,
2211 mbedtls_md_type_t md_alg,
2212 unsigned int hashlen,
2213 const unsigned char *hash,
2214 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002215{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002216 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002217 RSA_VALIDATE_RET(ctx != NULL);
2218 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2219 mode == MBEDTLS_RSA_PUBLIC);
2220 RSA_VALIDATE_RET(sig != NULL);
2221 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2222 hashlen == 0) ||
2223 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002225 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002227 : md_alg;
2228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002229 return mbedtls_rsa_rsassa_pss_verify_ext(ctx, f_rng, p_rng, mode,
2230 md_alg, hashlen, hash,
2231 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
2232 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002233
2234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002238/*
2239 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2240 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2242 int (*f_rng)(void *, unsigned char *, size_t),
2243 void *p_rng,
2244 int mode,
2245 mbedtls_md_type_t md_alg,
2246 unsigned int hashlen,
2247 const unsigned char *hash,
2248 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002249{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002250 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002251 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002252 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002254 RSA_VALIDATE_RET(ctx != NULL);
2255 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2256 mode == MBEDTLS_RSA_PUBLIC);
2257 RSA_VALIDATE_RET(sig != NULL);
2258 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2259 hashlen == 0) ||
2260 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002261
2262 sig_len = ctx->len;
2263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002264 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2265 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2266 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002267
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002268 /*
2269 * Prepare expected PKCS1 v1.5 encoding of hash.
2270 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002272 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2273 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002274 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2275 goto cleanup;
2276 }
2277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002278 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2279 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002280 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002282
2283 /*
2284 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2285 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002287 ret = (mode == MBEDTLS_RSA_PUBLIC)
2288 ? mbedtls_rsa_public(ctx, sig, encoded)
2289 : mbedtls_rsa_private(ctx, f_rng, p_rng, sig, encoded);
2290 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002291 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002292 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002293
Simon Butcher02037452016-03-01 21:19:12 +00002294 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002295 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002296 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002298 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2299 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002300 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2301 goto cleanup;
2302 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002303
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002304cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002306 if (encoded != NULL) {
2307 mbedtls_platform_zeroize(encoded, sig_len);
2308 mbedtls_free(encoded);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002309 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002311 if (encoded_expected != NULL) {
2312 mbedtls_platform_zeroize(encoded_expected, sig_len);
2313 mbedtls_free(encoded_expected);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002314 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002316 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002317}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002319
2320/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002321 * Do an RSA operation and check the message digest
2322 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002323int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2324 int (*f_rng)(void *, unsigned char *, size_t),
2325 void *p_rng,
2326 int mode,
2327 mbedtls_md_type_t md_alg,
2328 unsigned int hashlen,
2329 const unsigned char *hash,
2330 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002331{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002332 RSA_VALIDATE_RET(ctx != NULL);
2333 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2334 mode == MBEDTLS_RSA_PUBLIC);
2335 RSA_VALIDATE_RET(sig != NULL);
2336 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2337 hashlen == 0) ||
2338 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002340 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341#if defined(MBEDTLS_PKCS1_V15)
2342 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002343 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, f_rng, p_rng, mode, md_alg,
2344 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002345#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#if defined(MBEDTLS_PKCS1_V21)
2348 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349 return mbedtls_rsa_rsassa_pss_verify(ctx, f_rng, p_rng, mode, md_alg,
2350 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002351#endif
2352
2353 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002354 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002355 }
2356}
2357
2358/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002359 * Copy the components of an RSA key
2360 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002361int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002362{
Janos Follath24eed8d2019-11-22 13:21:35 +00002363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 RSA_VALIDATE_RET(dst != NULL);
2365 RSA_VALIDATE_RET(src != NULL);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002366
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002367 dst->len = src->len;
2368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002369 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2370 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002372 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2373 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2374 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002375
2376#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002377 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2378 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2379 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2380 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2381 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002382#endif
2383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002384 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002386 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2387 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002388
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002389 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002390 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002391
2392cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393 if (ret != 0) {
2394 mbedtls_rsa_free(dst);
2395 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002397 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002398}
2399
2400/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 * Free the components of an RSA key
2402 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002404{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002405 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002406 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002407 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002409 mbedtls_mpi_free(&ctx->Vi);
2410 mbedtls_mpi_free(&ctx->Vf);
2411 mbedtls_mpi_free(&ctx->RN);
2412 mbedtls_mpi_free(&ctx->D);
2413 mbedtls_mpi_free(&ctx->Q);
2414 mbedtls_mpi_free(&ctx->P);
2415 mbedtls_mpi_free(&ctx->E);
2416 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002417
Hanno Becker33c30a02017-08-23 07:00:22 +01002418#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002419 mbedtls_mpi_free(&ctx->RQ);
2420 mbedtls_mpi_free(&ctx->RP);
2421 mbedtls_mpi_free(&ctx->QP);
2422 mbedtls_mpi_free(&ctx->DQ);
2423 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002424#endif /* MBEDTLS_RSA_NO_CRT */
2425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002427 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002428 if (ctx->ver != 0) {
2429 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002430 ctx->ver = 0;
2431 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002432#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002433}
2434
Hanno Beckerab377312017-08-23 16:24:51 +01002435#endif /* !MBEDTLS_RSA_ALT */
2436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002439#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
2441/*
2442 * Example RSA-1024 keypair, for test purposes
2443 */
2444#define KEY_LEN 128
2445
2446#define RSA_N "9292758453063D803DD603D5E777D788" \
2447 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2448 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2449 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2450 "93A89813FBF3C4F8066D2D800F7C38A8" \
2451 "1AE31942917403FF4946B0A83D3D3E05" \
2452 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2453 "5E94BB77B07507233A0BC7BAC8F90F79"
2454
2455#define RSA_E "10001"
2456
2457#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2458 "66CA472BC44D253102F8B4A9D3BFA750" \
2459 "91386C0077937FE33FA3252D28855837" \
2460 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2461 "DF79C5CE07EE72C7F123142198164234" \
2462 "CABB724CF78B8173B9F880FC86322407" \
2463 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2464 "071513A1E85B5DFA031F21ECAE91A34D"
2465
2466#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2467 "2C01CAD19EA484A87EA4377637E75500" \
2468 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2469 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2470
2471#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2472 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2473 "910E4168387E3C30AA1E00C339A79508" \
2474 "8452DD96A9A5EA5D9DCA68DA636032AF"
2475
Paul Bakker5121ce52009-01-03 21:22:43 +00002476#define PT_LEN 24
2477#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2478 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002481static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002482{
gufe44c2620da2020-08-03 17:56:50 +02002483#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002484 size_t i;
2485
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002486 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002487 rng_state = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002488 }
Paul Bakker545570e2010-07-18 09:00:25 +00002489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002490 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002491 output[i] = rand();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002492 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002493#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002494 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002495 rng_state = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002496 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002498 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002499#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002501 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002504
Paul Bakker5121ce52009-01-03 21:22:43 +00002505/*
2506 * Checkup routine
2507 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002508int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002509{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002510 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002512 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 unsigned char rsa_plaintext[PT_LEN];
2515 unsigned char rsa_decrypted[PT_LEN];
2516 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002518 unsigned char sha1sum[20];
2519#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
Hanno Becker3a701162017-08-22 13:52:43 +01002521 mbedtls_mpi K;
2522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523 mbedtls_mpi_init(&K);
2524 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002526 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2527 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2528 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2529 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2530 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2531 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2532 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2533 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2534 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2535 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002537 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002539 if (verbose != 0) {
2540 mbedtls_printf(" RSA key validation: ");
2541 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002543 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2544 mbedtls_rsa_check_privkey(&rsa) != 0) {
2545 if (verbose != 0) {
2546 mbedtls_printf("failed\n");
2547 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
Hanno Becker5bc87292017-05-03 15:09:31 +01002549 ret = 1;
2550 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 }
2552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002553 if (verbose != 0) {
2554 mbedtls_printf("passed\n PKCS#1 encryption : ");
2555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002557 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002559 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2560 PT_LEN, rsa_plaintext,
2561 rsa_ciphertext) != 0) {
2562 if (verbose != 0) {
2563 mbedtls_printf("failed\n");
2564 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Hanno Becker5bc87292017-05-03 15:09:31 +01002566 ret = 1;
2567 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 }
2569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002570 if (verbose != 0) {
2571 mbedtls_printf("passed\n PKCS#1 decryption : ");
2572 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002574 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2575 &len, rsa_ciphertext, rsa_decrypted,
2576 sizeof(rsa_decrypted)) != 0) {
2577 if (verbose != 0) {
2578 mbedtls_printf("failed\n");
2579 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
Hanno Becker5bc87292017-05-03 15:09:31 +01002581 ret = 1;
2582 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 }
2584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002585 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2586 if (verbose != 0) {
2587 mbedtls_printf("failed\n");
2588 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
Hanno Becker5bc87292017-05-03 15:09:31 +01002590 ret = 1;
2591 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 }
2593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002594 if (verbose != 0) {
2595 mbedtls_printf("passed\n");
2596 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002599 if (verbose != 0) {
2600 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002603 if (mbedtls_sha1_ret(rsa_plaintext, PT_LEN, sha1sum) != 0) {
2604 if (verbose != 0) {
2605 mbedtls_printf("failed\n");
2606 }
2607
2608 return 1;
2609 }
2610
2611 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2612 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2613 sha1sum, rsa_ciphertext) != 0) {
2614 if (verbose != 0) {
2615 mbedtls_printf("failed\n");
2616 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002617
Hanno Becker5bc87292017-05-03 15:09:31 +01002618 ret = 1;
2619 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 }
2621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002622 if (verbose != 0) {
2623 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2624 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002626 if (mbedtls_rsa_pkcs1_verify(&rsa, NULL, NULL,
2627 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2628 sha1sum, rsa_ciphertext) != 0) {
2629 if (verbose != 0) {
2630 mbedtls_printf("failed\n");
2631 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Hanno Becker5bc87292017-05-03 15:09:31 +01002633 ret = 1;
2634 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 }
2636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002637 if (verbose != 0) {
2638 mbedtls_printf("passed\n");
2639 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002642 if (verbose != 0) {
2643 mbedtls_printf("\n");
2644 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002645
Paul Bakker3d8fb632014-04-17 12:42:41 +02002646cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002647 mbedtls_mpi_free(&K);
2648 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002650 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002652 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002653}
2654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#endif /* MBEDTLS_RSA_C */