blob: ede1381a5a690b6bc1de40949115b0d51007830e [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/*
22 * RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
23 *
24 * http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
25 * http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
Janos Follath578517d2017-03-22 13:38:28 +000026 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
27 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
28 * Stefan Mangard
29 * https://arxiv.org/abs/1702.08719v2
30 *
Paul Bakker5121ce52009-01-03 21:22:43 +000031 */
32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/rsa.h"
42#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000043
Rich Evans00ab4702015-02-06 13:43:58 +000044#include <string.h>
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010056#else
Rich Evans00ab4702015-02-06 13:43:58 +000057#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020059#define mbedtls_calloc calloc
60#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010061#endif
62
Gilles Peskine8877ec22017-03-23 14:37:37 +010063/* Implementation that should never be optimized out by the compiler */
64static void mbedtls_zeroize( void *v, size_t n ) {
65 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
66}
67
Paul Bakker5121ce52009-01-03 21:22:43 +000068/*
69 * Initialize an RSA context
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000072 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000073 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_THREADING_C)
80 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020081#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000082}
83
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010084/*
85 * Set padding for an existing RSA context
86 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010088{
89 ctx->padding = padding;
90 ctx->hash_id = hash_id;
91}
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000094
95/*
96 * Generate an RSA keypair
97 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +000099 int (*f_rng)(void *, unsigned char *, size_t),
100 void *p_rng,
101 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102{
103 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Paul Bakker21eb2802010-08-16 11:10:02 +0000106 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
Janos Follath95b30362016-09-21 13:18:12 +0100109 if( nbits % 2 )
110 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
111
112 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath1a59a502016-02-23 14:42:48 +0000113 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115 /*
116 * find primes P and Q with Q < P so that:
117 * GCD( E, (P-1)*(Q-1) ) == 1
118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 do
122 {
Janos Follath1a59a502016-02-23 14:42:48 +0000123 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000124 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Janos Follath95b30362016-09-21 13:18:12 +0100126 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000127 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 continue;
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200133 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 continue;
135
Janos Follath95b30362016-09-21 13:18:12 +0100136 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
137 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
140 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
141 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
142 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146 /*
147 * D = E^-1 mod ((P-1)*(Q-1))
148 * DP = D mod (P - 1)
149 * DQ = D mod (Q - 1)
150 * QP = Q^-1 mod P
151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
153 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
154 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
155 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200157 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159cleanup:
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 if( ret != 0 )
164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_rsa_free( ctx );
166 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 }
168
Paul Bakker48377d92013-08-30 12:06:24 +0200169 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170}
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
174/*
175 * Check a public RSA key
176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000178{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000179 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000181
Paul Bakker48377d92013-08-30 12:06:24 +0200182 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200186 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
187 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200190 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
192 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194 return( 0 );
195}
196
197/*
198 * Check a private RSA key
199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
202 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 return( ret );
207
Paul Bakker37940d9f2009-07-10 22:38:58 +0000208 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
212 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
213 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
214 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
217 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
218 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
219 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
220 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
221 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
225 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
228 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
229 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000230 /*
231 * Check for a valid PKCS1v2 private key
232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
234 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
235 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
236 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
237 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
238 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
239 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 }
Paul Bakker48377d92013-08-30 12:06:24 +0200243
Paul Bakker5121ce52009-01-03 21:22:43 +0000244cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
246 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
247 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
248 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000251 return( ret );
252
Paul Bakker6c591fa2011-05-05 11:49:20 +0000253 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000255
256 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257}
258
259/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100260 * Check if contexts holding a public and private key match
261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100263{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
265 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100268 }
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
271 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100274 }
275
276 return( 0 );
277}
278
279/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 * Do an RSA public key operation
281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000283 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 unsigned char *output )
285{
Paul Bakker23986e52011-04-24 08:57:21 +0000286 int ret;
287 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200292#if defined(MBEDTLS_THREADING_C)
293 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
294 return( ret );
295#endif
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200301 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
302 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 }
304
305 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
307 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200311 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
312 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100313#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
317 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320 return( 0 );
321}
322
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200323/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200324 * Generate or update blinding values, see section 10 of:
325 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200326 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200327 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200328 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200329static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200330 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
331{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200332 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200333
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200334 if( ctx->Vf.p != NULL )
335 {
336 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
338 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
339 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
340 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200341
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200342 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200343 }
344
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200345 /* Unblinding value: Vf = random number, invertible mod N */
346 do {
347 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
351 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
352 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200353
354 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
356 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 +0200357
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200358
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200359cleanup:
360 return( ret );
361}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200362
Paul Bakker5121ce52009-01-03 21:22:43 +0000363/*
Janos Follath578517d2017-03-22 13:38:28 +0000364 * Exponent blinding supposed to prevent side-channel attacks using multiple
365 * traces of measurements to recover the RSA key. The more collisions are there,
366 * the more bits of the key can be recovered. See [3].
367 *
368 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
369 * observations on avarage.
370 *
371 * For example with 28 byte blinding to achieve 2 collisions the adversary has
372 * to make 2^112 observations on avarage.
373 *
374 * (With the currently (as of 2017 April) known best algorithms breaking 2048
375 * bit RSA requires approximately as much time as trying out 2^112 random keys.
376 * Thus in this sense with 28 byte blinding the security is not reduced by
377 * side-channel attacks like the one in [3])
378 *
379 * This countermeasure does not help if the key recovery is possible with a
380 * single trace.
381 */
382#define RSA_EXPONENT_BLINDING 28
383
384/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 * Do an RSA private key operation
386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200388 int (*f_rng)(void *, unsigned char *, size_t),
389 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000390 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 unsigned char *output )
392{
Paul Bakker23986e52011-04-24 08:57:21 +0000393 int ret;
394 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_mpi T, T1, T2;
Janos Follath9ef9f102017-03-22 15:13:15 +0000396 mbedtls_mpi P1, Q1, R;
Janos Follath578517d2017-03-22 13:38:28 +0000397#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath9ef9f102017-03-22 15:13:15 +0000398 mbedtls_mpi D_blind;
Janos Follath578517d2017-03-22 13:38:28 +0000399 mbedtls_mpi *D = &ctx->D;
Janos Follath9ef9f102017-03-22 15:13:15 +0000400#else
401 mbedtls_mpi DP_blind, DQ_blind;
402 mbedtls_mpi *DP = &ctx->DP;
403 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follath578517d2017-03-22 13:38:28 +0000404#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
Hanno Beckera82f8912017-11-06 15:08:27 +0000406 /* Temporaries holding the initial input and the double
407 * checked result; should be the same in the end. */
408 mbedtls_mpi I, C;
409
Manuel Pégourié-Gonnard9f44a802015-10-30 10:56:25 +0100410 /* Make sure we have private key info, prevent possible misuse */
Hanno Beckerde0b70c2017-11-06 15:08:53 +0000411#if defined(MBEDTLS_RSA_NO_CRT)
412 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
413 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
414 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 ||
415 ( f_rng != NULL && mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ) ||
416 ( f_rng != NULL && mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ) )
417 {
Manuel Pégourié-Gonnard9f44a802015-10-30 10:56:25 +0100418 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerde0b70c2017-11-06 15:08:53 +0000419 }
420#else /* ! MBEDTLS_RSA_NO_CRT */
421 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
422 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 ||
423 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
424 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
425 mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
426 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
427 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
428 {
429 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
430 }
431#endif /* ! MBEDTLS_RSA_NO_CRT */
432
433
434#if defined(MBEDTLS_THREADING_C)
435 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
436 return( ret );
437#endif
Manuel Pégourié-Gonnard9f44a802015-10-30 10:56:25 +0100438
Hanno Beckera82f8912017-11-06 15:08:27 +0000439 mbedtls_mpi_init( &I );
440 mbedtls_mpi_init( &C );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200441
442 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follath9ef9f102017-03-22 15:13:15 +0000443 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
444
445
446 if( f_rng != NULL )
447 {
Janos Follath578517d2017-03-22 13:38:28 +0000448#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath9ef9f102017-03-22 15:13:15 +0000449 mbedtls_mpi_init( &D_blind );
450#else
451 mbedtls_mpi_init( &DP_blind );
452 mbedtls_mpi_init( &DQ_blind );
Janos Follath578517d2017-03-22 13:38:28 +0000453#endif
Janos Follath9ef9f102017-03-22 15:13:15 +0000454 }
Janos Follath578517d2017-03-22 13:38:28 +0000455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
457 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200459 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
460 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 }
462
Hanno Beckera82f8912017-11-06 15:08:27 +0000463 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
464
Paul Bakkerf451bac2013-08-30 15:37:02 +0200465 if( f_rng != NULL )
466 {
467 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200468 * Blinding
469 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200470 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200471 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
472 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follath578517d2017-03-22 13:38:28 +0000474
Janos Follath578517d2017-03-22 13:38:28 +0000475 /*
476 * Exponent blinding
477 */
478 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
479 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
480
Janos Follath9ef9f102017-03-22 15:13:15 +0000481#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath578517d2017-03-22 13:38:28 +0000482 /*
483 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
484 */
485 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
486 f_rng, p_rng ) );
487 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
488 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
489 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
490
491 D = &D_blind;
Janos Follath9ef9f102017-03-22 15:13:15 +0000492#else
493 /*
494 * DP_blind = ( P - 1 ) * R + DP
495 */
496 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
497 f_rng, p_rng ) );
498 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
499 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
500 &ctx->DP ) );
501
502 DP = &DP_blind;
503
504 /*
505 * DQ_blind = ( Q - 1 ) * R + DQ
506 */
507 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
508 f_rng, p_rng ) );
509 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
510 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
511 &ctx->DQ ) );
512
513 DQ = &DQ_blind;
Janos Follath578517d2017-03-22 13:38:28 +0000514#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200515 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath578517d2017-03-22 13:38:28 +0000518 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100519#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200520 /*
Janos Follath578517d2017-03-22 13:38:28 +0000521 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 *
523 * T1 = input ^ dP mod P
524 * T2 = input ^ dQ mod Q
525 */
Janos Follath9ef9f102017-03-22 15:13:15 +0000526 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
527 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 /*
530 * T = (T1 - T2) * (Q^-1 mod P) mod P
531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
533 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
534 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
536 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200537 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
540 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
541#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200542
Paul Bakkerf451bac2013-08-30 15:37:02 +0200543 if( f_rng != NULL )
544 {
545 /*
546 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200547 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200548 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200549 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200551 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Hanno Beckera82f8912017-11-06 15:08:27 +0000553 /* Verify the result to prevent glitching attacks. */
554 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
555 &ctx->N, &ctx->RN ) );
556 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
557 {
558 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
559 goto cleanup;
560 }
561
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
565cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200567 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
568 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200569#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follath9ef9f102017-03-22 15:13:15 +0000572 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
573
574 if( f_rng != NULL )
575 {
Janos Follath578517d2017-03-22 13:38:28 +0000576#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath9ef9f102017-03-22 15:13:15 +0000577 mbedtls_mpi_free( &D_blind );
578#else
579 mbedtls_mpi_free( &DP_blind );
580 mbedtls_mpi_free( &DQ_blind );
Janos Follath578517d2017-03-22 13:38:28 +0000581#endif
Janos Follath9ef9f102017-03-22 15:13:15 +0000582 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Hanno Beckera82f8912017-11-06 15:08:27 +0000584 mbedtls_mpi_free( &C );
585 mbedtls_mpi_free( &I );
586
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590 return( 0 );
591}
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000594/**
595 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
596 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000597 * \param dst buffer to mask
598 * \param dlen length of destination buffer
599 * \param src source of the mask generation
600 * \param slen length of the source buffer
601 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000602 */
Paul Bakker48377d92013-08-30 12:06:24 +0200603static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000605{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000607 unsigned char counter[4];
608 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000609 unsigned int hlen;
610 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000613 memset( counter, 0, 4 );
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000616
617 // Generate and apply dbMask
618 //
619 p = dst;
620
621 while( dlen > 0 )
622 {
623 use_len = hlen;
624 if( dlen < hlen )
625 use_len = dlen;
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_md_starts( md_ctx );
628 mbedtls_md_update( md_ctx, src, slen );
629 mbedtls_md_update( md_ctx, counter, 4 );
630 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000631
632 for( i = 0; i < use_len; ++i )
633 *p++ ^= mask[i];
634
635 counter[3]++;
636
637 dlen -= use_len;
638 }
Gilles Peskine74fd8682017-05-05 19:24:06 +0200639
640 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000641}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100645/*
646 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100649 int (*f_rng)(void *, unsigned char *, size_t),
650 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100651 int mode,
652 const unsigned char *label, size_t label_len,
653 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100654 const unsigned char *input,
655 unsigned char *output )
656{
657 size_t olen;
658 int ret;
659 unsigned char *p = output;
660 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 const mbedtls_md_info_t *md_info;
662 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
665 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200666
667 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100671 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100673
674 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100676
Janos Follathe33f5592016-02-08 14:52:29 +0000677 // first comparison checks for overflow
678 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100680
681 memset( output, 0, olen );
682
683 *p++ = 0;
684
685 // Generate a random octet string seed
686 //
687 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100689
690 p += hlen;
691
692 // Construct DB
693 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100695 p += hlen;
696 p += olen - 2 * hlen - 2 - ilen;
697 *p++ = 1;
698 memcpy( p, input, ilen );
699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -0700701 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
702 {
703 mbedtls_md_free( &md_ctx );
704 return( ret );
705 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100706
707 // maskedDB: Apply dbMask to DB
708 //
709 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
710 &md_ctx );
711
712 // maskedSeed: Apply seedMask to seed
713 //
714 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
715 &md_ctx );
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 return( ( mode == MBEDTLS_RSA_PUBLIC )
720 ? mbedtls_rsa_public( ctx, output, output )
721 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100722}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100726/*
727 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100730 int (*f_rng)(void *, unsigned char *, size_t),
731 void *p_rng,
732 int mode, size_t ilen,
733 const unsigned char *input,
734 unsigned char *output )
735{
736 size_t nb_pad, olen;
737 int ret;
738 unsigned char *p = output;
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
741 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200742
Janos Follath689a6272016-03-18 11:45:44 +0000743 // We don't check p_rng because it won't be dereferenced here
744 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100746
747 olen = ctx->len;
Hanno Beckerde0b70c2017-11-06 15:08:53 +0000748
Janos Follathe33f5592016-02-08 14:52:29 +0000749 // first comparison checks for overflow
750 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100752
753 nb_pad = olen - 3 - ilen;
754
755 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100759
760 while( nb_pad-- > 0 )
761 {
762 int rng_dl = 100;
763
764 do {
765 ret = f_rng( p_rng, p, 1 );
766 } while( *p == 0 && --rng_dl && ret == 0 );
767
768 // Check if RNG failed to generate data
769 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200770 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100772
773 p++;
774 }
775 }
776 else
777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100779
780 while( nb_pad-- > 0 )
781 *p++ = 0xFF;
782 }
783
784 *p++ = 0;
785 memcpy( p, input, ilen );
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( ( mode == MBEDTLS_RSA_PUBLIC )
788 ? mbedtls_rsa_public( ctx, output, output )
789 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100790}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100792
Paul Bakker5121ce52009-01-03 21:22:43 +0000793/*
794 * Add the message padding, then do an RSA operation
795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000797 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000798 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000799 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000800 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000801 unsigned char *output )
802{
Paul Bakker5121ce52009-01-03 21:22:43 +0000803 switch( ctx->padding )
804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805#if defined(MBEDTLS_PKCS1_V15)
806 case MBEDTLS_RSA_PKCS_V15:
807 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100808 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200809#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811#if defined(MBEDTLS_PKCS1_V21)
812 case MBEDTLS_RSA_PKCS_V21:
813 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100814 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000815#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000816
817 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000819 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000820}
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000823/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100824 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200827 int (*f_rng)(void *, unsigned char *, size_t),
828 void *p_rng,
829 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100830 const unsigned char *label, size_t label_len,
831 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100832 const unsigned char *input,
833 unsigned char *output,
834 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000835{
Paul Bakker23986e52011-04-24 08:57:21 +0000836 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100837 size_t ilen, i, pad_len;
838 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
840 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000841 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 const mbedtls_md_info_t *md_info;
843 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100844
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100845 /*
846 * Parameters sanity checks
847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
849 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000850
851 ilen = ctx->len;
852
Paul Bakker27fdf462011-06-09 13:55:13 +0000853 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100857 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100859
Janos Follath25da9b32016-02-11 11:08:18 +0000860 hlen = mbedtls_md_get_size( md_info );
861
862 // checking for integer underflow
863 if( 2 * hlen + 2 > ilen )
864 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
865
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100866 /*
867 * RSA operation
868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 ret = ( mode == MBEDTLS_RSA_PUBLIC )
870 ? mbedtls_rsa_public( ctx, input, buf )
871 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000872
873 if( ret != 0 )
Gilles Peskine8877ec22017-03-23 14:37:37 +0100874 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000875
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100876 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100877 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -0700880 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
881 {
882 mbedtls_md_free( &md_ctx );
Gilles Peskine8877ec22017-03-23 14:37:37 +0100883 goto cleanup;
Brian J Murray88c2d222016-06-23 12:57:03 -0700884 }
885
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100886
887 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100889
890 /* seed: Apply seedMask to maskedSeed */
891 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
892 &md_ctx );
893
894 /* DB: Apply dbMask to maskedDB */
895 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
896 &md_ctx );
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100899
900 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100901 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100902 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000903 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100904 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100906 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100907
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100908 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100909
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100910 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100911 for( i = 0; i < hlen; i++ )
912 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100913
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100914 /* Get zero-padding len, but always read till end of buffer
915 * (minus one, for the 01 byte) */
916 pad_len = 0;
917 pad_done = 0;
918 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
919 {
920 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100921 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100922 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100923
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100924 p += pad_len;
925 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100926
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100927 /*
928 * The only information "leaked" is whether the padding was correct or not
929 * (eg, no data is copied if it was not correct). This meets the
930 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
931 * the different error conditions.
932 */
933 if( bad != 0 )
Gilles Peskine8877ec22017-03-23 14:37:37 +0100934 {
935 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
936 goto cleanup;
937 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100938
Paul Bakker66d5d072014-06-17 16:39:18 +0200939 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine8877ec22017-03-23 14:37:37 +0100940 {
941 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
942 goto cleanup;
943 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100944
945 *olen = ilen - (p - buf);
946 memcpy( output, p, *olen );
Gilles Peskine8877ec22017-03-23 14:37:37 +0100947 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100948
Gilles Peskine8877ec22017-03-23 14:37:37 +0100949cleanup:
950 mbedtls_zeroize( buf, sizeof( buf ) );
951 mbedtls_zeroize( lhash, sizeof( lhash ) );
952
953 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100954}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinef50ee602018-10-02 22:44:41 +0200958/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
959 *
960 * \param value The value to analyze.
Gilles Peskine9f11f212018-10-04 18:32:29 +0200961 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinef50ee602018-10-02 22:44:41 +0200962 */
Gilles Peskine9f11f212018-10-04 18:32:29 +0200963static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinef50ee602018-10-02 22:44:41 +0200964{
965 /* MSVC has a warning about unary minus on unsigned, but this is
966 * well-defined and precisely what we want to do here */
967#if defined(_MSC_VER)
968#pragma warning( push )
969#pragma warning( disable : 4146 )
970#endif
971 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
972#if defined(_MSC_VER)
973#pragma warning( pop )
974#endif
975}
976
977/** Choose between two integer values, without branches.
978 *
Gilles Peskine9f11f212018-10-04 18:32:29 +0200979 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
980 * to code using bitwise operation rather than a branch.
981 *
982 * \param cond Condition to test.
983 * \param if1 Value to use if \p cond is nonzero.
984 * \param if0 Value to use if \p cond is zero.
985 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinef50ee602018-10-02 22:44:41 +0200986 */
Gilles Peskine9f11f212018-10-04 18:32:29 +0200987static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinef50ee602018-10-02 22:44:41 +0200988{
Gilles Peskine9f11f212018-10-04 18:32:29 +0200989 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinef50ee602018-10-02 22:44:41 +0200990 return( ( mask & if1 ) | (~mask & if0 ) );
991}
992
Paul Bakkerb3869132013-02-28 17:21:01 +0100993/*
994 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200997 int (*f_rng)(void *, unsigned char *, size_t),
998 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100999 int mode, size_t *olen,
1000 const unsigned char *input,
1001 unsigned char *output,
Gilles Peskinef7a88142018-10-02 22:43:06 +02001002 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001003{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001004 int ret;
Gilles Peskinef7a88142018-10-02 22:43:06 +02001005 size_t ilen = ctx->len;
1006 size_t pad_count = 0;
1007 size_t i;
1008 unsigned bad = 0;
1009 unsigned char pad_done = 0;
Gilles Peskinef50ee602018-10-02 22:44:41 +02001010 size_t plaintext_size = 0;
1011 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1012 ilen - 11 :
1013 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskinef7a88142018-10-02 22:43:06 +02001015 unsigned char *p = buf;
Paul Bakkerb3869132013-02-28 17:21:01 +01001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1018 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001019
Paul Bakkerb3869132013-02-28 17:21:01 +01001020 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1024 ? mbedtls_rsa_public( ctx, input, buf )
1025 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001026
1027 if( ret != 0 )
Gilles Peskine8877ec22017-03-23 14:37:37 +01001028 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001029
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001030 /*
1031 * Check and get padding len in "constant-time"
1032 */
1033 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001034
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001035 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001039
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001040 /* Get padding len, but always read till end of buffer
1041 * (minus one, for the 00 byte) */
1042 for( i = 0; i < ilen - 3; i++ )
1043 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001044 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1045 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001046 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001047
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001048 p += pad_count;
1049 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001050 }
1051 else
1052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001054
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001055 /* Get padding len, but always read till end of buffer
1056 * (minus one, for the 00 byte) */
1057 for( i = 0; i < ilen - 3; i++ )
1058 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001059 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001060 pad_count += ( pad_done == 0 );
1061 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001062
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001063 p += pad_count;
1064 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 }
1066
Gilles Peskinef50ee602018-10-02 22:44:41 +02001067 /* There must be at least 8 bytes of padding. */
Janos Follathe007c9f2016-02-12 13:30:09 +00001068 bad |= ( pad_count < 8 );
Janos Follatha9583432016-02-08 13:59:25 +00001069
Gilles Peskinef50ee602018-10-02 22:44:41 +02001070 /* Set bad to zero if the padding is valid and
1071 * all-bits-one otherwise. The whole calculation of bad
1072 * is done in such a way to avoid branches. */
Gilles Peskine9f11f212018-10-04 18:32:29 +02001073 bad = all_or_nothing_int( bad );
Paul Bakker8804f692013-02-28 18:06:26 +01001074
Gilles Peskinef50ee602018-10-02 22:44:41 +02001075 /* If the padding is valid, set plaintext_size to the number of
1076 * remaining bytes after stripping the padding. If the padding
1077 * is invalid, avoid leaking this fact through the size of the
1078 * output: use the maximum message size that fits in the output
1079 * buffer. Do it without branches to avoid leaking the padding
1080 * validity through timing. RSA keys are small enough that all the
1081 * size_t values involved fit in unsigned int. */
Gilles Peskine9f11f212018-10-04 18:32:29 +02001082 plaintext_size = if_int( bad,
1083 (unsigned) plaintext_max_size,
1084 (unsigned) ( ilen - ( p - buf ) ) );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001085
1086 /* Check if the decrypted plaintext fits in the output buffer.
1087 * If the padding is bad, this will always be the case,
1088 * thus we don't leak the padding validity by trying to produce
1089 * a larger output than what the caller expects. */
1090 if( plaintext_size > output_max_len )
Gilles Peskine8877ec22017-03-23 14:37:37 +01001091 {
1092 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1093 goto cleanup;
1094 }
Paul Bakker060c5682009-01-12 21:48:39 +00001095
Gilles Peskinef50ee602018-10-02 22:44:41 +02001096 /* Set ret to INVALID_PADDING if the padding is bad and to 0
1097 * otherwise. At this point, the variable bad is zero if
1098 * the padding is good and can be any nonzero value otherwise.
1099 * Do this without branches to avoid timing attacks. */
1100 ret = - ( bad & ( - MBEDTLS_ERR_RSA_INVALID_PADDING ) );
1101
1102 /* If the padding is bad, zero the data that we're about to copy
1103 * to the output buffer. We need to copy the same amount of data
1104 * from the same buffer whether the padding is good or not to
1105 * avoid leaking the padding validity through overall timing or
1106 * through memory or cache access patterns. */
1107 for( i = 11; i < ilen; i++ )
1108 buf[i] &= ~bad;
1109
1110 /* Copy the decrypted plaintext from the end of the buffer. */
1111 memcpy( output, buf + ilen - plaintext_size, plaintext_size );
1112
1113 /* Report the amount of data we copied to the output buffer.
1114 * When the padding is invalid, the value of *olen when this
1115 * function returns is not specified. Making it equivalent to
1116 * the good-padding case limits the risks of leaking the
1117 * padding validity. */
1118 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001119
Gilles Peskine8877ec22017-03-23 14:37:37 +01001120cleanup:
1121 mbedtls_zeroize( buf, sizeof( buf ) );
1122
1123 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001126
1127/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001128 * Do an RSA operation, then remove the message padding
1129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001131 int (*f_rng)(void *, unsigned char *, size_t),
1132 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001133 int mode, size_t *olen,
1134 const unsigned char *input,
1135 unsigned char *output,
1136 size_t output_max_len)
1137{
1138 switch( ctx->padding )
1139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#if defined(MBEDTLS_PKCS1_V15)
1141 case MBEDTLS_RSA_PKCS_V15:
1142 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001143 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001144#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#if defined(MBEDTLS_PKCS1_V21)
1147 case MBEDTLS_RSA_PKCS_V21:
1148 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001149 olen, input, output,
1150 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001151#endif
1152
1153 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001155 }
1156}
1157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001159/*
1160 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001163 int (*f_rng)(void *, unsigned char *, size_t),
1164 void *p_rng,
1165 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001167 unsigned int hashlen,
1168 const unsigned char *hash,
1169 unsigned char *sig )
1170{
1171 size_t olen;
1172 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001174 unsigned int slen, hlen, offset = 0;
1175 int ret;
1176 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 const mbedtls_md_info_t *md_info;
1178 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1181 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001182
1183 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001185
1186 olen = ctx->len;
1187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001189 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001190 // Gather length of hash to sign
1191 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001193 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001197 }
1198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001200 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001204 slen = hlen;
1205
1206 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001208
1209 memset( sig, 0, olen );
1210
Paul Bakkerb3869132013-02-28 17:21:01 +01001211 // Generate salt of length slen
1212 //
1213 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001215
1216 // Note: EMSA-PSS encoding is over the length of N - 1 bits
1217 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001218 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001219 p += olen - hlen * 2 - 2;
1220 *p++ = 0x01;
1221 memcpy( p, salt, slen );
1222 p += slen;
1223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -07001225 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1226 {
1227 mbedtls_md_free( &md_ctx );
Gilles Peskine74fd8682017-05-05 19:24:06 +02001228 /* No need to zeroize salt: we didn't use it. */
Brian J Murray88c2d222016-06-23 12:57:03 -07001229 return( ret );
1230 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001231
1232 // Generate H = Hash( M' )
1233 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_md_starts( &md_ctx );
1235 mbedtls_md_update( &md_ctx, p, 8 );
1236 mbedtls_md_update( &md_ctx, hash, hashlen );
1237 mbedtls_md_update( &md_ctx, salt, slen );
1238 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine74fd8682017-05-05 19:24:06 +02001239 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001240
1241 // Compensate for boundary condition when applying mask
1242 //
1243 if( msb % 8 == 0 )
1244 offset = 1;
1245
1246 // maskedDB: Apply dbMask to DB
1247 //
1248 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001251
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001252 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001253 sig[0] &= 0xFF >> ( olen * 8 - msb );
1254
1255 p += hlen;
1256 *p++ = 0xBC;
1257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 return( ( mode == MBEDTLS_RSA_PUBLIC )
1259 ? mbedtls_rsa_public( ctx, sig, sig )
1260 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001265/*
1266 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1267 */
1268/*
1269 * Do an RSA operation to sign the message digest
1270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001272 int (*f_rng)(void *, unsigned char *, size_t),
1273 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001274 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001276 unsigned int hashlen,
1277 const unsigned char *hash,
1278 unsigned char *sig )
1279{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001280 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001281 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001282 const char *oid = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1285 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001286
1287 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001288 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001293 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1297 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001298
Paul Bakkerc70b9822013-04-07 22:00:46 +02001299 nb_pad -= 10 + oid_size;
1300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001302 }
1303
Paul Bakkerc70b9822013-04-07 22:00:46 +02001304 nb_pad -= hashlen;
1305
Paul Bakkerb3869132013-02-28 17:21:01 +01001306 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001308
1309 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001311 memset( p, 0xFF, nb_pad );
1312 p += nb_pad;
1313 *p++ = 0;
1314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001316 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001317 memcpy( p, hash, hashlen );
1318 }
1319 else
1320 {
1321 /*
1322 * DigestInfo ::= SEQUENCE {
1323 * digestAlgorithm DigestAlgorithmIdentifier,
1324 * digest Digest }
1325 *
1326 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1327 *
1328 * Digest ::= OCTET STRING
1329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001331 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001333 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001335 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001336 memcpy( p, oid, oid_size );
1337 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001339 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001341 *p++ = hashlen;
1342 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001343 }
1344
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001345 if( mode == MBEDTLS_RSA_PUBLIC )
1346 return( mbedtls_rsa_public( ctx, sig, sig ) );
1347
Hanno Becker21f83752017-11-06 15:09:33 +00001348 return( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001349}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001351
1352/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001353 * Do an RSA operation to sign the message digest
1354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001356 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001357 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001360 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001361 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 unsigned char *sig )
1363{
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 switch( ctx->padding )
1365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_PKCS1_V15)
1367 case MBEDTLS_RSA_PKCS_V15:
1368 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001369 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001370#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_PKCS1_V21)
1373 case MBEDTLS_RSA_PKCS_V21:
1374 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001375 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001376#endif
1377
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001381}
1382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001384/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001385 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001388 int (*f_rng)(void *, unsigned char *, size_t),
1389 void *p_rng,
1390 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001392 unsigned int hashlen,
1393 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001395 int expected_salt_len,
1396 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001397{
Paul Bakker23986e52011-04-24 08:57:21 +00001398 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001399 size_t siglen;
1400 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskined0cd8552017-10-17 19:02:13 +02001402 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001404 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001405 unsigned int hlen;
Gilles Peskined0cd8552017-10-17 19:02:13 +02001406 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 const mbedtls_md_info_t *md_info;
1408 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1411 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001412
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 siglen = ctx->len;
1414
Paul Bakker27fdf462011-06-09 13:55:13 +00001415 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1419 ? mbedtls_rsa_public( ctx, sig, buf )
1420 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
1422 if( ret != 0 )
1423 return( ret );
1424
1425 p = buf;
1426
Paul Bakkerb3869132013-02-28 17:21:01 +01001427 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001432 // Gather length of hash to sign
1433 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001435 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001439 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001442 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001446
Paul Bakkerb3869132013-02-28 17:21:01 +01001447 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001448
Paul Bakkerb3869132013-02-28 17:21:01 +01001449 // Note: EMSA-PSS verification is over the length of N - 1 bits
1450 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001451 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001452
Gilles Peskine31a2d142017-10-19 15:23:49 +02001453 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1454 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1455
1456 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001457 if( msb % 8 == 0 )
1458 {
1459 p++;
1460 siglen -= 1;
1461 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001462
Gilles Peskine9e205822017-10-18 19:03:42 +02001463 if( siglen < hlen + 2 )
1464 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1465 hash_start = p + siglen - hlen - 1;
1466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -07001468 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1469 {
1470 mbedtls_md_free( &md_ctx );
1471 return( ret );
1472 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001473
Gilles Peskined0cd8552017-10-17 19:02:13 +02001474 mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001475
Paul Bakkerb3869132013-02-28 17:21:01 +01001476 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001477
Gilles Peskined0cd8552017-10-17 19:02:13 +02001478 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001479 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001480
Gilles Peskine9745cfd2017-10-19 17:46:14 +02001481 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 mbedtls_md_free( &md_ctx );
1484 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001485 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001486
Gilles Peskined0cd8552017-10-17 19:02:13 +02001487 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskined0cd8552017-10-17 19:02:13 +02001490 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 mbedtls_md_free( &md_ctx );
1493 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001494 }
1495
Paul Bakkerb3869132013-02-28 17:21:01 +01001496 // Generate H = Hash( M' )
1497 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 mbedtls_md_starts( &md_ctx );
1499 mbedtls_md_update( &md_ctx, zeros, 8 );
1500 mbedtls_md_update( &md_ctx, hash, hashlen );
Gilles Peskined0cd8552017-10-17 19:02:13 +02001501 mbedtls_md_update( &md_ctx, p, observed_salt_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001505
Gilles Peskined0cd8552017-10-17 19:02:13 +02001506 if( memcmp( hash_start, result, hlen ) == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001507 return( 0 );
1508 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001510}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001511
1512/*
1513 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001516 int (*f_rng)(void *, unsigned char *, size_t),
1517 void *p_rng,
1518 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001520 unsigned int hashlen,
1521 const unsigned char *hash,
1522 const unsigned char *sig )
1523{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1525 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001526 : md_alg;
1527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001529 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001531 sig ) );
1532
1533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001537/*
1538 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001541 int (*f_rng)(void *, unsigned char *, size_t),
1542 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001543 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001545 unsigned int hashlen,
1546 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001547 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001548{
1549 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001550 size_t len, siglen, asn1_len;
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001551 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1553 mbedtls_md_type_t msg_md_alg;
1554 const mbedtls_md_info_t *md_info;
1555 mbedtls_asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1558 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001559
1560 siglen = ctx->len;
1561
1562 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1566 ? mbedtls_rsa_public( ctx, sig, buf )
1567 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001568
1569 if( ret != 0 )
1570 return( ret );
1571
1572 p = buf;
1573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1575 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001576
1577 while( *p != 0 )
1578 {
1579 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001581 p++;
1582 }
Manuel Pégourié-Gonnard230ee312017-05-11 12:49:51 +02001583 p++; /* skip 00 byte */
1584
1585 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
1586 if( p - buf < 11 )
1587 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001588
1589 len = siglen - ( p - buf );
1590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001592 {
1593 if( memcmp( p, hash, hashlen ) == 0 )
1594 return( 0 );
1595 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 }
1598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001600 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1602 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001603
1604 end = p + len;
1605
Gilles Peskinebd908512017-05-04 12:48:39 +02001606 /*
1607 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
1608 * Insist on 2-byte length tags, to protect against variants of
1609 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
1610 */
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001611 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1613 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1614 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001615 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001617
Gilles Peskinebd908512017-05-04 12:48:39 +02001618 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1620 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1621 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001622 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001624
Gilles Peskinebd908512017-05-04 12:48:39 +02001625 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1627 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001628 if( p != p0 + 2 )
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001629 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001630
1631 oid.p = p;
1632 p += oid.len;
1633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1635 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001636
1637 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001639
1640 /*
1641 * assume the algorithm parameters must be NULL
1642 */
Gilles Peskinebd908512017-05-04 12:48:39 +02001643 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1645 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001646 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001648
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001649 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001650 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1651 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001652 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001654
1655 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001657
1658 p += hashlen;
1659
1660 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001662
1663 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001664}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
1667/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001668 * Do an RSA operation and check the message digest
1669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001671 int (*f_rng)(void *, unsigned char *, size_t),
1672 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001673 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001675 unsigned int hashlen,
1676 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001677 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001678{
1679 switch( ctx->padding )
1680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681#if defined(MBEDTLS_PKCS1_V15)
1682 case MBEDTLS_RSA_PKCS_V15:
1683 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001684 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001685#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687#if defined(MBEDTLS_PKCS1_V21)
1688 case MBEDTLS_RSA_PKCS_V21:
1689 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001690 hashlen, hash, sig );
1691#endif
1692
1693 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001695 }
1696}
1697
1698/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001699 * Copy the components of an RSA key
1700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001702{
1703 int ret;
1704
1705 dst->ver = src->ver;
1706 dst->len = src->len;
1707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1709 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1712 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1713 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1714 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1715 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1716 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1719 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1720 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1723 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001724
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001725 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001726 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001727
1728cleanup:
1729 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001731
1732 return( ret );
1733}
1734
1735/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001736 * Free the components of an RSA key
1737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001739{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1741 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1742 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1743 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1744 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#if defined(MBEDTLS_THREADING_C)
1747 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001748#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001749}
1750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001752
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001753#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
1755/*
1756 * Example RSA-1024 keypair, for test purposes
1757 */
1758#define KEY_LEN 128
1759
1760#define RSA_N "9292758453063D803DD603D5E777D788" \
1761 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1762 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1763 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1764 "93A89813FBF3C4F8066D2D800F7C38A8" \
1765 "1AE31942917403FF4946B0A83D3D3E05" \
1766 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1767 "5E94BB77B07507233A0BC7BAC8F90F79"
1768
1769#define RSA_E "10001"
1770
1771#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1772 "66CA472BC44D253102F8B4A9D3BFA750" \
1773 "91386C0077937FE33FA3252D28855837" \
1774 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1775 "DF79C5CE07EE72C7F123142198164234" \
1776 "CABB724CF78B8173B9F880FC86322407" \
1777 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1778 "071513A1E85B5DFA031F21ECAE91A34D"
1779
1780#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1781 "2C01CAD19EA484A87EA4377637E75500" \
1782 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1783 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1784
1785#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1786 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1787 "910E4168387E3C30AA1E00C339A79508" \
1788 "8452DD96A9A5EA5D9DCA68DA636032AF"
1789
1790#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1791 "3C94D22288ACD763FD8E5600ED4A702D" \
1792 "F84198A5F06C2E72236AE490C93F07F8" \
1793 "3CC559CD27BC2D1CA488811730BB5725"
1794
1795#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1796 "D8AAEA56749EA28623272E4F7D0592AF" \
1797 "7C1F1313CAC9471B5C523BFE592F517B" \
1798 "407A1BD76C164B93DA2D32A383E58357"
1799
1800#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1801 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1802 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1803 "A74206CEC169D74BF5A8C50D6F48EA08"
1804
1805#define PT_LEN 24
1806#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1807 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001810static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001811{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001812#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001813 size_t i;
1814
Paul Bakker545570e2010-07-18 09:00:25 +00001815 if( rng_state != NULL )
1816 rng_state = NULL;
1817
Paul Bakkera3d195c2011-11-27 21:07:34 +00001818 for( i = 0; i < len; ++i )
1819 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001820#else
1821 if( rng_state != NULL )
1822 rng_state = NULL;
1823
1824 arc4random_buf( output, len );
1825#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001826
Paul Bakkera3d195c2011-11-27 21:07:34 +00001827 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001830
Paul Bakker5121ce52009-01-03 21:22:43 +00001831/*
1832 * Checkup routine
1833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001835{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001836 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001838 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001840 unsigned char rsa_plaintext[PT_LEN];
1841 unsigned char rsa_decrypted[PT_LEN];
1842 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001844 unsigned char sha1sum[20];
1845#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
1849 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1851 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1852 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1853 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1854 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1855 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1856 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1857 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
1859 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1863 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 {
1865 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001867
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001868 ret = 1;
1869 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 }
1871
1872 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001874
1875 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 rsa_plaintext, rsa_ciphertext ) != 0 )
1879 {
1880 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001882
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001883 ret = 1;
1884 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001885 }
1886
1887 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001891 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001892 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001893 {
1894 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001897 ret = 1;
1898 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 }
1900
1901 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1902 {
1903 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001905
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001906 ret = 1;
1907 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001908 }
1909
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001910 if( verbose != 0 )
1911 mbedtls_printf( "passed\n" );
1912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001915 mbedtls_printf( "PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 sha1sum, rsa_ciphertext ) != 0 )
1921 {
1922 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001924
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001925 ret = 1;
1926 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 }
1928
1929 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 sha1sum, rsa_ciphertext ) != 0 )
1934 {
1935 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001937
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001938 ret = 1;
1939 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 }
1941
1942 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001943 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001946 if( verbose != 0 )
1947 mbedtls_printf( "\n" );
1948
Paul Bakker3d8fb632014-04-17 12:42:41 +02001949cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 mbedtls_rsa_free( &rsa );
1951#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001952 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001954 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001955}
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#endif /* MBEDTLS_RSA_C */