Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / dropbear / libtomcrypt / testprof / dsa_test.c
blobf62309254977992e690be465fb3361d2851ca3a0
1 #include <tomcrypt_test.h>
3 #ifdef MDSA
5 int dsa_test(void)
7 unsigned char msg[16], out[1024], out2[1024];
8 unsigned long x, y;
9 int stat1, stat2;
10 dsa_key key, key2;
12 /* make a random key */
13 DO(dsa_make_key(&yarrow_prng, find_prng("yarrow"), 20, 128, &key));
15 /* verify it */
16 DO(dsa_verify_key(&key, &stat1));
17 if (stat1 == 0) { fprintf(stderr, "dsa_verify_key "); return 1; }
19 /* encrypt a message */
20 for (x = 0; x < 16; x++) { msg[x] = x; }
21 x = sizeof(out);
22 DO(dsa_encrypt_key(msg, 16, out, &x, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), &key));
24 /* decrypt */
25 y = sizeof(out2);
26 DO(dsa_decrypt_key(out, x, out2, &y, &key));
28 if (y != 16 || memcmp(out2, msg, 16)) {
29 fprintf(stderr, "dsa_decrypt failed, y == %lu\n", y);
30 return 1;
33 /* sign the message */
34 x = sizeof(out);
35 DO(dsa_sign_hash(msg, sizeof(msg), out, &x, &yarrow_prng, find_prng("yarrow"), &key));
37 /* verify it once */
38 DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key));
40 /* Modify and verify again */
41 msg[0] ^= 1;
42 DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat2, &key));
43 msg[0] ^= 1;
44 if (!(stat1 == 1 && stat2 == 0)) { fprintf(stderr, "dsa_verify %d %d", stat1, stat2); return 1; }
46 /* test exporting it */
47 x = sizeof(out2);
48 DO(dsa_export(out2, &x, PK_PRIVATE, &key));
49 DO(dsa_import(out2, x, &key2));
51 /* verify a signature with it */
52 DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
53 if (stat1 == 0) { fprintf(stderr, "dsa_verify (import private) %d ", stat1); return 1; }
54 dsa_free(&key2);
56 /* export as public now */
57 x = sizeof(out2);
58 DO(dsa_export(out2, &x, PK_PUBLIC, &key));
60 DO(dsa_import(out2, x, &key2));
61 /* verify a signature with it */
62 DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
63 if (stat1 == 0) { fprintf(stderr, "dsa_verify (import public) %d ", stat1); return 1; }
64 dsa_free(&key2);
65 dsa_free(&key);
67 return 0;
70 #else
72 int dsa_test(void)
74 fprintf(stderr, "NOP");
75 return 0;
78 #endif
80 /* $Source: /cvs/libtom/libtomcrypt/testprof/dsa_test.c,v $ */
81 /* $Revision: 1.9 $ */
82 /* $Date: 2005/10/30 18:49:14 $ */