lantiq: Tune the XWAY subtarget cflags
[openwrt/openwrt.git] / package / px5g / src / library / rsa.c
1 /*
2 * The RSA public-key cryptosystem
3 *
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 /*
36 * RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
37 *
38 * http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
39 * http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
40 */
41
42 #include "polarssl/config.h"
43
44 #if defined(POLARSSL_RSA_C)
45
46 #include "polarssl/rsa.h"
47
48 #include <stdlib.h>
49 #include <string.h>
50 #include <stdio.h>
51
52 /*
53 * Initialize an RSA context
54 */
55 void rsa_init( rsa_context *ctx,
56 int padding,
57 int hash_id,
58 int (*f_rng)(void *),
59 void *p_rng )
60 {
61 memset( ctx, 0, sizeof( rsa_context ) );
62
63 ctx->padding = padding;
64 ctx->hash_id = hash_id;
65
66 ctx->f_rng = f_rng;
67 ctx->p_rng = p_rng;
68 }
69
70 #if defined(POLARSSL_GENPRIME)
71
72 /*
73 * Generate an RSA keypair
74 */
75 int rsa_gen_key( rsa_context *ctx, int nbits, int exponent )
76 {
77 int ret;
78 mpi P1, Q1, H, G;
79
80 if( ctx->f_rng == NULL || nbits < 128 || exponent < 3 )
81 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
82
83 mpi_init( &P1, &Q1, &H, &G, NULL );
84
85 /*
86 * find primes P and Q with Q < P so that:
87 * GCD( E, (P-1)*(Q-1) ) == 1
88 */
89 MPI_CHK( mpi_lset( &ctx->E, exponent ) );
90
91 do
92 {
93 MPI_CHK( mpi_gen_prime( &ctx->P, ( nbits + 1 ) >> 1, 0,
94 ctx->f_rng, ctx->p_rng ) );
95
96 MPI_CHK( mpi_gen_prime( &ctx->Q, ( nbits + 1 ) >> 1, 0,
97 ctx->f_rng, ctx->p_rng ) );
98
99 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
100 mpi_swap( &ctx->P, &ctx->Q );
101
102 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
103 continue;
104
105 MPI_CHK( mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
106 if( mpi_msb( &ctx->N ) != nbits )
107 continue;
108
109 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
110 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
111 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
112 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
113 }
114 while( mpi_cmp_int( &G, 1 ) != 0 );
115
116 /*
117 * D = E^-1 mod ((P-1)*(Q-1))
118 * DP = D mod (P - 1)
119 * DQ = D mod (Q - 1)
120 * QP = Q^-1 mod P
121 */
122 MPI_CHK( mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
123 MPI_CHK( mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
124 MPI_CHK( mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
125 MPI_CHK( mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
126
127 ctx->len = ( mpi_msb( &ctx->N ) + 7 ) >> 3;
128
129 cleanup:
130
131 mpi_free( &G, &H, &Q1, &P1, NULL );
132
133 if( ret != 0 )
134 {
135 rsa_free( ctx );
136 return( POLARSSL_ERR_RSA_KEY_GEN_FAILED | ret );
137 }
138
139 return( 0 );
140 }
141
142 #endif
143
144 /*
145 * Check a public RSA key
146 */
147 int rsa_check_pubkey( rsa_context *ctx )
148 {
149 if( ( ctx->N.p[0] & 1 ) == 0 ||
150 ( ctx->E.p[0] & 1 ) == 0 )
151 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
152
153 if( mpi_msb( &ctx->N ) < 128 ||
154 mpi_msb( &ctx->N ) > 4096 )
155 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
156
157 if( mpi_msb( &ctx->E ) < 2 ||
158 mpi_msb( &ctx->E ) > 64 )
159 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
160
161 return( 0 );
162 }
163
164 /*
165 * Check a private RSA key
166 */
167 int rsa_check_privkey( rsa_context *ctx )
168 {
169 int ret;
170 mpi PQ, DE, P1, Q1, H, I, G;
171
172 if( ( ret = rsa_check_pubkey( ctx ) ) != 0 )
173 return( ret );
174
175 mpi_init( &PQ, &DE, &P1, &Q1, &H, &I, &G, NULL );
176
177 MPI_CHK( mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
178 MPI_CHK( mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
179 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
180 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
181 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
182 MPI_CHK( mpi_mod_mpi( &I, &DE, &H ) );
183 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
184
185 if( mpi_cmp_mpi( &PQ, &ctx->N ) == 0 &&
186 mpi_cmp_int( &I, 1 ) == 0 &&
187 mpi_cmp_int( &G, 1 ) == 0 )
188 {
189 mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, NULL );
190 return( 0 );
191 }
192
193 cleanup:
194
195 mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, NULL );
196 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED | ret );
197 }
198
199 /*
200 * Do an RSA public key operation
201 */
202 int rsa_public( rsa_context *ctx,
203 unsigned char *input,
204 unsigned char *output )
205 {
206 int ret, olen;
207 mpi T;
208
209 mpi_init( &T, NULL );
210
211 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
212
213 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
214 {
215 mpi_free( &T, NULL );
216 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
217 }
218
219 olen = ctx->len;
220 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
221 MPI_CHK( mpi_write_binary( &T, output, olen ) );
222
223 cleanup:
224
225 mpi_free( &T, NULL );
226
227 if( ret != 0 )
228 return( POLARSSL_ERR_RSA_PUBLIC_FAILED | ret );
229
230 return( 0 );
231 }
232
233 /*
234 * Do an RSA private key operation
235 */
236 int rsa_private( rsa_context *ctx,
237 unsigned char *input,
238 unsigned char *output )
239 {
240 int ret, olen;
241 mpi T, T1, T2;
242
243 mpi_init( &T, &T1, &T2, NULL );
244
245 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
246
247 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
248 {
249 mpi_free( &T, NULL );
250 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
251 }
252
253 #if 0
254 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
255 #else
256 /*
257 * faster decryption using the CRT
258 *
259 * T1 = input ^ dP mod P
260 * T2 = input ^ dQ mod Q
261 */
262 MPI_CHK( mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
263 MPI_CHK( mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
264
265 /*
266 * T = (T1 - T2) * (Q^-1 mod P) mod P
267 */
268 MPI_CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
269 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
270 MPI_CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
271
272 /*
273 * output = T2 + T * Q
274 */
275 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
276 MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) );
277 #endif
278
279 olen = ctx->len;
280 MPI_CHK( mpi_write_binary( &T, output, olen ) );
281
282 cleanup:
283
284 mpi_free( &T, &T1, &T2, NULL );
285
286 if( ret != 0 )
287 return( POLARSSL_ERR_RSA_PRIVATE_FAILED | ret );
288
289 return( 0 );
290 }
291
292 /*
293 * Add the message padding, then do an RSA operation
294 */
295 int rsa_pkcs1_encrypt( rsa_context *ctx,
296 int mode, int ilen,
297 unsigned char *input,
298 unsigned char *output )
299 {
300 int nb_pad, olen;
301 unsigned char *p = output;
302
303 olen = ctx->len;
304
305 switch( ctx->padding )
306 {
307 case RSA_PKCS_V15:
308
309 if( ilen < 0 || olen < ilen + 11 )
310 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
311
312 nb_pad = olen - 3 - ilen;
313
314 *p++ = 0;
315 *p++ = RSA_CRYPT;
316
317 while( nb_pad-- > 0 )
318 {
319 do {
320 *p = (unsigned char) rand();
321 } while( *p == 0 );
322 p++;
323 }
324 *p++ = 0;
325 memcpy( p, input, ilen );
326 break;
327
328 default:
329
330 return( POLARSSL_ERR_RSA_INVALID_PADDING );
331 }
332
333 return( ( mode == RSA_PUBLIC )
334 ? rsa_public( ctx, output, output )
335 : rsa_private( ctx, output, output ) );
336 }
337
338 /*
339 * Do an RSA operation, then remove the message padding
340 */
341 int rsa_pkcs1_decrypt( rsa_context *ctx,
342 int mode, int *olen,
343 unsigned char *input,
344 unsigned char *output,
345 int output_max_len)
346 {
347 int ret, ilen;
348 unsigned char *p;
349 unsigned char buf[512];
350
351 ilen = ctx->len;
352
353 if( ilen < 16 || ilen > (int) sizeof( buf ) )
354 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
355
356 ret = ( mode == RSA_PUBLIC )
357 ? rsa_public( ctx, input, buf )
358 : rsa_private( ctx, input, buf );
359
360 if( ret != 0 )
361 return( ret );
362
363 p = buf;
364
365 switch( ctx->padding )
366 {
367 case RSA_PKCS_V15:
368
369 if( *p++ != 0 || *p++ != RSA_CRYPT )
370 return( POLARSSL_ERR_RSA_INVALID_PADDING );
371
372 while( *p != 0 )
373 {
374 if( p >= buf + ilen - 1 )
375 return( POLARSSL_ERR_RSA_INVALID_PADDING );
376 p++;
377 }
378 p++;
379 break;
380
381 default:
382
383 return( POLARSSL_ERR_RSA_INVALID_PADDING );
384 }
385
386 if (ilen - (int)(p - buf) > output_max_len)
387 return( POLARSSL_ERR_RSA_OUTPUT_TO_LARGE );
388
389 *olen = ilen - (int)(p - buf);
390 memcpy( output, p, *olen );
391
392 return( 0 );
393 }
394
395 /*
396 * Do an RSA operation to sign the message digest
397 */
398 int rsa_pkcs1_sign( rsa_context *ctx,
399 int mode,
400 int hash_id,
401 int hashlen,
402 unsigned char *hash,
403 unsigned char *sig )
404 {
405 int nb_pad, olen;
406 unsigned char *p = sig;
407
408 olen = ctx->len;
409
410 switch( ctx->padding )
411 {
412 case RSA_PKCS_V15:
413
414 switch( hash_id )
415 {
416 case RSA_RAW:
417 nb_pad = olen - 3 - hashlen;
418 break;
419
420 case RSA_MD2:
421 case RSA_MD4:
422 case RSA_MD5:
423 nb_pad = olen - 3 - 34;
424 break;
425
426 case RSA_SHA1:
427 nb_pad = olen - 3 - 35;
428 break;
429
430 default:
431 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
432 }
433
434 if( nb_pad < 8 )
435 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
436
437 *p++ = 0;
438 *p++ = RSA_SIGN;
439 memset( p, 0xFF, nb_pad );
440 p += nb_pad;
441 *p++ = 0;
442 break;
443
444 default:
445
446 return( POLARSSL_ERR_RSA_INVALID_PADDING );
447 }
448
449 switch( hash_id )
450 {
451 case RSA_RAW:
452 memcpy( p, hash, hashlen );
453 break;
454
455 case RSA_MD2:
456 memcpy( p, ASN1_HASH_MDX, 18 );
457 memcpy( p + 18, hash, 16 );
458 p[13] = 2; break;
459
460 case RSA_MD4:
461 memcpy( p, ASN1_HASH_MDX, 18 );
462 memcpy( p + 18, hash, 16 );
463 p[13] = 4; break;
464
465 case RSA_MD5:
466 memcpy( p, ASN1_HASH_MDX, 18 );
467 memcpy( p + 18, hash, 16 );
468 p[13] = 5; break;
469
470 case RSA_SHA1:
471 memcpy( p, ASN1_HASH_SHA1, 15 );
472 memcpy( p + 15, hash, 20 );
473 break;
474
475 default:
476 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
477 }
478
479 return( ( mode == RSA_PUBLIC )
480 ? rsa_public( ctx, sig, sig )
481 : rsa_private( ctx, sig, sig ) );
482 }
483
484 /*
485 * Do an RSA operation and check the message digest
486 */
487 int rsa_pkcs1_verify( rsa_context *ctx,
488 int mode,
489 int hash_id,
490 int hashlen,
491 unsigned char *hash,
492 unsigned char *sig )
493 {
494 int ret, len, siglen;
495 unsigned char *p, c;
496 unsigned char buf[512];
497
498 siglen = ctx->len;
499
500 if( siglen < 16 || siglen > (int) sizeof( buf ) )
501 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
502
503 ret = ( mode == RSA_PUBLIC )
504 ? rsa_public( ctx, sig, buf )
505 : rsa_private( ctx, sig, buf );
506
507 if( ret != 0 )
508 return( ret );
509
510 p = buf;
511
512 switch( ctx->padding )
513 {
514 case RSA_PKCS_V15:
515
516 if( *p++ != 0 || *p++ != RSA_SIGN )
517 return( POLARSSL_ERR_RSA_INVALID_PADDING );
518
519 while( *p != 0 )
520 {
521 if( p >= buf + siglen - 1 || *p != 0xFF )
522 return( POLARSSL_ERR_RSA_INVALID_PADDING );
523 p++;
524 }
525 p++;
526 break;
527
528 default:
529
530 return( POLARSSL_ERR_RSA_INVALID_PADDING );
531 }
532
533 len = siglen - (int)( p - buf );
534
535 if( len == 34 )
536 {
537 c = p[13];
538 p[13] = 0;
539
540 if( memcmp( p, ASN1_HASH_MDX, 18 ) != 0 )
541 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
542
543 if( ( c == 2 && hash_id == RSA_MD2 ) ||
544 ( c == 4 && hash_id == RSA_MD4 ) ||
545 ( c == 5 && hash_id == RSA_MD5 ) )
546 {
547 if( memcmp( p + 18, hash, 16 ) == 0 )
548 return( 0 );
549 else
550 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
551 }
552 }
553
554 if( len == 35 && hash_id == RSA_SHA1 )
555 {
556 if( memcmp( p, ASN1_HASH_SHA1, 15 ) == 0 &&
557 memcmp( p + 15, hash, 20 ) == 0 )
558 return( 0 );
559 else
560 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
561 }
562
563 if( len == hashlen && hash_id == RSA_RAW )
564 {
565 if( memcmp( p, hash, hashlen ) == 0 )
566 return( 0 );
567 else
568 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
569 }
570
571 return( POLARSSL_ERR_RSA_INVALID_PADDING );
572 }
573
574 /*
575 * Free the components of an RSA key
576 */
577 void rsa_free( rsa_context *ctx )
578 {
579 mpi_free( &ctx->RQ, &ctx->RP, &ctx->RN,
580 &ctx->QP, &ctx->DQ, &ctx->DP,
581 &ctx->Q, &ctx->P, &ctx->D,
582 &ctx->E, &ctx->N, NULL );
583 }
584
585 #if defined(POLARSSL_SELF_TEST)
586
587 #include "polarssl/sha1.h"
588
589 /*
590 * Example RSA-1024 keypair, for test purposes
591 */
592 #define KEY_LEN 128
593
594 #define RSA_N "9292758453063D803DD603D5E777D788" \
595 "8ED1D5BF35786190FA2F23EBC0848AEA" \
596 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
597 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
598 "93A89813FBF3C4F8066D2D800F7C38A8" \
599 "1AE31942917403FF4946B0A83D3D3E05" \
600 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
601 "5E94BB77B07507233A0BC7BAC8F90F79"
602
603 #define RSA_E "10001"
604
605 #define RSA_D "24BF6185468786FDD303083D25E64EFC" \
606 "66CA472BC44D253102F8B4A9D3BFA750" \
607 "91386C0077937FE33FA3252D28855837" \
608 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
609 "DF79C5CE07EE72C7F123142198164234" \
610 "CABB724CF78B8173B9F880FC86322407" \
611 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
612 "071513A1E85B5DFA031F21ECAE91A34D"
613
614 #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
615 "2C01CAD19EA484A87EA4377637E75500" \
616 "FCB2005C5C7DD6EC4AC023CDA285D796" \
617 "C3D9E75E1EFC42488BB4F1D13AC30A57"
618
619 #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
620 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
621 "910E4168387E3C30AA1E00C339A79508" \
622 "8452DD96A9A5EA5D9DCA68DA636032AF"
623
624 #define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
625 "3C94D22288ACD763FD8E5600ED4A702D" \
626 "F84198A5F06C2E72236AE490C93F07F8" \
627 "3CC559CD27BC2D1CA488811730BB5725"
628
629 #define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
630 "D8AAEA56749EA28623272E4F7D0592AF" \
631 "7C1F1313CAC9471B5C523BFE592F517B" \
632 "407A1BD76C164B93DA2D32A383E58357"
633
634 #define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
635 "F38D18D2B2F0E2DD275AA977E2BF4411" \
636 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
637 "A74206CEC169D74BF5A8C50D6F48EA08"
638
639 #define PT_LEN 24
640 #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
641 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
642
643 /*
644 * Checkup routine
645 */
646 int rsa_self_test( int verbose )
647 {
648 int len;
649 rsa_context rsa;
650 unsigned char sha1sum[20];
651 unsigned char rsa_plaintext[PT_LEN];
652 unsigned char rsa_decrypted[PT_LEN];
653 unsigned char rsa_ciphertext[KEY_LEN];
654
655 memset( &rsa, 0, sizeof( rsa_context ) );
656
657 rsa.len = KEY_LEN;
658 mpi_read_string( &rsa.N , 16, RSA_N );
659 mpi_read_string( &rsa.E , 16, RSA_E );
660 mpi_read_string( &rsa.D , 16, RSA_D );
661 mpi_read_string( &rsa.P , 16, RSA_P );
662 mpi_read_string( &rsa.Q , 16, RSA_Q );
663 mpi_read_string( &rsa.DP, 16, RSA_DP );
664 mpi_read_string( &rsa.DQ, 16, RSA_DQ );
665 mpi_read_string( &rsa.QP, 16, RSA_QP );
666
667 if( verbose != 0 )
668 printf( " RSA key validation: " );
669
670 if( rsa_check_pubkey( &rsa ) != 0 ||
671 rsa_check_privkey( &rsa ) != 0 )
672 {
673 if( verbose != 0 )
674 printf( "failed\n" );
675
676 return( 1 );
677 }
678
679 if( verbose != 0 )
680 printf( "passed\n PKCS#1 encryption : " );
681
682 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
683
684 if( rsa_pkcs1_encrypt( &rsa, RSA_PUBLIC, PT_LEN,
685 rsa_plaintext, rsa_ciphertext ) != 0 )
686 {
687 if( verbose != 0 )
688 printf( "failed\n" );
689
690 return( 1 );
691 }
692
693 if( verbose != 0 )
694 printf( "passed\n PKCS#1 decryption : " );
695
696 if( rsa_pkcs1_decrypt( &rsa, RSA_PRIVATE, &len,
697 rsa_ciphertext, rsa_decrypted,
698 sizeof(rsa_decrypted) ) != 0 )
699 {
700 if( verbose != 0 )
701 printf( "failed\n" );
702
703 return( 1 );
704 }
705
706 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
707 {
708 if( verbose != 0 )
709 printf( "failed\n" );
710
711 return( 1 );
712 }
713
714 if( verbose != 0 )
715 printf( "passed\n PKCS#1 data sign : " );
716
717 sha1( rsa_plaintext, PT_LEN, sha1sum );
718
719 if( rsa_pkcs1_sign( &rsa, RSA_PRIVATE, RSA_SHA1, 20,
720 sha1sum, rsa_ciphertext ) != 0 )
721 {
722 if( verbose != 0 )
723 printf( "failed\n" );
724
725 return( 1 );
726 }
727
728 if( verbose != 0 )
729 printf( "passed\n PKCS#1 sig. verify: " );
730
731 if( rsa_pkcs1_verify( &rsa, RSA_PUBLIC, RSA_SHA1, 20,
732 sha1sum, rsa_ciphertext ) != 0 )
733 {
734 if( verbose != 0 )
735 printf( "failed\n" );
736
737 return( 1 );
738 }
739
740 if( verbose != 0 )
741 printf( "passed\n\n" );
742
743 rsa_free( &rsa );
744
745 return( 0 );
746 }
747
748 #endif
749
750 #endif