33
int ecc_test_shamir(void)
35
void *modulus, *mp, *kA, *kB, *rA, *rB;
36
ecc_point *G, *A, *B, *C1, *C2;
38
unsigned char buf[ECC_BUF_SIZE];
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);
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;
52
LTC_ARGCHK(ltc_ecc_sets[z].name != NULL);
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));
58
DO(mp_read_radix(modulus, ltc_ecc_sets[z].prime, 16));
59
DO(mp_montgomery_setup(modulus, &mp));
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]));
69
/* compute rA * G = A */
70
DO(ltc_mp.ecc_ptmul(rA, G, A, modulus, 1));
72
/* compute rB * G = B */
73
DO(ltc_mp.ecc_ptmul(rB, G, B, modulus, 1));
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]));
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));
87
/* now compute using mul2add */
88
DO(ltc_mp.ecc_mul2add(A, kA, B, kB, C2, modulus));
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);
96
mp_montgomery_free(mp);
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);
23
108
int ecc_tests (void)
25
110
unsigned char buf[4][4096];
28
113
ecc_key usera, userb, pubKey, privKey;
32
for (s = 0; s < (int)(sizeof(sizes)/sizeof(sizes[0])); s++) {
121
for (s = 0; s < (sizeof(sizes)/sizeof(sizes[0])); s++) {
33
122
/* make up two keys */
34
123
DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
35
124
DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &userb));
37
126
/* make the shared secret */
39
128
DO(ecc_shared_secret (&usera, &userb, buf[0], &x));
42
131
DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
54
143
/* now export userb */
56
145
DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb));
57
146
ecc_free (&userb);
59
148
/* import and make the shared secret again */
60
149
DO(ecc_import (buf[1], y, &userb));
63
DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
66
fprintf(stderr, "failed. Size don't match?");
69
if (memcmp (buf[0], buf[2], x)) {
70
fprintf(stderr, "Failed. Contents didn't match.");
152
DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
155
fprintf(stderr, "failed. Size don't match?");
158
if (memcmp (buf[0], buf[2], x)) {
159
fprintf(stderr, "Failed. Contents didn't match.");
163
/* export with ANSI X9.63 */
165
DO(ecc_ansi_x963_export(&userb, buf[1], &y));
168
/* now import the ANSI key */
169
DO(ecc_ansi_x963_import(buf[1], y, &userb));
173
DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
176
fprintf(stderr, "failed. Size don't match?");
179
if (memcmp (buf[0], buf[2], x)) {
180
fprintf(stderr, "Failed. Contents didn't match.");
73
184
ecc_free (&usera);
74
185
ecc_free (&userb);