3 * AUTHOR: Rudolf Polzer - divVerent@xonotic.org
5 * Copyright (c) 2010, Rudolf Polzer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 #include "d0_blind_id.h"
45 static struct timeval thistime, lasttime;
47 static double *lastclock = &x;
49 gettimeofday(&thistime, NULL);
50 *lastclock += (thistime.tv_sec - lasttime.tv_sec) + 0.000001 * (thistime.tv_usec - lasttime.tv_usec);
55 #include <sys/signal.h>
57 volatile D0_BOOL quit = 0;
58 void mysignal(int signo)
66 static void errx(int status, const char *format, ...)
70 vfprintf(stderr, format, ap);
75 int main(int argc, char **argv)
77 char buf[65536]; size_t bufsize;
78 char buf2[65536]; size_t buf2size;
79 d0_blind_id_t *ctx_self, *ctx_other;
81 d0_blind_id_INITIALIZE();
82 ctx_self = d0_blind_id_new();
83 ctx_other = d0_blind_id_new();
85 printf("keygen RSA...\n");
86 if(!d0_blind_id_generate_private_key(ctx_self, 1024))
87 errx(1, "keygen fail");
88 buf2size = sizeof(buf2) - 1;
89 if(!d0_blind_id_fingerprint64_public_key(ctx_self, buf2, &buf2size))
91 printf("key has fingerprint %s\n", buf2);
92 bufsize = sizeof(buf); if(!d0_blind_id_write_public_key(ctx_self, buf, &bufsize))
93 errx(2, "writepub fail");
94 if(!d0_blind_id_read_public_key(ctx_other, buf, bufsize))
95 errx(3, "readpub fail");
97 printf("keygen modulus...\n");
98 if(!d0_blind_id_generate_private_id_modulus(ctx_other))
99 errx(1, "keygen fail");
101 bufsize = sizeof(buf); if(!d0_blind_id_write_private_id_modulus(ctx_other, buf, &bufsize))
102 errx(2, "writepub fail");
103 if(!d0_blind_id_read_private_id_modulus(ctx_self, buf, bufsize))
104 errx(3, "readpub fail");
108 signal(SIGINT, mysignal);
112 double bench_gen = 0, bench_fp = 0, bench_stop = 0;
116 bufsize = sizeof(buf); if(!d0_blind_id_generate_private_id_start(ctx_other))
117 errx(4, "genid fail");
119 buf2size = sizeof(buf2) - 1; if(!d0_blind_id_fingerprint64_public_id(ctx_other, buf2, &buf2size))
120 errx(4, "fp64 fail");
123 printf("gen=%f fp=%f\n", n/bench_gen, n/bench_fp);
126 while(!(quit || argc != 2 || (buf2size > strlen(argv[1]) && !memcmp(buf2, argv[1], strlen(argv[1])))));
129 printf("Generated key has ID: %s\n", buf2);
131 bufsize = sizeof(buf); if(!d0_blind_id_generate_private_id_request(ctx_other, buf, &bufsize))
132 errx(4, "genreq fail");
133 buf2size = sizeof(buf2); if(!d0_blind_id_answer_private_id_request(ctx_self, buf, bufsize, buf2, &buf2size))
134 errx(5, "ansreq fail");
135 if(!d0_blind_id_finish_private_id_request(ctx_other, buf2, buf2size))
136 errx(6, "finishreq fail");
138 bufsize = sizeof(buf); if(!d0_blind_id_write_public_id(ctx_other, buf, &bufsize))
139 errx(7, "writepub2 fail");
140 if(!d0_blind_id_read_public_id(ctx_self, buf, bufsize))
141 errx(8, "readpub2 fail");
144 double bench_auth = 0, bench_chall = 0, bench_resp = 0, bench_verify = 0, bench_dhkey1 = 0, bench_dhkey2 = 0, bench_sign = 0, bench_signverify = 0;
149 bufsize = sizeof(buf); if(!d0_blind_id_sign_with_private_id_sign(ctx_other, 1, 1, "hello world", 11, buf, &bufsize))
150 errx(9, "sign fail");
151 bench(&bench_signverify);
152 buf2size = sizeof(buf2); if(!d0_blind_id_sign_with_private_id_verify(ctx_self, 1, 1, buf, bufsize, buf2, &buf2size, &status))
153 errx(9, "signverify fail");
155 if(buf2size != 11 || memcmp(buf2, "hello world", 11))
156 errx(13, "signhello fail");
158 errx(14, "signsignature fail");
160 bufsize = sizeof(buf); if(!d0_blind_id_authenticate_with_private_id_start(ctx_other, 1, 1, "hello world", 11, buf, &bufsize))
161 errx(9, "start fail");
163 buf2size = sizeof(buf2); if(!d0_blind_id_authenticate_with_private_id_challenge(ctx_self, 1, 1, buf, bufsize, buf2, &buf2size, &status))
164 errx(10, "challenge fail");
166 errx(14, "signature prefail");
168 bufsize = sizeof(buf); if(!d0_blind_id_authenticate_with_private_id_response(ctx_other, buf2, buf2size, buf, &bufsize))
169 errx(11, "response fail");
170 bench(&bench_verify);
171 buf2size = sizeof(buf2); if(!d0_blind_id_authenticate_with_private_id_verify(ctx_self, buf, bufsize, buf2, &buf2size, &status))
172 errx(12, "verify fail");
173 if(buf2size != 11 || memcmp(buf2, "hello world", 11))
174 errx(13, "hello fail");
176 errx(14, "signature fail");
177 bench(&bench_dhkey1);
178 bufsize = 20; if(!d0_blind_id_sessionkey_public_id(ctx_self, buf, &bufsize))
179 errx(15, "dhkey1 fail");
180 bench(&bench_dhkey2);
181 buf2size = 20; if(!d0_blind_id_sessionkey_public_id(ctx_other, buf2, &buf2size))
182 errx(16, "dhkey2 fail");
184 if(bufsize != buf2size || memcmp(buf, buf2, bufsize))
185 errx(17, "dhkey match fail");
188 printf("sign=%f signverify=%f auth=%f chall=%f resp=%f verify=%f dh1=%f dh2=%f\n", n/bench_sign, n/bench_signverify, n/bench_auth, n/bench_chall, n/bench_resp, n/bench_verify, n/bench_dhkey1, n/bench_dhkey2);