blob: 499d14540522160d163645bfe7f5ee93adaf01ea [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Hanno Becker74716312017-10-02 10:00:37 +010021
Paul Bakker5121ce52009-01-03 21:22:43 +000022/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000023 * The following sources were referenced in the design of this implementation
24 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000025 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000026 * [1] A method for obtaining digital signatures and public-key cryptosystems
27 * R Rivest, A Shamir, and L Adleman
28 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
29 *
30 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
31 * Menezes, van Oorschot and Vanstone
32 *
Janos Follathe81102e2017-03-22 13:38:28 +000033 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
34 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
35 * Stefan Mangard
36 * https://arxiv.org/abs/1702.08719v2
37 *
Paul Bakker5121ce52009-01-03 21:22:43 +000038 */
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020042#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010049#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050051#include "mbedtls/platform_util.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000052
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <string.h>
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000061#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010065#else
Rich Evans00ab4702015-02-06 13:43:58 +000066#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020068#define mbedtls_calloc calloc
69#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010070#endif
71
Hanno Beckera565f542017-10-11 11:00:19 +010072#if !defined(MBEDTLS_RSA_ALT)
73
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010074#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +010075/* constant-time buffer comparison */
76static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
77{
78 size_t i;
79 const unsigned char *A = (const unsigned char *) a;
80 const unsigned char *B = (const unsigned char *) b;
81 unsigned char diff = 0;
82
83 for( i = 0; i < n; i++ )
84 diff |= A[i] ^ B[i];
85
86 return( diff );
87}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010088#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +010089
Hanno Becker617c1ae2017-08-23 14:11:24 +010090int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
91 const mbedtls_mpi *N,
92 const mbedtls_mpi *P, const mbedtls_mpi *Q,
93 const mbedtls_mpi *D, const mbedtls_mpi *E )
94{
95 int ret;
96
97 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
98 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
99 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
100 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
101 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
102 {
103 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
104 }
105
106 if( N != NULL )
107 ctx->len = mbedtls_mpi_size( &ctx->N );
108
109 return( 0 );
110}
111
112int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100113 unsigned char const *N, size_t N_len,
114 unsigned char const *P, size_t P_len,
115 unsigned char const *Q, size_t Q_len,
116 unsigned char const *D, size_t D_len,
117 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100118{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000119 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100120
121 if( N != NULL )
122 {
123 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
124 ctx->len = mbedtls_mpi_size( &ctx->N );
125 }
126
127 if( P != NULL )
128 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
129
130 if( Q != NULL )
131 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
132
133 if( D != NULL )
134 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
135
136 if( E != NULL )
137 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
138
139cleanup:
140
141 if( ret != 0 )
142 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
143
144 return( 0 );
145}
146
Hanno Becker705fc682017-10-10 17:57:02 +0100147/*
148 * Checks whether the context fields are set in such a way
149 * that the RSA primitives will be able to execute without error.
150 * It does *not* make guarantees for consistency of the parameters.
151 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100152static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
153 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100154{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100155#if !defined(MBEDTLS_RSA_NO_CRT)
156 /* blinding_needed is only used for NO_CRT to decide whether
157 * P,Q need to be present or not. */
158 ((void) blinding_needed);
159#endif
160
Hanno Becker3a760a12018-01-05 08:14:49 +0000161 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
162 ctx->len > MBEDTLS_MPI_MAX_SIZE )
163 {
Hanno Becker705fc682017-10-10 17:57:02 +0100164 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000165 }
Hanno Becker705fc682017-10-10 17:57:02 +0100166
167 /*
168 * 1. Modular exponentiation needs positive, odd moduli.
169 */
170
171 /* Modular exponentiation wrt. N is always used for
172 * RSA public key operations. */
173 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
174 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
175 {
176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
177 }
178
179#if !defined(MBEDTLS_RSA_NO_CRT)
180 /* Modular exponentiation for P and Q is only
181 * used for private key operations and if CRT
182 * is used. */
183 if( is_priv &&
184 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
185 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
186 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
187 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
188 {
189 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
190 }
191#endif /* !MBEDTLS_RSA_NO_CRT */
192
193 /*
194 * 2. Exponents must be positive
195 */
196
197 /* Always need E for public key operations */
198 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
199 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
200
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100201#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100202 /* For private key operations, use D or DP & DQ
203 * as (unblinded) exponents. */
204 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
206#else
207 if( is_priv &&
208 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
209 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
210 {
211 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
212 }
213#endif /* MBEDTLS_RSA_NO_CRT */
214
215 /* Blinding shouldn't make exponents negative either,
216 * so check that P, Q >= 1 if that hasn't yet been
217 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100218#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100219 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100220 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
221 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
222 {
223 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
224 }
225#endif
226
227 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100228 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100229#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100230 if( is_priv &&
231 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
232 {
233 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
234 }
235#endif
236
237 return( 0 );
238}
239
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100240int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100241{
242 int ret = 0;
243
Hanno Becker617c1ae2017-08-23 14:11:24 +0100244 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
245 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
246 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
247 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
248 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100249
Hanno Becker617c1ae2017-08-23 14:11:24 +0100250 /*
251 * Check whether provided parameters are enough
252 * to deduce all others. The following incomplete
253 * parameter sets for private keys are supported:
254 *
255 * (1) P, Q missing.
256 * (2) D and potentially N missing.
257 *
258 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100259
Hanno Becker2cca6f32017-09-29 11:46:40 +0100260 const int n_missing = have_P && have_Q && have_D && have_E;
261 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
262 const int d_missing = have_P && have_Q && !have_D && have_E;
263 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
264
265 /* These three alternatives are mutually exclusive */
266 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100267
Hanno Becker617c1ae2017-08-23 14:11:24 +0100268 if( !is_priv && !is_pub )
269 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
270
271 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100272 * Step 1: Deduce N if P, Q are provided.
273 */
274
275 if( !have_N && have_P && have_Q )
276 {
277 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
278 &ctx->Q ) ) != 0 )
279 {
280 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
281 }
282
283 ctx->len = mbedtls_mpi_size( &ctx->N );
284 }
285
286 /*
287 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100288 */
289
290 if( pq_missing )
291 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100292 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100293 &ctx->P, &ctx->Q );
294 if( ret != 0 )
295 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
296
297 }
298 else if( d_missing )
299 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100300 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
301 &ctx->Q,
302 &ctx->E,
303 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100304 {
305 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
306 }
307 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100308
Hanno Becker617c1ae2017-08-23 14:11:24 +0100309 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100310 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100311 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100312 */
313
Hanno Becker23344b52017-08-23 07:43:27 +0100314#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100315 if( is_priv )
316 {
317 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
318 &ctx->DP, &ctx->DQ, &ctx->QP );
319 if( ret != 0 )
320 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
321 }
Hanno Becker23344b52017-08-23 07:43:27 +0100322#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100323
324 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100325 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100326 */
327
Hanno Beckerebd2c022017-10-12 10:54:53 +0100328 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100329}
330
Hanno Becker617c1ae2017-08-23 14:11:24 +0100331int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
332 unsigned char *N, size_t N_len,
333 unsigned char *P, size_t P_len,
334 unsigned char *Q, size_t Q_len,
335 unsigned char *D, size_t D_len,
336 unsigned char *E, size_t E_len )
337{
338 int ret = 0;
339
340 /* Check if key is private or public */
341 const int is_priv =
342 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
343 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
344 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
345 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
346 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
347
348 if( !is_priv )
349 {
350 /* If we're trying to export private parameters for a public key,
351 * something must be wrong. */
352 if( P != NULL || Q != NULL || D != NULL )
353 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
354
355 }
356
357 if( N != NULL )
358 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
359
360 if( P != NULL )
361 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
362
363 if( Q != NULL )
364 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
365
366 if( D != NULL )
367 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
368
369 if( E != NULL )
370 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100371
372cleanup:
373
374 return( ret );
375}
376
Hanno Becker617c1ae2017-08-23 14:11:24 +0100377int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
378 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
379 mbedtls_mpi *D, mbedtls_mpi *E )
380{
381 int ret;
382
383 /* Check if key is private or public */
384 int is_priv =
385 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
386 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
387 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
388 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
389 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
390
391 if( !is_priv )
392 {
393 /* If we're trying to export private parameters for a public key,
394 * something must be wrong. */
395 if( P != NULL || Q != NULL || D != NULL )
396 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
397
398 }
399
400 /* Export all requested core parameters. */
401
402 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
403 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
404 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
405 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
406 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
407 {
408 return( ret );
409 }
410
411 return( 0 );
412}
413
414/*
415 * Export CRT parameters
416 * This must also be implemented if CRT is not used, for being able to
417 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
418 * can be used in this case.
419 */
420int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
421 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
422{
423 int ret;
424
425 /* Check if key is private or public */
426 int is_priv =
427 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
428 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
429 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
430 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
431 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
432
433 if( !is_priv )
434 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
435
Hanno Beckerdc95c892017-08-23 06:57:02 +0100436#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100437 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100438 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
439 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
440 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
441 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100442 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100443 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100444#else
445 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
446 DP, DQ, QP ) ) != 0 )
447 {
448 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
449 }
450#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100451
452 return( 0 );
453}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100454
Paul Bakker5121ce52009-01-03 21:22:43 +0000455/*
456 * Initialize an RSA context
457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000460 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000461{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#if defined(MBEDTLS_THREADING_C)
467 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200468#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000469}
470
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100471/*
472 * Set padding for an existing RSA context
473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100475{
476 ctx->padding = padding;
477 ctx->hash_id = hash_id;
478}
479
Hanno Becker617c1ae2017-08-23 14:11:24 +0100480/*
481 * Get length in bytes of RSA modulus
482 */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100483size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
484{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100485 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100486}
487
Gilles Peskine1d267092018-01-28 18:13:03 +0100488/*
489 * Get length in bits of RSA modulus
490 */
491size_t mbedtls_rsa_get_bitlen( const mbedtls_rsa_context *ctx )
492{
493 return( mbedtls_mpi_bitlen( &ctx->N ) );
494}
495
Hanno Becker617c1ae2017-08-23 14:11:24 +0100496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
499/*
500 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800501 *
502 * This generation method follows the RSA key pair generation procedure of
503 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000506 int (*f_rng)(void *, unsigned char *, size_t),
507 void *p_rng,
508 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000509{
510 int ret;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800511 mbedtls_mpi H, G, L;
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Paul Bakker21eb2802010-08-16 11:10:02 +0000513 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Janos Follathef441782016-09-21 13:18:12 +0100516 if( nbits % 2 )
517 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
518
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100519 mbedtls_mpi_init( &H );
520 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800521 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523 /*
524 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800525 * 1. |P-Q| > 2^( nbits / 2 - 100 )
526 * 2. GCD( E, (P-1)*(Q-1) ) == 1
527 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
531 do
532 {
Janos Follath10c575b2016-02-23 14:42:48 +0000533 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100534 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Janos Follathef441782016-09-21 13:18:12 +0100536 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100537 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800539 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
540 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
541 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 continue;
543
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800544 /* not required by any standards, but some users rely on the fact that P > Q */
545 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100546 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100547
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100548 /* Temporarily replace P,Q by P-1, Q-1 */
549 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
550 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
551 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800552
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800553 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800555 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
556 continue;
557
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800558 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Jethro Beekman97f95c92018-02-13 15:50:36 -0800559 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
560 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
561 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
562
563 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
564 continue;
565
566 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800568 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100570 /* Restore P,Q */
571 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
572 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
573
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800574 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
575
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100576 ctx->len = mbedtls_mpi_size( &ctx->N );
577
Jethro Beekman97f95c92018-02-13 15:50:36 -0800578#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 * DP = D mod (P - 1)
581 * DQ = D mod (Q - 1)
582 * QP = Q^-1 mod P
583 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100584 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
585 &ctx->DP, &ctx->DQ, &ctx->QP ) );
586#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Hanno Becker83aad1f2017-08-23 06:45:10 +0100588 /* Double-check */
589 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591cleanup:
592
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100593 mbedtls_mpi_free( &H );
594 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800595 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
597 if( ret != 0 )
598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_rsa_free( ctx );
600 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601 }
602
Paul Bakker48377d92013-08-30 12:06:24 +0200603 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000604}
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
608/*
609 * Check a public RSA key
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000612{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100613 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000615
Hanno Becker3a760a12018-01-05 08:14:49 +0000616 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100619 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Hanno Becker705fc682017-10-10 17:57:02 +0100621 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
622 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100626 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
628 return( 0 );
629}
630
631/*
Hanno Becker705fc682017-10-10 17:57:02 +0100632 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000635{
Hanno Becker705fc682017-10-10 17:57:02 +0100636 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100637 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 {
Hanno Becker98838b02017-10-02 13:16:10 +0100639 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 }
Paul Bakker48377d92013-08-30 12:06:24 +0200641
Hanno Becker98838b02017-10-02 13:16:10 +0100642 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100643 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100645 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000647
Hanno Beckerb269a852017-08-25 08:03:21 +0100648#if !defined(MBEDTLS_RSA_NO_CRT)
649 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
650 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
651 {
652 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
653 }
654#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000655
656 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000657}
658
659/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100660 * Check if contexts holding a public and private key match
661 */
Hanno Becker98838b02017-10-02 13:16:10 +0100662int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
663 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100664{
Hanno Becker98838b02017-10-02 13:16:10 +0100665 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100669 }
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
672 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100675 }
676
677 return( 0 );
678}
679
680/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 * Do an RSA public key operation
682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000684 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 unsigned char *output )
686{
Paul Bakker23986e52011-04-24 08:57:21 +0000687 int ret;
688 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000690
Hanno Beckerebd2c022017-10-12 10:54:53 +0100691 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100692 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000695
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200696#if defined(MBEDTLS_THREADING_C)
697 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
698 return( ret );
699#endif
700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200705 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
706 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 }
708
709 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
711 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000712
713cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200715 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
716 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100717#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
721 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
724 return( 0 );
725}
726
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200727/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200728 * Generate or update blinding values, see section 10 of:
729 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200730 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200731 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200732 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200733static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200734 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
735{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200736 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200737
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200738 if( ctx->Vf.p != NULL )
739 {
740 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
742 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
743 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
744 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200745
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200746 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200747 }
748
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200749 /* Unblinding value: Vf = random number, invertible mod N */
750 do {
751 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
755 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
756 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200757
758 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
760 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200761
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200762
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200763cleanup:
764 return( ret );
765}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200766
Paul Bakker5121ce52009-01-03 21:22:43 +0000767/*
Janos Follathe81102e2017-03-22 13:38:28 +0000768 * Exponent blinding supposed to prevent side-channel attacks using multiple
769 * traces of measurements to recover the RSA key. The more collisions are there,
770 * the more bits of the key can be recovered. See [3].
771 *
772 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
773 * observations on avarage.
774 *
775 * For example with 28 byte blinding to achieve 2 collisions the adversary has
776 * to make 2^112 observations on avarage.
777 *
778 * (With the currently (as of 2017 April) known best algorithms breaking 2048
779 * bit RSA requires approximately as much time as trying out 2^112 random keys.
780 * Thus in this sense with 28 byte blinding the security is not reduced by
781 * side-channel attacks like the one in [3])
782 *
783 * This countermeasure does not help if the key recovery is possible with a
784 * single trace.
785 */
786#define RSA_EXPONENT_BLINDING 28
787
788/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000789 * Do an RSA private key operation
790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200792 int (*f_rng)(void *, unsigned char *, size_t),
793 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000794 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000795 unsigned char *output )
796{
Paul Bakker23986e52011-04-24 08:57:21 +0000797 int ret;
798 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100799
800 /* Temporary holding the result */
801 mbedtls_mpi T;
802
803 /* Temporaries holding P-1, Q-1 and the
804 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000805 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100806
807#if !defined(MBEDTLS_RSA_NO_CRT)
808 /* Temporaries holding the results mod p resp. mod q. */
809 mbedtls_mpi TP, TQ;
810
811 /* Temporaries holding the blinded exponents for
812 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000813 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100814
815 /* Pointers to actual exponents to be used - either the unblinded
816 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000817 mbedtls_mpi *DP = &ctx->DP;
818 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100819#else
820 /* Temporary holding the blinded exponent (if used). */
821 mbedtls_mpi D_blind;
822
823 /* Pointer to actual exponent to be used - either the unblinded
824 * or the blinded one, depending on the presence of a PRNG. */
825 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100826#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100827
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100828 /* Temporaries holding the initial input and the double
829 * checked result; should be the same in the end. */
830 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Hanno Beckerebd2c022017-10-12 10:54:53 +0100832 if( rsa_check_context( ctx, 1 /* private key checks */,
833 f_rng != NULL /* blinding y/n */ ) != 0 )
834 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100835 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100836 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100837
Hanno Becker06811ce2017-05-03 15:10:34 +0100838#if defined(MBEDTLS_THREADING_C)
839 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
840 return( ret );
841#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000842
Hanno Becker06811ce2017-05-03 15:10:34 +0100843 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100844 mbedtls_mpi_init( &T );
845
846 mbedtls_mpi_init( &P1 );
847 mbedtls_mpi_init( &Q1 );
848 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000849
Janos Follathf9203b42017-03-22 15:13:15 +0000850 if( f_rng != NULL )
851 {
Janos Follathe81102e2017-03-22 13:38:28 +0000852#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000853 mbedtls_mpi_init( &D_blind );
854#else
855 mbedtls_mpi_init( &DP_blind );
856 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000857#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000858 }
Janos Follathe81102e2017-03-22 13:38:28 +0000859
Hanno Becker06811ce2017-05-03 15:10:34 +0100860#if !defined(MBEDTLS_RSA_NO_CRT)
861 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200862#endif
863
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100864 mbedtls_mpi_init( &I );
865 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100866
867 /* End of MPI initialization */
868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
870 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200872 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
873 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000874 }
875
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100876 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100877
Paul Bakkerf451bac2013-08-30 15:37:02 +0200878 if( f_rng != NULL )
879 {
880 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200881 * Blinding
882 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200883 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200884 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
885 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000887
Janos Follathe81102e2017-03-22 13:38:28 +0000888 /*
889 * Exponent blinding
890 */
891 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
892 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
893
Janos Follathf9203b42017-03-22 15:13:15 +0000894#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000895 /*
896 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
897 */
898 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
899 f_rng, p_rng ) );
900 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
901 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
902 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
903
904 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000905#else
906 /*
907 * DP_blind = ( P - 1 ) * R + DP
908 */
909 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
910 f_rng, p_rng ) );
911 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
912 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
913 &ctx->DP ) );
914
915 DP = &DP_blind;
916
917 /*
918 * DQ_blind = ( Q - 1 ) * R + DQ
919 */
920 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
921 f_rng, p_rng ) );
922 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
923 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
924 &ctx->DQ ) );
925
926 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000927#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200928 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000931 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100932#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200933 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000934 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100936 * TP = input ^ dP mod P
937 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100939
940 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
941 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000942
943 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100944 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000945 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100946 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
947 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
948 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000949
950 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100951 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100953 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
954 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200956
Paul Bakkerf451bac2013-08-30 15:37:02 +0200957 if( f_rng != NULL )
958 {
959 /*
960 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200961 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200962 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200963 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200965 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000966
Hanno Becker2dec5e82017-10-03 07:49:52 +0100967 /* Verify the result to prevent glitching attacks. */
968 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
969 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100970 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +0100971 {
Hanno Becker06811ce2017-05-03 15:10:34 +0100972 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
973 goto cleanup;
974 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100975
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
979cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200981 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
982 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200983#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200984
Hanno Becker06811ce2017-05-03 15:10:34 +0100985 mbedtls_mpi_free( &P1 );
986 mbedtls_mpi_free( &Q1 );
987 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000988
989 if( f_rng != NULL )
990 {
Janos Follathe81102e2017-03-22 13:38:28 +0000991#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000992 mbedtls_mpi_free( &D_blind );
993#else
994 mbedtls_mpi_free( &DP_blind );
995 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000996#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000997 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000998
Hanno Becker06811ce2017-05-03 15:10:34 +0100999 mbedtls_mpi_free( &T );
1000
1001#if !defined(MBEDTLS_RSA_NO_CRT)
1002 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1003#endif
1004
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001005 mbedtls_mpi_free( &C );
1006 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001007
Paul Bakker5121ce52009-01-03 21:22:43 +00001008 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
1011 return( 0 );
1012}
1013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001015/**
1016 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1017 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001018 * \param dst buffer to mask
1019 * \param dlen length of destination buffer
1020 * \param src source of the mask generation
1021 * \param slen length of the source buffer
1022 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001023 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001024static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001026{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001028 unsigned char counter[4];
1029 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001030 unsigned int hlen;
1031 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001032 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001035 memset( counter, 0, 4 );
1036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001038
Simon Butcher02037452016-03-01 21:19:12 +00001039 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001040 p = dst;
1041
1042 while( dlen > 0 )
1043 {
1044 use_len = hlen;
1045 if( dlen < hlen )
1046 use_len = dlen;
1047
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001048 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1049 goto exit;
1050 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1051 goto exit;
1052 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1053 goto exit;
1054 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1055 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001056
1057 for( i = 0; i < use_len; ++i )
1058 *p++ ^= mask[i];
1059
1060 counter[3]++;
1061
1062 dlen -= use_len;
1063 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001064
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001065exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001066 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001067
1068 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001073/*
1074 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001077 int (*f_rng)(void *, unsigned char *, size_t),
1078 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001079 int mode,
1080 const unsigned char *label, size_t label_len,
1081 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001082 const unsigned char *input,
1083 unsigned char *output )
1084{
1085 size_t olen;
1086 int ret;
1087 unsigned char *p = output;
1088 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 const mbedtls_md_info_t *md_info;
1090 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1093 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001094
1095 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001099 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001101
1102 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001104
Simon Butcher02037452016-03-01 21:19:12 +00001105 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001106 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001108
1109 memset( output, 0, olen );
1110
1111 *p++ = 0;
1112
Simon Butcher02037452016-03-01 21:19:12 +00001113 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001114 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001116
1117 p += hlen;
1118
Simon Butcher02037452016-03-01 21:19:12 +00001119 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001120 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1121 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001122 p += hlen;
1123 p += olen - 2 * hlen - 2 - ilen;
1124 *p++ = 1;
Gilles Peskine69e033a2018-07-06 15:47:54 +02001125 if( ilen != 0 )
1126 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001129 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001130 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001131
Simon Butcher02037452016-03-01 21:19:12 +00001132 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001133 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1134 &md_ctx ) ) != 0 )
1135 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001136
Simon Butcher02037452016-03-01 21:19:12 +00001137 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001138 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1139 &md_ctx ) ) != 0 )
1140 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001141
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001142exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001144
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001145 if( ret != 0 )
1146 return( ret );
1147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 return( ( mode == MBEDTLS_RSA_PUBLIC )
1149 ? mbedtls_rsa_public( ctx, output, output )
1150 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001151}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001155/*
1156 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001159 int (*f_rng)(void *, unsigned char *, size_t),
1160 void *p_rng,
1161 int mode, size_t ilen,
1162 const unsigned char *input,
1163 unsigned char *output )
1164{
1165 size_t nb_pad, olen;
1166 int ret;
1167 unsigned char *p = output;
1168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1170 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001171
Janos Follath1ed9f992016-03-18 11:45:44 +00001172 // We don't check p_rng because it won't be dereferenced here
Gilles Peskine69e033a2018-07-06 15:47:54 +02001173 if( f_rng == NULL || output == NULL )
1174 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1175 if( ilen != 0 && input == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001177
1178 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001179
Simon Butcher02037452016-03-01 21:19:12 +00001180 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001181 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001183
1184 nb_pad = olen - 3 - ilen;
1185
1186 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001190
1191 while( nb_pad-- > 0 )
1192 {
1193 int rng_dl = 100;
1194
1195 do {
1196 ret = f_rng( p_rng, p, 1 );
1197 } while( *p == 0 && --rng_dl && ret == 0 );
1198
Simon Butcher02037452016-03-01 21:19:12 +00001199 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001200 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001202
1203 p++;
1204 }
1205 }
1206 else
1207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001209
1210 while( nb_pad-- > 0 )
1211 *p++ = 0xFF;
1212 }
1213
1214 *p++ = 0;
Gilles Peskine69e033a2018-07-06 15:47:54 +02001215 if( ilen != 0 )
1216 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 return( ( mode == MBEDTLS_RSA_PUBLIC )
1219 ? mbedtls_rsa_public( ctx, output, output )
1220 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001223
Paul Bakker5121ce52009-01-03 21:22:43 +00001224/*
1225 * Add the message padding, then do an RSA operation
1226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001228 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001229 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001230 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001231 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 unsigned char *output )
1233{
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 switch( ctx->padding )
1235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#if defined(MBEDTLS_PKCS1_V15)
1237 case MBEDTLS_RSA_PKCS_V15:
1238 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001239 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001240#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_PKCS1_V21)
1243 case MBEDTLS_RSA_PKCS_V21:
1244 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001245 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001246#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
1248 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001251}
1252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001254/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001255 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001258 int (*f_rng)(void *, unsigned char *, size_t),
1259 void *p_rng,
1260 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001261 const unsigned char *label, size_t label_len,
1262 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001263 const unsigned char *input,
1264 unsigned char *output,
1265 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001266{
Paul Bakker23986e52011-04-24 08:57:21 +00001267 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001268 size_t ilen, i, pad_len;
1269 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1271 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001272 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 const mbedtls_md_info_t *md_info;
1274 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001275
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001276 /*
1277 * Parameters sanity checks
1278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1280 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281
1282 ilen = ctx->len;
1283
Paul Bakker27fdf462011-06-09 13:55:13 +00001284 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001288 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001290
Janos Follathc17cda12016-02-11 11:08:18 +00001291 hlen = mbedtls_md_get_size( md_info );
1292
1293 // checking for integer underflow
1294 if( 2 * hlen + 2 > ilen )
1295 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1296
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001297 /*
1298 * RSA operation
1299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1301 ? mbedtls_rsa_public( ctx, input, buf )
1302 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
1304 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001305 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001307 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001308 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001311 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1312 {
1313 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001314 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001315 }
1316
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001317 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001318 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1319 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001320 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001321 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1322 &md_ctx ) ) != 0 )
1323 {
1324 mbedtls_md_free( &md_ctx );
1325 goto cleanup;
1326 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001329
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001330 /* Generate lHash */
1331 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1332 goto cleanup;
1333
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001334 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001335 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001336 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001338 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001340 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001341
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001342 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001343
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001344 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001345 for( i = 0; i < hlen; i++ )
1346 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001347
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001348 /* Get zero-padding len, but always read till end of buffer
1349 * (minus one, for the 01 byte) */
1350 pad_len = 0;
1351 pad_done = 0;
1352 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1353 {
1354 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001355 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001356 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001357
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001358 p += pad_len;
1359 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001360
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001361 /*
1362 * The only information "leaked" is whether the padding was correct or not
1363 * (eg, no data is copied if it was not correct). This meets the
1364 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1365 * the different error conditions.
1366 */
1367 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001368 {
1369 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1370 goto cleanup;
1371 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001372
Paul Bakker66d5d072014-06-17 16:39:18 +02001373 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001374 {
1375 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1376 goto cleanup;
1377 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001378
1379 *olen = ilen - (p - buf);
Gilles Peskine69e033a2018-07-06 15:47:54 +02001380 if( *olen != 0 )
1381 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001382 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001383
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001384cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001385 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1386 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001387
1388 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001393/*
1394 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001397 int (*f_rng)(void *, unsigned char *, size_t),
1398 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001399 int mode, size_t *olen,
1400 const unsigned char *input,
1401 unsigned char *output,
1402 size_t output_max_len)
1403{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001404 int ret;
1405 size_t ilen, pad_count = 0, i;
1406 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1410 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001411
1412 ilen = ctx->len;
1413
1414 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1418 ? mbedtls_rsa_public( ctx, input, buf )
1419 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001420
1421 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001422 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001423
1424 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001425 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001426
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001427 /*
1428 * Check and get padding len in "constant-time"
1429 */
1430 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001431
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001432 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001436
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001437 /* Get padding len, but always read till end of buffer
1438 * (minus one, for the 00 byte) */
1439 for( i = 0; i < ilen - 3; i++ )
1440 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001441 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1442 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001443 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001444
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001445 p += pad_count;
1446 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001447 }
1448 else
1449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001451
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001452 /* Get padding len, but always read till end of buffer
1453 * (minus one, for the 00 byte) */
1454 for( i = 0; i < ilen - 3; i++ )
1455 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001456 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001457 pad_count += ( pad_done == 0 );
1458 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001459
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001460 p += pad_count;
1461 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 }
1463
Janos Follathc69fa502016-02-12 13:30:09 +00001464 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001465
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001466 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001467 {
1468 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1469 goto cleanup;
1470 }
Paul Bakker8804f692013-02-28 18:06:26 +01001471
Paul Bakker66d5d072014-06-17 16:39:18 +02001472 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001473 {
1474 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1475 goto cleanup;
1476 }
Paul Bakker060c5682009-01-12 21:48:39 +00001477
Paul Bakker27fdf462011-06-09 13:55:13 +00001478 *olen = ilen - (p - buf);
Gilles Peskine69e033a2018-07-06 15:47:54 +02001479 if( *olen != 0 )
1480 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001481 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001482
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001483cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001484 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001485
1486 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001489
1490/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001491 * Do an RSA operation, then remove the message padding
1492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001494 int (*f_rng)(void *, unsigned char *, size_t),
1495 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001496 int mode, size_t *olen,
1497 const unsigned char *input,
1498 unsigned char *output,
1499 size_t output_max_len)
1500{
1501 switch( ctx->padding )
1502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#if defined(MBEDTLS_PKCS1_V15)
1504 case MBEDTLS_RSA_PKCS_V15:
1505 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001506 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001507#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#if defined(MBEDTLS_PKCS1_V21)
1510 case MBEDTLS_RSA_PKCS_V21:
1511 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001512 olen, input, output,
1513 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001514#endif
1515
1516 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001518 }
1519}
1520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001522/*
1523 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001526 int (*f_rng)(void *, unsigned char *, size_t),
1527 void *p_rng,
1528 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001530 unsigned int hashlen,
1531 const unsigned char *hash,
1532 unsigned char *sig )
1533{
1534 size_t olen;
1535 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001537 unsigned int slen, hlen, offset = 0;
1538 int ret;
1539 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 const mbedtls_md_info_t *md_info;
1541 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1544 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001545
1546 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001548
1549 olen = ctx->len;
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001552 {
Simon Butcher02037452016-03-01 21:19:12 +00001553 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001555 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001559 }
1560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001562 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001566 slen = hlen;
1567
1568 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001570
1571 memset( sig, 0, olen );
1572
Simon Butcher02037452016-03-01 21:19:12 +00001573 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001574 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001576
Simon Butcher02037452016-03-01 21:19:12 +00001577 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001578 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001579 p += olen - hlen * 2 - 2;
1580 *p++ = 0x01;
1581 memcpy( p, salt, slen );
1582 p += slen;
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001585 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001586 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001587
Simon Butcher02037452016-03-01 21:19:12 +00001588 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001589 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1590 goto exit;
1591 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1592 goto exit;
1593 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1594 goto exit;
1595 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1596 goto exit;
1597 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1598 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001599
Simon Butcher02037452016-03-01 21:19:12 +00001600 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001601 if( msb % 8 == 0 )
1602 offset = 1;
1603
Simon Butcher02037452016-03-01 21:19:12 +00001604 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001605 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1606 &md_ctx ) ) != 0 )
1607 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001608
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001609 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001610 sig[0] &= 0xFF >> ( olen * 8 - msb );
1611
1612 p += hlen;
1613 *p++ = 0xBC;
1614
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001615 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001616
1617exit:
1618 mbedtls_md_free( &md_ctx );
1619
1620 if( ret != 0 )
1621 return( ret );
1622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 return( ( mode == MBEDTLS_RSA_PUBLIC )
1624 ? mbedtls_rsa_public( ctx, sig, sig )
1625 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001626}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001630/*
1631 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1632 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001633
1634/* Construct a PKCS v1.5 encoding of a hashed message
1635 *
1636 * This is used both for signature generation and verification.
1637 *
1638 * Parameters:
1639 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001640 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001641 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001642 * - hash: Buffer containing the hashed message or the raw data.
1643 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001644 * - dst: Buffer to hold the encoded message.
1645 *
1646 * Assumptions:
1647 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1648 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001649 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001650 *
1651 */
1652static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1653 unsigned int hashlen,
1654 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001655 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001656 unsigned char *dst )
1657{
1658 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001659 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001660 unsigned char *p = dst;
1661 const char *oid = NULL;
1662
1663 /* Are we signing hashed or raw data? */
1664 if( md_alg != MBEDTLS_MD_NONE )
1665 {
1666 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1667 if( md_info == NULL )
1668 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1669
1670 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1671 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1672
1673 hashlen = mbedtls_md_get_size( md_info );
1674
1675 /* Double-check that 8 + hashlen + oid_size can be used as a
1676 * 1-byte ASN.1 length encoding and that there's no overflow. */
1677 if( 8 + hashlen + oid_size >= 0x80 ||
1678 10 + hashlen < hashlen ||
1679 10 + hashlen + oid_size < 10 + hashlen )
1680 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1681
1682 /*
1683 * Static bounds check:
1684 * - Need 10 bytes for five tag-length pairs.
1685 * (Insist on 1-byte length encodings to protect against variants of
1686 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1687 * - Need hashlen bytes for hash
1688 * - Need oid_size bytes for hash alg OID.
1689 */
1690 if( nb_pad < 10 + hashlen + oid_size )
1691 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1692 nb_pad -= 10 + hashlen + oid_size;
1693 }
1694 else
1695 {
1696 if( nb_pad < hashlen )
1697 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1698
1699 nb_pad -= hashlen;
1700 }
1701
Hanno Becker2b2f8982017-09-27 17:10:03 +01001702 /* Need space for signature header and padding delimiter (3 bytes),
1703 * and 8 bytes for the minimal padding */
1704 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001705 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1706 nb_pad -= 3;
1707
1708 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001709 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001710
1711 /* Write signature header and padding */
1712 *p++ = 0;
1713 *p++ = MBEDTLS_RSA_SIGN;
1714 memset( p, 0xFF, nb_pad );
1715 p += nb_pad;
1716 *p++ = 0;
1717
1718 /* Are we signing raw data? */
1719 if( md_alg == MBEDTLS_MD_NONE )
1720 {
1721 memcpy( p, hash, hashlen );
1722 return( 0 );
1723 }
1724
1725 /* Signing hashed data, add corresponding ASN.1 structure
1726 *
1727 * DigestInfo ::= SEQUENCE {
1728 * digestAlgorithm DigestAlgorithmIdentifier,
1729 * digest Digest }
1730 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1731 * Digest ::= OCTET STRING
1732 *
1733 * Schematic:
1734 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1735 * TAG-NULL + LEN [ NULL ] ]
1736 * TAG-OCTET + LEN [ HASH ] ]
1737 */
1738 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001739 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001740 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001741 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001742 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001743 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001744 memcpy( p, oid, oid_size );
1745 p += oid_size;
1746 *p++ = MBEDTLS_ASN1_NULL;
1747 *p++ = 0x00;
1748 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001749 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001750 memcpy( p, hash, hashlen );
1751 p += hashlen;
1752
1753 /* Just a sanity-check, should be automatic
1754 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001755 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001756 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001757 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001758 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1759 }
1760
1761 return( 0 );
1762}
1763
Paul Bakkerb3869132013-02-28 17:21:01 +01001764/*
1765 * Do an RSA operation to sign the message digest
1766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001768 int (*f_rng)(void *, unsigned char *, size_t),
1769 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001770 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001772 unsigned int hashlen,
1773 const unsigned char *hash,
1774 unsigned char *sig )
1775{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001776 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001777 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1780 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001781
Hanno Beckerfdf38032017-09-06 12:35:55 +01001782 /*
1783 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1784 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001785
Hanno Beckerfdf38032017-09-06 12:35:55 +01001786 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1787 ctx->len, sig ) ) != 0 )
1788 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001789
1790 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001791 * Call respective RSA primitive
1792 */
1793
1794 if( mode == MBEDTLS_RSA_PUBLIC )
1795 {
1796 /* Skip verification on a public key operation */
1797 return( mbedtls_rsa_public( ctx, sig, sig ) );
1798 }
1799
1800 /* Private key operation
1801 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001802 * In order to prevent Lenstra's attack, make the signature in a
1803 * temporary buffer and check it before returning it.
1804 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001805
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001806 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001807 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001808 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1809
Hanno Beckerfdf38032017-09-06 12:35:55 +01001810 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001811 if( verif == NULL )
1812 {
1813 mbedtls_free( sig_try );
1814 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1815 }
1816
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001817 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1818 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1819
Hanno Becker171a8f12017-09-06 12:32:16 +01001820 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001821 {
1822 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1823 goto cleanup;
1824 }
1825
1826 memcpy( sig, sig_try, ctx->len );
1827
1828cleanup:
1829 mbedtls_free( sig_try );
1830 mbedtls_free( verif );
1831
1832 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001833}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001835
1836/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 * Do an RSA operation to sign the message digest
1838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001840 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001841 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001844 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001845 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001846 unsigned char *sig )
1847{
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 switch( ctx->padding )
1849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#if defined(MBEDTLS_PKCS1_V15)
1851 case MBEDTLS_RSA_PKCS_V15:
1852 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001853 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001854#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856#if defined(MBEDTLS_PKCS1_V21)
1857 case MBEDTLS_RSA_PKCS_V21:
1858 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001859 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001860#endif
1861
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001865}
1866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001868/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001869 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001872 int (*f_rng)(void *, unsigned char *, size_t),
1873 void *p_rng,
1874 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001876 unsigned int hashlen,
1877 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001879 int expected_salt_len,
1880 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001881{
Paul Bakker23986e52011-04-24 08:57:21 +00001882 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001883 size_t siglen;
1884 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001885 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001887 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001888 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001889 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 const mbedtls_md_info_t *md_info;
1891 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001892 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1895 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001896
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 siglen = ctx->len;
1898
Paul Bakker27fdf462011-06-09 13:55:13 +00001899 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1903 ? mbedtls_rsa_public( ctx, sig, buf )
1904 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001905
1906 if( ret != 0 )
1907 return( ret );
1908
1909 p = buf;
1910
Paul Bakkerb3869132013-02-28 17:21:01 +01001911 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 {
Simon Butcher02037452016-03-01 21:19:12 +00001916 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001918 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001922 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001925 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001929
Paul Bakkerb3869132013-02-28 17:21:01 +01001930 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001931
Simon Butcher02037452016-03-01 21:19:12 +00001932 /*
1933 * Note: EMSA-PSS verification is over the length of N - 1 bits
1934 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001935 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001936
Gilles Peskineb00b0da2017-10-19 15:23:49 +02001937 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1938 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1939
Simon Butcher02037452016-03-01 21:19:12 +00001940 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001941 if( msb % 8 == 0 )
1942 {
1943 p++;
1944 siglen -= 1;
1945 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001946
Gilles Peskine139108a2017-10-18 19:03:42 +02001947 if( siglen < hlen + 2 )
1948 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1949 hash_start = p + siglen - hlen - 1;
1950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001952 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001953 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001954
Jaeden Amero66954e12018-01-25 16:05:54 +00001955 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
1956 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001957 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01001958
Paul Bakkerb3869132013-02-28 17:21:01 +01001959 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001960
Gilles Peskine6a54b022017-10-17 19:02:13 +02001961 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001962 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001963
Gilles Peskine91048a32017-10-19 17:46:14 +02001964 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001965 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001966 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1967 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001968 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001969
Gilles Peskine6a54b022017-10-17 19:02:13 +02001970 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02001973 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001974 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001975 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1976 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001977 }
1978
Simon Butcher02037452016-03-01 21:19:12 +00001979 /*
1980 * Generate H = Hash( M' )
1981 */
Jaeden Amero66954e12018-01-25 16:05:54 +00001982 ret = mbedtls_md_starts( &md_ctx );
1983 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001984 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00001985 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
1986 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001987 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00001988 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
1989 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001990 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00001991 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
1992 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001993 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00001994 ret = mbedtls_md_finish( &md_ctx, result );
1995 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001996 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00001997
Jaeden Amero66954e12018-01-25 16:05:54 +00001998 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01001999 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002000 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002001 goto exit;
2002 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002003
2004exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002006
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002007 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002008}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002009
2010/*
2011 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002014 int (*f_rng)(void *, unsigned char *, size_t),
2015 void *p_rng,
2016 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002018 unsigned int hashlen,
2019 const unsigned char *hash,
2020 const unsigned char *sig )
2021{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2023 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002024 : md_alg;
2025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002027 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002029 sig ) );
2030
2031}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002035/*
2036 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002039 int (*f_rng)(void *, unsigned char *, size_t),
2040 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002041 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002043 unsigned int hashlen,
2044 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002045 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002046{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002047 int ret = 0;
2048 const size_t sig_len = ctx->len;
2049 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2052 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002053
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002054 /*
2055 * Prepare expected PKCS1 v1.5 encoding of hash.
2056 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002057
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002058 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2059 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2060 {
2061 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2062 goto cleanup;
2063 }
2064
2065 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2066 encoded_expected ) ) != 0 )
2067 goto cleanup;
2068
2069 /*
2070 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2071 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002074 ? mbedtls_rsa_public( ctx, sig, encoded )
2075 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002076 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002077 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002078
Simon Butcher02037452016-03-01 21:19:12 +00002079 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002080 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002081 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002082
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002083 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2084 sig_len ) ) != 0 )
2085 {
2086 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2087 goto cleanup;
2088 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002089
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002090cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002091
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002092 if( encoded != NULL )
2093 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002094 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002095 mbedtls_free( encoded );
2096 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002097
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002098 if( encoded_expected != NULL )
2099 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002100 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002101 mbedtls_free( encoded_expected );
2102 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002103
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002104 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002107
2108/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002109 * Do an RSA operation and check the message digest
2110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002112 int (*f_rng)(void *, unsigned char *, size_t),
2113 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002114 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002116 unsigned int hashlen,
2117 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002118 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002119{
2120 switch( ctx->padding )
2121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#if defined(MBEDTLS_PKCS1_V15)
2123 case MBEDTLS_RSA_PKCS_V15:
2124 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002125 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002126#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128#if defined(MBEDTLS_PKCS1_V21)
2129 case MBEDTLS_RSA_PKCS_V21:
2130 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002131 hashlen, hash, sig );
2132#endif
2133
2134 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002136 }
2137}
2138
2139/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002140 * Copy the components of an RSA key
2141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002143{
2144 int ret;
2145
2146 dst->ver = src->ver;
2147 dst->len = src->len;
2148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2150 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2153 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2154 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002155
2156#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2158 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2159 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2161 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002162#endif
2163
2164 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2167 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002168
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002169 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002170 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002171
2172cleanup:
2173 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002175
2176 return( ret );
2177}
2178
2179/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 * Free the components of an RSA key
2181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002183{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002185 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2186 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002188
Hanno Becker33c30a02017-08-23 07:00:22 +01002189#if !defined(MBEDTLS_RSA_NO_CRT)
2190 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2191 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2192 mbedtls_mpi_free( &ctx->DP );
2193#endif /* MBEDTLS_RSA_NO_CRT */
2194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195#if defined(MBEDTLS_THREADING_C)
2196 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002197#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002198}
2199
Hanno Beckerab377312017-08-23 16:24:51 +01002200#endif /* !MBEDTLS_RSA_ALT */
2201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002203
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002204#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002205
2206/*
2207 * Example RSA-1024 keypair, for test purposes
2208 */
2209#define KEY_LEN 128
2210
2211#define RSA_N "9292758453063D803DD603D5E777D788" \
2212 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2213 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2214 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2215 "93A89813FBF3C4F8066D2D800F7C38A8" \
2216 "1AE31942917403FF4946B0A83D3D3E05" \
2217 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2218 "5E94BB77B07507233A0BC7BAC8F90F79"
2219
2220#define RSA_E "10001"
2221
2222#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2223 "66CA472BC44D253102F8B4A9D3BFA750" \
2224 "91386C0077937FE33FA3252D28855837" \
2225 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2226 "DF79C5CE07EE72C7F123142198164234" \
2227 "CABB724CF78B8173B9F880FC86322407" \
2228 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2229 "071513A1E85B5DFA031F21ECAE91A34D"
2230
2231#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2232 "2C01CAD19EA484A87EA4377637E75500" \
2233 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2234 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2235
2236#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2237 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2238 "910E4168387E3C30AA1E00C339A79508" \
2239 "8452DD96A9A5EA5D9DCA68DA636032AF"
2240
Paul Bakker5121ce52009-01-03 21:22:43 +00002241#define PT_LEN 24
2242#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2243 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002246static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002247{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002248#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002249 size_t i;
2250
Paul Bakker545570e2010-07-18 09:00:25 +00002251 if( rng_state != NULL )
2252 rng_state = NULL;
2253
Paul Bakkera3d195c2011-11-27 21:07:34 +00002254 for( i = 0; i < len; ++i )
2255 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002256#else
2257 if( rng_state != NULL )
2258 rng_state = NULL;
2259
2260 arc4random_buf( output, len );
2261#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002262
Paul Bakkera3d195c2011-11-27 21:07:34 +00002263 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002264}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002266
Paul Bakker5121ce52009-01-03 21:22:43 +00002267/*
2268 * Checkup routine
2269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002271{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002272 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002274 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 unsigned char rsa_plaintext[PT_LEN];
2277 unsigned char rsa_decrypted[PT_LEN];
2278 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002280 unsigned char sha1sum[20];
2281#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
Hanno Becker3a701162017-08-22 13:52:43 +01002283 mbedtls_mpi K;
2284
2285 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002287
Hanno Becker3a701162017-08-22 13:52:43 +01002288 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2289 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2290 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2291 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2292 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2293 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2294 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2295 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2296 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2297 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2298
Hanno Becker7f25f852017-10-10 16:56:22 +01002299 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
2301 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2305 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 {
2307 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002309
Hanno Becker5bc87292017-05-03 15:09:31 +01002310 ret = 1;
2311 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002312 }
2313
2314 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
2317 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2318
Hanno Becker98838b02017-10-02 13:16:10 +01002319 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2320 PT_LEN, rsa_plaintext,
2321 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 {
2323 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
Hanno Becker5bc87292017-05-03 15:09:31 +01002326 ret = 1;
2327 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 }
2329
2330 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002332
Hanno Becker98838b02017-10-02 13:16:10 +01002333 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2334 &len, rsa_ciphertext, rsa_decrypted,
2335 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002336 {
2337 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
Hanno Becker5bc87292017-05-03 15:09:31 +01002340 ret = 1;
2341 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 }
2343
2344 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2345 {
2346 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002348
Hanno Becker5bc87292017-05-03 15:09:31 +01002349 ret = 1;
2350 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002351 }
2352
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002353 if( verbose != 0 )
2354 mbedtls_printf( "passed\n" );
2355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002358 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002360 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002361 {
2362 if( verbose != 0 )
2363 mbedtls_printf( "failed\n" );
2364
2365 return( 1 );
2366 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
Hanno Becker98838b02017-10-02 13:16:10 +01002368 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2369 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2370 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002371 {
2372 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002374
Hanno Becker5bc87292017-05-03 15:09:31 +01002375 ret = 1;
2376 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 }
2378
2379 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
Hanno Becker98838b02017-10-02 13:16:10 +01002382 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2383 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2384 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 {
2386 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Hanno Becker5bc87292017-05-03 15:09:31 +01002389 ret = 1;
2390 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 }
2392
2393 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002394 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002397 if( verbose != 0 )
2398 mbedtls_printf( "\n" );
2399
Paul Bakker3d8fb632014-04-17 12:42:41 +02002400cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002401 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 mbedtls_rsa_free( &rsa );
2403#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002404 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002406 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002407}
2408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411#endif /* MBEDTLS_RSA_C */