blob: facb11a523912af347b7d06f55a575ddebf0eb8e [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++ )
1040 buf[n] = if_int( no_op, buf[n], buf[n+1] );
1041 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1042 }
1043}
1044
Paul Bakkerb3869132013-02-28 17:21:01 +01001045/*
1046 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001049 int (*f_rng)(void *, unsigned char *, size_t),
1050 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001051 int mode, size_t *olen,
1052 const unsigned char *input,
1053 unsigned char *output,
Gilles Peskinef7a88142018-10-02 22:43:06 +02001054 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001055{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001056 int ret;
Gilles Peskinef7a88142018-10-02 22:43:06 +02001057 size_t ilen = ctx->len;
Gilles Peskinef7a88142018-10-02 22:43:06 +02001058 size_t i;
Gilles Peskinef50ee602018-10-02 22:44:41 +02001059 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1060 ilen - 11 :
1061 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskinec5552e82018-10-05 14:50:21 +02001063 /* The following variables take sensitive values: their value must
1064 * not leak into the observable behavior of the function other than
1065 * the designated outputs (output, olen, return value). Otherwise
1066 * this would open the execution of the function to
1067 * side-channel-based variants of the Bleichenbacher padding oracle
1068 * attack. Potential side channels include overall timing, memory
1069 * access patterns (especially visible to an adversary who has access
1070 * to a shared memory cache), and branches (especially visible to
1071 * an adversary who has access to a shared code cache or to a shared
1072 * branch predictor). */
1073 size_t pad_count = 0;
1074 unsigned bad = 0;
1075 unsigned char pad_done = 0;
1076 size_t plaintext_size = 0;
1077 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1080 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001081
Paul Bakkerb3869132013-02-28 17:21:01 +01001082 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1086 ? mbedtls_rsa_public( ctx, input, buf )
1087 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001088
1089 if( ret != 0 )
Gilles Peskine8877ec22017-03-23 14:37:37 +01001090 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001091
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001092 /*
1093 * Check and get padding len in "constant-time"
1094 */
Gilles Peskinec5552e82018-10-05 14:50:21 +02001095 bad |= buf[0]; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001096
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001097 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001099 {
Gilles Peskinec5552e82018-10-05 14:50:21 +02001100 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001102 /* Get padding len, but always read till end of buffer
1103 * (minus one, for the 00 byte) */
Gilles Peskinec5552e82018-10-05 14:50:21 +02001104 for( i = 2; i < ilen - 1; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001105 {
Gilles Peskinec5552e82018-10-05 14:50:21 +02001106 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001107 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001108 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001109
Gilles Peskinec5552e82018-10-05 14:50:21 +02001110 bad |= buf[pad_count + 2];
Paul Bakkerb3869132013-02-28 17:21:01 +01001111 }
1112 else
1113 {
Gilles Peskinec5552e82018-10-05 14:50:21 +02001114 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001115
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001116 /* Get padding len, but always read till end of buffer
1117 * (minus one, for the 00 byte) */
Gilles Peskinec5552e82018-10-05 14:50:21 +02001118 for( i = 2; i < ilen - 1; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001119 {
Gilles Peskinec5552e82018-10-05 14:50:21 +02001120 pad_done |= ( buf[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001121 pad_count += ( pad_done == 0 );
1122 }
Gilles Peskinec5552e82018-10-05 14:50:21 +02001123 bad |= buf[pad_count + 2];
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 }
1125
Gilles Peskinef50ee602018-10-02 22:44:41 +02001126 /* There must be at least 8 bytes of padding. */
Gilles Peskine08513ce2018-10-04 21:24:21 +02001127 bad |= size_greater_than( 8, pad_count );
Janos Follatha9583432016-02-08 13:59:25 +00001128
Gilles Peskinef50ee602018-10-02 22:44:41 +02001129 /* If the padding is valid, set plaintext_size to the number of
1130 * remaining bytes after stripping the padding. If the padding
1131 * is invalid, avoid leaking this fact through the size of the
1132 * output: use the maximum message size that fits in the output
1133 * buffer. Do it without branches to avoid leaking the padding
1134 * validity through timing. RSA keys are small enough that all the
1135 * size_t values involved fit in unsigned int. */
Gilles Peskine9f11f212018-10-04 18:32:29 +02001136 plaintext_size = if_int( bad,
1137 (unsigned) plaintext_max_size,
Gilles Peskinec5552e82018-10-05 14:50:21 +02001138 (unsigned) ( ilen - pad_count - 3 ) );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001139
Gilles Peskine20365082018-10-04 19:13:43 +02001140 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskine08513ce2018-10-04 21:24:21 +02001141 * buffer and to 1 otherwise. */
1142 output_too_large = size_greater_than( plaintext_size,
1143 plaintext_max_size );
Paul Bakker060c5682009-01-12 21:48:39 +00001144
Gilles Peskine20365082018-10-04 19:13:43 +02001145 /* Set ret without branches to avoid timing attacks. Return:
1146 * - INVALID_PADDING if the padding is bad (bad != 0).
1147 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1148 * plaintext does not fit in the output buffer.
1149 * - 0 if the padding is correct. */
1150 ret = - if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1151 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1152 0 ) );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001153
Gilles Peskine20365082018-10-04 19:13:43 +02001154 /* If the padding is bad or the plaintext is too large, zero the
1155 * data that we're about to copy to the output buffer.
1156 * We need to copy the same amount of data
Gilles Peskinef50ee602018-10-02 22:44:41 +02001157 * from the same buffer whether the padding is good or not to
1158 * avoid leaking the padding validity through overall timing or
1159 * through memory or cache access patterns. */
1160 for( i = 11; i < ilen; i++ )
Gilles Peskine20365082018-10-04 19:13:43 +02001161 buf[i] &= ~( bad | output_too_large );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001162
Gilles Peskine20365082018-10-04 19:13:43 +02001163 /* If the plaintext is too large, truncate it to the buffer size.
1164 * Copy anyway to avoid revealing the length through timing, because
1165 * revealing the length is as bad as revealing the padding validity
1166 * for a Bleichenbacher attack. */
1167 plaintext_size = if_int( output_too_large,
1168 (unsigned) plaintext_max_size,
1169 (unsigned) plaintext_size );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001170
Gilles Peskinef19aefb2018-10-04 22:45:13 +02001171 /* Move the plaintext to the leftmost position where it can start in
1172 * the working buffer, i.e. make it start plaintext_max_size from
1173 * the end of the buffer. Do this with a memory access trace that
1174 * does not depend on the plaintext size. After this move, the
1175 * starting location of the plaintext is no longer sensitive
1176 * information. */
Gilles Peskinec5552e82018-10-05 14:50:21 +02001177 mem_move_to_left( buf + ilen - plaintext_max_size,
1178 plaintext_max_size,
Gilles Peskinef19aefb2018-10-04 22:45:13 +02001179 plaintext_max_size - plaintext_size );
Gilles Peskine20365082018-10-04 19:13:43 +02001180
Gilles Peskinef19aefb2018-10-04 22:45:13 +02001181 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskine20365082018-10-04 19:13:43 +02001182 * into the output buffer. */
Gilles Peskinec5552e82018-10-05 14:50:21 +02001183 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskine20365082018-10-04 19:13:43 +02001184
1185 /* Report the amount of data we copied to the output buffer. In case
1186 * of errors (bad padding or output too large), the value of *olen
1187 * when this function returns is not specified. Making it equivalent
1188 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinef50ee602018-10-02 22:44:41 +02001189 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001190
Gilles Peskine8877ec22017-03-23 14:37:37 +01001191cleanup:
1192 mbedtls_zeroize( buf, sizeof( buf ) );
1193
1194 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001195}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001197
1198/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001199 * Do an RSA operation, then remove the message padding
1200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001202 int (*f_rng)(void *, unsigned char *, size_t),
1203 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001204 int mode, size_t *olen,
1205 const unsigned char *input,
1206 unsigned char *output,
1207 size_t output_max_len)
1208{
1209 switch( ctx->padding )
1210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#if defined(MBEDTLS_PKCS1_V15)
1212 case MBEDTLS_RSA_PKCS_V15:
1213 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001214 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001215#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_PKCS1_V21)
1218 case MBEDTLS_RSA_PKCS_V21:
1219 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001220 olen, input, output,
1221 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001222#endif
1223
1224 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001226 }
1227}
1228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001230/*
1231 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001234 int (*f_rng)(void *, unsigned char *, size_t),
1235 void *p_rng,
1236 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001238 unsigned int hashlen,
1239 const unsigned char *hash,
1240 unsigned char *sig )
1241{
1242 size_t olen;
1243 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001245 unsigned int slen, hlen, offset = 0;
1246 int ret;
1247 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 const mbedtls_md_info_t *md_info;
1249 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1252 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001253
1254 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001256
1257 olen = ctx->len;
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001260 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001261 // Gather length of hash to sign
1262 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001264 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001268 }
1269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001271 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001275 slen = hlen;
1276
1277 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001279
1280 memset( sig, 0, olen );
1281
Paul Bakkerb3869132013-02-28 17:21:01 +01001282 // Generate salt of length slen
1283 //
1284 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001286
1287 // Note: EMSA-PSS encoding is over the length of N - 1 bits
1288 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001289 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001290 p += olen - hlen * 2 - 2;
1291 *p++ = 0x01;
1292 memcpy( p, salt, slen );
1293 p += slen;
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -07001296 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1297 {
1298 mbedtls_md_free( &md_ctx );
Gilles Peskine74fd8682017-05-05 19:24:06 +02001299 /* No need to zeroize salt: we didn't use it. */
Brian J Murray88c2d222016-06-23 12:57:03 -07001300 return( ret );
1301 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001302
1303 // Generate H = Hash( M' )
1304 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 mbedtls_md_starts( &md_ctx );
1306 mbedtls_md_update( &md_ctx, p, 8 );
1307 mbedtls_md_update( &md_ctx, hash, hashlen );
1308 mbedtls_md_update( &md_ctx, salt, slen );
1309 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine74fd8682017-05-05 19:24:06 +02001310 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001311
1312 // Compensate for boundary condition when applying mask
1313 //
1314 if( msb % 8 == 0 )
1315 offset = 1;
1316
1317 // maskedDB: Apply dbMask to DB
1318 //
1319 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001322
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001323 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001324 sig[0] &= 0xFF >> ( olen * 8 - msb );
1325
1326 p += hlen;
1327 *p++ = 0xBC;
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 return( ( mode == MBEDTLS_RSA_PUBLIC )
1330 ? mbedtls_rsa_public( ctx, sig, sig )
1331 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001336/*
1337 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1338 */
1339/*
1340 * Do an RSA operation to sign the message digest
1341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001343 int (*f_rng)(void *, unsigned char *, size_t),
1344 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001345 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001347 unsigned int hashlen,
1348 const unsigned char *hash,
1349 unsigned char *sig )
1350{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001351 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001352 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001353 const char *oid = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1356 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001357
1358 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001359 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001364 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1368 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001369
Paul Bakkerc70b9822013-04-07 22:00:46 +02001370 nb_pad -= 10 + oid_size;
1371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001373 }
1374
Paul Bakkerc70b9822013-04-07 22:00:46 +02001375 nb_pad -= hashlen;
1376
Paul Bakkerb3869132013-02-28 17:21:01 +01001377 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001379
1380 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001382 memset( p, 0xFF, nb_pad );
1383 p += nb_pad;
1384 *p++ = 0;
1385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001387 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001388 memcpy( p, hash, hashlen );
1389 }
1390 else
1391 {
1392 /*
1393 * DigestInfo ::= SEQUENCE {
1394 * digestAlgorithm DigestAlgorithmIdentifier,
1395 * digest Digest }
1396 *
1397 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1398 *
1399 * Digest ::= OCTET STRING
1400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001402 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001404 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001406 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001407 memcpy( p, oid, oid_size );
1408 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001410 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001412 *p++ = hashlen;
1413 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001414 }
1415
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001416 if( mode == MBEDTLS_RSA_PUBLIC )
1417 return( mbedtls_rsa_public( ctx, sig, sig ) );
1418
Hanno Becker21f83752017-11-06 15:09:33 +00001419 return( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001422
1423/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 * Do an RSA operation to sign the message digest
1425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001427 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001428 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001431 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001432 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 unsigned char *sig )
1434{
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 switch( ctx->padding )
1436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#if defined(MBEDTLS_PKCS1_V15)
1438 case MBEDTLS_RSA_PKCS_V15:
1439 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001440 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001441#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#if defined(MBEDTLS_PKCS1_V21)
1444 case MBEDTLS_RSA_PKCS_V21:
1445 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001446 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001447#endif
1448
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001452}
1453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001455/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001456 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001459 int (*f_rng)(void *, unsigned char *, size_t),
1460 void *p_rng,
1461 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001463 unsigned int hashlen,
1464 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001466 int expected_salt_len,
1467 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001468{
Paul Bakker23986e52011-04-24 08:57:21 +00001469 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001470 size_t siglen;
1471 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskined0cd8552017-10-17 19:02:13 +02001473 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001475 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001476 unsigned int hlen;
Gilles Peskined0cd8552017-10-17 19:02:13 +02001477 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 const mbedtls_md_info_t *md_info;
1479 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1482 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001483
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 siglen = ctx->len;
1485
Paul Bakker27fdf462011-06-09 13:55:13 +00001486 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1490 ? mbedtls_rsa_public( ctx, sig, buf )
1491 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001492
1493 if( ret != 0 )
1494 return( ret );
1495
1496 p = buf;
1497
Paul Bakkerb3869132013-02-28 17:21:01 +01001498 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001503 // Gather length of hash to sign
1504 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001506 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001510 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001513 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001517
Paul Bakkerb3869132013-02-28 17:21:01 +01001518 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001519
Paul Bakkerb3869132013-02-28 17:21:01 +01001520 // Note: EMSA-PSS verification is over the length of N - 1 bits
1521 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001522 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001523
Gilles Peskine31a2d142017-10-19 15:23:49 +02001524 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1525 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1526
1527 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001528 if( msb % 8 == 0 )
1529 {
1530 p++;
1531 siglen -= 1;
1532 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001533
Gilles Peskine9e205822017-10-18 19:03:42 +02001534 if( siglen < hlen + 2 )
1535 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1536 hash_start = p + siglen - hlen - 1;
1537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -07001539 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1540 {
1541 mbedtls_md_free( &md_ctx );
1542 return( ret );
1543 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001544
Gilles Peskined0cd8552017-10-17 19:02:13 +02001545 mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001546
Paul Bakkerb3869132013-02-28 17:21:01 +01001547 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001548
Gilles Peskined0cd8552017-10-17 19:02:13 +02001549 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001550 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001551
Gilles Peskine9745cfd2017-10-19 17:46:14 +02001552 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 mbedtls_md_free( &md_ctx );
1555 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001556 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001557
Gilles Peskined0cd8552017-10-17 19:02:13 +02001558 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskined0cd8552017-10-17 19:02:13 +02001561 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 mbedtls_md_free( &md_ctx );
1564 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001565 }
1566
Paul Bakkerb3869132013-02-28 17:21:01 +01001567 // Generate H = Hash( M' )
1568 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 mbedtls_md_starts( &md_ctx );
1570 mbedtls_md_update( &md_ctx, zeros, 8 );
1571 mbedtls_md_update( &md_ctx, hash, hashlen );
Gilles Peskined0cd8552017-10-17 19:02:13 +02001572 mbedtls_md_update( &md_ctx, p, observed_salt_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001576
Gilles Peskined0cd8552017-10-17 19:02:13 +02001577 if( memcmp( hash_start, result, hlen ) == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001578 return( 0 );
1579 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001581}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001582
1583/*
1584 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001587 int (*f_rng)(void *, unsigned char *, size_t),
1588 void *p_rng,
1589 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001591 unsigned int hashlen,
1592 const unsigned char *hash,
1593 const unsigned char *sig )
1594{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1596 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001597 : md_alg;
1598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001600 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001602 sig ) );
1603
1604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001608/*
1609 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001612 int (*f_rng)(void *, unsigned char *, size_t),
1613 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001614 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001616 unsigned int hashlen,
1617 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001618 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001619{
1620 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001621 size_t len, siglen, asn1_len;
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001622 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1624 mbedtls_md_type_t msg_md_alg;
1625 const mbedtls_md_info_t *md_info;
1626 mbedtls_asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1629 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001630
1631 siglen = ctx->len;
1632
1633 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1637 ? mbedtls_rsa_public( ctx, sig, buf )
1638 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001639
1640 if( ret != 0 )
1641 return( ret );
1642
1643 p = buf;
1644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1646 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001647
1648 while( *p != 0 )
1649 {
1650 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001652 p++;
1653 }
Manuel Pégourié-Gonnard230ee312017-05-11 12:49:51 +02001654 p++; /* skip 00 byte */
1655
1656 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
1657 if( p - buf < 11 )
1658 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001659
1660 len = siglen - ( p - buf );
1661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001663 {
1664 if( memcmp( p, hash, hashlen ) == 0 )
1665 return( 0 );
1666 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 }
1669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001671 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1673 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001674
1675 end = p + len;
1676
Gilles Peskinebd908512017-05-04 12:48:39 +02001677 /*
1678 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
1679 * Insist on 2-byte length tags, to protect against variants of
1680 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
1681 */
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001682 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1684 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1685 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001686 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001688
Gilles Peskinebd908512017-05-04 12:48:39 +02001689 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1691 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1692 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001693 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001695
Gilles Peskinebd908512017-05-04 12:48:39 +02001696 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1698 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001699 if( p != p0 + 2 )
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001700 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001701
1702 oid.p = p;
1703 p += oid.len;
1704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1706 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001707
1708 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001710
1711 /*
1712 * assume the algorithm parameters must be NULL
1713 */
Gilles Peskinebd908512017-05-04 12:48:39 +02001714 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1716 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001717 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001719
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001720 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001721 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1722 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001723 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001725
1726 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001728
1729 p += hashlen;
1730
1731 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001733
1734 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001735}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001737
1738/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001739 * Do an RSA operation and check the message digest
1740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001742 int (*f_rng)(void *, unsigned char *, size_t),
1743 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001744 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001746 unsigned int hashlen,
1747 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001748 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001749{
1750 switch( ctx->padding )
1751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752#if defined(MBEDTLS_PKCS1_V15)
1753 case MBEDTLS_RSA_PKCS_V15:
1754 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001755 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001756#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758#if defined(MBEDTLS_PKCS1_V21)
1759 case MBEDTLS_RSA_PKCS_V21:
1760 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001761 hashlen, hash, sig );
1762#endif
1763
1764 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001766 }
1767}
1768
1769/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001770 * Copy the components of an RSA key
1771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001773{
1774 int ret;
1775
1776 dst->ver = src->ver;
1777 dst->len = src->len;
1778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1780 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1783 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1784 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1785 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1786 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1787 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1790 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1791 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1794 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001795
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001796 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001797 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001798
1799cleanup:
1800 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001802
1803 return( ret );
1804}
1805
1806/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 * Free the components of an RSA key
1808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001810{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1812 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1813 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1814 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1815 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817#if defined(MBEDTLS_THREADING_C)
1818 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001819#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001820}
1821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001823
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001824#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001825
1826/*
1827 * Example RSA-1024 keypair, for test purposes
1828 */
1829#define KEY_LEN 128
1830
1831#define RSA_N "9292758453063D803DD603D5E777D788" \
1832 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1833 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1834 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1835 "93A89813FBF3C4F8066D2D800F7C38A8" \
1836 "1AE31942917403FF4946B0A83D3D3E05" \
1837 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1838 "5E94BB77B07507233A0BC7BAC8F90F79"
1839
1840#define RSA_E "10001"
1841
1842#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1843 "66CA472BC44D253102F8B4A9D3BFA750" \
1844 "91386C0077937FE33FA3252D28855837" \
1845 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1846 "DF79C5CE07EE72C7F123142198164234" \
1847 "CABB724CF78B8173B9F880FC86322407" \
1848 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1849 "071513A1E85B5DFA031F21ECAE91A34D"
1850
1851#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1852 "2C01CAD19EA484A87EA4377637E75500" \
1853 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1854 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1855
1856#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1857 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1858 "910E4168387E3C30AA1E00C339A79508" \
1859 "8452DD96A9A5EA5D9DCA68DA636032AF"
1860
1861#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1862 "3C94D22288ACD763FD8E5600ED4A702D" \
1863 "F84198A5F06C2E72236AE490C93F07F8" \
1864 "3CC559CD27BC2D1CA488811730BB5725"
1865
1866#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1867 "D8AAEA56749EA28623272E4F7D0592AF" \
1868 "7C1F1313CAC9471B5C523BFE592F517B" \
1869 "407A1BD76C164B93DA2D32A383E58357"
1870
1871#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1872 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1873 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1874 "A74206CEC169D74BF5A8C50D6F48EA08"
1875
1876#define PT_LEN 24
1877#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1878 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001881static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001882{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001883#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001884 size_t i;
1885
Paul Bakker545570e2010-07-18 09:00:25 +00001886 if( rng_state != NULL )
1887 rng_state = NULL;
1888
Paul Bakkera3d195c2011-11-27 21:07:34 +00001889 for( i = 0; i < len; ++i )
1890 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001891#else
1892 if( rng_state != NULL )
1893 rng_state = NULL;
1894
1895 arc4random_buf( output, len );
1896#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001897
Paul Bakkera3d195c2011-11-27 21:07:34 +00001898 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001899}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001901
Paul Bakker5121ce52009-01-03 21:22:43 +00001902/*
1903 * Checkup routine
1904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001906{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001907 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001909 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 unsigned char rsa_plaintext[PT_LEN];
1912 unsigned char rsa_decrypted[PT_LEN];
1913 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001915 unsigned char sha1sum[20];
1916#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001919
1920 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1922 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1923 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1924 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1925 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1926 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1927 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1928 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001929
1930 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1934 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 {
1936 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001939 ret = 1;
1940 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 }
1942
1943 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
1946 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 rsa_plaintext, rsa_ciphertext ) != 0 )
1950 {
1951 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001953
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001954 ret = 1;
1955 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 }
1957
1958 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001962 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001963 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 {
1965 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001967
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001968 ret = 1;
1969 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 }
1971
1972 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1973 {
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
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001981 if( verbose != 0 )
1982 mbedtls_printf( "passed\n" );
1983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001985 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001986 mbedtls_printf( "PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001991 sha1sum, rsa_ciphertext ) != 0 )
1992 {
1993 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001995
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001996 ret = 1;
1997 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001998 }
1999
2000 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 sha1sum, rsa_ciphertext ) != 0 )
2005 {
2006 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
Hanno Beckerb81fcd02017-05-03 15:09:31 +01002009 ret = 1;
2010 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 }
2012
2013 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002014 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002016
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002017 if( verbose != 0 )
2018 mbedtls_printf( "\n" );
2019
Paul Bakker3d8fb632014-04-17 12:42:41 +02002020cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 mbedtls_rsa_free( &rsa );
2022#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002023 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002025 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002026}
2027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030#endif /* MBEDTLS_RSA_C */