blob: d531c26073bdbebe84b69192f47307b56e43d44c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
24 *
25 * http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
26 * http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
Janos Follath5d392572017-03-22 13:38:28 +000027 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
28 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
29 * Stefan Mangard
30 * https://arxiv.org/abs/1702.08719v2
31 *
Paul Bakker5121ce52009-01-03 21:22:43 +000032 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker40e46942009-01-03 21:51:57 +000042#include "polarssl/rsa.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020043#include "polarssl/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000044
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
46
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000047#if defined(POLARSSL_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +000048#include "polarssl/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Rich Evans00ab4702015-02-06 13:43:58 +000051#if defined(POLARSSL_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000052#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Paul Bakker7dc4c442014-02-01 22:50:26 +010055#if defined(POLARSSL_PLATFORM_C)
56#include "polarssl/platform.h"
57#else
Rich Evans00ab4702015-02-06 13:43:58 +000058#include <stdio.h>
Paul Bakker7dc4c442014-02-01 22:50:26 +010059#define polarssl_printf printf
Manuel Pégourié-Gonnarda1cdcd22015-09-03 20:03:15 +020060#define polarssl_malloc malloc
61#define polarssl_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#endif
63
Gilles Peskine213aec82017-03-23 14:37:37 +010064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker5121ce52009-01-03 21:22:43 +000069/*
70 * Initialize an RSA context
71 */
72void rsa_init( rsa_context *ctx,
73 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000074 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000075{
76 memset( ctx, 0, sizeof( rsa_context ) );
77
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010078 rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020079
80#if defined(POLARSSL_THREADING_C)
81 polarssl_mutex_init( &ctx->mutex );
82#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000083}
84
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010085/*
86 * Set padding for an existing RSA context
87 */
88void rsa_set_padding( rsa_context *ctx, int padding, int hash_id )
89{
90 ctx->padding = padding;
91 ctx->hash_id = hash_id;
92}
93
Paul Bakker40e46942009-01-03 21:51:57 +000094#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96/*
97 * Generate an RSA keypair
98 */
Paul Bakker21eb2802010-08-16 11:10:02 +000099int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000100 int (*f_rng)(void *, unsigned char *, size_t),
101 void *p_rng,
102 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000103{
104 int ret;
105 mpi P1, Q1, H, G;
106
Paul Bakker21eb2802010-08-16 11:10:02 +0000107 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Paul Bakker40e46942009-01-03 21:51:57 +0000108 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Janos Follathbfcd0322016-09-21 13:18:12 +0100110 if( nbits % 2 )
111 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
112
Janos Follathd61fc682016-02-23 14:42:48 +0000113 mpi_init( &P1 ); mpi_init( &Q1 );
114 mpi_init( &H ); mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116 /*
117 * find primes P and Q with Q < P so that:
118 * GCD( E, (P-1)*(Q-1) ) == 1
119 */
120 MPI_CHK( mpi_lset( &ctx->E, exponent ) );
121
122 do
123 {
Janos Follathd61fc682016-02-23 14:42:48 +0000124 MPI_CHK( mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000125 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Janos Follathbfcd0322016-09-21 13:18:12 +0100127 MPI_CHK( mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000128 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
130 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
131 continue;
132
133 MPI_CHK( mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
134 if( mpi_msb( &ctx->N ) != nbits )
135 continue;
136
Janos Follathbfcd0322016-09-21 13:18:12 +0100137 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
138 mpi_swap( &ctx->P, &ctx->Q );
139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
141 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
142 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
143 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
144 }
145 while( mpi_cmp_int( &G, 1 ) != 0 );
146
147 /*
148 * D = E^-1 mod ((P-1)*(Q-1))
149 * DP = D mod (P - 1)
150 * DQ = D mod (Q - 1)
151 * QP = Q^-1 mod P
152 */
153 MPI_CHK( mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
154 MPI_CHK( mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
155 MPI_CHK( mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
156 MPI_CHK( mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
157
158 ctx->len = ( mpi_msb( &ctx->N ) + 7 ) >> 3;
159
160cleanup:
161
Paul Bakker6c591fa2011-05-05 11:49:20 +0000162 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164 if( ret != 0 )
165 {
166 rsa_free( ctx );
Paul Bakker9d781402011-05-09 16:17:09 +0000167 return( POLARSSL_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 }
169
Paul Bakker48377d92013-08-30 12:06:24 +0200170 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171}
172
Paul Bakker9af723c2014-05-01 13:03:14 +0200173#endif /* POLARSSL_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175/*
176 * Check a public RSA key
177 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000178int rsa_check_pubkey( const rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000179{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000180 if( !ctx->N.p || !ctx->E.p )
181 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
182
Paul Bakker48377d92013-08-30 12:06:24 +0200183 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 ( ctx->E.p[0] & 1 ) == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000185 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
187 if( mpi_msb( &ctx->N ) < 128 ||
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000188 mpi_msb( &ctx->N ) > POLARSSL_MPI_MAX_BITS )
Paul Bakker40e46942009-01-03 21:51:57 +0000189 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191 if( mpi_msb( &ctx->E ) < 2 ||
Paul Bakker24f37cc2014-04-30 13:33:35 +0200192 mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000193 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195 return( 0 );
196}
197
198/*
199 * Check a private RSA key
200 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000201int rsa_check_privkey( const rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000202{
203 int ret;
Paul Bakker321df6f2012-09-27 13:21:34 +0000204 mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
206 if( ( ret = rsa_check_pubkey( ctx ) ) != 0 )
207 return( ret );
208
Paul Bakker37940d9f2009-07-10 22:38:58 +0000209 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
210 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
211
Paul Bakker6c591fa2011-05-05 11:49:20 +0000212 mpi_init( &PQ ); mpi_init( &DE ); mpi_init( &P1 ); mpi_init( &Q1 );
213 mpi_init( &H ); mpi_init( &I ); mpi_init( &G ); mpi_init( &G2 );
Paul Bakker321df6f2012-09-27 13:21:34 +0000214 mpi_init( &L1 ); mpi_init( &L2 ); mpi_init( &DP ); mpi_init( &DQ );
215 mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217 MPI_CHK( mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
218 MPI_CHK( mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
219 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
220 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
221 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
223
Paul Bakkerb572adf2010-07-18 08:29:32 +0000224 MPI_CHK( mpi_gcd( &G2, &P1, &Q1 ) );
Paul Bakker48377d92013-08-30 12:06:24 +0200225 MPI_CHK( mpi_div_mpi( &L1, &L2, &H, &G2 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000226 MPI_CHK( mpi_mod_mpi( &I, &DE, &L1 ) );
227
Paul Bakker321df6f2012-09-27 13:21:34 +0000228 MPI_CHK( mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
229 MPI_CHK( mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
230 MPI_CHK( mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000231 /*
232 * Check for a valid PKCS1v2 private key
233 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000234 if( mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
Paul Bakker321df6f2012-09-27 13:21:34 +0000235 mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
236 mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
237 mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
Paul Bakker6c591fa2011-05-05 11:49:20 +0000238 mpi_cmp_int( &L2, 0 ) != 0 ||
239 mpi_cmp_int( &I, 1 ) != 0 ||
240 mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000242 ret = POLARSSL_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 }
Paul Bakker48377d92013-08-30 12:06:24 +0200244
Paul Bakker5121ce52009-01-03 21:22:43 +0000245cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +0000246 mpi_free( &PQ ); mpi_free( &DE ); mpi_free( &P1 ); mpi_free( &Q1 );
247 mpi_free( &H ); mpi_free( &I ); mpi_free( &G ); mpi_free( &G2 );
Paul Bakker321df6f2012-09-27 13:21:34 +0000248 mpi_free( &L1 ); mpi_free( &L2 ); mpi_free( &DP ); mpi_free( &DQ );
249 mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000250
Paul Bakker9d781402011-05-09 16:17:09 +0000251 if( ret == POLARSSL_ERR_RSA_KEY_CHECK_FAILED )
252 return( ret );
253
Paul Bakker6c591fa2011-05-05 11:49:20 +0000254 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000255 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000256
257 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000258}
259
260/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100261 * Check if contexts holding a public and private key match
262 */
263int rsa_check_pub_priv( const rsa_context *pub, const rsa_context *prv )
264{
265 if( rsa_check_pubkey( pub ) != 0 ||
266 rsa_check_privkey( prv ) != 0 )
267 {
268 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
269 }
270
271 if( mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
272 mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
273 {
274 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
275 }
276
277 return( 0 );
278}
279
280/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 * Do an RSA public key operation
282 */
283int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000284 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 unsigned char *output )
286{
Paul Bakker23986e52011-04-24 08:57:21 +0000287 int ret;
288 size_t olen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 mpi T;
290
Paul Bakker6c591fa2011-05-05 11:49:20 +0000291 mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
Manuel Pégourié-Gonnard5efed092015-08-31 10:03:16 +0200293#if defined(POLARSSL_THREADING_C)
294 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
295 return( ret );
296#endif
297
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
299
300 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
301 {
Manuel Pégourié-Gonnard5efed092015-08-31 10:03:16 +0200302 ret = POLARSSL_ERR_MPI_BAD_INPUT_DATA;
303 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 }
305
306 olen = ctx->len;
307 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
308 MPI_CHK( mpi_write_binary( &T, output, olen ) );
309
310cleanup:
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100311#if defined(POLARSSL_THREADING_C)
Manuel Pégourié-Gonnard5efed092015-08-31 10:03:16 +0200312 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
313 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100314#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
Paul Bakker6c591fa2011-05-05 11:49:20 +0000316 mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000319 return( POLARSSL_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
321 return( 0 );
322}
323
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200324/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200325 * Generate or update blinding values, see section 10 of:
326 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
327 * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
328 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200329 */
Manuel Pégourié-Gonnard5efed092015-08-31 10:03:16 +0200330static int rsa_prepare_blinding( rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200331 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
332{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200333 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200334
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200335 if( ctx->Vf.p != NULL )
336 {
337 /* We already have blinding values, just update them by squaring */
338 MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200339 MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200340 MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200341 MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200342
Manuel Pégourié-Gonnard5efed092015-08-31 10:03:16 +0200343 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200344 }
345
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200346 /* Unblinding value: Vf = random number, invertible mod N */
347 do {
348 if( count++ > 10 )
349 return( POLARSSL_ERR_RSA_RNG_FAILED );
350
351 MPI_CHK( mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
352 MPI_CHK( mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
353 } while( mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200354
355 /* Blinding value: Vi = Vf^(-e) mod N */
356 MPI_CHK( mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
357 MPI_CHK( mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
358
359cleanup:
360 return( ret );
361}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200362
Paul Bakker5121ce52009-01-03 21:22:43 +0000363/*
Janos Follath5d392572017-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 */
387int rsa_private( 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;
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 mpi T, T1, T2;
Janos Follathbb1e6882017-03-22 15:13:15 +0000396 mpi P1, Q1, R;
Janos Follath5d392572017-03-22 13:38:28 +0000397#if defined(POLARSSL_RSA_NO_CRT)
Janos Follathbb1e6882017-03-22 15:13:15 +0000398 mpi D_blind;
Janos Follath5d392572017-03-22 13:38:28 +0000399 mpi *D = &ctx->D;
Janos Follathbb1e6882017-03-22 15:13:15 +0000400#else
401 mpi DP_blind, DQ_blind;
402 mpi *DP = &ctx->DP;
403 mpi *DQ = &ctx->DQ;
Janos Follath5d392572017-03-22 13:38:28 +0000404#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
Paul Bakker6c591fa2011-05-05 11:49:20 +0000406 mpi_init( &T ); mpi_init( &T1 ); mpi_init( &T2 );
Janos Follathbb1e6882017-03-22 15:13:15 +0000407 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &R );
408
Janos Follathbb1e6882017-03-22 15:13:15 +0000409 if( f_rng != NULL )
410 {
Janos Follath5d392572017-03-22 13:38:28 +0000411#if defined(POLARSSL_RSA_NO_CRT)
Janos Follathbb1e6882017-03-22 15:13:15 +0000412 mpi_init( &D_blind );
413#else
414 mpi_init( &DP_blind );
415 mpi_init( &DQ_blind );
Janos Follath5d392572017-03-22 13:38:28 +0000416#endif
Janos Follathbb1e6882017-03-22 15:13:15 +0000417 }
Janos Follath5d392572017-03-22 13:38:28 +0000418
Manuel Pégourié-Gonnard5efed092015-08-31 10:03:16 +0200419#if defined(POLARSSL_THREADING_C)
Manuel Pégourié-Gonnard139708d2017-05-11 15:10:32 +0200420 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
Manuel Pégourié-Gonnard5efed092015-08-31 10:03:16 +0200421 return( ret );
422#endif
423
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
426 {
Manuel Pégourié-Gonnard5efed092015-08-31 10:03:16 +0200427 ret = POLARSSL_ERR_MPI_BAD_INPUT_DATA;
428 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 }
430
Paul Bakkerf451bac2013-08-30 15:37:02 +0200431 if( f_rng != NULL )
432 {
433 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200434 * Blinding
435 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200436 */
Manuel Pégourié-Gonnard5efed092015-08-31 10:03:16 +0200437 MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
438 MPI_CHK( mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200439 MPI_CHK( mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follath5d392572017-03-22 13:38:28 +0000440
Janos Follath5d392572017-03-22 13:38:28 +0000441 /*
442 * Exponent blinding
443 */
444 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
445 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
446
Janos Follathbb1e6882017-03-22 15:13:15 +0000447#if defined(POLARSSL_RSA_NO_CRT)
Janos Follath5d392572017-03-22 13:38:28 +0000448 /*
449 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
450 */
451 MPI_CHK( mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
452 f_rng, p_rng ) );
453 MPI_CHK( mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
454 MPI_CHK( mpi_mul_mpi( &D_blind, &D_blind, &R ) );
455 MPI_CHK( mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
456
457 D = &D_blind;
Janos Follathbb1e6882017-03-22 15:13:15 +0000458#else
459 /*
460 * DP_blind = ( P - 1 ) * R + DP
461 */
462 MPI_CHK( mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
463 f_rng, p_rng ) );
464 MPI_CHK( mpi_mul_mpi( &DP_blind, &P1, &R ) );
465 MPI_CHK( mpi_add_mpi( &DP_blind, &DP_blind,
466 &ctx->DP ) );
467
468 DP = &DP_blind;
469
470 /*
471 * DQ_blind = ( Q - 1 ) * R + DQ
472 */
473 MPI_CHK( mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
474 f_rng, p_rng ) );
475 MPI_CHK( mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
476 MPI_CHK( mpi_add_mpi( &DQ_blind, &DQ_blind,
477 &ctx->DQ ) );
478
479 DQ = &DQ_blind;
Janos Follath5d392572017-03-22 13:38:28 +0000480#endif /* POLARSSL_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200481 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200482
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100483#if defined(POLARSSL_RSA_NO_CRT)
Janos Follath5d392572017-03-22 13:38:28 +0000484 MPI_CHK( mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100485#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200486 /*
Janos Follath5d392572017-03-22 13:38:28 +0000487 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 *
489 * T1 = input ^ dP mod P
490 * T2 = input ^ dQ mod Q
491 */
Janos Follathbb1e6882017-03-22 15:13:15 +0000492 MPI_CHK( mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
493 MPI_CHK( mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495 /*
496 * T = (T1 - T2) * (Q^-1 mod P) mod P
497 */
498 MPI_CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
499 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
500 MPI_CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
501
502 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200503 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 */
505 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200506 MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100507#endif /* POLARSSL_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200508
Paul Bakkerf451bac2013-08-30 15:37:02 +0200509 if( f_rng != NULL )
510 {
511 /*
512 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200513 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200514 */
Manuel Pégourié-Gonnard5efed092015-08-31 10:03:16 +0200515 MPI_CHK( mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200516 MPI_CHK( mpi_mod_mpi( &T, &T, &ctx->N ) );
517 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
519 olen = ctx->len;
520 MPI_CHK( mpi_write_binary( &T, output, olen ) );
521
522cleanup:
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100523#if defined(POLARSSL_THREADING_C)
Manuel Pégourié-Gonnard5efed092015-08-31 10:03:16 +0200524 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
525 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200526#endif
Manuel Pégourié-Gonnard5efed092015-08-31 10:03:16 +0200527
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100528 mpi_free( &T ); mpi_free( &T1 ); mpi_free( &T2 );
Janos Follathbb1e6882017-03-22 15:13:15 +0000529 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &R );
530
531 if( f_rng != NULL )
532 {
Janos Follath5d392572017-03-22 13:38:28 +0000533#if defined(POLARSSL_RSA_NO_CRT)
Janos Follathbb1e6882017-03-22 15:13:15 +0000534 mpi_free( &D_blind );
535#else
536 mpi_free( &DP_blind );
537 mpi_free( &DQ_blind );
Janos Follath5d392572017-03-22 13:38:28 +0000538#endif
Janos Follathbb1e6882017-03-22 15:13:15 +0000539 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
541 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000542 return( POLARSSL_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
544 return( 0 );
545}
546
Paul Bakker9dcc3222011-03-08 14:16:06 +0000547#if defined(POLARSSL_PKCS1_V21)
548/**
549 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
550 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000551 * \param dst buffer to mask
552 * \param dlen length of destination buffer
553 * \param src source of the mask generation
554 * \param slen length of the source buffer
555 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000556 */
Paul Bakker48377d92013-08-30 12:06:24 +0200557static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
558 size_t slen, md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000559{
560 unsigned char mask[POLARSSL_MD_MAX_SIZE];
561 unsigned char counter[4];
562 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000563 unsigned int hlen;
564 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000565
566 memset( mask, 0, POLARSSL_MD_MAX_SIZE );
567 memset( counter, 0, 4 );
568
569 hlen = md_ctx->md_info->size;
570
571 // Generate and apply dbMask
572 //
573 p = dst;
574
575 while( dlen > 0 )
576 {
577 use_len = hlen;
578 if( dlen < hlen )
579 use_len = dlen;
580
581 md_starts( md_ctx );
582 md_update( md_ctx, src, slen );
583 md_update( md_ctx, counter, 4 );
584 md_finish( md_ctx, mask );
585
586 for( i = 0; i < use_len; ++i )
587 *p++ ^= mask[i];
588
589 counter[3]++;
590
591 dlen -= use_len;
592 }
Gilles Peskine73e7f4c2017-05-05 19:24:06 +0200593
594 polarssl_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000595}
Paul Bakker9af723c2014-05-01 13:03:14 +0200596#endif /* POLARSSL_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000597
Paul Bakkerb3869132013-02-28 17:21:01 +0100598#if defined(POLARSSL_PKCS1_V21)
599/*
600 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
601 */
602int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
603 int (*f_rng)(void *, unsigned char *, size_t),
604 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100605 int mode,
606 const unsigned char *label, size_t label_len,
607 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100608 const unsigned char *input,
609 unsigned char *output )
610{
611 size_t olen;
612 int ret;
613 unsigned char *p = output;
614 unsigned int hlen;
615 const md_info_t *md_info;
616 md_context_t md_ctx;
617
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200618 if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V21 )
619 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
620
621 if( f_rng == NULL )
Paul Bakkerb3869132013-02-28 17:21:01 +0100622 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
623
Manuel Pégourié-Gonnarda2733712015-02-10 17:32:14 +0100624 md_info = md_info_from_type( (md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100625 if( md_info == NULL )
626 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
627
628 olen = ctx->len;
629 hlen = md_get_size( md_info );
630
Janos Follath742783f2016-02-08 14:52:29 +0000631 // first comparison checks for overflow
632 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Paul Bakkerb3869132013-02-28 17:21:01 +0100633 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
634
635 memset( output, 0, olen );
636
637 *p++ = 0;
638
639 // Generate a random octet string seed
640 //
641 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
642 return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
643
644 p += hlen;
645
646 // Construct DB
647 //
Paul Bakkera43231c2013-02-28 17:33:49 +0100648 md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100649 p += hlen;
650 p += olen - 2 * hlen - 2 - ilen;
651 *p++ = 1;
652 memcpy( p, input, ilen );
653
Paul Bakker84bbeb52014-07-01 14:53:22 +0200654 md_init( &md_ctx );
Brian J Murray4556d202016-06-23 12:57:03 -0700655 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
656 {
657 md_free( &md_ctx );
658 return( ret );
659 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100660
661 // maskedDB: Apply dbMask to DB
662 //
663 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
664 &md_ctx );
665
666 // maskedSeed: Apply seedMask to seed
667 //
668 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
669 &md_ctx );
670
Paul Bakker84bbeb52014-07-01 14:53:22 +0200671 md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100672
673 return( ( mode == RSA_PUBLIC )
674 ? rsa_public( ctx, output, output )
Paul Bakker548957d2013-08-30 10:30:02 +0200675 : rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100676}
677#endif /* POLARSSL_PKCS1_V21 */
678
Paul Bakker48377d92013-08-30 12:06:24 +0200679#if defined(POLARSSL_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100680/*
681 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
682 */
683int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
684 int (*f_rng)(void *, unsigned char *, size_t),
685 void *p_rng,
686 int mode, size_t ilen,
687 const unsigned char *input,
688 unsigned char *output )
689{
690 size_t nb_pad, olen;
691 int ret;
692 unsigned char *p = output;
693
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200694 if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V15 )
695 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
696
Janos Follath7ddc2cd2016-03-18 11:45:44 +0000697 // We don't check p_rng because it won't be dereferenced here
698 if( f_rng == NULL || input == NULL || output == NULL )
Paul Bakkerb3869132013-02-28 17:21:01 +0100699 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
700
701 olen = ctx->len;
702
Janos Follath742783f2016-02-08 14:52:29 +0000703 // first comparison checks for overflow
704 if( ilen + 11 < ilen || olen < ilen + 11 )
Paul Bakkerb3869132013-02-28 17:21:01 +0100705 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
706
707 nb_pad = olen - 3 - ilen;
708
709 *p++ = 0;
710 if( mode == RSA_PUBLIC )
711 {
712 *p++ = RSA_CRYPT;
713
714 while( nb_pad-- > 0 )
715 {
716 int rng_dl = 100;
717
718 do {
719 ret = f_rng( p_rng, p, 1 );
720 } while( *p == 0 && --rng_dl && ret == 0 );
721
722 // Check if RNG failed to generate data
723 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200724 if( rng_dl == 0 || ret != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200725 return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100726
727 p++;
728 }
729 }
730 else
731 {
732 *p++ = RSA_SIGN;
733
734 while( nb_pad-- > 0 )
735 *p++ = 0xFF;
736 }
737
738 *p++ = 0;
739 memcpy( p, input, ilen );
740
741 return( ( mode == RSA_PUBLIC )
742 ? rsa_public( ctx, output, output )
Paul Bakker548957d2013-08-30 10:30:02 +0200743 : rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100744}
Paul Bakker48377d92013-08-30 12:06:24 +0200745#endif /* POLARSSL_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100746
Paul Bakker5121ce52009-01-03 21:22:43 +0000747/*
748 * Add the message padding, then do an RSA operation
749 */
750int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000751 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000752 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000753 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000754 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000755 unsigned char *output )
756{
Paul Bakker5121ce52009-01-03 21:22:43 +0000757 switch( ctx->padding )
758 {
Paul Bakker48377d92013-08-30 12:06:24 +0200759#if defined(POLARSSL_PKCS1_V15)
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 case RSA_PKCS_V15:
Paul Bakkerb3869132013-02-28 17:21:01 +0100761 return rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
762 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200763#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000764
Paul Bakker9dcc3222011-03-08 14:16:06 +0000765#if defined(POLARSSL_PKCS1_V21)
766 case RSA_PKCS_V21:
Paul Bakkerb3869132013-02-28 17:21:01 +0100767 return rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
768 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000769#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
771 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000772 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000774}
775
Paul Bakkerb3869132013-02-28 17:21:01 +0100776#if defined(POLARSSL_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000777/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100778 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100780int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200781 int (*f_rng)(void *, unsigned char *, size_t),
782 void *p_rng,
783 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100784 const unsigned char *label, size_t label_len,
785 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100786 const unsigned char *input,
787 unsigned char *output,
788 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000789{
Paul Bakker23986e52011-04-24 08:57:21 +0000790 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100791 size_t ilen, i, pad_len;
792 unsigned char *p, bad, pad_done;
Paul Bakker0be82f22012-10-03 20:36:33 +0000793 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000794 unsigned char lhash[POLARSSL_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000795 unsigned int hlen;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000796 const md_info_t *md_info;
797 md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100798
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100799 /*
800 * Parameters sanity checks
801 */
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200802 if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V21 )
Paul Bakkerb3869132013-02-28 17:21:01 +0100803 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000804
805 ilen = ctx->len;
806
Paul Bakker27fdf462011-06-09 13:55:13 +0000807 if( ilen < 16 || ilen > sizeof( buf ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000808 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000809
Manuel Pégourié-Gonnarda2733712015-02-10 17:32:14 +0100810 md_info = md_info_from_type( (md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100811 if( md_info == NULL )
812 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
813
Simon Butcherd3253b02016-03-17 00:57:18 +0000814 hlen = md_get_size( md_info );
Janos Follath092f2c42016-02-11 11:08:18 +0000815
816 // checking for integer underflow
817 if( 2 * hlen + 2 > ilen )
Simon Butcherd3253b02016-03-17 00:57:18 +0000818 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Janos Follath092f2c42016-02-11 11:08:18 +0000819
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100820 /*
821 * RSA operation
822 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 ret = ( mode == RSA_PUBLIC )
824 ? rsa_public( ctx, input, buf )
Paul Bakker548957d2013-08-30 10:30:02 +0200825 : rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000826
827 if( ret != 0 )
Gilles Peskine213aec82017-03-23 14:37:37 +0100828 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100830 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100831 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100832 */
833 hlen = md_get_size( md_info );
834
Janos Follath3bed13d2016-02-09 14:51:35 +0000835 // checking for integer underflow
836 if( 2 * hlen + 2 > ilen )
837 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
838
Simon Butcherd3253b02016-03-17 00:57:18 +0000839 md_init( &md_ctx );
Brian J Murray4556d202016-06-23 12:57:03 -0700840 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
841 {
842 md_free( &md_ctx );
843 return( ret );
844 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100845
846 /* Generate lHash */
847 md( md_info, label, label_len, lhash );
848
849 /* seed: Apply seedMask to maskedSeed */
850 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
851 &md_ctx );
852
853 /* DB: Apply dbMask to maskedDB */
854 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
855 &md_ctx );
856
Paul Bakker84bbeb52014-07-01 14:53:22 +0200857 md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100858
859 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100860 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100861 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100863 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100865 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100866
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100867 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100868
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100869 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100870 for( i = 0; i < hlen; i++ )
871 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100872
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100873 /* Get zero-padding len, but always read till end of buffer
874 * (minus one, for the 01 byte) */
875 pad_len = 0;
876 pad_done = 0;
877 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
878 {
879 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100880 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100881 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100882
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100883 p += pad_len;
884 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100885
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100886 /*
887 * The only information "leaked" is whether the padding was correct or not
888 * (eg, no data is copied if it was not correct). This meets the
889 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
890 * the different error conditions.
891 */
892 if( bad != 0 )
Gilles Peskine213aec82017-03-23 14:37:37 +0100893 {
894 ret = POLARSSL_ERR_RSA_INVALID_PADDING;
895 goto cleanup;
896 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100897
Paul Bakker66d5d072014-06-17 16:39:18 +0200898 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine213aec82017-03-23 14:37:37 +0100899 {
900 ret = POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE;
901 goto cleanup;
902 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100903
904 *olen = ilen - (p - buf);
905 memcpy( output, p, *olen );
Gilles Peskine213aec82017-03-23 14:37:37 +0100906 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100907
Gilles Peskine213aec82017-03-23 14:37:37 +0100908cleanup:
909 polarssl_zeroize( buf, sizeof( buf ) );
910 polarssl_zeroize( lhash, sizeof( lhash ) );
911
912 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100913}
914#endif /* POLARSSL_PKCS1_V21 */
915
Paul Bakker48377d92013-08-30 12:06:24 +0200916#if defined(POLARSSL_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100917/*
918 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
919 */
920int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200921 int (*f_rng)(void *, unsigned char *, size_t),
922 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100923 int mode, size_t *olen,
924 const unsigned char *input,
925 unsigned char *output,
926 size_t output_max_len)
927{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100928 int ret;
929 size_t ilen, pad_count = 0, i;
930 unsigned char *p, bad, pad_done = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100931 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
932
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200933 if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V15 )
Paul Bakkerb3869132013-02-28 17:21:01 +0100934 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
935
936 ilen = ctx->len;
937
938 if( ilen < 16 || ilen > sizeof( buf ) )
939 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
940
941 ret = ( mode == RSA_PUBLIC )
942 ? rsa_public( ctx, input, buf )
Paul Bakker548957d2013-08-30 10:30:02 +0200943 : rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100944
945 if( ret != 0 )
Gilles Peskine213aec82017-03-23 14:37:37 +0100946 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +0100947
948 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100949 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100950
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100951 /*
952 * Check and get padding len in "constant-time"
953 */
954 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100955
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100956 /* This test does not depend on secret data */
957 if( mode == RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000958 {
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100959 bad |= *p++ ^ RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100961 /* Get padding len, but always read till end of buffer
962 * (minus one, for the 00 byte) */
963 for( i = 0; i < ilen - 3; i++ )
964 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100965 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
966 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100967 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000968
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100969 p += pad_count;
970 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100971 }
972 else
973 {
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100974 bad |= *p++ ^ RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100975
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100976 /* Get padding len, but always read till end of buffer
977 * (minus one, for the 00 byte) */
978 for( i = 0; i < ilen - 3; i++ )
979 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100980 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100981 pad_count += ( pad_done == 0 );
982 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100983
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100984 p += pad_count;
985 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 }
987
Janos Follathf18263d2016-02-12 13:30:09 +0000988 bad |= ( pad_count < 8 );
Janos Follathf570f7f2016-02-08 13:59:25 +0000989
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100990 if( bad )
Gilles Peskine213aec82017-03-23 14:37:37 +0100991 {
992 ret = POLARSSL_ERR_RSA_INVALID_PADDING;
993 goto cleanup;
994 }
Paul Bakker8804f692013-02-28 18:06:26 +0100995
Paul Bakker66d5d072014-06-17 16:39:18 +0200996 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine213aec82017-03-23 14:37:37 +0100997 {
998 ret = POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE;
999 goto cleanup;
1000 }
Paul Bakker060c5682009-01-12 21:48:39 +00001001
Paul Bakker27fdf462011-06-09 13:55:13 +00001002 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 memcpy( output, p, *olen );
Gilles Peskine213aec82017-03-23 14:37:37 +01001004 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001005
Gilles Peskine213aec82017-03-23 14:37:37 +01001006cleanup:
1007 polarssl_zeroize( buf, sizeof( buf ) );
1008
1009 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010}
Paul Bakker48377d92013-08-30 12:06:24 +02001011#endif /* POLARSSL_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
1013/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001014 * Do an RSA operation, then remove the message padding
1015 */
1016int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001017 int (*f_rng)(void *, unsigned char *, size_t),
1018 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001019 int mode, size_t *olen,
1020 const unsigned char *input,
1021 unsigned char *output,
1022 size_t output_max_len)
1023{
1024 switch( ctx->padding )
1025 {
Paul Bakker48377d92013-08-30 12:06:24 +02001026#if defined(POLARSSL_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001027 case RSA_PKCS_V15:
Paul Bakker548957d2013-08-30 10:30:02 +02001028 return rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
1029 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001030#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001031
1032#if defined(POLARSSL_PKCS1_V21)
1033 case RSA_PKCS_V21:
Paul Bakker548957d2013-08-30 10:30:02 +02001034 return rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
1035 olen, input, output,
1036 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001037#endif
1038
1039 default:
1040 return( POLARSSL_ERR_RSA_INVALID_PADDING );
1041 }
1042}
1043
1044#if defined(POLARSSL_PKCS1_V21)
1045/*
1046 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1047 */
1048int rsa_rsassa_pss_sign( rsa_context *ctx,
1049 int (*f_rng)(void *, unsigned char *, size_t),
1050 void *p_rng,
1051 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +02001052 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001053 unsigned int hashlen,
1054 const unsigned char *hash,
1055 unsigned char *sig )
1056{
1057 size_t olen;
1058 unsigned char *p = sig;
1059 unsigned char salt[POLARSSL_MD_MAX_SIZE];
1060 unsigned int slen, hlen, offset = 0;
1061 int ret;
1062 size_t msb;
1063 const md_info_t *md_info;
1064 md_context_t md_ctx;
1065
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001066 if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V21 )
1067 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1068
1069 if( f_rng == NULL )
Paul Bakkerb3869132013-02-28 17:21:01 +01001070 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1071
1072 olen = ctx->len;
1073
Paul Bakkerc70b9822013-04-07 22:00:46 +02001074 if( md_alg != POLARSSL_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001075 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001076 // Gather length of hash to sign
1077 //
1078 md_info = md_info_from_type( md_alg );
1079 if( md_info == NULL )
Paul Bakkerb3869132013-02-28 17:21:01 +01001080 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001081
1082 hashlen = md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001083 }
1084
Manuel Pégourié-Gonnarda2733712015-02-10 17:32:14 +01001085 md_info = md_info_from_type( (md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001086 if( md_info == NULL )
1087 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1088
1089 hlen = md_get_size( md_info );
1090 slen = hlen;
1091
1092 if( olen < hlen + slen + 2 )
1093 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1094
1095 memset( sig, 0, olen );
1096
Paul Bakkerb3869132013-02-28 17:21:01 +01001097 // Generate salt of length slen
1098 //
1099 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
1100 return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
1101
1102 // Note: EMSA-PSS encoding is over the length of N - 1 bits
1103 //
1104 msb = mpi_msb( &ctx->N ) - 1;
1105 p += olen - hlen * 2 - 2;
1106 *p++ = 0x01;
1107 memcpy( p, salt, slen );
1108 p += slen;
1109
Paul Bakker84bbeb52014-07-01 14:53:22 +02001110 md_init( &md_ctx );
Brian J Murray4556d202016-06-23 12:57:03 -07001111 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
1112 {
1113 md_free( &md_ctx );
Gilles Peskine73e7f4c2017-05-05 19:24:06 +02001114 /* No need to zeroize salt: we didn't use it. */
Brian J Murray4556d202016-06-23 12:57:03 -07001115 return( ret );
1116 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001117
1118 // Generate H = Hash( M' )
1119 //
1120 md_starts( &md_ctx );
1121 md_update( &md_ctx, p, 8 );
1122 md_update( &md_ctx, hash, hashlen );
1123 md_update( &md_ctx, salt, slen );
1124 md_finish( &md_ctx, p );
Gilles Peskine73e7f4c2017-05-05 19:24:06 +02001125 polarssl_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001126
1127 // Compensate for boundary condition when applying mask
1128 //
1129 if( msb % 8 == 0 )
1130 offset = 1;
1131
1132 // maskedDB: Apply dbMask to DB
1133 //
1134 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1135
Paul Bakker84bbeb52014-07-01 14:53:22 +02001136 md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001137
1138 msb = mpi_msb( &ctx->N ) - 1;
1139 sig[0] &= 0xFF >> ( olen * 8 - msb );
1140
1141 p += hlen;
1142 *p++ = 0xBC;
1143
1144 return( ( mode == RSA_PUBLIC )
1145 ? rsa_public( ctx, sig, sig )
Paul Bakker548957d2013-08-30 10:30:02 +02001146 : rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001147}
1148#endif /* POLARSSL_PKCS1_V21 */
1149
Paul Bakker48377d92013-08-30 12:06:24 +02001150#if defined(POLARSSL_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001151/*
1152 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1153 */
1154/*
1155 * Do an RSA operation to sign the message digest
1156 */
1157int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001158 int (*f_rng)(void *, unsigned char *, size_t),
1159 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001160 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +02001161 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001162 unsigned int hashlen,
1163 const unsigned char *hash,
1164 unsigned char *sig )
1165{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001166 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001167 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001168 const char *oid = NULL;
Manuel Pégourié-Gonnarda1cdcd22015-09-03 20:03:15 +02001169 unsigned char *sig_try = NULL, *verif = NULL;
1170 size_t i;
1171 unsigned char diff;
1172 volatile unsigned char diff_no_optimize;
1173 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001174
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001175 if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V15 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001176 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1177
1178 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001179 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001180
Paul Bakkerc70b9822013-04-07 22:00:46 +02001181 if( md_alg != POLARSSL_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001182 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001183 const md_info_t *md_info = md_info_from_type( md_alg );
1184 if( md_info == NULL )
Paul Bakkerb3869132013-02-28 17:21:01 +01001185 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001186
Paul Bakker1c3853b2013-09-10 11:43:44 +02001187 if( oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
Paul Bakkerc70b9822013-04-07 22:00:46 +02001188 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1189
Paul Bakkerc70b9822013-04-07 22:00:46 +02001190 nb_pad -= 10 + oid_size;
1191
1192 hashlen = md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001193 }
1194
Paul Bakkerc70b9822013-04-07 22:00:46 +02001195 nb_pad -= hashlen;
1196
Paul Bakkerb3869132013-02-28 17:21:01 +01001197 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
1198 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1199
1200 *p++ = 0;
1201 *p++ = RSA_SIGN;
1202 memset( p, 0xFF, nb_pad );
1203 p += nb_pad;
1204 *p++ = 0;
1205
Paul Bakkerc70b9822013-04-07 22:00:46 +02001206 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001207 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001208 memcpy( p, hash, hashlen );
1209 }
1210 else
1211 {
1212 /*
1213 * DigestInfo ::= SEQUENCE {
1214 * digestAlgorithm DigestAlgorithmIdentifier,
1215 * digest Digest }
1216 *
1217 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1218 *
1219 * Digest ::= OCTET STRING
1220 */
1221 *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001222 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001223 *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001224 *p++ = (unsigned char) ( 0x04 + oid_size );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001225 *p++ = ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001226 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001227 memcpy( p, oid, oid_size );
1228 p += oid_size;
1229 *p++ = ASN1_NULL;
1230 *p++ = 0x00;
1231 *p++ = ASN1_OCTET_STRING;
1232 *p++ = hashlen;
1233 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001234 }
1235
Manuel Pégourié-Gonnarda1cdcd22015-09-03 20:03:15 +02001236 if( mode == RSA_PUBLIC )
1237 return( rsa_public( ctx, sig, sig ) );
1238
1239 /*
1240 * In order to prevent Lenstra's attack, make the signature in a
1241 * temporary buffer and check it before returning it.
1242 */
1243 sig_try = polarssl_malloc( ctx->len );
Simon Butcher7d3f3a82016-01-02 00:03:39 +00001244 if( sig_try == NULL )
Manuel Pégourié-Gonnarda1cdcd22015-09-03 20:03:15 +02001245 return( POLARSSL_ERR_MPI_MALLOC_FAILED );
1246
Simon Butcher7d3f3a82016-01-02 00:03:39 +00001247 verif = polarssl_malloc( ctx->len );
1248 if( verif == NULL )
1249 {
1250 polarssl_free( sig_try );
1251 return( POLARSSL_ERR_MPI_MALLOC_FAILED );
1252 }
1253
Manuel Pégourié-Gonnarda1cdcd22015-09-03 20:03:15 +02001254 MPI_CHK( rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1255 MPI_CHK( rsa_public( ctx, sig_try, verif ) );
1256
1257 /* Compare in constant time just in case */
1258 for( diff = 0, i = 0; i < ctx->len; i++ )
1259 diff |= verif[i] ^ sig[i];
1260 diff_no_optimize = diff;
1261
1262 if( diff_no_optimize != 0 )
1263 {
1264 ret = POLARSSL_ERR_RSA_PRIVATE_FAILED;
1265 goto cleanup;
1266 }
1267
1268 memcpy( sig, sig_try, ctx->len );
1269
1270cleanup:
1271 polarssl_free( sig_try );
1272 polarssl_free( verif );
1273
1274 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001275}
Paul Bakker48377d92013-08-30 12:06:24 +02001276#endif /* POLARSSL_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001277
1278/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 * Do an RSA operation to sign the message digest
1280 */
1281int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001282 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001283 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +02001285 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001286 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001287 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 unsigned char *sig )
1289{
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 switch( ctx->padding )
1291 {
Paul Bakker48377d92013-08-30 12:06:24 +02001292#if defined(POLARSSL_PKCS1_V15)
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 case RSA_PKCS_V15:
Paul Bakker548957d2013-08-30 10:30:02 +02001294 return rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001295 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001297
Paul Bakker9dcc3222011-03-08 14:16:06 +00001298#if defined(POLARSSL_PKCS1_V21)
1299 case RSA_PKCS_V21:
Paul Bakkerc70b9822013-04-07 22:00:46 +02001300 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001301 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001302#endif
1303
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 default:
Paul Bakker40e46942009-01-03 21:51:57 +00001305 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001307}
1308
Paul Bakkerb3869132013-02-28 17:21:01 +01001309#if defined(POLARSSL_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001310/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001311 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 */
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001313int rsa_rsassa_pss_verify_ext( rsa_context *ctx,
1314 int (*f_rng)(void *, unsigned char *, size_t),
1315 void *p_rng,
1316 int mode,
1317 md_type_t md_alg,
1318 unsigned int hashlen,
1319 const unsigned char *hash,
1320 md_type_t mgf1_hash_id,
1321 int expected_salt_len,
1322 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001323{
Paul Bakker23986e52011-04-24 08:57:21 +00001324 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001325 size_t siglen;
1326 unsigned char *p;
Paul Bakker0be82f22012-10-03 20:36:33 +00001327 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker1fe7d9b2011-11-15 15:26:03 +00001328 unsigned char result[POLARSSL_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001329 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001330 unsigned int hlen;
1331 size_t slen, msb;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001332 const md_info_t *md_info;
1333 md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001334
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001335 if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V21 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001336 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1337
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 siglen = ctx->len;
1339
Paul Bakker27fdf462011-06-09 13:55:13 +00001340 if( siglen < 16 || siglen > sizeof( buf ) )
Paul Bakker40e46942009-01-03 21:51:57 +00001341 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
1343 ret = ( mode == RSA_PUBLIC )
1344 ? rsa_public( ctx, sig, buf )
Paul Bakker548957d2013-08-30 10:30:02 +02001345 : rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
1347 if( ret != 0 )
1348 return( ret );
1349
1350 p = buf;
1351
Paul Bakkerb3869132013-02-28 17:21:01 +01001352 if( buf[siglen - 1] != 0xBC )
1353 return( POLARSSL_ERR_RSA_INVALID_PADDING );
1354
Paul Bakkerc70b9822013-04-07 22:00:46 +02001355 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001357 // Gather length of hash to sign
1358 //
1359 md_info = md_info_from_type( md_alg );
1360 if( md_info == NULL )
Paul Bakkerb3869132013-02-28 17:21:01 +01001361 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001362
1363 hashlen = md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001364 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001365
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001366 md_info = md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001367 if( md_info == NULL )
1368 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001369
Paul Bakkerb3869132013-02-28 17:21:01 +01001370 hlen = md_get_size( md_info );
Gilles Peskine55db24c2017-10-17 19:01:38 +02001371 if( siglen < hlen + 2 )
1372 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001373 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001374
Paul Bakkerb3869132013-02-28 17:21:01 +01001375 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001376
Paul Bakkerb3869132013-02-28 17:21:01 +01001377 // Note: EMSA-PSS verification is over the length of N - 1 bits
1378 //
1379 msb = mpi_msb( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001380
Paul Bakkerb3869132013-02-28 17:21:01 +01001381 // Compensate for boundary condition when applying mask
1382 //
1383 if( msb % 8 == 0 )
1384 {
1385 p++;
1386 siglen -= 1;
1387 }
1388 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1389 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001390
Paul Bakker84bbeb52014-07-01 14:53:22 +02001391 md_init( &md_ctx );
Brian J Murray4556d202016-06-23 12:57:03 -07001392 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
1393 {
1394 md_free( &md_ctx );
1395 return( ret );
1396 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001397
Paul Bakkerb3869132013-02-28 17:21:01 +01001398 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001399
Paul Bakkerb3869132013-02-28 17:21:01 +01001400 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001401
Paul Bakker4de44aa2013-12-31 11:43:01 +01001402 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001403 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001404
Paul Bakkerb3869132013-02-28 17:21:01 +01001405 if( p == buf + siglen ||
1406 *p++ != 0x01 )
1407 {
Paul Bakker84bbeb52014-07-01 14:53:22 +02001408 md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001409 return( POLARSSL_ERR_RSA_INVALID_PADDING );
1410 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001411
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001412 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001413 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001414
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001415 if( expected_salt_len != RSA_SALT_LEN_ANY &&
1416 slen != (size_t) expected_salt_len )
1417 {
Paul Bakker84bbeb52014-07-01 14:53:22 +02001418 md_free( &md_ctx );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001419 return( POLARSSL_ERR_RSA_INVALID_PADDING );
1420 }
1421
Paul Bakkerb3869132013-02-28 17:21:01 +01001422 // Generate H = Hash( M' )
1423 //
1424 md_starts( &md_ctx );
1425 md_update( &md_ctx, zeros, 8 );
1426 md_update( &md_ctx, hash, hashlen );
1427 md_update( &md_ctx, p, slen );
1428 md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001429
Paul Bakker84bbeb52014-07-01 14:53:22 +02001430 md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001431
Paul Bakkerb3869132013-02-28 17:21:01 +01001432 if( memcmp( p + slen, result, hlen ) == 0 )
1433 return( 0 );
1434 else
1435 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1436}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001437
1438/*
1439 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1440 */
1441int rsa_rsassa_pss_verify( rsa_context *ctx,
1442 int (*f_rng)(void *, unsigned char *, size_t),
1443 void *p_rng,
1444 int mode,
1445 md_type_t md_alg,
1446 unsigned int hashlen,
1447 const unsigned char *hash,
1448 const unsigned char *sig )
1449{
1450 md_type_t mgf1_hash_id = ( ctx->hash_id != POLARSSL_MD_NONE )
Manuel Pégourié-Gonnard0eaa8be2014-06-05 18:07:20 +02001451 ? (md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001452 : md_alg;
1453
1454 return( rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
1455 md_alg, hashlen, hash,
1456 mgf1_hash_id, RSA_SALT_LEN_ANY,
1457 sig ) );
1458
1459}
Paul Bakkerb3869132013-02-28 17:21:01 +01001460#endif /* POLARSSL_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001461
Paul Bakker48377d92013-08-30 12:06:24 +02001462#if defined(POLARSSL_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001463/*
1464 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1465 */
1466int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001467 int (*f_rng)(void *, unsigned char *, size_t),
1468 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001469 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +02001470 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001471 unsigned int hashlen,
1472 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001473 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001474{
1475 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001476 size_t len, siglen, asn1_len;
Gilles Peskine6de05fa2017-05-03 18:32:21 +02001477 unsigned char *p, *p0, *end;
Paul Bakkerb3869132013-02-28 17:21:01 +01001478 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001479 md_type_t msg_md_alg;
1480 const md_info_t *md_info;
1481 asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001482
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001483 if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V15 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001484 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1485
1486 siglen = ctx->len;
1487
1488 if( siglen < 16 || siglen > sizeof( buf ) )
1489 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1490
1491 ret = ( mode == RSA_PUBLIC )
1492 ? rsa_public( ctx, sig, buf )
Paul Bakker548957d2013-08-30 10:30:02 +02001493 : rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001494
1495 if( ret != 0 )
1496 return( ret );
1497
1498 p = buf;
1499
1500 if( *p++ != 0 || *p++ != RSA_SIGN )
1501 return( POLARSSL_ERR_RSA_INVALID_PADDING );
1502
1503 while( *p != 0 )
1504 {
1505 if( p >= buf + siglen - 1 || *p != 0xFF )
1506 return( POLARSSL_ERR_RSA_INVALID_PADDING );
1507 p++;
1508 }
Manuel Pégourié-Gonnard19c10e92017-05-11 12:49:51 +02001509 p++; /* skip 00 byte */
1510
1511 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
1512 if( p - buf < 11 )
1513 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001514
1515 len = siglen - ( p - buf );
1516
Paul Bakkerc70b9822013-04-07 22:00:46 +02001517 if( len == hashlen && md_alg == POLARSSL_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001518 {
1519 if( memcmp( p, hash, hashlen ) == 0 )
1520 return( 0 );
1521 else
1522 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 }
1524
Paul Bakkerc70b9822013-04-07 22:00:46 +02001525 md_info = md_info_from_type( md_alg );
1526 if( md_info == NULL )
1527 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1528 hashlen = md_get_size( md_info );
1529
1530 end = p + len;
1531
Gilles Peskine6e598a22017-05-04 12:48:39 +02001532 /*
1533 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
1534 * Insist on 2-byte length tags, to protect against variants of
1535 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
1536 */
Gilles Peskine6de05fa2017-05-03 18:32:21 +02001537 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001538 if( ( ret = asn1_get_tag( &p, end, &asn1_len,
1539 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1540 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Gilles Peskine6de05fa2017-05-03 18:32:21 +02001541 if( p != p0 + 2 || asn1_len + 2 != len )
Paul Bakkerc70b9822013-04-07 22:00:46 +02001542 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1543
Gilles Peskine6e598a22017-05-04 12:48:39 +02001544 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001545 if( ( ret = asn1_get_tag( &p, end, &asn1_len,
1546 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1547 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Gilles Peskine6e598a22017-05-04 12:48:39 +02001548 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Paul Bakkerc70b9822013-04-07 22:00:46 +02001549 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1550
Gilles Peskine6e598a22017-05-04 12:48:39 +02001551 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001552 if( ( ret = asn1_get_tag( &p, end, &oid.len, ASN1_OID ) ) != 0 )
1553 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Gilles Peskine6e598a22017-05-04 12:48:39 +02001554 if( p != p0 + 2 )
Gilles Peskine6de05fa2017-05-03 18:32:21 +02001555 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001556
1557 oid.p = p;
1558 p += oid.len;
1559
1560 if( oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1561 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1562
1563 if( md_alg != msg_md_alg )
1564 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1565
1566 /*
1567 * assume the algorithm parameters must be NULL
1568 */
Gilles Peskine6e598a22017-05-04 12:48:39 +02001569 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001570 if( ( ret = asn1_get_tag( &p, end, &asn1_len, ASN1_NULL ) ) != 0 )
1571 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Gilles Peskine6e598a22017-05-04 12:48:39 +02001572 if( p != p0 + 2 )
Paul Bakkerc70b9822013-04-07 22:00:46 +02001573 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1574
Gilles Peskine6de05fa2017-05-03 18:32:21 +02001575 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001576 if( ( ret = asn1_get_tag( &p, end, &asn1_len, ASN1_OCTET_STRING ) ) != 0 )
1577 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Gilles Peskine6de05fa2017-05-03 18:32:21 +02001578 if( p != p0 + 2 || asn1_len != hashlen )
Paul Bakkerc70b9822013-04-07 22:00:46 +02001579 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1580
1581 if( memcmp( p, hash, hashlen ) != 0 )
1582 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1583
1584 p += hashlen;
1585
1586 if( p != end )
1587 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1588
1589 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001590}
Paul Bakker48377d92013-08-30 12:06:24 +02001591#endif /* POLARSSL_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001592
1593/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001594 * Do an RSA operation and check the message digest
1595 */
1596int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001597 int (*f_rng)(void *, unsigned char *, size_t),
1598 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001599 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +02001600 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001601 unsigned int hashlen,
1602 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001603 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001604{
1605 switch( ctx->padding )
1606 {
Paul Bakker48377d92013-08-30 12:06:24 +02001607#if defined(POLARSSL_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001608 case RSA_PKCS_V15:
Paul Bakker548957d2013-08-30 10:30:02 +02001609 return rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001610 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001611#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001612
1613#if defined(POLARSSL_PKCS1_V21)
1614 case RSA_PKCS_V21:
Paul Bakker548957d2013-08-30 10:30:02 +02001615 return rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001616 hashlen, hash, sig );
1617#endif
1618
1619 default:
1620 return( POLARSSL_ERR_RSA_INVALID_PADDING );
1621 }
1622}
1623
1624/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001625 * Copy the components of an RSA key
1626 */
1627int rsa_copy( rsa_context *dst, const rsa_context *src )
1628{
1629 int ret;
1630
1631 dst->ver = src->ver;
1632 dst->len = src->len;
1633
1634 MPI_CHK( mpi_copy( &dst->N, &src->N ) );
1635 MPI_CHK( mpi_copy( &dst->E, &src->E ) );
1636
1637 MPI_CHK( mpi_copy( &dst->D, &src->D ) );
1638 MPI_CHK( mpi_copy( &dst->P, &src->P ) );
1639 MPI_CHK( mpi_copy( &dst->Q, &src->Q ) );
1640 MPI_CHK( mpi_copy( &dst->DP, &src->DP ) );
1641 MPI_CHK( mpi_copy( &dst->DQ, &src->DQ ) );
1642 MPI_CHK( mpi_copy( &dst->QP, &src->QP ) );
1643
1644 MPI_CHK( mpi_copy( &dst->RN, &src->RN ) );
1645 MPI_CHK( mpi_copy( &dst->RP, &src->RP ) );
1646 MPI_CHK( mpi_copy( &dst->RQ, &src->RQ ) );
1647
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001648 MPI_CHK( mpi_copy( &dst->Vi, &src->Vi ) );
1649 MPI_CHK( mpi_copy( &dst->Vf, &src->Vf ) );
1650
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001651 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001652 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001653
1654cleanup:
1655 if( ret != 0 )
1656 rsa_free( dst );
1657
1658 return( ret );
1659}
1660
1661/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001662 * Free the components of an RSA key
1663 */
1664void rsa_free( rsa_context *ctx )
1665{
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001666 mpi_free( &ctx->Vi ); mpi_free( &ctx->Vf );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001667 mpi_free( &ctx->RQ ); mpi_free( &ctx->RP ); mpi_free( &ctx->RN );
1668 mpi_free( &ctx->QP ); mpi_free( &ctx->DQ ); mpi_free( &ctx->DP );
1669 mpi_free( &ctx->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D );
1670 mpi_free( &ctx->E ); mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001671
1672#if defined(POLARSSL_THREADING_C)
1673 polarssl_mutex_free( &ctx->mutex );
1674#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001675}
1676
Paul Bakker40e46942009-01-03 21:51:57 +00001677#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
Paul Bakker40e46942009-01-03 21:51:57 +00001679#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
1681/*
1682 * Example RSA-1024 keypair, for test purposes
1683 */
1684#define KEY_LEN 128
1685
1686#define RSA_N "9292758453063D803DD603D5E777D788" \
1687 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1688 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1689 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1690 "93A89813FBF3C4F8066D2D800F7C38A8" \
1691 "1AE31942917403FF4946B0A83D3D3E05" \
1692 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1693 "5E94BB77B07507233A0BC7BAC8F90F79"
1694
1695#define RSA_E "10001"
1696
1697#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1698 "66CA472BC44D253102F8B4A9D3BFA750" \
1699 "91386C0077937FE33FA3252D28855837" \
1700 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1701 "DF79C5CE07EE72C7F123142198164234" \
1702 "CABB724CF78B8173B9F880FC86322407" \
1703 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1704 "071513A1E85B5DFA031F21ECAE91A34D"
1705
1706#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1707 "2C01CAD19EA484A87EA4377637E75500" \
1708 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1709 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1710
1711#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1712 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1713 "910E4168387E3C30AA1E00C339A79508" \
1714 "8452DD96A9A5EA5D9DCA68DA636032AF"
1715
1716#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1717 "3C94D22288ACD763FD8E5600ED4A702D" \
1718 "F84198A5F06C2E72236AE490C93F07F8" \
1719 "3CC559CD27BC2D1CA488811730BB5725"
1720
1721#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1722 "D8AAEA56749EA28623272E4F7D0592AF" \
1723 "7C1F1313CAC9471B5C523BFE592F517B" \
1724 "407A1BD76C164B93DA2D32A383E58357"
1725
1726#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1727 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1728 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1729 "A74206CEC169D74BF5A8C50D6F48EA08"
1730
1731#define PT_LEN 24
1732#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1733 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1734
Paul Bakkerfef3c5a2013-12-11 13:36:30 +01001735#if defined(POLARSSL_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001736static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001737{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001738#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001739 size_t i;
1740
Paul Bakker545570e2010-07-18 09:00:25 +00001741 if( rng_state != NULL )
1742 rng_state = NULL;
1743
Paul Bakkera3d195c2011-11-27 21:07:34 +00001744 for( i = 0; i < len; ++i )
1745 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001746#else
1747 if( rng_state != NULL )
1748 rng_state = NULL;
1749
1750 arc4random_buf( output, len );
1751#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001752
Paul Bakkera3d195c2011-11-27 21:07:34 +00001753 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001754}
Paul Bakker9af723c2014-05-01 13:03:14 +02001755#endif /* POLARSSL_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001756
Paul Bakker5121ce52009-01-03 21:22:43 +00001757/*
1758 * Checkup routine
1759 */
1760int rsa_self_test( int verbose )
1761{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001762 int ret = 0;
Paul Bakkerfef3c5a2013-12-11 13:36:30 +01001763#if defined(POLARSSL_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001764 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001766 unsigned char rsa_plaintext[PT_LEN];
1767 unsigned char rsa_decrypted[PT_LEN];
1768 unsigned char rsa_ciphertext[KEY_LEN];
Paul Bakker5690efc2011-05-26 13:16:06 +00001769#if defined(POLARSSL_SHA1_C)
1770 unsigned char sha1sum[20];
1771#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
Paul Bakker21eb2802010-08-16 11:10:02 +00001773 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
1775 rsa.len = KEY_LEN;
Paul Bakker3d8fb632014-04-17 12:42:41 +02001776 MPI_CHK( mpi_read_string( &rsa.N , 16, RSA_N ) );
1777 MPI_CHK( mpi_read_string( &rsa.E , 16, RSA_E ) );
1778 MPI_CHK( mpi_read_string( &rsa.D , 16, RSA_D ) );
1779 MPI_CHK( mpi_read_string( &rsa.P , 16, RSA_P ) );
1780 MPI_CHK( mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1781 MPI_CHK( mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1782 MPI_CHK( mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1783 MPI_CHK( mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
1785 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001786 polarssl_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
1788 if( rsa_check_pubkey( &rsa ) != 0 ||
1789 rsa_check_privkey( &rsa ) != 0 )
1790 {
1791 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001792 polarssl_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
1794 return( 1 );
1795 }
1796
1797 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001798 polarssl_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001799
1800 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1801
Paul Bakker548957d2013-08-30 10:30:02 +02001802 if( rsa_pkcs1_encrypt( &rsa, myrand, NULL, RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001803 rsa_plaintext, rsa_ciphertext ) != 0 )
1804 {
1805 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001806 polarssl_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001807
1808 return( 1 );
1809 }
1810
1811 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001812 polarssl_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001813
Paul Bakker548957d2013-08-30 10:30:02 +02001814 if( rsa_pkcs1_decrypt( &rsa, myrand, NULL, RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001815 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001816 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001817 {
1818 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001819 polarssl_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
1821 return( 1 );
1822 }
1823
1824 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1825 {
1826 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001827 polarssl_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001828
1829 return( 1 );
1830 }
1831
Paul Bakker5690efc2011-05-26 13:16:06 +00001832#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001834 polarssl_printf( "passed\n PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001835
1836 sha1( rsa_plaintext, PT_LEN, sha1sum );
1837
Paul Bakkeraab30c12013-08-30 11:00:25 +02001838 if( rsa_pkcs1_sign( &rsa, myrand, NULL, RSA_PRIVATE, POLARSSL_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 sha1sum, rsa_ciphertext ) != 0 )
1840 {
1841 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001842 polarssl_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
1844 return( 1 );
1845 }
1846
1847 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001848 polarssl_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001849
Paul Bakker548957d2013-08-30 10:30:02 +02001850 if( rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC, POLARSSL_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001851 sha1sum, rsa_ciphertext ) != 0 )
1852 {
1853 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001854 polarssl_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
1856 return( 1 );
1857 }
1858
1859 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001860 polarssl_printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00001861#endif /* POLARSSL_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Paul Bakker3d8fb632014-04-17 12:42:41 +02001863cleanup:
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 rsa_free( &rsa );
Paul Bakker48377d92013-08-30 12:06:24 +02001865#else /* POLARSSL_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001866 ((void) verbose);
Paul Bakker48377d92013-08-30 12:06:24 +02001867#endif /* POLARSSL_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001868 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001869}
1870
Paul Bakker9af723c2014-05-01 13:03:14 +02001871#endif /* POLARSSL_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001872
Paul Bakker9af723c2014-05-01 13:03:14 +02001873#endif /* POLARSSL_RSA_C */