15
|
1 #include "test.h" |
|
2 |
|
3 int ecc_tests (void) |
|
4 { |
|
5 unsigned char buf[4][4096]; |
|
6 unsigned long x, y, z; |
|
7 int stat, stat2; |
|
8 ecc_key usera, userb; |
|
9 |
|
10 DO(ecc_test ()); |
|
11 |
|
12 /* make up two keys */ |
|
13 DO(ecc_make_key (&test_yarrow, find_prng ("yarrow"), 24, &usera)); |
|
14 DO(ecc_make_key (&test_yarrow, find_prng ("yarrow"), 24, &userb)); |
|
15 |
|
16 /* make the shared secret */ |
|
17 x = 4096; |
|
18 DO(ecc_shared_secret (&usera, &userb, buf[0], &x)); |
|
19 |
|
20 y = 4096; |
|
21 DO(ecc_shared_secret (&userb, &usera, buf[1], &y)); |
|
22 |
|
23 if (y != x) { |
|
24 printf ("ecc Shared keys are not same size."); |
|
25 return 1; |
|
26 } |
|
27 |
|
28 if (memcmp (buf[0], buf[1], x)) { |
|
29 printf ("ecc Shared keys not same contents."); |
|
30 return 1; |
|
31 } |
|
32 |
|
33 /* now export userb */ |
|
34 y = 4096; |
|
35 DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb)); |
|
36 ecc_free (&userb); |
|
37 |
|
38 /* import and make the shared secret again */ |
|
39 DO(ecc_import (buf[1], y, &userb)); |
|
40 |
|
41 z = 4096; |
|
42 DO(ecc_shared_secret (&usera, &userb, buf[2], &z)); |
|
43 |
|
44 if (z != x) { |
|
45 printf ("failed. Size don't match?"); |
|
46 return 1; |
|
47 } |
|
48 if (memcmp (buf[0], buf[2], x)) { |
|
49 printf ("Failed. Content didn't match."); |
|
50 return 1; |
|
51 } |
|
52 ecc_free (&usera); |
|
53 ecc_free (&userb); |
|
54 |
|
55 /* test encrypt_key */ |
|
56 ecc_make_key (&test_yarrow, find_prng ("yarrow"), 20, &usera); |
|
57 for (x = 0; x < 32; x++) { |
|
58 buf[0][x] = x; |
|
59 } |
|
60 y = sizeof (buf[1]); |
|
61 DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &test_yarrow, find_prng ("yarrow"), find_hash ("sha256"), &usera)); |
|
62 zeromem (buf[0], sizeof (buf[0])); |
|
63 x = sizeof (buf[0]); |
|
64 DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &usera)); |
|
65 if (x != 32) { |
|
66 printf ("Failed (length)"); |
|
67 return 1; |
|
68 } |
|
69 for (x = 0; x < 32; x++) |
|
70 if (buf[0][x] != x) { |
|
71 printf ("Failed (contents)"); |
|
72 return 1; |
|
73 } |
|
74 /* test sign_hash */ |
|
75 for (x = 0; x < 16; x++) { |
|
76 buf[0][x] = x; |
|
77 } |
|
78 x = sizeof (buf[1]); |
|
79 DO(ecc_sign_hash (buf[0], 16, buf[1], &x, &test_yarrow, find_prng ("yarrow"), &usera)); |
|
80 DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &usera)); |
|
81 buf[0][0] ^= 1; |
|
82 DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera)); |
|
83 if (!(stat == 1 && stat2 == 0)) { |
|
84 printf("ecc_verify_hash failed"); |
|
85 return 1; |
|
86 } |
|
87 ecc_free (&usera); |
|
88 return 0; |
|
89 } |