blob: a93cdb1c4babe0665f7e2e54a856a316e21febf0 [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 */
21/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000022 * The following sources were referenced in the design of this implementation
23 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000024 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000025 * [1] A method for obtaining digital signatures and public-key cryptosystems
26 * R Rivest, A Shamir, and L Adleman
27 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
28 *
29 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
30 * Menezes, van Oorschot and Vanstone
31 *
Janos Follathe81102e2017-03-22 13:38:28 +000032 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
33 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
34 * Stefan Mangard
35 * https://arxiv.org/abs/1702.08719v2
36 *
Paul Bakker5121ce52009-01-03 21:22:43 +000037 */
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020041#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/rsa.h"
48#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#else
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020065#define mbedtls_calloc calloc
66#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#endif
68
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010069/* Implementation that should never be optimized out by the compiler */
70static void mbedtls_zeroize( void *v, size_t n ) {
71 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
72}
73
Hanno Becker171a8f12017-09-06 12:32:16 +010074/* constant-time buffer comparison */
75static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
76{
77 size_t i;
78 const unsigned char *A = (const unsigned char *) a;
79 const unsigned char *B = (const unsigned char *) b;
80 unsigned char diff = 0;
81
82 for( i = 0; i < n; i++ )
83 diff |= A[i] ^ B[i];
84
85 return( diff );
86}
87
Paul Bakker5121ce52009-01-03 21:22:43 +000088/*
89 * Initialize an RSA context
90 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000092 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000093 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if defined(MBEDTLS_THREADING_C)
100 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200101#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000102}
103
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100104/*
105 * Set padding for an existing RSA context
106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100108{
109 ctx->padding = padding;
110 ctx->hash_id = hash_id;
111}
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115/*
116 * Generate an RSA keypair
117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000119 int (*f_rng)(void *, unsigned char *, size_t),
120 void *p_rng,
121 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000122{
123 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Paul Bakker21eb2802010-08-16 11:10:02 +0000126 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Janos Follathef441782016-09-21 13:18:12 +0100129 if( nbits % 2 )
130 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
131
132 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath10c575b2016-02-23 14:42:48 +0000133 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135 /*
136 * find primes P and Q with Q < P so that:
137 * GCD( E, (P-1)*(Q-1) ) == 1
138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 do
142 {
Janos Follath10c575b2016-02-23 14:42:48 +0000143 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000144 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Janos Follathef441782016-09-21 13:18:12 +0100146 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000147 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 continue;
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200153 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 continue;
155
Janos Follathef441782016-09-21 13:18:12 +0100156 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
157 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
160 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
161 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
162 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166 /*
167 * D = E^-1 mod ((P-1)*(Q-1))
168 * DP = D mod (P - 1)
169 * DQ = D mod (Q - 1)
170 * QP = Q^-1 mod P
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
173 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
174 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
175 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200177 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179cleanup:
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
183 if( ret != 0 )
184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_rsa_free( ctx );
186 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 }
188
Paul Bakker48377d92013-08-30 12:06:24 +0200189 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190}
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194/*
195 * Check a public RSA key
196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000199 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000201
Paul Bakker48377d92013-08-30 12:06:24 +0200202 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200206 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
207 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200210 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
212 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
214 return( 0 );
215}
216
217/*
218 * Check a private RSA key
219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000221{
222 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 return( ret );
227
Paul Bakker37940d9f2009-07-10 22:38:58 +0000228 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
232 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
233 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
234 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
237 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
238 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
239 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
240 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
241 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
244 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
245 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
248 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
249 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000250 /*
251 * Check for a valid PKCS1v2 private key
252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
254 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
255 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
256 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
257 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
258 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
259 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 }
Paul Bakker48377d92013-08-30 12:06:24 +0200263
Paul Bakker5121ce52009-01-03 21:22:43 +0000264cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
266 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
267 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
268 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000271 return( ret );
272
Paul Bakker6c591fa2011-05-05 11:49:20 +0000273 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000275
276 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277}
278
279/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100280 * Check if contexts holding a public and private key match
281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100283{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
285 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100288 }
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
291 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100294 }
295
296 return( 0 );
297}
298
299/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 * Do an RSA public key operation
301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000303 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 unsigned char *output )
305{
Paul Bakker23986e52011-04-24 08:57:21 +0000306 int ret;
307 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200312#if defined(MBEDTLS_THREADING_C)
313 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
314 return( ret );
315#endif
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200321 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
322 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 }
324
325 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
327 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200331 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
332 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
340 return( 0 );
341}
342
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200343/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200344 * Generate or update blinding values, see section 10 of:
345 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200346 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200347 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200348 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200349static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200350 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
351{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200352 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200353
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200354 if( ctx->Vf.p != NULL )
355 {
356 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
358 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
359 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
360 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200361
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200362 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200363 }
364
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200365 /* Unblinding value: Vf = random number, invertible mod N */
366 do {
367 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
371 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
372 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200373
374 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
376 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 +0200377
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200378
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200379cleanup:
380 return( ret );
381}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200382
Paul Bakker5121ce52009-01-03 21:22:43 +0000383/*
Janos Follathe81102e2017-03-22 13:38:28 +0000384 * Exponent blinding supposed to prevent side-channel attacks using multiple
385 * traces of measurements to recover the RSA key. The more collisions are there,
386 * the more bits of the key can be recovered. See [3].
387 *
388 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
389 * observations on avarage.
390 *
391 * For example with 28 byte blinding to achieve 2 collisions the adversary has
392 * to make 2^112 observations on avarage.
393 *
394 * (With the currently (as of 2017 April) known best algorithms breaking 2048
395 * bit RSA requires approximately as much time as trying out 2^112 random keys.
396 * Thus in this sense with 28 byte blinding the security is not reduced by
397 * side-channel attacks like the one in [3])
398 *
399 * This countermeasure does not help if the key recovery is possible with a
400 * single trace.
401 */
402#define RSA_EXPONENT_BLINDING 28
403
404/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 * Do an RSA private key operation
406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200408 int (*f_rng)(void *, unsigned char *, size_t),
409 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000410 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 unsigned char *output )
412{
Paul Bakker23986e52011-04-24 08:57:21 +0000413 int ret;
414 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +0000416 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +0000417#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000418 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000419 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +0000420#else
421 mbedtls_mpi DP_blind, DQ_blind;
422 mbedtls_mpi *DP = &ctx->DP;
423 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +0000424#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100426 /* Make sure we have private key info, prevent possible misuse */
427 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
428 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000431 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
432
433
434 if( f_rng != NULL )
435 {
Janos Follathe81102e2017-03-22 13:38:28 +0000436#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000437 mbedtls_mpi_init( &D_blind );
438#else
439 mbedtls_mpi_init( &DP_blind );
440 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000441#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000442 }
Janos Follathe81102e2017-03-22 13:38:28 +0000443
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200445#if defined(MBEDTLS_THREADING_C)
446 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
447 return( ret );
448#endif
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
451 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200453 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
454 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 }
456
Paul Bakkerf451bac2013-08-30 15:37:02 +0200457 if( f_rng != NULL )
458 {
459 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200460 * Blinding
461 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200462 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200463 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
464 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000466
Janos Follathe81102e2017-03-22 13:38:28 +0000467 /*
468 * Exponent blinding
469 */
470 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
471 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
472
Janos Follathf9203b42017-03-22 15:13:15 +0000473#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000474 /*
475 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
476 */
477 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
478 f_rng, p_rng ) );
479 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
480 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
481 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
482
483 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000484#else
485 /*
486 * DP_blind = ( P - 1 ) * R + DP
487 */
488 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
489 f_rng, p_rng ) );
490 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
491 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
492 &ctx->DP ) );
493
494 DP = &DP_blind;
495
496 /*
497 * DQ_blind = ( Q - 1 ) * R + DQ
498 */
499 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
500 f_rng, p_rng ) );
501 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
502 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
503 &ctx->DQ ) );
504
505 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000506#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200507 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000510 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100511#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200512 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000513 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 *
515 * T1 = input ^ dP mod P
516 * T2 = input ^ dQ mod Q
517 */
Janos Follathf9203b42017-03-22 15:13:15 +0000518 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
519 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521 /*
522 * T = (T1 - T2) * (Q^-1 mod P) mod P
523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
525 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
526 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200529 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
532 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
533#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200534
Paul Bakkerf451bac2013-08-30 15:37:02 +0200535 if( f_rng != NULL )
536 {
537 /*
538 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200539 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200540 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200541 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200543 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
545 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200550 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
551 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200552#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000555 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
556
557 if( f_rng != NULL )
558 {
Janos Follathe81102e2017-03-22 13:38:28 +0000559#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000560 mbedtls_mpi_free( &D_blind );
561#else
562 mbedtls_mpi_free( &DP_blind );
563 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000564#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000565 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570 return( 0 );
571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000574/**
575 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
576 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000577 * \param dst buffer to mask
578 * \param dlen length of destination buffer
579 * \param src source of the mask generation
580 * \param slen length of the source buffer
581 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000582 */
Paul Bakker48377d92013-08-30 12:06:24 +0200583static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000587 unsigned char counter[4];
588 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000589 unsigned int hlen;
590 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000593 memset( counter, 0, 4 );
594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000596
Simon Butcher02037452016-03-01 21:19:12 +0000597 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000598 p = dst;
599
600 while( dlen > 0 )
601 {
602 use_len = hlen;
603 if( dlen < hlen )
604 use_len = dlen;
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_md_starts( md_ctx );
607 mbedtls_md_update( md_ctx, src, slen );
608 mbedtls_md_update( md_ctx, counter, 4 );
609 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000610
611 for( i = 0; i < use_len; ++i )
612 *p++ ^= mask[i];
613
614 counter[3]++;
615
616 dlen -= use_len;
617 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200618
619 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100624/*
625 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100628 int (*f_rng)(void *, unsigned char *, size_t),
629 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100630 int mode,
631 const unsigned char *label, size_t label_len,
632 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100633 const unsigned char *input,
634 unsigned char *output )
635{
636 size_t olen;
637 int ret;
638 unsigned char *p = output;
639 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 const mbedtls_md_info_t *md_info;
641 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
644 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200645
646 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100650 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100652
653 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100655
Simon Butcher02037452016-03-01 21:19:12 +0000656 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000657 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100659
660 memset( output, 0, olen );
661
662 *p++ = 0;
663
Simon Butcher02037452016-03-01 21:19:12 +0000664 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100665 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100667
668 p += hlen;
669
Simon Butcher02037452016-03-01 21:19:12 +0000670 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100672 p += hlen;
673 p += olen - 2 * hlen - 2 - ilen;
674 *p++ = 1;
675 memcpy( p, input, ilen );
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700678 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
679 {
680 mbedtls_md_free( &md_ctx );
681 return( ret );
682 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100683
Simon Butcher02037452016-03-01 21:19:12 +0000684 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100685 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
686 &md_ctx );
687
Simon Butcher02037452016-03-01 21:19:12 +0000688 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100689 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
690 &md_ctx );
691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 return( ( mode == MBEDTLS_RSA_PUBLIC )
695 ? mbedtls_rsa_public( ctx, output, output )
696 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100701/*
702 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100705 int (*f_rng)(void *, unsigned char *, size_t),
706 void *p_rng,
707 int mode, size_t ilen,
708 const unsigned char *input,
709 unsigned char *output )
710{
711 size_t nb_pad, olen;
712 int ret;
713 unsigned char *p = output;
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
716 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200717
Janos Follath1ed9f992016-03-18 11:45:44 +0000718 // We don't check p_rng because it won't be dereferenced here
719 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100721
722 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100723
Simon Butcher02037452016-03-01 21:19:12 +0000724 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000725 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100727
728 nb_pad = olen - 3 - ilen;
729
730 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100734
735 while( nb_pad-- > 0 )
736 {
737 int rng_dl = 100;
738
739 do {
740 ret = f_rng( p_rng, p, 1 );
741 } while( *p == 0 && --rng_dl && ret == 0 );
742
Simon Butcher02037452016-03-01 21:19:12 +0000743 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200744 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100746
747 p++;
748 }
749 }
750 else
751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100753
754 while( nb_pad-- > 0 )
755 *p++ = 0xFF;
756 }
757
758 *p++ = 0;
759 memcpy( p, input, ilen );
760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 return( ( mode == MBEDTLS_RSA_PUBLIC )
762 ? mbedtls_rsa_public( ctx, output, output )
763 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100764}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100766
Paul Bakker5121ce52009-01-03 21:22:43 +0000767/*
768 * Add the message padding, then do an RSA operation
769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000771 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000772 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000773 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000774 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 unsigned char *output )
776{
Paul Bakker5121ce52009-01-03 21:22:43 +0000777 switch( ctx->padding )
778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#if defined(MBEDTLS_PKCS1_V15)
780 case MBEDTLS_RSA_PKCS_V15:
781 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100782 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200783#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_PKCS1_V21)
786 case MBEDTLS_RSA_PKCS_V21:
787 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100788 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000789#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
791 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000793 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000794}
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000797/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100798 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200801 int (*f_rng)(void *, unsigned char *, size_t),
802 void *p_rng,
803 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100804 const unsigned char *label, size_t label_len,
805 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100806 const unsigned char *input,
807 unsigned char *output,
808 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000809{
Paul Bakker23986e52011-04-24 08:57:21 +0000810 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100811 size_t ilen, i, pad_len;
812 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
814 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000815 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 const mbedtls_md_info_t *md_info;
817 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100818
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100819 /*
820 * Parameters sanity checks
821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
823 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
825 ilen = ctx->len;
826
Paul Bakker27fdf462011-06-09 13:55:13 +0000827 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100831 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100833
Janos Follathc17cda12016-02-11 11:08:18 +0000834 hlen = mbedtls_md_get_size( md_info );
835
836 // checking for integer underflow
837 if( 2 * hlen + 2 > ilen )
838 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
839
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100840 /*
841 * RSA operation
842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 ret = ( mode == MBEDTLS_RSA_PUBLIC )
844 ? mbedtls_rsa_public( ctx, input, buf )
845 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
847 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100848 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000849
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100850 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100851 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700854 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
855 {
856 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100857 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700858 }
859
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100860
861 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100863
864 /* seed: Apply seedMask to maskedSeed */
865 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
866 &md_ctx );
867
868 /* DB: Apply dbMask to maskedDB */
869 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
870 &md_ctx );
871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100873
874 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100875 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100876 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000877 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100878 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000879
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100880 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100881
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100882 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100883
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100884 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100885 for( i = 0; i < hlen; i++ )
886 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100887
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100888 /* Get zero-padding len, but always read till end of buffer
889 * (minus one, for the 01 byte) */
890 pad_len = 0;
891 pad_done = 0;
892 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
893 {
894 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100895 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100896 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100897
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100898 p += pad_len;
899 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100900
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100901 /*
902 * The only information "leaked" is whether the padding was correct or not
903 * (eg, no data is copied if it was not correct). This meets the
904 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
905 * the different error conditions.
906 */
907 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100908 {
909 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
910 goto cleanup;
911 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100912
Paul Bakker66d5d072014-06-17 16:39:18 +0200913 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100914 {
915 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
916 goto cleanup;
917 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100918
919 *olen = ilen - (p - buf);
920 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100921 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100922
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100923cleanup:
924 mbedtls_zeroize( buf, sizeof( buf ) );
925 mbedtls_zeroize( lhash, sizeof( lhash ) );
926
927 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100932/*
933 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
934 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200936 int (*f_rng)(void *, unsigned char *, size_t),
937 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100938 int mode, size_t *olen,
939 const unsigned char *input,
940 unsigned char *output,
941 size_t output_max_len)
942{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100943 int ret;
944 size_t ilen, pad_count = 0, i;
945 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
949 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100950
951 ilen = ctx->len;
952
953 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 ret = ( mode == MBEDTLS_RSA_PUBLIC )
957 ? mbedtls_rsa_public( ctx, input, buf )
958 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100959
960 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100961 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +0100962
963 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100964 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100965
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100966 /*
967 * Check and get padding len in "constant-time"
968 */
969 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100970
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100971 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100976 /* Get padding len, but always read till end of buffer
977 * (minus one, for the 00 byte) */
978 for( i = 0; i < ilen - 3; i++ )
979 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100980 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
981 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100982 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000983
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100984 p += pad_count;
985 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100986 }
987 else
988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100990
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100991 /* Get padding len, but always read till end of buffer
992 * (minus one, for the 00 byte) */
993 for( i = 0; i < ilen - 3; i++ )
994 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100995 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100996 pad_count += ( pad_done == 0 );
997 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100998
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100999 p += pad_count;
1000 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001001 }
1002
Janos Follathc69fa502016-02-12 13:30:09 +00001003 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001004
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001005 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001006 {
1007 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1008 goto cleanup;
1009 }
Paul Bakker8804f692013-02-28 18:06:26 +01001010
Paul Bakker66d5d072014-06-17 16:39:18 +02001011 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001012 {
1013 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1014 goto cleanup;
1015 }
Paul Bakker060c5682009-01-12 21:48:39 +00001016
Paul Bakker27fdf462011-06-09 13:55:13 +00001017 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001019 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001021cleanup:
1022 mbedtls_zeroize( buf, sizeof( buf ) );
1023
1024 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001025}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
1028/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001029 * Do an RSA operation, then remove the message padding
1030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001032 int (*f_rng)(void *, unsigned char *, size_t),
1033 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001034 int mode, size_t *olen,
1035 const unsigned char *input,
1036 unsigned char *output,
1037 size_t output_max_len)
1038{
1039 switch( ctx->padding )
1040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041#if defined(MBEDTLS_PKCS1_V15)
1042 case MBEDTLS_RSA_PKCS_V15:
1043 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001044 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001045#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#if defined(MBEDTLS_PKCS1_V21)
1048 case MBEDTLS_RSA_PKCS_V21:
1049 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001050 olen, input, output,
1051 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001052#endif
1053
1054 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001056 }
1057}
1058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001060/*
1061 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001064 int (*f_rng)(void *, unsigned char *, size_t),
1065 void *p_rng,
1066 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001068 unsigned int hashlen,
1069 const unsigned char *hash,
1070 unsigned char *sig )
1071{
1072 size_t olen;
1073 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001075 unsigned int slen, hlen, offset = 0;
1076 int ret;
1077 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 const mbedtls_md_info_t *md_info;
1079 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1082 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001083
1084 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001086
1087 olen = ctx->len;
1088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001090 {
Simon Butcher02037452016-03-01 21:19:12 +00001091 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001093 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001097 }
1098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001100 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001104 slen = hlen;
1105
1106 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001108
1109 memset( sig, 0, olen );
1110
Simon Butcher02037452016-03-01 21:19:12 +00001111 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001112 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001114
Simon Butcher02037452016-03-01 21:19:12 +00001115 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001116 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001117 p += olen - hlen * 2 - 2;
1118 *p++ = 0x01;
1119 memcpy( p, salt, slen );
1120 p += slen;
1121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001123 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1124 {
1125 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001126 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001127 return( ret );
1128 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001129
Simon Butcher02037452016-03-01 21:19:12 +00001130 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 mbedtls_md_starts( &md_ctx );
1132 mbedtls_md_update( &md_ctx, p, 8 );
1133 mbedtls_md_update( &md_ctx, hash, hashlen );
1134 mbedtls_md_update( &md_ctx, salt, slen );
1135 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001136 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001137
Simon Butcher02037452016-03-01 21:19:12 +00001138 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001139 if( msb % 8 == 0 )
1140 offset = 1;
1141
Simon Butcher02037452016-03-01 21:19:12 +00001142 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001143 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001146
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001147 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001148 sig[0] &= 0xFF >> ( olen * 8 - msb );
1149
1150 p += hlen;
1151 *p++ = 0xBC;
1152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 return( ( mode == MBEDTLS_RSA_PUBLIC )
1154 ? mbedtls_rsa_public( ctx, sig, sig )
1155 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001160/*
1161 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1162 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001163
1164/* Construct a PKCS v1.5 encoding of a hashed message
1165 *
1166 * This is used both for signature generation and verification.
1167 *
1168 * Parameters:
1169 * - md_alg: Identifies the hash algorithm used to generate the given hash;
1170 * MBEDTLS_MD_NONE if raw data are signed.
1171 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
1172 * - hash: Buffer containing the hashed message.
1173 * - sig_len: Length of the encoded message.
1174 * - dst: Buffer to hold the encoded message.
1175 *
1176 * Assumptions:
1177 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1178 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
1179 * - dst points to a buffer of size at least sig_len.
1180 *
1181 */
1182static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1183 unsigned int hashlen,
1184 const unsigned char *hash,
1185 size_t sig_len,
1186 unsigned char *dst )
1187{
1188 size_t oid_size = 0;
1189 size_t nb_pad = sig_len;
1190 unsigned char *p = dst;
1191 const char *oid = NULL;
1192
1193 /* Are we signing hashed or raw data? */
1194 if( md_alg != MBEDTLS_MD_NONE )
1195 {
1196 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1197 if( md_info == NULL )
1198 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1199
1200 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1201 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1202
1203 hashlen = mbedtls_md_get_size( md_info );
1204
1205 /* Double-check that 8 + hashlen + oid_size can be used as a
1206 * 1-byte ASN.1 length encoding and that there's no overflow. */
1207 if( 8 + hashlen + oid_size >= 0x80 ||
1208 10 + hashlen < hashlen ||
1209 10 + hashlen + oid_size < 10 + hashlen )
1210 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1211
1212 /*
1213 * Static bounds check:
1214 * - Need 10 bytes for five tag-length pairs.
1215 * (Insist on 1-byte length encodings to protect against variants of
1216 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1217 * - Need hashlen bytes for hash
1218 * - Need oid_size bytes for hash alg OID.
1219 */
1220 if( nb_pad < 10 + hashlen + oid_size )
1221 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1222 nb_pad -= 10 + hashlen + oid_size;
1223 }
1224 else
1225 {
1226 if( nb_pad < hashlen )
1227 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1228
1229 nb_pad -= hashlen;
1230 }
1231
1232 /* Signature header and padding delimiter */
1233 if( nb_pad < 3 )
1234 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1235 nb_pad -= 3;
1236
1237 /* Now nb_pad is the amount of memory to be filled
1238 * with padding; must be at least 8 bytes. */
1239 if( nb_pad < 8 )
1240 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1241
1242 /* Write signature header and padding */
1243 *p++ = 0;
1244 *p++ = MBEDTLS_RSA_SIGN;
1245 memset( p, 0xFF, nb_pad );
1246 p += nb_pad;
1247 *p++ = 0;
1248
1249 /* Are we signing raw data? */
1250 if( md_alg == MBEDTLS_MD_NONE )
1251 {
1252 memcpy( p, hash, hashlen );
1253 return( 0 );
1254 }
1255
1256 /* Signing hashed data, add corresponding ASN.1 structure
1257 *
1258 * DigestInfo ::= SEQUENCE {
1259 * digestAlgorithm DigestAlgorithmIdentifier,
1260 * digest Digest }
1261 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1262 * Digest ::= OCTET STRING
1263 *
1264 * Schematic:
1265 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1266 * TAG-NULL + LEN [ NULL ] ]
1267 * TAG-OCTET + LEN [ HASH ] ]
1268 */
1269 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
1270 *p++ = 0x08 + oid_size + hashlen;
1271 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
1272 *p++ = 0x04 + oid_size;
1273 *p++ = MBEDTLS_ASN1_OID;
1274 *p++ = oid_size;
1275 memcpy( p, oid, oid_size );
1276 p += oid_size;
1277 *p++ = MBEDTLS_ASN1_NULL;
1278 *p++ = 0x00;
1279 *p++ = MBEDTLS_ASN1_OCTET_STRING;
1280 *p++ = hashlen;
1281 memcpy( p, hash, hashlen );
1282 p += hashlen;
1283
1284 /* Just a sanity-check, should be automatic
1285 * after the initial bounds check. */
1286 if( p != dst + sig_len )
1287 {
1288 mbedtls_zeroize( dst, sig_len );
1289 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1290 }
1291
1292 return( 0 );
1293}
1294
Paul Bakkerb3869132013-02-28 17:21:01 +01001295/*
1296 * Do an RSA operation to sign the message digest
1297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001299 int (*f_rng)(void *, unsigned char *, size_t),
1300 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001301 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001303 unsigned int hashlen,
1304 const unsigned char *hash,
1305 unsigned char *sig )
1306{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001307 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001308 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1311 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001312
Hanno Beckerfdf38032017-09-06 12:35:55 +01001313 /*
1314 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1315 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001316
Hanno Beckerfdf38032017-09-06 12:35:55 +01001317 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1318 ctx->len, sig ) ) != 0 )
1319 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001320
1321 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001322 * Call respective RSA primitive
1323 */
1324
1325 if( mode == MBEDTLS_RSA_PUBLIC )
1326 {
1327 /* Skip verification on a public key operation */
1328 return( mbedtls_rsa_public( ctx, sig, sig ) );
1329 }
1330
1331 /* Private key operation
1332 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001333 * In order to prevent Lenstra's attack, make the signature in a
1334 * temporary buffer and check it before returning it.
1335 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001336
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001337 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001338 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001339 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1340
Hanno Beckerfdf38032017-09-06 12:35:55 +01001341 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001342 if( verif == NULL )
1343 {
1344 mbedtls_free( sig_try );
1345 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1346 }
1347
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001348 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1349 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1350
Hanno Becker171a8f12017-09-06 12:32:16 +01001351 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001352 {
1353 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1354 goto cleanup;
1355 }
1356
1357 memcpy( sig, sig_try, ctx->len );
1358
1359cleanup:
1360 mbedtls_free( sig_try );
1361 mbedtls_free( verif );
1362
1363 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001366
1367/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 * Do an RSA operation to sign the message digest
1369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001371 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001372 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001375 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001376 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 unsigned char *sig )
1378{
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 switch( ctx->padding )
1380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#if defined(MBEDTLS_PKCS1_V15)
1382 case MBEDTLS_RSA_PKCS_V15:
1383 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001384 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001385#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#if defined(MBEDTLS_PKCS1_V21)
1388 case MBEDTLS_RSA_PKCS_V21:
1389 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001390 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001391#endif
1392
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001396}
1397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001399/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001400 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001403 int (*f_rng)(void *, unsigned char *, size_t),
1404 void *p_rng,
1405 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001407 unsigned int hashlen,
1408 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001410 int expected_salt_len,
1411 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001412{
Paul Bakker23986e52011-04-24 08:57:21 +00001413 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001414 size_t siglen;
1415 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001417 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001418 unsigned int hlen;
1419 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 const mbedtls_md_info_t *md_info;
1421 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001422 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1425 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001426
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 siglen = ctx->len;
1428
Paul Bakker27fdf462011-06-09 13:55:13 +00001429 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1433 ? mbedtls_rsa_public( ctx, sig, buf )
1434 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
1436 if( ret != 0 )
1437 return( ret );
1438
1439 p = buf;
1440
Paul Bakkerb3869132013-02-28 17:21:01 +01001441 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 {
Simon Butcher02037452016-03-01 21:19:12 +00001446 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001448 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001452 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001455 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001459 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001460
Paul Bakkerb3869132013-02-28 17:21:01 +01001461 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001462
Simon Butcher02037452016-03-01 21:19:12 +00001463 /*
1464 * Note: EMSA-PSS verification is over the length of N - 1 bits
1465 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001466 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001467
Simon Butcher02037452016-03-01 21:19:12 +00001468 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001469 if( msb % 8 == 0 )
1470 {
1471 p++;
1472 siglen -= 1;
1473 }
1474 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001478 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1479 {
1480 mbedtls_md_free( &md_ctx );
1481 return( ret );
1482 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001483
Paul Bakkerb3869132013-02-28 17:21:01 +01001484 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001485
Paul Bakkerb3869132013-02-28 17:21:01 +01001486 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001487
Paul Bakker4de44aa2013-12-31 11:43:01 +01001488 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001489 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001490
Paul Bakkerb3869132013-02-28 17:21:01 +01001491 if( p == buf + siglen ||
1492 *p++ != 0x01 )
1493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 mbedtls_md_free( &md_ctx );
1495 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001496 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001497
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001498 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001499 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001502 slen != (size_t) expected_salt_len )
1503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 mbedtls_md_free( &md_ctx );
1505 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001506 }
1507
Simon Butcher02037452016-03-01 21:19:12 +00001508 /*
1509 * Generate H = Hash( M' )
1510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 mbedtls_md_starts( &md_ctx );
1512 mbedtls_md_update( &md_ctx, zeros, 8 );
1513 mbedtls_md_update( &md_ctx, hash, hashlen );
1514 mbedtls_md_update( &md_ctx, p, slen );
1515 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001518
Paul Bakkerb3869132013-02-28 17:21:01 +01001519 if( memcmp( p + slen, result, hlen ) == 0 )
1520 return( 0 );
1521 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001523}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001524
1525/*
1526 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001529 int (*f_rng)(void *, unsigned char *, size_t),
1530 void *p_rng,
1531 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001533 unsigned int hashlen,
1534 const unsigned char *hash,
1535 const unsigned char *sig )
1536{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1538 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001539 : md_alg;
1540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001542 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001544 sig ) );
1545
1546}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001550/*
1551 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001554 int (*f_rng)(void *, unsigned char *, size_t),
1555 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001556 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001558 unsigned int hashlen,
1559 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001560 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001561{
1562 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001563 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001564 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 mbedtls_md_type_t msg_md_alg;
1566 const mbedtls_md_info_t *md_info;
1567 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001568 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1571 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001572
1573 siglen = ctx->len;
1574
1575 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1579 ? mbedtls_rsa_public( ctx, sig, buf )
1580 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001581
1582 if( ret != 0 )
1583 return( ret );
1584
1585 p = buf;
1586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1588 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001589
1590 while( *p != 0 )
1591 {
1592 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001594 p++;
1595 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02001596 p++; /* skip 00 byte */
1597
1598 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
1599 if( p - buf < 11 )
1600 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001601
1602 len = siglen - ( p - buf );
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001605 {
1606 if( memcmp( p, hash, hashlen ) == 0 )
1607 return( 0 );
1608 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610 }
1611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001613 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1615 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001616
1617 end = p + len;
1618
Simon Butcher02037452016-03-01 21:19:12 +00001619 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02001620 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
1621 * Insist on 2-byte length tags, to protect against variants of
1622 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00001623 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001624 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1626 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1627 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001628 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001630
Gilles Peskinee7e76502017-05-04 12:48:39 +02001631 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1633 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1634 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001635 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001637
Gilles Peskinee7e76502017-05-04 12:48:39 +02001638 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1640 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001641 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001642 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001643
1644 oid.p = p;
1645 p += oid.len;
1646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1648 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001649
1650 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001652
1653 /*
1654 * assume the algorithm parameters must be NULL
1655 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02001656 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1658 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001659 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001661
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001662 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001663 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1664 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001665 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001667
1668 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001670
1671 p += hashlen;
1672
1673 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001675
1676 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001679
1680/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001681 * Do an RSA operation and check the message digest
1682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001684 int (*f_rng)(void *, unsigned char *, size_t),
1685 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001686 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001688 unsigned int hashlen,
1689 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001690 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001691{
1692 switch( ctx->padding )
1693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#if defined(MBEDTLS_PKCS1_V15)
1695 case MBEDTLS_RSA_PKCS_V15:
1696 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001697 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001698#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#if defined(MBEDTLS_PKCS1_V21)
1701 case MBEDTLS_RSA_PKCS_V21:
1702 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001703 hashlen, hash, sig );
1704#endif
1705
1706 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001708 }
1709}
1710
1711/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001712 * Copy the components of an RSA key
1713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001715{
1716 int ret;
1717
1718 dst->ver = src->ver;
1719 dst->len = src->len;
1720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1722 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1725 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1726 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1727 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1728 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1729 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1732 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1733 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1736 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001737
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001738 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001739 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001740
1741cleanup:
1742 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001744
1745 return( ret );
1746}
1747
1748/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001749 * Free the components of an RSA key
1750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001752{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1754 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1755 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1756 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1757 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#if defined(MBEDTLS_THREADING_C)
1760 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001761#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001762}
1763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001766#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
1768/*
1769 * Example RSA-1024 keypair, for test purposes
1770 */
1771#define KEY_LEN 128
1772
1773#define RSA_N "9292758453063D803DD603D5E777D788" \
1774 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1775 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1776 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1777 "93A89813FBF3C4F8066D2D800F7C38A8" \
1778 "1AE31942917403FF4946B0A83D3D3E05" \
1779 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1780 "5E94BB77B07507233A0BC7BAC8F90F79"
1781
1782#define RSA_E "10001"
1783
1784#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1785 "66CA472BC44D253102F8B4A9D3BFA750" \
1786 "91386C0077937FE33FA3252D28855837" \
1787 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1788 "DF79C5CE07EE72C7F123142198164234" \
1789 "CABB724CF78B8173B9F880FC86322407" \
1790 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1791 "071513A1E85B5DFA031F21ECAE91A34D"
1792
1793#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1794 "2C01CAD19EA484A87EA4377637E75500" \
1795 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1796 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1797
1798#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1799 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1800 "910E4168387E3C30AA1E00C339A79508" \
1801 "8452DD96A9A5EA5D9DCA68DA636032AF"
1802
1803#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1804 "3C94D22288ACD763FD8E5600ED4A702D" \
1805 "F84198A5F06C2E72236AE490C93F07F8" \
1806 "3CC559CD27BC2D1CA488811730BB5725"
1807
1808#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1809 "D8AAEA56749EA28623272E4F7D0592AF" \
1810 "7C1F1313CAC9471B5C523BFE592F517B" \
1811 "407A1BD76C164B93DA2D32A383E58357"
1812
1813#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1814 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1815 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1816 "A74206CEC169D74BF5A8C50D6F48EA08"
1817
1818#define PT_LEN 24
1819#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1820 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001823static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001824{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001825#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001826 size_t i;
1827
Paul Bakker545570e2010-07-18 09:00:25 +00001828 if( rng_state != NULL )
1829 rng_state = NULL;
1830
Paul Bakkera3d195c2011-11-27 21:07:34 +00001831 for( i = 0; i < len; ++i )
1832 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001833#else
1834 if( rng_state != NULL )
1835 rng_state = NULL;
1836
1837 arc4random_buf( output, len );
1838#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001839
Paul Bakkera3d195c2011-11-27 21:07:34 +00001840 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001841}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001843
Paul Bakker5121ce52009-01-03 21:22:43 +00001844/*
1845 * Checkup routine
1846 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001848{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001849 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001851 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001853 unsigned char rsa_plaintext[PT_LEN];
1854 unsigned char rsa_decrypted[PT_LEN];
1855 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001857 unsigned char sha1sum[20];
1858#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001861
1862 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1864 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1865 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1866 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1867 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1868 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1869 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1870 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001871
1872 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1876 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 {
1878 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001880
1881 return( 1 );
1882 }
1883
1884 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
1887 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 rsa_plaintext, rsa_ciphertext ) != 0 )
1891 {
1892 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001894
1895 return( 1 );
1896 }
1897
1898 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001902 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001903 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 {
1905 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001907
1908 return( 1 );
1909 }
1910
1911 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1912 {
1913 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
1916 return( 1 );
1917 }
1918
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001919 if( verbose != 0 )
1920 mbedtls_printf( "passed\n" );
1921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001924 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 sha1sum, rsa_ciphertext ) != 0 )
1930 {
1931 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
1934 return( 1 );
1935 }
1936
1937 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 sha1sum, rsa_ciphertext ) != 0 )
1942 {
1943 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
1946 return( 1 );
1947 }
1948
1949 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001950 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001952
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001953 if( verbose != 0 )
1954 mbedtls_printf( "\n" );
1955
Paul Bakker3d8fb632014-04-17 12:42:41 +02001956cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 mbedtls_rsa_free( &rsa );
1958#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001959 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001961 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001962}
1963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966#endif /* MBEDTLS_RSA_C */