comparison libtomcrypt/testprof/ecc_test.c @ 382:0cbe8f6dbf9e

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f) to branch 'au.asn.ucc.matt.dropbear' (head 02c413252c90e9de8e03d91e9939dde3029f5c0a)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:41:05 +0000
parents 1b9e69c058d2
children f849a5ca2efc
comparison
equal deleted inserted replaced
379:b66a00272a90 382:0cbe8f6dbf9e
1 #include <tomcrypt_test.h> 1 #include <tomcrypt_test.h>
2 2
3 #ifdef MECC 3 #ifdef MECC
4 4
5 static int sizes[] = { 5 static int sizes[] = {
6 #ifdef ECC112
7 14,
8 #endif
9 #ifdef ECC128
10 16,
11 #endif
12 #ifdef ECC160
13 20,
14 #endif
6 #ifdef ECC192 15 #ifdef ECC192
7 24, 16 24,
8 #endif 17 #endif
9 #ifdef ECC224 18 #ifdef ECC224
10 28, 19 28,
13 32, 22 32,
14 #endif 23 #endif
15 #ifdef ECC384 24 #ifdef ECC384
16 48, 25 48,
17 #endif 26 #endif
18 #ifdef ECC512 27 #ifdef ECC521
19 65 28 65
20 #endif 29 #endif
21 }; 30 };
31
32 #ifdef LTC_ECC_SHAMIR
33 int ecc_test_shamir(void)
34 {
35 void *modulus, *mp, *kA, *kB, *rA, *rB;
36 ecc_point *G, *A, *B, *C1, *C2;
37 int x, y, z;
38 unsigned char buf[ECC_BUF_SIZE];
39
40 DO(mp_init_multi(&kA, &kB, &rA, &rB, &modulus, NULL));
41 LTC_ARGCHK((G = ltc_ecc_new_point()) != NULL);
42 LTC_ARGCHK((A = ltc_ecc_new_point()) != NULL);
43 LTC_ARGCHK((B = ltc_ecc_new_point()) != NULL);
44 LTC_ARGCHK((C1 = ltc_ecc_new_point()) != NULL);
45 LTC_ARGCHK((C2 = ltc_ecc_new_point()) != NULL);
46
47 for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) {
48 /* get the base point */
49 for (z = 0; ltc_ecc_sets[z].name; z++) {
50 if (sizes[z] < ltc_ecc_sets[z].size) break;
51 }
52 LTC_ARGCHK(ltc_ecc_sets[z].name != NULL);
53
54 /* load it */
55 DO(mp_read_radix(G->x, ltc_ecc_sets[z].Gx, 16));
56 DO(mp_read_radix(G->y, ltc_ecc_sets[z].Gy, 16));
57 DO(mp_set(G->z, 1));
58 DO(mp_read_radix(modulus, ltc_ecc_sets[z].prime, 16));
59 DO(mp_montgomery_setup(modulus, &mp));
60
61 /* do 100 random tests */
62 for (y = 0; y < 100; y++) {
63 /* pick a random r1, r2 */
64 LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
65 DO(mp_read_unsigned_bin(rA, buf, sizes[x]));
66 LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
67 DO(mp_read_unsigned_bin(rB, buf, sizes[x]));
68
69 /* compute rA * G = A */
70 DO(ltc_mp.ecc_ptmul(rA, G, A, modulus, 1));
71
72 /* compute rB * G = B */
73 DO(ltc_mp.ecc_ptmul(rB, G, B, modulus, 1));
74
75 /* pick a random kA, kB */
76 LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
77 DO(mp_read_unsigned_bin(kA, buf, sizes[x]));
78 LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
79 DO(mp_read_unsigned_bin(kB, buf, sizes[x]));
80
81 /* now, compute kA*A + kB*B = C1 using the older method */
82 DO(ltc_mp.ecc_ptmul(kA, A, C1, modulus, 0));
83 DO(ltc_mp.ecc_ptmul(kB, B, C2, modulus, 0));
84 DO(ltc_mp.ecc_ptadd(C1, C2, C1, modulus, mp));
85 DO(ltc_mp.ecc_map(C1, modulus, mp));
86
87 /* now compute using mul2add */
88 DO(ltc_mp.ecc_mul2add(A, kA, B, kB, C2, modulus));
89
90 /* is they the sames? */
91 if ((mp_cmp(C1->x, C2->x) != LTC_MP_EQ) || (mp_cmp(C1->y, C2->y) != LTC_MP_EQ) || (mp_cmp(C1->z, C2->z) != LTC_MP_EQ)) {
92 fprintf(stderr, "ECC failed shamir test: size=%d, testno=%d\n", sizes[x], y);
93 return 1;
94 }
95 }
96 mp_montgomery_free(mp);
97 }
98 ltc_ecc_del_point(C2);
99 ltc_ecc_del_point(C1);
100 ltc_ecc_del_point(B);
101 ltc_ecc_del_point(A);
102 ltc_ecc_del_point(G);
103 mp_clear_multi(kA, kB, rA, rB, modulus, NULL);
104 return 0;
105 }
106 #endif
22 107
23 int ecc_tests (void) 108 int ecc_tests (void)
24 { 109 {
25 unsigned char buf[4][4096]; 110 unsigned char buf[4][4096];
26 unsigned long x, y, z, s; 111 unsigned long x, y, z, s;
27 int stat, stat2; 112 int stat, stat2;
28 ecc_key usera, userb, pubKey, privKey; 113 ecc_key usera, userb, pubKey, privKey;
29 114
30 DO(ecc_test ()); 115 DO(ecc_test ());
31 116 DO(ecc_test ());
32 for (s = 0; s < (int)(sizeof(sizes)/sizeof(sizes[0])); s++) { 117 DO(ecc_test ());
118 DO(ecc_test ());
119 DO(ecc_test ());
120
121 for (s = 0; s < (sizeof(sizes)/sizeof(sizes[0])); s++) {
33 /* make up two keys */ 122 /* make up two keys */
34 DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera)); 123 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)); 124 DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &userb));
36 125
37 /* make the shared secret */ 126 /* make the shared secret */
38 x = 4096; 127 x = sizeof(buf[0]);
39 DO(ecc_shared_secret (&usera, &userb, buf[0], &x)); 128 DO(ecc_shared_secret (&usera, &userb, buf[0], &x));
40 129
41 y = 4096; 130 y = sizeof(buf[1]);
42 DO(ecc_shared_secret (&userb, &usera, buf[1], &y)); 131 DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
43 132
44 if (y != x) { 133 if (y != x) {
45 fprintf(stderr, "ecc Shared keys are not same size."); 134 fprintf(stderr, "ecc Shared keys are not same size.");
46 return 1; 135 return 1;
50 fprintf(stderr, "ecc Shared keys not same contents."); 139 fprintf(stderr, "ecc Shared keys not same contents.");
51 return 1; 140 return 1;
52 } 141 }
53 142
54 /* now export userb */ 143 /* now export userb */
55 y = 4096; 144 y = sizeof(buf[0]);
56 DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb)); 145 DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb));
57 ecc_free (&userb); 146 ecc_free (&userb);
58 147
59 /* import and make the shared secret again */ 148 /* import and make the shared secret again */
60 DO(ecc_import (buf[1], y, &userb)); 149 DO(ecc_import (buf[1], y, &userb));
61 150
62 z = 4096; 151 z = sizeof(buf[0]);
63 DO(ecc_shared_secret (&usera, &userb, buf[2], &z)); 152 DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
64 153
65 if (z != x) { 154 if (z != x) {
66 fprintf(stderr, "failed. Size don't match?"); 155 fprintf(stderr, "failed. Size don't match?");
67 return 1; 156 return 1;
68 } 157 }
69 if (memcmp (buf[0], buf[2], x)) { 158 if (memcmp (buf[0], buf[2], x)) {
70 fprintf(stderr, "Failed. Contents didn't match."); 159 fprintf(stderr, "Failed. Contents didn't match.");
71 return 1; 160 return 1;
72 } 161 }
162
163 /* export with ANSI X9.63 */
164 y = sizeof(buf[1]);
165 DO(ecc_ansi_x963_export(&userb, buf[1], &y));
166 ecc_free (&userb);
167
168 /* now import the ANSI key */
169 DO(ecc_ansi_x963_import(buf[1], y, &userb));
170
171 /* shared secret */
172 z = sizeof(buf[0]);
173 DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
174
175 if (z != x) {
176 fprintf(stderr, "failed. Size don't match?");
177 return 1;
178 }
179 if (memcmp (buf[0], buf[2], x)) {
180 fprintf(stderr, "Failed. Contents didn't match.");
181 return 1;
182 }
183
73 ecc_free (&usera); 184 ecc_free (&usera);
74 ecc_free (&userb); 185 ecc_free (&userb);
75 186
76 /* test encrypt_key */ 187 /* test encrypt_key */
77 DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera)); 188 DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
117 } 228 }
118 ecc_free (&usera); 229 ecc_free (&usera);
119 ecc_free (&pubKey); 230 ecc_free (&pubKey);
120 ecc_free (&privKey); 231 ecc_free (&privKey);
121 } 232 }
233 #ifdef LTC_ECC_SHAMIR
234 return ecc_test_shamir();
235 #else
122 return 0; 236 return 0;
237 #endif
123 } 238 }
124 239
125 #else 240 #else
126 241
127 int ecc_tests(void) 242 int ecc_tests(void)
131 } 246 }
132 247
133 #endif 248 #endif
134 249
135 /* $Source: /cvs/libtom/libtomcrypt/testprof/ecc_test.c,v $ */ 250 /* $Source: /cvs/libtom/libtomcrypt/testprof/ecc_test.c,v $ */
136 /* $Revision: 1.9 $ */ 251 /* $Revision: 1.21 $ */
137 /* $Date: 2005/06/14 19:43:29 $ */ 252 /* $Date: 2006/12/04 03:21:03 $ */