comparison demos/test/dsa_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 dsa_test(void)
4 {
5 unsigned char msg[16], out[1024], out2[1024];
6 unsigned long x, y;
7 int err, stat1, stat2;
8 dsa_key key, key2;
9
10 /* make a random key */
11 DO(dsa_make_key(&test_yarrow, find_prng("yarrow"), 20, 128, &key));
12
13 /* verify it */
14 DO(dsa_verify_key(&key, &stat1));
15 if (stat1 == 0) { printf("dsa_verify_key "); return 1; }
16
17 /* sign the message */
18 x = sizeof(out);
19 DO(dsa_sign_hash(msg, sizeof(msg), out, &x, &test_yarrow, find_prng("yarrow"), &key));
20
21 /* verify it once */
22 DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key));
23
24 /* Modify and verify again */
25 msg[0] ^= 1;
26 DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat2, &key));
27 msg[0] ^= 1;
28 if (!(stat1 == 1 && stat2 == 0)) { printf("dsa_verify %d %d", stat1, stat2); return 1; }
29
30 /* test exporting it */
31 x = sizeof(out2);
32 DO(dsa_export(out2, &x, PK_PRIVATE, &key));
33 DO(dsa_import(out2, x, &key2));
34
35 /* verify a signature with it */
36 DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
37 if (stat1 == 0) { printf("dsa_verify (import private) %d ", stat1); return 1; }
38 dsa_free(&key2);
39
40 /* export as public now */
41 x = sizeof(out2);
42 DO(dsa_export(out2, &x, PK_PUBLIC, &key));
43 DO(dsa_import(out2, x, &key2));
44 /* verify a signature with it */
45 DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
46 if (stat1 == 0) { printf("dsa_verify (import public) %d ", stat1); return 1; }
47 dsa_free(&key2);
48 dsa_free(&key);
49
50 return 0;
51 }