comparison libtomcrypt/testprof/ecc_test.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 0cbe8f6dbf9e
comparison
equal deleted inserted replaced
281:997e6f7dc01e 285:1b9e69c058d2
1 #include <tomcrypt_test.h>
2
3 #ifdef MECC
4
5 static int sizes[] = {
6 #ifdef ECC192
7 24,
8 #endif
9 #ifdef ECC224
10 28,
11 #endif
12 #ifdef ECC256
13 32,
14 #endif
15 #ifdef ECC384
16 48,
17 #endif
18 #ifdef ECC512
19 65
20 #endif
21 };
22
23 int ecc_tests (void)
24 {
25 unsigned char buf[4][4096];
26 unsigned long x, y, z, s;
27 int stat, stat2;
28 ecc_key usera, userb, pubKey, privKey;
29
30 DO(ecc_test ());
31
32 for (s = 0; s < (int)(sizeof(sizes)/sizeof(sizes[0])); s++) {
33 /* make up two keys */
34 DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
35 DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &userb));
36
37 /* make the shared secret */
38 x = 4096;
39 DO(ecc_shared_secret (&usera, &userb, buf[0], &x));
40
41 y = 4096;
42 DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
43
44 if (y != x) {
45 fprintf(stderr, "ecc Shared keys are not same size.");
46 return 1;
47 }
48
49 if (memcmp (buf[0], buf[1], x)) {
50 fprintf(stderr, "ecc Shared keys not same contents.");
51 return 1;
52 }
53
54 /* now export userb */
55 y = 4096;
56 DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb));
57 ecc_free (&userb);
58
59 /* import and make the shared secret again */
60 DO(ecc_import (buf[1], y, &userb));
61
62 z = 4096;
63 DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
64
65 if (z != x) {
66 fprintf(stderr, "failed. Size don't match?");
67 return 1;
68 }
69 if (memcmp (buf[0], buf[2], x)) {
70 fprintf(stderr, "Failed. Contents didn't match.");
71 return 1;
72 }
73 ecc_free (&usera);
74 ecc_free (&userb);
75
76 /* test encrypt_key */
77 DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
78
79 /* export key */
80 x = sizeof(buf[0]);
81 DO(ecc_export(buf[0], &x, PK_PUBLIC, &usera));
82 DO(ecc_import(buf[0], x, &pubKey));
83 x = sizeof(buf[0]);
84 DO(ecc_export(buf[0], &x, PK_PRIVATE, &usera));
85 DO(ecc_import(buf[0], x, &privKey));
86
87 for (x = 0; x < 32; x++) {
88 buf[0][x] = x;
89 }
90 y = sizeof (buf[1]);
91 DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &yarrow_prng, find_prng ("yarrow"), find_hash ("sha256"), &pubKey));
92 zeromem (buf[0], sizeof (buf[0]));
93 x = sizeof (buf[0]);
94 DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &privKey));
95 if (x != 32) {
96 fprintf(stderr, "Failed (length)");
97 return 1;
98 }
99 for (x = 0; x < 32; x++) {
100 if (buf[0][x] != x) {
101 fprintf(stderr, "Failed (contents)");
102 return 1;
103 }
104 }
105 /* test sign_hash */
106 for (x = 0; x < 16; x++) {
107 buf[0][x] = x;
108 }
109 x = sizeof (buf[1]);
110 DO(ecc_sign_hash (buf[0], 16, buf[1], &x, &yarrow_prng, find_prng ("yarrow"), &privKey));
111 DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &pubKey));
112 buf[0][0] ^= 1;
113 DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &privKey));
114 if (!(stat == 1 && stat2 == 0)) {
115 fprintf(stderr, "ecc_verify_hash failed %d, %d, ", stat, stat2);
116 return 1;
117 }
118 ecc_free (&usera);
119 ecc_free (&pubKey);
120 ecc_free (&privKey);
121 }
122 return 0;
123 }
124
125 #else
126
127 int ecc_tests(void)
128 {
129 fprintf(stderr, "NOP");
130 return 0;
131 }
132
133 #endif
134
135 /* $Source: /cvs/libtom/libtomcrypt/testprof/ecc_test.c,v $ */
136 /* $Revision: 1.9 $ */
137 /* $Date: 2005/06/14 19:43:29 $ */