comparison testprof/ecc_test.c @ 209:39d5d58461d6 libtomcrypt-orig LTC_1.05

Import of libtomcrypt 1.05
author Matt Johnston <matt@ucc.asn.au>
date Wed, 06 Jul 2005 03:53:40 +0000
parents 1c15b283127b
children
comparison
equal deleted inserted replaced
191:1c15b283127b 209:39d5d58461d6
1 #include <tomcrypt_test.h> 1 #include <tomcrypt_test.h>
2 2
3 #ifdef MECC 3 #ifdef MECC
4 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
5 int ecc_tests (void) 23 int ecc_tests (void)
6 { 24 {
7 unsigned char buf[4][4096]; 25 unsigned char buf[4][4096];
8 unsigned long x, y, z; 26 unsigned long x, y, z, s;
9 int stat, stat2; 27 int stat, stat2;
10 ecc_key usera, userb, pubKey, privKey; 28 ecc_key usera, userb, pubKey, privKey;
11 29
12 DO(ecc_test ()); 30 DO(ecc_test ());
13 31
14 /* make up two keys */ 32 for (s = 0; s < (int)(sizeof(sizes)/sizeof(sizes[0])); s++) {
15 DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), 65, &usera)); 33 /* make up two keys */
16 DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), 65, &userb)); 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));
17 36
18 /* make the shared secret */ 37 /* make the shared secret */
19 x = 4096; 38 x = 4096;
20 DO(ecc_shared_secret (&usera, &userb, buf[0], &x)); 39 DO(ecc_shared_secret (&usera, &userb, buf[0], &x));
21 40
22 y = 4096; 41 y = 4096;
23 DO(ecc_shared_secret (&userb, &usera, buf[1], &y)); 42 DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
24 43
25 if (y != x) { 44 if (y != x) {
26 printf ("ecc Shared keys are not same size."); 45 fprintf(stderr, "ecc Shared keys are not same size.");
27 return 1; 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);
28 } 121 }
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; 122 return 0;
102 } 123 }
103 124
104 #else 125 #else
105 126
106 int ecc_tests(void) 127 int ecc_tests(void)
107 { 128 {
108 printf("NOP"); 129 fprintf(stderr, "NOP");
109 return 0; 130 return 0;
110 } 131 }
111 132
112 #endif 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 $ */