blob: 79316733981bdb9dfcb1fb86a79f6120af9af7fa [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 Becker705fc682017-10-10 17:57:02 +0100149 if( ctx->len != mbedtls_mpi_size( &ctx->N ) )
150 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
151
152 /*
153 * 1. Modular exponentiation needs positive, odd moduli.
154 */
155
156 /* Modular exponentiation wrt. N is always used for
157 * RSA public key operations. */
158 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
159 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
160 {
161 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
162 }
163
164#if !defined(MBEDTLS_RSA_NO_CRT)
165 /* Modular exponentiation for P and Q is only
166 * used for private key operations and if CRT
167 * is used. */
168 if( is_priv &&
169 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
170 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
171 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
172 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
173 {
174 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
175 }
176#endif /* !MBEDTLS_RSA_NO_CRT */
177
178 /*
179 * 2. Exponents must be positive
180 */
181
182 /* Always need E for public key operations */
183 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
185
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100186#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100187 /* For private key operations, use D or DP & DQ
188 * as (unblinded) exponents. */
189 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
190 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
191#else
192 if( is_priv &&
193 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
194 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
195 {
196 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
197 }
198#endif /* MBEDTLS_RSA_NO_CRT */
199
200 /* Blinding shouldn't make exponents negative either,
201 * so check that P, Q >= 1 if that hasn't yet been
202 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100203#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100204 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100205 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
206 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
207 {
208 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
209 }
210#endif
211
212 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100213 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100214#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100215 if( is_priv &&
216 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
217 {
218 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
219 }
220#endif
221
222 return( 0 );
223}
224
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100225int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100226{
227 int ret = 0;
228
Hanno Becker617c1ae2017-08-23 14:11:24 +0100229 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
230 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
231 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
232 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
233 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100234
Hanno Becker617c1ae2017-08-23 14:11:24 +0100235 /*
236 * Check whether provided parameters are enough
237 * to deduce all others. The following incomplete
238 * parameter sets for private keys are supported:
239 *
240 * (1) P, Q missing.
241 * (2) D and potentially N missing.
242 *
243 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100244
Hanno Becker2cca6f32017-09-29 11:46:40 +0100245 const int n_missing = have_P && have_Q && have_D && have_E;
246 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
247 const int d_missing = have_P && have_Q && !have_D && have_E;
248 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
249
250 /* These three alternatives are mutually exclusive */
251 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100252
Hanno Becker617c1ae2017-08-23 14:11:24 +0100253 if( !is_priv && !is_pub )
254 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
255
256 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100257 * Step 1: Deduce N if P, Q are provided.
258 */
259
260 if( !have_N && have_P && have_Q )
261 {
262 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
263 &ctx->Q ) ) != 0 )
264 {
265 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
266 }
267
268 ctx->len = mbedtls_mpi_size( &ctx->N );
269 }
270
271 /*
272 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100273 */
274
275 if( pq_missing )
276 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100277 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100278 &ctx->P, &ctx->Q );
279 if( ret != 0 )
280 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
281
282 }
283 else if( d_missing )
284 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100285 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
286 &ctx->Q,
287 &ctx->E,
288 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100289 {
290 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
291 }
292 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100293
Hanno Becker617c1ae2017-08-23 14:11:24 +0100294 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100295 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100296 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100297 */
298
Hanno Becker23344b52017-08-23 07:43:27 +0100299#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100300 if( is_priv )
301 {
302 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
303 &ctx->DP, &ctx->DQ, &ctx->QP );
304 if( ret != 0 )
305 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
306 }
Hanno Becker23344b52017-08-23 07:43:27 +0100307#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100308
309 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100310 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100311 */
312
Hanno Beckerebd2c022017-10-12 10:54:53 +0100313 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100314}
315
Hanno Becker617c1ae2017-08-23 14:11:24 +0100316int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
317 unsigned char *N, size_t N_len,
318 unsigned char *P, size_t P_len,
319 unsigned char *Q, size_t Q_len,
320 unsigned char *D, size_t D_len,
321 unsigned char *E, size_t E_len )
322{
323 int ret = 0;
324
325 /* Check if key is private or public */
326 const int is_priv =
327 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
328 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
329 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
330 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
331 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
332
333 if( !is_priv )
334 {
335 /* If we're trying to export private parameters for a public key,
336 * something must be wrong. */
337 if( P != NULL || Q != NULL || D != NULL )
338 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
339
340 }
341
342 if( N != NULL )
343 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
344
345 if( P != NULL )
346 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
347
348 if( Q != NULL )
349 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
350
351 if( D != NULL )
352 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
353
354 if( E != NULL )
355 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100356
357cleanup:
358
359 return( ret );
360}
361
Hanno Becker617c1ae2017-08-23 14:11:24 +0100362int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
363 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
364 mbedtls_mpi *D, mbedtls_mpi *E )
365{
366 int ret;
367
368 /* Check if key is private or public */
369 int is_priv =
370 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
371 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
372 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
373 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
374 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
375
376 if( !is_priv )
377 {
378 /* If we're trying to export private parameters for a public key,
379 * something must be wrong. */
380 if( P != NULL || Q != NULL || D != NULL )
381 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
382
383 }
384
385 /* Export all requested core parameters. */
386
387 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
388 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
389 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
390 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
391 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
392 {
393 return( ret );
394 }
395
396 return( 0 );
397}
398
399/*
400 * Export CRT parameters
401 * This must also be implemented if CRT is not used, for being able to
402 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
403 * can be used in this case.
404 */
405int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
406 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
407{
408 int ret;
409
410 /* Check if key is private or public */
411 int is_priv =
412 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
413 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
414 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
415 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
416 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
417
418 if( !is_priv )
419 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
420
Hanno Beckerdc95c892017-08-23 06:57:02 +0100421#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100422 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100423 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
424 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
425 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
426 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100427 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100428 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100429#else
430 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
431 DP, DQ, QP ) ) != 0 )
432 {
433 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
434 }
435#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100436
437 return( 0 );
438}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100439
440/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 * Initialize an RSA context
442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000445 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000446{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#if defined(MBEDTLS_THREADING_C)
452 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200453#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000454}
455
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100456/*
457 * Set padding for an existing RSA context
458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100460{
461 ctx->padding = padding;
462 ctx->hash_id = hash_id;
463}
464
Hanno Becker617c1ae2017-08-23 14:11:24 +0100465/*
466 * Get length in bytes of RSA modulus
467 */
468
469size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
470{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100471 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100472}
473
474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477/*
478 * Generate an RSA keypair
479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000481 int (*f_rng)(void *, unsigned char *, size_t),
482 void *p_rng,
483 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000484{
485 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100486 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
Paul Bakker21eb2802010-08-16 11:10:02 +0000488 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
Janos Follathef441782016-09-21 13:18:12 +0100491 if( nbits % 2 )
492 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
493
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100494 mbedtls_mpi_init( &H );
495 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
497 /*
498 * find primes P and Q with Q < P so that:
499 * GCD( E, (P-1)*(Q-1) ) == 1
500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
503 do
504 {
Janos Follath10c575b2016-02-23 14:42:48 +0000505 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100506 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Janos Follathef441782016-09-21 13:18:12 +0100508 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100509 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 continue;
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200515 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 continue;
517
Janos Follathef441782016-09-21 13:18:12 +0100518 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100519 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100520
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100521 /* Temporarily replace P,Q by P-1, Q-1 */
522 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
523 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
524 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100529 /* Restore P,Q */
530 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
531 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
532
533 ctx->len = mbedtls_mpi_size( &ctx->N );
534
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 /*
536 * D = E^-1 mod ((P-1)*(Q-1))
537 * DP = D mod (P - 1)
538 * DQ = D mod (Q - 1)
539 * QP = Q^-1 mod P
540 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100542 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
543
544#if !defined(MBEDTLS_RSA_NO_CRT)
545 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
546 &ctx->DP, &ctx->DQ, &ctx->QP ) );
547#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Hanno Becker83aad1f2017-08-23 06:45:10 +0100549 /* Double-check */
550 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
551
Paul Bakker5121ce52009-01-03 21:22:43 +0000552cleanup:
553
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100554 mbedtls_mpi_free( &H );
555 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557 if( ret != 0 )
558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_rsa_free( ctx );
560 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 }
562
Paul Bakker48377d92013-08-30 12:06:24 +0200563 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564}
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
568/*
569 * Check a public RSA key
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000572{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100573 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200576 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
577 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Hanno Becker98838b02017-10-02 13:16:10 +0100578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100580 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
Hanno Becker705fc682017-10-10 17:57:02 +0100582 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
583 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100587 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
589 return( 0 );
590}
591
592/*
Hanno Becker705fc682017-10-10 17:57:02 +0100593 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000596{
Hanno Becker705fc682017-10-10 17:57:02 +0100597 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100598 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Hanno Becker705fc682017-10-10 17:57:02 +0100599 {
Hanno Becker98838b02017-10-02 13:16:10 +0100600 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker705fc682017-10-10 17:57:02 +0100601 }
Hanno Becker98838b02017-10-02 13:16:10 +0100602
603 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100604 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100606 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 }
Hanno Becker705fc682017-10-10 17:57:02 +0100608
Hanno Beckerb269a852017-08-25 08:03:21 +0100609#if !defined(MBEDTLS_RSA_NO_CRT)
610 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
611 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
612 {
613 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
614 }
615#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000616
617 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000618}
619
620/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100621 * Check if contexts holding a public and private key match
622 */
Hanno Becker98838b02017-10-02 13:16:10 +0100623int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
624 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100625{
Hanno Becker98838b02017-10-02 13:16:10 +0100626 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100630 }
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
633 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100636 }
637
638 return( 0 );
639}
640
641/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 * Do an RSA public key operation
643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000645 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 unsigned char *output )
647{
Paul Bakker23986e52011-04-24 08:57:21 +0000648 int ret;
649 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Hanno Beckerebd2c022017-10-12 10:54:53 +0100652 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100653 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200657#if defined(MBEDTLS_THREADING_C)
658 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
659 return( ret );
660#endif
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200666 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
667 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 }
669
670 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
672 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
674cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200676 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
677 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100678#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681
682 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
685 return( 0 );
686}
687
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200688/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200689 * Generate or update blinding values, see section 10 of:
690 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200691 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200692 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200693 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200694static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200695 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
696{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200697 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200698
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200699 if( ctx->Vf.p != NULL )
700 {
701 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
703 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
704 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
705 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200706
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200707 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200708 }
709
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200710 /* Unblinding value: Vf = random number, invertible mod N */
711 do {
712 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
716 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
717 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200718
719 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
721 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 +0200722
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200723
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200724cleanup:
725 return( ret );
726}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200727
Paul Bakker5121ce52009-01-03 21:22:43 +0000728/*
Janos Follathe81102e2017-03-22 13:38:28 +0000729 * Exponent blinding supposed to prevent side-channel attacks using multiple
730 * traces of measurements to recover the RSA key. The more collisions are there,
731 * the more bits of the key can be recovered. See [3].
732 *
733 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
734 * observations on avarage.
735 *
736 * For example with 28 byte blinding to achieve 2 collisions the adversary has
737 * to make 2^112 observations on avarage.
738 *
739 * (With the currently (as of 2017 April) known best algorithms breaking 2048
740 * bit RSA requires approximately as much time as trying out 2^112 random keys.
741 * Thus in this sense with 28 byte blinding the security is not reduced by
742 * side-channel attacks like the one in [3])
743 *
744 * This countermeasure does not help if the key recovery is possible with a
745 * single trace.
746 */
747#define RSA_EXPONENT_BLINDING 28
748
749/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000750 * Do an RSA private key operation
751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200753 int (*f_rng)(void *, unsigned char *, size_t),
754 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000755 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 unsigned char *output )
757{
Paul Bakker23986e52011-04-24 08:57:21 +0000758 int ret;
759 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +0000761 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +0000762#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000763 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000764 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +0000765#else
766 mbedtls_mpi DP_blind, DQ_blind;
767 mbedtls_mpi *DP = &ctx->DP;
768 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +0000769#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Hanno Beckerebd2c022017-10-12 10:54:53 +0100771 if( rsa_check_context( ctx, 1 /* private key checks */,
772 f_rng != NULL /* blinding y/n */ ) != 0 )
773 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100774 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100775 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000778 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
779
Janos Follathf9203b42017-03-22 15:13:15 +0000780 if( f_rng != NULL )
781 {
Janos Follathe81102e2017-03-22 13:38:28 +0000782#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000783 mbedtls_mpi_init( &D_blind );
784#else
785 mbedtls_mpi_init( &DP_blind );
786 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000787#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000788 }
Janos Follathe81102e2017-03-22 13:38:28 +0000789
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200791#if defined(MBEDTLS_THREADING_C)
792 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
793 return( ret );
794#endif
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
797 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000798 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200799 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
800 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000801 }
802
Paul Bakkerf451bac2013-08-30 15:37:02 +0200803 if( f_rng != NULL )
804 {
805 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200806 * Blinding
807 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200808 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200809 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
810 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000812
Janos Follathe81102e2017-03-22 13:38:28 +0000813 /*
814 * Exponent blinding
815 */
816 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
817 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
818
Janos Follathf9203b42017-03-22 15:13:15 +0000819#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000820 /*
821 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
822 */
823 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
824 f_rng, p_rng ) );
825 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
826 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
827 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
828
829 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000830#else
831 /*
832 * DP_blind = ( P - 1 ) * R + DP
833 */
834 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
835 f_rng, p_rng ) );
836 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
837 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
838 &ctx->DP ) );
839
840 DP = &DP_blind;
841
842 /*
843 * DQ_blind = ( Q - 1 ) * R + DQ
844 */
845 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
846 f_rng, p_rng ) );
847 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
848 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
849 &ctx->DQ ) );
850
851 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000852#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200853 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000856 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100857#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200858 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000859 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 *
861 * T1 = input ^ dP mod P
862 * T2 = input ^ dQ mod Q
863 */
Janos Follathf9203b42017-03-22 15:13:15 +0000864 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
865 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000866
867 /*
868 * T = (T1 - T2) * (Q^-1 mod P) mod P
869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
871 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
872 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000873
874 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200875 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
878 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
879#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200880
Paul Bakkerf451bac2013-08-30 15:37:02 +0200881 if( f_rng != NULL )
882 {
883 /*
884 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200885 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200886 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200887 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200889 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000890
891 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
894cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200896 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
897 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200898#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000901 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
902
903 if( f_rng != NULL )
904 {
Janos Follathe81102e2017-03-22 13:38:28 +0000905#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000906 mbedtls_mpi_free( &D_blind );
907#else
908 mbedtls_mpi_free( &DP_blind );
909 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000910#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000911 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
913 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
916 return( 0 );
917}
918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000920/**
921 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
922 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000923 * \param dst buffer to mask
924 * \param dlen length of destination buffer
925 * \param src source of the mask generation
926 * \param slen length of the source buffer
927 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000928 */
Paul Bakker48377d92013-08-30 12:06:24 +0200929static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000931{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000933 unsigned char counter[4];
934 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000935 unsigned int hlen;
936 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000939 memset( counter, 0, 4 );
940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000942
Simon Butcher02037452016-03-01 21:19:12 +0000943 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000944 p = dst;
945
946 while( dlen > 0 )
947 {
948 use_len = hlen;
949 if( dlen < hlen )
950 use_len = dlen;
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 mbedtls_md_starts( md_ctx );
953 mbedtls_md_update( md_ctx, src, slen );
954 mbedtls_md_update( md_ctx, counter, 4 );
955 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000956
957 for( i = 0; i < use_len; ++i )
958 *p++ ^= mask[i];
959
960 counter[3]++;
961
962 dlen -= use_len;
963 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200964
965 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000966}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100970/*
971 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
972 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100974 int (*f_rng)(void *, unsigned char *, size_t),
975 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100976 int mode,
977 const unsigned char *label, size_t label_len,
978 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100979 const unsigned char *input,
980 unsigned char *output )
981{
982 size_t olen;
983 int ret;
984 unsigned char *p = output;
985 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 const mbedtls_md_info_t *md_info;
987 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
990 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200991
992 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100996 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100998
999 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001001
Simon Butcher02037452016-03-01 21:19:12 +00001002 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001003 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001005
1006 memset( output, 0, olen );
1007
1008 *p++ = 0;
1009
Simon Butcher02037452016-03-01 21:19:12 +00001010 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001011 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001013
1014 p += hlen;
1015
Simon Butcher02037452016-03-01 21:19:12 +00001016 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001018 p += hlen;
1019 p += olen - 2 * hlen - 2 - ilen;
1020 *p++ = 1;
1021 memcpy( p, input, ilen );
1022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001024 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1025 {
1026 mbedtls_md_free( &md_ctx );
1027 return( ret );
1028 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001029
Simon Butcher02037452016-03-01 21:19:12 +00001030 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001031 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1032 &md_ctx );
1033
Simon Butcher02037452016-03-01 21:19:12 +00001034 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001035 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1036 &md_ctx );
1037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 return( ( mode == MBEDTLS_RSA_PUBLIC )
1041 ? mbedtls_rsa_public( ctx, output, output )
1042 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001043}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001047/*
1048 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001051 int (*f_rng)(void *, unsigned char *, size_t),
1052 void *p_rng,
1053 int mode, size_t ilen,
1054 const unsigned char *input,
1055 unsigned char *output )
1056{
1057 size_t nb_pad, olen;
1058 int ret;
1059 unsigned char *p = output;
1060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1062 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001063
Janos Follath1ed9f992016-03-18 11:45:44 +00001064 // We don't check p_rng because it won't be dereferenced here
1065 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001067
1068 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001069
Simon Butcher02037452016-03-01 21:19:12 +00001070 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001071 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001073
1074 nb_pad = olen - 3 - ilen;
1075
1076 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001080
1081 while( nb_pad-- > 0 )
1082 {
1083 int rng_dl = 100;
1084
1085 do {
1086 ret = f_rng( p_rng, p, 1 );
1087 } while( *p == 0 && --rng_dl && ret == 0 );
1088
Simon Butcher02037452016-03-01 21:19:12 +00001089 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001090 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001092
1093 p++;
1094 }
1095 }
1096 else
1097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001099
1100 while( nb_pad-- > 0 )
1101 *p++ = 0xFF;
1102 }
1103
1104 *p++ = 0;
1105 memcpy( p, input, ilen );
1106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 return( ( mode == MBEDTLS_RSA_PUBLIC )
1108 ? mbedtls_rsa_public( ctx, output, output )
1109 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001112
Paul Bakker5121ce52009-01-03 21:22:43 +00001113/*
1114 * Add the message padding, then do an RSA operation
1115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001117 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001118 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001119 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001120 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 unsigned char *output )
1122{
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 switch( ctx->padding )
1124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125#if defined(MBEDTLS_PKCS1_V15)
1126 case MBEDTLS_RSA_PKCS_V15:
1127 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001128 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001129#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#if defined(MBEDTLS_PKCS1_V21)
1132 case MBEDTLS_RSA_PKCS_V21:
1133 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001134 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001135#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
1137 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001140}
1141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001143/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001144 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001147 int (*f_rng)(void *, unsigned char *, size_t),
1148 void *p_rng,
1149 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001150 const unsigned char *label, size_t label_len,
1151 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001152 const unsigned char *input,
1153 unsigned char *output,
1154 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001155{
Paul Bakker23986e52011-04-24 08:57:21 +00001156 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001157 size_t ilen, i, pad_len;
1158 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1160 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001161 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 const mbedtls_md_info_t *md_info;
1163 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001164
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001165 /*
1166 * Parameters sanity checks
1167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1169 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001170
1171 ilen = ctx->len;
1172
Paul Bakker27fdf462011-06-09 13:55:13 +00001173 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001177 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001179
Janos Follathc17cda12016-02-11 11:08:18 +00001180 hlen = mbedtls_md_get_size( md_info );
1181
1182 // checking for integer underflow
1183 if( 2 * hlen + 2 > ilen )
1184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1185
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001186 /*
1187 * RSA operation
1188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1190 ? mbedtls_rsa_public( ctx, input, buf )
1191 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
1193 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001194 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001195
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001196 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001197 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001200 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1201 {
1202 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001203 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001204 }
1205
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001206
1207 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001209
1210 /* seed: Apply seedMask to maskedSeed */
1211 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1212 &md_ctx );
1213
1214 /* DB: Apply dbMask to maskedDB */
1215 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1216 &md_ctx );
1217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001219
1220 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001221 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001222 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001224 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001226 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001227
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001228 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001229
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001230 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001231 for( i = 0; i < hlen; i++ )
1232 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001233
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001234 /* Get zero-padding len, but always read till end of buffer
1235 * (minus one, for the 01 byte) */
1236 pad_len = 0;
1237 pad_done = 0;
1238 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1239 {
1240 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001241 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001242 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001243
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001244 p += pad_len;
1245 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001246
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001247 /*
1248 * The only information "leaked" is whether the padding was correct or not
1249 * (eg, no data is copied if it was not correct). This meets the
1250 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1251 * the different error conditions.
1252 */
1253 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001254 {
1255 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1256 goto cleanup;
1257 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001258
Paul Bakker66d5d072014-06-17 16:39:18 +02001259 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001260 {
1261 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1262 goto cleanup;
1263 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001264
1265 *olen = ilen - (p - buf);
1266 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001267 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001268
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001269cleanup:
1270 mbedtls_zeroize( buf, sizeof( buf ) );
1271 mbedtls_zeroize( lhash, sizeof( lhash ) );
1272
1273 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001274}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001278/*
1279 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001282 int (*f_rng)(void *, unsigned char *, size_t),
1283 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001284 int mode, size_t *olen,
1285 const unsigned char *input,
1286 unsigned char *output,
1287 size_t output_max_len)
1288{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001289 int ret;
1290 size_t ilen, pad_count = 0, i;
1291 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1295 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001296
1297 ilen = ctx->len;
1298
1299 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1303 ? mbedtls_rsa_public( ctx, input, buf )
1304 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001305
1306 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001307 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001308
1309 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001310 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001311
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001312 /*
1313 * Check and get padding len in "constant-time"
1314 */
1315 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001316
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001317 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001322 /* Get padding len, but always read till end of buffer
1323 * (minus one, for the 00 byte) */
1324 for( i = 0; i < ilen - 3; i++ )
1325 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001326 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1327 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001328 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001329
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001330 p += pad_count;
1331 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001332 }
1333 else
1334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001336
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001337 /* Get padding len, but always read till end of buffer
1338 * (minus one, for the 00 byte) */
1339 for( i = 0; i < ilen - 3; i++ )
1340 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001341 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001342 pad_count += ( pad_done == 0 );
1343 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001344
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001345 p += pad_count;
1346 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 }
1348
Janos Follathc69fa502016-02-12 13:30:09 +00001349 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001350
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001351 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001352 {
1353 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1354 goto cleanup;
1355 }
Paul Bakker8804f692013-02-28 18:06:26 +01001356
Paul Bakker66d5d072014-06-17 16:39:18 +02001357 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001358 {
1359 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1360 goto cleanup;
1361 }
Paul Bakker060c5682009-01-12 21:48:39 +00001362
Paul Bakker27fdf462011-06-09 13:55:13 +00001363 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001365 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001367cleanup:
1368 mbedtls_zeroize( buf, sizeof( buf ) );
1369
1370 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
1374/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001375 * Do an RSA operation, then remove the message padding
1376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001378 int (*f_rng)(void *, unsigned char *, size_t),
1379 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001380 int mode, size_t *olen,
1381 const unsigned char *input,
1382 unsigned char *output,
1383 size_t output_max_len)
1384{
1385 switch( ctx->padding )
1386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#if defined(MBEDTLS_PKCS1_V15)
1388 case MBEDTLS_RSA_PKCS_V15:
1389 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001390 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001391#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#if defined(MBEDTLS_PKCS1_V21)
1394 case MBEDTLS_RSA_PKCS_V21:
1395 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001396 olen, input, output,
1397 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001398#endif
1399
1400 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001402 }
1403}
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001406/*
1407 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001410 int (*f_rng)(void *, unsigned char *, size_t),
1411 void *p_rng,
1412 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001414 unsigned int hashlen,
1415 const unsigned char *hash,
1416 unsigned char *sig )
1417{
1418 size_t olen;
1419 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001421 unsigned int slen, hlen, offset = 0;
1422 int ret;
1423 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 const mbedtls_md_info_t *md_info;
1425 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1428 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001429
1430 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001432
1433 olen = ctx->len;
1434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001436 {
Simon Butcher02037452016-03-01 21:19:12 +00001437 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001439 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001443 }
1444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001446 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001450 slen = hlen;
1451
1452 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001454
1455 memset( sig, 0, olen );
1456
Simon Butcher02037452016-03-01 21:19:12 +00001457 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001458 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001460
Simon Butcher02037452016-03-01 21:19:12 +00001461 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001462 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001463 p += olen - hlen * 2 - 2;
1464 *p++ = 0x01;
1465 memcpy( p, salt, slen );
1466 p += slen;
1467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001469 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1470 {
1471 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001472 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001473 return( ret );
1474 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001475
Simon Butcher02037452016-03-01 21:19:12 +00001476 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mbedtls_md_starts( &md_ctx );
1478 mbedtls_md_update( &md_ctx, p, 8 );
1479 mbedtls_md_update( &md_ctx, hash, hashlen );
1480 mbedtls_md_update( &md_ctx, salt, slen );
1481 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001482 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001483
Simon Butcher02037452016-03-01 21:19:12 +00001484 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001485 if( msb % 8 == 0 )
1486 offset = 1;
1487
Simon Butcher02037452016-03-01 21:19:12 +00001488 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001489 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001492
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001493 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001494 sig[0] &= 0xFF >> ( olen * 8 - msb );
1495
1496 p += hlen;
1497 *p++ = 0xBC;
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 return( ( mode == MBEDTLS_RSA_PUBLIC )
1500 ? mbedtls_rsa_public( ctx, sig, sig )
1501 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001506/*
1507 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1508 */
1509/*
1510 * Do an RSA operation to sign the message digest
1511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001513 int (*f_rng)(void *, unsigned char *, size_t),
1514 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001515 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001517 unsigned int hashlen,
1518 const unsigned char *hash,
1519 unsigned char *sig )
1520{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001521 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001522 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001523 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001524 unsigned char *sig_try = NULL, *verif = NULL;
1525 size_t i;
1526 unsigned char diff;
1527 volatile unsigned char diff_no_optimize;
1528 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1531 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001532
1533 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001534 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001539 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1543 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001544
Paul Bakkerc70b9822013-04-07 22:00:46 +02001545 nb_pad -= 10 + oid_size;
1546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001548 }
1549
Paul Bakkerc70b9822013-04-07 22:00:46 +02001550 nb_pad -= hashlen;
1551
Paul Bakkerb3869132013-02-28 17:21:01 +01001552 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001554
1555 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001557 memset( p, 0xFF, nb_pad );
1558 p += nb_pad;
1559 *p++ = 0;
1560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001562 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001563 memcpy( p, hash, hashlen );
1564 }
1565 else
1566 {
1567 /*
1568 * DigestInfo ::= SEQUENCE {
1569 * digestAlgorithm DigestAlgorithmIdentifier,
1570 * digest Digest }
1571 *
1572 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1573 *
1574 * Digest ::= OCTET STRING
1575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001577 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
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) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001581 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001582 memcpy( p, oid, oid_size );
1583 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001585 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001587 *p++ = hashlen;
1588 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001589 }
1590
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001591 if( mode == MBEDTLS_RSA_PUBLIC )
1592 return( mbedtls_rsa_public( ctx, sig, sig ) );
1593
1594 /*
1595 * In order to prevent Lenstra's attack, make the signature in a
1596 * temporary buffer and check it before returning it.
1597 */
1598 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001599 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001600 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1601
Simon Butcher1285ab52016-01-01 21:42:47 +00001602 verif = mbedtls_calloc( 1, ctx->len );
1603 if( verif == NULL )
1604 {
1605 mbedtls_free( sig_try );
1606 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1607 }
1608
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001609 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1610 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1611
1612 /* Compare in constant time just in case */
1613 for( diff = 0, i = 0; i < ctx->len; i++ )
1614 diff |= verif[i] ^ sig[i];
1615 diff_no_optimize = diff;
1616
1617 if( diff_no_optimize != 0 )
1618 {
1619 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1620 goto cleanup;
1621 }
1622
1623 memcpy( sig, sig_try, ctx->len );
1624
1625cleanup:
1626 mbedtls_free( sig_try );
1627 mbedtls_free( verif );
1628
1629 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001630}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001632
1633/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001634 * Do an RSA operation to sign the message digest
1635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001637 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001638 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001641 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001642 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001643 unsigned char *sig )
1644{
Paul Bakker5121ce52009-01-03 21:22:43 +00001645 switch( ctx->padding )
1646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#if defined(MBEDTLS_PKCS1_V15)
1648 case MBEDTLS_RSA_PKCS_V15:
1649 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001650 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001651#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#if defined(MBEDTLS_PKCS1_V21)
1654 case MBEDTLS_RSA_PKCS_V21:
1655 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001656 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001657#endif
1658
Paul Bakker5121ce52009-01-03 21:22:43 +00001659 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001661 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001662}
1663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001665/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001666 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001669 int (*f_rng)(void *, unsigned char *, size_t),
1670 void *p_rng,
1671 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001673 unsigned int hashlen,
1674 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001676 int expected_salt_len,
1677 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001678{
Paul Bakker23986e52011-04-24 08:57:21 +00001679 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001680 size_t siglen;
1681 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001683 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001684 unsigned int hlen;
1685 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 const mbedtls_md_info_t *md_info;
1687 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001688 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1691 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001692
Paul Bakker5121ce52009-01-03 21:22:43 +00001693 siglen = ctx->len;
1694
Paul Bakker27fdf462011-06-09 13:55:13 +00001695 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1699 ? mbedtls_rsa_public( ctx, sig, buf )
1700 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001701
1702 if( ret != 0 )
1703 return( ret );
1704
1705 p = buf;
1706
Paul Bakkerb3869132013-02-28 17:21:01 +01001707 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001711 {
Simon Butcher02037452016-03-01 21:19:12 +00001712 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001714 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001718 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001721 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001725 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001726
Paul Bakkerb3869132013-02-28 17:21:01 +01001727 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001728
Simon Butcher02037452016-03-01 21:19:12 +00001729 /*
1730 * Note: EMSA-PSS verification is over the length of N - 1 bits
1731 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001732 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001733
Simon Butcher02037452016-03-01 21:19:12 +00001734 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001735 if( msb % 8 == 0 )
1736 {
1737 p++;
1738 siglen -= 1;
1739 }
1740 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001744 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1745 {
1746 mbedtls_md_free( &md_ctx );
1747 return( ret );
1748 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001749
Paul Bakkerb3869132013-02-28 17:21:01 +01001750 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001751
Paul Bakkerb3869132013-02-28 17:21:01 +01001752 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001753
Paul Bakker4de44aa2013-12-31 11:43:01 +01001754 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001755 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001756
Paul Bakkerb3869132013-02-28 17:21:01 +01001757 if( p == buf + siglen ||
1758 *p++ != 0x01 )
1759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 mbedtls_md_free( &md_ctx );
1761 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001762 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001763
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001764 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001765 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001768 slen != (size_t) expected_salt_len )
1769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 mbedtls_md_free( &md_ctx );
1771 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001772 }
1773
Simon Butcher02037452016-03-01 21:19:12 +00001774 /*
1775 * Generate H = Hash( M' )
1776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 mbedtls_md_starts( &md_ctx );
1778 mbedtls_md_update( &md_ctx, zeros, 8 );
1779 mbedtls_md_update( &md_ctx, hash, hashlen );
1780 mbedtls_md_update( &md_ctx, p, slen );
1781 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001784
Paul Bakkerb3869132013-02-28 17:21:01 +01001785 if( memcmp( p + slen, result, hlen ) == 0 )
1786 return( 0 );
1787 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001789}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001790
1791/*
1792 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001795 int (*f_rng)(void *, unsigned char *, size_t),
1796 void *p_rng,
1797 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001799 unsigned int hashlen,
1800 const unsigned char *hash,
1801 const unsigned char *sig )
1802{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1804 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001805 : md_alg;
1806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001808 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001810 sig ) );
1811
1812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001816/*
1817 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001820 int (*f_rng)(void *, unsigned char *, size_t),
1821 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001822 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001824 unsigned int hashlen,
1825 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001826 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001827{
1828 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001829 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001830 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 mbedtls_md_type_t msg_md_alg;
1832 const mbedtls_md_info_t *md_info;
1833 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001834 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1837 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001838
1839 siglen = ctx->len;
1840
1841 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1845 ? mbedtls_rsa_public( ctx, sig, buf )
1846 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001847
1848 if( ret != 0 )
1849 return( ret );
1850
1851 p = buf;
1852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1854 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001855
1856 while( *p != 0 )
1857 {
1858 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001860 p++;
1861 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02001862 p++; /* skip 00 byte */
1863
1864 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
1865 if( p - buf < 11 )
1866 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001867
1868 len = siglen - ( p - buf );
1869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001871 {
1872 if( memcmp( p, hash, hashlen ) == 0 )
1873 return( 0 );
1874 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 }
1877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001879 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1881 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001882
1883 end = p + len;
1884
Simon Butcher02037452016-03-01 21:19:12 +00001885 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02001886 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
1887 * Insist on 2-byte length tags, to protect against variants of
1888 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00001889 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001890 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1892 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1893 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001894 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001896
Gilles Peskinee7e76502017-05-04 12:48:39 +02001897 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1899 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1900 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001901 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001903
Gilles Peskinee7e76502017-05-04 12:48:39 +02001904 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1906 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001907 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001908 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001909
1910 oid.p = p;
1911 p += oid.len;
1912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1914 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001915
1916 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001918
1919 /*
1920 * assume the algorithm parameters must be NULL
1921 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02001922 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1924 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001925 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001927
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001928 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001929 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1930 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001931 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001933
1934 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001936
1937 p += hashlen;
1938
1939 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001941
1942 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001943}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
1946/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001947 * Do an RSA operation and check the message digest
1948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001950 int (*f_rng)(void *, unsigned char *, size_t),
1951 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001952 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001954 unsigned int hashlen,
1955 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001956 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001957{
1958 switch( ctx->padding )
1959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#if defined(MBEDTLS_PKCS1_V15)
1961 case MBEDTLS_RSA_PKCS_V15:
1962 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001963 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001964#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966#if defined(MBEDTLS_PKCS1_V21)
1967 case MBEDTLS_RSA_PKCS_V21:
1968 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001969 hashlen, hash, sig );
1970#endif
1971
1972 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001974 }
1975}
1976
1977/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001978 * Copy the components of an RSA key
1979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001981{
1982 int ret;
1983
1984 dst->ver = src->ver;
1985 dst->len = src->len;
1986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1988 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1991 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1992 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01001993
1994#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1996 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1997 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1999 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002000#endif
2001
2002 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2005 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002006
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002007 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002008 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002009
2010cleanup:
2011 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002013
2014 return( ret );
2015}
2016
2017/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 * Free the components of an RSA key
2019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002021{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002023 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2024 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002026
Hanno Becker33c30a02017-08-23 07:00:22 +01002027#if !defined(MBEDTLS_RSA_NO_CRT)
2028 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2029 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2030 mbedtls_mpi_free( &ctx->DP );
2031#endif /* MBEDTLS_RSA_NO_CRT */
2032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#if defined(MBEDTLS_THREADING_C)
2034 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002035#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002036}
2037
Hanno Beckerab377312017-08-23 16:24:51 +01002038#endif /* !MBEDTLS_RSA_ALT */
2039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002042#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
2044/*
2045 * Example RSA-1024 keypair, for test purposes
2046 */
2047#define KEY_LEN 128
2048
2049#define RSA_N "9292758453063D803DD603D5E777D788" \
2050 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2051 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2052 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2053 "93A89813FBF3C4F8066D2D800F7C38A8" \
2054 "1AE31942917403FF4946B0A83D3D3E05" \
2055 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2056 "5E94BB77B07507233A0BC7BAC8F90F79"
2057
2058#define RSA_E "10001"
2059
2060#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2061 "66CA472BC44D253102F8B4A9D3BFA750" \
2062 "91386C0077937FE33FA3252D28855837" \
2063 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2064 "DF79C5CE07EE72C7F123142198164234" \
2065 "CABB724CF78B8173B9F880FC86322407" \
2066 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2067 "071513A1E85B5DFA031F21ECAE91A34D"
2068
2069#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2070 "2C01CAD19EA484A87EA4377637E75500" \
2071 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2072 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2073
2074#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2075 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2076 "910E4168387E3C30AA1E00C339A79508" \
2077 "8452DD96A9A5EA5D9DCA68DA636032AF"
2078
Paul Bakker5121ce52009-01-03 21:22:43 +00002079#define PT_LEN 24
2080#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2081 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002084static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002085{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002086#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002087 size_t i;
2088
Paul Bakker545570e2010-07-18 09:00:25 +00002089 if( rng_state != NULL )
2090 rng_state = NULL;
2091
Paul Bakkera3d195c2011-11-27 21:07:34 +00002092 for( i = 0; i < len; ++i )
2093 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002094#else
2095 if( rng_state != NULL )
2096 rng_state = NULL;
2097
2098 arc4random_buf( output, len );
2099#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002100
Paul Bakkera3d195c2011-11-27 21:07:34 +00002101 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002104
Paul Bakker5121ce52009-01-03 21:22:43 +00002105/*
2106 * Checkup routine
2107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002109{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002110 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002112 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 unsigned char rsa_plaintext[PT_LEN];
2115 unsigned char rsa_decrypted[PT_LEN];
2116 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002118 unsigned char sha1sum[20];
2119#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002120
Hanno Becker3a701162017-08-22 13:52:43 +01002121 mbedtls_mpi K;
2122
2123 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002125
Hanno Becker3a701162017-08-22 13:52:43 +01002126 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2127 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2128 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2129 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2130 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2131 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2132 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2133 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2134 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2135 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2136
Hanno Becker7f25f852017-10-10 16:56:22 +01002137 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
2139 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2143 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 {
2145 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
2148 return( 1 );
2149 }
2150
2151 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002153
2154 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2155
Hanno Becker98838b02017-10-02 13:16:10 +01002156 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2157 PT_LEN, rsa_plaintext,
2158 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 {
2160 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002162
2163 return( 1 );
2164 }
2165
2166 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002168
Hanno Becker98838b02017-10-02 13:16:10 +01002169 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2170 &len, rsa_ciphertext, rsa_decrypted,
2171 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 {
2173 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002175
2176 return( 1 );
2177 }
2178
2179 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2180 {
2181 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183
2184 return( 1 );
2185 }
2186
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002187 if( verbose != 0 )
2188 mbedtls_printf( "passed\n" );
2189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002192 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002195
Hanno Becker98838b02017-10-02 13:16:10 +01002196 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2197 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2198 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 {
2200 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
2203 return( 1 );
2204 }
2205
2206 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002208
Hanno Becker98838b02017-10-02 13:16:10 +01002209 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2210 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2211 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 {
2213 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215
2216 return( 1 );
2217 }
2218
2219 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002220 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002222
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002223 if( verbose != 0 )
2224 mbedtls_printf( "\n" );
2225
Paul Bakker3d8fb632014-04-17 12:42:41 +02002226cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002227 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 mbedtls_rsa_free( &rsa );
2229#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002230 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002232 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002233}
2234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237#endif /* MBEDTLS_RSA_C */