comparison demos/test/dh_tests.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 dh_tests (void)
4 {
5 unsigned char buf[3][4096];
6 unsigned long x, y, z;
7 int stat, stat2;
8 dh_key usera, userb;
9
10 DO(dh_test());
11
12 /* make up two keys */
13 DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 96, &usera));
14 DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 96, &userb));
15
16 /* make the shared secret */
17 x = 4096;
18 DO(dh_shared_secret (&usera, &userb, buf[0], &x));
19
20 y = 4096;
21 DO(dh_shared_secret (&userb, &usera, buf[1], &y));
22 if (y != x) {
23 printf ("DH Shared keys are not same size.\n");
24 return 1;
25 }
26 if (memcmp (buf[0], buf[1], x)) {
27 printf ("DH Shared keys not same contents.\n");
28 return 1;
29 }
30
31 /* now export userb */
32 y = 4096;
33 DO(dh_export (buf[1], &y, PK_PUBLIC, &userb));
34 dh_free (&userb);
35
36 /* import and make the shared secret again */
37 DO(dh_import (buf[1], y, &userb));
38 z = 4096;
39 DO(dh_shared_secret (&usera, &userb, buf[2], &z));
40
41 if (z != x) {
42 printf ("failed. Size don't match?\n");
43 return 1;
44 }
45 if (memcmp (buf[0], buf[2], x)) {
46 printf ("Failed. Content didn't match.\n");
47 return 1;
48 }
49 dh_free (&usera);
50 dh_free (&userb);
51
52 /* test encrypt_key */
53 dh_make_key (&test_yarrow, find_prng ("yarrow"), 128, &usera);
54 for (x = 0; x < 16; x++) {
55 buf[0][x] = x;
56 }
57 y = sizeof (buf[1]);
58 DO(dh_encrypt_key (buf[0], 16, buf[1], &y, &test_yarrow, find_prng ("yarrow"), find_hash ("md5"), &usera));
59 zeromem (buf[0], sizeof (buf[0]));
60 x = sizeof (buf[0]);
61 DO(dh_decrypt_key (buf[1], y, buf[0], &x, &usera));
62 if (x != 16) {
63 printf ("Failed (length)\n");
64 return 1;
65 }
66 for (x = 0; x < 16; x++)
67 if (buf[0][x] != x) {
68 printf ("Failed (contents)\n");
69 return 1;
70 }
71
72 /* test sign_hash */
73 for (x = 0; x < 16; x++) {
74 buf[0][x] = x;
75 }
76 x = sizeof (buf[1]);
77 DO(dh_sign_hash (buf[0], 16, buf[1], &x, &test_yarrow , find_prng ("yarrow"), &usera));
78 DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat, &usera));
79 buf[0][0] ^= 1;
80 DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera));
81 if (!(stat == 1 && stat2 == 0)) {
82 printf("dh_sign/verify_hash %d %d", stat, stat2);
83 return 1;
84 }
85 dh_free (&usera);
86 return 0;
87 }