initial import of blind_id
[xonotic/d0_blind_id.git] / main.c
1 #include "d0_blind_id.h"
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <time.h>
6
7 void bench(double *b)
8 {
9         static struct timespec thistime, lasttime;
10         static double x = 0;
11         static double *lastclock = &x;
12         lasttime = thistime;
13         clock_gettime(CLOCK_MONOTONIC, &thistime);
14         *lastclock += (thistime.tv_sec - lasttime.tv_sec) + 0.000000001 * (thistime.tv_nsec - lasttime.tv_nsec);
15         lastclock = b;
16 }
17
18 #include <sys/signal.h>
19 volatile BOOL quit = 0;
20 void mysignal(int signo)
21 {
22         (void) signo;
23         quit = 1;
24 }
25
26 #include <err.h>
27 int main(int argc, char **argv)
28 {
29         char buf[65536]; size_t bufsize;
30         char buf2[65536]; size_t buf2size; ssize_t buf2ssize;
31         d0_blind_id_t *ctx_self, *ctx_other;
32
33         d0_blind_id_INITIALIZE();
34         ctx_self = d0_blind_id_new();
35         ctx_other = d0_blind_id_new();
36
37         if(!d0_blind_id_generate_private_keys(ctx_self, 1024))
38                 errx(1, "keygen fail");
39         bufsize = sizeof(buf); if(!d0_blind_id_write_public_keys(ctx_self, buf, &bufsize))
40                 errx(2, "writepub fail");
41         if(!d0_blind_id_read_public_keys(ctx_other, buf, bufsize))
42                 errx(3, "readpub fail");
43
44         signal(SIGINT, mysignal);
45
46         int n = 0;
47         double bench_gen = 0, bench_fp = 0, bench_stop = 0;
48         do
49         {
50                 bench(&bench_gen);
51                 bufsize = sizeof(buf); if(!d0_blind_id_generate_private_id_start(ctx_other))
52                         errx(4, "genid fail");
53                 bench(&bench_fp);
54                 buf2size = sizeof(buf2) - 1; if(!d0_blind_id_fingerprint64_public_id(ctx_other, buf2, &buf2size))
55                         errx(4, "fp64 fail");
56                 bench(&bench_stop);
57                 if(n % 1024 == 0)
58                         printf("gen=%f fp=%f\n", n/bench_gen, n/bench_fp);
59                 ++n;
60         }
61         while(!(quit || argc != 2 || (buf2size > strlen(argv[1]) && !memcmp(buf2, argv[1], strlen(argv[1])))));
62
63         buf2[buf2size] = 0;
64         printf("Generated key has ID: %s\n", buf2);
65
66         bufsize = sizeof(buf); if(!d0_blind_id_generate_private_id_request(ctx_other, buf, &bufsize))
67                 errx(4, "genreq fail");
68         buf2size = sizeof(buf2); if(!d0_blind_id_answer_private_id_request(ctx_self, buf, bufsize, buf2, &buf2size))
69                 errx(5, "ansreq fail");
70         if(!d0_blind_id_finish_private_id_request(ctx_other, buf2, buf2size))
71                 errx(6, "finishreq fail");
72
73         bufsize = sizeof(buf); if(!d0_blind_id_write_public_id(ctx_other, buf, &bufsize))
74                 errx(7, "writepub2 fail");
75         if(!d0_blind_id_read_public_id(ctx_self, buf, bufsize))
76                 errx(8, "readpub2 fail");
77
78         n = 0;
79         double bench_auth = 0, bench_chall = 0, bench_resp = 0, bench_verify = 0;
80         while(!quit)
81         {
82                 bench(&bench_auth);
83                 bufsize = sizeof(buf); if(!d0_blind_id_authenticate_with_private_id_start(ctx_other, 1, "hello world", 11, buf, &bufsize))
84                         errx(9, "start fail");
85                 bench(&bench_chall);
86                 buf2size = sizeof(buf2); if(!d0_blind_id_authenticate_with_private_id_challenge(ctx_self, 1, buf, bufsize, buf2, &buf2size))
87                         errx(10, "challenge fail");
88                 bench(&bench_resp);
89                 bufsize = sizeof(buf); if(!d0_blind_id_authenticate_with_private_id_response(ctx_other, buf2, buf2size, buf, &bufsize))
90                         errx(11, "response fail");
91                 bench(&bench_verify);
92                 buf2ssize = sizeof(buf2); if(!d0_blind_id_authenticate_with_private_id_verify(ctx_self, buf, bufsize, buf2, &buf2ssize))
93                         errx(12, "verify fail");
94                 if(buf2ssize != 11 || memcmp(buf2, "hello world", 11))
95                         errx(13, "hello fail");
96                 bench(&bench_stop);
97                 ++n;
98                 if(n % 1024 == 0)
99                         printf("auth=%f chall=%f resp=%f verify=%f\n", n/bench_auth, n/bench_chall, n/bench_resp, n/bench_verify);
100         }
101
102         return 0;
103 }