-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_strobe.c
373 lines (319 loc) · 10.3 KB
/
test_strobe.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* @cond internal
* @file test_strobe.c
* @copyright
* Copyright (c) 2016 Cryptography Research, Inc. \n
* Released under the MIT License. See LICENSE.txt for license information.
* @author Mike Hamburg
* @brief Test/example code for STROBE.
*/
#define _GNU_SOURCE 1
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "x25519.h"
#include "strobe.h"
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
/* FUTURE: should these go into header? */
static inline __attribute__((unused))
ssize_t strobe_put_and_steg_mac (
strobe_t strobe,
control_word_t cw,
const uint8_t *data,
ssize_t len,
uint16_t pad
) {
TRY( strobe_put(strobe, cw|FLAG_META_C|FLAG_C, data, len) );
TRY( strobe_put(strobe, MAC|FLAG_META_T|FLAG_META_C, NULL, pad + STROBE_INTEROP_MAC_BYTES) );
return len;
}
static inline __attribute__((unused))
ssize_t strobe_get_and_steg_mac (
strobe_t strobe,
control_word_t cw,
uint8_t *data,
ssize_t len
) {
ssize_t len2;
TRY( len = strobe_get(strobe, cw|FLAG_META_C|FLAG_C, data, len) );
TRY( len2 = strobe_get(strobe, MAC|FLAG_META_T|FLAG_META_C, NULL, -(1<<14)) );
if (len2 < STROBE_INTEROP_MAC_BYTES) return -1;
return len;
}
int strobe_x_mksocket_client (
uint16_t port
) {
struct sockaddr_in addr;
int sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1) { return -1; }
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_family = AF_INET;
addr.sin_port = htons( port );
if (connect(sock , (struct sockaddr *)&addr , sizeof(addr)) < 0) {
close(sock);
return -1;
}
return sock;
}
int strobe_x_mksocket_server (
uint16_t port
) {
struct sockaddr_in addr;
int option = 1;
int sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1) { return -1; }
if(setsockopt(sock, SOL_SOCKET,SO_REUSEADDR,(const char*)&option,sizeof(option)) < 0)
return -1;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_family = AF_INET;
addr.sin_port = htons( port );
if (bind(sock , (struct sockaddr *)&addr , sizeof(addr)) < 0) {
close(sock);
return -1;
}
if (listen(sock,1)) return -1;
int sock2 = accept(sock,NULL,NULL);
close(sock);
return sock2;
}
// FUTURE: combine sending.
static ssize_t cb_recv(strobe_io_ctx_s *ctx, const uint8_t **buffer, ssize_t size) {
uint8_t *a = ctx->a, *b = ctx->b;
if (size < 0) {
return -1;
} else if (size == 0) {
return 0;
} else if (size > b-a) {
size = b-a;
}
*buffer = a;
ssize_t avail = recv(ctx->fd, a, size, MSG_WAITALL);
if (avail == 0) avail = -1; // Cause an error on end of file
return avail;
}
static ssize_t cb_send(strobe_io_ctx_s *ctx, uint8_t **buffer, ssize_t size) {
(void)size; /* don't care how many bytes you're gonna write */
uint8_t *a = ctx->a, *b = ctx->b;
ssize_t ret;
if (*buffer > a && *buffer <= b) {
TRY(( ret = send(ctx->fd, a, *buffer-a, 0) ));
if (ret != *buffer-a) return -1;
}
*buffer = a;
return b-a;
}
const strobe_io_callbacks_s strobe_x_cb_socket = { cb_recv, cb_send };
static double now(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec/1000000.0;
}
int hexchar(char x) {
if (x>= '0' && x <= '9') return x-'0';
if (x>= 'a' && x <= 'f') return 10 + x-'a';
if (x>= 'A' && x <= 'F') return 10 + x-'A';
return -1;
}
int hex2bin(unsigned char *bin, size_t len, const char *hex) {
if (strlen(hex) != len*2) return -1;
unsigned int i;
int res = 0;
for (i=0; i<2*len; i++) {
int c = hexchar(hex[i]);
if (c<0) return -1;
res = 16*res+c;
if (i%2 == 1) {
*(bin++) = res;
res = 0;
}
}
return 0;
}
static const char *descr = "toy protocol";
uint8_t g_buffer[1024];
static void strobe_x_attach_socket(strobe_t strobe, int sock, uint8_t *buffer, size_t size) {
strobe->io = &strobe_x_cb_socket;
strobe->io_ctx.a = buffer;
strobe->io_ctx.b = buffer + size;
strobe->io_ctx.fd = sock;
}
int strobe_toy_client (
strobe_t strobe,
const uint8_t their_pubkey[32],
int sock
) {
strobe_init(strobe, (const uint8_t *)descr, strlen(descr));
strobe_x_attach_socket(strobe,sock,g_buffer,sizeof(g_buffer));
TRY( strobe_eph_ecdh(strobe, 1) );
TRY( strobe_session_verify(strobe, their_pubkey) );
return 0;
}
int strobe_sym_client_server (
strobe_t strobe,
int sock
) {
const char *descr0="hello", *key="my key";
strobe_init(strobe, (const uint8_t *)descr0, strlen(descr0));
strobe_put(strobe, SYM_KEY, (const uint8_t *)key, strlen(key));
strobe_x_attach_socket(strobe,sock,g_buffer,sizeof(g_buffer));
return 0;
}
int strobe_toy_server (
strobe_t strobe,
const uint8_t my_seckey[32],
int sock
) {
strobe_init(strobe, (const uint8_t *)descr, strlen(descr));
strobe_x_attach_socket(strobe,sock,g_buffer,sizeof(g_buffer));
TRY( strobe_eph_ecdh(strobe, 0) );
uint8_t my_pubkey[32];
x25519_base(my_pubkey, my_seckey,0);
TRY( strobe_session_sign(strobe, my_seckey, my_pubkey) );
return 0;
}
#ifdef TEST_KECCAK
void dokeccak(strobe_t sponge, uint8_t xor);
#endif
int echo_server(strobe_t strobe) {
int ret=0;
ssize_t get = 0;
const char *s_ = "I got your message! It was: ";
size_t off = strlen(s_);
unsigned char buffer[1024+1+off];
memset(buffer, 0, sizeof(buffer));
memcpy(buffer,s_,off);
while (ret >= 0) {
get = strobe_get(strobe,APP_CIPHERTEXT|FLAG_POST_MAC,&buffer[off],-(int)sizeof(buffer)-off);
if (get < 0) { ret = get; break; }
buffer[get+off] = 0;
printf("Received %zd %s\n", get, &buffer[off]);
printf("Sending %zd %s\n", get+off, buffer);
ret = strobe_put(strobe,APP_CIPHERTEXT|FLAG_POST_MAC,buffer,get+off);
}
return ret;
}
int echo_client(strobe_t strobe) {
char *linep = NULL;
size_t linep_size = 0;
int ret=0;
ssize_t get;
unsigned char buffer[1024];
memset(buffer, 0, sizeof(buffer));
while (ret >= 0) {
get = getline(&linep, &linep_size, stdin);
//printf("GET %d\n", get);
if (get <= 0) { ret = get; break; }
if (get > 1024) { ret = -1; break; }
if ((uint16_t)get != (size_t)get) { return -1; }
ret = strobe_put(strobe,APP_CIPHERTEXT|FLAG_POST_MAC,(uint8_t*)linep,get-1);
if (ret < 0) break;
get = strobe_get(strobe,APP_CIPHERTEXT|FLAG_POST_MAC,buffer,1-(int)sizeof(buffer));
if (get < 0) { ret = get; break; }
buffer[get] = 0;
printf("Received %zd %s\n", get, buffer);
}
free(linep);
return ret;
}
int main(int argc, char **argv) {
int ret = 0, sock = 0;
uint8_t pub[32], sec[32], seed[32]={0};
strobe_t strobe;
int randfd = open("/dev/urandom",O_RDONLY);
if (randfd < 0) {
printf("Can't open /dev/urandom: %s\n", strerror(errno));
return -1;
} else if ((ret = read(randfd,seed,sizeof(seed))) != sizeof(seed)) {
printf("Can't read /dev/urandom: %s\n", strerror(errno));
return -1;
}
strobe_seed_prng(seed,sizeof(seed));
if (argc == 3 && !strcmp(argv[1],"--client")) {
if (hex2bin(pub,sizeof(pub),argv[2])) {
printf("bad hex\n");
return -1;
}
sock = strobe_x_mksocket_client(4444);
if (sock < 0) ret = sock;
else ret = strobe_toy_client(strobe,pub,sock);
if (ret >= 0) ret = echo_client(strobe);
} else if (argc == 2 && !strcmp(argv[1],"--sym-client")) {
sock = strobe_x_mksocket_client(4444);
if (sock < 0) ret = sock;
else ret = strobe_sym_client_server(strobe,sock);
if (ret >= 0) ret = echo_client(strobe);
} else if (argc == 2 && !strcmp(argv[1],"--sym-server")) {
sock = strobe_x_mksocket_server(4444);
if (sock < 0) ret = sock;
else ret = strobe_sym_client_server(strobe,sock);
if (ret >= 0) ret = echo_server(strobe);
} else if (argc == 3 && !strcmp(argv[1],"--server")) {
if (hex2bin(sec,sizeof(sec),argv[2])) {
printf("bad hex\n");
return -1;
}
sock = strobe_x_mksocket_server(4444);
if (sock < 0) ret = sock;
else ret = strobe_toy_server(strobe,sec,sock);
if (ret >= 0) ret = echo_server(strobe);
} else if (argc == 2 && !strcmp(argv[1],"--keygen")) {
uint8_t pub[32],sec[32];
if (( ret = strobe_randomize(sec,sizeof(sec)) ) >= 0 ) {
int i;
printf("%s --server ", argv[0]);
for (i=0; i<32; i++) printf("%02x",sec[i]);
printf("\n");
x25519_base(pub,sec,0);
printf("%s --client ", argv[0]);
for (i=0; i<32; i++) printf("%02x",pub[i]);
printf("\n");
return 0;
}
} else if (argc == 2 && !strcmp(argv[1],"--bench")) {
const int BYTES = 10000, ROUNDS = 100;
uint8_t buffer[BYTES];
uint8_t buffer2[BYTES+4];
memset(buffer,0,sizeof(buffer));
strobe_init(strobe,(const unsigned char *)"benching",5);
double t = now();
for (int i=0; i<ROUNDS; i++) {
strobe_attach_buffer(strobe,buffer2,sizeof(buffer2));
int ret = strobe_operate(strobe,APP_CIPHERTEXT,buffer,sizeof(buffer));
if (ret<0) return -1;
}
t = now()-t;
printf("%dB/%0.3fs = %0.3fkB/s\n", BYTES*ROUNDS, t, BYTES*ROUNDS/t/1000);
return 0;
#ifdef TEST_KECCAK
} else if (argc == 2 && !strcmp(argv[1],"--tv")) {
memset(strobe,0,sizeof(strobe));
dokeccak(strobe,0);
for (int i=0; i<25*4; i++)
printf("%02x ", strobe->state->b[i]);
printf("\n");
#endif
} else {
printf(
"Usage: %s --keygen\n"
" %s --client key\n"
" %s --server key\n"
" %s --client-too key\n"
" %s --server-too key\n"
" %s --bench\n",
argv[0],argv[0],argv[0],
argv[0],argv[0],argv[0]
);
return -1;
}
if (sock) close(sock);
printf("ret = %d\n", ret);
return 0;
}