blob: 5236856b321e824994d17e8c112e8b0b4584b7c2 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00004 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
6 * Copyright (C) 2009 Paul Bakker
Paul Bakker5121ce52009-01-03 21:22:43 +00007 *
8 * 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
27 */
28
Paul Bakker40e46942009-01-03 21:51:57 +000029#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker40e46942009-01-03 21:51:57 +000033#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
35#include <stdlib.h>
36#include <string.h>
37#include <stdio.h>
38
39/*
40 * Initialize an RSA context
41 */
42void rsa_init( rsa_context *ctx,
43 int padding,
44 int hash_id,
45 int (*f_rng)(void *),
46 void *p_rng )
47{
48 memset( ctx, 0, sizeof( rsa_context ) );
49
50 ctx->padding = padding;
51 ctx->hash_id = hash_id;
52
53 ctx->f_rng = f_rng;
54 ctx->p_rng = p_rng;
55}
56
Paul Bakker40e46942009-01-03 21:51:57 +000057#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000058
59/*
60 * Generate an RSA keypair
61 */
62int rsa_gen_key( rsa_context *ctx, int nbits, int exponent )
63{
64 int ret;
65 mpi P1, Q1, H, G;
66
67 if( ctx->f_rng == NULL || nbits < 128 || exponent < 3 )
Paul Bakker40e46942009-01-03 21:51:57 +000068 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70 mpi_init( &P1, &Q1, &H, &G, NULL );
71
72 /*
73 * find primes P and Q with Q < P so that:
74 * GCD( E, (P-1)*(Q-1) ) == 1
75 */
76 MPI_CHK( mpi_lset( &ctx->E, exponent ) );
77
78 do
79 {
80 MPI_CHK( mpi_gen_prime( &ctx->P, ( nbits + 1 ) >> 1, 0,
81 ctx->f_rng, ctx->p_rng ) );
82
83 MPI_CHK( mpi_gen_prime( &ctx->Q, ( nbits + 1 ) >> 1, 0,
84 ctx->f_rng, ctx->p_rng ) );
85
86 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
87 mpi_swap( &ctx->P, &ctx->Q );
88
89 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
90 continue;
91
92 MPI_CHK( mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
93 if( mpi_msb( &ctx->N ) != nbits )
94 continue;
95
96 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
97 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
98 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
99 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
100 }
101 while( mpi_cmp_int( &G, 1 ) != 0 );
102
103 /*
104 * D = E^-1 mod ((P-1)*(Q-1))
105 * DP = D mod (P - 1)
106 * DQ = D mod (Q - 1)
107 * QP = Q^-1 mod P
108 */
109 MPI_CHK( mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
110 MPI_CHK( mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
111 MPI_CHK( mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
112 MPI_CHK( mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
113
114 ctx->len = ( mpi_msb( &ctx->N ) + 7 ) >> 3;
115
116cleanup:
117
118 mpi_free( &G, &H, &Q1, &P1, NULL );
119
120 if( ret != 0 )
121 {
122 rsa_free( ctx );
Paul Bakker40e46942009-01-03 21:51:57 +0000123 return( POLARSSL_ERR_RSA_KEY_GEN_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 }
125
126 return( 0 );
127}
128
129#endif
130
131/*
132 * Check a public RSA key
133 */
134int rsa_check_pubkey( rsa_context *ctx )
135{
136 if( ( ctx->N.p[0] & 1 ) == 0 ||
137 ( ctx->E.p[0] & 1 ) == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000138 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140 if( mpi_msb( &ctx->N ) < 128 ||
141 mpi_msb( &ctx->N ) > 4096 )
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->E ) < 2 ||
145 mpi_msb( &ctx->E ) > 64 )
Paul Bakker40e46942009-01-03 21:51:57 +0000146 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
148 return( 0 );
149}
150
151/*
152 * Check a private RSA key
153 */
154int rsa_check_privkey( rsa_context *ctx )
155{
156 int ret;
157 mpi PQ, DE, P1, Q1, H, I, G;
158
159 if( ( ret = rsa_check_pubkey( ctx ) ) != 0 )
160 return( ret );
161
162 mpi_init( &PQ, &DE, &P1, &Q1, &H, &I, &G, NULL );
163
164 MPI_CHK( mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
165 MPI_CHK( mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
166 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
167 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
168 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
169 MPI_CHK( mpi_mod_mpi( &I, &DE, &H ) );
170 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
171
172 if( mpi_cmp_mpi( &PQ, &ctx->N ) == 0 &&
173 mpi_cmp_int( &I, 1 ) == 0 &&
174 mpi_cmp_int( &G, 1 ) == 0 )
175 {
176 mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, NULL );
177 return( 0 );
178 }
179
180cleanup:
181
182 mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, NULL );
Paul Bakker40e46942009-01-03 21:51:57 +0000183 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184}
185
186/*
187 * Do an RSA public key operation
188 */
189int rsa_public( rsa_context *ctx,
190 unsigned char *input,
191 unsigned char *output )
192{
193 int ret, olen;
194 mpi T;
195
196 mpi_init( &T, NULL );
197
198 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
199
200 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
201 {
202 mpi_free( &T, NULL );
Paul Bakker40e46942009-01-03 21:51:57 +0000203 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 }
205
206 olen = ctx->len;
207 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
208 MPI_CHK( mpi_write_binary( &T, output, olen ) );
209
210cleanup:
211
212 mpi_free( &T, NULL );
213
214 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000215 return( POLARSSL_ERR_RSA_PUBLIC_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217 return( 0 );
218}
219
220/*
221 * Do an RSA private key operation
222 */
223int rsa_private( rsa_context *ctx,
224 unsigned char *input,
225 unsigned char *output )
226{
227 int ret, olen;
228 mpi T, T1, T2;
229
230 mpi_init( &T, &T1, &T2, NULL );
231
232 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
233
234 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
235 {
236 mpi_free( &T, NULL );
Paul Bakker40e46942009-01-03 21:51:57 +0000237 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 }
239
240#if 0
241 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
242#else
243 /*
244 * faster decryption using the CRT
245 *
246 * T1 = input ^ dP mod P
247 * T2 = input ^ dQ mod Q
248 */
249 MPI_CHK( mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
250 MPI_CHK( mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
251
252 /*
253 * T = (T1 - T2) * (Q^-1 mod P) mod P
254 */
255 MPI_CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
256 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
257 MPI_CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
258
259 /*
260 * output = T2 + T * Q
261 */
262 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
263 MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) );
264#endif
265
266 olen = ctx->len;
267 MPI_CHK( mpi_write_binary( &T, output, olen ) );
268
269cleanup:
270
271 mpi_free( &T, &T1, &T2, NULL );
272
273 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000274 return( POLARSSL_ERR_RSA_PRIVATE_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
276 return( 0 );
277}
278
279/*
280 * Add the message padding, then do an RSA operation
281 */
282int rsa_pkcs1_encrypt( rsa_context *ctx,
283 int mode, int ilen,
284 unsigned char *input,
285 unsigned char *output )
286{
287 int nb_pad, olen;
288 unsigned char *p = output;
289
290 olen = ctx->len;
291
292 switch( ctx->padding )
293 {
294 case RSA_PKCS_V15:
295
296 if( ilen < 0 || olen < ilen + 11 )
Paul Bakker40e46942009-01-03 21:51:57 +0000297 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298
299 nb_pad = olen - 3 - ilen;
300
301 *p++ = 0;
302 *p++ = RSA_CRYPT;
303
304 while( nb_pad-- > 0 )
305 {
306 do {
307 *p = (unsigned char) rand();
308 } while( *p == 0 );
309 p++;
310 }
311 *p++ = 0;
312 memcpy( p, input, ilen );
313 break;
314
315 default:
316
Paul Bakker40e46942009-01-03 21:51:57 +0000317 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 }
319
320 return( ( mode == RSA_PUBLIC )
321 ? rsa_public( ctx, output, output )
322 : rsa_private( ctx, output, output ) );
323}
324
325/*
326 * Do an RSA operation, then remove the message padding
327 */
328int rsa_pkcs1_decrypt( rsa_context *ctx,
329 int mode, int *olen,
330 unsigned char *input,
331 unsigned char *output )
332{
333 int ret, ilen;
334 unsigned char *p;
335 unsigned char buf[512];
336
337 ilen = ctx->len;
338
339 if( ilen < 16 || ilen > (int) sizeof( buf ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000340 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342 ret = ( mode == RSA_PUBLIC )
343 ? rsa_public( ctx, input, buf )
344 : rsa_private( ctx, input, buf );
345
346 if( ret != 0 )
347 return( ret );
348
349 p = buf;
350
351 switch( ctx->padding )
352 {
353 case RSA_PKCS_V15:
354
355 if( *p++ != 0 || *p++ != RSA_CRYPT )
Paul Bakker40e46942009-01-03 21:51:57 +0000356 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
358 while( *p != 0 )
359 {
360 if( p >= buf + ilen - 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000361 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 p++;
363 }
364 p++;
365 break;
366
367 default:
368
Paul Bakker40e46942009-01-03 21:51:57 +0000369 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 }
371
372 *olen = ilen - (int)(p - buf);
373 memcpy( output, p, *olen );
374
375 return( 0 );
376}
377
378/*
379 * Do an RSA operation to sign the message digest
380 */
381int rsa_pkcs1_sign( rsa_context *ctx,
382 int mode,
383 int hash_id,
384 int hashlen,
385 unsigned char *hash,
386 unsigned char *sig )
387{
388 int nb_pad, olen;
389 unsigned char *p = sig;
390
391 olen = ctx->len;
392
393 switch( ctx->padding )
394 {
395 case RSA_PKCS_V15:
396
397 switch( hash_id )
398 {
399 case RSA_RAW:
400 nb_pad = olen - 3 - hashlen;
401 break;
402
403 case RSA_MD2:
404 case RSA_MD4:
405 case RSA_MD5:
406 nb_pad = olen - 3 - 34;
407 break;
408
409 case RSA_SHA1:
410 nb_pad = olen - 3 - 35;
411 break;
412
413 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000414 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 }
416
417 if( nb_pad < 8 )
Paul Bakker40e46942009-01-03 21:51:57 +0000418 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
420 *p++ = 0;
421 *p++ = RSA_SIGN;
422 memset( p, 0xFF, nb_pad );
423 p += nb_pad;
424 *p++ = 0;
425 break;
426
427 default:
428
Paul Bakker40e46942009-01-03 21:51:57 +0000429 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 }
431
432 switch( hash_id )
433 {
434 case RSA_RAW:
435 memcpy( p, hash, hashlen );
436 break;
437
438 case RSA_MD2:
439 memcpy( p, ASN1_HASH_MDX, 18 );
440 memcpy( p + 18, hash, 16 );
441 p[13] = 2; break;
442
443 case RSA_MD4:
444 memcpy( p, ASN1_HASH_MDX, 18 );
445 memcpy( p + 18, hash, 16 );
446 p[13] = 4; break;
447
448 case RSA_MD5:
449 memcpy( p, ASN1_HASH_MDX, 18 );
450 memcpy( p + 18, hash, 16 );
451 p[13] = 5; break;
452
453 case RSA_SHA1:
454 memcpy( p, ASN1_HASH_SHA1, 15 );
455 memcpy( p + 15, hash, 20 );
456 break;
457
458 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000459 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 }
461
462 return( ( mode == RSA_PUBLIC )
463 ? rsa_public( ctx, sig, sig )
464 : rsa_private( ctx, sig, sig ) );
465}
466
467/*
468 * Do an RSA operation and check the message digest
469 */
470int rsa_pkcs1_verify( rsa_context *ctx,
471 int mode,
472 int hash_id,
473 int hashlen,
474 unsigned char *hash,
475 unsigned char *sig )
476{
477 int ret, len, siglen;
478 unsigned char *p, c;
479 unsigned char buf[512];
480
481 siglen = ctx->len;
482
483 if( siglen < 16 || siglen > (int) sizeof( buf ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000484 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 ret = ( mode == RSA_PUBLIC )
487 ? rsa_public( ctx, sig, buf )
488 : rsa_private( ctx, sig, buf );
489
490 if( ret != 0 )
491 return( ret );
492
493 p = buf;
494
495 switch( ctx->padding )
496 {
497 case RSA_PKCS_V15:
498
499 if( *p++ != 0 || *p++ != RSA_SIGN )
Paul Bakker40e46942009-01-03 21:51:57 +0000500 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 while( *p != 0 )
503 {
504 if( p >= buf + siglen - 1 || *p != 0xFF )
Paul Bakker40e46942009-01-03 21:51:57 +0000505 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 p++;
507 }
508 p++;
509 break;
510
511 default:
512
Paul Bakker40e46942009-01-03 21:51:57 +0000513 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 }
515
516 len = siglen - (int)( p - buf );
517
518 if( len == 34 )
519 {
520 c = p[13];
521 p[13] = 0;
522
523 if( memcmp( p, ASN1_HASH_MDX, 18 ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000524 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526 if( ( c == 2 && hash_id == RSA_MD2 ) ||
527 ( c == 4 && hash_id == RSA_MD4 ) ||
528 ( c == 5 && hash_id == RSA_MD5 ) )
529 {
530 if( memcmp( p + 18, hash, 16 ) == 0 )
531 return( 0 );
532 else
Paul Bakker40e46942009-01-03 21:51:57 +0000533 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 }
535 }
536
537 if( len == 35 && hash_id == RSA_SHA1 )
538 {
539 if( memcmp( p, ASN1_HASH_SHA1, 15 ) == 0 &&
540 memcmp( p + 15, hash, 20 ) == 0 )
541 return( 0 );
542 else
Paul Bakker40e46942009-01-03 21:51:57 +0000543 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 }
545
546 if( len == hashlen && hash_id == RSA_RAW )
547 {
548 if( memcmp( p, hash, hashlen ) == 0 )
549 return( 0 );
550 else
Paul Bakker40e46942009-01-03 21:51:57 +0000551 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 }
553
Paul Bakker40e46942009-01-03 21:51:57 +0000554 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555}
556
557/*
558 * Free the components of an RSA key
559 */
560void rsa_free( rsa_context *ctx )
561{
562 mpi_free( &ctx->RQ, &ctx->RP, &ctx->RN,
563 &ctx->QP, &ctx->DQ, &ctx->DP,
564 &ctx->Q, &ctx->P, &ctx->D,
565 &ctx->E, &ctx->N, NULL );
566}
567
Paul Bakker40e46942009-01-03 21:51:57 +0000568#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Paul Bakker40e46942009-01-03 21:51:57 +0000570#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572/*
573 * Example RSA-1024 keypair, for test purposes
574 */
575#define KEY_LEN 128
576
577#define RSA_N "9292758453063D803DD603D5E777D788" \
578 "8ED1D5BF35786190FA2F23EBC0848AEA" \
579 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
580 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
581 "93A89813FBF3C4F8066D2D800F7C38A8" \
582 "1AE31942917403FF4946B0A83D3D3E05" \
583 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
584 "5E94BB77B07507233A0BC7BAC8F90F79"
585
586#define RSA_E "10001"
587
588#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
589 "66CA472BC44D253102F8B4A9D3BFA750" \
590 "91386C0077937FE33FA3252D28855837" \
591 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
592 "DF79C5CE07EE72C7F123142198164234" \
593 "CABB724CF78B8173B9F880FC86322407" \
594 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
595 "071513A1E85B5DFA031F21ECAE91A34D"
596
597#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
598 "2C01CAD19EA484A87EA4377637E75500" \
599 "FCB2005C5C7DD6EC4AC023CDA285D796" \
600 "C3D9E75E1EFC42488BB4F1D13AC30A57"
601
602#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
603 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
604 "910E4168387E3C30AA1E00C339A79508" \
605 "8452DD96A9A5EA5D9DCA68DA636032AF"
606
607#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
608 "3C94D22288ACD763FD8E5600ED4A702D" \
609 "F84198A5F06C2E72236AE490C93F07F8" \
610 "3CC559CD27BC2D1CA488811730BB5725"
611
612#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
613 "D8AAEA56749EA28623272E4F7D0592AF" \
614 "7C1F1313CAC9471B5C523BFE592F517B" \
615 "407A1BD76C164B93DA2D32A383E58357"
616
617#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
618 "F38D18D2B2F0E2DD275AA977E2BF4411" \
619 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
620 "A74206CEC169D74BF5A8C50D6F48EA08"
621
622#define PT_LEN 24
623#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
624 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
625
626/*
627 * Checkup routine
628 */
629int rsa_self_test( int verbose )
630{
631 int len;
632 rsa_context rsa;
633 unsigned char sha1sum[20];
634 unsigned char rsa_plaintext[PT_LEN];
635 unsigned char rsa_decrypted[PT_LEN];
636 unsigned char rsa_ciphertext[KEY_LEN];
637
638 memset( &rsa, 0, sizeof( rsa_context ) );
639
640 rsa.len = KEY_LEN;
641 mpi_read_string( &rsa.N , 16, RSA_N );
642 mpi_read_string( &rsa.E , 16, RSA_E );
643 mpi_read_string( &rsa.D , 16, RSA_D );
644 mpi_read_string( &rsa.P , 16, RSA_P );
645 mpi_read_string( &rsa.Q , 16, RSA_Q );
646 mpi_read_string( &rsa.DP, 16, RSA_DP );
647 mpi_read_string( &rsa.DQ, 16, RSA_DQ );
648 mpi_read_string( &rsa.QP, 16, RSA_QP );
649
650 if( verbose != 0 )
651 printf( " RSA key validation: " );
652
653 if( rsa_check_pubkey( &rsa ) != 0 ||
654 rsa_check_privkey( &rsa ) != 0 )
655 {
656 if( verbose != 0 )
657 printf( "failed\n" );
658
659 return( 1 );
660 }
661
662 if( verbose != 0 )
663 printf( "passed\n PKCS#1 encryption : " );
664
665 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
666
667 if( rsa_pkcs1_encrypt( &rsa, RSA_PUBLIC, PT_LEN,
668 rsa_plaintext, rsa_ciphertext ) != 0 )
669 {
670 if( verbose != 0 )
671 printf( "failed\n" );
672
673 return( 1 );
674 }
675
676 if( verbose != 0 )
677 printf( "passed\n PKCS#1 decryption : " );
678
679 if( rsa_pkcs1_decrypt( &rsa, RSA_PRIVATE, &len,
680 rsa_ciphertext, rsa_decrypted ) != 0 )
681 {
682 if( verbose != 0 )
683 printf( "failed\n" );
684
685 return( 1 );
686 }
687
688 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
689 {
690 if( verbose != 0 )
691 printf( "failed\n" );
692
693 return( 1 );
694 }
695
696 if( verbose != 0 )
697 printf( "passed\n PKCS#1 data sign : " );
698
699 sha1( rsa_plaintext, PT_LEN, sha1sum );
700
701 if( rsa_pkcs1_sign( &rsa, RSA_PRIVATE, RSA_SHA1, 20,
702 sha1sum, rsa_ciphertext ) != 0 )
703 {
704 if( verbose != 0 )
705 printf( "failed\n" );
706
707 return( 1 );
708 }
709
710 if( verbose != 0 )
711 printf( "passed\n PKCS#1 sig. verify: " );
712
713 if( rsa_pkcs1_verify( &rsa, RSA_PUBLIC, RSA_SHA1, 20,
714 sha1sum, rsa_ciphertext ) != 0 )
715 {
716 if( verbose != 0 )
717 printf( "failed\n" );
718
719 return( 1 );
720 }
721
722 if( verbose != 0 )
723 printf( "passed\n\n" );
724
725 rsa_free( &rsa );
726
727 return( 0 );
728}
729
730#endif
731
732#endif