blob: 1ba23159b9e539ff6d367917be6a5e2ca9711c49 [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
Gilles Peskine9fb28dd2018-10-04 21:18:30 +0200977/** Check whether a size is out of bounds, without branches.
978 *
979 * This is equivalent to `size > max`, but is likely to be compiled to
980 * to code using bitwise operation rather than a branch.
981 *
982 * \param size Size to check.
983 * \param max Maximum desired value for \p size.
984 * \return \c 0 if `size <= max`.
985 * \return \c 1 if `size > max`.
986 */
987static unsigned size_greater_than( size_t size, size_t max )
988{
989 /* Return the sign bit (1 for negative) of (max - size). */
990 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
991}
992
Gilles Peskinef50ee602018-10-02 22:44:41 +0200993/** Choose between two integer values, without branches.
994 *
Gilles Peskine9f11f212018-10-04 18:32:29 +0200995 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
996 * to code using bitwise operation rather than a branch.
997 *
998 * \param cond Condition to test.
999 * \param if1 Value to use if \p cond is nonzero.
1000 * \param if0 Value to use if \p cond is zero.
1001 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinef50ee602018-10-02 22:44:41 +02001002 */
Gilles Peskine9f11f212018-10-04 18:32:29 +02001003static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinef50ee602018-10-02 22:44:41 +02001004{
Gilles Peskine9f11f212018-10-04 18:32:29 +02001005 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001006 return( ( mask & if1 ) | (~mask & if0 ) );
1007}
1008
Gilles Peskine9fb28dd2018-10-04 21:18:30 +02001009/** Shift some data towards the left inside a buffer without leaking
1010 * the length of the data through side channels.
1011 *
1012 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1013 * ```
1014 * memmove(start, start + offset, total - offset);
1015 * memset(start + offset, 0, total - offset);
1016 * ```
1017 * but it strives to use a memory access pattern (and thus total timing)
1018 * that does not depend on \p offset. This timing independence comes at
1019 * the expense of performance.
1020 *
1021 * \param start Pointer to the start of the buffer.
1022 * \param total Total size of the buffer.
1023 * \param offset Offset from which to copy \p total - \p offset bytes.
1024 */
1025static void mem_move_to_left( void *start,
1026 size_t total,
1027 size_t offset )
1028{
1029 volatile unsigned char *buf = start;
1030 size_t i, n;
1031 if( total == 0 )
1032 return;
1033 for( i = 0; i < total; i++ )
1034 {
1035 unsigned no_op = size_greater_than( total - offset, i );
1036 /* The first `total - offset` passes are a no-op. The last
1037 * `offset` passes shift the data one byte to the left and
1038 * zero out the last byte. */
1039 for( n = 0; n < total - 1; n++ )
Gilles Peskine996f30d2018-10-12 19:15:34 +02001040 {
1041 unsigned char current = buf[n];
1042 unsigned char next = buf[n+1];
1043 buf[n] = if_int( no_op, current, next );
1044 }
Gilles Peskine9fb28dd2018-10-04 21:18:30 +02001045 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1046 }
1047}
1048
Paul Bakkerb3869132013-02-28 17:21:01 +01001049/*
1050 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001053 int (*f_rng)(void *, unsigned char *, size_t),
1054 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001055 int mode, size_t *olen,
1056 const unsigned char *input,
1057 unsigned char *output,
Gilles Peskinef7a88142018-10-02 22:43:06 +02001058 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001059{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001060 int ret;
Gilles Peskinef7a88142018-10-02 22:43:06 +02001061 size_t ilen = ctx->len;
Gilles Peskinef7a88142018-10-02 22:43:06 +02001062 size_t i;
Gilles Peskinef50ee602018-10-02 22:44:41 +02001063 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1064 ilen - 11 :
1065 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskinec5552e82018-10-05 14:50:21 +02001067 /* The following variables take sensitive values: their value must
1068 * not leak into the observable behavior of the function other than
1069 * the designated outputs (output, olen, return value). Otherwise
1070 * this would open the execution of the function to
1071 * side-channel-based variants of the Bleichenbacher padding oracle
1072 * attack. Potential side channels include overall timing, memory
1073 * access patterns (especially visible to an adversary who has access
1074 * to a shared memory cache), and branches (especially visible to
1075 * an adversary who has access to a shared code cache or to a shared
1076 * branch predictor). */
1077 size_t pad_count = 0;
1078 unsigned bad = 0;
1079 unsigned char pad_done = 0;
1080 size_t plaintext_size = 0;
1081 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1084 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001085
Paul Bakkerb3869132013-02-28 17:21:01 +01001086 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1090 ? mbedtls_rsa_public( ctx, input, buf )
1091 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001092
1093 if( ret != 0 )
Gilles Peskine8877ec22017-03-23 14:37:37 +01001094 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001095
Gilles Peskinefde301a2018-10-05 15:06:12 +02001096 /* Check and get padding length in constant time and constant
1097 * memory trace. The first byte must be 0. */
1098 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 {
Gilles Peskinefde301a2018-10-05 15:06:12 +02001102 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1103 * where PS must be at least 8 nonzero bytes. */
Gilles Peskinec5552e82018-10-05 14:50:21 +02001104 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001105
Gilles Peskine2bd65182018-10-05 18:11:27 +02001106 /* Read the whole buffer. Set pad_done to nonzero if we find
1107 * the 0x00 byte and remember the padding length in pad_count. */
1108 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001109 {
Gilles Peskinec5552e82018-10-05 14:50:21 +02001110 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001111 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001112 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001113 }
1114 else
1115 {
Gilles Peskinefde301a2018-10-05 15:06:12 +02001116 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1117 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskinec5552e82018-10-05 14:50:21 +02001118 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001119
Gilles Peskine2bd65182018-10-05 18:11:27 +02001120 /* Read the whole buffer. Set pad_done to nonzero if we find
1121 * the 0x00 byte and remember the padding length in pad_count.
1122 * If there's a non-0xff byte in the padding, the padding is bad. */
1123 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001124 {
Gilles Peskinefde301a2018-10-05 15:06:12 +02001125 pad_done |= if_int( buf[i], 0, 1 );
1126 pad_count += if_int( pad_done, 0, 1 );
1127 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001128 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 }
1130
Gilles Peskinefde301a2018-10-05 15:06:12 +02001131 /* If pad_done is still zero, there's no data, only unfinished padding. */
1132 bad |= if_int( pad_done, 0, 1 );
1133
Gilles Peskinef50ee602018-10-02 22:44:41 +02001134 /* There must be at least 8 bytes of padding. */
Gilles Peskine08513ce2018-10-04 21:24:21 +02001135 bad |= size_greater_than( 8, pad_count );
Janos Follatha9583432016-02-08 13:59:25 +00001136
Gilles Peskinef50ee602018-10-02 22:44:41 +02001137 /* If the padding is valid, set plaintext_size to the number of
1138 * remaining bytes after stripping the padding. If the padding
1139 * is invalid, avoid leaking this fact through the size of the
1140 * output: use the maximum message size that fits in the output
1141 * buffer. Do it without branches to avoid leaking the padding
1142 * validity through timing. RSA keys are small enough that all the
1143 * size_t values involved fit in unsigned int. */
Gilles Peskine9f11f212018-10-04 18:32:29 +02001144 plaintext_size = if_int( bad,
1145 (unsigned) plaintext_max_size,
Gilles Peskinec5552e82018-10-05 14:50:21 +02001146 (unsigned) ( ilen - pad_count - 3 ) );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001147
Gilles Peskine20365082018-10-04 19:13:43 +02001148 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskine08513ce2018-10-04 21:24:21 +02001149 * buffer and to 1 otherwise. */
1150 output_too_large = size_greater_than( plaintext_size,
1151 plaintext_max_size );
Paul Bakker060c5682009-01-12 21:48:39 +00001152
Gilles Peskine20365082018-10-04 19:13:43 +02001153 /* Set ret without branches to avoid timing attacks. Return:
1154 * - INVALID_PADDING if the padding is bad (bad != 0).
1155 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1156 * plaintext does not fit in the output buffer.
1157 * - 0 if the padding is correct. */
Gilles Peskinee3be8d62018-10-12 19:19:12 +02001158 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1159 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1160 0 ) );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001161
Gilles Peskine20365082018-10-04 19:13:43 +02001162 /* If the padding is bad or the plaintext is too large, zero the
1163 * data that we're about to copy to the output buffer.
1164 * We need to copy the same amount of data
Gilles Peskinef50ee602018-10-02 22:44:41 +02001165 * from the same buffer whether the padding is good or not to
1166 * avoid leaking the padding validity through overall timing or
1167 * through memory or cache access patterns. */
Gilles Peskinefde301a2018-10-05 15:06:12 +02001168 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001169 for( i = 11; i < ilen; i++ )
Gilles Peskinefde301a2018-10-05 15:06:12 +02001170 buf[i] &= ~bad;
Gilles Peskinef50ee602018-10-02 22:44:41 +02001171
Gilles Peskine20365082018-10-04 19:13:43 +02001172 /* If the plaintext is too large, truncate it to the buffer size.
1173 * Copy anyway to avoid revealing the length through timing, because
1174 * revealing the length is as bad as revealing the padding validity
1175 * for a Bleichenbacher attack. */
1176 plaintext_size = if_int( output_too_large,
1177 (unsigned) plaintext_max_size,
1178 (unsigned) plaintext_size );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001179
Gilles Peskinef19aefb2018-10-04 22:45:13 +02001180 /* Move the plaintext to the leftmost position where it can start in
1181 * the working buffer, i.e. make it start plaintext_max_size from
1182 * the end of the buffer. Do this with a memory access trace that
1183 * does not depend on the plaintext size. After this move, the
1184 * starting location of the plaintext is no longer sensitive
1185 * information. */
Gilles Peskinec5552e82018-10-05 14:50:21 +02001186 mem_move_to_left( buf + ilen - plaintext_max_size,
1187 plaintext_max_size,
Gilles Peskinef19aefb2018-10-04 22:45:13 +02001188 plaintext_max_size - plaintext_size );
Gilles Peskine20365082018-10-04 19:13:43 +02001189
Gilles Peskinef19aefb2018-10-04 22:45:13 +02001190 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskine20365082018-10-04 19:13:43 +02001191 * into the output buffer. */
Gilles Peskinec5552e82018-10-05 14:50:21 +02001192 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskine20365082018-10-04 19:13:43 +02001193
1194 /* Report the amount of data we copied to the output buffer. In case
1195 * of errors (bad padding or output too large), the value of *olen
1196 * when this function returns is not specified. Making it equivalent
1197 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinef50ee602018-10-02 22:44:41 +02001198 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
Gilles Peskine8877ec22017-03-23 14:37:37 +01001200cleanup:
1201 mbedtls_zeroize( buf, sizeof( buf ) );
1202
1203 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001206
1207/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001208 * Do an RSA operation, then remove the message padding
1209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001211 int (*f_rng)(void *, unsigned char *, size_t),
1212 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001213 int mode, size_t *olen,
1214 const unsigned char *input,
1215 unsigned char *output,
1216 size_t output_max_len)
1217{
1218 switch( ctx->padding )
1219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#if defined(MBEDTLS_PKCS1_V15)
1221 case MBEDTLS_RSA_PKCS_V15:
1222 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001223 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001224#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#if defined(MBEDTLS_PKCS1_V21)
1227 case MBEDTLS_RSA_PKCS_V21:
1228 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001229 olen, input, output,
1230 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001231#endif
1232
1233 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001235 }
1236}
1237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001239/*
1240 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001243 int (*f_rng)(void *, unsigned char *, size_t),
1244 void *p_rng,
1245 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001247 unsigned int hashlen,
1248 const unsigned char *hash,
1249 unsigned char *sig )
1250{
1251 size_t olen;
1252 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001254 unsigned int slen, hlen, offset = 0;
1255 int ret;
1256 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 const mbedtls_md_info_t *md_info;
1258 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1261 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001262
1263 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001265
1266 olen = ctx->len;
1267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001269 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001270 // Gather length of hash to sign
1271 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001273 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001277 }
1278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001280 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001284 slen = hlen;
1285
1286 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001288
1289 memset( sig, 0, olen );
1290
Paul Bakkerb3869132013-02-28 17:21:01 +01001291 // Generate salt of length slen
1292 //
1293 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001295
1296 // Note: EMSA-PSS encoding is over the length of N - 1 bits
1297 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001298 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001299 p += olen - hlen * 2 - 2;
1300 *p++ = 0x01;
1301 memcpy( p, salt, slen );
1302 p += slen;
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -07001305 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1306 {
1307 mbedtls_md_free( &md_ctx );
Gilles Peskine74fd8682017-05-05 19:24:06 +02001308 /* No need to zeroize salt: we didn't use it. */
Brian J Murray88c2d222016-06-23 12:57:03 -07001309 return( ret );
1310 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001311
1312 // Generate H = Hash( M' )
1313 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_md_starts( &md_ctx );
1315 mbedtls_md_update( &md_ctx, p, 8 );
1316 mbedtls_md_update( &md_ctx, hash, hashlen );
1317 mbedtls_md_update( &md_ctx, salt, slen );
1318 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine74fd8682017-05-05 19:24:06 +02001319 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001320
1321 // Compensate for boundary condition when applying mask
1322 //
1323 if( msb % 8 == 0 )
1324 offset = 1;
1325
1326 // maskedDB: Apply dbMask to DB
1327 //
1328 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001331
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001332 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001333 sig[0] &= 0xFF >> ( olen * 8 - msb );
1334
1335 p += hlen;
1336 *p++ = 0xBC;
1337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 return( ( mode == MBEDTLS_RSA_PUBLIC )
1339 ? mbedtls_rsa_public( ctx, sig, sig )
1340 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001341}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001345/*
1346 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1347 */
1348/*
1349 * Do an RSA operation to sign the message digest
1350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001352 int (*f_rng)(void *, unsigned char *, size_t),
1353 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001354 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001356 unsigned int hashlen,
1357 const unsigned char *hash,
1358 unsigned char *sig )
1359{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001360 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001361 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001362 const char *oid = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1365 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001366
1367 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001368 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001373 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1377 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001378
Paul Bakkerc70b9822013-04-07 22:00:46 +02001379 nb_pad -= 10 + oid_size;
1380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001382 }
1383
Paul Bakkerc70b9822013-04-07 22:00:46 +02001384 nb_pad -= hashlen;
1385
Paul Bakkerb3869132013-02-28 17:21:01 +01001386 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001388
1389 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001391 memset( p, 0xFF, nb_pad );
1392 p += nb_pad;
1393 *p++ = 0;
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001396 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001397 memcpy( p, hash, hashlen );
1398 }
1399 else
1400 {
1401 /*
1402 * DigestInfo ::= SEQUENCE {
1403 * digestAlgorithm DigestAlgorithmIdentifier,
1404 * digest Digest }
1405 *
1406 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1407 *
1408 * Digest ::= OCTET STRING
1409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001411 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001413 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001415 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001416 memcpy( p, oid, oid_size );
1417 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001419 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001421 *p++ = hashlen;
1422 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001423 }
1424
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001425 if( mode == MBEDTLS_RSA_PUBLIC )
1426 return( mbedtls_rsa_public( ctx, sig, sig ) );
1427
Hanno Becker21f83752017-11-06 15:09:33 +00001428 return( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001431
1432/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 * Do an RSA operation to sign the message digest
1434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001436 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001437 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001440 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001441 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 unsigned char *sig )
1443{
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 switch( ctx->padding )
1445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#if defined(MBEDTLS_PKCS1_V15)
1447 case MBEDTLS_RSA_PKCS_V15:
1448 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001449 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001450#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#if defined(MBEDTLS_PKCS1_V21)
1453 case MBEDTLS_RSA_PKCS_V21:
1454 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001455 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001456#endif
1457
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001461}
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001464/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001465 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001468 int (*f_rng)(void *, unsigned char *, size_t),
1469 void *p_rng,
1470 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001472 unsigned int hashlen,
1473 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001475 int expected_salt_len,
1476 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001477{
Paul Bakker23986e52011-04-24 08:57:21 +00001478 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001479 size_t siglen;
1480 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskined0cd8552017-10-17 19:02:13 +02001482 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001484 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001485 unsigned int hlen;
Gilles Peskined0cd8552017-10-17 19:02:13 +02001486 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 const mbedtls_md_info_t *md_info;
1488 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1491 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001492
Paul Bakker5121ce52009-01-03 21:22:43 +00001493 siglen = ctx->len;
1494
Paul Bakker27fdf462011-06-09 13:55:13 +00001495 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1499 ? mbedtls_rsa_public( ctx, sig, buf )
1500 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001501
1502 if( ret != 0 )
1503 return( ret );
1504
1505 p = buf;
1506
Paul Bakkerb3869132013-02-28 17:21:01 +01001507 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001512 // Gather length of hash to sign
1513 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001515 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001519 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001522 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001526
Paul Bakkerb3869132013-02-28 17:21:01 +01001527 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001528
Paul Bakkerb3869132013-02-28 17:21:01 +01001529 // Note: EMSA-PSS verification is over the length of N - 1 bits
1530 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001531 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001532
Gilles Peskine31a2d142017-10-19 15:23:49 +02001533 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1534 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1535
1536 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001537 if( msb % 8 == 0 )
1538 {
1539 p++;
1540 siglen -= 1;
1541 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001542
Gilles Peskine9e205822017-10-18 19:03:42 +02001543 if( siglen < hlen + 2 )
1544 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1545 hash_start = p + siglen - hlen - 1;
1546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -07001548 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1549 {
1550 mbedtls_md_free( &md_ctx );
1551 return( ret );
1552 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001553
Gilles Peskined0cd8552017-10-17 19:02:13 +02001554 mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001555
Paul Bakkerb3869132013-02-28 17:21:01 +01001556 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001557
Gilles Peskined0cd8552017-10-17 19:02:13 +02001558 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001559 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001560
Gilles Peskine9745cfd2017-10-19 17:46:14 +02001561 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 mbedtls_md_free( &md_ctx );
1564 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001565 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001566
Gilles Peskined0cd8552017-10-17 19:02:13 +02001567 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskined0cd8552017-10-17 19:02:13 +02001570 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 mbedtls_md_free( &md_ctx );
1573 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001574 }
1575
Paul Bakkerb3869132013-02-28 17:21:01 +01001576 // Generate H = Hash( M' )
1577 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 mbedtls_md_starts( &md_ctx );
1579 mbedtls_md_update( &md_ctx, zeros, 8 );
1580 mbedtls_md_update( &md_ctx, hash, hashlen );
Gilles Peskined0cd8552017-10-17 19:02:13 +02001581 mbedtls_md_update( &md_ctx, p, observed_salt_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001585
Gilles Peskined0cd8552017-10-17 19:02:13 +02001586 if( memcmp( hash_start, result, hlen ) == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001587 return( 0 );
1588 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001590}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001591
1592/*
1593 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001596 int (*f_rng)(void *, unsigned char *, size_t),
1597 void *p_rng,
1598 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001600 unsigned int hashlen,
1601 const unsigned char *hash,
1602 const unsigned char *sig )
1603{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1605 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001606 : md_alg;
1607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001609 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001611 sig ) );
1612
1613}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001615
Hanno Beckerb46e7332018-10-25 14:37:35 +01001616#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_ASN1_PARSE_C)
Paul Bakkerb3869132013-02-28 17:21:01 +01001617/*
1618 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001621 int (*f_rng)(void *, unsigned char *, size_t),
1622 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001623 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001625 unsigned int hashlen,
1626 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001627 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001628{
1629 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001630 size_t len, siglen, asn1_len;
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001631 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1633 mbedtls_md_type_t msg_md_alg;
1634 const mbedtls_md_info_t *md_info;
1635 mbedtls_asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1638 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001639
1640 siglen = ctx->len;
1641
1642 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1646 ? mbedtls_rsa_public( ctx, sig, buf )
1647 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001648
1649 if( ret != 0 )
1650 return( ret );
1651
1652 p = buf;
1653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1655 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001656
1657 while( *p != 0 )
1658 {
1659 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001661 p++;
1662 }
Manuel Pégourié-Gonnard230ee312017-05-11 12:49:51 +02001663 p++; /* skip 00 byte */
1664
1665 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
1666 if( p - buf < 11 )
1667 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001668
1669 len = siglen - ( p - buf );
1670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001672 {
1673 if( memcmp( p, hash, hashlen ) == 0 )
1674 return( 0 );
1675 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 }
1678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001680 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1682 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001683
1684 end = p + len;
1685
Gilles Peskinebd908512017-05-04 12:48:39 +02001686 /*
1687 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
1688 * Insist on 2-byte length tags, to protect against variants of
1689 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
1690 */
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001691 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1693 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1694 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001695 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001697
Gilles Peskinebd908512017-05-04 12:48:39 +02001698 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1700 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1701 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001702 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001704
Gilles Peskinebd908512017-05-04 12:48:39 +02001705 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1707 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001708 if( p != p0 + 2 )
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001709 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001710
1711 oid.p = p;
1712 p += oid.len;
1713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1715 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001716
1717 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001719
1720 /*
1721 * assume the algorithm parameters must be NULL
1722 */
Gilles Peskinebd908512017-05-04 12:48:39 +02001723 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1725 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001726 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001728
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001729 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001730 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1731 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001732 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001734
1735 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001737
1738 p += hashlen;
1739
1740 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001742
1743 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001744}
Hanno Beckerb46e7332018-10-25 14:37:35 +01001745#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_ASN1_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001746
1747/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001748 * Do an RSA operation and check the message digest
1749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001751 int (*f_rng)(void *, unsigned char *, size_t),
1752 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001753 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001755 unsigned int hashlen,
1756 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001757 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001758{
1759 switch( ctx->padding )
1760 {
Hanno Beckerb46e7332018-10-25 14:37:35 +01001761#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_ASN1_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 case MBEDTLS_RSA_PKCS_V15:
1763 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001764 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001765#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#if defined(MBEDTLS_PKCS1_V21)
1768 case MBEDTLS_RSA_PKCS_V21:
1769 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001770 hashlen, hash, sig );
1771#endif
1772
1773 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001775 }
1776}
1777
1778/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001779 * Copy the components of an RSA key
1780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001782{
1783 int ret;
1784
1785 dst->ver = src->ver;
1786 dst->len = src->len;
1787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1789 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1792 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1793 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1794 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1795 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1796 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1799 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1800 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1803 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001804
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001805 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001806 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001807
1808cleanup:
1809 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001811
1812 return( ret );
1813}
1814
1815/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 * Free the components of an RSA key
1817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001819{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1821 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1822 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1823 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1824 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#if defined(MBEDTLS_THREADING_C)
1827 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001828#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001829}
1830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001832
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001833#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001834
1835/*
1836 * Example RSA-1024 keypair, for test purposes
1837 */
1838#define KEY_LEN 128
1839
1840#define RSA_N "9292758453063D803DD603D5E777D788" \
1841 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1842 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1843 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1844 "93A89813FBF3C4F8066D2D800F7C38A8" \
1845 "1AE31942917403FF4946B0A83D3D3E05" \
1846 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1847 "5E94BB77B07507233A0BC7BAC8F90F79"
1848
1849#define RSA_E "10001"
1850
1851#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1852 "66CA472BC44D253102F8B4A9D3BFA750" \
1853 "91386C0077937FE33FA3252D28855837" \
1854 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1855 "DF79C5CE07EE72C7F123142198164234" \
1856 "CABB724CF78B8173B9F880FC86322407" \
1857 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1858 "071513A1E85B5DFA031F21ECAE91A34D"
1859
1860#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1861 "2C01CAD19EA484A87EA4377637E75500" \
1862 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1863 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1864
1865#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1866 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1867 "910E4168387E3C30AA1E00C339A79508" \
1868 "8452DD96A9A5EA5D9DCA68DA636032AF"
1869
1870#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1871 "3C94D22288ACD763FD8E5600ED4A702D" \
1872 "F84198A5F06C2E72236AE490C93F07F8" \
1873 "3CC559CD27BC2D1CA488811730BB5725"
1874
1875#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1876 "D8AAEA56749EA28623272E4F7D0592AF" \
1877 "7C1F1313CAC9471B5C523BFE592F517B" \
1878 "407A1BD76C164B93DA2D32A383E58357"
1879
1880#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1881 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1882 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1883 "A74206CEC169D74BF5A8C50D6F48EA08"
1884
1885#define PT_LEN 24
1886#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1887 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001890static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001891{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001892#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001893 size_t i;
1894
Paul Bakker545570e2010-07-18 09:00:25 +00001895 if( rng_state != NULL )
1896 rng_state = NULL;
1897
Paul Bakkera3d195c2011-11-27 21:07:34 +00001898 for( i = 0; i < len; ++i )
1899 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001900#else
1901 if( rng_state != NULL )
1902 rng_state = NULL;
1903
1904 arc4random_buf( output, len );
1905#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001906
Paul Bakkera3d195c2011-11-27 21:07:34 +00001907 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001908}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001910
Paul Bakker5121ce52009-01-03 21:22:43 +00001911/*
1912 * Checkup routine
1913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001915{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001916 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001918 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 unsigned char rsa_plaintext[PT_LEN];
1921 unsigned char rsa_decrypted[PT_LEN];
1922 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001924 unsigned char sha1sum[20];
1925#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
1929 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1931 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1932 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1933 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1934 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1935 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1936 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1937 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
1939 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1943 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 {
1945 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001947
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001948 ret = 1;
1949 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 }
1951
1952 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001954
1955 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 rsa_plaintext, rsa_ciphertext ) != 0 )
1959 {
1960 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001962
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001963 ret = 1;
1964 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 }
1966
1967 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001971 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001972 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 {
1974 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001976
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001977 ret = 1;
1978 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 }
1980
1981 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1982 {
1983 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001985
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001986 ret = 1;
1987 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 }
1989
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001990 if( verbose != 0 )
1991 mbedtls_printf( "passed\n" );
1992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001995 mbedtls_printf( "PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 sha1sum, rsa_ciphertext ) != 0 )
2001 {
2002 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002004
Hanno Beckerb81fcd02017-05-03 15:09:31 +01002005 ret = 1;
2006 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 }
2008
2009 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 sha1sum, rsa_ciphertext ) != 0 )
2014 {
2015 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002017
Hanno Beckerb81fcd02017-05-03 15:09:31 +01002018 ret = 1;
2019 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 }
2021
2022 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002023 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002025
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002026 if( verbose != 0 )
2027 mbedtls_printf( "\n" );
2028
Paul Bakker3d8fb632014-04-17 12:42:41 +02002029cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 mbedtls_rsa_free( &rsa );
2031#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002032 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002034 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035}
2036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#endif /* MBEDTLS_RSA_C */