blob: 84403c45795328d7a25a87167b2d54fd12197445 [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"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000043
gufe44c2620da2020-08-03 17:56:50 +020044#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000045#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010049
Hanno Beckera565f542017-10-11 11:00:19 +010050#if !defined(MBEDTLS_RSA_ALT)
51
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050052/* Parameter validation macros */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053#define RSA_VALIDATE_RET(cond) \
54 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA)
55#define RSA_VALIDATE(cond) \
56 MBEDTLS_INTERNAL_VALIDATE(cond)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010058int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
59 const mbedtls_mpi *N,
60 const mbedtls_mpi *P, const mbedtls_mpi *Q,
61 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +010062{
Janos Follath24eed8d2019-11-22 13:21:35 +000063 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +010065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
67 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
68 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
69 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
70 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
71 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +010072 }
73
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 if (N != NULL) {
75 ctx->len = mbedtls_mpi_size(&ctx->N);
76 }
Hanno Becker617c1ae2017-08-23 14:11:24 +010077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +010079}
80
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
82 unsigned char const *N, size_t N_len,
83 unsigned char const *P, size_t P_len,
84 unsigned char const *Q, size_t Q_len,
85 unsigned char const *D, size_t D_len,
86 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +010087{
Hanno Beckerd4d60572018-01-10 07:12:01 +000088 int ret = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +010090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 if (N != NULL) {
92 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
93 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +010094 }
95
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 if (P != NULL) {
97 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
98 }
Hanno Becker617c1ae2017-08-23 14:11:24 +010099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 if (Q != NULL) {
101 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
102 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 if (D != NULL) {
105 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
106 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 if (E != NULL) {
109 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
110 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100111
112cleanup:
113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 if (ret != 0) {
115 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
116 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100119}
120
Hanno Becker705fc682017-10-10 17:57:02 +0100121/*
122 * Checks whether the context fields are set in such a way
123 * that the RSA primitives will be able to execute without error.
124 * It does *not* make guarantees for consistency of the parameters.
125 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
127 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100128{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100129#if !defined(MBEDTLS_RSA_NO_CRT)
130 /* blinding_needed is only used for NO_CRT to decide whether
131 * P,Q need to be present or not. */
132 ((void) blinding_needed);
133#endif
134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
136 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
137 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000138 }
Hanno Becker705fc682017-10-10 17:57:02 +0100139
140 /*
141 * 1. Modular exponentiation needs positive, odd moduli.
142 */
143
144 /* Modular exponentiation wrt. N is always used for
145 * RSA public key operations. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
147 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
148 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100149 }
150
151#if !defined(MBEDTLS_RSA_NO_CRT)
152 /* Modular exponentiation for P and Q is only
153 * used for private key operations and if CRT
154 * is used. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 if (is_priv &&
156 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
157 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
158 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
159 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
160 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100161 }
162#endif /* !MBEDTLS_RSA_NO_CRT */
163
164 /*
165 * 2. Exponents must be positive
166 */
167
168 /* Always need E for public key operations */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
170 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
171 }
Hanno Becker705fc682017-10-10 17:57:02 +0100172
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100173#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100174 /* For private key operations, use D or DP & DQ
175 * as (unblinded) exponents. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
177 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
178 }
Hanno Becker705fc682017-10-10 17:57:02 +0100179#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 if (is_priv &&
181 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
182 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
183 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100184 }
185#endif /* MBEDTLS_RSA_NO_CRT */
186
187 /* Blinding shouldn't make exponents negative either,
188 * so check that P, Q >= 1 if that hasn't yet been
189 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100190#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 if (is_priv && blinding_needed &&
192 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
193 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
194 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100195 }
196#endif
197
198 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100199 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100200#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 if (is_priv &&
202 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
203 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100204 }
205#endif
206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100208}
209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100211{
212 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500213 int have_N, have_P, have_Q, have_D, have_E;
214#if !defined(MBEDTLS_RSA_NO_CRT)
215 int have_DP, have_DQ, have_QP;
216#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500217 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 RSA_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
222 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
223 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
224 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
225 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500226
227#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
229 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
230 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500231#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100232
Hanno Becker617c1ae2017-08-23 14:11:24 +0100233 /*
234 * Check whether provided parameters are enough
235 * to deduce all others. The following incomplete
236 * parameter sets for private keys are supported:
237 *
238 * (1) P, Q missing.
239 * (2) D and potentially N missing.
240 *
241 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100242
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500243 n_missing = have_P && have_Q && have_D && have_E;
244 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
245 d_missing = have_P && have_Q && !have_D && have_E;
246 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100247
248 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500249 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 if (!is_priv && !is_pub) {
252 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
253 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100254
255 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100256 * Step 1: Deduce N if P, Q are provided.
257 */
258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 if (!have_N && have_P && have_Q) {
260 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
261 &ctx->Q)) != 0) {
262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100263 }
264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100266 }
267
268 /*
269 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100270 */
271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 if (pq_missing) {
273 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
274 &ctx->P, &ctx->Q);
275 if (ret != 0) {
276 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
277 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 } else if (d_missing) {
280 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
281 &ctx->Q,
282 &ctx->E,
283 &ctx->D)) != 0) {
284 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100285 }
286 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100287
Hanno Becker617c1ae2017-08-23 14:11:24 +0100288 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100289 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100290 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100291 */
292
Hanno Becker23344b52017-08-23 07:43:27 +0100293#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 if (is_priv && !(have_DP && have_DQ && have_QP)) {
295 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
296 &ctx->DP, &ctx->DQ, &ctx->QP);
297 if (ret != 0) {
298 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
299 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100300 }
Hanno Becker23344b52017-08-23 07:43:27 +0100301#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100302
303 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100304 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100305 */
306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100308}
309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
311 unsigned char *N, size_t N_len,
312 unsigned char *P, size_t P_len,
313 unsigned char *Q, size_t Q_len,
314 unsigned char *D, size_t D_len,
315 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100316{
317 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500318 int is_priv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100320
321 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500322 is_priv =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
324 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
325 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
326 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
327 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100330 /* If we're trying to export private parameters for a public key,
331 * something must be wrong. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 if (P != NULL || Q != NULL || D != NULL) {
333 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
334 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100335
336 }
337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 if (N != NULL) {
339 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
340 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 if (P != NULL) {
343 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
344 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 if (Q != NULL) {
347 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
348 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 if (D != NULL) {
351 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
352 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 if (E != NULL) {
355 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
356 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100357
358cleanup:
359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100361}
362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
364 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
365 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100366{
Janos Follath24eed8d2019-11-22 13:21:35 +0000367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500368 int is_priv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100370
371 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500372 is_priv =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
374 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
375 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
376 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
377 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100380 /* If we're trying to export private parameters for a public key,
381 * something must be wrong. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 if (P != NULL || Q != NULL || D != NULL) {
383 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
384 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100385
386 }
387
388 /* Export all requested core parameters. */
389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
391 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
392 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
393 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
394 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
395 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100396 }
397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100399}
400
401/*
402 * Export CRT parameters
403 * This must also be implemented if CRT is not used, for being able to
404 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
405 * can be used in this case.
406 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
408 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100409{
Janos Follath24eed8d2019-11-22 13:21:35 +0000410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500411 int is_priv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100413
414 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500415 is_priv =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
417 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
418 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
419 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
420 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 if (!is_priv) {
423 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
424 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100425
Hanno Beckerdc95c892017-08-23 06:57:02 +0100426#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100427 /* Export all requested blinding parameters. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
429 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
430 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
431 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100432 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100433#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
435 DP, DQ, QP)) != 0) {
436 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100437 }
438#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100441}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100442
Paul Bakker5121ce52009-01-03 21:22:43 +0000443/*
444 * Initialize an RSA context
445 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446void mbedtls_rsa_init(mbedtls_rsa_context *ctx,
447 int padding,
448 int hash_id)
Paul Bakker5121ce52009-01-03 21:22:43 +0000449{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 RSA_VALIDATE(ctx != NULL);
451 RSA_VALIDATE(padding == MBEDTLS_RSA_PKCS_V15 ||
452 padding == MBEDTLS_RSA_PKCS_V21);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 mbedtls_rsa_set_padding(ctx, padding, hash_id);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100459 /* Set ctx->ver to nonzero to indicate that the mutex has been
460 * initialized and will need to be freed. */
461 ctx->ver = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200463#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000464}
465
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100466/*
467 * Set padding for an existing RSA context
468 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
470 int hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100471{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 RSA_VALIDATE(ctx != NULL);
473 RSA_VALIDATE(padding == MBEDTLS_RSA_PKCS_V15 ||
474 padding == MBEDTLS_RSA_PKCS_V21);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500475
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100476 ctx->padding = padding;
477 ctx->hash_id = hash_id;
478}
479
Hanno Becker617c1ae2017-08-23 14:11:24 +0100480/*
481 * Get length in bytes of RSA modulus
482 */
483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100485{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100487}
488
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492/*
493 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800494 *
495 * This generation method follows the RSA key pair generation procedure of
496 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
499 int (*f_rng)(void *, unsigned char *, size_t),
500 void *p_rng,
501 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000502{
Janos Follath24eed8d2019-11-22 13:21:35 +0000503 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800504 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100505 int prime_quality = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 RSA_VALIDATE_RET(ctx != NULL);
507 RSA_VALIDATE_RET(f_rng != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
Janos Follathb8fc1b02018-09-03 15:37:01 +0100509 /*
510 * If the modulus is 1024 bit long or shorter, then the security strength of
511 * the RSA algorithm is less than or equal to 80 bits and therefore an error
512 * rate of 2^-80 is sufficient.
513 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100514 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100515 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100518 mbedtls_mpi_init(&H);
519 mbedtls_mpi_init(&G);
520 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100522 if (nbits < 128 || exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100523 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
524 goto cleanup;
525 }
526
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 /*
528 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800529 * 1. |P-Q| > 2^( nbits / 2 - 100 )
530 * 2. GCD( E, (P-1)*(Q-1) ) == 1
531 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100535 do {
536 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
537 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100539 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
540 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800542 /* 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 +0100543 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
544 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800548 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100549 if (H.s < 0) {
550 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
551 }
Janos Follathef441782016-09-21 13:18:12 +0100552
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100553 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
555 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
556 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800557
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800558 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100559 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
560 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800561 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800563
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800564 /* 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 +0100565 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
566 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
567 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569 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 -0800570 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800572
573 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100576 /* Restore P,Q */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100577 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
578 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100583
Jethro Beekman97f95c92018-02-13 15:50:36 -0800584#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 * DP = D mod (P - 1)
587 * DQ = D mod (Q - 1)
588 * QP = Q^-1 mod P
589 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100590 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
591 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100592#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
Hanno Becker83aad1f2017-08-23 06:45:10 +0100594 /* Double-check */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
597cleanup:
598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100599 mbedtls_mpi_free(&H);
600 mbedtls_mpi_free(&G);
601 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100603 if (ret != 0) {
604 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100606 if ((-ret & ~0x7f) == 0) {
607 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
608 }
609 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 }
611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100612 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000613}
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
617/*
618 * Check a public RSA key
619 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100620int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000621{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100622 RSA_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
625 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100626 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100628 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
629 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100630 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100632 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
633 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
634 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
635 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
636 }
637
638 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000639}
640
641/*
Hanno Becker705fc682017-10-10 17:57:02 +0100642 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000645{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100646 RSA_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100648 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
649 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
650 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 }
Paul Bakker48377d92013-08-30 12:06:24 +0200652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100653 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
654 &ctx->D, &ctx->E, NULL, NULL) != 0) {
655 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000657
Hanno Beckerb269a852017-08-25 08:03:21 +0100658#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
660 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
661 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100662 }
663#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000666}
667
668/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100669 * Check if contexts holding a public and private key match
670 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100671int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
672 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100673{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100674 RSA_VALIDATE_RET(pub != NULL);
675 RSA_VALIDATE_RET(prv != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100677 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
678 mbedtls_rsa_check_privkey(prv) != 0) {
679 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100680 }
681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100682 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
683 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
684 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100685 }
686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100687 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100688}
689
690/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 * Do an RSA public key operation
692 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
694 const unsigned char *input,
695 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000696{
Janos Follath24eed8d2019-11-22 13:21:35 +0000697 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000698 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_mpi T;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100700 RSA_VALIDATE_RET(ctx != NULL);
701 RSA_VALIDATE_RET(input != NULL);
702 RSA_VALIDATE_RET(output != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100704 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
705 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
706 }
Hanno Becker705fc682017-10-10 17:57:02 +0100707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100708 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000709
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200710#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
712 return ret;
713 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200714#endif
715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100716 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100718 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200719 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
720 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 }
722
723 olen = ctx->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100724 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
725 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000726
727cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100729 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
730 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
731 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100732#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100734 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100736 if (ret != 0) {
737 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
738 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100740 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000741}
742
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200743/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200744 * Generate or update blinding values, see section 10 of:
745 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200746 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200747 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200748 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
750 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200751{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200752 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200753 mbedtls_mpi R;
754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100755 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100757 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200758 /* We already have blinding values, just update them by squaring */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100759 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
760 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
761 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
762 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200763
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200764 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200765 }
766
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200767 /* Unblinding value: Vf = random number, invertible mod N */
768 do {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100769 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200770 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
771 goto cleanup;
772 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100774 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 +0200775
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200776 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100777 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
778 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
779 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200780
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200781 /* At this point, Vi is invertible mod N if and only if both Vf and R
782 * are invertible mod N. If one of them isn't, we don't need to know
783 * which one, we just loop and choose new values for both of them.
784 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100785 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
786 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200787 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100788 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500791
792 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100793 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
794 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200795
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200796 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200797 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 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 +0200799
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200800
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200801cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100802 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100804 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200805}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200806
Paul Bakker5121ce52009-01-03 21:22:43 +0000807/*
Janos Follathe81102e2017-03-22 13:38:28 +0000808 * Exponent blinding supposed to prevent side-channel attacks using multiple
809 * traces of measurements to recover the RSA key. The more collisions are there,
810 * the more bits of the key can be recovered. See [3].
811 *
812 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case0e7791f2021-12-20 21:14:10 -0800813 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000814 *
815 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case0e7791f2021-12-20 21:14:10 -0800816 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000817 *
818 * (With the currently (as of 2017 April) known best algorithms breaking 2048
819 * bit RSA requires approximately as much time as trying out 2^112 random keys.
820 * Thus in this sense with 28 byte blinding the security is not reduced by
821 * side-channel attacks like the one in [3])
822 *
823 * This countermeasure does not help if the key recovery is possible with a
824 * single trace.
825 */
826#define RSA_EXPONENT_BLINDING 28
827
828/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 * Do an RSA private key operation
830 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100831int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
832 int (*f_rng)(void *, unsigned char *, size_t),
833 void *p_rng,
834 const unsigned char *input,
835 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000836{
Janos Follath24eed8d2019-11-22 13:21:35 +0000837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000838 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100839
840 /* Temporary holding the result */
841 mbedtls_mpi T;
842
843 /* Temporaries holding P-1, Q-1 and the
844 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000845 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100846
847#if !defined(MBEDTLS_RSA_NO_CRT)
848 /* Temporaries holding the results mod p resp. mod q. */
849 mbedtls_mpi TP, TQ;
850
851 /* Temporaries holding the blinded exponents for
852 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000853 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100854
855 /* Pointers to actual exponents to be used - either the unblinded
856 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000857 mbedtls_mpi *DP = &ctx->DP;
858 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100859#else
860 /* Temporary holding the blinded exponent (if used). */
861 mbedtls_mpi D_blind;
862
863 /* Pointer to actual exponent to be used - either the unblinded
864 * or the blinded one, depending on the presence of a PRNG. */
865 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100866#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100867
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100868 /* Temporaries holding the initial input and the double
869 * checked result; should be the same in the end. */
870 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100872 RSA_VALIDATE_RET(ctx != NULL);
873 RSA_VALIDATE_RET(input != NULL);
874 RSA_VALIDATE_RET(output != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100876 if (rsa_check_context(ctx, 1 /* private key checks */,
877 f_rng != NULL /* blinding y/n */) != 0) {
878 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +0100879 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100880
Hanno Becker06811ce2017-05-03 15:10:34 +0100881#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100882 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
883 return ret;
884 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100885#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000886
Hanno Becker06811ce2017-05-03 15:10:34 +0100887 /* MPI Initialization */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +0100889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100890 mbedtls_mpi_init(&P1);
891 mbedtls_mpi_init(&Q1);
892 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +0000893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 if (f_rng != NULL) {
Janos Follathe81102e2017-03-22 13:38:28 +0000895#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100896 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +0000897#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100898 mbedtls_mpi_init(&DP_blind);
899 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +0000900#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000901 }
Janos Follathe81102e2017-03-22 13:38:28 +0000902
Hanno Becker06811ce2017-05-03 15:10:34 +0100903#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100904 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200905#endif
906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100907 mbedtls_mpi_init(&I);
908 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +0100909
910 /* End of MPI initialization */
911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100912 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
913 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200914 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
915 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000916 }
917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +0100919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100920 if (f_rng != NULL) {
Paul Bakkerf451bac2013-08-30 15:37:02 +0200921 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200922 * Blinding
923 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200924 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100925 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
926 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
927 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +0000928
Janos Follathe81102e2017-03-22 13:38:28 +0000929 /*
930 * Exponent blinding
931 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100932 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
933 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +0000934
Janos Follathf9203b42017-03-22 15:13:15 +0000935#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000936 /*
937 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
938 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100939 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
940 f_rng, p_rng));
941 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
942 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
943 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +0000944
945 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000946#else
947 /*
948 * DP_blind = ( P - 1 ) * R + DP
949 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
951 f_rng, p_rng));
952 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
953 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
954 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +0000955
956 DP = &DP_blind;
957
958 /*
959 * DQ_blind = ( Q - 1 ) * R + DQ
960 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100961 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
962 f_rng, p_rng));
963 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
964 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
965 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +0000966
967 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000968#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200969 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100972 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100973#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200974 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000975 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100977 * TP = input ^ dP mod P
978 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
982 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
984 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100985 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100987 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
988 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
989 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +0000990
991 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100992 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100994 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
995 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100998 if (f_rng != NULL) {
Paul Bakkerf451bac2013-08-30 15:37:02 +0200999 /*
1000 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001001 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001002 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001003 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1004 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakkerf451bac2013-08-30 15:37:02 +02001005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
Hanno Becker2dec5e82017-10-03 07:49:52 +01001007 /* Verify the result to prevent glitching attacks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001008 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1009 &ctx->N, &ctx->RN));
1010 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001011 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1012 goto cleanup;
1013 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001014
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 olen = ctx->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001016 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
1018cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001020 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1021 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1022 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001023#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001024
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001025 mbedtls_mpi_free(&P1);
1026 mbedtls_mpi_free(&Q1);
1027 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001029 if (f_rng != NULL) {
Janos Follathe81102e2017-03-22 13:38:28 +00001030#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001031 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001032#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001033 mbedtls_mpi_free(&DP_blind);
1034 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001035#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001038 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001039
1040#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001042#endif
1043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001044 mbedtls_mpi_free(&C);
1045 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001046
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001047 if (ret != 0 && ret >= -0x007f) {
1048 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1049 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001051 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001052}
1053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001055/**
1056 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1057 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001058 * \param dst buffer to mask
1059 * \param dlen length of destination buffer
1060 * \param src source of the mask generation
1061 * \param slen length of the source buffer
1062 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001063 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001064static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1065 size_t slen, mbedtls_md_context_t *md_ctx)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001068 unsigned char counter[4];
1069 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001070 unsigned int hlen;
1071 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001072 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001074 memset(mask, 0, MBEDTLS_MD_MAX_SIZE);
1075 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001077 hlen = mbedtls_md_get_size(md_ctx->md_info);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001078
Simon Butcher02037452016-03-01 21:19:12 +00001079 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001080 p = dst;
1081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001082 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001083 use_len = hlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001084 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001085 use_len = dlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001088 if ((ret = mbedtls_md_starts(md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001089 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001090 }
1091 if ((ret = mbedtls_md_update(md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001092 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001093 }
1094 if ((ret = mbedtls_md_update(md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001095 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001096 }
1097 if ((ret = mbedtls_md_finish(md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001098 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001099 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001101 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001102 *p++ ^= mask[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001103 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104
1105 counter[3]++;
1106
1107 dlen -= use_len;
1108 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001109
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001110exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001111 mbedtls_platform_zeroize(mask, sizeof(mask));
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001113 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001118/*
1119 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1120 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001121int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1122 int (*f_rng)(void *, unsigned char *, size_t),
1123 void *p_rng,
1124 int mode,
1125 const unsigned char *label, size_t label_len,
1126 size_t ilen,
1127 const unsigned char *input,
1128 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001129{
1130 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001132 unsigned char *p = output;
1133 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 const mbedtls_md_info_t *md_info;
1135 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001137 RSA_VALIDATE_RET(ctx != NULL);
1138 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1139 mode == MBEDTLS_RSA_PUBLIC);
1140 RSA_VALIDATE_RET(output != NULL);
1141 RSA_VALIDATE_RET(ilen == 0 || input != NULL);
1142 RSA_VALIDATE_RET(label_len == 0 || label != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001144 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1145 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1146 }
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001148 if (f_rng == NULL) {
1149 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1150 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001152 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ctx->hash_id);
1153 if (md_info == NULL) {
1154 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1155 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001156
1157 olen = ctx->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001158 hlen = mbedtls_md_get_size(md_info);
Paul Bakkerb3869132013-02-28 17:21:01 +01001159
Simon Butcher02037452016-03-01 21:19:12 +00001160 /* first comparison checks for overflow */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001161 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1162 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1163 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001165 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001166
1167 *p++ = 0;
1168
Simon Butcher02037452016-03-01 21:19:12 +00001169 /* Generate a random octet string seed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001170 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1171 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1172 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001173
1174 p += hlen;
1175
Simon Butcher02037452016-03-01 21:19:12 +00001176 /* Construct DB */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001177 if ((ret = mbedtls_md(md_info, label, label_len, p)) != 0) {
1178 return ret;
1179 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001180 p += hlen;
1181 p += olen - 2 * hlen - 2 - ilen;
1182 *p++ = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001183 if (ilen != 0) {
1184 memcpy(p, input, ilen);
1185 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001187 mbedtls_md_init(&md_ctx);
1188 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001189 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001190 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001191
Simon Butcher02037452016-03-01 21:19:12 +00001192 /* maskedDB: Apply dbMask to DB */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001193 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1194 &md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001195 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001197
Simon Butcher02037452016-03-01 21:19:12 +00001198 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001199 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1200 &md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001201 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001202 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001203
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001204exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001205 mbedtls_md_free(&md_ctx);
Paul Bakkerb3869132013-02-28 17:21:01 +01001206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001207 if (ret != 0) {
1208 return ret;
1209 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001211 return (mode == MBEDTLS_RSA_PUBLIC)
1212 ? mbedtls_rsa_public(ctx, output, output)
1213 : mbedtls_rsa_private(ctx, f_rng, p_rng, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001218/*
1219 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1220 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001221int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1222 int (*f_rng)(void *, unsigned char *, size_t),
1223 void *p_rng,
1224 int mode, size_t ilen,
1225 const unsigned char *input,
1226 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001227{
1228 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001230 unsigned char *p = output;
1231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001232 RSA_VALIDATE_RET(ctx != NULL);
1233 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1234 mode == MBEDTLS_RSA_PUBLIC);
1235 RSA_VALIDATE_RET(output != NULL);
1236 RSA_VALIDATE_RET(ilen == 0 || input != NULL);
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001238 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1239 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1240 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001241
1242 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001243
Simon Butcher02037452016-03-01 21:19:12 +00001244 /* first comparison checks for overflow */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001245 if (ilen + 11 < ilen || olen < ilen + 11) {
1246 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1247 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001248
1249 nb_pad = olen - 3 - ilen;
1250
1251 *p++ = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001252 if (mode == MBEDTLS_RSA_PUBLIC) {
1253 if (f_rng == NULL) {
1254 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1255 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001259 while (nb_pad-- > 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001260 int rng_dl = 100;
1261
1262 do {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001263 ret = f_rng(p_rng, p, 1);
1264 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001265
Simon Butcher02037452016-03-01 21:19:12 +00001266 /* Check if RNG failed to generate data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001267 if (rng_dl == 0 || ret != 0) {
1268 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1269 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001270
1271 p++;
1272 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001273 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 while (nb_pad-- > 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001277 *p++ = 0xFF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001278 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001279 }
1280
1281 *p++ = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001282 if (ilen != 0) {
1283 memcpy(p, input, ilen);
1284 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001286 return (mode == MBEDTLS_RSA_PUBLIC)
1287 ? mbedtls_rsa_public(ctx, output, output)
1288 : mbedtls_rsa_private(ctx, f_rng, p_rng, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001291
Paul Bakker5121ce52009-01-03 21:22:43 +00001292/*
1293 * Add the message padding, then do an RSA operation
1294 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001295int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1296 int (*f_rng)(void *, unsigned char *, size_t),
1297 void *p_rng,
1298 int mode, size_t ilen,
1299 const unsigned char *input,
1300 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001301{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001302 RSA_VALIDATE_RET(ctx != NULL);
1303 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1304 mode == MBEDTLS_RSA_PUBLIC);
1305 RSA_VALIDATE_RET(output != NULL);
1306 RSA_VALIDATE_RET(ilen == 0 || input != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001308 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309#if defined(MBEDTLS_PKCS1_V15)
1310 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001311 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng, mode, ilen,
1312 input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001313#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#if defined(MBEDTLS_PKCS1_V21)
1316 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, mode, NULL, 0,
1318 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001319#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
1321 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001322 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001324}
1325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001327/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001328 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001330int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1331 int (*f_rng)(void *, unsigned char *, size_t),
1332 void *p_rng,
1333 int mode,
1334 const unsigned char *label, size_t label_len,
1335 size_t *olen,
1336 const unsigned char *input,
1337 unsigned char *output,
1338 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001339{
Janos Follath24eed8d2019-11-22 13:21:35 +00001340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001341 size_t ilen, i, pad_len;
Dave Rodgman954a2da2023-09-20 14:10:35 +01001342 unsigned char *p, pad_done;
1343 int bad;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1345 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001346 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 const mbedtls_md_info_t *md_info;
1348 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001350 RSA_VALIDATE_RET(ctx != NULL);
1351 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1352 mode == MBEDTLS_RSA_PUBLIC);
1353 RSA_VALIDATE_RET(output_max_len == 0 || output != NULL);
1354 RSA_VALIDATE_RET(label_len == 0 || label != NULL);
1355 RSA_VALIDATE_RET(input != NULL);
1356 RSA_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001357
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001358 /*
1359 * Parameters sanity checks
1360 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001361 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1362 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
1365 ilen = ctx->len;
1366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001367 if (ilen < 16 || ilen > sizeof(buf)) {
1368 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1369 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001371 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ctx->hash_id);
1372 if (md_info == NULL) {
1373 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1374 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001376 hlen = mbedtls_md_get_size(md_info);
Janos Follathc17cda12016-02-11 11:08:18 +00001377
1378 // checking for integer underflow
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001379 if (2 * hlen + 2 > ilen) {
1380 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1381 }
Janos Follathc17cda12016-02-11 11:08:18 +00001382
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001383 /*
1384 * RSA operation
1385 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001386 ret = (mode == MBEDTLS_RSA_PUBLIC)
1387 ? mbedtls_rsa_public(ctx, input, buf)
1388 : mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001390 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001391 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001394 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001395 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001396 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001397 mbedtls_md_init(&md_ctx);
1398 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
1399 mbedtls_md_free(&md_ctx);
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001400 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001401 }
1402
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001403 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001404 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1405 &md_ctx)) != 0 ||
1406 /* DB: Apply dbMask to maskedDB */
1407 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1408 &md_ctx)) != 0) {
1409 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001410 goto cleanup;
1411 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001413 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001414
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001415 /* Generate lHash */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001416 if ((ret = mbedtls_md(md_info, label, label_len, lhash)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001417 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001418 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001419
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001420 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001421 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001422 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001424 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001426 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001427
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001428 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001429
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001430 /* Check lHash */
Dave Rodgman954a2da2023-09-20 14:10:35 +01001431 bad |= mbedtls_ct_memcmp(lhash, p, hlen);
1432 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001433
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001434 /* Get zero-padding len, but always read till end of buffer
1435 * (minus one, for the 01 byte) */
1436 pad_len = 0;
1437 pad_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001438 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001439 pad_done |= p[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001440 pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001441 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001442
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001443 p += pad_len;
1444 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001446 /*
1447 * The only information "leaked" is whether the padding was correct or not
1448 * (eg, no data is copied if it was not correct). This meets the
1449 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1450 * the different error conditions.
1451 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001452 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001453 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1454 goto cleanup;
1455 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001457 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001458 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1459 goto cleanup;
1460 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001461
1462 *olen = ilen - (p - buf);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001463 if (*olen != 0) {
1464 memcpy(output, p, *olen);
1465 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001466 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001467
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001468cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001469 mbedtls_platform_zeroize(buf, sizeof(buf));
1470 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001472 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001473}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001477/*
1478 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1479 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001480int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1481 int (*f_rng)(void *, unsigned char *, size_t),
1482 void *p_rng,
1483 int mode,
1484 size_t *olen,
1485 const unsigned char *input,
1486 unsigned char *output,
1487 size_t output_max_len)
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001488{
1489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1490 size_t ilen;
1491 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001493 RSA_VALIDATE_RET(ctx != NULL);
1494 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1495 mode == MBEDTLS_RSA_PUBLIC);
1496 RSA_VALIDATE_RET(output_max_len == 0 || output != NULL);
1497 RSA_VALIDATE_RET(input != NULL);
1498 RSA_VALIDATE_RET(olen != NULL);
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001499
1500 ilen = ctx->len;
1501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001502 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1503 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1504 }
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001506 if (ilen < 16 || ilen > sizeof(buf)) {
1507 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1508 }
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001510 ret = (mode == MBEDTLS_RSA_PUBLIC)
1511 ? mbedtls_rsa_public(ctx, input, buf)
1512 : mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001514 if (ret != 0) {
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001515 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001516 }
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001518 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(mode, buf, ilen,
1519 output, output_max_len, olen);
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001520
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001521cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001524 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001527
1528/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001529 * Do an RSA operation, then remove the message padding
1530 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001531int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1532 int (*f_rng)(void *, unsigned char *, size_t),
1533 void *p_rng,
1534 int mode, size_t *olen,
1535 const unsigned char *input,
1536 unsigned char *output,
1537 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001538{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001539 RSA_VALIDATE_RET(ctx != NULL);
1540 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1541 mode == MBEDTLS_RSA_PUBLIC);
1542 RSA_VALIDATE_RET(output_max_len == 0 || output != NULL);
1543 RSA_VALIDATE_RET(input != NULL);
1544 RSA_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001546 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_PKCS1_V15)
1548 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001549 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, mode, olen,
1550 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001551#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#if defined(MBEDTLS_PKCS1_V21)
1554 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001555 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, mode, NULL, 0,
1556 olen, input, output,
1557 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001558#endif
1559
1560 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001561 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001562 }
1563}
1564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001566static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1567 int (*f_rng)(void *, unsigned char *, size_t),
1568 void *p_rng,
1569 int mode,
1570 mbedtls_md_type_t md_alg,
1571 unsigned int hashlen,
1572 const unsigned char *hash,
1573 int saltlen,
1574 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001575{
1576 size_t olen;
1577 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001578 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001579 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001581 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 const mbedtls_md_info_t *md_info;
1583 mbedtls_md_context_t md_ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001584 RSA_VALIDATE_RET(ctx != NULL);
1585 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1586 mode == MBEDTLS_RSA_PUBLIC);
1587 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
1588 hashlen == 0) ||
1589 hash != NULL);
1590 RSA_VALIDATE_RET(sig != NULL);
Paul Bakkerb3869132013-02-28 17:21:01 +01001591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001592 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1593 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1594 }
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001596 if (f_rng == NULL) {
1597 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1598 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001599
1600 olen = ctx->len;
1601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001602 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001603 /* Gather length of hash to sign */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001604 md_info = mbedtls_md_info_from_type(md_alg);
1605 if (md_info == NULL) {
1606 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1607 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001609 hashlen = mbedtls_md_get_size(md_info);
Paul Bakkerb3869132013-02-28 17:21:01 +01001610 }
1611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001612 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ctx->hash_id);
1613 if (md_info == NULL) {
1614 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1615 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001617 hlen = mbedtls_md_get_size(md_info);
Paul Bakkerb3869132013-02-28 17:21:01 +01001618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001619 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1620 /* Calculate the largest possible salt length, up to the hash size.
1621 * Normally this is the hash length, which is the maximum salt length
1622 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1623 * enough room, use the maximum salt length that fits. The constraint is
1624 * that the hash length plus the salt length plus 2 bytes must be at most
1625 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1626 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001627 min_slen = hlen - 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001628 if (olen < hlen + min_slen + 2) {
1629 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1630 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001631 slen = hlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001632 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001633 slen = olen - hlen - 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001634 }
1635 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1636 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1637 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001638 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001639 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001641 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001642
Simon Butcher02037452016-03-01 21:19:12 +00001643 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001644 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001645 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001646 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001647
1648 /* Generate salt of length slen in place in the encoded message */
1649 salt = p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001650 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1651 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1652 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001653
Paul Bakkerb3869132013-02-28 17:21:01 +01001654 p += slen;
1655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001656 mbedtls_md_init(&md_ctx);
1657 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001658 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001659 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001660
Simon Butcher02037452016-03-01 21:19:12 +00001661 /* Generate H = Hash( M' ) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001662 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001663 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001664 }
1665 if ((ret = mbedtls_md_update(&md_ctx, p, 8)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001666 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001667 }
1668 if ((ret = mbedtls_md_update(&md_ctx, hash, hashlen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001669 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001670 }
1671 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001672 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001673 }
1674 if ((ret = mbedtls_md_finish(&md_ctx, p)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001675 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001676 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001677
Simon Butcher02037452016-03-01 21:19:12 +00001678 /* Compensate for boundary condition when applying mask */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001679 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001680 offset = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001681 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001682
Simon Butcher02037452016-03-01 21:19:12 +00001683 /* maskedDB: Apply dbMask to DB */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001684 if ((ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
1685 &md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001686 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001687 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001689 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1690 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001691
1692 p += hlen;
1693 *p++ = 0xBC;
1694
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001695exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001696 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001698 if (ret != 0) {
1699 return ret;
1700 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001702 return (mode == MBEDTLS_RSA_PUBLIC)
1703 ? mbedtls_rsa_public(ctx, sig, sig)
1704 : mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001705}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001706
1707/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001708 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1709 * the option to pass in the salt length.
1710 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001711int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1712 int (*f_rng)(void *, unsigned char *, size_t),
1713 void *p_rng,
1714 mbedtls_md_type_t md_alg,
1715 unsigned int hashlen,
1716 const unsigned char *hash,
1717 int saltlen,
1718 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001719{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001720 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, md_alg,
1721 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001722}
1723
1724
1725/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001726 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1727 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001728int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1729 int (*f_rng)(void *, unsigned char *, size_t),
1730 void *p_rng,
1731 int mode,
1732 mbedtls_md_type_t md_alg,
1733 unsigned int hashlen,
1734 const unsigned char *hash,
1735 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001736{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001737 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, mode, md_alg,
1738 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001739}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001743/*
1744 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1745 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001746
1747/* Construct a PKCS v1.5 encoding of a hashed message
1748 *
1749 * This is used both for signature generation and verification.
1750 *
1751 * Parameters:
1752 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001753 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001754 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001755 * - hash: Buffer containing the hashed message or the raw data.
1756 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001757 * - dst: Buffer to hold the encoded message.
1758 *
1759 * Assumptions:
1760 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1761 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001762 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001763 *
1764 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001765static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1766 unsigned int hashlen,
1767 const unsigned char *hash,
1768 size_t dst_len,
1769 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001770{
1771 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001772 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001773 unsigned char *p = dst;
1774 const char *oid = NULL;
1775
1776 /* Are we signing hashed or raw data? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001777 if (md_alg != MBEDTLS_MD_NONE) {
1778 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1779 if (md_info == NULL) {
1780 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1781 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001783 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1784 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1785 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001787 hashlen = mbedtls_md_get_size(md_info);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001788
1789 /* Double-check that 8 + hashlen + oid_size can be used as a
1790 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001791 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001792 10 + hashlen < hashlen ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001793 10 + hashlen + oid_size < 10 + hashlen) {
1794 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1795 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001796
1797 /*
1798 * Static bounds check:
1799 * - Need 10 bytes for five tag-length pairs.
1800 * (Insist on 1-byte length encodings to protect against variants of
1801 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1802 * - Need hashlen bytes for hash
1803 * - Need oid_size bytes for hash alg OID.
1804 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001805 if (nb_pad < 10 + hashlen + oid_size) {
1806 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1807 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001808 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001809 } else {
1810 if (nb_pad < hashlen) {
1811 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1812 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001813
1814 nb_pad -= hashlen;
1815 }
1816
Hanno Becker2b2f8982017-09-27 17:10:03 +01001817 /* Need space for signature header and padding delimiter (3 bytes),
1818 * and 8 bytes for the minimal padding */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001819 if (nb_pad < 3 + 8) {
1820 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1821 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001822 nb_pad -= 3;
1823
1824 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001825 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001826
1827 /* Write signature header and padding */
1828 *p++ = 0;
1829 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001830 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001831 p += nb_pad;
1832 *p++ = 0;
1833
1834 /* Are we signing raw data? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001835 if (md_alg == MBEDTLS_MD_NONE) {
1836 memcpy(p, hash, hashlen);
1837 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001838 }
1839
1840 /* Signing hashed data, add corresponding ASN.1 structure
1841 *
1842 * DigestInfo ::= SEQUENCE {
1843 * digestAlgorithm DigestAlgorithmIdentifier,
1844 * digest Digest }
1845 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1846 * Digest ::= OCTET STRING
1847 *
1848 * Schematic:
1849 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1850 * TAG-NULL + LEN [ NULL ] ]
1851 * TAG-OCTET + LEN [ HASH ] ]
1852 */
1853 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001854 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001855 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001856 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001857 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001858 *p++ = (unsigned char) oid_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001859 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001860 p += oid_size;
1861 *p++ = MBEDTLS_ASN1_NULL;
1862 *p++ = 0x00;
1863 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001864 *p++ = (unsigned char) hashlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001865 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001866 p += hashlen;
1867
1868 /* Just a sanity-check, should be automatic
1869 * after the initial bounds check. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001870 if (p != dst + dst_len) {
1871 mbedtls_platform_zeroize(dst, dst_len);
1872 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001873 }
1874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001875 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001876}
1877
Paul Bakkerb3869132013-02-28 17:21:01 +01001878/*
1879 * Do an RSA operation to sign the message digest
1880 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001881int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
1882 int (*f_rng)(void *, unsigned char *, size_t),
1883 void *p_rng,
1884 int mode,
1885 mbedtls_md_type_t md_alg,
1886 unsigned int hashlen,
1887 const unsigned char *hash,
1888 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001889{
Janos Follath24eed8d2019-11-22 13:21:35 +00001890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001891 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001892
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001893 RSA_VALIDATE_RET(ctx != NULL);
1894 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1895 mode == MBEDTLS_RSA_PUBLIC);
1896 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
1897 hashlen == 0) ||
1898 hash != NULL);
1899 RSA_VALIDATE_RET(sig != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001900
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001901 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1902 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1903 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001904
Hanno Beckerfdf38032017-09-06 12:35:55 +01001905 /*
1906 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1907 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001909 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
1910 ctx->len, sig)) != 0) {
1911 return ret;
1912 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001913
1914 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001915 * Call respective RSA primitive
1916 */
1917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001918 if (mode == MBEDTLS_RSA_PUBLIC) {
Hanno Beckerfdf38032017-09-06 12:35:55 +01001919 /* Skip verification on a public key operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001920 return mbedtls_rsa_public(ctx, sig, sig);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001921 }
1922
1923 /* Private key operation
1924 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001925 * In order to prevent Lenstra's attack, make the signature in a
1926 * temporary buffer and check it before returning it.
1927 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001928
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001929 sig_try = mbedtls_calloc(1, ctx->len);
1930 if (sig_try == NULL) {
1931 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00001932 }
1933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001934 verif = mbedtls_calloc(1, ctx->len);
1935 if (verif == NULL) {
1936 mbedtls_free(sig_try);
1937 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
1938 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001940 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
1941 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
1942
1943 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001944 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1945 goto cleanup;
1946 }
1947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001948 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001949
1950cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001951 mbedtls_platform_zeroize(sig_try, ctx->len);
1952 mbedtls_platform_zeroize(verif, ctx->len);
1953 mbedtls_free(sig_try);
1954 mbedtls_free(verif);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001956 if (ret != 0) {
1957 memset(sig, '!', ctx->len);
1958 }
1959 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001960}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001962
1963/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 * Do an RSA operation to sign the message digest
1965 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001966int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
1967 int (*f_rng)(void *, unsigned char *, size_t),
1968 void *p_rng,
1969 int mode,
1970 mbedtls_md_type_t md_alg,
1971 unsigned int hashlen,
1972 const unsigned char *hash,
1973 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00001974{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001975 RSA_VALIDATE_RET(ctx != NULL);
1976 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
1977 mode == MBEDTLS_RSA_PUBLIC);
1978 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
1979 hashlen == 0) ||
1980 hash != NULL);
1981 RSA_VALIDATE_RET(sig != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001982
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001983 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#if defined(MBEDTLS_PKCS1_V15)
1985 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001986 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng, mode, md_alg,
1987 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02001988#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990#if defined(MBEDTLS_PKCS1_V21)
1991 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001992 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, mode, md_alg,
1993 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001994#endif
1995
Paul Bakker5121ce52009-01-03 21:22:43 +00001996 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001998 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001999}
2000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002002/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002003 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2006 int (*f_rng)(void *, unsigned char *, size_t),
2007 void *p_rng,
2008 int mode,
2009 mbedtls_md_type_t md_alg,
2010 unsigned int hashlen,
2011 const unsigned char *hash,
2012 mbedtls_md_type_t mgf1_hash_id,
2013 int expected_salt_len,
2014 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002015{
Janos Follath24eed8d2019-11-22 13:21:35 +00002016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002017 size_t siglen;
2018 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002019 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002021 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002022 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002023 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 const mbedtls_md_info_t *md_info;
2025 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002026 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 RSA_VALIDATE_RET(ctx != NULL);
2029 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2030 mode == MBEDTLS_RSA_PUBLIC);
2031 RSA_VALIDATE_RET(sig != NULL);
2032 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2033 hashlen == 0) ||
2034 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002036 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2037 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2038 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002039
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 siglen = ctx->len;
2041
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002042 if (siglen < 16 || siglen > sizeof(buf)) {
2043 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2044 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002046 ret = (mode == MBEDTLS_RSA_PUBLIC)
2047 ? mbedtls_rsa_public(ctx, sig, buf)
2048 : mbedtls_rsa_private(ctx, f_rng, p_rng, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002050 if (ret != 0) {
2051 return ret;
2052 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002053
2054 p = buf;
2055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002056 if (buf[siglen - 1] != 0xBC) {
2057 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002058 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002060 if (md_alg != MBEDTLS_MD_NONE) {
2061 /* Gather length of hash to sign */
2062 md_info = mbedtls_md_info_from_type(md_alg);
2063 if (md_info == NULL) {
2064 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2065 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002067 hashlen = mbedtls_md_get_size(md_info);
2068 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002070 md_info = mbedtls_md_info_from_type(mgf1_hash_id);
2071 if (md_info == NULL) {
2072 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2073 }
2074
2075 hlen = mbedtls_md_get_size(md_info);
2076
2077 memset(zeros, 0, 8);
Paul Bakker53019ae2011-03-25 13:58:48 +00002078
Simon Butcher02037452016-03-01 21:19:12 +00002079 /*
2080 * Note: EMSA-PSS verification is over the length of N - 1 bits
2081 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002082 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002084 if (buf[0] >> (8 - siglen * 8 + msb)) {
2085 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2086 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002087
Simon Butcher02037452016-03-01 21:19:12 +00002088 /* Compensate for boundary condition when applying mask */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002089 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002090 p++;
2091 siglen -= 1;
2092 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094 if (siglen < hlen + 2) {
2095 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2096 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002097 hash_start = p + siglen - hlen - 1;
2098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002099 mbedtls_md_init(&md_ctx);
2100 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002101 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002102 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002104 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, &md_ctx);
2105 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002106 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002107 }
Paul Bakker02303e82013-01-03 11:08:31 +01002108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002109 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002111 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002112 p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002113 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002115 if (*p++ != 0x01) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002116 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2117 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002118 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002119
Gilles Peskine6a54b022017-10-17 19:02:13 +02002120 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002122 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2123 observed_salt_len != (size_t) expected_salt_len) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002124 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2125 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002126 }
2127
Simon Butcher02037452016-03-01 21:19:12 +00002128 /*
2129 * Generate H = Hash( M' )
2130 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002131 ret = mbedtls_md_starts(&md_ctx);
2132 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002133 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002134 }
2135 ret = mbedtls_md_update(&md_ctx, zeros, 8);
2136 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002137 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002138 }
2139 ret = mbedtls_md_update(&md_ctx, hash, hashlen);
2140 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002141 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002142 }
2143 ret = mbedtls_md_update(&md_ctx, p, observed_salt_len);
2144 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002145 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002146 }
2147 ret = mbedtls_md_finish(&md_ctx, result);
2148 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002149 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002150 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 if (memcmp(hash_start, result, hlen) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002153 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002154 goto exit;
2155 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002156
2157exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158 mbedtls_md_free(&md_ctx);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002160 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002161}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002162
2163/*
2164 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002166int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2167 int (*f_rng)(void *, unsigned char *, size_t),
2168 void *p_rng,
2169 int mode,
2170 mbedtls_md_type_t md_alg,
2171 unsigned int hashlen,
2172 const unsigned char *hash,
2173 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002174{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002175 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002176 RSA_VALIDATE_RET(ctx != NULL);
2177 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2178 mode == MBEDTLS_RSA_PUBLIC);
2179 RSA_VALIDATE_RET(sig != NULL);
2180 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2181 hashlen == 0) ||
2182 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002184 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002186 : md_alg;
2187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188 return mbedtls_rsa_rsassa_pss_verify_ext(ctx, f_rng, p_rng, mode,
2189 md_alg, hashlen, hash,
2190 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
2191 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002192
2193}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002197/*
2198 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2199 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002200int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2201 int (*f_rng)(void *, unsigned char *, size_t),
2202 void *p_rng,
2203 int mode,
2204 mbedtls_md_type_t md_alg,
2205 unsigned int hashlen,
2206 const unsigned char *hash,
2207 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002208{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002209 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002210 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002211 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002213 RSA_VALIDATE_RET(ctx != NULL);
2214 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2215 mode == MBEDTLS_RSA_PUBLIC);
2216 RSA_VALIDATE_RET(sig != NULL);
2217 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2218 hashlen == 0) ||
2219 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002220
2221 sig_len = ctx->len;
2222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002223 if (mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2224 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2225 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002226
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002227 /*
2228 * Prepare expected PKCS1 v1.5 encoding of hash.
2229 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002231 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2232 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002233 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2234 goto cleanup;
2235 }
2236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002237 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2238 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002239 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002240 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002241
2242 /*
2243 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2244 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002246 ret = (mode == MBEDTLS_RSA_PUBLIC)
2247 ? mbedtls_rsa_public(ctx, sig, encoded)
2248 : mbedtls_rsa_private(ctx, f_rng, p_rng, sig, encoded);
2249 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002250 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002251 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002252
Simon Butcher02037452016-03-01 21:19:12 +00002253 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002254 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002255 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002257 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2258 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002259 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2260 goto cleanup;
2261 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002262
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002263cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002265 if (encoded != NULL) {
2266 mbedtls_platform_zeroize(encoded, sig_len);
2267 mbedtls_free(encoded);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002268 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002270 if (encoded_expected != NULL) {
2271 mbedtls_platform_zeroize(encoded_expected, sig_len);
2272 mbedtls_free(encoded_expected);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002273 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002275 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002276}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
2279/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002280 * Do an RSA operation and check the message digest
2281 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002282int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2283 int (*f_rng)(void *, unsigned char *, size_t),
2284 void *p_rng,
2285 int mode,
2286 mbedtls_md_type_t md_alg,
2287 unsigned int hashlen,
2288 const unsigned char *hash,
2289 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002290{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002291 RSA_VALIDATE_RET(ctx != NULL);
2292 RSA_VALIDATE_RET(mode == MBEDTLS_RSA_PRIVATE ||
2293 mode == MBEDTLS_RSA_PUBLIC);
2294 RSA_VALIDATE_RET(sig != NULL);
2295 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE &&
2296 hashlen == 0) ||
2297 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002299 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300#if defined(MBEDTLS_PKCS1_V15)
2301 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002302 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, f_rng, p_rng, mode, md_alg,
2303 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002304#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306#if defined(MBEDTLS_PKCS1_V21)
2307 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308 return mbedtls_rsa_rsassa_pss_verify(ctx, f_rng, p_rng, mode, md_alg,
2309 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002310#endif
2311
2312 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002313 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002314 }
2315}
2316
2317/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002318 * Copy the components of an RSA key
2319 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002320int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002321{
Janos Follath24eed8d2019-11-22 13:21:35 +00002322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002323 RSA_VALIDATE_RET(dst != NULL);
2324 RSA_VALIDATE_RET(src != NULL);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002325
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002326 dst->len = src->len;
2327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002328 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2329 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002331 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2332 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2333 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002334
2335#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002336 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2337 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2338 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2339 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2340 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002341#endif
2342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002343 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002345 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2346 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002347
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002348 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002349 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002350
2351cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002352 if (ret != 0) {
2353 mbedtls_rsa_free(dst);
2354 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002356 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002357}
2358
2359/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002360 * Free the components of an RSA key
2361 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002362void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002363{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002365 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002366 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002368 mbedtls_mpi_free(&ctx->Vi);
2369 mbedtls_mpi_free(&ctx->Vf);
2370 mbedtls_mpi_free(&ctx->RN);
2371 mbedtls_mpi_free(&ctx->D);
2372 mbedtls_mpi_free(&ctx->Q);
2373 mbedtls_mpi_free(&ctx->P);
2374 mbedtls_mpi_free(&ctx->E);
2375 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002376
Hanno Becker33c30a02017-08-23 07:00:22 +01002377#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002378 mbedtls_mpi_free(&ctx->RQ);
2379 mbedtls_mpi_free(&ctx->RP);
2380 mbedtls_mpi_free(&ctx->QP);
2381 mbedtls_mpi_free(&ctx->DQ);
2382 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002383#endif /* MBEDTLS_RSA_NO_CRT */
2384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002386 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002387 if (ctx->ver != 0) {
2388 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002389 ctx->ver = 0;
2390 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002391#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002392}
2393
Hanno Beckerab377312017-08-23 16:24:51 +01002394#endif /* !MBEDTLS_RSA_ALT */
2395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002398#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
2400/*
2401 * Example RSA-1024 keypair, for test purposes
2402 */
2403#define KEY_LEN 128
2404
2405#define RSA_N "9292758453063D803DD603D5E777D788" \
2406 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2407 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2408 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2409 "93A89813FBF3C4F8066D2D800F7C38A8" \
2410 "1AE31942917403FF4946B0A83D3D3E05" \
2411 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2412 "5E94BB77B07507233A0BC7BAC8F90F79"
2413
2414#define RSA_E "10001"
2415
2416#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2417 "66CA472BC44D253102F8B4A9D3BFA750" \
2418 "91386C0077937FE33FA3252D28855837" \
2419 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2420 "DF79C5CE07EE72C7F123142198164234" \
2421 "CABB724CF78B8173B9F880FC86322407" \
2422 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2423 "071513A1E85B5DFA031F21ECAE91A34D"
2424
2425#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2426 "2C01CAD19EA484A87EA4377637E75500" \
2427 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2428 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2429
2430#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2431 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2432 "910E4168387E3C30AA1E00C339A79508" \
2433 "8452DD96A9A5EA5D9DCA68DA636032AF"
2434
Paul Bakker5121ce52009-01-03 21:22:43 +00002435#define PT_LEN 24
2436#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2437 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002440static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002441{
gufe44c2620da2020-08-03 17:56:50 +02002442#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002443 size_t i;
2444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002445 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002446 rng_state = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002447 }
Paul Bakker545570e2010-07-18 09:00:25 +00002448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002449 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002450 output[i] = rand();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002451 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002452#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002454 rng_state = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002455 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002457 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002458#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002460 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002463
Paul Bakker5121ce52009-01-03 21:22:43 +00002464/*
2465 * Checkup routine
2466 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002467int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002468{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002469 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002471 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 unsigned char rsa_plaintext[PT_LEN];
2474 unsigned char rsa_decrypted[PT_LEN];
2475 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002477 unsigned char sha1sum[20];
2478#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
Hanno Becker3a701162017-08-22 13:52:43 +01002480 mbedtls_mpi K;
2481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002482 mbedtls_mpi_init(&K);
2483 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002485 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2486 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2487 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2488 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2489 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2490 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2491 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2492 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2493 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2494 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002496 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002498 if (verbose != 0) {
2499 mbedtls_printf(" RSA key validation: ");
2500 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002502 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2503 mbedtls_rsa_check_privkey(&rsa) != 0) {
2504 if (verbose != 0) {
2505 mbedtls_printf("failed\n");
2506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Hanno Becker5bc87292017-05-03 15:09:31 +01002508 ret = 1;
2509 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 }
2511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002512 if (verbose != 0) {
2513 mbedtls_printf("passed\n PKCS#1 encryption : ");
2514 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002516 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002518 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2519 PT_LEN, rsa_plaintext,
2520 rsa_ciphertext) != 0) {
2521 if (verbose != 0) {
2522 mbedtls_printf("failed\n");
2523 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Hanno Becker5bc87292017-05-03 15:09:31 +01002525 ret = 1;
2526 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 }
2528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002529 if (verbose != 0) {
2530 mbedtls_printf("passed\n PKCS#1 decryption : ");
2531 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002533 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2534 &len, rsa_ciphertext, rsa_decrypted,
2535 sizeof(rsa_decrypted)) != 0) {
2536 if (verbose != 0) {
2537 mbedtls_printf("failed\n");
2538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Hanno Becker5bc87292017-05-03 15:09:31 +01002540 ret = 1;
2541 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 }
2543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002544 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 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");
2555 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557#if defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002558 if (verbose != 0) {
2559 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002560 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002562 if (mbedtls_sha1_ret(rsa_plaintext, PT_LEN, sha1sum) != 0) {
2563 if (verbose != 0) {
2564 mbedtls_printf("failed\n");
2565 }
2566
2567 return 1;
2568 }
2569
2570 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2571 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2572 sha1sum, rsa_ciphertext) != 0) {
2573 if (verbose != 0) {
2574 mbedtls_printf("failed\n");
2575 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002576
Hanno Becker5bc87292017-05-03 15:09:31 +01002577 ret = 1;
2578 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 }
2580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002581 if (verbose != 0) {
2582 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002585 if (mbedtls_rsa_pkcs1_verify(&rsa, NULL, NULL,
2586 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2587 sha1sum, rsa_ciphertext) != 0) {
2588 if (verbose != 0) {
2589 mbedtls_printf("failed\n");
2590 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Hanno Becker5bc87292017-05-03 15:09:31 +01002592 ret = 1;
2593 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 }
2595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002596 if (verbose != 0) {
2597 mbedtls_printf("passed\n");
2598 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002601 if (verbose != 0) {
2602 mbedtls_printf("\n");
2603 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002604
Paul Bakker3d8fb632014-04-17 12:42:41 +02002605cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002606 mbedtls_mpi_free(&K);
2607 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002609 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002611 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002612}
2613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616#endif /* MBEDTLS_RSA_C */