embed a diffie hellmann key exchange
[xonotic/d0_blind_id.git] / main.c
1 /*
2 Blind-ID library for user identification using RSA blind signatures
3 Copyright (C) 2010  Rudolf Polzer
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 #include "d0_blind_id.h"
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <time.h>
25
26 void bench(double *b)
27 {
28         static struct timespec thistime, lasttime;
29         static double x = 0;
30         static double *lastclock = &x;
31         lasttime = thistime;
32         clock_gettime(CLOCK_MONOTONIC, &thistime);
33         *lastclock += (thistime.tv_sec - lasttime.tv_sec) + 0.000000001 * (thistime.tv_nsec - lasttime.tv_nsec);
34         lastclock = b;
35 }
36
37 #include <sys/signal.h>
38 volatile BOOL quit = 0;
39 void mysignal(int signo)
40 {
41         (void) signo;
42         quit = 1;
43 }
44
45 #include <err.h>
46 int main(int argc, char **argv)
47 {
48         char buf[65536]; size_t bufsize;
49         char buf2[65536]; size_t buf2size;
50         d0_blind_id_t *ctx_self, *ctx_other;
51
52         d0_blind_id_INITIALIZE();
53         ctx_self = d0_blind_id_new();
54         ctx_other = d0_blind_id_new();
55
56         printf("keygen RSA...\n");
57         if(!d0_blind_id_generate_private_key(ctx_self, 1024))
58                 errx(1, "keygen fail");
59         bufsize = sizeof(buf); if(!d0_blind_id_write_public_key(ctx_self, buf, &bufsize))
60                 errx(2, "writepub fail");
61         if(!d0_blind_id_read_public_key(ctx_other, buf, bufsize))
62                 errx(3, "readpub fail");
63
64         printf("keygen modulus...\n");
65         if(!d0_blind_id_generate_private_id_modulus(ctx_other))
66                 errx(1, "keygen fail");
67         /*
68         bufsize = sizeof(buf); if(!d0_blind_id_write_private_id_modulus(ctx_other, buf, &bufsize))
69                 errx(2, "writepub fail");
70         if(!d0_blind_id_read_private_id_modulus(ctx_self, buf, bufsize))
71                 errx(3, "readpub fail");
72         */
73
74         signal(SIGINT, mysignal);
75
76         int n = 0;
77         double bench_gen = 0, bench_fp = 0, bench_stop = 0;
78         do
79         {
80                 bench(&bench_gen);
81                 bufsize = sizeof(buf); if(!d0_blind_id_generate_private_id_start(ctx_other))
82                         errx(4, "genid fail");
83                 bench(&bench_fp);
84                 buf2size = sizeof(buf2) - 1; if(!d0_blind_id_fingerprint64_public_id(ctx_other, buf2, &buf2size))
85                         errx(4, "fp64 fail");
86                 bench(&bench_stop);
87                 if(n % 1024 == 0)
88                         printf("gen=%f fp=%f\n", n/bench_gen, n/bench_fp);
89                 ++n;
90         }
91         while(!(quit || argc != 2 || (buf2size > strlen(argv[1]) && !memcmp(buf2, argv[1], strlen(argv[1])))));
92
93         buf2[buf2size] = 0;
94         printf("Generated key has ID: %s\n", buf2);
95
96         bufsize = sizeof(buf); if(!d0_blind_id_generate_private_id_request(ctx_other, buf, &bufsize))
97                 errx(4, "genreq fail");
98         buf2size = sizeof(buf2); if(!d0_blind_id_answer_private_id_request(ctx_self, buf, bufsize, buf2, &buf2size))
99                 errx(5, "ansreq fail");
100         if(!d0_blind_id_finish_private_id_request(ctx_other, buf2, buf2size))
101                 errx(6, "finishreq fail");
102
103         bufsize = sizeof(buf); if(!d0_blind_id_write_public_id(ctx_other, buf, &bufsize))
104                 errx(7, "writepub2 fail");
105         if(!d0_blind_id_read_public_id(ctx_self, buf, bufsize))
106                 errx(8, "readpub2 fail");
107
108         n = 0;
109         double bench_auth = 0, bench_chall = 0, bench_resp = 0, bench_verify = 0, bench_dhkey1 = 0, bench_dhkey2 = 0;
110         BOOL status;
111         while(!quit)
112         {
113                 bench(&bench_auth);
114                 bufsize = sizeof(buf); if(!d0_blind_id_authenticate_with_private_id_start(ctx_other, 1, 1, "hello world", 11, buf, &bufsize))
115                         errx(9, "start fail");
116                 bench(&bench_chall);
117                 buf2size = sizeof(buf2); if(!d0_blind_id_authenticate_with_private_id_challenge(ctx_self, 1, 1, buf, bufsize, buf2, &buf2size, NULL))
118                         errx(10, "challenge fail");
119                 bench(&bench_resp);
120                 bufsize = sizeof(buf); if(!d0_blind_id_authenticate_with_private_id_response(ctx_other, 1, buf2, buf2size, buf, &bufsize))
121                         errx(11, "response fail");
122                 bench(&bench_verify);
123                 buf2size = sizeof(buf2); if(!d0_blind_id_authenticate_with_private_id_verify(ctx_self, buf, bufsize, buf2, &buf2size, &status))
124                         errx(12, "verify fail");
125                 if(buf2size != 11 || memcmp(buf2, "hello world", 11))
126                         errx(13, "hello fail");
127                 if(!status)
128                         errx(14, "signature fail");
129                 bench(&bench_dhkey1);
130                 bufsize = sizeof(buf); if(!d0_blind_id_sessionkey_public_id(ctx_self, buf, &bufsize))
131                         errx(15, "dhkey1 fail");
132                 bench(&bench_dhkey2);
133                 buf2size = sizeof(buf2); if(!d0_blind_id_sessionkey_public_id(ctx_other, buf2, &buf2size))
134                         errx(16, "dhkey2 fail");
135                 bench(&bench_stop);
136                 if(bufsize != buf2size || memcmp(buf, buf2, bufsize))
137                         errx(17, "dhkey match fail");
138                 ++n;
139                 if(n % 1024 == 0)
140                         printf("auth=%f chall=%f resp=%f verify=%f dh1=%f dh2=%f\n", n/bench_auth, n/bench_chall, n/bench_resp, n/bench_verify, n/bench_dhkey1, n/bench_dhkey2);
141         }
142
143         return 0;
144 }