blob: c6c0e9985cb0706a2e9eda88fcc86e4f517b6898 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker5121ce52009-01-03 21:22:43 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21/*
22 * RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
23 *
24 * http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
25 * http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
26 */
27
Paul Bakker40e46942009-01-03 21:51:57 +000028#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
34#include <stdlib.h>
35#include <string.h>
36#include <stdio.h>
37
38/*
39 * Initialize an RSA context
40 */
41void rsa_init( rsa_context *ctx,
42 int padding,
43 int hash_id,
44 int (*f_rng)(void *),
45 void *p_rng )
46{
47 memset( ctx, 0, sizeof( rsa_context ) );
48
49 ctx->padding = padding;
50 ctx->hash_id = hash_id;
51
52 ctx->f_rng = f_rng;
53 ctx->p_rng = p_rng;
54}
55
Paul Bakker40e46942009-01-03 21:51:57 +000056#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000057
58/*
59 * Generate an RSA keypair
60 */
61int rsa_gen_key( rsa_context *ctx, int nbits, int exponent )
62{
63 int ret;
64 mpi P1, Q1, H, G;
65
66 if( ctx->f_rng == NULL || nbits < 128 || exponent < 3 )
Paul Bakker40e46942009-01-03 21:51:57 +000067 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000068
69 mpi_init( &P1, &Q1, &H, &G, NULL );
70
71 /*
72 * find primes P and Q with Q < P so that:
73 * GCD( E, (P-1)*(Q-1) ) == 1
74 */
75 MPI_CHK( mpi_lset( &ctx->E, exponent ) );
76
77 do
78 {
79 MPI_CHK( mpi_gen_prime( &ctx->P, ( nbits + 1 ) >> 1, 0,
80 ctx->f_rng, ctx->p_rng ) );
81
82 MPI_CHK( mpi_gen_prime( &ctx->Q, ( nbits + 1 ) >> 1, 0,
83 ctx->f_rng, ctx->p_rng ) );
84
85 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
86 mpi_swap( &ctx->P, &ctx->Q );
87
88 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
89 continue;
90
91 MPI_CHK( mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
92 if( mpi_msb( &ctx->N ) != nbits )
93 continue;
94
95 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
96 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
97 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
98 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
99 }
100 while( mpi_cmp_int( &G, 1 ) != 0 );
101
102 /*
103 * D = E^-1 mod ((P-1)*(Q-1))
104 * DP = D mod (P - 1)
105 * DQ = D mod (Q - 1)
106 * QP = Q^-1 mod P
107 */
108 MPI_CHK( mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
109 MPI_CHK( mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
110 MPI_CHK( mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
111 MPI_CHK( mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
112
113 ctx->len = ( mpi_msb( &ctx->N ) + 7 ) >> 3;
114
115cleanup:
116
117 mpi_free( &G, &H, &Q1, &P1, NULL );
118
119 if( ret != 0 )
120 {
121 rsa_free( ctx );
Paul Bakker40e46942009-01-03 21:51:57 +0000122 return( POLARSSL_ERR_RSA_KEY_GEN_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 }
124
125 return( 0 );
126}
127
128#endif
129
130/*
131 * Check a public RSA key
132 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000133int rsa_check_pubkey( const rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000134{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000135 if( !ctx->N.p || !ctx->E.p )
136 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
137
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 if( ( ctx->N.p[0] & 1 ) == 0 ||
139 ( ctx->E.p[0] & 1 ) == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000140 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142 if( mpi_msb( &ctx->N ) < 128 ||
143 mpi_msb( &ctx->N ) > 4096 )
Paul Bakker40e46942009-01-03 21:51:57 +0000144 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146 if( mpi_msb( &ctx->E ) < 2 ||
147 mpi_msb( &ctx->E ) > 64 )
Paul Bakker40e46942009-01-03 21:51:57 +0000148 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
150 return( 0 );
151}
152
153/*
154 * Check a private RSA key
155 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000156int rsa_check_privkey( const rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000157{
158 int ret;
159 mpi PQ, DE, P1, Q1, H, I, G;
160
161 if( ( ret = rsa_check_pubkey( ctx ) ) != 0 )
162 return( ret );
163
Paul Bakker37940d9f2009-07-10 22:38:58 +0000164 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
165 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
166
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 mpi_init( &PQ, &DE, &P1, &Q1, &H, &I, &G, NULL );
168
169 MPI_CHK( mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
170 MPI_CHK( mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
171 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
172 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
173 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
174 MPI_CHK( mpi_mod_mpi( &I, &DE, &H ) );
175 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
176
177 if( mpi_cmp_mpi( &PQ, &ctx->N ) == 0 &&
178 mpi_cmp_int( &I, 1 ) == 0 &&
179 mpi_cmp_int( &G, 1 ) == 0 )
180 {
181 mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, NULL );
182 return( 0 );
183 }
184
185cleanup:
186
187 mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, NULL );
Paul Bakker40e46942009-01-03 21:51:57 +0000188 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189}
190
191/*
192 * Do an RSA public key operation
193 */
194int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000195 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 unsigned char *output )
197{
198 int ret, olen;
199 mpi T;
200
201 mpi_init( &T, NULL );
202
203 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
204
205 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
206 {
207 mpi_free( &T, NULL );
Paul Bakker40e46942009-01-03 21:51:57 +0000208 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 }
210
211 olen = ctx->len;
212 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
213 MPI_CHK( mpi_write_binary( &T, output, olen ) );
214
215cleanup:
216
217 mpi_free( &T, NULL );
218
219 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000220 return( POLARSSL_ERR_RSA_PUBLIC_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222 return( 0 );
223}
224
225/*
226 * Do an RSA private key operation
227 */
228int rsa_private( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000229 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 unsigned char *output )
231{
232 int ret, olen;
233 mpi T, T1, T2;
234
235 mpi_init( &T, &T1, &T2, NULL );
236
237 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
238
239 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
240 {
241 mpi_free( &T, NULL );
Paul Bakker40e46942009-01-03 21:51:57 +0000242 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 }
244
245#if 0
246 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
247#else
248 /*
249 * faster decryption using the CRT
250 *
251 * T1 = input ^ dP mod P
252 * T2 = input ^ dQ mod Q
253 */
254 MPI_CHK( mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
255 MPI_CHK( mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
256
257 /*
258 * T = (T1 - T2) * (Q^-1 mod P) mod P
259 */
260 MPI_CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
261 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
262 MPI_CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
263
264 /*
265 * output = T2 + T * Q
266 */
267 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
268 MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) );
269#endif
270
271 olen = ctx->len;
272 MPI_CHK( mpi_write_binary( &T, output, olen ) );
273
274cleanup:
275
276 mpi_free( &T, &T1, &T2, NULL );
277
278 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000279 return( POLARSSL_ERR_RSA_PRIVATE_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000280
281 return( 0 );
282}
283
284/*
285 * Add the message padding, then do an RSA operation
286 */
287int rsa_pkcs1_encrypt( rsa_context *ctx,
288 int mode, int ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000289 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 unsigned char *output )
291{
292 int nb_pad, olen;
293 unsigned char *p = output;
294
295 olen = ctx->len;
296
297 switch( ctx->padding )
298 {
299 case RSA_PKCS_V15:
300
301 if( ilen < 0 || olen < ilen + 11 )
Paul Bakker40e46942009-01-03 21:51:57 +0000302 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
304 nb_pad = olen - 3 - ilen;
305
306 *p++ = 0;
307 *p++ = RSA_CRYPT;
308
309 while( nb_pad-- > 0 )
310 {
311 do {
312 *p = (unsigned char) rand();
313 } while( *p == 0 );
314 p++;
315 }
316 *p++ = 0;
317 memcpy( p, input, ilen );
318 break;
319
320 default:
321
Paul Bakker40e46942009-01-03 21:51:57 +0000322 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 }
324
325 return( ( mode == RSA_PUBLIC )
326 ? rsa_public( ctx, output, output )
327 : rsa_private( ctx, output, output ) );
328}
329
330/*
331 * Do an RSA operation, then remove the message padding
332 */
333int rsa_pkcs1_decrypt( rsa_context *ctx,
334 int mode, int *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000335 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000336 unsigned char *output,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000337 int output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000338{
339 int ret, ilen;
340 unsigned char *p;
Paul Bakkercde51572009-05-17 10:11:56 +0000341 unsigned char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
343 ilen = ctx->len;
344
345 if( ilen < 16 || ilen > (int) sizeof( buf ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000346 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
348 ret = ( mode == RSA_PUBLIC )
349 ? rsa_public( ctx, input, buf )
350 : rsa_private( ctx, input, buf );
351
352 if( ret != 0 )
353 return( ret );
354
355 p = buf;
356
357 switch( ctx->padding )
358 {
359 case RSA_PKCS_V15:
360
361 if( *p++ != 0 || *p++ != RSA_CRYPT )
Paul Bakker40e46942009-01-03 21:51:57 +0000362 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
364 while( *p != 0 )
365 {
366 if( p >= buf + ilen - 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000367 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 p++;
369 }
370 p++;
371 break;
372
373 default:
374
Paul Bakker40e46942009-01-03 21:51:57 +0000375 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 }
377
Paul Bakker060c5682009-01-12 21:48:39 +0000378 if (ilen - (int)(p - buf) > output_max_len)
Paul Bakker38e2b482009-07-19 20:41:06 +0000379 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000380
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 *olen = ilen - (int)(p - buf);
382 memcpy( output, p, *olen );
383
384 return( 0 );
385}
386
387/*
388 * Do an RSA operation to sign the message digest
389 */
390int rsa_pkcs1_sign( rsa_context *ctx,
391 int mode,
392 int hash_id,
393 int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000394 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 unsigned char *sig )
396{
397 int nb_pad, olen;
398 unsigned char *p = sig;
399
400 olen = ctx->len;
401
402 switch( ctx->padding )
403 {
404 case RSA_PKCS_V15:
405
406 switch( hash_id )
407 {
Paul Bakkerfc22c442009-07-19 20:36:27 +0000408 case SIG_RSA_RAW:
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 nb_pad = olen - 3 - hashlen;
410 break;
411
Paul Bakker4593aea2009-02-09 22:32:35 +0000412 case SIG_RSA_MD2:
413 case SIG_RSA_MD4:
414 case SIG_RSA_MD5:
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 nb_pad = olen - 3 - 34;
416 break;
417
Paul Bakker4593aea2009-02-09 22:32:35 +0000418 case SIG_RSA_SHA1:
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 nb_pad = olen - 3 - 35;
420 break;
421
Paul Bakkercde51572009-05-17 10:11:56 +0000422 case SIG_RSA_SHA224:
423 nb_pad = olen - 3 - 47;
424 break;
425
426 case SIG_RSA_SHA256:
427 nb_pad = olen - 3 - 51;
428 break;
429
430 case SIG_RSA_SHA384:
431 nb_pad = olen - 3 - 67;
432 break;
433
434 case SIG_RSA_SHA512:
435 nb_pad = olen - 3 - 83;
436 break;
437
438
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000440 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 }
442
443 if( nb_pad < 8 )
Paul Bakker40e46942009-01-03 21:51:57 +0000444 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000445
446 *p++ = 0;
447 *p++ = RSA_SIGN;
448 memset( p, 0xFF, nb_pad );
449 p += nb_pad;
450 *p++ = 0;
451 break;
452
453 default:
454
Paul Bakker40e46942009-01-03 21:51:57 +0000455 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 }
457
458 switch( hash_id )
459 {
Paul Bakkerfc22c442009-07-19 20:36:27 +0000460 case SIG_RSA_RAW:
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 memcpy( p, hash, hashlen );
462 break;
463
Paul Bakker4593aea2009-02-09 22:32:35 +0000464 case SIG_RSA_MD2:
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 memcpy( p, ASN1_HASH_MDX, 18 );
466 memcpy( p + 18, hash, 16 );
467 p[13] = 2; break;
468
Paul Bakker4593aea2009-02-09 22:32:35 +0000469 case SIG_RSA_MD4:
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 memcpy( p, ASN1_HASH_MDX, 18 );
471 memcpy( p + 18, hash, 16 );
472 p[13] = 4; break;
473
Paul Bakker4593aea2009-02-09 22:32:35 +0000474 case SIG_RSA_MD5:
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 memcpy( p, ASN1_HASH_MDX, 18 );
476 memcpy( p + 18, hash, 16 );
477 p[13] = 5; break;
478
Paul Bakker4593aea2009-02-09 22:32:35 +0000479 case SIG_RSA_SHA1:
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 memcpy( p, ASN1_HASH_SHA1, 15 );
481 memcpy( p + 15, hash, 20 );
482 break;
483
Paul Bakker4593aea2009-02-09 22:32:35 +0000484 case SIG_RSA_SHA224:
485 memcpy( p, ASN1_HASH_SHA2X, 19 );
486 memcpy( p + 19, hash, 28 );
487 p[1] += 28; p[14] = 4; p[18] += 28; break;
488
489 case SIG_RSA_SHA256:
490 memcpy( p, ASN1_HASH_SHA2X, 19 );
491 memcpy( p + 19, hash, 32 );
492 p[1] += 32; p[14] = 1; p[18] += 32; break;
493
494 case SIG_RSA_SHA384:
495 memcpy( p, ASN1_HASH_SHA2X, 19 );
496 memcpy( p + 19, hash, 48 );
497 p[1] += 48; p[14] = 2; p[18] += 48; break;
498
499 case SIG_RSA_SHA512:
500 memcpy( p, ASN1_HASH_SHA2X, 19 );
501 memcpy( p + 19, hash, 64 );
502 p[1] += 64; p[14] = 3; p[18] += 64; break;
503
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000505 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 }
507
508 return( ( mode == RSA_PUBLIC )
509 ? rsa_public( ctx, sig, sig )
510 : rsa_private( ctx, sig, sig ) );
511}
512
513/*
514 * Do an RSA operation and check the message digest
515 */
516int rsa_pkcs1_verify( rsa_context *ctx,
517 int mode,
518 int hash_id,
519 int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000520 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 unsigned char *sig )
522{
523 int ret, len, siglen;
524 unsigned char *p, c;
Paul Bakkercde51572009-05-17 10:11:56 +0000525 unsigned char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 siglen = ctx->len;
528
529 if( siglen < 16 || siglen > (int) sizeof( buf ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000530 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
532 ret = ( mode == RSA_PUBLIC )
533 ? rsa_public( ctx, sig, buf )
534 : rsa_private( ctx, sig, buf );
535
536 if( ret != 0 )
537 return( ret );
538
539 p = buf;
540
541 switch( ctx->padding )
542 {
543 case RSA_PKCS_V15:
544
545 if( *p++ != 0 || *p++ != RSA_SIGN )
Paul Bakker40e46942009-01-03 21:51:57 +0000546 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548 while( *p != 0 )
549 {
550 if( p >= buf + siglen - 1 || *p != 0xFF )
Paul Bakker40e46942009-01-03 21:51:57 +0000551 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 p++;
553 }
554 p++;
555 break;
556
557 default:
558
Paul Bakker40e46942009-01-03 21:51:57 +0000559 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 }
561
562 len = siglen - (int)( p - buf );
563
564 if( len == 34 )
565 {
566 c = p[13];
567 p[13] = 0;
568
569 if( memcmp( p, ASN1_HASH_MDX, 18 ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000570 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Paul Bakker4593aea2009-02-09 22:32:35 +0000572 if( ( c == 2 && hash_id == SIG_RSA_MD2 ) ||
573 ( c == 4 && hash_id == SIG_RSA_MD4 ) ||
574 ( c == 5 && hash_id == SIG_RSA_MD5 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 {
576 if( memcmp( p + 18, hash, 16 ) == 0 )
577 return( 0 );
578 else
Paul Bakker40e46942009-01-03 21:51:57 +0000579 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 }
581 }
582
Paul Bakker4593aea2009-02-09 22:32:35 +0000583 if( len == 35 && hash_id == SIG_RSA_SHA1 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 {
585 if( memcmp( p, ASN1_HASH_SHA1, 15 ) == 0 &&
586 memcmp( p + 15, hash, 20 ) == 0 )
587 return( 0 );
588 else
Paul Bakker40e46942009-01-03 21:51:57 +0000589 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 }
Paul Bakker4593aea2009-02-09 22:32:35 +0000591 if( ( len == 19 + 28 && p[14] == 4 && hash_id == SIG_RSA_SHA224 ) ||
592 ( len == 19 + 32 && p[14] == 1 && hash_id == SIG_RSA_SHA256 ) ||
593 ( len == 19 + 48 && p[14] == 2 && hash_id == SIG_RSA_SHA384 ) ||
594 ( len == 19 + 64 && p[14] == 3 && hash_id == SIG_RSA_SHA512 ) )
595 {
596 c = p[1] - 17;
Paul Bakkercde51572009-05-17 10:11:56 +0000597 p[1] = 17;
598 p[14] = 0;
Paul Bakker4593aea2009-02-09 22:32:35 +0000599
600 if( p[18] == c &&
Paul Bakkercde51572009-05-17 10:11:56 +0000601 memcmp( p, ASN1_HASH_SHA2X, 18 ) == 0 &&
602 memcmp( p + 19, hash, c ) == 0 )
603 return( 0 );
604 else
605 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker4593aea2009-02-09 22:32:35 +0000606 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
Paul Bakkerfc22c442009-07-19 20:36:27 +0000608 if( len == hashlen && hash_id == SIG_RSA_RAW )
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 {
610 if( memcmp( p, hash, hashlen ) == 0 )
611 return( 0 );
612 else
Paul Bakker40e46942009-01-03 21:51:57 +0000613 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 }
615
Paul Bakker40e46942009-01-03 21:51:57 +0000616 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000617}
618
619/*
620 * Free the components of an RSA key
621 */
622void rsa_free( rsa_context *ctx )
623{
624 mpi_free( &ctx->RQ, &ctx->RP, &ctx->RN,
625 &ctx->QP, &ctx->DQ, &ctx->DP,
626 &ctx->Q, &ctx->P, &ctx->D,
627 &ctx->E, &ctx->N, NULL );
628}
629
Paul Bakker40e46942009-01-03 21:51:57 +0000630#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Paul Bakker40e46942009-01-03 21:51:57 +0000632#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
634/*
635 * Example RSA-1024 keypair, for test purposes
636 */
637#define KEY_LEN 128
638
639#define RSA_N "9292758453063D803DD603D5E777D788" \
640 "8ED1D5BF35786190FA2F23EBC0848AEA" \
641 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
642 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
643 "93A89813FBF3C4F8066D2D800F7C38A8" \
644 "1AE31942917403FF4946B0A83D3D3E05" \
645 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
646 "5E94BB77B07507233A0BC7BAC8F90F79"
647
648#define RSA_E "10001"
649
650#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
651 "66CA472BC44D253102F8B4A9D3BFA750" \
652 "91386C0077937FE33FA3252D28855837" \
653 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
654 "DF79C5CE07EE72C7F123142198164234" \
655 "CABB724CF78B8173B9F880FC86322407" \
656 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
657 "071513A1E85B5DFA031F21ECAE91A34D"
658
659#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
660 "2C01CAD19EA484A87EA4377637E75500" \
661 "FCB2005C5C7DD6EC4AC023CDA285D796" \
662 "C3D9E75E1EFC42488BB4F1D13AC30A57"
663
664#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
665 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
666 "910E4168387E3C30AA1E00C339A79508" \
667 "8452DD96A9A5EA5D9DCA68DA636032AF"
668
669#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
670 "3C94D22288ACD763FD8E5600ED4A702D" \
671 "F84198A5F06C2E72236AE490C93F07F8" \
672 "3CC559CD27BC2D1CA488811730BB5725"
673
674#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
675 "D8AAEA56749EA28623272E4F7D0592AF" \
676 "7C1F1313CAC9471B5C523BFE592F517B" \
677 "407A1BD76C164B93DA2D32A383E58357"
678
679#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
680 "F38D18D2B2F0E2DD275AA977E2BF4411" \
681 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
682 "A74206CEC169D74BF5A8C50D6F48EA08"
683
684#define PT_LEN 24
685#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
686 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
687
688/*
689 * Checkup routine
690 */
691int rsa_self_test( int verbose )
692{
693 int len;
694 rsa_context rsa;
695 unsigned char sha1sum[20];
696 unsigned char rsa_plaintext[PT_LEN];
697 unsigned char rsa_decrypted[PT_LEN];
698 unsigned char rsa_ciphertext[KEY_LEN];
699
700 memset( &rsa, 0, sizeof( rsa_context ) );
701
702 rsa.len = KEY_LEN;
703 mpi_read_string( &rsa.N , 16, RSA_N );
704 mpi_read_string( &rsa.E , 16, RSA_E );
705 mpi_read_string( &rsa.D , 16, RSA_D );
706 mpi_read_string( &rsa.P , 16, RSA_P );
707 mpi_read_string( &rsa.Q , 16, RSA_Q );
708 mpi_read_string( &rsa.DP, 16, RSA_DP );
709 mpi_read_string( &rsa.DQ, 16, RSA_DQ );
710 mpi_read_string( &rsa.QP, 16, RSA_QP );
711
712 if( verbose != 0 )
713 printf( " RSA key validation: " );
714
715 if( rsa_check_pubkey( &rsa ) != 0 ||
716 rsa_check_privkey( &rsa ) != 0 )
717 {
718 if( verbose != 0 )
719 printf( "failed\n" );
720
721 return( 1 );
722 }
723
724 if( verbose != 0 )
725 printf( "passed\n PKCS#1 encryption : " );
726
727 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
728
729 if( rsa_pkcs1_encrypt( &rsa, RSA_PUBLIC, PT_LEN,
730 rsa_plaintext, rsa_ciphertext ) != 0 )
731 {
732 if( verbose != 0 )
733 printf( "failed\n" );
734
735 return( 1 );
736 }
737
738 if( verbose != 0 )
739 printf( "passed\n PKCS#1 decryption : " );
740
741 if( rsa_pkcs1_decrypt( &rsa, RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +0000742 rsa_ciphertext, rsa_decrypted,
743 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 {
745 if( verbose != 0 )
746 printf( "failed\n" );
747
748 return( 1 );
749 }
750
751 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
752 {
753 if( verbose != 0 )
754 printf( "failed\n" );
755
756 return( 1 );
757 }
758
759 if( verbose != 0 )
760 printf( "passed\n PKCS#1 data sign : " );
761
762 sha1( rsa_plaintext, PT_LEN, sha1sum );
763
Paul Bakker4593aea2009-02-09 22:32:35 +0000764 if( rsa_pkcs1_sign( &rsa, RSA_PRIVATE, SIG_RSA_SHA1, 20,
Paul Bakker5121ce52009-01-03 21:22:43 +0000765 sha1sum, rsa_ciphertext ) != 0 )
766 {
767 if( verbose != 0 )
768 printf( "failed\n" );
769
770 return( 1 );
771 }
772
773 if( verbose != 0 )
774 printf( "passed\n PKCS#1 sig. verify: " );
775
Paul Bakker4593aea2009-02-09 22:32:35 +0000776 if( rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1, 20,
Paul Bakker5121ce52009-01-03 21:22:43 +0000777 sha1sum, rsa_ciphertext ) != 0 )
778 {
779 if( verbose != 0 )
780 printf( "failed\n" );
781
782 return( 1 );
783 }
784
785 if( verbose != 0 )
786 printf( "passed\n\n" );
787
788 rsa_free( &rsa );
789
790 return( 0 );
791}
792
793#endif
794
795#endif