comparison demos/test/rsa_test.c @ 15:6362d3854bb4 libtomcrypt-orig

0.96 release of LibTomCrypt
author Matt Johnston <matt@ucc.asn.au>
date Tue, 15 Jun 2004 14:07:21 +0000
parents
children 5d99163f7e32
comparison
equal deleted inserted replaced
3:7faae8f46238 15:6362d3854bb4
1 #include "test.h"
2
3 int rsa_test(void)
4 {
5 unsigned char in[1024], out[1024], tmp[1024];
6 rsa_key key;
7 int hash_idx, prng_idx, stat, stat2;
8 unsigned long len, len2;
9 static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
10
11 hash_idx = find_hash("sha1");
12 prng_idx = find_prng("yarrow");
13 if (hash_idx == -1 || prng_idx == -1) {
14 printf("rsa_test requires SHA1 and yarrow");
15 return 1;
16 }
17
18 /* make a random key/msg */
19 yarrow_read(in, 20, &test_yarrow);
20
21 /* make a random key */
22 DO(rsa_make_key(&test_yarrow, prng_idx, 1024/8, 65537, &key));
23
24 /* 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;
41 }
42
43 /* 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;
60 }
61
62 /* sign a message (unsalted, lower cholestorol and Atkins approved) now */
63 len = sizeof(out);
64 DO(rsa_sign_hash(in, 20, out, &len, &test_yarrow, prng_idx, hash_idx, 0, &key));
65 DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 0, &stat, &key));
66 /* change a byte */
67 in[0] ^= 1;
68 DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 0, &stat2, &key));
69
70 if (!(stat == 1 && stat2 == 0)) {
71 printf("rsa_verify_hash (unsalted) failed, %d, %d", stat, stat2);
72 return 1;
73 }
74
75 /* sign a message (salted) now */
76 len = sizeof(out);
77 DO(rsa_sign_hash(in, 20, out, &len, &test_yarrow, prng_idx, hash_idx, 8, &key));
78 DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 8, &stat, &key));
79 /* change a byte */
80 in[0] ^= 1;
81 DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 8, &stat2, &key));
82
83 if (!(stat == 1 && stat2 == 0)) {
84 printf("rsa_verify_hash (salted) failed, %d, %d", stat, stat2);
85 return 1;
86 }
87
88 /* free the key and return */
89 rsa_free(&key);
90 return 0;
91 }