blob: 614b23c84b80cdf6ea3fb5da349d413c9124d8b4 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
27 *
28 * http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
29 * http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
30 */
31
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#include "polarssl/rsa.h"
Paul Bakker9dcc3222011-03-08 14:16:06 +000037#include "polarssl/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
39#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000040#include <stdio.h>
41
42/*
43 * Initialize an RSA context
44 */
45void rsa_init( rsa_context *ctx,
46 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000047 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000048{
49 memset( ctx, 0, sizeof( rsa_context ) );
50
51 ctx->padding = padding;
52 ctx->hash_id = hash_id;
Paul Bakker5121ce52009-01-03 21:22:43 +000053}
54
Paul Bakker40e46942009-01-03 21:51:57 +000055#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000056
57/*
58 * Generate an RSA keypair
59 */
Paul Bakker21eb2802010-08-16 11:10:02 +000060int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +000061 int (*f_rng)(void *, unsigned char *, size_t),
62 void *p_rng,
63 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +000064{
65 int ret;
66 mpi P1, Q1, H, G;
67
Paul Bakker21eb2802010-08-16 11:10:02 +000068 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Paul Bakker40e46942009-01-03 21:51:57 +000069 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakker6c591fa2011-05-05 11:49:20 +000071 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +000072
73 /*
74 * find primes P and Q with Q < P so that:
75 * GCD( E, (P-1)*(Q-1) ) == 1
76 */
77 MPI_CHK( mpi_lset( &ctx->E, exponent ) );
78
79 do
80 {
81 MPI_CHK( mpi_gen_prime( &ctx->P, ( nbits + 1 ) >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +000082 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000083
84 MPI_CHK( mpi_gen_prime( &ctx->Q, ( nbits + 1 ) >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +000085 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
88 mpi_swap( &ctx->P, &ctx->Q );
89
90 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
91 continue;
92
93 MPI_CHK( mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
94 if( mpi_msb( &ctx->N ) != nbits )
95 continue;
96
97 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
98 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
99 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
100 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
101 }
102 while( mpi_cmp_int( &G, 1 ) != 0 );
103
104 /*
105 * D = E^-1 mod ((P-1)*(Q-1))
106 * DP = D mod (P - 1)
107 * DQ = D mod (Q - 1)
108 * QP = Q^-1 mod P
109 */
110 MPI_CHK( mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
111 MPI_CHK( mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
112 MPI_CHK( mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
113 MPI_CHK( mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
114
115 ctx->len = ( mpi_msb( &ctx->N ) + 7 ) >> 3;
116
117cleanup:
118
Paul Bakker6c591fa2011-05-05 11:49:20 +0000119 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 if( ret != 0 )
122 {
123 rsa_free( ctx );
Paul Bakker9d781402011-05-09 16:17:09 +0000124 return( POLARSSL_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 }
126
127 return( 0 );
128}
129
130#endif
131
132/*
133 * Check a public RSA key
134 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000135int rsa_check_pubkey( const rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000136{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000137 if( !ctx->N.p || !ctx->E.p )
138 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 if( ( ctx->N.p[0] & 1 ) == 0 ||
141 ( ctx->E.p[0] & 1 ) == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000142 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144 if( mpi_msb( &ctx->N ) < 128 ||
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000145 mpi_msb( &ctx->N ) > POLARSSL_MPI_MAX_BITS )
Paul Bakker40e46942009-01-03 21:51:57 +0000146 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
148 if( mpi_msb( &ctx->E ) < 2 ||
149 mpi_msb( &ctx->E ) > 64 )
Paul Bakker40e46942009-01-03 21:51:57 +0000150 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 return( 0 );
153}
154
155/*
156 * Check a private RSA key
157 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000158int rsa_check_privkey( const rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000159{
160 int ret;
Paul Bakkerb572adf2010-07-18 08:29:32 +0000161 mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 if( ( ret = rsa_check_pubkey( ctx ) ) != 0 )
164 return( ret );
165
Paul Bakker37940d9f2009-07-10 22:38:58 +0000166 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
167 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
168
Paul Bakker6c591fa2011-05-05 11:49:20 +0000169 mpi_init( &PQ ); mpi_init( &DE ); mpi_init( &P1 ); mpi_init( &Q1 );
170 mpi_init( &H ); mpi_init( &I ); mpi_init( &G ); mpi_init( &G2 );
171 mpi_init( &L1 ); mpi_init( &L2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173 MPI_CHK( mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
174 MPI_CHK( mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
175 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
176 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
177 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
179
Paul Bakkerb572adf2010-07-18 08:29:32 +0000180 MPI_CHK( mpi_gcd( &G2, &P1, &Q1 ) );
181 MPI_CHK( mpi_div_mpi( &L1, &L2, &H, &G2 ) );
182 MPI_CHK( mpi_mod_mpi( &I, &DE, &L1 ) );
183
184 /*
185 * Check for a valid PKCS1v2 private key
186 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000187 if( mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
188 mpi_cmp_int( &L2, 0 ) != 0 ||
189 mpi_cmp_int( &I, 1 ) != 0 ||
190 mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000192 ret = POLARSSL_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 }
194
Paul Bakkerb572adf2010-07-18 08:29:32 +0000195
Paul Bakker5121ce52009-01-03 21:22:43 +0000196cleanup:
197
Paul Bakker6c591fa2011-05-05 11:49:20 +0000198 mpi_free( &PQ ); mpi_free( &DE ); mpi_free( &P1 ); mpi_free( &Q1 );
199 mpi_free( &H ); mpi_free( &I ); mpi_free( &G ); mpi_free( &G2 );
200 mpi_free( &L1 ); mpi_free( &L2 );
201
Paul Bakker9d781402011-05-09 16:17:09 +0000202 if( ret == POLARSSL_ERR_RSA_KEY_CHECK_FAILED )
203 return( ret );
204
Paul Bakker6c591fa2011-05-05 11:49:20 +0000205 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000206 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000207
208 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209}
210
211/*
212 * Do an RSA public key operation
213 */
214int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000215 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 unsigned char *output )
217{
Paul Bakker23986e52011-04-24 08:57:21 +0000218 int ret;
219 size_t olen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 mpi T;
221
Paul Bakker6c591fa2011-05-05 11:49:20 +0000222 mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
225
226 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
227 {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000228 mpi_free( &T );
Paul Bakker40e46942009-01-03 21:51:57 +0000229 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 }
231
232 olen = ctx->len;
233 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
234 MPI_CHK( mpi_write_binary( &T, output, olen ) );
235
236cleanup:
237
Paul Bakker6c591fa2011-05-05 11:49:20 +0000238 mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
240 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000241 return( POLARSSL_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
243 return( 0 );
244}
245
246/*
247 * Do an RSA private key operation
248 */
249int rsa_private( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000250 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 unsigned char *output )
252{
Paul Bakker23986e52011-04-24 08:57:21 +0000253 int ret;
254 size_t olen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 mpi T, T1, T2;
256
Paul Bakker6c591fa2011-05-05 11:49:20 +0000257 mpi_init( &T ); mpi_init( &T1 ); mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
259 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
260
261 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
262 {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000263 mpi_free( &T );
Paul Bakker40e46942009-01-03 21:51:57 +0000264 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 }
266
Paul Bakker0216cc12011-03-26 13:40:23 +0000267#if defined(POLARSSL_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
269#else
270 /*
271 * faster decryption using the CRT
272 *
273 * T1 = input ^ dP mod P
274 * T2 = input ^ dQ mod Q
275 */
276 MPI_CHK( mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
277 MPI_CHK( mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
278
279 /*
280 * T = (T1 - T2) * (Q^-1 mod P) mod P
281 */
282 MPI_CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
283 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
284 MPI_CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
285
286 /*
287 * output = T2 + T * Q
288 */
289 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
290 MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) );
291#endif
292
293 olen = ctx->len;
294 MPI_CHK( mpi_write_binary( &T, output, olen ) );
295
296cleanup:
297
Paul Bakker6c591fa2011-05-05 11:49:20 +0000298 mpi_free( &T ); mpi_free( &T1 ); mpi_free( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000301 return( POLARSSL_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
303 return( 0 );
304}
305
Paul Bakker9dcc3222011-03-08 14:16:06 +0000306#if defined(POLARSSL_PKCS1_V21)
307/**
308 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
309 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000310 * \param dst buffer to mask
311 * \param dlen length of destination buffer
312 * \param src source of the mask generation
313 * \param slen length of the source buffer
314 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000315 */
Paul Bakker23986e52011-04-24 08:57:21 +0000316static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src, size_t slen,
Paul Bakker9dcc3222011-03-08 14:16:06 +0000317 md_context_t *md_ctx )
318{
319 unsigned char mask[POLARSSL_MD_MAX_SIZE];
320 unsigned char counter[4];
321 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000322 unsigned int hlen;
323 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000324
325 memset( mask, 0, POLARSSL_MD_MAX_SIZE );
326 memset( counter, 0, 4 );
327
328 hlen = md_ctx->md_info->size;
329
330 // Generate and apply dbMask
331 //
332 p = dst;
333
334 while( dlen > 0 )
335 {
336 use_len = hlen;
337 if( dlen < hlen )
338 use_len = dlen;
339
340 md_starts( md_ctx );
341 md_update( md_ctx, src, slen );
342 md_update( md_ctx, counter, 4 );
343 md_finish( md_ctx, mask );
344
345 for( i = 0; i < use_len; ++i )
346 *p++ ^= mask[i];
347
348 counter[3]++;
349
350 dlen -= use_len;
351 }
352}
353#endif
354
Paul Bakker5121ce52009-01-03 21:22:43 +0000355/*
356 * Add the message padding, then do an RSA operation
357 */
358int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000359 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000360 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000361 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000362 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 unsigned char *output )
364{
Paul Bakkere2e36d32012-01-23 09:56:51 +0000365 size_t nb_pad, olen;
366 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 unsigned char *p = output;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000368#if defined(POLARSSL_PKCS1_V21)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000369 unsigned int hlen;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000370 const md_info_t *md_info;
371 md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000372#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
374 olen = ctx->len;
375
Paul Bakker9dcc3222011-03-08 14:16:06 +0000376 if( f_rng == NULL )
377 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
378
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 switch( ctx->padding )
380 {
381 case RSA_PKCS_V15:
382
Paul Bakker23986e52011-04-24 08:57:21 +0000383 if( olen < ilen + 11 )
Paul Bakker40e46942009-01-03 21:51:57 +0000384 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
386 nb_pad = olen - 3 - ilen;
387
388 *p++ = 0;
Paul Bakker5f5593a2013-01-16 13:26:56 +0100389 if( mode == RSA_PUBLIC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 {
Paul Bakker5f5593a2013-01-16 13:26:56 +0100391 *p++ = RSA_CRYPT;
Paul Bakkerb572adf2010-07-18 08:29:32 +0000392
Paul Bakker5f5593a2013-01-16 13:26:56 +0100393 while( nb_pad-- > 0 )
394 {
395 int rng_dl = 100;
Paul Bakkerb572adf2010-07-18 08:29:32 +0000396
Paul Bakker5f5593a2013-01-16 13:26:56 +0100397 do {
398 ret = f_rng( p_rng, p, 1 );
399 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000400
Paul Bakker5f5593a2013-01-16 13:26:56 +0100401 // Check if RNG failed to generate data
402 //
403 if( rng_dl == 0 || ret != 0)
404 return POLARSSL_ERR_RSA_RNG_FAILED + ret;
405
406 p++;
407 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 }
Paul Bakker5f5593a2013-01-16 13:26:56 +0100409 else
410 {
411 *p++ = RSA_SIGN;
412
413 while( nb_pad-- > 0 )
414 *p++ = 0xFF;
415 }
416
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 *p++ = 0;
418 memcpy( p, input, ilen );
419 break;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000420
421#if defined(POLARSSL_PKCS1_V21)
422 case RSA_PKCS_V21:
423
424 md_info = md_info_from_type( ctx->hash_id );
425 if( md_info == NULL )
426 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
427
428 hlen = md_get_size( md_info );
429
Paul Bakker23986e52011-04-24 08:57:21 +0000430 if( olen < ilen + 2 * hlen + 2 || f_rng == NULL )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000431 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
432
433 memset( output, 0, olen );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000434
435 *p++ = 0;
436
437 // Generate a random octet string seed
438 //
Paul Bakkera3d195c2011-11-27 21:07:34 +0000439 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
440 return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
441
442 p += hlen;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000443
444 // Construct DB
445 //
446 md( md_info, p, 0, p );
447 p += hlen;
448 p += olen - 2 * hlen - 2 - ilen;
449 *p++ = 1;
450 memcpy( p, input, ilen );
451
Paul Bakkerc0484932013-01-03 10:50:31 +0100452 md_init_ctx( &md_ctx, md_info );
453
Paul Bakker9dcc3222011-03-08 14:16:06 +0000454 // maskedDB: Apply dbMask to DB
455 //
456 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
457 &md_ctx );
458
459 // maskedSeed: Apply seedMask to seed
460 //
461 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
462 &md_ctx );
Paul Bakkerc0484932013-01-03 10:50:31 +0100463
464 md_free_ctx( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000465 break;
466#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468 default:
469
Paul Bakker40e46942009-01-03 21:51:57 +0000470 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 }
472
473 return( ( mode == RSA_PUBLIC )
474 ? rsa_public( ctx, output, output )
475 : rsa_private( ctx, output, output ) );
476}
477
478/*
479 * Do an RSA operation, then remove the message padding
480 */
481int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000482 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000483 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000484 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000485 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000486{
Paul Bakker23986e52011-04-24 08:57:21 +0000487 int ret;
488 size_t ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 unsigned char *p;
Paul Bakker5f5593a2013-01-16 13:26:56 +0100490 unsigned char bt;
Paul Bakkercde51572009-05-17 10:11:56 +0000491 unsigned char buf[1024];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000492#if defined(POLARSSL_PKCS1_V21)
493 unsigned char lhash[POLARSSL_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000494 unsigned int hlen;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000495 const md_info_t *md_info;
496 md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
499 ilen = ctx->len;
500
Paul Bakker27fdf462011-06-09 13:55:13 +0000501 if( ilen < 16 || ilen > sizeof( buf ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000502 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 ret = ( mode == RSA_PUBLIC )
505 ? rsa_public( ctx, input, buf )
506 : rsa_private( ctx, input, buf );
507
508 if( ret != 0 )
509 return( ret );
510
511 p = buf;
512
513 switch( ctx->padding )
514 {
515 case RSA_PKCS_V15:
516
Paul Bakker5f5593a2013-01-16 13:26:56 +0100517 if( *p++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000518 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5f5593a2013-01-16 13:26:56 +0100519
520 bt = *p++;
521 if( ( bt != RSA_CRYPT && mode == RSA_PRIVATE ) ||
522 ( bt != RSA_SIGN && mode == RSA_PUBLIC ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 {
Paul Bakker5f5593a2013-01-16 13:26:56 +0100524 return( POLARSSL_ERR_RSA_INVALID_PADDING );
525 }
526
527 if( bt == RSA_CRYPT )
528 {
529 while( *p != 0 && p < buf + ilen - 1 )
530 p++;
531
532 if( *p != 0 || p >= buf + ilen - 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000533 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5f5593a2013-01-16 13:26:56 +0100534
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 p++;
536 }
Paul Bakker5f5593a2013-01-16 13:26:56 +0100537 else
538 {
539 while( *p == 0xFF && p < buf + ilen - 1 )
540 p++;
541
542 if( *p != 0 || p >= buf + ilen - 1 )
543 return( POLARSSL_ERR_RSA_INVALID_PADDING );
544
545 p++;
546 }
547
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 break;
549
Paul Bakker9dcc3222011-03-08 14:16:06 +0000550#if defined(POLARSSL_PKCS1_V21)
551 case RSA_PKCS_V21:
552
553 if( *p++ != 0 )
554 return( POLARSSL_ERR_RSA_INVALID_PADDING );
555
556 md_info = md_info_from_type( ctx->hash_id );
557 if( md_info == NULL )
558 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
559
560 hlen = md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000561
562 md_init_ctx( &md_ctx, md_info );
563
564 // Generate lHash
565 //
566 md( md_info, lhash, 0, lhash );
567
568 // seed: Apply seedMask to maskedSeed
569 //
570 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
571 &md_ctx );
572
573 // DB: Apply dbMask to maskedDB
574 //
575 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
576 &md_ctx );
577
578 p += hlen;
Paul Bakkerc0484932013-01-03 10:50:31 +0100579 md_free_ctx( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000580
581 // Check validity
582 //
583 if( memcmp( lhash, p, hlen ) != 0 )
584 return( POLARSSL_ERR_RSA_INVALID_PADDING );
585
586 p += hlen;
587
588 while( *p == 0 && p < buf + ilen )
589 p++;
590
591 if( p == buf + ilen )
592 return( POLARSSL_ERR_RSA_INVALID_PADDING );
593
594 if( *p++ != 0x01 )
595 return( POLARSSL_ERR_RSA_INVALID_PADDING );
596
597 break;
598#endif
599
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 default:
601
Paul Bakker40e46942009-01-03 21:51:57 +0000602 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 }
604
Paul Bakker27fdf462011-06-09 13:55:13 +0000605 if (ilen - (p - buf) > output_max_len)
Paul Bakker23986e52011-04-24 08:57:21 +0000606 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000607
Paul Bakker27fdf462011-06-09 13:55:13 +0000608 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 memcpy( output, p, *olen );
610
611 return( 0 );
612}
613
614/*
615 * Do an RSA operation to sign the message digest
616 */
617int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000618 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000619 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 int mode,
621 int hash_id,
Paul Bakker23986e52011-04-24 08:57:21 +0000622 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000623 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 unsigned char *sig )
625{
Paul Bakker23986e52011-04-24 08:57:21 +0000626 size_t nb_pad, olen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 unsigned char *p = sig;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000628#if defined(POLARSSL_PKCS1_V21)
629 unsigned char salt[POLARSSL_MD_MAX_SIZE];
Paul Bakkere2e36d32012-01-23 09:56:51 +0000630 unsigned int slen, hlen, offset = 0;
631 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000632 size_t msb;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000633 const md_info_t *md_info;
634 md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000635#else
636 (void) f_rng;
637 (void) p_rng;
638#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640 olen = ctx->len;
641
642 switch( ctx->padding )
643 {
644 case RSA_PKCS_V15:
645
646 switch( hash_id )
647 {
Paul Bakkerfc22c442009-07-19 20:36:27 +0000648 case SIG_RSA_RAW:
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 nb_pad = olen - 3 - hashlen;
650 break;
651
Paul Bakker4593aea2009-02-09 22:32:35 +0000652 case SIG_RSA_MD2:
653 case SIG_RSA_MD4:
654 case SIG_RSA_MD5:
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 nb_pad = olen - 3 - 34;
656 break;
657
Paul Bakker4593aea2009-02-09 22:32:35 +0000658 case SIG_RSA_SHA1:
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 nb_pad = olen - 3 - 35;
660 break;
661
Paul Bakkercde51572009-05-17 10:11:56 +0000662 case SIG_RSA_SHA224:
663 nb_pad = olen - 3 - 47;
664 break;
665
666 case SIG_RSA_SHA256:
667 nb_pad = olen - 3 - 51;
668 break;
669
670 case SIG_RSA_SHA384:
671 nb_pad = olen - 3 - 67;
672 break;
673
674 case SIG_RSA_SHA512:
675 nb_pad = olen - 3 - 83;
676 break;
677
678
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000680 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 }
682
Paul Bakker0ea57e82012-07-05 13:58:08 +0000683 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000684 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
686 *p++ = 0;
687 *p++ = RSA_SIGN;
688 memset( p, 0xFF, nb_pad );
689 p += nb_pad;
690 *p++ = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000691
692 switch( hash_id )
693 {
694 case SIG_RSA_RAW:
695 memcpy( p, hash, hashlen );
696 break;
697
698 case SIG_RSA_MD2:
699 memcpy( p, ASN1_HASH_MDX, 18 );
700 memcpy( p + 18, hash, 16 );
701 p[13] = 2; break;
702
703 case SIG_RSA_MD4:
704 memcpy( p, ASN1_HASH_MDX, 18 );
705 memcpy( p + 18, hash, 16 );
706 p[13] = 4; break;
707
708 case SIG_RSA_MD5:
709 memcpy( p, ASN1_HASH_MDX, 18 );
710 memcpy( p + 18, hash, 16 );
711 p[13] = 5; break;
712
713 case SIG_RSA_SHA1:
714 memcpy( p, ASN1_HASH_SHA1, 15 );
715 memcpy( p + 15, hash, 20 );
716 break;
717
718 case SIG_RSA_SHA224:
719 memcpy( p, ASN1_HASH_SHA2X, 19 );
720 memcpy( p + 19, hash, 28 );
721 p[1] += 28; p[14] = 4; p[18] += 28; break;
722
723 case SIG_RSA_SHA256:
724 memcpy( p, ASN1_HASH_SHA2X, 19 );
725 memcpy( p + 19, hash, 32 );
726 p[1] += 32; p[14] = 1; p[18] += 32; break;
727
728 case SIG_RSA_SHA384:
729 memcpy( p, ASN1_HASH_SHA2X, 19 );
730 memcpy( p + 19, hash, 48 );
731 p[1] += 48; p[14] = 2; p[18] += 48; break;
732
733 case SIG_RSA_SHA512:
734 memcpy( p, ASN1_HASH_SHA2X, 19 );
735 memcpy( p + 19, hash, 64 );
736 p[1] += 64; p[14] = 3; p[18] += 64; break;
737
738 default:
739 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
740 }
741
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 break;
743
Paul Bakker9dcc3222011-03-08 14:16:06 +0000744#if defined(POLARSSL_PKCS1_V21)
745 case RSA_PKCS_V21:
746
747 if( f_rng == NULL )
748 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
749
750 switch( hash_id )
751 {
752 case SIG_RSA_MD2:
753 case SIG_RSA_MD4:
754 case SIG_RSA_MD5:
755 hashlen = 16;
756 break;
757
758 case SIG_RSA_SHA1:
759 hashlen = 20;
760 break;
761
762 case SIG_RSA_SHA224:
763 hashlen = 28;
764 break;
765
766 case SIG_RSA_SHA256:
767 hashlen = 32;
768 break;
769
770 case SIG_RSA_SHA384:
771 hashlen = 48;
772 break;
773
774 case SIG_RSA_SHA512:
775 hashlen = 64;
776 break;
777
778 default:
779 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
780 }
781
782 md_info = md_info_from_type( ctx->hash_id );
783 if( md_info == NULL )
784 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
785
786 hlen = md_get_size( md_info );
Paul Bakker53019ae2011-03-25 13:58:48 +0000787 slen = hlen;
788
Paul Bakker144c3cc2012-11-13 12:13:27 +0000789 if( olen < hlen + slen + 2 )
790 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
791
Paul Bakker9dcc3222011-03-08 14:16:06 +0000792 memset( sig, 0, olen );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000793
794 msb = mpi_msb( &ctx->N ) - 1;
795
Paul Bakker53019ae2011-03-25 13:58:48 +0000796 // Generate salt of length slen
Paul Bakker9dcc3222011-03-08 14:16:06 +0000797 //
Paul Bakkera3d195c2011-11-27 21:07:34 +0000798 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
799 return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000800
801 // Note: EMSA-PSS encoding is over the length of N - 1 bits
802 //
803 msb = mpi_msb( &ctx->N ) - 1;
804 p += olen - hlen * 2 - 2;
805 *p++ = 0x01;
Paul Bakker53019ae2011-03-25 13:58:48 +0000806 memcpy( p, salt, slen );
807 p += slen;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000808
Paul Bakkerc0484932013-01-03 10:50:31 +0100809 md_init_ctx( &md_ctx, md_info );
810
Paul Bakker9dcc3222011-03-08 14:16:06 +0000811 // Generate H = Hash( M' )
812 //
813 md_starts( &md_ctx );
814 md_update( &md_ctx, p, 8 );
815 md_update( &md_ctx, hash, hashlen );
Paul Bakker53019ae2011-03-25 13:58:48 +0000816 md_update( &md_ctx, salt, slen );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000817 md_finish( &md_ctx, p );
818
819 // Compensate for boundary condition when applying mask
820 //
821 if( msb % 8 == 0 )
822 offset = 1;
823
824 // maskedDB: Apply dbMask to DB
825 //
826 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
827
Paul Bakkerc0484932013-01-03 10:50:31 +0100828 md_free_ctx( &md_ctx );
829
Paul Bakker9dcc3222011-03-08 14:16:06 +0000830 msb = mpi_msb( &ctx->N ) - 1;
831 sig[0] &= 0xFF >> ( olen * 8 - msb );
832
833 p += hlen;
834 *p++ = 0xBC;
835 break;
836#endif
837
Paul Bakker5121ce52009-01-03 21:22:43 +0000838 default:
839
Paul Bakker40e46942009-01-03 21:51:57 +0000840 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 }
842
Paul Bakker5121ce52009-01-03 21:22:43 +0000843 return( ( mode == RSA_PUBLIC )
844 ? rsa_public( ctx, sig, sig )
845 : rsa_private( ctx, sig, sig ) );
846}
847
848/*
849 * Do an RSA operation and check the message digest
850 */
851int rsa_pkcs1_verify( rsa_context *ctx,
852 int mode,
853 int hash_id,
Paul Bakker23986e52011-04-24 08:57:21 +0000854 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000855 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 unsigned char *sig )
857{
Paul Bakker23986e52011-04-24 08:57:21 +0000858 int ret;
859 size_t len, siglen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 unsigned char *p, c;
Paul Bakkercde51572009-05-17 10:11:56 +0000861 unsigned char buf[1024];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000862#if defined(POLARSSL_PKCS1_V21)
Paul Bakker1fe7d9b2011-11-15 15:26:03 +0000863 unsigned char result[POLARSSL_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000864 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +0000865 unsigned int hlen;
866 size_t slen, msb;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000867 const md_info_t *md_info;
868 md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000869#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000870 siglen = ctx->len;
871
Paul Bakker27fdf462011-06-09 13:55:13 +0000872 if( siglen < 16 || siglen > sizeof( buf ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000873 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000874
875 ret = ( mode == RSA_PUBLIC )
876 ? rsa_public( ctx, sig, buf )
877 : rsa_private( ctx, sig, buf );
878
879 if( ret != 0 )
880 return( ret );
881
882 p = buf;
883
884 switch( ctx->padding )
885 {
886 case RSA_PKCS_V15:
887
888 if( *p++ != 0 || *p++ != RSA_SIGN )
Paul Bakker40e46942009-01-03 21:51:57 +0000889 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000890
891 while( *p != 0 )
892 {
893 if( p >= buf + siglen - 1 || *p != 0xFF )
Paul Bakker40e46942009-01-03 21:51:57 +0000894 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000895 p++;
896 }
897 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000898
Paul Bakker27fdf462011-06-09 13:55:13 +0000899 len = siglen - ( p - buf );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000900
901 if( len == 34 )
902 {
903 c = p[13];
904 p[13] = 0;
905
906 if( memcmp( p, ASN1_HASH_MDX, 18 ) != 0 )
907 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
908
909 if( ( c == 2 && hash_id == SIG_RSA_MD2 ) ||
910 ( c == 4 && hash_id == SIG_RSA_MD4 ) ||
911 ( c == 5 && hash_id == SIG_RSA_MD5 ) )
912 {
913 if( memcmp( p + 18, hash, 16 ) == 0 )
914 return( 0 );
915 else
916 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
917 }
918 }
919
920 if( len == 35 && hash_id == SIG_RSA_SHA1 )
921 {
922 if( memcmp( p, ASN1_HASH_SHA1, 15 ) == 0 &&
923 memcmp( p + 15, hash, 20 ) == 0 )
924 return( 0 );
925 else
926 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
927 }
928 if( ( len == 19 + 28 && p[14] == 4 && hash_id == SIG_RSA_SHA224 ) ||
929 ( len == 19 + 32 && p[14] == 1 && hash_id == SIG_RSA_SHA256 ) ||
930 ( len == 19 + 48 && p[14] == 2 && hash_id == SIG_RSA_SHA384 ) ||
931 ( len == 19 + 64 && p[14] == 3 && hash_id == SIG_RSA_SHA512 ) )
932 {
933 c = p[1] - 17;
934 p[1] = 17;
935 p[14] = 0;
936
937 if( p[18] == c &&
938 memcmp( p, ASN1_HASH_SHA2X, 18 ) == 0 &&
939 memcmp( p + 19, hash, c ) == 0 )
940 return( 0 );
941 else
942 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
943 }
944
945 if( len == hashlen && hash_id == SIG_RSA_RAW )
946 {
947 if( memcmp( p, hash, hashlen ) == 0 )
948 return( 0 );
949 else
950 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
951 }
952
Paul Bakker5121ce52009-01-03 21:22:43 +0000953 break;
954
Paul Bakker9dcc3222011-03-08 14:16:06 +0000955#if defined(POLARSSL_PKCS1_V21)
956 case RSA_PKCS_V21:
957
958 if( buf[siglen - 1] != 0xBC )
959 return( POLARSSL_ERR_RSA_INVALID_PADDING );
960
961 switch( hash_id )
962 {
963 case SIG_RSA_MD2:
964 case SIG_RSA_MD4:
965 case SIG_RSA_MD5:
966 hashlen = 16;
967 break;
968
969 case SIG_RSA_SHA1:
970 hashlen = 20;
971 break;
972
973 case SIG_RSA_SHA224:
974 hashlen = 28;
975 break;
976
977 case SIG_RSA_SHA256:
978 hashlen = 32;
979 break;
980
981 case SIG_RSA_SHA384:
982 hashlen = 48;
983 break;
984
985 case SIG_RSA_SHA512:
986 hashlen = 64;
987 break;
988
989 default:
990 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
991 }
992
993 md_info = md_info_from_type( ctx->hash_id );
994 if( md_info == NULL )
995 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
996
997 hlen = md_get_size( md_info );
Paul Bakker53019ae2011-03-25 13:58:48 +0000998 slen = siglen - hlen - 1;
999
Paul Bakker9dcc3222011-03-08 14:16:06 +00001000 memset( zeros, 0, 8 );
1001
Paul Bakker9dcc3222011-03-08 14:16:06 +00001002 // Note: EMSA-PSS verification is over the length of N - 1 bits
1003 //
1004 msb = mpi_msb( &ctx->N ) - 1;
1005
1006 // Compensate for boundary condition when applying mask
1007 //
1008 if( msb % 8 == 0 )
1009 {
1010 p++;
1011 siglen -= 1;
1012 }
1013 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1014 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1015
Paul Bakkerc0484932013-01-03 10:50:31 +01001016 md_init_ctx( &md_ctx, md_info );
1017
Paul Bakker9dcc3222011-03-08 14:16:06 +00001018 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
1019
1020 buf[0] &= 0xFF >> ( siglen * 8 - msb );
1021
1022 while( *p == 0 && p < buf + siglen )
1023 p++;
1024
Paul Bakkerc0484932013-01-03 10:50:31 +01001025 if( p == buf + siglen ||
1026 *p++ != 0x01 )
1027 {
1028 md_free_ctx( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001029 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakkerc0484932013-01-03 10:50:31 +01001030 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001031
Paul Bakker53019ae2011-03-25 13:58:48 +00001032 slen -= p - buf;
1033
Paul Bakker9dcc3222011-03-08 14:16:06 +00001034 // Generate H = Hash( M' )
1035 //
1036 md_starts( &md_ctx );
1037 md_update( &md_ctx, zeros, 8 );
1038 md_update( &md_ctx, hash, hashlen );
Paul Bakker53019ae2011-03-25 13:58:48 +00001039 md_update( &md_ctx, p, slen );
Paul Bakker1fe7d9b2011-11-15 15:26:03 +00001040 md_finish( &md_ctx, result );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001041
Paul Bakkerc0484932013-01-03 10:50:31 +01001042 md_free_ctx( &md_ctx );
1043
Paul Bakker1fe7d9b2011-11-15 15:26:03 +00001044 if( memcmp( p + slen, result, hlen ) == 0 )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001045 return( 0 );
1046 else
1047 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001048#endif
1049
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 default:
1051
Paul Bakker40e46942009-01-03 21:51:57 +00001052 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 }
1054
Paul Bakker40e46942009-01-03 21:51:57 +00001055 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001056}
1057
1058/*
1059 * Free the components of an RSA key
1060 */
1061void rsa_free( rsa_context *ctx )
1062{
Paul Bakker6c591fa2011-05-05 11:49:20 +00001063 mpi_free( &ctx->RQ ); mpi_free( &ctx->RP ); mpi_free( &ctx->RN );
1064 mpi_free( &ctx->QP ); mpi_free( &ctx->DQ ); mpi_free( &ctx->DP );
1065 mpi_free( &ctx->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D );
1066 mpi_free( &ctx->E ); mpi_free( &ctx->N );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067}
1068
Paul Bakker40e46942009-01-03 21:51:57 +00001069#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Paul Bakker40e46942009-01-03 21:51:57 +00001071#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
1073/*
1074 * Example RSA-1024 keypair, for test purposes
1075 */
1076#define KEY_LEN 128
1077
1078#define RSA_N "9292758453063D803DD603D5E777D788" \
1079 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1080 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1081 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1082 "93A89813FBF3C4F8066D2D800F7C38A8" \
1083 "1AE31942917403FF4946B0A83D3D3E05" \
1084 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1085 "5E94BB77B07507233A0BC7BAC8F90F79"
1086
1087#define RSA_E "10001"
1088
1089#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1090 "66CA472BC44D253102F8B4A9D3BFA750" \
1091 "91386C0077937FE33FA3252D28855837" \
1092 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1093 "DF79C5CE07EE72C7F123142198164234" \
1094 "CABB724CF78B8173B9F880FC86322407" \
1095 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1096 "071513A1E85B5DFA031F21ECAE91A34D"
1097
1098#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1099 "2C01CAD19EA484A87EA4377637E75500" \
1100 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1101 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1102
1103#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1104 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1105 "910E4168387E3C30AA1E00C339A79508" \
1106 "8452DD96A9A5EA5D9DCA68DA636032AF"
1107
1108#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1109 "3C94D22288ACD763FD8E5600ED4A702D" \
1110 "F84198A5F06C2E72236AE490C93F07F8" \
1111 "3CC559CD27BC2D1CA488811730BB5725"
1112
1113#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1114 "D8AAEA56749EA28623272E4F7D0592AF" \
1115 "7C1F1313CAC9471B5C523BFE592F517B" \
1116 "407A1BD76C164B93DA2D32A383E58357"
1117
1118#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1119 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1120 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1121 "A74206CEC169D74BF5A8C50D6F48EA08"
1122
1123#define PT_LEN 24
1124#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1125 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1126
Paul Bakkera3d195c2011-11-27 21:07:34 +00001127static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001128{
Paul Bakkera3d195c2011-11-27 21:07:34 +00001129 size_t i;
1130
Paul Bakker545570e2010-07-18 09:00:25 +00001131 if( rng_state != NULL )
1132 rng_state = NULL;
1133
Paul Bakkera3d195c2011-11-27 21:07:34 +00001134 for( i = 0; i < len; ++i )
1135 output[i] = rand();
1136
1137 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001138}
1139
Paul Bakker5121ce52009-01-03 21:22:43 +00001140/*
1141 * Checkup routine
1142 */
1143int rsa_self_test( int verbose )
1144{
Paul Bakker23986e52011-04-24 08:57:21 +00001145 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 unsigned char rsa_plaintext[PT_LEN];
1148 unsigned char rsa_decrypted[PT_LEN];
1149 unsigned char rsa_ciphertext[KEY_LEN];
Paul Bakker5690efc2011-05-26 13:16:06 +00001150#if defined(POLARSSL_SHA1_C)
1151 unsigned char sha1sum[20];
1152#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Paul Bakker21eb2802010-08-16 11:10:02 +00001154 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
1156 rsa.len = KEY_LEN;
1157 mpi_read_string( &rsa.N , 16, RSA_N );
1158 mpi_read_string( &rsa.E , 16, RSA_E );
1159 mpi_read_string( &rsa.D , 16, RSA_D );
1160 mpi_read_string( &rsa.P , 16, RSA_P );
1161 mpi_read_string( &rsa.Q , 16, RSA_Q );
1162 mpi_read_string( &rsa.DP, 16, RSA_DP );
1163 mpi_read_string( &rsa.DQ, 16, RSA_DQ );
1164 mpi_read_string( &rsa.QP, 16, RSA_QP );
1165
1166 if( verbose != 0 )
1167 printf( " RSA key validation: " );
1168
1169 if( rsa_check_pubkey( &rsa ) != 0 ||
1170 rsa_check_privkey( &rsa ) != 0 )
1171 {
1172 if( verbose != 0 )
1173 printf( "failed\n" );
1174
1175 return( 1 );
1176 }
1177
1178 if( verbose != 0 )
1179 printf( "passed\n PKCS#1 encryption : " );
1180
1181 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1182
Paul Bakker21eb2802010-08-16 11:10:02 +00001183 if( rsa_pkcs1_encrypt( &rsa, &myrand, NULL, RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 rsa_plaintext, rsa_ciphertext ) != 0 )
1185 {
1186 if( verbose != 0 )
1187 printf( "failed\n" );
1188
1189 return( 1 );
1190 }
1191
1192 if( verbose != 0 )
1193 printf( "passed\n PKCS#1 decryption : " );
1194
1195 if( rsa_pkcs1_decrypt( &rsa, RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001196 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001197 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 {
1199 if( verbose != 0 )
1200 printf( "failed\n" );
1201
1202 return( 1 );
1203 }
1204
1205 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1206 {
1207 if( verbose != 0 )
1208 printf( "failed\n" );
1209
1210 return( 1 );
1211 }
1212
Paul Bakker5690efc2011-05-26 13:16:06 +00001213#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001214 if( verbose != 0 )
1215 printf( "passed\n PKCS#1 data sign : " );
1216
1217 sha1( rsa_plaintext, PT_LEN, sha1sum );
1218
Paul Bakker9dcc3222011-03-08 14:16:06 +00001219 if( rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, SIG_RSA_SHA1, 20,
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 sha1sum, rsa_ciphertext ) != 0 )
1221 {
1222 if( verbose != 0 )
1223 printf( "failed\n" );
1224
1225 return( 1 );
1226 }
1227
1228 if( verbose != 0 )
1229 printf( "passed\n PKCS#1 sig. verify: " );
1230
Paul Bakker4593aea2009-02-09 22:32:35 +00001231 if( rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1, 20,
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 sha1sum, rsa_ciphertext ) != 0 )
1233 {
1234 if( verbose != 0 )
1235 printf( "failed\n" );
1236
1237 return( 1 );
1238 }
1239
1240 if( verbose != 0 )
1241 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00001242#endif /* POLARSSL_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
1244 rsa_free( &rsa );
1245
1246 return( 0 );
1247}
1248
1249#endif
1250
1251#endif