annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #include "test.h"
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 int dsa_test(void)
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 unsigned char msg[16], out[1024], out2[1024];
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 unsigned long x, y;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 int err, stat1, stat2;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 dsa_key key, key2;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 /* make a random key */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 DO(dsa_make_key(&test_yarrow, find_prng("yarrow"), 20, 128, &key));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 /* verify it */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 DO(dsa_verify_key(&key, &stat1));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 if (stat1 == 0) { printf("dsa_verify_key "); return 1; }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 /* sign the message */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 x = sizeof(out);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 DO(dsa_sign_hash(msg, sizeof(msg), out, &x, &test_yarrow, find_prng("yarrow"), &key));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 /* verify it once */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 /* Modify and verify again */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 msg[0] ^= 1;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat2, &key));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 msg[0] ^= 1;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 if (!(stat1 == 1 && stat2 == 0)) { printf("dsa_verify %d %d", stat1, stat2); return 1; }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 /* test exporting it */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 x = sizeof(out2);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 DO(dsa_export(out2, &x, PK_PRIVATE, &key));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 DO(dsa_import(out2, x, &key2));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 /* verify a signature with it */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 if (stat1 == 0) { printf("dsa_verify (import private) %d ", stat1); return 1; }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 dsa_free(&key2);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 /* export as public now */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 x = sizeof(out2);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 DO(dsa_export(out2, &x, PK_PUBLIC, &key));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 DO(dsa_import(out2, x, &key2));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 /* verify a signature with it */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 if (stat1 == 0) { printf("dsa_verify (import public) %d ", stat1); return 1; }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 dsa_free(&key2);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 dsa_free(&key);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 return 0;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 }