comparison libtomcrypt/testprof/dh_tests.c @ 285:1b9e69c058d2

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3) to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 13:23:58 +0000
parents
children
comparison
equal deleted inserted replaced
281:997e6f7dc01e 285:1b9e69c058d2
1 #include <tomcrypt_test.h>
2
3 #ifdef MDH
4
5 int dh_tests (void)
6 {
7 unsigned char buf[3][4096];
8 unsigned long x, y, z;
9 int stat, stat2;
10 dh_key usera, userb;
11
12 DO(dh_test());
13
14 /* make up two keys */
15 DO(dh_make_key (&yarrow_prng, find_prng ("yarrow"), 512, &usera));
16 DO(dh_make_key (&yarrow_prng, find_prng ("yarrow"), 512, &userb));
17
18 /* make the shared secret */
19 x = 4096;
20 DO(dh_shared_secret (&usera, &userb, buf[0], &x));
21
22 y = 4096;
23 DO(dh_shared_secret (&userb, &usera, buf[1], &y));
24 if (y != x) {
25 fprintf(stderr, "DH Shared keys are not same size.\n");
26 return 1;
27 }
28 if (memcmp (buf[0], buf[1], x)) {
29 fprintf(stderr, "DH Shared keys not same contents.\n");
30 return 1;
31 }
32
33 /* now export userb */
34 y = 4096;
35 DO(dh_export (buf[1], &y, PK_PUBLIC, &userb));
36 dh_free (&userb);
37
38 /* import and make the shared secret again */
39 DO(dh_import (buf[1], y, &userb));
40 z = 4096;
41 DO(dh_shared_secret (&usera, &userb, buf[2], &z));
42
43 if (z != x) {
44 fprintf(stderr, "failed. Size don't match?\n");
45 return 1;
46 }
47 if (memcmp (buf[0], buf[2], x)) {
48 fprintf(stderr, "Failed. Content didn't match.\n");
49 return 1;
50 }
51 dh_free (&usera);
52 dh_free (&userb);
53
54 /* test encrypt_key */
55 dh_make_key (&yarrow_prng, find_prng ("yarrow"), 512, &usera);
56 for (x = 0; x < 16; x++) {
57 buf[0][x] = x;
58 }
59 y = sizeof (buf[1]);
60 DO(dh_encrypt_key (buf[0], 16, buf[1], &y, &yarrow_prng, find_prng ("yarrow"), find_hash ("md5"), &usera));
61 zeromem (buf[0], sizeof (buf[0]));
62 x = sizeof (buf[0]);
63 DO(dh_decrypt_key (buf[1], y, buf[0], &x, &usera));
64 if (x != 16) {
65 fprintf(stderr, "Failed (length)\n");
66 return 1;
67 }
68 for (x = 0; x < 16; x++)
69 if (buf[0][x] != x) {
70 fprintf(stderr, "Failed (contents)\n");
71 return 1;
72 }
73
74 /* test sign_hash */
75 for (x = 0; x < 16; x++) {
76 buf[0][x] = x;
77 }
78 x = sizeof (buf[1]);
79 DO(dh_sign_hash (buf[0], 16, buf[1], &x, &yarrow_prng , find_prng ("yarrow"), &usera));
80 DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat, &usera));
81 buf[0][0] ^= 1;
82 DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera));
83 if (!(stat == 1 && stat2 == 0)) {
84 fprintf(stderr, "dh_sign/verify_hash %d %d", stat, stat2);
85 return 1;
86 }
87 dh_free (&usera);
88 return 0;
89 }
90
91 #else
92
93 int dh_tests(void)
94 {
95 fprintf(stderr, "NOP");
96 return 0;
97 }
98
99 #endif
100
101 /* $Source: /cvs/libtom/libtomcrypt/testprof/dh_tests.c,v $ */
102 /* $Revision: 1.5 $ */
103 /* $Date: 2005/05/21 12:51:25 $ */