add a function to get a fingerprint of the public key (not id)
[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 #include <sys/signal.h>
39 volatile BOOL quit = 0;
40 void mysignal(int signo)
41 {
42         (void) signo;
43         quit = 1;
44 }
45
46 #include <err.h>
47 int main(int argc, char **argv)
48 {
49         char buf[65536]; size_t bufsize;
50         char buf2[65536]; size_t buf2size;
51         d0_blind_id_t *ctx_self, *ctx_other;
52
53         d0_blind_id_INITIALIZE();
54         ctx_self = d0_blind_id_new();
55         ctx_other = d0_blind_id_new();
56
57         printf("keygen RSA...\n");
58         if(!d0_blind_id_generate_private_key(ctx_self, 1024))
59                 errx(1, "keygen fail");
60         buf2size = sizeof(buf2) - 1;
61         if(!d0_blind_id_fingerprint64_public_key(ctx_self, buf2, &buf2size))
62                 errx(2, "fp64 fail");
63         printf("key has fingerprint %s\n", buf2);
64         bufsize = sizeof(buf); if(!d0_blind_id_write_public_key(ctx_self, buf, &bufsize))
65                 errx(2, "writepub fail");
66         if(!d0_blind_id_read_public_key(ctx_other, buf, bufsize))
67                 errx(3, "readpub fail");
68
69         printf("keygen modulus...\n");
70         if(!d0_blind_id_generate_private_id_modulus(ctx_other))
71                 errx(1, "keygen fail");
72         /*
73         bufsize = sizeof(buf); if(!d0_blind_id_write_private_id_modulus(ctx_other, buf, &bufsize))
74                 errx(2, "writepub fail");
75         if(!d0_blind_id_read_private_id_modulus(ctx_self, buf, bufsize))
76                 errx(3, "readpub fail");
77         */
78
79         signal(SIGINT, mysignal);
80
81         int n = 0;
82         double bench_gen = 0, bench_fp = 0, bench_stop = 0;
83         do
84         {
85                 bench(&bench_gen);
86                 bufsize = sizeof(buf); if(!d0_blind_id_generate_private_id_start(ctx_other))
87                         errx(4, "genid fail");
88                 bench(&bench_fp);
89                 buf2size = sizeof(buf2) - 1; if(!d0_blind_id_fingerprint64_public_id(ctx_other, buf2, &buf2size))
90                         errx(4, "fp64 fail");
91                 bench(&bench_stop);
92                 if(n % 1024 == 0)
93                         printf("gen=%f fp=%f\n", n/bench_gen, n/bench_fp);
94                 ++n;
95         }
96         while(!(quit || argc != 2 || (buf2size > strlen(argv[1]) && !memcmp(buf2, argv[1], strlen(argv[1])))));
97
98         buf2[buf2size] = 0;
99         printf("Generated key has ID: %s\n", buf2);
100
101         bufsize = sizeof(buf); if(!d0_blind_id_generate_private_id_request(ctx_other, buf, &bufsize))
102                 errx(4, "genreq fail");
103         buf2size = sizeof(buf2); if(!d0_blind_id_answer_private_id_request(ctx_self, buf, bufsize, buf2, &buf2size))
104                 errx(5, "ansreq fail");
105         if(!d0_blind_id_finish_private_id_request(ctx_other, buf2, buf2size))
106                 errx(6, "finishreq fail");
107
108         bufsize = sizeof(buf); if(!d0_blind_id_write_public_id(ctx_other, buf, &bufsize))
109                 errx(7, "writepub2 fail");
110         if(!d0_blind_id_read_public_id(ctx_self, buf, bufsize))
111                 errx(8, "readpub2 fail");
112
113         n = 0;
114         double bench_auth = 0, bench_chall = 0, bench_resp = 0, bench_verify = 0, bench_dhkey1 = 0, bench_dhkey2 = 0;
115         BOOL status;
116         while(!quit)
117         {
118                 bench(&bench_auth);
119                 bufsize = sizeof(buf); if(!d0_blind_id_authenticate_with_private_id_start(ctx_other, 1, 1, "hello world", 11, buf, &bufsize))
120                         errx(9, "start fail");
121                 bench(&bench_chall);
122                 buf2size = sizeof(buf2); if(!d0_blind_id_authenticate_with_private_id_challenge(ctx_self, 1, 1, buf, bufsize, buf2, &buf2size, NULL))
123                         errx(10, "challenge fail");
124                 bench(&bench_resp);
125                 bufsize = sizeof(buf); if(!d0_blind_id_authenticate_with_private_id_response(ctx_other, buf2, buf2size, buf, &bufsize))
126                         errx(11, "response fail");
127                 bench(&bench_verify);
128                 buf2size = sizeof(buf2); if(!d0_blind_id_authenticate_with_private_id_verify(ctx_self, buf, bufsize, buf2, &buf2size, &status))
129                         errx(12, "verify fail");
130                 if(buf2size != 11 || memcmp(buf2, "hello world", 11))
131                         errx(13, "hello fail");
132                 if(!status)
133                         errx(14, "signature fail");
134                 bench(&bench_dhkey1);
135                 bufsize = sizeof(buf); if(!d0_blind_id_sessionkey_public_id(ctx_self, buf, &bufsize))
136                         errx(15, "dhkey1 fail");
137                 bench(&bench_dhkey2);
138                 buf2size = sizeof(buf2); if(!d0_blind_id_sessionkey_public_id(ctx_other, buf2, &buf2size))
139                         errx(16, "dhkey2 fail");
140                 bench(&bench_stop);
141                 if(bufsize != buf2size || memcmp(buf, buf2, bufsize))
142                         errx(17, "dhkey match fail");
143                 ++n;
144                 if(n % 1024 == 0)
145                         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);
146         }
147
148         return 0;
149 }