blob: 9aa87ec214cc000b4cf74ea23e4d82ea381379de [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
Hanno Becker74716312017-10-02 10:00:37 +010019
Paul Bakker5121ce52009-01-03 21:22:43 +000020/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000021 * The following sources were referenced in the design of this implementation
22 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000023 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000024 * [1] A method for obtaining digital signatures and public-key cryptosystems
25 * R Rivest, A Shamir, and L Adleman
26 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
27 *
28 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
29 * Menezes, van Oorschot and Vanstone
30 *
Janos Follathe81102e2017-03-22 13:38:28 +000031 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
32 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
33 * Stefan Mangard
34 * https://arxiv.org/abs/1702.08719v2
35 *
Paul Bakker5121ce52009-01-03 21:22:43 +000036 */
37
Gilles Peskinedb09ef62020-06-03 01:43:33 +020038#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042# include "mbedtls/rsa.h"
43# include "rsa_alt_helpers.h"
44# include "mbedtls/oid.h"
45# include "mbedtls/platform_util.h"
46# include "mbedtls/error.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000047
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048# include <string.h>
Rich Evans00ab4702015-02-06 13:43:58 +000049
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020050# if defined(MBEDTLS_PKCS1_V21)
51# include "mbedtls/md.h"
52# endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020054# if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && \
55 !defined(__NetBSD__)
56# include <stdlib.h>
57# endif
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059# if defined(MBEDTLS_PLATFORM_C)
60# include "mbedtls/platform.h"
61# else
62# include <stdio.h>
63# define mbedtls_printf printf
64# define mbedtls_calloc calloc
65# define mbedtls_free free
66# endif
Paul Bakker7dc4c442014-02-01 22:50:26 +010067
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068# if !defined(MBEDTLS_RSA_ALT)
Hanno Beckera565f542017-10-11 11:00:19 +010069
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050070/* Parameter validation macros */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020071# define RSA_VALIDATE_RET(cond) \
72 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA)
73# define RSA_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE(cond)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050074
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020075# if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +010076/* constant-time buffer comparison */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020077static inline int mbedtls_safer_memcmp(const void *a, const void *b, size_t n)
Hanno Becker171a8f12017-09-06 12:32:16 +010078{
79 size_t i;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020080 const unsigned char *A = (const unsigned char *)a;
81 const unsigned char *B = (const unsigned char *)b;
Hanno Becker171a8f12017-09-06 12:32:16 +010082 unsigned char diff = 0;
83
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020084 for (i = 0; i < n; i++)
Hanno Becker171a8f12017-09-06 12:32:16 +010085 diff |= A[i] ^ B[i];
86
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020087 return diff;
Hanno Becker171a8f12017-09-06 12:32:16 +010088}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020089# endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +010090
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020091int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
92 const mbedtls_mpi *N,
93 const mbedtls_mpi *P,
94 const mbedtls_mpi *Q,
95 const mbedtls_mpi *D,
96 const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +010097{
Janos Follath24eed8d2019-11-22 13:21:35 +000098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100100
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200101 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
102 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
103 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
104 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
105 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
106 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100107 }
108
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200109 if (N != NULL)
110 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100111
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200112 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100113}
114
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200115int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
116 unsigned char const *N,
117 size_t N_len,
118 unsigned char const *P,
119 size_t P_len,
120 unsigned char const *Q,
121 size_t Q_len,
122 unsigned char const *D,
123 size_t D_len,
124 unsigned char const *E,
125 size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100126{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000127 int ret = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200128 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100129
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200130 if (N != NULL) {
131 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
132 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100133 }
134
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200135 if (P != NULL)
136 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
Hanno Becker617c1ae2017-08-23 14:11:24 +0100137
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200138 if (Q != NULL)
139 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
Hanno Becker617c1ae2017-08-23 14:11:24 +0100140
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200141 if (D != NULL)
142 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
Hanno Becker617c1ae2017-08-23 14:11:24 +0100143
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200144 if (E != NULL)
145 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
Hanno Becker617c1ae2017-08-23 14:11:24 +0100146
147cleanup:
148
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149 if (ret != 0)
150 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100151
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200152 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100153}
154
Hanno Becker705fc682017-10-10 17:57:02 +0100155/*
156 * Checks whether the context fields are set in such a way
157 * that the RSA primitives will be able to execute without error.
158 * It does *not* make guarantees for consistency of the parameters.
159 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200160static int rsa_check_context(mbedtls_rsa_context const *ctx,
161 int is_priv,
162 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100163{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200164# if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100165 /* blinding_needed is only used for NO_CRT to decide whether
166 * P,Q need to be present or not. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200167 ((void)blinding_needed);
168# endif
Hanno Beckerebd2c022017-10-12 10:54:53 +0100169
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200170 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
171 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
172 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000173 }
Hanno Becker705fc682017-10-10 17:57:02 +0100174
175 /*
176 * 1. Modular exponentiation needs positive, odd moduli.
177 */
178
179 /* Modular exponentiation wrt. N is always used for
180 * RSA public key operations. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200181 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
182 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
183 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100184 }
185
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200186# if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100187 /* Modular exponentiation for P and Q is only
188 * used for private key operations and if CRT
189 * is used. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200190 if (is_priv && (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
191 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
192 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
193 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
194 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100195 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200196# endif /* !MBEDTLS_RSA_NO_CRT */
Hanno Becker705fc682017-10-10 17:57:02 +0100197
198 /*
199 * 2. Exponents must be positive
200 */
201
202 /* Always need E for public key operations */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200203 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0)
204 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100205
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200206# if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100207 /* For private key operations, use D or DP & DQ
208 * as (unblinded) exponents. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200209 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0)
210 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
211# else
212 if (is_priv && (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
213 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
214 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100215 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200216# endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker705fc682017-10-10 17:57:02 +0100217
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200218 /* Blinding shouldn't make exponents negative either,
219 * so check that P, Q >= 1 if that hasn't yet been
220 * done as part of 1. */
221# if defined(MBEDTLS_RSA_NO_CRT)
222 if (is_priv && blinding_needed &&
223 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
224 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
225 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100226 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200227# endif
Hanno Becker705fc682017-10-10 17:57:02 +0100228
229 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100230 * but check for QP >= 1 nonetheless. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200231# if !defined(MBEDTLS_RSA_NO_CRT)
232 if (is_priv && mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
233 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100234 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200235# endif
Hanno Becker705fc682017-10-10 17:57:02 +0100236
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200237 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100238}
239
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200240int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100241{
242 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500243 int have_N, have_P, have_Q, have_D, have_E;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200244# if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500245 int have_DP, have_DQ, have_QP;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200246# endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500247 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100248
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200249 RSA_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500250
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200251 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
252 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
253 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
254 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
255 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500256
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200257# if !defined(MBEDTLS_RSA_NO_CRT)
258 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
259 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
260 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
261# endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100262
Hanno Becker617c1ae2017-08-23 14:11:24 +0100263 /*
264 * Check whether provided parameters are enough
265 * to deduce all others. The following incomplete
266 * parameter sets for private keys are supported:
267 *
268 * (1) P, Q missing.
269 * (2) D and potentially N missing.
270 *
271 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100272
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200273 n_missing = have_P && have_Q && have_D && have_E;
274 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
275 d_missing = have_P && have_Q && !have_D && have_E;
276 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100277
278 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500279 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100280
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200281 if (!is_priv && !is_pub)
282 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100283
284 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100285 * Step 1: Deduce N if P, Q are provided.
286 */
287
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200288 if (!have_N && have_P && have_Q) {
289 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q)) != 0) {
290 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100291 }
292
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200293 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100294 }
295
296 /*
297 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100298 */
299
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200300 if (pq_missing) {
301 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D, &ctx->P,
302 &ctx->Q);
303 if (ret != 0)
304 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100305
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200306 } else if (d_missing) {
307 if ((ret = mbedtls_rsa_deduce_private_exponent(
308 &ctx->P, &ctx->Q, &ctx->E, &ctx->D)) != 0) {
309 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100310 }
311 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100312
Hanno Becker617c1ae2017-08-23 14:11:24 +0100313 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100314 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100315 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100316 */
317
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200318# if !defined(MBEDTLS_RSA_NO_CRT)
319 if (is_priv && !(have_DP && have_DQ && have_QP)) {
320 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D, &ctx->DP,
321 &ctx->DQ, &ctx->QP);
322 if (ret != 0)
323 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200325# endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100326
327 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100328 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100329 */
330
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200331 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100332}
333
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200334int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
335 unsigned char *N,
336 size_t N_len,
337 unsigned char *P,
338 size_t P_len,
339 unsigned char *Q,
340 size_t Q_len,
341 unsigned char *D,
342 size_t D_len,
343 unsigned char *E,
344 size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100345{
346 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500347 int is_priv;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200348 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100349
350 /* Check if key is private or public */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200351 is_priv = mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
352 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
353 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
354 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
355 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100356
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200357 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100358 /* If we're trying to export private parameters for a public key,
359 * something must be wrong. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200360 if (P != NULL || Q != NULL || D != NULL)
361 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100362 }
363
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200364 if (N != NULL)
365 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
Hanno Becker617c1ae2017-08-23 14:11:24 +0100366
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200367 if (P != NULL)
368 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
Hanno Becker617c1ae2017-08-23 14:11:24 +0100369
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200370 if (Q != NULL)
371 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
Hanno Becker617c1ae2017-08-23 14:11:24 +0100372
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200373 if (D != NULL)
374 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
Hanno Becker617c1ae2017-08-23 14:11:24 +0100375
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200376 if (E != NULL)
377 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100378
379cleanup:
380
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200381 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100382}
383
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200384int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
385 mbedtls_mpi *N,
386 mbedtls_mpi *P,
387 mbedtls_mpi *Q,
388 mbedtls_mpi *D,
389 mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100390{
Janos Follath24eed8d2019-11-22 13:21:35 +0000391 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500392 int is_priv;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200393 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100394
395 /* Check if key is private or public */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200396 is_priv = mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
397 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
398 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
399 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
400 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100401
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200402 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100403 /* If we're trying to export private parameters for a public key,
404 * something must be wrong. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200405 if (P != NULL || Q != NULL || D != NULL)
406 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100407 }
408
409 /* Export all requested core parameters. */
410
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200411 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
412 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
413 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
414 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
415 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
416 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100417 }
418
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200419 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100420}
421
422/*
423 * Export CRT parameters
424 * This must also be implemented if CRT is not used, for being able to
425 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
426 * can be used in this case.
427 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200428int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
429 mbedtls_mpi *DP,
430 mbedtls_mpi *DQ,
431 mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100432{
Janos Follath24eed8d2019-11-22 13:21:35 +0000433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500434 int is_priv;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200435 RSA_VALIDATE_RET(ctx != NULL);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100436
437 /* Check if key is private or public */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200438 is_priv = mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
439 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
440 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
441 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
442 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100443
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200444 if (!is_priv)
445 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100446
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200447# if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100448 /* Export all requested blinding parameters. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200449 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
450 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
451 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
452 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100453 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200454# else
455 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D, DP, DQ, QP)) !=
456 0) {
457 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100458 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200459# endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100460
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200461 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100462}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100463
Paul Bakker5121ce52009-01-03 21:22:43 +0000464/*
465 * Initialize an RSA context
466 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200467void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000468{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200469 RSA_VALIDATE(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500470
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200471 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Ronald Cronc1905a12021-06-05 11:11:14 +0200473 ctx->padding = MBEDTLS_RSA_PKCS_V15;
474 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200475
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200476# if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100477 /* Set ctx->ver to nonzero to indicate that the mutex has been
478 * initialized and will need to be freed. */
479 ctx->ver = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200480 mbedtls_mutex_init(&ctx->mutex);
481# endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000482}
483
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100484/*
485 * Set padding for an existing RSA context
486 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200487int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx,
488 int padding,
489 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100490{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200491 switch (padding) {
492# if defined(MBEDTLS_PKCS1_V15)
Ronald Cron3a0375f2021-06-08 10:22:28 +0200493 case MBEDTLS_RSA_PKCS_V15:
494 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200495# endif
Ronald Cron3a0375f2021-06-08 10:22:28 +0200496
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200497# if defined(MBEDTLS_PKCS1_V21)
Ronald Cron3a0375f2021-06-08 10:22:28 +0200498 case MBEDTLS_RSA_PKCS_V21:
499 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200500# endif
Ronald Cron3a0375f2021-06-08 10:22:28 +0200501 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200502 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200503 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200504
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200505 if ((padding == MBEDTLS_RSA_PKCS_V21) && (hash_id != MBEDTLS_MD_NONE)) {
Ronald Cronea7631b2021-06-03 18:51:59 +0200506 const mbedtls_md_info_t *md_info;
507
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200508 md_info = mbedtls_md_info_from_type(hash_id);
509 if (md_info == NULL)
510 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cronea7631b2021-06-03 18:51:59 +0200511 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500512
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100513 ctx->padding = padding;
514 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200515
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200516 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100517}
518
Hanno Becker617c1ae2017-08-23 14:11:24 +0100519/*
520 * Get length in bytes of RSA modulus
521 */
522
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200523size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100524{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200525 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100526}
527
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200528# if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
530/*
531 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800532 *
533 * This generation method follows the RSA key pair generation procedure of
534 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200536int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
537 int (*f_rng)(void *, unsigned char *, size_t),
538 void *p_rng,
539 unsigned int nbits,
540 int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000541{
Janos Follath24eed8d2019-11-22 13:21:35 +0000542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800543 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100544 int prime_quality = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200545 RSA_VALIDATE_RET(ctx != NULL);
546 RSA_VALIDATE_RET(f_rng != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Janos Follathb8fc1b02018-09-03 15:37:01 +0100548 /*
549 * If the modulus is 1024 bit long or shorter, then the security strength of
550 * the RSA algorithm is less than or equal to 80 bits and therefore an error
551 * rate of 2^-80 is sufficient.
552 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200553 if (nbits > 1024)
Janos Follathb8fc1b02018-09-03 15:37:01 +0100554 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
555
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200556 mbedtls_mpi_init(&H);
557 mbedtls_mpi_init(&G);
558 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200560 if (nbits < 128 || exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100561 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
562 goto cleanup;
563 }
564
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 /*
566 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800567 * 1. |P-Q| > 2^( nbits / 2 - 100 )
568 * 2. GCD( E, (P-1)*(Q-1) ) == 1
569 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200571 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200573 do {
574 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
575 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200577 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
578 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200580 /* make sure the difference between p and q is not too small (FIPS 186-4
581 * §B.3.3 step 5.4) */
582 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
583 if (mbedtls_mpi_bitlen(&H) <=
584 ((nbits >= 200) ? ((nbits >> 1) - 99) : 0))
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 continue;
586
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200587 /* not required by any standards, but some users rely on the fact that P
588 * > Q */
589 if (H.s < 0)
590 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
Janos Follathef441782016-09-21 13:18:12 +0100591
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100592 /* Temporarily replace P,Q by P-1, Q-1 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200593 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
594 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
595 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800596
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200597 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a))
598 */
599 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
600 if (mbedtls_mpi_cmp_int(&G, 1) != 0)
Jethro Beekman97f95c92018-02-13 15:50:36 -0800601 continue;
602
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200603 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4
604 * §B.3.1 criterion 3(b)) */
605 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
606 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
607 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800608
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200609 if (mbedtls_mpi_bitlen(&ctx->D) <= ((nbits + 1) / 2)) // (FIPS 186-4
610 // §B.3.1
611 // criterion 3(a))
Jethro Beekman97f95c92018-02-13 15:50:36 -0800612 continue;
613
614 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200615 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100617 /* Restore P,Q */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200618 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
619 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100620
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200621 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800622
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200623 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100624
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200625# if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000626 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 * DP = D mod (P - 1)
628 * DQ = D mod (Q - 1)
629 * QP = Q^-1 mod P
630 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200631 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D, &ctx->DP,
632 &ctx->DQ, &ctx->QP));
633# endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
Hanno Becker83aad1f2017-08-23 06:45:10 +0100635 /* Double-check */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200636 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
638cleanup:
639
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200640 mbedtls_mpi_free(&H);
641 mbedtls_mpi_free(&G);
642 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200644 if (ret != 0) {
645 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100646
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200647 if ((-ret & ~0x7f) == 0)
648 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
649 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 }
651
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200652 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000653}
654
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200655# endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
657/*
658 * Check a public RSA key
659 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200660int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000661{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200662 RSA_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500663
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200664 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0)
665 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000666
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200667 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
668 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100669 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200671 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
672 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
673 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
674 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100675 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200677 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000678}
679
680/*
Hanno Becker705fc682017-10-10 17:57:02 +0100681 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200683int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000684{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200685 RSA_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500686
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200687 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
688 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
689 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 }
Paul Bakker48377d92013-08-30 12:06:24 +0200691
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200692 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q, &ctx->D, &ctx->E,
693 NULL, NULL) != 0) {
694 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200696# if !defined(MBEDTLS_RSA_NO_CRT)
697 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D, &ctx->DP,
698 &ctx->DQ, &ctx->QP) != 0) {
699 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100700 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200701# endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000702
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200703 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000704}
705
706/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100707 * Check if contexts holding a public and private key match
708 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200709int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
710 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100711{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200712 RSA_VALIDATE_RET(pub != NULL);
713 RSA_VALIDATE_RET(prv != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500714
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200715 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
716 mbedtls_rsa_check_privkey(prv) != 0) {
717 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100718 }
719
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200720 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
721 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
722 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100723 }
724
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200725 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100726}
727
728/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 * Do an RSA public key operation
730 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200731int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
732 const unsigned char *input,
733 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000734{
Janos Follath24eed8d2019-11-22 13:21:35 +0000735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000736 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_mpi T;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200738 RSA_VALIDATE_RET(ctx != NULL);
739 RSA_VALIDATE_RET(input != NULL);
740 RSA_VALIDATE_RET(output != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000741
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200742 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */))
743 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100744
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200745 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200747# if defined(MBEDTLS_THREADING_C)
748 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0)
749 return ret;
750# endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200751
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200752 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000753
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200754 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200755 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
756 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000757 }
758
759 olen = ctx->len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200760 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
761 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
763cleanup:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200764# if defined(MBEDTLS_THREADING_C)
765 if (mbedtls_mutex_unlock(&ctx->mutex) != 0)
766 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
767# endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200769 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200771 if (ret != 0)
772 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200774 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000775}
776
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200777/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200778 * Generate or update blinding values, see section 10 of:
779 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200780 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200781 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200782 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200783static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
784 int (*f_rng)(void *, unsigned char *, size_t),
785 void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200786{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200787 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200788 mbedtls_mpi R;
789
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200790 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200791
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200792 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200793 /* We already have blinding values, just update them by squaring */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200794 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
795 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
796 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
797 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200798
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200799 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200800 }
801
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200802 /* Unblinding value: Vf = random number, invertible mod N */
803 do {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200804 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200805 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
806 goto cleanup;
807 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200808
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200809 MBEDTLS_MPI_CHK(
810 mbedtls_mpi_fill_random(&ctx->Vf, ctx->len - 1, f_rng, p_rng));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200811
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200812 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200813 MBEDTLS_MPI_CHK(
814 mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
815 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
816 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200817
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200818 /* At this point, Vi is invertible mod N if and only if both Vf and R
819 * are invertible mod N. If one of them isn't, we don't need to know
820 * which one, we just loop and choose new values for both of them.
821 * (Each iteration succeeds with overwhelming probability.) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200822 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
823 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE)
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200824 goto cleanup;
825
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200826 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500827
828 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200829 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
830 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200831
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200832 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200833 * (Vi already contains Vf^-1 at this point) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200834 MBEDTLS_MPI_CHK(
835 mbedtls_mpi_exp_mod(&ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200836
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200837cleanup:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200838 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200839
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200840 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200841}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200842
Paul Bakker5121ce52009-01-03 21:22:43 +0000843/*
Janos Follathe81102e2017-03-22 13:38:28 +0000844 * Exponent blinding supposed to prevent side-channel attacks using multiple
845 * traces of measurements to recover the RSA key. The more collisions are there,
846 * the more bits of the key can be recovered. See [3].
847 *
848 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
849 * observations on avarage.
850 *
851 * For example with 28 byte blinding to achieve 2 collisions the adversary has
852 * to make 2^112 observations on avarage.
853 *
854 * (With the currently (as of 2017 April) known best algorithms breaking 2048
855 * bit RSA requires approximately as much time as trying out 2^112 random keys.
856 * Thus in this sense with 28 byte blinding the security is not reduced by
857 * side-channel attacks like the one in [3])
858 *
859 * This countermeasure does not help if the key recovery is possible with a
860 * single trace.
861 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200862# define RSA_EXPONENT_BLINDING 28
Janos Follathe81102e2017-03-22 13:38:28 +0000863
864/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000865 * Do an RSA private key operation
866 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200867int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
868 int (*f_rng)(void *, unsigned char *, size_t),
869 void *p_rng,
870 const unsigned char *input,
871 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000872{
Janos Follath24eed8d2019-11-22 13:21:35 +0000873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000874 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100875
876 /* Temporary holding the result */
877 mbedtls_mpi T;
878
879 /* Temporaries holding P-1, Q-1 and the
880 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000881 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100882
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200883# if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker06811ce2017-05-03 15:10:34 +0100884 /* Temporaries holding the results mod p resp. mod q. */
885 mbedtls_mpi TP, TQ;
886
887 /* Temporaries holding the blinded exponents for
888 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000889 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100890
891 /* Pointers to actual exponents to be used - either the unblinded
892 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000893 mbedtls_mpi *DP = &ctx->DP;
894 mbedtls_mpi *DQ = &ctx->DQ;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200895# else
Hanno Becker06811ce2017-05-03 15:10:34 +0100896 /* Temporary holding the blinded exponent (if used). */
897 mbedtls_mpi D_blind;
898
899 /* Pointer to actual exponent to be used - either the unblinded
900 * or the blinded one, depending on the presence of a PRNG. */
901 mbedtls_mpi *D = &ctx->D;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200902# endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100903
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100904 /* Temporaries holding the initial input and the double
905 * checked result; should be the same in the end. */
906 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000907
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200908 RSA_VALIDATE_RET(ctx != NULL);
909 RSA_VALIDATE_RET(input != NULL);
910 RSA_VALIDATE_RET(output != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500911
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200912 if (f_rng == NULL)
913 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200914
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200915 if (rsa_check_context(ctx, 1 /* private key checks */,
916 1 /* blinding on */) != 0) {
917 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +0100918 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100919
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200920# if defined(MBEDTLS_THREADING_C)
921 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0)
922 return ret;
923# endif
Janos Follathf9203b42017-03-22 15:13:15 +0000924
Hanno Becker06811ce2017-05-03 15:10:34 +0100925 /* MPI Initialization */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200926 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +0100927
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200928 mbedtls_mpi_init(&P1);
929 mbedtls_mpi_init(&Q1);
930 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +0000931
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200932# if defined(MBEDTLS_RSA_NO_CRT)
933 mbedtls_mpi_init(&D_blind);
934# else
935 mbedtls_mpi_init(&DP_blind);
936 mbedtls_mpi_init(&DQ_blind);
937# endif
Janos Follathe81102e2017-03-22 13:38:28 +0000938
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200939# if !defined(MBEDTLS_RSA_NO_CRT)
940 mbedtls_mpi_init(&TP);
941 mbedtls_mpi_init(&TQ);
942# endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200943
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200944 mbedtls_mpi_init(&I);
945 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +0100946
947 /* End of MPI initialization */
948
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200949 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
950 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200951 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
952 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000953 }
954
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200955 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +0100956
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200957 /*
958 * Blinding
959 * T = T * Vi mod N
960 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200961 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
962 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
963 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +0000964
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200965 /*
966 * Exponent blinding
967 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200968 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
969 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +0000970
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200971# if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200972 /*
973 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
974 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200975 MBEDTLS_MPI_CHK(
976 mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING, f_rng, p_rng));
977 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
978 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
979 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +0000980
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200981 D = &D_blind;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200982# else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200983 /*
984 * DP_blind = ( P - 1 ) * R + DP
985 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200986 MBEDTLS_MPI_CHK(
987 mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING, f_rng, p_rng));
988 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
989 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind, &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +0000990
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200991 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000992
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200993 /*
994 * DQ_blind = ( Q - 1 ) * R + DQ
995 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200996 MBEDTLS_MPI_CHK(
997 mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING, f_rng, p_rng));
998 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
999 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind, &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001000
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001001 DQ = &DQ_blind;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001002# endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001003
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001004# if defined(MBEDTLS_RSA_NO_CRT)
1005 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
1006# else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001007 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001008 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001010 * TP = input ^ dP mod P
1011 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001012 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001013
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001014 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1015 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
1017 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001018 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001020 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1021 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1022 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001023
1024 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001025 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001027 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1028 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
1029# endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001030
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001031 /*
1032 * Unblind
1033 * T = T * Vf mod N
1034 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001035 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1036 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Hanno Becker2dec5e82017-10-03 07:49:52 +01001038 /* Verify the result to prevent glitching attacks. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001039 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E, &ctx->N, &ctx->RN));
1040 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001041 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1042 goto cleanup;
1043 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001044
Paul Bakker5121ce52009-01-03 21:22:43 +00001045 olen = ctx->len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001046 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
1048cleanup:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001049# if defined(MBEDTLS_THREADING_C)
1050 if (mbedtls_mutex_unlock(&ctx->mutex) != 0)
1051 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1052# endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001053
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001054 mbedtls_mpi_free(&P1);
1055 mbedtls_mpi_free(&Q1);
1056 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001057
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001058# if defined(MBEDTLS_RSA_NO_CRT)
1059 mbedtls_mpi_free(&D_blind);
1060# else
1061 mbedtls_mpi_free(&DP_blind);
1062 mbedtls_mpi_free(&DQ_blind);
1063# endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001064
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001065 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001066
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001067# if !defined(MBEDTLS_RSA_NO_CRT)
1068 mbedtls_mpi_free(&TP);
1069 mbedtls_mpi_free(&TQ);
1070# endif
Hanno Becker06811ce2017-05-03 15:10:34 +01001071
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001072 mbedtls_mpi_free(&C);
1073 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001074
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001075 if (ret != 0 && ret >= -0x007f)
1076 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001078 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001079}
1080
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001081# if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001082/**
1083 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1084 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001085 * \param dst buffer to mask
1086 * \param dlen length of destination buffer
1087 * \param src source of the mask generation
1088 * \param slen length of the source buffer
1089 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001090 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001091static int mgf_mask(unsigned char *dst,
1092 size_t dlen,
1093 unsigned char *src,
1094 size_t slen,
1095 mbedtls_md_context_t *md_ctx)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001098 unsigned char counter[4];
1099 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001100 unsigned int hlen;
1101 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001102 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001103
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001104 memset(mask, 0, MBEDTLS_MD_MAX_SIZE);
1105 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001106
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001107 hlen = mbedtls_md_get_size(md_ctx->md_info);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001108
Simon Butcher02037452016-03-01 21:19:12 +00001109 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001110 p = dst;
1111
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001112 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001113 use_len = hlen;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001114 if (dlen < hlen)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001115 use_len = dlen;
1116
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001117 if ((ret = mbedtls_md_starts(md_ctx)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001118 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001119 if ((ret = mbedtls_md_update(md_ctx, src, slen)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001120 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001121 if ((ret = mbedtls_md_update(md_ctx, counter, 4)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001122 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001123 if ((ret = mbedtls_md_finish(md_ctx, mask)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001124 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001125
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001126 for (i = 0; i < use_len; ++i)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001127 *p++ ^= mask[i];
1128
1129 counter[3]++;
1130
1131 dlen -= use_len;
1132 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001133
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001134exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001135 mbedtls_platform_zeroize(mask, sizeof(mask));
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001136
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001137 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001138}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001139# endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001140
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001141# if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001142/*
1143 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1144 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001145int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1146 int (*f_rng)(void *, unsigned char *, size_t),
1147 void *p_rng,
1148 const unsigned char *label,
1149 size_t label_len,
1150 size_t ilen,
1151 const unsigned char *input,
1152 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001153{
1154 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001156 unsigned char *p = output;
1157 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 const mbedtls_md_info_t *md_info;
1159 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001160
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001161 RSA_VALIDATE_RET(ctx != NULL);
1162 RSA_VALIDATE_RET(output != NULL);
1163 RSA_VALIDATE_RET(ilen == 0 || input != NULL);
1164 RSA_VALIDATE_RET(label_len == 0 || label != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001165
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001166 if (f_rng == NULL)
1167 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01001168
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001169 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t)ctx->hash_id);
1170 if (md_info == NULL)
1171 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01001172
1173 olen = ctx->len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001174 hlen = mbedtls_md_get_size(md_info);
Paul Bakkerb3869132013-02-28 17:21:01 +01001175
Simon Butcher02037452016-03-01 21:19:12 +00001176 /* first comparison checks for overflow */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001177 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2)
1178 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01001179
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001180 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001181
1182 *p++ = 0;
1183
Simon Butcher02037452016-03-01 21:19:12 +00001184 /* Generate a random octet string seed */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001185 if ((ret = f_rng(p_rng, p, hlen)) != 0)
1186 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
Paul Bakkerb3869132013-02-28 17:21:01 +01001187
1188 p += hlen;
1189
Simon Butcher02037452016-03-01 21:19:12 +00001190 /* Construct DB */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001191 if ((ret = mbedtls_md(md_info, label, label_len, p)) != 0)
1192 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001193 p += hlen;
1194 p += olen - 2 * hlen - 2 - ilen;
1195 *p++ = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001196 if (ilen != 0)
1197 memcpy(p, input, ilen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001198
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001199 mbedtls_md_init(&md_ctx);
1200 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001201 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001202
Simon Butcher02037452016-03-01 21:19:12 +00001203 /* maskedDB: Apply dbMask to DB */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001204 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1205 &md_ctx)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001206 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001207
Simon Butcher02037452016-03-01 21:19:12 +00001208 /* maskedSeed: Apply seedMask to seed */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001209 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1210 &md_ctx)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001211 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001212
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001213exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001214 mbedtls_md_free(&md_ctx);
Paul Bakkerb3869132013-02-28 17:21:01 +01001215
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001216 if (ret != 0)
1217 return ret;
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001218
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001219 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001220}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001221# endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001222
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001223# if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001224/*
1225 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1226 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001227int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1228 int (*f_rng)(void *,
1229 unsigned char *,
1230 size_t),
1231 void *p_rng,
1232 size_t ilen,
1233 const unsigned char *input,
1234 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001235{
1236 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001238 unsigned char *p = output;
1239
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001240 RSA_VALIDATE_RET(ctx != NULL);
1241 RSA_VALIDATE_RET(output != NULL);
1242 RSA_VALIDATE_RET(ilen == 0 || input != NULL);
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001243
Paul Bakkerb3869132013-02-28 17:21:01 +01001244 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001245
Simon Butcher02037452016-03-01 21:19:12 +00001246 /* first comparison checks for overflow */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001247 if (ilen + 11 < ilen || olen < ilen + 11)
1248 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01001249
1250 nb_pad = olen - 3 - ilen;
1251
1252 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001253
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001254 if (f_rng == NULL)
1255 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001256
1257 *p++ = MBEDTLS_RSA_CRYPT;
1258
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001259 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001260 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001261
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001262 do {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001263 ret = f_rng(p_rng, p, 1);
1264 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001265
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001266 /* Check if RNG failed to generate data */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001267 if (rng_dl == 0 || ret != 0)
1268 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
Paul Bakkerb3869132013-02-28 17:21:01 +01001269
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001270 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001271 }
1272
1273 *p++ = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001274 if (ilen != 0)
1275 memcpy(p, input, ilen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001276
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001277 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001278}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001279# endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001280
Paul Bakker5121ce52009-01-03 21:22:43 +00001281/*
1282 * Add the message padding, then do an RSA operation
1283 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001284int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1285 int (*f_rng)(void *, unsigned char *, size_t),
1286 void *p_rng,
1287 size_t ilen,
1288 const unsigned char *input,
1289 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001290{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001291 RSA_VALIDATE_RET(ctx != NULL);
1292 RSA_VALIDATE_RET(output != NULL);
1293 RSA_VALIDATE_RET(ilen == 0 || input != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001294
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001295 switch (ctx->padding) {
1296# if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 case MBEDTLS_RSA_PKCS_V15:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001298 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng, ilen,
1299 input, output);
1300# endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001301
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001302# if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 case MBEDTLS_RSA_PKCS_V21:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001304 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1305 ilen, input, output);
1306# endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
1308 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001309 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001311}
1312
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001313# if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001314/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001315 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001317int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1318 int (*f_rng)(void *, unsigned char *, size_t),
1319 void *p_rng,
1320 const unsigned char *label,
1321 size_t label_len,
1322 size_t *olen,
1323 const unsigned char *input,
1324 unsigned char *output,
1325 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001326{
Janos Follath24eed8d2019-11-22 13:21:35 +00001327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001328 size_t ilen, i, pad_len;
1329 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1331 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001332 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 const mbedtls_md_info_t *md_info;
1334 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001335
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001336 RSA_VALIDATE_RET(ctx != NULL);
1337 RSA_VALIDATE_RET(output_max_len == 0 || output != NULL);
1338 RSA_VALIDATE_RET(label_len == 0 || label != NULL);
1339 RSA_VALIDATE_RET(input != NULL);
1340 RSA_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001341
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001342 /*
1343 * Parameters sanity checks
1344 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001345 if (ctx->padding != MBEDTLS_RSA_PKCS_V21)
1346 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +00001347
1348 ilen = ctx->len;
1349
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001350 if (ilen < 16 || ilen > sizeof(buf))
1351 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001353 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t)ctx->hash_id);
1354 if (md_info == NULL)
1355 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001356
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001357 hlen = mbedtls_md_get_size(md_info);
Janos Follathc17cda12016-02-11 11:08:18 +00001358
1359 // checking for integer underflow
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001360 if (2 * hlen + 2 > ilen)
1361 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Janos Follathc17cda12016-02-11 11:08:18 +00001362
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001363 /*
1364 * RSA operation
1365 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001366 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001368 if (ret != 0)
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001369 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001371 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001372 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001373 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001374 mbedtls_md_init(&md_ctx);
1375 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
1376 mbedtls_md_free(&md_ctx);
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001377 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001378 }
1379
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001380 /* seed: Apply seedMask to maskedSeed */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001381 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1382 &md_ctx)) != 0 ||
1383 /* DB: Apply dbMask to maskedDB */
1384 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1385 &md_ctx)) != 0) {
1386 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001387 goto cleanup;
1388 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001389
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001390 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001391
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001392 /* Generate lHash */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001393 if ((ret = mbedtls_md(md_info, label, label_len, lhash)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001394 goto cleanup;
1395
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001396 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001397 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001398 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001400 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001402 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001403
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001404 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001405
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001406 /* Check lHash */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001407 for (i = 0; i < hlen; i++)
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001408 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001409
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001410 /* Get zero-padding len, but always read till end of buffer
1411 * (minus one, for the 01 byte) */
1412 pad_len = 0;
1413 pad_done = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001414 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001415 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001416 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001417 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001418
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001419 p += pad_len;
1420 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001421
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001422 /*
1423 * The only information "leaked" is whether the padding was correct or not
1424 * (eg, no data is copied if it was not correct). This meets the
1425 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1426 * the different error conditions.
1427 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001428 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001429 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1430 goto cleanup;
1431 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001432
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001433 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001434 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1435 goto cleanup;
1436 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001437
1438 *olen = ilen - (p - buf);
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001439 if (*olen != 0)
1440 memcpy(output, p, *olen);
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001441 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001442
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001443cleanup:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001444 mbedtls_platform_zeroize(buf, sizeof(buf));
1445 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001446
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001447 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001448}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001449# endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001450
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001451# if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001452/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1453 *
1454 * \param value The value to analyze.
1455 * \return Zero if \p value is zero, otherwise all-bits-one.
1456 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001457static unsigned all_or_nothing_int(unsigned value)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001458{
1459 /* MSVC has a warning about unary minus on unsigned, but this is
1460 * well-defined and precisely what we want to do here */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001461# if defined(_MSC_VER)
1462# pragma warning(push)
1463# pragma warning(disable : 4146)
1464# endif
1465 return (-((value | -value) >> (sizeof(value) * 8 - 1)));
1466# if defined(_MSC_VER)
1467# pragma warning(pop)
1468# endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001469}
1470
1471/** Check whether a size is out of bounds, without branches.
1472 *
1473 * This is equivalent to `size > max`, but is likely to be compiled to
1474 * to code using bitwise operation rather than a branch.
1475 *
1476 * \param size Size to check.
1477 * \param max Maximum desired value for \p size.
1478 * \return \c 0 if `size <= max`.
1479 * \return \c 1 if `size > max`.
1480 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001481static unsigned size_greater_than(size_t size, size_t max)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001482{
1483 /* Return the sign bit (1 for negative) of (max - size). */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001484 return ((max - size) >> (sizeof(size_t) * 8 - 1));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001485}
1486
1487/** Choose between two integer values, without branches.
1488 *
1489 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1490 * to code using bitwise operation rather than a branch.
1491 *
1492 * \param cond Condition to test.
1493 * \param if1 Value to use if \p cond is nonzero.
1494 * \param if0 Value to use if \p cond is zero.
1495 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1496 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001497static unsigned if_int(unsigned cond, unsigned if1, unsigned if0)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001498{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001499 unsigned mask = all_or_nothing_int(cond);
1500 return ((mask & if1) | (~mask & if0));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001501}
1502
1503/** Shift some data towards the left inside a buffer without leaking
1504 * the length of the data through side channels.
1505 *
1506 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1507 * ```
1508 * memmove(start, start + offset, total - offset);
1509 * memset(start + offset, 0, total - offset);
1510 * ```
1511 * but it strives to use a memory access pattern (and thus total timing)
1512 * that does not depend on \p offset. This timing independence comes at
1513 * the expense of performance.
1514 *
1515 * \param start Pointer to the start of the buffer.
1516 * \param total Total size of the buffer.
1517 * \param offset Offset from which to copy \p total - \p offset bytes.
1518 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001519static void mem_move_to_left(void *start, size_t total, size_t offset)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001520{
1521 volatile unsigned char *buf = start;
1522 size_t i, n;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001523 if (total == 0)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001524 return;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001525 for (i = 0; i < total; i++) {
1526 unsigned no_op = size_greater_than(total - offset, i);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001527 /* The first `total - offset` passes are a no-op. The last
1528 * `offset` passes shift the data one byte to the left and
1529 * zero out the last byte. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001530 for (n = 0; n < total - 1; n++) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001531 unsigned char current = buf[n];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001532 unsigned char next = buf[n + 1];
1533 buf[n] = if_int(no_op, current, next);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001534 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001535 buf[total - 1] = if_int(no_op, buf[total - 1], 0);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001536 }
1537}
1538
Paul Bakkerb3869132013-02-28 17:21:01 +01001539/*
1540 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1541 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001542int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1543 int (*f_rng)(void *,
1544 unsigned char *,
1545 size_t),
1546 void *p_rng,
1547 size_t *olen,
1548 const unsigned char *input,
1549 unsigned char *output,
1550 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001551{
Janos Follath24eed8d2019-11-22 13:21:35 +00001552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001553 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001555 /* The following variables take sensitive values: their value must
1556 * not leak into the observable behavior of the function other than
1557 * the designated outputs (output, olen, return value). Otherwise
1558 * this would open the execution of the function to
1559 * side-channel-based variants of the Bleichenbacher padding oracle
1560 * attack. Potential side channels include overall timing, memory
1561 * access patterns (especially visible to an adversary who has access
1562 * to a shared memory cache), and branches (especially visible to
1563 * an adversary who has access to a shared code cache or to a shared
1564 * branch predictor). */
1565 size_t pad_count = 0;
1566 unsigned bad = 0;
1567 unsigned char pad_done = 0;
1568 size_t plaintext_size = 0;
1569 unsigned output_too_large;
1570
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001571 RSA_VALIDATE_RET(ctx != NULL);
1572 RSA_VALIDATE_RET(output_max_len == 0 || output != NULL);
1573 RSA_VALIDATE_RET(input != NULL);
1574 RSA_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001575
1576 ilen = ctx->len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001577 plaintext_max_size =
1578 (output_max_len > ilen - 11 ? ilen - 11 : output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001579
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001580 if (ctx->padding != MBEDTLS_RSA_PKCS_V15)
1581 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01001582
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001583 if (ilen < 16 || ilen > sizeof(buf))
1584 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01001585
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001586 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakkerb3869132013-02-28 17:21:01 +01001587
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001588 if (ret != 0)
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001589 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001590
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001591 /* Check and get padding length in constant time and constant
1592 * memory trace. The first byte must be 0. */
1593 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001594
Thomas Daubney34733082021-05-12 09:24:29 +01001595 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001596 * where PS must be at least 8 nonzero bytes. */
Thomas Daubney34733082021-05-12 09:24:29 +01001597 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001598
Thomas Daubney34733082021-05-12 09:24:29 +01001599 /* Read the whole buffer. Set pad_done to nonzero if we find
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001600 * the 0x00 byte and remember the padding length in pad_count. */
1601 for (i = 2; i < ilen; i++) {
1602 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Thomas Daubney34733082021-05-12 09:24:29 +01001603 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001604 }
1605
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001606 /* If pad_done is still zero, there's no data, only unfinished padding. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001607 bad |= if_int(pad_done, 0, 1);
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001608
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001609 /* There must be at least 8 bytes of padding. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001610 bad |= size_greater_than(8, pad_count);
Paul Bakker8804f692013-02-28 18:06:26 +01001611
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001612 /* If the padding is valid, set plaintext_size to the number of
1613 * remaining bytes after stripping the padding. If the padding
1614 * is invalid, avoid leaking this fact through the size of the
1615 * output: use the maximum message size that fits in the output
1616 * buffer. Do it without branches to avoid leaking the padding
1617 * validity through timing. RSA keys are small enough that all the
1618 * size_t values involved fit in unsigned int. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001619 plaintext_size = if_int(bad, (unsigned)plaintext_max_size,
1620 (unsigned)(ilen - pad_count - 3));
Paul Bakker060c5682009-01-12 21:48:39 +00001621
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001622 /* Set output_too_large to 0 if the plaintext fits in the output
1623 * buffer and to 1 otherwise. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001624 output_too_large = size_greater_than(plaintext_size, plaintext_max_size);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001625
1626 /* Set ret without branches to avoid timing attacks. Return:
1627 * - INVALID_PADDING if the padding is bad (bad != 0).
1628 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1629 * plaintext does not fit in the output buffer.
1630 * - 0 if the padding is correct. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001631 ret = -(int)if_int(bad, -MBEDTLS_ERR_RSA_INVALID_PADDING,
1632 if_int(output_too_large,
1633 -MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE, 0));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001634
1635 /* If the padding is bad or the plaintext is too large, zero the
1636 * data that we're about to copy to the output buffer.
1637 * We need to copy the same amount of data
1638 * from the same buffer whether the padding is good or not to
1639 * avoid leaking the padding validity through overall timing or
1640 * through memory or cache access patterns. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001641 bad = all_or_nothing_int(bad | output_too_large);
1642 for (i = 11; i < ilen; i++)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001643 buf[i] &= ~bad;
1644
1645 /* If the plaintext is too large, truncate it to the buffer size.
1646 * Copy anyway to avoid revealing the length through timing, because
1647 * revealing the length is as bad as revealing the padding validity
1648 * for a Bleichenbacher attack. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001649 plaintext_size = if_int(output_too_large, (unsigned)plaintext_max_size,
1650 (unsigned)plaintext_size);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001651
1652 /* Move the plaintext to the leftmost position where it can start in
1653 * the working buffer, i.e. make it start plaintext_max_size from
1654 * the end of the buffer. Do this with a memory access trace that
1655 * does not depend on the plaintext size. After this move, the
1656 * starting location of the plaintext is no longer sensitive
1657 * information. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001658 mem_move_to_left(buf + ilen - plaintext_max_size, plaintext_max_size,
1659 plaintext_max_size - plaintext_size);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001660
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001661 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1662 * buffer. If output_max_len is 0, then output may be an invalid pointer
1663 * and the result of memcpy() would be undefined; prevent undefined
1664 * behavior making sure to depend only on output_max_len (the size of the
1665 * user-provided output buffer), which is independent from plaintext
1666 * length, validity of padding, success of the decryption, and other
1667 * secrets. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001668 if (output_max_len != 0)
1669 memcpy(output, buf + ilen - plaintext_max_size, plaintext_max_size);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001670
1671 /* Report the amount of data we copied to the output buffer. In case
1672 * of errors (bad padding or output too large), the value of *olen
1673 * when this function returns is not specified. Making it equivalent
1674 * to the good case limits the risks of leaking the padding validity. */
1675 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001677cleanup:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001678 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001679
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001680 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001681}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001682# endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
1684/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001685 * Do an RSA operation, then remove the message padding
1686 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001687int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1688 int (*f_rng)(void *, unsigned char *, size_t),
1689 void *p_rng,
1690 size_t *olen,
1691 const unsigned char *input,
1692 unsigned char *output,
1693 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001694{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001695 RSA_VALIDATE_RET(ctx != NULL);
1696 RSA_VALIDATE_RET(output_max_len == 0 || output != NULL);
1697 RSA_VALIDATE_RET(input != NULL);
1698 RSA_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001699
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001700 switch (ctx->padding) {
1701# if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 case MBEDTLS_RSA_PKCS_V15:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001703 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(
1704 ctx, f_rng, p_rng, olen, input, output, output_max_len);
1705# endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001706
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001707# if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 case MBEDTLS_RSA_PKCS_V21:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001709 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1710 olen, input, output,
1711 output_max_len);
1712# endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001713
1714 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001715 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001716 }
1717}
1718
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001719# if defined(MBEDTLS_PKCS1_V21)
1720static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1721 int (*f_rng)(void *, unsigned char *, size_t),
1722 void *p_rng,
1723 mbedtls_md_type_t md_alg,
1724 unsigned int hashlen,
1725 const unsigned char *hash,
1726 int saltlen,
1727 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001728{
1729 size_t olen;
1730 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001731 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001732 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001733 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001734 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 const mbedtls_md_info_t *md_info;
1736 mbedtls_md_context_t md_ctx;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001737 RSA_VALIDATE_RET(ctx != NULL);
1738 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hashlen == 0) ||
1739 hash != NULL);
1740 RSA_VALIDATE_RET(sig != NULL);
Paul Bakkerb3869132013-02-28 17:21:01 +01001741
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001742 if (ctx->padding != MBEDTLS_RSA_PKCS_V21)
1743 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001744
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001745 if (f_rng == NULL)
1746 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01001747
1748 olen = ctx->len;
1749
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001750 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001751 /* Gather length of hash to sign */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001752 md_info = mbedtls_md_info_from_type(md_alg);
1753 if (md_info == NULL)
1754 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001755
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001756 if (hashlen != mbedtls_md_get_size(md_info))
1757 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01001758 }
1759
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001760 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t)ctx->hash_id);
1761 if (md_info == NULL)
1762 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01001763
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001764 hlen = mbedtls_md_get_size(md_info);
Paul Bakkerb3869132013-02-28 17:21:01 +01001765
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001766 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1767 /* Calculate the largest possible salt length, up to the hash size.
1768 * Normally this is the hash length, which is the maximum salt length
1769 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1770 * enough room, use the maximum salt length that fits. The constraint is
1771 * that the hash length plus the salt length plus 2 bytes must be at
1772 * most the key length. This complies with FIPS 186-4 §5.5 (e) and RFC
1773 * 8017 (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001774 min_slen = hlen - 2;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001775 if (olen < hlen + min_slen + 2)
1776 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1777 else if (olen >= hlen + hlen + 2)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001778 slen = hlen;
1779 else
1780 slen = olen - hlen - 2;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001781 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1782 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1783 } else {
1784 slen = (size_t)saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001785 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001786
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001787 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001788
Simon Butcher02037452016-03-01 21:19:12 +00001789 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001790 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001791 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001792 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001793
1794 /* Generate salt of length slen in place in the encoded message */
1795 salt = p;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001796 if ((ret = f_rng(p_rng, salt, slen)) != 0)
1797 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
Cédric Meuter668a78d2020-04-30 11:57:04 +02001798
Paul Bakkerb3869132013-02-28 17:21:01 +01001799 p += slen;
1800
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001801 mbedtls_md_init(&md_ctx);
1802 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001803 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001804
Simon Butcher02037452016-03-01 21:19:12 +00001805 /* Generate H = Hash( M' ) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001806 if ((ret = mbedtls_md_starts(&md_ctx)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001807 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001808 if ((ret = mbedtls_md_update(&md_ctx, p, 8)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001809 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001810 if ((ret = mbedtls_md_update(&md_ctx, hash, hashlen)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001811 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001812 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001813 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001814 if ((ret = mbedtls_md_finish(&md_ctx, p)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001815 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001816
Simon Butcher02037452016-03-01 21:19:12 +00001817 /* Compensate for boundary condition when applying mask */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001818 if (msb % 8 == 0)
Paul Bakkerb3869132013-02-28 17:21:01 +01001819 offset = 1;
1820
Simon Butcher02037452016-03-01 21:19:12 +00001821 /* maskedDB: Apply dbMask to DB */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001822 if ((ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
1823 &md_ctx)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001824 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001825
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001826 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1827 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001828
1829 p += hlen;
1830 *p++ = 0xBC;
1831
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001832exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001833 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001834
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001835 if (ret != 0)
1836 return ret;
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001837
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001838 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001839}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001840
1841/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001842 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1843 * the option to pass in the salt length.
1844 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001845int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1846 int (*f_rng)(void *,
1847 unsigned char *,
1848 size_t),
1849 void *p_rng,
1850 mbedtls_md_type_t md_alg,
1851 unsigned int hashlen,
1852 const unsigned char *hash,
1853 int saltlen,
1854 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001855{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001856 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg, hashlen, hash,
1857 saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001858}
1859
Cédric Meuterf3fab332020-04-25 11:30:45 +02001860/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001861 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1862 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001863int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1864 int (*f_rng)(void *, unsigned char *, size_t),
1865 void *p_rng,
1866 mbedtls_md_type_t md_alg,
1867 unsigned int hashlen,
1868 const unsigned char *hash,
1869 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001870{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001871 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg, hashlen, hash,
1872 MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001873}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001874# endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001875
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001876# if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001877/*
1878 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1879 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001880
1881/* Construct a PKCS v1.5 encoding of a hashed message
1882 *
1883 * This is used both for signature generation and verification.
1884 *
1885 * Parameters:
1886 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001887 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001888 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001889 * - hash: Buffer containing the hashed message or the raw data.
1890 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001891 * - dst: Buffer to hold the encoded message.
1892 *
1893 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001894 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001895 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001896 *
1897 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001898static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1899 unsigned int hashlen,
1900 const unsigned char *hash,
1901 size_t dst_len,
1902 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001903{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001904 size_t oid_size = 0;
1905 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001906 unsigned char *p = dst;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001907 const char *oid = NULL;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001908
1909 /* Are we signing hashed or raw data? */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001910 if (md_alg != MBEDTLS_MD_NONE) {
1911 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1912 if (md_info == NULL)
1913 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001914
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001915 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0)
1916 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001917
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001918 if (hashlen != mbedtls_md_get_size(md_info))
1919 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001920
1921 /* Double-check that 8 + hashlen + oid_size can be used as a
1922 * 1-byte ASN.1 length encoding and that there's no overflow. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001923 if (8 + hashlen + oid_size >= 0x80 || 10 + hashlen < hashlen ||
1924 10 + hashlen + oid_size < 10 + hashlen)
1925 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001926
1927 /*
1928 * Static bounds check:
1929 * - Need 10 bytes for five tag-length pairs.
1930 * (Insist on 1-byte length encodings to protect against variants of
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001931 * Bleichenbacher's forgery attack against lax PKCS#1v1.5
1932 * verification)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001933 * - Need hashlen bytes for hash
1934 * - Need oid_size bytes for hash alg OID.
1935 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001936 if (nb_pad < 10 + hashlen + oid_size)
1937 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001938 nb_pad -= 10 + hashlen + oid_size;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001939 } else {
1940 if (nb_pad < hashlen)
1941 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001942
1943 nb_pad -= hashlen;
1944 }
1945
Hanno Becker2b2f8982017-09-27 17:10:03 +01001946 /* Need space for signature header and padding delimiter (3 bytes),
1947 * and 8 bytes for the minimal padding */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001948 if (nb_pad < 3 + 8)
1949 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001950 nb_pad -= 3;
1951
1952 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001953 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001954
1955 /* Write signature header and padding */
1956 *p++ = 0;
1957 *p++ = MBEDTLS_RSA_SIGN;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001958 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001959 p += nb_pad;
1960 *p++ = 0;
1961
1962 /* Are we signing raw data? */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001963 if (md_alg == MBEDTLS_MD_NONE) {
1964 memcpy(p, hash, hashlen);
1965 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001966 }
1967
1968 /* Signing hashed data, add corresponding ASN.1 structure
1969 *
1970 * DigestInfo ::= SEQUENCE {
1971 * digestAlgorithm DigestAlgorithmIdentifier,
1972 * digest Digest }
1973 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1974 * Digest ::= OCTET STRING
1975 *
1976 * Schematic:
1977 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1978 * TAG-NULL + LEN [ NULL ] ]
1979 * TAG-OCTET + LEN [ HASH ] ]
1980 */
1981 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001982 *p++ = (unsigned char)(0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001983 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001984 *p++ = (unsigned char)(0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001985 *p++ = MBEDTLS_ASN1_OID;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001986 *p++ = (unsigned char)oid_size;
1987 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001988 p += oid_size;
1989 *p++ = MBEDTLS_ASN1_NULL;
1990 *p++ = 0x00;
1991 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001992 *p++ = (unsigned char)hashlen;
1993 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001994 p += hashlen;
1995
1996 /* Just a sanity-check, should be automatic
1997 * after the initial bounds check. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001998 if (p != dst + dst_len) {
1999 mbedtls_platform_zeroize(dst, dst_len);
2000 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002001 }
2002
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002003 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002004}
2005
Paul Bakkerb3869132013-02-28 17:21:01 +01002006/*
2007 * Do an RSA operation to sign the message digest
2008 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002009int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2010 int (*f_rng)(void *,
2011 unsigned char *,
2012 size_t),
2013 void *p_rng,
2014 mbedtls_md_type_t md_alg,
2015 unsigned int hashlen,
2016 const unsigned char *hash,
2017 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002018{
Janos Follath24eed8d2019-11-22 13:21:35 +00002019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002020 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002021
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002022 RSA_VALIDATE_RET(ctx != NULL);
2023 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hashlen == 0) ||
2024 hash != NULL);
2025 RSA_VALIDATE_RET(sig != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002026
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002027 if (ctx->padding != MBEDTLS_RSA_PKCS_V15)
2028 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002029
Hanno Beckerfdf38032017-09-06 12:35:55 +01002030 /*
2031 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2032 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002033
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002034 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, ctx->len,
2035 sig)) != 0)
2036 return ret;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002037
Hanno Beckerfdf38032017-09-06 12:35:55 +01002038 /* Private key operation
2039 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002040 * In order to prevent Lenstra's attack, make the signature in a
2041 * temporary buffer and check it before returning it.
2042 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002043
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002044 sig_try = mbedtls_calloc(1, ctx->len);
2045 if (sig_try == NULL)
2046 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002047
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002048 verif = mbedtls_calloc(1, ctx->len);
2049 if (verif == NULL) {
2050 mbedtls_free(sig_try);
2051 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002052 }
2053
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002054 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2055 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002056
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002057 if (mbedtls_safer_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002058 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2059 goto cleanup;
2060 }
2061
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002062 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002063
2064cleanup:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002065 mbedtls_free(sig_try);
2066 mbedtls_free(verif);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002067
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002068 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002069}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002070# endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002071
2072/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 * Do an RSA operation to sign the message digest
2074 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002075int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2076 int (*f_rng)(void *, unsigned char *, size_t),
2077 void *p_rng,
2078 mbedtls_md_type_t md_alg,
2079 unsigned int hashlen,
2080 const unsigned char *hash,
2081 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002082{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002083 RSA_VALIDATE_RET(ctx != NULL);
2084 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hashlen == 0) ||
2085 hash != NULL);
2086 RSA_VALIDATE_RET(sig != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002087
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002088 switch (ctx->padding) {
2089# if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 case MBEDTLS_RSA_PKCS_V15:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002091 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng, md_alg,
2092 hashlen, hash, sig);
2093# endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002094
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002095# if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 case MBEDTLS_RSA_PKCS_V21:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002097 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2098 hashlen, hash, sig);
2099# endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00002100
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002102 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002104}
2105
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002106# if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002107/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002108 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002110int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2111 mbedtls_md_type_t md_alg,
2112 unsigned int hashlen,
2113 const unsigned char *hash,
2114 mbedtls_md_type_t mgf1_hash_id,
2115 int expected_salt_len,
2116 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002117{
Janos Follath24eed8d2019-11-22 13:21:35 +00002118 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002119 size_t siglen;
2120 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002121 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002123 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002124 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002125 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 const mbedtls_md_info_t *md_info;
2127 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002128 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002129
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002130 RSA_VALIDATE_RET(ctx != NULL);
2131 RSA_VALIDATE_RET(sig != NULL);
2132 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hashlen == 0) ||
2133 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002134
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 siglen = ctx->len;
2136
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002137 if (siglen < 16 || siglen > sizeof(buf))
2138 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +00002139
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002140 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002142 if (ret != 0)
2143 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002144
2145 p = buf;
2146
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002147 if (buf[siglen - 1] != 0xBC)
2148 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002149
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002150 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002151 /* Gather length of hash to sign */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002152 md_info = mbedtls_md_info_from_type(md_alg);
2153 if (md_info == NULL)
2154 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002155
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002156 if (hashlen != mbedtls_md_get_size(md_info))
2157 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01002158 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002159
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002160 md_info = mbedtls_md_info_from_type(mgf1_hash_id);
2161 if (md_info == NULL)
2162 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002163
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002164 hlen = mbedtls_md_get_size(md_info);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002165
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002166 memset(zeros, 0, 8);
Paul Bakker53019ae2011-03-25 13:58:48 +00002167
Simon Butcher02037452016-03-01 21:19:12 +00002168 /*
2169 * Note: EMSA-PSS verification is over the length of N - 1 bits
2170 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002171 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002172
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002173 if (buf[0] >> (8 - siglen * 8 + msb))
2174 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002175
Simon Butcher02037452016-03-01 21:19:12 +00002176 /* Compensate for boundary condition when applying mask */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002177 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002178 p++;
2179 siglen -= 1;
2180 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002181
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002182 if (siglen < hlen + 2)
2183 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine139108a2017-10-18 19:03:42 +02002184 hash_start = p + siglen - hlen - 1;
2185
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002186 mbedtls_md_init(&md_ctx);
2187 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002188 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002189
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002190 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, &md_ctx);
2191 if (ret != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002192 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002193
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002194 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002195
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002196 while (p < hash_start - 1 && *p == 0)
Paul Bakkerb3869132013-02-28 17:21:01 +01002197 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002198
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002199 if (*p++ != 0x01) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002200 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2201 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002202 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002203
Gilles Peskine6a54b022017-10-17 19:02:13 +02002204 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002205
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002206 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2207 observed_salt_len != (size_t)expected_salt_len) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002208 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2209 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002210 }
2211
Simon Butcher02037452016-03-01 21:19:12 +00002212 /*
2213 * Generate H = Hash( M' )
2214 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002215 ret = mbedtls_md_starts(&md_ctx);
2216 if (ret != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002217 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002218 ret = mbedtls_md_update(&md_ctx, zeros, 8);
2219 if (ret != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002220 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002221 ret = mbedtls_md_update(&md_ctx, hash, hashlen);
2222 if (ret != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002223 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002224 ret = mbedtls_md_update(&md_ctx, p, observed_salt_len);
2225 if (ret != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002226 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002227 ret = mbedtls_md_finish(&md_ctx, result);
2228 if (ret != 0)
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002229 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002230
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002231 if (memcmp(hash_start, result, hlen) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002232 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002233 goto exit;
2234 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002235
2236exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002237 mbedtls_md_free(&md_ctx);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002238
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002239 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002240}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002241
2242/*
2243 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2244 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002245int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2246 mbedtls_md_type_t md_alg,
2247 unsigned int hashlen,
2248 const unsigned char *hash,
2249 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002250{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002251 mbedtls_md_type_t mgf1_hash_id;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002252 RSA_VALIDATE_RET(ctx != NULL);
2253 RSA_VALIDATE_RET(sig != NULL);
2254 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hashlen == 0) ||
2255 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002256
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002257 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE) ?
2258 (mbedtls_md_type_t)ctx->hash_id :
2259 md_alg;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002260
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002261 return (mbedtls_rsa_rsassa_pss_verify_ext(ctx, md_alg, hashlen, hash,
2262 mgf1_hash_id,
2263 MBEDTLS_RSA_SALT_LEN_ANY, sig));
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002264}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002265# endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002266
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002267# if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002268/*
2269 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2270 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002271int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2272 mbedtls_md_type_t md_alg,
2273 unsigned int hashlen,
2274 const unsigned char *hash,
2275 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002276{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002277 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002278 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002279 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002280
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002281 RSA_VALIDATE_RET(ctx != NULL);
2282 RSA_VALIDATE_RET(sig != NULL);
2283 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hashlen == 0) ||
2284 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002285
2286 sig_len = ctx->len;
2287
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002288 /*
2289 * Prepare expected PKCS1 v1.5 encoding of hash.
2290 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002291
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002292 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2293 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002294 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2295 goto cleanup;
2296 }
2297
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002298 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2299 encoded_expected)) != 0)
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002300 goto cleanup;
2301
2302 /*
2303 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2304 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002305
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002306 ret = mbedtls_rsa_public(ctx, sig, encoded);
2307 if (ret != 0)
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002308 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002309
Simon Butcher02037452016-03-01 21:19:12 +00002310 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002311 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002312 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002313
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002314 if ((ret = mbedtls_safer_memcmp(encoded, encoded_expected, sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002315 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2316 goto cleanup;
2317 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002318
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002319cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002320
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002321 if (encoded != NULL) {
2322 mbedtls_platform_zeroize(encoded, sig_len);
2323 mbedtls_free(encoded);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002324 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002325
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002326 if (encoded_expected != NULL) {
2327 mbedtls_platform_zeroize(encoded_expected, sig_len);
2328 mbedtls_free(encoded_expected);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002329 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002330
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002331 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002332}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002333# endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002334
2335/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002336 * Do an RSA operation and check the message digest
2337 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002338int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2339 mbedtls_md_type_t md_alg,
2340 unsigned int hashlen,
2341 const unsigned char *hash,
2342 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002343{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002344 RSA_VALIDATE_RET(ctx != NULL);
2345 RSA_VALIDATE_RET(sig != NULL);
2346 RSA_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hashlen == 0) ||
2347 hash != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002348
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002349 switch (ctx->padding) {
2350# if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 case MBEDTLS_RSA_PKCS_V15:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002352 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg, hashlen,
2353 hash, sig);
2354# endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002355
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002356# if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 case MBEDTLS_RSA_PKCS_V21:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002358 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg, hashlen, hash,
2359 sig);
2360# endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002361
2362 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002363 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002364 }
2365}
2366
2367/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002368 * Copy the components of an RSA key
2369 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002370int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002371{
Janos Follath24eed8d2019-11-22 13:21:35 +00002372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002373 RSA_VALIDATE_RET(dst != NULL);
2374 RSA_VALIDATE_RET(src != NULL);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002375
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002376 dst->len = src->len;
2377
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002378 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2379 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002380
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002381 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2382 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2383 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002384
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002385# if !defined(MBEDTLS_RSA_NO_CRT)
2386 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2387 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2388 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2389 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2390 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
2391# endif
Hanno Becker33c30a02017-08-23 07:00:22 +01002392
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002393 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002394
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002395 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2396 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002397
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002398 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002399 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002400
2401cleanup:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002402 if (ret != 0)
2403 mbedtls_rsa_free(dst);
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002404
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002405 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002406}
2407
2408/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 * Free the components of an RSA key
2410 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002411void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002412{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002413 if (ctx == NULL)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002414 return;
2415
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002416 mbedtls_mpi_free(&ctx->Vi);
2417 mbedtls_mpi_free(&ctx->Vf);
2418 mbedtls_mpi_free(&ctx->RN);
2419 mbedtls_mpi_free(&ctx->D);
2420 mbedtls_mpi_free(&ctx->Q);
2421 mbedtls_mpi_free(&ctx->P);
2422 mbedtls_mpi_free(&ctx->E);
2423 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002424
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002425# if !defined(MBEDTLS_RSA_NO_CRT)
2426 mbedtls_mpi_free(&ctx->RQ);
2427 mbedtls_mpi_free(&ctx->RP);
2428 mbedtls_mpi_free(&ctx->QP);
2429 mbedtls_mpi_free(&ctx->DQ);
2430 mbedtls_mpi_free(&ctx->DP);
2431# endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker33c30a02017-08-23 07:00:22 +01002432
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002433# if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002434 /* Free the mutex, but only if it hasn't been freed already. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002435 if (ctx->ver != 0) {
2436 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002437 ctx->ver = 0;
2438 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002439# endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002440}
2441
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002442# endif /* !MBEDTLS_RSA_ALT */
Hanno Beckerab377312017-08-23 16:24:51 +01002443
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002444# if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002446# include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002447
2448/*
2449 * Example RSA-1024 keypair, for test purposes
2450 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002451# define KEY_LEN 128
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002453# define RSA_N \
2454 "9292758453063D803DD603D5E777D788" \
2455 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2456 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2457 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2458 "93A89813FBF3C4F8066D2D800F7C38A8" \
2459 "1AE31942917403FF4946B0A83D3D3E05" \
2460 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2461 "5E94BB77B07507233A0BC7BAC8F90F79"
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002463# define RSA_E "10001"
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002465# define RSA_D \
2466 "24BF6185468786FDD303083D25E64EFC" \
2467 "66CA472BC44D253102F8B4A9D3BFA750" \
2468 "91386C0077937FE33FA3252D28855837" \
2469 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2470 "DF79C5CE07EE72C7F123142198164234" \
2471 "CABB724CF78B8173B9F880FC86322407" \
2472 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2473 "071513A1E85B5DFA031F21ECAE91A34D"
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002475# define RSA_P \
2476 "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2477 "2C01CAD19EA484A87EA4377637E75500" \
2478 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2479 "C3D9E75E1EFC42488BB4F1D13AC30A57"
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002481# define RSA_Q \
2482 "C000DF51A7C77AE8D7C7370C1FF55B69" \
2483 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2484 "910E4168387E3C30AA1E00C339A79508" \
2485 "8452DD96A9A5EA5D9DCA68DA636032AF"
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002487# define PT_LEN 24
2488# define RSA_PT \
2489 "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2490 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002492# if defined(MBEDTLS_PKCS1_V15)
2493static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002494{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002495# if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002496 size_t i;
2497
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002498 if (rng_state != NULL)
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002499 rng_state = NULL;
2500
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002501 for (i = 0; i < len; ++i)
2502 output[i] = rand();
2503# else
2504 if (rng_state != NULL)
2505 rng_state = NULL;
Paul Bakker48377d92013-08-30 12:06:24 +02002506
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002507 arc4random_buf(output, len);
2508# endif /* !OpenBSD && !NetBSD */
2509
2510 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002511}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002512# endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002513
Paul Bakker5121ce52009-01-03 21:22:43 +00002514/*
2515 * Checkup routine
2516 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002517int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002518{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002519 int ret = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002520# if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002521 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 unsigned char rsa_plaintext[PT_LEN];
2524 unsigned char rsa_decrypted[PT_LEN];
2525 unsigned char rsa_ciphertext[KEY_LEN];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002526# if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002527 unsigned char sha1sum[20];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002528# endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
Hanno Becker3a701162017-08-22 13:52:43 +01002530 mbedtls_mpi K;
2531
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002532 mbedtls_mpi_init(&K);
2533 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002535 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2536 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2537 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2538 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2539 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2540 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2541 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2542 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2543 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2544 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002545
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002546 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002548 if (verbose != 0)
2549 mbedtls_printf(" RSA key validation: ");
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002551 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2552 mbedtls_rsa_check_privkey(&rsa) != 0) {
2553 if (verbose != 0)
2554 mbedtls_printf("failed\n");
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Hanno Becker5bc87292017-05-03 15:09:31 +01002556 ret = 1;
2557 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 }
2559
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002560 if (verbose != 0)
2561 mbedtls_printf("passed\n PKCS#1 encryption : ");
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002563 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002564
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002565 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL, PT_LEN, rsa_plaintext,
2566 rsa_ciphertext) != 0) {
2567 if (verbose != 0)
2568 mbedtls_printf("failed\n");
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Hanno Becker5bc87292017-05-03 15:09:31 +01002570 ret = 1;
2571 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 }
2573
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002574 if (verbose != 0)
2575 mbedtls_printf("passed\n PKCS#1 decryption : ");
Paul Bakker5121ce52009-01-03 21:22:43 +00002576
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002577 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL, &len, rsa_ciphertext,
2578 rsa_decrypted, sizeof(rsa_decrypted)) != 0) {
2579 if (verbose != 0)
2580 mbedtls_printf("failed\n");
Paul Bakker5121ce52009-01-03 21:22:43 +00002581
Hanno Becker5bc87292017-05-03 15:09:31 +01002582 ret = 1;
2583 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 }
2585
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002586 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2587 if (verbose != 0)
2588 mbedtls_printf("failed\n");
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
Hanno Becker5bc87292017-05-03 15:09:31 +01002590 ret = 1;
2591 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 }
2593
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002594 if (verbose != 0)
2595 mbedtls_printf("passed\n");
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002596
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002597# if defined(MBEDTLS_SHA1_C)
2598 if (verbose != 0)
2599 mbedtls_printf(" PKCS#1 data sign : ");
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002601 if (mbedtls_sha1(rsa_plaintext, PT_LEN, sha1sum) != 0) {
2602 if (verbose != 0)
2603 mbedtls_printf("failed\n");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002604
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002605 return 1;
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002606 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002608 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL, MBEDTLS_MD_SHA1, 20, sha1sum,
2609 rsa_ciphertext) != 0) {
2610 if (verbose != 0)
2611 mbedtls_printf("failed\n");
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
Hanno Becker5bc87292017-05-03 15:09:31 +01002613 ret = 1;
2614 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 }
2616
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002617 if (verbose != 0)
2618 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002620 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20, sha1sum,
2621 rsa_ciphertext) != 0) {
2622 if (verbose != 0)
2623 mbedtls_printf("failed\n");
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Hanno Becker5bc87292017-05-03 15:09:31 +01002625 ret = 1;
2626 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 }
2628
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002629 if (verbose != 0)
2630 mbedtls_printf("passed\n");
2631# endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002633 if (verbose != 0)
2634 mbedtls_printf("\n");
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002635
Paul Bakker3d8fb632014-04-17 12:42:41 +02002636cleanup:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002637 mbedtls_mpi_free(&K);
2638 mbedtls_rsa_free(&rsa);
2639# else /* MBEDTLS_PKCS1_V15 */
2640 ((void)verbose);
2641# endif /* MBEDTLS_PKCS1_V15 */
2642 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002643}
2644
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002645# endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647#endif /* MBEDTLS_RSA_C */