11#include "test.h"
22
3+ #define RSA_MSGSIZE 78
4+
5+
36int rsa_test (void )
47{
58 unsigned char in [1024 ], out [1024 ], tmp [1024 ];
69 rsa_key key ;
710 int hash_idx , prng_idx , stat , stat2 ;
8- unsigned long len , len2 ;
11+ unsigned long rsa_msgsize , len , len2 ;
912 static unsigned char lparam [] = { 0x01 , 0x02 , 0x03 , 0x04 };
1013
1114 hash_idx = find_hash ("sha1" );
@@ -15,48 +18,82 @@ int rsa_test(void)
1518 return 1 ;
1619 }
1720
18- /* make a random key/msg */
19- yarrow_read (in , 20 , & test_yarrow );
20-
2121 /* make a random key */
2222 DO (rsa_make_key (& test_yarrow , prng_idx , 1024 /8 , 65537 , & key ));
2323
2424 /* encrypt the key (without lparam) */
25- len = sizeof (out );
26- len2 = sizeof (tmp );
27- DO (rsa_encrypt_key (in , 20 , out , & len , NULL , 0 , & test_yarrow , prng_idx , hash_idx , & key ));
28- /* change a byte */
29- out [0 ] ^= 1 ;
30- DO (rsa_decrypt_key (out , len , tmp , & len2 , NULL , 0 , & test_yarrow , prng_idx , hash_idx , & stat2 , & key ));
31- /* change a byte back */
32- out [0 ] ^= 1 ;
33- DO (rsa_decrypt_key (out , len , tmp , & len2 , NULL , 0 , & test_yarrow , prng_idx , hash_idx , & stat , & key ));
34- if (!(stat == 1 && stat2 == 0 )) {
35- printf ("rsa_decrypt_key failed" );
36- return 1 ;
37- }
38- if (len2 != 20 || memcmp (tmp , in , 20 )) {
39- printf ("rsa_decrypt_key mismatch len %lu" , len2 );
40- return 1 ;
25+ for (rsa_msgsize = 1 ; rsa_msgsize <= 86 ; rsa_msgsize ++ ) {
26+ /* make a random key/msg */
27+ yarrow_read (in , rsa_msgsize , & test_yarrow );
28+
29+ len = sizeof (out );
30+ len2 = rsa_msgsize ;
31+
32+ DO (rsa_encrypt_key (in , rsa_msgsize , out , & len , NULL , 0 , & test_yarrow , prng_idx , hash_idx , & key ));
33+ /* change a byte */
34+ out [8 ] ^= 1 ;
35+ DO (rsa_decrypt_key (out , len , tmp , & len2 , NULL , 0 , & test_yarrow , prng_idx , hash_idx , & stat2 , & key ));
36+ /* change a byte back */
37+ out [8 ] ^= 1 ;
38+ if (len2 != rsa_msgsize ) {
39+ printf ("\nrsa_decrypt_key mismatch len %lu (first decrypt)" , len2 );
40+ return 1 ;
41+ }
42+
43+ len2 = rsa_msgsize ;
44+ DO (rsa_decrypt_key (out , len , tmp , & len2 , NULL , 0 , & test_yarrow , prng_idx , hash_idx , & stat , & key ));
45+ if (!(stat == 1 && stat2 == 0 )) {
46+ printf ("rsa_decrypt_key failed" );
47+ return 1 ;
48+ }
49+ if (len2 != rsa_msgsize || memcmp (tmp , in , rsa_msgsize )) {
50+ int x ;
51+ printf ("\nrsa_decrypt_key mismatch, len %lu (second decrypt)\n" , len2 );
52+ printf ("Original contents: \n" );
53+ for (x = 0 ; x < rsa_msgsize ; ) {
54+ printf ("%02x " , in [x ]);
55+ if (!(++ x % 16 )) {
56+ printf ("\n" );
57+ }
58+ }
59+ printf ("\n" );
60+ printf ("Output contents: \n" );
61+ for (x = 0 ; x < rsa_msgsize ; ) {
62+ printf ("%02x " , out [x ]);
63+ if (!(++ x % 16 )) {
64+ printf ("\n" );
65+ }
66+ }
67+ printf ("\n" );
68+ return 1 ;
69+ }
4170 }
4271
4372 /* encrypt the key (with lparam) */
44- len = sizeof (out );
45- len2 = sizeof (tmp );
46- DO (rsa_encrypt_key (in , 20 , out , & len , lparam , sizeof (lparam ), & test_yarrow , prng_idx , hash_idx , & key ));
47- /* change a byte */
48- out [0 ] ^= 1 ;
49- DO (rsa_decrypt_key (out , len , tmp , & len2 , lparam , sizeof (lparam ), & test_yarrow , prng_idx , hash_idx , & stat2 , & key ));
50- /* change a byte back */
51- out [0 ] ^= 1 ;
52- DO (rsa_decrypt_key (out , len , tmp , & len2 , lparam , sizeof (lparam ), & test_yarrow , prng_idx , hash_idx , & stat , & key ));
53- if (!(stat == 1 && stat2 == 0 )) {
54- printf ("rsa_decrypt_key failed" );
55- return 1 ;
56- }
57- if (len2 != 20 || memcmp (tmp , in , 20 )) {
58- printf ("rsa_decrypt_key mismatch len %lu" , len2 );
59- return 1 ;
73+ for (rsa_msgsize = 1 ; rsa_msgsize <= 86 ; rsa_msgsize ++ ) {
74+ len = sizeof (out );
75+ len2 = rsa_msgsize ;
76+ DO (rsa_encrypt_key (in , rsa_msgsize , out , & len , lparam , sizeof (lparam ), & test_yarrow , prng_idx , hash_idx , & key ));
77+ /* change a byte */
78+ out [8 ] ^= 1 ;
79+ DO (rsa_decrypt_key (out , len , tmp , & len2 , lparam , sizeof (lparam ), & test_yarrow , prng_idx , hash_idx , & stat2 , & key ));
80+ if (len2 != rsa_msgsize ) {
81+ printf ("\nrsa_decrypt_key mismatch len %lu (first decrypt)" , len2 );
82+ return 1 ;
83+ }
84+ /* change a byte back */
85+ out [8 ] ^= 1 ;
86+
87+ len2 = rsa_msgsize ;
88+ DO (rsa_decrypt_key (out , len , tmp , & len2 , lparam , sizeof (lparam ), & test_yarrow , prng_idx , hash_idx , & stat , & key ));
89+ if (!(stat == 1 && stat2 == 0 )) {
90+ printf ("rsa_decrypt_key failed" );
91+ return 1 ;
92+ }
93+ if (len2 != rsa_msgsize || memcmp (tmp , in , rsa_msgsize )) {
94+ printf ("rsa_decrypt_key mismatch len %lu" , len2 );
95+ return 1 ;
96+ }
6097 }
6198
6299 /* sign a message (unsalted, lower cholestorol and Atkins approved) now */
0 commit comments