]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - main.c
License: now entirely licensed under the 3-clause BSD license (as sha2.c already...
[xonotic/d0_blind_id.git] / main.c
1 /*
2  * FILE:        d0_iobuf.c
3  * AUTHOR:      Rudolf Polzer - divVerent@xonotic.org
4  * 
5  * Copyright (c) 2010, Rudolf Polzer
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holder nor the names of contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id$
33  */
34
35 #include "d0_blind_id.h"
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <time.h>
40 #include <sys/time.h>
41
42 void bench(double *b)
43 {
44         static struct timeval thistime, lasttime;
45         static double x = 0;
46         static double *lastclock = &x;
47         lasttime = thistime;
48         gettimeofday(&thistime, NULL);
49         *lastclock += (thistime.tv_sec - lasttime.tv_sec) + 0.000001 * (thistime.tv_usec - lasttime.tv_usec);
50         lastclock = b;
51 }
52
53 #ifndef WIN32
54 #include <sys/signal.h>
55 #endif
56 volatile BOOL quit = 0;
57 void mysignal(int signo)
58 {
59         (void) signo;
60         quit = 1;
61 }
62
63 #include <stdarg.h>
64 #include <stdlib.h>
65 static void errx(int status, const char *format, ...)
66 {
67         va_list ap;
68         va_start(ap, format);
69         vfprintf(stderr, format, ap);
70         fputs("\n", stderr);
71         exit(status);
72 }
73
74 int main(int argc, char **argv)
75 {
76         char buf[65536]; size_t bufsize;
77         char buf2[65536]; size_t buf2size;
78         d0_blind_id_t *ctx_self, *ctx_other;
79
80         d0_blind_id_INITIALIZE();
81         ctx_self = d0_blind_id_new();
82         ctx_other = d0_blind_id_new();
83
84         printf("keygen RSA...\n");
85         if(!d0_blind_id_generate_private_key(ctx_self, 1024))
86                 errx(1, "keygen fail");
87         buf2size = sizeof(buf2) - 1;
88         if(!d0_blind_id_fingerprint64_public_key(ctx_self, buf2, &buf2size))
89                 errx(2, "fp64 fail");
90         printf("key has fingerprint %s\n", buf2);
91         bufsize = sizeof(buf); if(!d0_blind_id_write_public_key(ctx_self, buf, &bufsize))
92                 errx(2, "writepub fail");
93         if(!d0_blind_id_read_public_key(ctx_other, buf, bufsize))
94                 errx(3, "readpub fail");
95
96         printf("keygen modulus...\n");
97         if(!d0_blind_id_generate_private_id_modulus(ctx_other))
98                 errx(1, "keygen fail");
99         /*
100         bufsize = sizeof(buf); if(!d0_blind_id_write_private_id_modulus(ctx_other, buf, &bufsize))
101                 errx(2, "writepub fail");
102         if(!d0_blind_id_read_private_id_modulus(ctx_self, buf, bufsize))
103                 errx(3, "readpub fail");
104         */
105
106 #ifndef WIN32
107         signal(SIGINT, mysignal);
108 #endif
109
110         int n = 0;
111         double bench_gen = 0, bench_fp = 0, bench_stop = 0;
112         do
113         {
114                 bench(&bench_gen);
115                 bufsize = sizeof(buf); if(!d0_blind_id_generate_private_id_start(ctx_other))
116                         errx(4, "genid fail");
117                 bench(&bench_fp);
118                 buf2size = sizeof(buf2) - 1; if(!d0_blind_id_fingerprint64_public_id(ctx_other, buf2, &buf2size))
119                         errx(4, "fp64 fail");
120                 bench(&bench_stop);
121                 if(n % 1024 == 0)
122                         printf("gen=%f fp=%f\n", n/bench_gen, n/bench_fp);
123                 ++n;
124         }
125         while(!(quit || argc != 2 || (buf2size > strlen(argv[1]) && !memcmp(buf2, argv[1], strlen(argv[1])))));
126
127         buf2[buf2size] = 0;
128         printf("Generated key has ID: %s\n", buf2);
129
130         bufsize = sizeof(buf); if(!d0_blind_id_generate_private_id_request(ctx_other, buf, &bufsize))
131                 errx(4, "genreq fail");
132         buf2size = sizeof(buf2); if(!d0_blind_id_answer_private_id_request(ctx_self, buf, bufsize, buf2, &buf2size))
133                 errx(5, "ansreq fail");
134         if(!d0_blind_id_finish_private_id_request(ctx_other, buf2, buf2size))
135                 errx(6, "finishreq fail");
136
137         bufsize = sizeof(buf); if(!d0_blind_id_write_public_id(ctx_other, buf, &bufsize))
138                 errx(7, "writepub2 fail");
139         if(!d0_blind_id_read_public_id(ctx_self, buf, bufsize))
140                 errx(8, "readpub2 fail");
141
142         n = 0;
143         double bench_auth = 0, bench_chall = 0, bench_resp = 0, bench_verify = 0, bench_dhkey1 = 0, bench_dhkey2 = 0;
144         BOOL status;
145         while(!quit)
146         {
147                 bench(&bench_auth);
148                 bufsize = sizeof(buf); if(!d0_blind_id_authenticate_with_private_id_start(ctx_other, 1, 1, "hello world", 11, buf, &bufsize))
149                         errx(9, "start fail");
150                 bench(&bench_chall);
151                 buf2size = sizeof(buf2); if(!d0_blind_id_authenticate_with_private_id_challenge(ctx_self, 1, 1, buf, bufsize, buf2, &buf2size, &status))
152                         errx(10, "challenge fail");
153                 if(!status)
154                         errx(14, "signature prefail");
155                 bench(&bench_resp);
156                 bufsize = sizeof(buf); if(!d0_blind_id_authenticate_with_private_id_response(ctx_other, buf2, buf2size, buf, &bufsize))
157                         errx(11, "response fail");
158                 bench(&bench_verify);
159                 buf2size = sizeof(buf2); if(!d0_blind_id_authenticate_with_private_id_verify(ctx_self, buf, bufsize, buf2, &buf2size, &status))
160                         errx(12, "verify fail");
161                 if(buf2size != 11 || memcmp(buf2, "hello world", 11))
162                         errx(13, "hello fail");
163                 if(!status)
164                         errx(14, "signature fail");
165                 bench(&bench_dhkey1);
166                 bufsize = 20; if(!d0_blind_id_sessionkey_public_id(ctx_self, buf, &bufsize))
167                         errx(15, "dhkey1 fail");
168                 bench(&bench_dhkey2);
169                 buf2size = 20; if(!d0_blind_id_sessionkey_public_id(ctx_other, buf2, &buf2size))
170                         errx(16, "dhkey2 fail");
171                 bench(&bench_stop);
172                 if(bufsize != buf2size || memcmp(buf, buf2, bufsize))
173                         errx(17, "dhkey match fail");
174                 ++n;
175                 if(n % 1024 == 0)
176                         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);
177         }
178
179         return 0;
180 }