]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - main.c
fix a MITM attack in the protocol
[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 #include <sys/time.h>
26
27 void bench(double *b)
28 {
29         static struct timeval thistime, lasttime;
30         static double x = 0;
31         static double *lastclock = &x;
32         lasttime = thistime;
33         gettimeofday(&thistime, NULL);
34         *lastclock += (thistime.tv_sec - lasttime.tv_sec) + 0.000001 * (thistime.tv_usec - lasttime.tv_usec);
35         lastclock = b;
36 }
37
38 #ifndef WIN32
39 #include <sys/signal.h>
40 #endif
41 volatile BOOL quit = 0;
42 void mysignal(int signo)
43 {
44         (void) signo;
45         quit = 1;
46 }
47
48 #include <stdarg.h>
49 #include <stdlib.h>
50 static void errx(int status, const char *format, ...)
51 {
52         va_list ap;
53         va_start(ap, format);
54         vfprintf(stderr, format, ap);
55         fputs("\n", stderr);
56         exit(status);
57 }
58
59 int main(int argc, char **argv)
60 {
61         char buf[65536]; size_t bufsize;
62         char buf2[65536]; size_t buf2size;
63         d0_blind_id_t *ctx_self, *ctx_other;
64
65         d0_blind_id_INITIALIZE();
66         ctx_self = d0_blind_id_new();
67         ctx_other = d0_blind_id_new();
68
69         printf("keygen RSA...\n");
70         if(!d0_blind_id_generate_private_key(ctx_self, 1024))
71                 errx(1, "keygen fail");
72         buf2size = sizeof(buf2) - 1;
73         if(!d0_blind_id_fingerprint64_public_key(ctx_self, buf2, &buf2size))
74                 errx(2, "fp64 fail");
75         printf("key has fingerprint %s\n", buf2);
76         bufsize = sizeof(buf); if(!d0_blind_id_write_public_key(ctx_self, buf, &bufsize))
77                 errx(2, "writepub fail");
78         if(!d0_blind_id_read_public_key(ctx_other, buf, bufsize))
79                 errx(3, "readpub fail");
80
81         printf("keygen modulus...\n");
82         if(!d0_blind_id_generate_private_id_modulus(ctx_other))
83                 errx(1, "keygen fail");
84         /*
85         bufsize = sizeof(buf); if(!d0_blind_id_write_private_id_modulus(ctx_other, buf, &bufsize))
86                 errx(2, "writepub fail");
87         if(!d0_blind_id_read_private_id_modulus(ctx_self, buf, bufsize))
88                 errx(3, "readpub fail");
89         */
90
91 #ifndef WIN32
92         signal(SIGINT, mysignal);
93 #endif
94
95         int n = 0;
96         double bench_gen = 0, bench_fp = 0, bench_stop = 0;
97         do
98         {
99                 bench(&bench_gen);
100                 bufsize = sizeof(buf); if(!d0_blind_id_generate_private_id_start(ctx_other))
101                         errx(4, "genid fail");
102                 bench(&bench_fp);
103                 buf2size = sizeof(buf2) - 1; if(!d0_blind_id_fingerprint64_public_id(ctx_other, buf2, &buf2size))
104                         errx(4, "fp64 fail");
105                 bench(&bench_stop);
106                 if(n % 1024 == 0)
107                         printf("gen=%f fp=%f\n", n/bench_gen, n/bench_fp);
108                 ++n;
109         }
110         while(!(quit || argc != 2 || (buf2size > strlen(argv[1]) && !memcmp(buf2, argv[1], strlen(argv[1])))));
111
112         buf2[buf2size] = 0;
113         printf("Generated key has ID: %s\n", buf2);
114
115         bufsize = sizeof(buf); if(!d0_blind_id_generate_private_id_request(ctx_other, buf, &bufsize))
116                 errx(4, "genreq fail");
117         buf2size = sizeof(buf2); if(!d0_blind_id_answer_private_id_request(ctx_self, buf, bufsize, buf2, &buf2size))
118                 errx(5, "ansreq fail");
119         if(!d0_blind_id_finish_private_id_request(ctx_other, buf2, buf2size))
120                 errx(6, "finishreq fail");
121
122         bufsize = sizeof(buf); if(!d0_blind_id_write_public_id(ctx_other, buf, &bufsize))
123                 errx(7, "writepub2 fail");
124         if(!d0_blind_id_read_public_id(ctx_self, buf, bufsize))
125                 errx(8, "readpub2 fail");
126
127         n = 0;
128         double bench_auth = 0, bench_chall = 0, bench_resp = 0, bench_verify = 0, bench_dhkey1 = 0, bench_dhkey2 = 0;
129         BOOL status;
130         while(!quit)
131         {
132                 bench(&bench_auth);
133                 bufsize = sizeof(buf); if(!d0_blind_id_authenticate_with_private_id_start(ctx_other, 1, 1, "hello world", 11, buf, &bufsize))
134                         errx(9, "start fail");
135                 bench(&bench_chall);
136                 buf2size = sizeof(buf2); if(!d0_blind_id_authenticate_with_private_id_challenge(ctx_self, 1, 1, buf, bufsize, buf2, &buf2size, &status))
137                         errx(10, "challenge fail");
138                 if(!status)
139                         errx(14, "signature prefail");
140                 bench(&bench_resp);
141                 bufsize = sizeof(buf); if(!d0_blind_id_authenticate_with_private_id_response(ctx_other, buf2, buf2size, buf, &bufsize))
142                         errx(11, "response fail");
143                 bench(&bench_verify);
144                 buf2size = sizeof(buf2); if(!d0_blind_id_authenticate_with_private_id_verify(ctx_self, buf, bufsize, buf2, &buf2size, &status))
145                         errx(12, "verify fail");
146                 if(buf2size != 11 || memcmp(buf2, "hello world", 11))
147                         errx(13, "hello fail");
148                 if(!status)
149                         errx(14, "signature fail");
150                 bench(&bench_dhkey1);
151                 bufsize = 20; if(!d0_blind_id_sessionkey_public_id(ctx_self, buf, &bufsize))
152                         errx(15, "dhkey1 fail");
153                 bench(&bench_dhkey2);
154                 buf2size = 20; if(!d0_blind_id_sessionkey_public_id(ctx_other, buf2, &buf2size))
155                         errx(16, "dhkey2 fail");
156                 bench(&bench_stop);
157                 if(bufsize != buf2size || memcmp(buf, buf2, bufsize))
158                         errx(17, "dhkey match fail");
159                 ++n;
160                 if(n % 1024 == 0)
161                         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);
162         }
163
164         return 0;
165 }