comparison testprof/ecc_test.c @ 191:1c15b283127b libtomcrypt-orig

Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 May 2005 13:23:02 +0000
parents
children 39d5d58461d6
comparison
equal deleted inserted replaced
143:5d99163f7e32 191:1c15b283127b
1 #include <tomcrypt_test.h>
2
3 #ifdef MECC
4
5 int ecc_tests (void)
6 {
7 unsigned char buf[4][4096];
8 unsigned long x, y, z;
9 int stat, stat2;
10 ecc_key usera, userb, pubKey, privKey;
11
12 DO(ecc_test ());
13
14 /* make up two keys */
15 DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), 65, &usera));
16 DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), 65, &userb));
17
18 /* make the shared secret */
19 x = 4096;
20 DO(ecc_shared_secret (&usera, &userb, buf[0], &x));
21
22 y = 4096;
23 DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
24
25 if (y != x) {
26 printf ("ecc Shared keys are not same size.");
27 return 1;
28 }
29
30 if (memcmp (buf[0], buf[1], x)) {
31 printf ("ecc Shared keys not same contents.");
32 return 1;
33 }
34
35 /* now export userb */
36 y = 4096;
37 DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb));
38 ecc_free (&userb);
39
40 /* import and make the shared secret again */
41 DO(ecc_import (buf[1], y, &userb));
42
43 z = 4096;
44 DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
45
46 if (z != x) {
47 printf ("failed. Size don't match?");
48 return 1;
49 }
50 if (memcmp (buf[0], buf[2], x)) {
51 printf ("Failed. Content didn't match.");
52 return 1;
53 }
54 ecc_free (&usera);
55 ecc_free (&userb);
56
57 /* test encrypt_key */
58 DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), 65, &usera));
59
60 /* export key */
61 x = sizeof(buf[0]);
62 DO(ecc_export(buf[0], &x, PK_PUBLIC, &usera));
63 DO(ecc_import(buf[0], x, &pubKey));
64 x = sizeof(buf[0]);
65 DO(ecc_export(buf[0], &x, PK_PRIVATE, &usera));
66 DO(ecc_import(buf[0], x, &privKey));
67
68 for (x = 0; x < 32; x++) {
69 buf[0][x] = x;
70 }
71 y = sizeof (buf[1]);
72 DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &yarrow_prng, find_prng ("yarrow"), find_hash ("sha256"), &pubKey));
73 zeromem (buf[0], sizeof (buf[0]));
74 x = sizeof (buf[0]);
75 DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &privKey));
76 if (x != 32) {
77 printf ("Failed (length)");
78 return 1;
79 }
80 for (x = 0; x < 32; x++)
81 if (buf[0][x] != x) {
82 printf ("Failed (contents)");
83 return 1;
84 }
85 /* test sign_hash */
86 for (x = 0; x < 16; x++) {
87 buf[0][x] = x;
88 }
89 x = sizeof (buf[1]);
90 DO(ecc_sign_hash (buf[0], 16, buf[1], &x, &yarrow_prng, find_prng ("yarrow"), &privKey));
91 DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &pubKey));
92 buf[0][0] ^= 1;
93 DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &privKey));
94 if (!(stat == 1 && stat2 == 0)) {
95 printf("ecc_verify_hash failed %d, %d, ", stat, stat2);
96 return 1;
97 }
98 ecc_free (&usera);
99 ecc_free (&pubKey);
100 ecc_free (&privKey);
101 return 0;
102 }
103
104 #else
105
106 int ecc_tests(void)
107 {
108 printf("NOP");
109 return 0;
110 }
111
112 #endif