blob: ad1ef6db24ce1451228026f9bde648421bb255ed [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"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000059#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000060#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010064#else
Rich Evans00ab4702015-02-06 13:43:58 +000065#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020067#define mbedtls_calloc calloc
68#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010069#endif
70
Hanno Beckera565f542017-10-11 11:00:19 +010071#if !defined(MBEDTLS_RSA_ALT)
72
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010073/* Implementation that should never be optimized out by the compiler */
74static void mbedtls_zeroize( void *v, size_t n ) {
75 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
76}
77
Hanno Becker617c1ae2017-08-23 14:11:24 +010078int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
79 const mbedtls_mpi *N,
80 const mbedtls_mpi *P, const mbedtls_mpi *Q,
81 const mbedtls_mpi *D, const mbedtls_mpi *E )
82{
83 int ret;
84
85 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
86 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
87 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
88 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
89 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
90 {
91 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
92 }
93
94 if( N != NULL )
95 ctx->len = mbedtls_mpi_size( &ctx->N );
96
97 return( 0 );
98}
99
100int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100101 unsigned char const *N, size_t N_len,
102 unsigned char const *P, size_t P_len,
103 unsigned char const *Q, size_t Q_len,
104 unsigned char const *D, size_t D_len,
105 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100106{
107 int ret;
108
109 if( N != NULL )
110 {
111 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
112 ctx->len = mbedtls_mpi_size( &ctx->N );
113 }
114
115 if( P != NULL )
116 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
117
118 if( Q != NULL )
119 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
120
121 if( D != NULL )
122 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
123
124 if( E != NULL )
125 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
126
127cleanup:
128
129 if( ret != 0 )
130 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
131
132 return( 0 );
133}
134
Hanno Becker705fc682017-10-10 17:57:02 +0100135/*
136 * Checks whether the context fields are set in such a way
137 * that the RSA primitives will be able to execute without error.
138 * It does *not* make guarantees for consistency of the parameters.
139 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100140static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
141 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100142{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100143#if !defined(MBEDTLS_RSA_NO_CRT)
144 /* blinding_needed is only used for NO_CRT to decide whether
145 * P,Q need to be present or not. */
146 ((void) blinding_needed);
147#endif
148
Hanno Becker3a760a12018-01-05 08:14:49 +0000149 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
150 ctx->len > MBEDTLS_MPI_MAX_SIZE )
151 {
Hanno Becker705fc682017-10-10 17:57:02 +0100152 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000153 }
Hanno Becker705fc682017-10-10 17:57:02 +0100154
155 /*
156 * 1. Modular exponentiation needs positive, odd moduli.
157 */
158
159 /* Modular exponentiation wrt. N is always used for
160 * RSA public key operations. */
161 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
162 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
163 {
164 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
165 }
166
167#if !defined(MBEDTLS_RSA_NO_CRT)
168 /* Modular exponentiation for P and Q is only
169 * used for private key operations and if CRT
170 * is used. */
171 if( is_priv &&
172 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
173 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
174 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
175 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
176 {
177 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
178 }
179#endif /* !MBEDTLS_RSA_NO_CRT */
180
181 /*
182 * 2. Exponents must be positive
183 */
184
185 /* Always need E for public key operations */
186 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
187 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
188
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100189#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100190 /* For private key operations, use D or DP & DQ
191 * as (unblinded) exponents. */
192 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
194#else
195 if( is_priv &&
196 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
197 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
198 {
199 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
200 }
201#endif /* MBEDTLS_RSA_NO_CRT */
202
203 /* Blinding shouldn't make exponents negative either,
204 * so check that P, Q >= 1 if that hasn't yet been
205 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100206#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100207 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100208 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
209 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
210 {
211 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
212 }
213#endif
214
215 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100216 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100217#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100218 if( is_priv &&
219 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
220 {
221 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
222 }
223#endif
224
225 return( 0 );
226}
227
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100228int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100229{
230 int ret = 0;
231
Hanno Becker617c1ae2017-08-23 14:11:24 +0100232 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
233 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
234 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
235 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
236 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100237
Hanno Becker617c1ae2017-08-23 14:11:24 +0100238 /*
239 * Check whether provided parameters are enough
240 * to deduce all others. The following incomplete
241 * parameter sets for private keys are supported:
242 *
243 * (1) P, Q missing.
244 * (2) D and potentially N missing.
245 *
246 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100247
Hanno Becker2cca6f32017-09-29 11:46:40 +0100248 const int n_missing = have_P && have_Q && have_D && have_E;
249 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
250 const int d_missing = have_P && have_Q && !have_D && have_E;
251 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
252
253 /* These three alternatives are mutually exclusive */
254 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100255
Hanno Becker617c1ae2017-08-23 14:11:24 +0100256 if( !is_priv && !is_pub )
257 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
258
259 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100260 * Step 1: Deduce N if P, Q are provided.
261 */
262
263 if( !have_N && have_P && have_Q )
264 {
265 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
266 &ctx->Q ) ) != 0 )
267 {
268 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
269 }
270
271 ctx->len = mbedtls_mpi_size( &ctx->N );
272 }
273
274 /*
275 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100276 */
277
278 if( pq_missing )
279 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100280 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100281 &ctx->P, &ctx->Q );
282 if( ret != 0 )
283 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
284
285 }
286 else if( d_missing )
287 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100288 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
289 &ctx->Q,
290 &ctx->E,
291 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100292 {
293 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
294 }
295 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100296
Hanno Becker617c1ae2017-08-23 14:11:24 +0100297 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100298 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100299 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100300 */
301
Hanno Becker23344b52017-08-23 07:43:27 +0100302#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100303 if( is_priv )
304 {
305 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
306 &ctx->DP, &ctx->DQ, &ctx->QP );
307 if( ret != 0 )
308 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
309 }
Hanno Becker23344b52017-08-23 07:43:27 +0100310#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100311
312 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100313 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100314 */
315
Hanno Beckerebd2c022017-10-12 10:54:53 +0100316 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100317}
318
Hanno Becker617c1ae2017-08-23 14:11:24 +0100319int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
320 unsigned char *N, size_t N_len,
321 unsigned char *P, size_t P_len,
322 unsigned char *Q, size_t Q_len,
323 unsigned char *D, size_t D_len,
324 unsigned char *E, size_t E_len )
325{
326 int ret = 0;
327
328 /* Check if key is private or public */
329 const int is_priv =
330 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
331 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
332 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
333 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
334 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
335
336 if( !is_priv )
337 {
338 /* If we're trying to export private parameters for a public key,
339 * something must be wrong. */
340 if( P != NULL || Q != NULL || D != NULL )
341 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
342
343 }
344
345 if( N != NULL )
346 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
347
348 if( P != NULL )
349 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
350
351 if( Q != NULL )
352 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
353
354 if( D != NULL )
355 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
356
357 if( E != NULL )
358 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100359
360cleanup:
361
362 return( ret );
363}
364
Hanno Becker617c1ae2017-08-23 14:11:24 +0100365int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
366 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
367 mbedtls_mpi *D, mbedtls_mpi *E )
368{
369 int ret;
370
371 /* Check if key is private or public */
372 int is_priv =
373 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
374 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
375 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
376 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
377 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
378
379 if( !is_priv )
380 {
381 /* If we're trying to export private parameters for a public key,
382 * something must be wrong. */
383 if( P != NULL || Q != NULL || D != NULL )
384 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
385
386 }
387
388 /* Export all requested core parameters. */
389
390 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
391 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
392 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
393 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
394 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
395 {
396 return( ret );
397 }
398
399 return( 0 );
400}
401
402/*
403 * Export CRT parameters
404 * This must also be implemented if CRT is not used, for being able to
405 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
406 * can be used in this case.
407 */
408int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
409 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
410{
411 int ret;
412
413 /* Check if key is private or public */
414 int is_priv =
415 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
416 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
417 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
418 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
419 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
420
421 if( !is_priv )
422 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
423
Hanno Beckerdc95c892017-08-23 06:57:02 +0100424#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100425 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100426 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
427 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
428 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
429 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100430 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100431 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100432#else
433 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
434 DP, DQ, QP ) ) != 0 )
435 {
436 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
437 }
438#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100439
440 return( 0 );
441}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100442
443/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 * Initialize an RSA context
445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000448 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000449{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#if defined(MBEDTLS_THREADING_C)
455 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200456#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000457}
458
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100459/*
460 * Set padding for an existing RSA context
461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100463{
464 ctx->padding = padding;
465 ctx->hash_id = hash_id;
466}
467
Hanno Becker617c1ae2017-08-23 14:11:24 +0100468/*
469 * Get length in bytes of RSA modulus
470 */
471
472size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
473{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100474 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100475}
476
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480/*
481 * Generate an RSA keypair
482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000484 int (*f_rng)(void *, unsigned char *, size_t),
485 void *p_rng,
486 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000487{
488 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100489 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
Paul Bakker21eb2802010-08-16 11:10:02 +0000491 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
Janos Follathef441782016-09-21 13:18:12 +0100494 if( nbits % 2 )
495 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
496
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100497 mbedtls_mpi_init( &H );
498 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 /*
501 * find primes P and Q with Q < P so that:
502 * GCD( E, (P-1)*(Q-1) ) == 1
503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
506 do
507 {
Janos Follath10c575b2016-02-23 14:42:48 +0000508 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100509 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Janos Follathef441782016-09-21 13:18:12 +0100511 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100512 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 continue;
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200518 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 continue;
520
Janos Follathef441782016-09-21 13:18:12 +0100521 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100522 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100523
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100524 /* Temporarily replace P,Q by P-1, Q-1 */
525 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
526 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
527 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100532 /* Restore P,Q */
533 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
534 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
535
536 ctx->len = mbedtls_mpi_size( &ctx->N );
537
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 /*
539 * D = E^-1 mod ((P-1)*(Q-1))
540 * DP = D mod (P - 1)
541 * DQ = D mod (Q - 1)
542 * QP = Q^-1 mod P
543 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100545 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
546
547#if !defined(MBEDTLS_RSA_NO_CRT)
548 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
549 &ctx->DP, &ctx->DQ, &ctx->QP ) );
550#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
Hanno Becker83aad1f2017-08-23 06:45:10 +0100552 /* Double-check */
553 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
554
Paul Bakker5121ce52009-01-03 21:22:43 +0000555cleanup:
556
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100557 mbedtls_mpi_free( &H );
558 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 if( ret != 0 )
561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_rsa_free( ctx );
563 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 }
565
Paul Bakker48377d92013-08-30 12:06:24 +0200566 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567}
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571/*
572 * Check a public RSA key
573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000575{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100576 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Hanno Becker3a760a12018-01-05 08:14:49 +0000579 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100582 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Hanno Becker705fc682017-10-10 17:57:02 +0100584 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
585 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100589 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591 return( 0 );
592}
593
594/*
Hanno Becker705fc682017-10-10 17:57:02 +0100595 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000598{
Hanno Becker705fc682017-10-10 17:57:02 +0100599 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100600 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Hanno Becker705fc682017-10-10 17:57:02 +0100601 {
Hanno Becker98838b02017-10-02 13:16:10 +0100602 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker705fc682017-10-10 17:57:02 +0100603 }
Hanno Becker98838b02017-10-02 13:16:10 +0100604
605 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100606 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100608 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 }
Hanno Becker705fc682017-10-10 17:57:02 +0100610
Hanno Beckerb269a852017-08-25 08:03:21 +0100611#if !defined(MBEDTLS_RSA_NO_CRT)
612 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
613 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
614 {
615 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
616 }
617#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000618
619 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620}
621
622/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100623 * Check if contexts holding a public and private key match
624 */
Hanno Becker98838b02017-10-02 13:16:10 +0100625int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
626 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100627{
Hanno Becker98838b02017-10-02 13:16:10 +0100628 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100632 }
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
635 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100638 }
639
640 return( 0 );
641}
642
643/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 * Do an RSA public key operation
645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000647 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 unsigned char *output )
649{
Paul Bakker23986e52011-04-24 08:57:21 +0000650 int ret;
651 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
Hanno Beckerebd2c022017-10-12 10:54:53 +0100654 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100655 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000658
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200659#if defined(MBEDTLS_THREADING_C)
660 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
661 return( ret );
662#endif
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200668 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
669 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 }
671
672 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
674 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200678 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
679 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100680#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
684 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000686
687 return( 0 );
688}
689
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200690/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200691 * Generate or update blinding values, see section 10 of:
692 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200693 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200694 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200695 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200696static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200697 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
698{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200699 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200700
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200701 if( ctx->Vf.p != NULL )
702 {
703 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
705 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
706 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
707 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200708
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200709 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200710 }
711
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200712 /* Unblinding value: Vf = random number, invertible mod N */
713 do {
714 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
718 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
719 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200720
721 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
723 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 +0200724
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200725
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200726cleanup:
727 return( ret );
728}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200729
Paul Bakker5121ce52009-01-03 21:22:43 +0000730/*
Janos Follathe81102e2017-03-22 13:38:28 +0000731 * Exponent blinding supposed to prevent side-channel attacks using multiple
732 * traces of measurements to recover the RSA key. The more collisions are there,
733 * the more bits of the key can be recovered. See [3].
734 *
735 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
736 * observations on avarage.
737 *
738 * For example with 28 byte blinding to achieve 2 collisions the adversary has
739 * to make 2^112 observations on avarage.
740 *
741 * (With the currently (as of 2017 April) known best algorithms breaking 2048
742 * bit RSA requires approximately as much time as trying out 2^112 random keys.
743 * Thus in this sense with 28 byte blinding the security is not reduced by
744 * side-channel attacks like the one in [3])
745 *
746 * This countermeasure does not help if the key recovery is possible with a
747 * single trace.
748 */
749#define RSA_EXPONENT_BLINDING 28
750
751/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000752 * Do an RSA private key operation
753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200755 int (*f_rng)(void *, unsigned char *, size_t),
756 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000757 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000758 unsigned char *output )
759{
Paul Bakker23986e52011-04-24 08:57:21 +0000760 int ret;
761 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +0000763 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +0000764#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000765 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000766 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +0000767#else
768 mbedtls_mpi DP_blind, DQ_blind;
769 mbedtls_mpi *DP = &ctx->DP;
770 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +0000771#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
Hanno Beckerebd2c022017-10-12 10:54:53 +0100773 if( rsa_check_context( ctx, 1 /* private key checks */,
774 f_rng != NULL /* blinding y/n */ ) != 0 )
775 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100776 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100777 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000780 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
781
Janos Follathf9203b42017-03-22 15:13:15 +0000782 if( f_rng != NULL )
783 {
Janos Follathe81102e2017-03-22 13:38:28 +0000784#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000785 mbedtls_mpi_init( &D_blind );
786#else
787 mbedtls_mpi_init( &DP_blind );
788 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000789#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000790 }
Janos Follathe81102e2017-03-22 13:38:28 +0000791
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200793#if defined(MBEDTLS_THREADING_C)
794 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
795 return( ret );
796#endif
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
799 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000800 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200801 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
802 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000803 }
804
Paul Bakkerf451bac2013-08-30 15:37:02 +0200805 if( f_rng != NULL )
806 {
807 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200808 * Blinding
809 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200810 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200811 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
812 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000814
Janos Follathe81102e2017-03-22 13:38:28 +0000815 /*
816 * Exponent blinding
817 */
818 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
819 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
820
Janos Follathf9203b42017-03-22 15:13:15 +0000821#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000822 /*
823 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
824 */
825 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
826 f_rng, p_rng ) );
827 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
828 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
829 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
830
831 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000832#else
833 /*
834 * DP_blind = ( P - 1 ) * R + DP
835 */
836 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
837 f_rng, p_rng ) );
838 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
839 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
840 &ctx->DP ) );
841
842 DP = &DP_blind;
843
844 /*
845 * DQ_blind = ( Q - 1 ) * R + DQ
846 */
847 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
848 f_rng, p_rng ) );
849 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
850 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
851 &ctx->DQ ) );
852
853 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000854#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200855 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000858 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100859#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200860 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000861 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 *
863 * T1 = input ^ dP mod P
864 * T2 = input ^ dQ mod Q
865 */
Janos Follathf9203b42017-03-22 15:13:15 +0000866 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
867 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000868
869 /*
870 * T = (T1 - T2) * (Q^-1 mod P) mod P
871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
873 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
874 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000875
876 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200877 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
880 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
881#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200882
Paul Bakkerf451bac2013-08-30 15:37:02 +0200883 if( f_rng != NULL )
884 {
885 /*
886 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200887 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200888 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200889 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200891 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
893 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000895
896cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200898 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
899 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200900#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000903 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
904
905 if( f_rng != NULL )
906 {
Janos Follathe81102e2017-03-22 13:38:28 +0000907#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000908 mbedtls_mpi_free( &D_blind );
909#else
910 mbedtls_mpi_free( &DP_blind );
911 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000912#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000913 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
915 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000917
918 return( 0 );
919}
920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000922/**
923 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
924 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000925 * \param dst buffer to mask
926 * \param dlen length of destination buffer
927 * \param src source of the mask generation
928 * \param slen length of the source buffer
929 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000930 */
Paul Bakker48377d92013-08-30 12:06:24 +0200931static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000933{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000935 unsigned char counter[4];
936 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000937 unsigned int hlen;
938 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000941 memset( counter, 0, 4 );
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000944
Simon Butcher02037452016-03-01 21:19:12 +0000945 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000946 p = dst;
947
948 while( dlen > 0 )
949 {
950 use_len = hlen;
951 if( dlen < hlen )
952 use_len = dlen;
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 mbedtls_md_starts( md_ctx );
955 mbedtls_md_update( md_ctx, src, slen );
956 mbedtls_md_update( md_ctx, counter, 4 );
957 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000958
959 for( i = 0; i < use_len; ++i )
960 *p++ ^= mask[i];
961
962 counter[3]++;
963
964 dlen -= use_len;
965 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200966
967 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000968}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100972/*
973 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100976 int (*f_rng)(void *, unsigned char *, size_t),
977 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100978 int mode,
979 const unsigned char *label, size_t label_len,
980 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100981 const unsigned char *input,
982 unsigned char *output )
983{
984 size_t olen;
985 int ret;
986 unsigned char *p = output;
987 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 const mbedtls_md_info_t *md_info;
989 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
992 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200993
994 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100998 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001000
1001 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001003
Simon Butcher02037452016-03-01 21:19:12 +00001004 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001005 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001007
1008 memset( output, 0, olen );
1009
1010 *p++ = 0;
1011
Simon Butcher02037452016-03-01 21:19:12 +00001012 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001013 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001015
1016 p += hlen;
1017
Simon Butcher02037452016-03-01 21:19:12 +00001018 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001020 p += hlen;
1021 p += olen - 2 * hlen - 2 - ilen;
1022 *p++ = 1;
1023 memcpy( p, input, ilen );
1024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001026 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1027 {
1028 mbedtls_md_free( &md_ctx );
1029 return( ret );
1030 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001031
Simon Butcher02037452016-03-01 21:19:12 +00001032 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001033 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1034 &md_ctx );
1035
Simon Butcher02037452016-03-01 21:19:12 +00001036 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001037 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1038 &md_ctx );
1039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 return( ( mode == MBEDTLS_RSA_PUBLIC )
1043 ? mbedtls_rsa_public( ctx, output, output )
1044 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001049/*
1050 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001053 int (*f_rng)(void *, unsigned char *, size_t),
1054 void *p_rng,
1055 int mode, size_t ilen,
1056 const unsigned char *input,
1057 unsigned char *output )
1058{
1059 size_t nb_pad, olen;
1060 int ret;
1061 unsigned char *p = output;
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1064 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001065
Janos Follath1ed9f992016-03-18 11:45:44 +00001066 // We don't check p_rng because it won't be dereferenced here
1067 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001069
1070 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001071
Simon Butcher02037452016-03-01 21:19:12 +00001072 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001073 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001075
1076 nb_pad = olen - 3 - ilen;
1077
1078 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001082
1083 while( nb_pad-- > 0 )
1084 {
1085 int rng_dl = 100;
1086
1087 do {
1088 ret = f_rng( p_rng, p, 1 );
1089 } while( *p == 0 && --rng_dl && ret == 0 );
1090
Simon Butcher02037452016-03-01 21:19:12 +00001091 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001092 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001094
1095 p++;
1096 }
1097 }
1098 else
1099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001101
1102 while( nb_pad-- > 0 )
1103 *p++ = 0xFF;
1104 }
1105
1106 *p++ = 0;
1107 memcpy( p, input, ilen );
1108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 return( ( mode == MBEDTLS_RSA_PUBLIC )
1110 ? mbedtls_rsa_public( ctx, output, output )
1111 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001114
Paul Bakker5121ce52009-01-03 21:22:43 +00001115/*
1116 * Add the message padding, then do an RSA operation
1117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001119 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001120 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001121 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001122 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 unsigned char *output )
1124{
Paul Bakker5121ce52009-01-03 21:22:43 +00001125 switch( ctx->padding )
1126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#if defined(MBEDTLS_PKCS1_V15)
1128 case MBEDTLS_RSA_PKCS_V15:
1129 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001130 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001131#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133#if defined(MBEDTLS_PKCS1_V21)
1134 case MBEDTLS_RSA_PKCS_V21:
1135 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001136 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001137#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
1139 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001141 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001142}
1143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001145/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001146 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001149 int (*f_rng)(void *, unsigned char *, size_t),
1150 void *p_rng,
1151 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001152 const unsigned char *label, size_t label_len,
1153 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001154 const unsigned char *input,
1155 unsigned char *output,
1156 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001157{
Paul Bakker23986e52011-04-24 08:57:21 +00001158 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001159 size_t ilen, i, pad_len;
1160 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1162 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001163 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 const mbedtls_md_info_t *md_info;
1165 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001166
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001167 /*
1168 * Parameters sanity checks
1169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1171 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
1173 ilen = ctx->len;
1174
Paul Bakker27fdf462011-06-09 13:55:13 +00001175 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001179 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001181
Janos Follathc17cda12016-02-11 11:08:18 +00001182 hlen = mbedtls_md_get_size( md_info );
1183
1184 // checking for integer underflow
1185 if( 2 * hlen + 2 > ilen )
1186 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1187
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001188 /*
1189 * RSA operation
1190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1192 ? mbedtls_rsa_public( ctx, input, buf )
1193 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
1195 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001196 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001197
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001198 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001199 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001202 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1203 {
1204 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001205 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001206 }
1207
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001208
1209 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001211
1212 /* seed: Apply seedMask to maskedSeed */
1213 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1214 &md_ctx );
1215
1216 /* DB: Apply dbMask to maskedDB */
1217 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1218 &md_ctx );
1219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001221
1222 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001223 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001224 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001226 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001227
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001228 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001229
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001230 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001231
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001232 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001233 for( i = 0; i < hlen; i++ )
1234 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001235
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001236 /* Get zero-padding len, but always read till end of buffer
1237 * (minus one, for the 01 byte) */
1238 pad_len = 0;
1239 pad_done = 0;
1240 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1241 {
1242 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001243 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001244 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001245
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001246 p += pad_len;
1247 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001248
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001249 /*
1250 * The only information "leaked" is whether the padding was correct or not
1251 * (eg, no data is copied if it was not correct). This meets the
1252 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1253 * the different error conditions.
1254 */
1255 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001256 {
1257 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1258 goto cleanup;
1259 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001260
Paul Bakker66d5d072014-06-17 16:39:18 +02001261 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001262 {
1263 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1264 goto cleanup;
1265 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001266
1267 *olen = ilen - (p - buf);
1268 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001269 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001270
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001271cleanup:
1272 mbedtls_zeroize( buf, sizeof( buf ) );
1273 mbedtls_zeroize( lhash, sizeof( lhash ) );
1274
1275 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001276}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001280/*
1281 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001284 int (*f_rng)(void *, unsigned char *, size_t),
1285 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001286 int mode, size_t *olen,
1287 const unsigned char *input,
1288 unsigned char *output,
1289 size_t output_max_len)
1290{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001291 int ret;
1292 size_t ilen, pad_count = 0, i;
1293 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1297 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001298
1299 ilen = ctx->len;
1300
1301 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1305 ? mbedtls_rsa_public( ctx, input, buf )
1306 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001307
1308 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001309 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001310
1311 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001312 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001313
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001314 /*
1315 * Check and get padding len in "constant-time"
1316 */
1317 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001318
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001319 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001323
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001324 /* Get padding len, but always read till end of buffer
1325 * (minus one, for the 00 byte) */
1326 for( i = 0; i < ilen - 3; i++ )
1327 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001328 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1329 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001330 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001331
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001332 p += pad_count;
1333 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001334 }
1335 else
1336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001338
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001339 /* Get padding len, but always read till end of buffer
1340 * (minus one, for the 00 byte) */
1341 for( i = 0; i < ilen - 3; i++ )
1342 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001343 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001344 pad_count += ( pad_done == 0 );
1345 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001346
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001347 p += pad_count;
1348 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 }
1350
Janos Follathc69fa502016-02-12 13:30:09 +00001351 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001352
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001353 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001354 {
1355 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1356 goto cleanup;
1357 }
Paul Bakker8804f692013-02-28 18:06:26 +01001358
Paul Bakker66d5d072014-06-17 16:39:18 +02001359 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001360 {
1361 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1362 goto cleanup;
1363 }
Paul Bakker060c5682009-01-12 21:48:39 +00001364
Paul Bakker27fdf462011-06-09 13:55:13 +00001365 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001367 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001368
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001369cleanup:
1370 mbedtls_zeroize( buf, sizeof( buf ) );
1371
1372 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001375
1376/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001377 * Do an RSA operation, then remove the message padding
1378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001380 int (*f_rng)(void *, unsigned char *, size_t),
1381 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001382 int mode, size_t *olen,
1383 const unsigned char *input,
1384 unsigned char *output,
1385 size_t output_max_len)
1386{
1387 switch( ctx->padding )
1388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389#if defined(MBEDTLS_PKCS1_V15)
1390 case MBEDTLS_RSA_PKCS_V15:
1391 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001392 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001393#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#if defined(MBEDTLS_PKCS1_V21)
1396 case MBEDTLS_RSA_PKCS_V21:
1397 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001398 olen, input, output,
1399 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001400#endif
1401
1402 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001404 }
1405}
1406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001408/*
1409 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001412 int (*f_rng)(void *, unsigned char *, size_t),
1413 void *p_rng,
1414 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001416 unsigned int hashlen,
1417 const unsigned char *hash,
1418 unsigned char *sig )
1419{
1420 size_t olen;
1421 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001423 unsigned int slen, hlen, offset = 0;
1424 int ret;
1425 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 const mbedtls_md_info_t *md_info;
1427 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1430 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001431
1432 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001434
1435 olen = ctx->len;
1436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001438 {
Simon Butcher02037452016-03-01 21:19:12 +00001439 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001441 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001445 }
1446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001448 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001452 slen = hlen;
1453
1454 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001456
1457 memset( sig, 0, olen );
1458
Simon Butcher02037452016-03-01 21:19:12 +00001459 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001460 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001462
Simon Butcher02037452016-03-01 21:19:12 +00001463 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001464 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001465 p += olen - hlen * 2 - 2;
1466 *p++ = 0x01;
1467 memcpy( p, salt, slen );
1468 p += slen;
1469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001471 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1472 {
1473 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001474 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001475 return( ret );
1476 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001477
Simon Butcher02037452016-03-01 21:19:12 +00001478 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 mbedtls_md_starts( &md_ctx );
1480 mbedtls_md_update( &md_ctx, p, 8 );
1481 mbedtls_md_update( &md_ctx, hash, hashlen );
1482 mbedtls_md_update( &md_ctx, salt, slen );
1483 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001484 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001485
Simon Butcher02037452016-03-01 21:19:12 +00001486 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001487 if( msb % 8 == 0 )
1488 offset = 1;
1489
Simon Butcher02037452016-03-01 21:19:12 +00001490 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001491 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001494
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001495 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001496 sig[0] &= 0xFF >> ( olen * 8 - msb );
1497
1498 p += hlen;
1499 *p++ = 0xBC;
1500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 return( ( mode == MBEDTLS_RSA_PUBLIC )
1502 ? mbedtls_rsa_public( ctx, sig, sig )
1503 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001508/*
1509 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1510 */
1511/*
1512 * Do an RSA operation to sign the message digest
1513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001515 int (*f_rng)(void *, unsigned char *, size_t),
1516 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001517 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001519 unsigned int hashlen,
1520 const unsigned char *hash,
1521 unsigned char *sig )
1522{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001523 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001524 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001525 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001526 unsigned char *sig_try = NULL, *verif = NULL;
1527 size_t i;
1528 unsigned char diff;
1529 volatile unsigned char diff_no_optimize;
1530 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1533 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001534
1535 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001536 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001541 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1545 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001546
Paul Bakkerc70b9822013-04-07 22:00:46 +02001547 nb_pad -= 10 + oid_size;
1548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001550 }
1551
Paul Bakkerc70b9822013-04-07 22:00:46 +02001552 nb_pad -= hashlen;
1553
Paul Bakkerb3869132013-02-28 17:21:01 +01001554 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001556
1557 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001559 memset( p, 0xFF, nb_pad );
1560 p += nb_pad;
1561 *p++ = 0;
1562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001564 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001565 memcpy( p, hash, hashlen );
1566 }
1567 else
1568 {
1569 /*
1570 * DigestInfo ::= SEQUENCE {
1571 * digestAlgorithm DigestAlgorithmIdentifier,
1572 * digest Digest }
1573 *
1574 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1575 *
1576 * Digest ::= OCTET STRING
1577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001579 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001581 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001583 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001584 memcpy( p, oid, oid_size );
1585 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001587 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001589 *p++ = hashlen;
1590 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001591 }
1592
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001593 if( mode == MBEDTLS_RSA_PUBLIC )
1594 return( mbedtls_rsa_public( ctx, sig, sig ) );
1595
1596 /*
1597 * In order to prevent Lenstra's attack, make the signature in a
1598 * temporary buffer and check it before returning it.
1599 */
1600 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001601 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001602 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1603
Simon Butcher1285ab52016-01-01 21:42:47 +00001604 verif = mbedtls_calloc( 1, ctx->len );
1605 if( verif == NULL )
1606 {
1607 mbedtls_free( sig_try );
1608 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1609 }
1610
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001611 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1612 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1613
1614 /* Compare in constant time just in case */
1615 for( diff = 0, i = 0; i < ctx->len; i++ )
1616 diff |= verif[i] ^ sig[i];
1617 diff_no_optimize = diff;
1618
1619 if( diff_no_optimize != 0 )
1620 {
1621 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1622 goto cleanup;
1623 }
1624
1625 memcpy( sig, sig_try, ctx->len );
1626
1627cleanup:
1628 mbedtls_free( sig_try );
1629 mbedtls_free( verif );
1630
1631 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001634
1635/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001636 * Do an RSA operation to sign the message digest
1637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001639 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001640 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001641 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001643 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001644 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001645 unsigned char *sig )
1646{
Paul Bakker5121ce52009-01-03 21:22:43 +00001647 switch( ctx->padding )
1648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649#if defined(MBEDTLS_PKCS1_V15)
1650 case MBEDTLS_RSA_PKCS_V15:
1651 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001652 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001653#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#if defined(MBEDTLS_PKCS1_V21)
1656 case MBEDTLS_RSA_PKCS_V21:
1657 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001658 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001659#endif
1660
Paul Bakker5121ce52009-01-03 21:22:43 +00001661 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001664}
1665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001667/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001668 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001671 int (*f_rng)(void *, unsigned char *, size_t),
1672 void *p_rng,
1673 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001675 unsigned int hashlen,
1676 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001678 int expected_salt_len,
1679 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001680{
Paul Bakker23986e52011-04-24 08:57:21 +00001681 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001682 size_t siglen;
1683 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001685 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001686 unsigned int hlen;
1687 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 const mbedtls_md_info_t *md_info;
1689 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001690 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1693 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001694
Paul Bakker5121ce52009-01-03 21:22:43 +00001695 siglen = ctx->len;
1696
Paul Bakker27fdf462011-06-09 13:55:13 +00001697 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1701 ? mbedtls_rsa_public( ctx, sig, buf )
1702 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
1704 if( ret != 0 )
1705 return( ret );
1706
1707 p = buf;
1708
Paul Bakkerb3869132013-02-28 17:21:01 +01001709 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001713 {
Simon Butcher02037452016-03-01 21:19:12 +00001714 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001716 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001720 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001723 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001727 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001728
Paul Bakkerb3869132013-02-28 17:21:01 +01001729 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001730
Simon Butcher02037452016-03-01 21:19:12 +00001731 /*
1732 * Note: EMSA-PSS verification is over the length of N - 1 bits
1733 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001734 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001735
Simon Butcher02037452016-03-01 21:19:12 +00001736 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001737 if( msb % 8 == 0 )
1738 {
1739 p++;
1740 siglen -= 1;
1741 }
1742 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001746 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1747 {
1748 mbedtls_md_free( &md_ctx );
1749 return( ret );
1750 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001751
Paul Bakkerb3869132013-02-28 17:21:01 +01001752 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001753
Paul Bakkerb3869132013-02-28 17:21:01 +01001754 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001755
Paul Bakker4de44aa2013-12-31 11:43:01 +01001756 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001757 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001758
Paul Bakkerb3869132013-02-28 17:21:01 +01001759 if( p == buf + siglen ||
1760 *p++ != 0x01 )
1761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 mbedtls_md_free( &md_ctx );
1763 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001764 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001765
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001766 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001767 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001770 slen != (size_t) expected_salt_len )
1771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 mbedtls_md_free( &md_ctx );
1773 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001774 }
1775
Simon Butcher02037452016-03-01 21:19:12 +00001776 /*
1777 * Generate H = Hash( M' )
1778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 mbedtls_md_starts( &md_ctx );
1780 mbedtls_md_update( &md_ctx, zeros, 8 );
1781 mbedtls_md_update( &md_ctx, hash, hashlen );
1782 mbedtls_md_update( &md_ctx, p, slen );
1783 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001786
Paul Bakkerb3869132013-02-28 17:21:01 +01001787 if( memcmp( p + slen, result, hlen ) == 0 )
1788 return( 0 );
1789 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001791}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001792
1793/*
1794 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001797 int (*f_rng)(void *, unsigned char *, size_t),
1798 void *p_rng,
1799 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001801 unsigned int hashlen,
1802 const unsigned char *hash,
1803 const unsigned char *sig )
1804{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1806 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001807 : md_alg;
1808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001810 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001812 sig ) );
1813
1814}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001818/*
1819 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001822 int (*f_rng)(void *, unsigned char *, size_t),
1823 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001824 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001826 unsigned int hashlen,
1827 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001828 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001829{
1830 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001831 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001832 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 mbedtls_md_type_t msg_md_alg;
1834 const mbedtls_md_info_t *md_info;
1835 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001836 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1839 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001840
1841 siglen = ctx->len;
1842
1843 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1847 ? mbedtls_rsa_public( ctx, sig, buf )
1848 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001849
1850 if( ret != 0 )
1851 return( ret );
1852
1853 p = buf;
1854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1856 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001857
1858 while( *p != 0 )
1859 {
1860 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001862 p++;
1863 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02001864 p++; /* skip 00 byte */
1865
1866 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
1867 if( p - buf < 11 )
1868 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001869
1870 len = siglen - ( p - buf );
1871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001873 {
1874 if( memcmp( p, hash, hashlen ) == 0 )
1875 return( 0 );
1876 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 }
1879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001881 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1883 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001884
1885 end = p + len;
1886
Simon Butcher02037452016-03-01 21:19:12 +00001887 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02001888 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
1889 * Insist on 2-byte length tags, to protect against variants of
1890 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00001891 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001892 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1894 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1895 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001896 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001898
Gilles Peskinee7e76502017-05-04 12:48:39 +02001899 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1901 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1902 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001903 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001905
Gilles Peskinee7e76502017-05-04 12:48:39 +02001906 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1908 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001909 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001910 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001911
1912 oid.p = p;
1913 p += oid.len;
1914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1916 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001917
1918 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001920
1921 /*
1922 * assume the algorithm parameters must be NULL
1923 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02001924 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1926 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001927 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001929
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001930 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001931 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1932 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001933 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001935
1936 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001938
1939 p += hashlen;
1940
1941 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001943
1944 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001945}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001947
1948/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001949 * Do an RSA operation and check the message digest
1950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001952 int (*f_rng)(void *, unsigned char *, size_t),
1953 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001954 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001956 unsigned int hashlen,
1957 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001958 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001959{
1960 switch( ctx->padding )
1961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962#if defined(MBEDTLS_PKCS1_V15)
1963 case MBEDTLS_RSA_PKCS_V15:
1964 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001965 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001966#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968#if defined(MBEDTLS_PKCS1_V21)
1969 case MBEDTLS_RSA_PKCS_V21:
1970 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001971 hashlen, hash, sig );
1972#endif
1973
1974 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001976 }
1977}
1978
1979/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001980 * Copy the components of an RSA key
1981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001983{
1984 int ret;
1985
1986 dst->ver = src->ver;
1987 dst->len = src->len;
1988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1990 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1993 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1994 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01001995
1996#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1998 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1999 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2001 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002002#endif
2003
2004 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2007 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002008
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002009 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002010 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002011
2012cleanup:
2013 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002015
2016 return( ret );
2017}
2018
2019/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 * Free the components of an RSA key
2021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002023{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002025 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2026 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002028
Hanno Becker33c30a02017-08-23 07:00:22 +01002029#if !defined(MBEDTLS_RSA_NO_CRT)
2030 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2031 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2032 mbedtls_mpi_free( &ctx->DP );
2033#endif /* MBEDTLS_RSA_NO_CRT */
2034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035#if defined(MBEDTLS_THREADING_C)
2036 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002037#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002038}
2039
Hanno Beckerab377312017-08-23 16:24:51 +01002040#endif /* !MBEDTLS_RSA_ALT */
2041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002044#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
2046/*
2047 * Example RSA-1024 keypair, for test purposes
2048 */
2049#define KEY_LEN 128
2050
2051#define RSA_N "9292758453063D803DD603D5E777D788" \
2052 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2053 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2054 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2055 "93A89813FBF3C4F8066D2D800F7C38A8" \
2056 "1AE31942917403FF4946B0A83D3D3E05" \
2057 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2058 "5E94BB77B07507233A0BC7BAC8F90F79"
2059
2060#define RSA_E "10001"
2061
2062#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2063 "66CA472BC44D253102F8B4A9D3BFA750" \
2064 "91386C0077937FE33FA3252D28855837" \
2065 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2066 "DF79C5CE07EE72C7F123142198164234" \
2067 "CABB724CF78B8173B9F880FC86322407" \
2068 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2069 "071513A1E85B5DFA031F21ECAE91A34D"
2070
2071#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2072 "2C01CAD19EA484A87EA4377637E75500" \
2073 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2074 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2075
2076#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2077 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2078 "910E4168387E3C30AA1E00C339A79508" \
2079 "8452DD96A9A5EA5D9DCA68DA636032AF"
2080
Paul Bakker5121ce52009-01-03 21:22:43 +00002081#define PT_LEN 24
2082#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2083 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002086static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002087{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002088#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002089 size_t i;
2090
Paul Bakker545570e2010-07-18 09:00:25 +00002091 if( rng_state != NULL )
2092 rng_state = NULL;
2093
Paul Bakkera3d195c2011-11-27 21:07:34 +00002094 for( i = 0; i < len; ++i )
2095 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002096#else
2097 if( rng_state != NULL )
2098 rng_state = NULL;
2099
2100 arc4random_buf( output, len );
2101#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002102
Paul Bakkera3d195c2011-11-27 21:07:34 +00002103 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002104}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002106
Paul Bakker5121ce52009-01-03 21:22:43 +00002107/*
2108 * Checkup routine
2109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002111{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002112 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002114 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 unsigned char rsa_plaintext[PT_LEN];
2117 unsigned char rsa_decrypted[PT_LEN];
2118 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002120 unsigned char sha1sum[20];
2121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
Hanno Becker3a701162017-08-22 13:52:43 +01002123 mbedtls_mpi K;
2124
2125 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002127
Hanno Becker3a701162017-08-22 13:52:43 +01002128 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2129 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2130 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2131 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2132 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2133 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2134 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2135 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2136 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2137 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2138
Hanno Becker7f25f852017-10-10 16:56:22 +01002139 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002140
2141 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2145 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 {
2147 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002149
2150 return( 1 );
2151 }
2152
2153 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
2156 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2157
Hanno Becker98838b02017-10-02 13:16:10 +01002158 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2159 PT_LEN, rsa_plaintext,
2160 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 {
2162 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002164
2165 return( 1 );
2166 }
2167
2168 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
Hanno Becker98838b02017-10-02 13:16:10 +01002171 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2172 &len, rsa_ciphertext, rsa_decrypted,
2173 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 {
2175 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
2178 return( 1 );
2179 }
2180
2181 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2182 {
2183 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
2186 return( 1 );
2187 }
2188
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002189 if( verbose != 0 )
2190 mbedtls_printf( "passed\n" );
2191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002194 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002197
Hanno Becker98838b02017-10-02 13:16:10 +01002198 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2199 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2200 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 {
2202 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
2205 return( 1 );
2206 }
2207
2208 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002210
Hanno Becker98838b02017-10-02 13:16:10 +01002211 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2212 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2213 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 {
2215 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002217
2218 return( 1 );
2219 }
2220
2221 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002222 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002224
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002225 if( verbose != 0 )
2226 mbedtls_printf( "\n" );
2227
Paul Bakker3d8fb632014-04-17 12:42:41 +02002228cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002229 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 mbedtls_rsa_free( &rsa );
2231#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002232 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002234 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235}
2236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#endif /* MBEDTLS_RSA_C */