forked from telehash/telehash-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
165 lines (140 loc) · 3.46 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
#include "platform.h"
#include "crypt.h"
#include "ecc.h"
#include "aes.h"
#include "sha256.h"
#include "sha1.h"
#include "js0n.h"
#include "switch.h"
double timer()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;
}
void vli_print(uint8_t *p_vli, unsigned int p_size)
{
char buf[256];
printf("%s",util_hex(p_vli,p_size,(unsigned char*)buf));
}
void setup() {
crypt_init();
}
int ecc_test(int loops)
{
int i;
uint8_t l_private1[ECC_BYTES];
uint8_t l_private2[ECC_BYTES];
uint8_t l_public1[ECC_BYTES *2];
uint8_t l_public2[ECC_BYTES *2];
uint8_t l_secret1[ECC_BYTES];
uint8_t l_secret2[ECC_BYTES];
printf("Testing random private key pairs\n");
for(i=0; i<loops; ++i)
{
double a = timer();
ecc_make_key(l_public1, l_private1);
printf("%f",timer()-a);
printf(" gen\n");
a = timer();
ecc_make_key(l_public2, l_private2);
printf("%f",timer()-a);
printf(" gen\n");
a = timer();
if(!ecdh_shared_secret(l_public2, l_private1, l_secret1))
{
printf("shared_secret() failed (1)\n");
return 1;
}
printf("%f",timer()-a);
printf(" dh\n");
a = timer();
if(!ecdh_shared_secret(l_public1, l_private2, l_secret2))
{
printf("shared_secret() failed (2)\n");
return 1;
}
printf("%f",timer()-a);
printf(" dh\n");
if(memcmp(l_secret1, l_secret2, sizeof(l_secret1)) != 0)
{
printf("Shared secrets are not identical!\n");
printf("Shared secret 1 = ");
vli_print(l_secret1, ECC_BYTES);
printf("\n");
printf("Shared secret 2 = ");
vli_print(l_secret2, ECC_BYTES);
printf("\n");
printf("Private key 1 = ");
vli_print(l_private1, ECC_BYTES);
printf("\n");
printf("Private key 2 = ");
vli_print(l_private2, ECC_BYTES);
printf("\n");
}
}
printf("\n");
return 0;
}
void aes_test()
{
aes_context ctx;
unsigned char key[16], data[6],iv[16],block[16];
size_t off = 0;
memcpy(key,"1234567812345678",16);
memset(iv,0,16);
memcpy(data,"foobar",6);
aes_setkey_enc(&ctx,(unsigned char*)key,16);
aes_crypt_ctr(&ctx,6,&off,iv,block,data,data);
printf("aes ");
vli_print(data,6);
printf("\n");
}
void sha256_test()
{
unsigned char hash[32];
sha256((unsigned char*)"foo",3,hash,0);
printf("sha256 ");
vli_print(hash,32);
printf("\n");
}
void sha1_test()
{
unsigned char hash[20];
sha1((unsigned char*)"foo",3,hash);
printf("sha1 ");
vli_print(hash,20);
printf("\n");
}
void hmac_test()
{
unsigned char hmac[20];
sha1_hmac((unsigned char*)"foo",3,(unsigned char*)"bar",3,hmac);
printf("hmac ");
vli_print(hmac,20);
printf("\n");
}
void keygen()
{
printf("keygen ");
packet_t keys = packet_new();
crypt_keygen(0x1a,keys);
printf("%s",keys->json); //,keys->json_len);
printf("length=%d\n",keys->json_len);
}
int main() {
long start = timer();
keygen();
aes_test();
sha1_test();
hmac_test();
sha256_test();
printf("Time taken to complete keygen(), aes_test(), sha1_test(), hmac_test() & sha256_test()=%f\n", timer() - start);
ecc_test(5);
DEBUG_PRINTF(("test"));
printf("\n");
}