blob: 3cc90c0bec00f3a01492019011e55996965ebae0 [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 */
1163/*
1164 * Do an RSA operation to sign the message digest
1165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001167 int (*f_rng)(void *, unsigned char *, size_t),
1168 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001169 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001171 unsigned int hashlen,
1172 const unsigned char *hash,
1173 unsigned char *sig )
1174{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001175 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001176 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001177 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001178 unsigned char *sig_try = NULL, *verif = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001179 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1182 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001183
1184 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001185 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001190 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1194 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001195
Paul Bakkerc70b9822013-04-07 22:00:46 +02001196 nb_pad -= 10 + oid_size;
1197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001199 }
1200
Paul Bakkerc70b9822013-04-07 22:00:46 +02001201 nb_pad -= hashlen;
1202
Paul Bakkerb3869132013-02-28 17:21:01 +01001203 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001205
1206 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001208 memset( p, 0xFF, nb_pad );
1209 p += nb_pad;
1210 *p++ = 0;
1211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001213 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001214 memcpy( p, hash, hashlen );
1215 }
1216 else
1217 {
1218 /*
1219 * DigestInfo ::= SEQUENCE {
1220 * digestAlgorithm DigestAlgorithmIdentifier,
1221 * digest Digest }
1222 *
1223 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1224 *
1225 * Digest ::= OCTET STRING
1226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001228 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001230 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001232 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001233 memcpy( p, oid, oid_size );
1234 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001236 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001238 *p++ = hashlen;
1239 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001240 }
1241
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001242 if( mode == MBEDTLS_RSA_PUBLIC )
1243 return( mbedtls_rsa_public( ctx, sig, sig ) );
1244
1245 /*
1246 * In order to prevent Lenstra's attack, make the signature in a
1247 * temporary buffer and check it before returning it.
1248 */
1249 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001250 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001251 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1252
Simon Butcher1285ab52016-01-01 21:42:47 +00001253 verif = mbedtls_calloc( 1, ctx->len );
1254 if( verif == NULL )
1255 {
1256 mbedtls_free( sig_try );
1257 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1258 }
1259
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001260 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1261 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1262
Hanno Becker171a8f12017-09-06 12:32:16 +01001263 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001264 {
1265 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1266 goto cleanup;
1267 }
1268
1269 memcpy( sig, sig_try, ctx->len );
1270
1271cleanup:
1272 mbedtls_free( sig_try );
1273 mbedtls_free( verif );
1274
1275 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001276}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001278
1279/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 * Do an RSA operation to sign the message digest
1281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001283 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001284 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001287 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001288 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 unsigned char *sig )
1290{
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 switch( ctx->padding )
1292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#if defined(MBEDTLS_PKCS1_V15)
1294 case MBEDTLS_RSA_PKCS_V15:
1295 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001296 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001297#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#if defined(MBEDTLS_PKCS1_V21)
1300 case MBEDTLS_RSA_PKCS_V21:
1301 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001302 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001303#endif
1304
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001308}
1309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001311/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001312 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001315 int (*f_rng)(void *, unsigned char *, size_t),
1316 void *p_rng,
1317 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001319 unsigned int hashlen,
1320 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001322 int expected_salt_len,
1323 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001324{
Paul Bakker23986e52011-04-24 08:57:21 +00001325 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001326 size_t siglen;
1327 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001329 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001330 unsigned int hlen;
1331 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 const mbedtls_md_info_t *md_info;
1333 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001334 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1337 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001338
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 siglen = ctx->len;
1340
Paul Bakker27fdf462011-06-09 13:55:13 +00001341 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1345 ? mbedtls_rsa_public( ctx, sig, buf )
1346 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001347
1348 if( ret != 0 )
1349 return( ret );
1350
1351 p = buf;
1352
Paul Bakkerb3869132013-02-28 17:21:01 +01001353 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 {
Simon Butcher02037452016-03-01 21:19:12 +00001358 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001360 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001364 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001367 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001371 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001372
Paul Bakkerb3869132013-02-28 17:21:01 +01001373 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001374
Simon Butcher02037452016-03-01 21:19:12 +00001375 /*
1376 * Note: EMSA-PSS verification is over the length of N - 1 bits
1377 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001378 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001379
Simon Butcher02037452016-03-01 21:19:12 +00001380 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001381 if( msb % 8 == 0 )
1382 {
1383 p++;
1384 siglen -= 1;
1385 }
1386 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001390 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1391 {
1392 mbedtls_md_free( &md_ctx );
1393 return( ret );
1394 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001395
Paul Bakkerb3869132013-02-28 17:21:01 +01001396 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001397
Paul Bakkerb3869132013-02-28 17:21:01 +01001398 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001399
Paul Bakker4de44aa2013-12-31 11:43:01 +01001400 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001401 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001402
Paul Bakkerb3869132013-02-28 17:21:01 +01001403 if( p == buf + siglen ||
1404 *p++ != 0x01 )
1405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 mbedtls_md_free( &md_ctx );
1407 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001408 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001409
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001410 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001411 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001414 slen != (size_t) expected_salt_len )
1415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 mbedtls_md_free( &md_ctx );
1417 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001418 }
1419
Simon Butcher02037452016-03-01 21:19:12 +00001420 /*
1421 * Generate H = Hash( M' )
1422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 mbedtls_md_starts( &md_ctx );
1424 mbedtls_md_update( &md_ctx, zeros, 8 );
1425 mbedtls_md_update( &md_ctx, hash, hashlen );
1426 mbedtls_md_update( &md_ctx, p, slen );
1427 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001430
Paul Bakkerb3869132013-02-28 17:21:01 +01001431 if( memcmp( p + slen, result, hlen ) == 0 )
1432 return( 0 );
1433 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001435}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001436
1437/*
1438 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001441 int (*f_rng)(void *, unsigned char *, size_t),
1442 void *p_rng,
1443 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001445 unsigned int hashlen,
1446 const unsigned char *hash,
1447 const unsigned char *sig )
1448{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1450 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001451 : md_alg;
1452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001454 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001456 sig ) );
1457
1458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001462/*
1463 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001466 int (*f_rng)(void *, unsigned char *, size_t),
1467 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001468 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001470 unsigned int hashlen,
1471 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001472 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001473{
1474 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001475 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001476 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mbedtls_md_type_t msg_md_alg;
1478 const mbedtls_md_info_t *md_info;
1479 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001480 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1483 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001484
1485 siglen = ctx->len;
1486
1487 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1491 ? mbedtls_rsa_public( ctx, sig, buf )
1492 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001493
1494 if( ret != 0 )
1495 return( ret );
1496
1497 p = buf;
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1500 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001501
1502 while( *p != 0 )
1503 {
1504 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001506 p++;
1507 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02001508 p++; /* skip 00 byte */
1509
1510 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
1511 if( p - buf < 11 )
1512 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001513
1514 len = siglen - ( p - buf );
1515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001517 {
1518 if( memcmp( p, hash, hashlen ) == 0 )
1519 return( 0 );
1520 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 }
1523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001525 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1527 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001528
1529 end = p + len;
1530
Simon Butcher02037452016-03-01 21:19:12 +00001531 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02001532 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
1533 * Insist on 2-byte length tags, to protect against variants of
1534 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00001535 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001536 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1538 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1539 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001540 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001542
Gilles Peskinee7e76502017-05-04 12:48:39 +02001543 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1545 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1546 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001547 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001549
Gilles Peskinee7e76502017-05-04 12:48:39 +02001550 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1552 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001553 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001554 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001555
1556 oid.p = p;
1557 p += oid.len;
1558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1560 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001561
1562 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001564
1565 /*
1566 * assume the algorithm parameters must be NULL
1567 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02001568 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1570 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001571 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001573
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001574 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001575 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1576 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001577 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001579
1580 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001582
1583 p += hashlen;
1584
1585 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001587
1588 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001591
1592/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001593 * Do an RSA operation and check the message digest
1594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001596 int (*f_rng)(void *, unsigned char *, size_t),
1597 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001598 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001600 unsigned int hashlen,
1601 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001602 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001603{
1604 switch( ctx->padding )
1605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_PKCS1_V15)
1607 case MBEDTLS_RSA_PKCS_V15:
1608 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001609 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001610#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#if defined(MBEDTLS_PKCS1_V21)
1613 case MBEDTLS_RSA_PKCS_V21:
1614 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001615 hashlen, hash, sig );
1616#endif
1617
1618 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001620 }
1621}
1622
1623/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001624 * Copy the components of an RSA key
1625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001627{
1628 int ret;
1629
1630 dst->ver = src->ver;
1631 dst->len = src->len;
1632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1634 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1637 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1638 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1639 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1640 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1641 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1644 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1645 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1648 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001649
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001650 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001651 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001652
1653cleanup:
1654 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001656
1657 return( ret );
1658}
1659
1660/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001661 * Free the components of an RSA key
1662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001664{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1666 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1667 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1668 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1669 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671#if defined(MBEDTLS_THREADING_C)
1672 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001673#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001674}
1675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001678#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001679
1680/*
1681 * Example RSA-1024 keypair, for test purposes
1682 */
1683#define KEY_LEN 128
1684
1685#define RSA_N "9292758453063D803DD603D5E777D788" \
1686 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1687 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1688 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1689 "93A89813FBF3C4F8066D2D800F7C38A8" \
1690 "1AE31942917403FF4946B0A83D3D3E05" \
1691 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1692 "5E94BB77B07507233A0BC7BAC8F90F79"
1693
1694#define RSA_E "10001"
1695
1696#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1697 "66CA472BC44D253102F8B4A9D3BFA750" \
1698 "91386C0077937FE33FA3252D28855837" \
1699 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1700 "DF79C5CE07EE72C7F123142198164234" \
1701 "CABB724CF78B8173B9F880FC86322407" \
1702 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1703 "071513A1E85B5DFA031F21ECAE91A34D"
1704
1705#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1706 "2C01CAD19EA484A87EA4377637E75500" \
1707 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1708 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1709
1710#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1711 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1712 "910E4168387E3C30AA1E00C339A79508" \
1713 "8452DD96A9A5EA5D9DCA68DA636032AF"
1714
1715#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1716 "3C94D22288ACD763FD8E5600ED4A702D" \
1717 "F84198A5F06C2E72236AE490C93F07F8" \
1718 "3CC559CD27BC2D1CA488811730BB5725"
1719
1720#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1721 "D8AAEA56749EA28623272E4F7D0592AF" \
1722 "7C1F1313CAC9471B5C523BFE592F517B" \
1723 "407A1BD76C164B93DA2D32A383E58357"
1724
1725#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1726 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1727 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1728 "A74206CEC169D74BF5A8C50D6F48EA08"
1729
1730#define PT_LEN 24
1731#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1732 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001735static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001736{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001737#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001738 size_t i;
1739
Paul Bakker545570e2010-07-18 09:00:25 +00001740 if( rng_state != NULL )
1741 rng_state = NULL;
1742
Paul Bakkera3d195c2011-11-27 21:07:34 +00001743 for( i = 0; i < len; ++i )
1744 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001745#else
1746 if( rng_state != NULL )
1747 rng_state = NULL;
1748
1749 arc4random_buf( output, len );
1750#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001751
Paul Bakkera3d195c2011-11-27 21:07:34 +00001752 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001755
Paul Bakker5121ce52009-01-03 21:22:43 +00001756/*
1757 * Checkup routine
1758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001760{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001761 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001763 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 unsigned char rsa_plaintext[PT_LEN];
1766 unsigned char rsa_decrypted[PT_LEN];
1767 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001769 unsigned char sha1sum[20];
1770#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001773
1774 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1776 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1777 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1778 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1779 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1780 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1781 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1782 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001783
1784 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1788 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 {
1790 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
1793 return( 1 );
1794 }
1795
1796 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
1799 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001802 rsa_plaintext, rsa_ciphertext ) != 0 )
1803 {
1804 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001806
1807 return( 1 );
1808 }
1809
1810 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001814 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001815 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 {
1817 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001819
1820 return( 1 );
1821 }
1822
1823 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1824 {
1825 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001827
1828 return( 1 );
1829 }
1830
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001831 if( verbose != 0 )
1832 mbedtls_printf( "passed\n" );
1833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001836 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 sha1sum, rsa_ciphertext ) != 0 )
1842 {
1843 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001845
1846 return( 1 );
1847 }
1848
1849 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001853 sha1sum, rsa_ciphertext ) != 0 )
1854 {
1855 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
1858 return( 1 );
1859 }
1860
1861 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001862 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001864
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001865 if( verbose != 0 )
1866 mbedtls_printf( "\n" );
1867
Paul Bakker3d8fb632014-04-17 12:42:41 +02001868cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 mbedtls_rsa_free( &rsa );
1870#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001871 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001873 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001874}
1875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878#endif /* MBEDTLS_RSA_C */