]> git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_blind_id.c
673245525f1211f41a21a143656a0d0609b71d14
[xonotic/d0_blind_id.git] / d0_blind_id.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 "d0_bignum.h"
25 #include "sha1.h"
26
27 // for zero knowledge, we need multiple instances of schnorr ID scheme... should normally be sequential
28 // parallel schnorr ID is not provably zero knowledge :(
29 //   (evil verifier can know all questions in advance, so sequential is disadvantage for him)
30 // we'll just live with a 1:1048576 chance of cheating, and support reauthenticating
31
32 #define SCHNORR_BITS 20
33 // probability of cheat: 2^(-bits+1)
34
35 #define SCHNORR_HASHSIZE 3
36 // cannot be >= SHA_DIGEST_LENGTH
37 // *8 must be >= SCHNORR_BITS
38
39 #define MSGSIZE 640 // ought to be enough for anyone
40
41 struct d0_blind_id_s
42 {
43         // signing (Xonotic pub and priv key)
44         d0_bignum_t *rsa_n, *rsa_e, *rsa_d;
45
46         // public data (Schnorr ID)
47         d0_bignum_t *schnorr_G;
48
49         // private data (player ID private key)
50         d0_bignum_t *schnorr_s;
51
52         // public data (player ID public key, this is what the server gets to know)
53         d0_bignum_t *schnorr_4_to_s;
54         d0_bignum_t *schnorr_4_to_s_signature; // 0 when signature is invalid
55
56         // temp data
57         d0_bignum_t *rn; // random number blind signature
58         d0_bignum_t *r; // random number for schnorr ID
59         char xnbh[SCHNORR_HASHSIZE]; // init hash
60         d0_bignum_t *e; // challenge
61         char msg[MSGSIZE]; // message
62         size_t msglen; // message length
63 };
64
65 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
66 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
67
68 static d0_bignum_t *zero, *one, *four, *temp0, *temp1, *temp2, *temp3, *temp4;
69
70 void d0_blind_id_INITIALIZE(void)
71 {
72         d0_bignum_INITIALIZE();
73         CHECK_ASSIGN(zero, d0_bignum_int(zero, 0));
74         CHECK_ASSIGN(one, d0_bignum_int(one, 1));
75         CHECK_ASSIGN(four, d0_bignum_int(four, 4));
76         CHECK_ASSIGN(temp0, d0_bignum_int(temp0, 0));
77         CHECK_ASSIGN(temp1, d0_bignum_int(temp1, 0));
78         CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0));
79         CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0));
80         CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0));
81 fail:
82         ;
83 }
84
85 void d0_blind_id_SHUTDOWN(void)
86 {
87         d0_bignum_free(zero);
88         d0_bignum_free(one);
89         d0_bignum_free(four);
90         d0_bignum_free(temp0);
91         d0_bignum_free(temp1);
92         d0_bignum_free(temp2);
93         d0_bignum_free(temp3);
94         d0_bignum_free(temp4);
95         d0_bignum_SHUTDOWN();
96 }
97
98 // (G-1)/2
99 d0_bignum_t *d0_dl_get_order(d0_bignum_t *o, const d0_bignum_t *G)
100 {
101         CHECK_ASSIGN(o, d0_bignum_sub(o, G, one));
102         CHECK(d0_bignum_shl(o, o, -1)); // order o = (G-1)/2
103         return o;
104 fail:
105         return NULL;
106 }
107 // 2o+1
108 d0_bignum_t *d0_dl_get_from_order(d0_bignum_t *G, const d0_bignum_t *o)
109 {
110         CHECK_ASSIGN(G, d0_bignum_shl(G, o, 1));
111         CHECK(d0_bignum_add(G, G, one));
112         return G;
113 fail:
114         return NULL;
115 }
116
117 BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
118 {
119         // using: temp0
120         if(size < 16)
121                 size = 16;
122         for(;;)
123         {
124                 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
125                 if(d0_bignum_isprime(temp0, 0) == 0)
126                         continue;
127                 CHECK(d0_dl_get_from_order(G, temp0));
128                 if(d0_bignum_isprime(G, 10) == 0)
129                         continue;
130                 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
131                         continue;
132                 break;
133         }
134         return 1;
135 fail:
136         return 0;
137 }
138
139 BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *e, d0_bignum_t *d, d0_bignum_t *n)
140 {
141         // uses temp0 to temp4
142         int fail = 0;
143         int gcdfail = 0;
144         int pb = (size + 1)/2;
145         int qb = size - pb;
146         if(pb < 8)
147                 pb = 8;
148         if(qb < 8)
149                 qb = 8;
150         for (;;)
151         {
152                 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
153                 if(d0_bignum_isprime(temp0, 10) == 0)
154                         continue;
155                 CHECK(d0_bignum_sub(temp2, temp0, one));
156                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, e));
157                 if(!d0_bignum_cmp(temp4, one))
158                         break;
159                 if(++gcdfail == 3)
160                         return 0;
161                 ++gcdfail;
162         }
163         gcdfail = 0;
164         for (;;)
165         {
166                 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
167                 if(!d0_bignum_cmp(temp1, temp0))
168                 {
169                         if(++fail == 3)
170                                 return 0;
171                 }
172                 fail = 0;
173                 if(d0_bignum_isprime(temp1, 10) == 0)
174                         continue;
175                 CHECK(d0_bignum_sub(temp3, temp1, one));
176                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, e));
177                 if(!d0_bignum_cmp(temp4, one))
178                         break;
179                 if(++gcdfail == 3)
180                         return 0;
181                 ++gcdfail;
182         }
183
184         // n = temp0*temp1
185         CHECK(d0_bignum_mul(n, temp0, temp1));
186                 
187         // d = e^-1 mod (temp0-1)(temp1-1)
188         CHECK(d0_bignum_mul(temp0, temp2, temp3));
189         CHECK(d0_bignum_mod_inv(d, e, temp0));
190         return 1;
191 fail:
192         return 0;
193 }
194
195 void d0_blind_id_clear(d0_blind_id_t *ctx)
196 {
197         if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
198         if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
199         if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
200         if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
201         if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
202         if(ctx->schnorr_4_to_s) d0_bignum_free(ctx->schnorr_4_to_s);
203         if(ctx->schnorr_4_to_s_signature) d0_bignum_free(ctx->schnorr_4_to_s_signature);
204         if(ctx->rn) d0_bignum_free(ctx->rn);
205         if(ctx->r) d0_bignum_free(ctx->r);
206         if(ctx->e) d0_bignum_free(ctx->e);
207         memset(ctx, 0, sizeof(*ctx));
208 }
209
210 void d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
211 {
212         d0_blind_id_clear(ctx);
213         if(src->rsa_n) ctx->rsa_n = d0_bignum_mov(NULL, src->rsa_n);
214         if(src->rsa_e) ctx->rsa_e = d0_bignum_mov(NULL, src->rsa_e);
215         if(src->rsa_d) ctx->rsa_d = d0_bignum_mov(NULL, src->rsa_d);
216         if(src->schnorr_G) ctx->schnorr_G = d0_bignum_mov(NULL, src->schnorr_G);
217         if(src->schnorr_s) ctx->schnorr_s = d0_bignum_mov(NULL, src->schnorr_s);
218         if(src->schnorr_4_to_s) ctx->schnorr_4_to_s = d0_bignum_mov(NULL, ctx->schnorr_G);
219         if(src->schnorr_4_to_s_signature) ctx->schnorr_4_to_s_signature = d0_bignum_mov(NULL, src->schnorr_4_to_s_signature);
220         if(src->rn) ctx->rn = d0_bignum_mov(NULL, src->rn);
221         if(src->r) ctx->r = d0_bignum_mov(NULL, src->r);
222         if(src->e) ctx->e = d0_bignum_mov(NULL, src->e);
223         // TODO xnbh, msg, msglen?
224 }
225
226 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
227 {
228         d0_blind_id_clear(ctx);
229         CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
230         CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
231         CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
232         CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
233         return 1;
234 fail:
235         return 0;
236 }
237
238 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
239 {
240         d0_iobuf_t *in = d0_iobuf_open_read(inbuf, inbuflen);
241         CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
242         CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
243         CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
244         return d0_iobuf_close(in, NULL);
245
246 fail:
247         d0_iobuf_close(in, NULL);
248         return 0;
249 }
250
251 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
252 {
253         d0_iobuf_t *in = d0_iobuf_open_read(inbuf, inbuflen);
254         CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
255         CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
256         return d0_iobuf_close(in, NULL);
257
258 fail:
259         d0_iobuf_close(in, NULL);
260         return 0;
261 }
262
263 #define USING(x) if(!(ctx->x)) return 0
264 #define REPLACING(x)
265
266 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_key(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
267 {
268         d0_iobuf_t *out;
269
270         USING(rsa_n); USING(rsa_e); USING(rsa_d);
271
272         out = d0_iobuf_open_write(outbuf, *outbuflen);
273         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
274         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
275         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
276         return d0_iobuf_close(out, outbuflen);
277
278 fail:
279         d0_iobuf_close(out, outbuflen);
280         return 0;
281 }
282
283 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_key(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
284 {
285         d0_iobuf_t *out;
286
287         USING(rsa_n); USING(rsa_e);
288
289         out = d0_iobuf_open_write(outbuf, *outbuflen);
290         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
291         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
292         return d0_iobuf_close(out, outbuflen);
293
294 fail:
295         if(!d0_iobuf_close(out, outbuflen))
296                 return 0;
297         return 0;
298 }
299
300 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
301 {
302         USING(rsa_n);
303         REPLACING(schnorr_G);
304
305         CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
306         CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
307         return 1;
308 fail:
309         return 0;
310 }
311
312 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
313 {
314         d0_iobuf_t *in = d0_iobuf_open_read(inbuf, inbuflen);
315         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
316         return d0_iobuf_close(in, NULL);
317
318 fail:
319         d0_iobuf_close(in, NULL);
320         return 0;
321 }
322
323 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_modulus(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
324 {
325         d0_iobuf_t *out;
326
327         USING(schnorr_G);
328
329         out = d0_iobuf_open_write(outbuf, *outbuflen);
330         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
331         return d0_iobuf_close(out, outbuflen);
332
333 fail:
334         d0_iobuf_close(out, outbuflen);
335         return 0;
336 }
337
338 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
339 {
340         // temps: temp0 order
341         USING(schnorr_G);
342         REPLACING(schnorr_s); REPLACING(schnorr_4_to_s);
343
344         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
345         CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
346         CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_bignum_mod_pow(ctx->schnorr_4_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
347         CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_zero(ctx->schnorr_4_to_s_signature));
348         return 1;
349
350 fail:
351         return 0;
352 }
353
354 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
355 {
356         d0_iobuf_t *out;
357
358         // temps: temp0 temp1
359         USING(rsa_n); USING(rsa_e); USING(schnorr_4_to_s);
360         REPLACING(rn);
361
362         out = d0_iobuf_open_write(outbuf, *outbuflen);
363
364         CHECK_ASSIGN(ctx->rn, d0_bignum_rand_bit_atmost(ctx->rn, d0_bignum_size(ctx->rsa_n)));
365         CHECK(d0_bignum_mod_pow(temp0, ctx->rn, ctx->rsa_e, ctx->rsa_n));
366         CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_4_to_s, temp0, ctx->rsa_n));
367         CHECK(d0_iobuf_write_bignum(out, temp1));
368         return d0_iobuf_close(out, outbuflen);
369
370 fail:
371         d0_iobuf_close(out, outbuflen);
372         return 0;
373 }
374
375 WARN_UNUSED_RESULT BOOL d0_blind_id_answer_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen)
376 {
377         d0_iobuf_t *in;
378         d0_iobuf_t *out;
379
380         // temps: temp0 temp1
381         USING(rsa_d); USING(rsa_n);
382
383         in = d0_iobuf_open_read(inbuf, inbuflen);
384         out = d0_iobuf_open_write(outbuf, *outbuflen);
385
386         CHECK(d0_iobuf_read_bignum(in, temp0));
387         CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
388         CHECK(d0_iobuf_write_bignum(out, temp1));
389
390         d0_iobuf_close(in, NULL);
391         return d0_iobuf_close(out, outbuflen);
392
393 fail:
394         d0_iobuf_close(in, NULL);
395         d0_iobuf_close(out, outbuflen);
396         return 0;
397 }
398
399 WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
400 {
401         d0_iobuf_t *in;
402
403         // temps: temp0 temp1
404         USING(rn); USING(rsa_n);
405         REPLACING(schnorr_4_to_s_signature);
406
407         in = d0_iobuf_open_read(inbuf, inbuflen);
408
409         CHECK(d0_iobuf_read_bignum(in, temp0));
410         CHECK(d0_bignum_mod_inv(temp1, ctx->rn, ctx->rsa_n));
411         CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_4_to_s_signature, temp0, temp1, ctx->rsa_n));
412
413         return d0_iobuf_close(in, NULL);
414
415 fail:
416         d0_iobuf_close(in, NULL);
417         return 0;
418 }
419
420 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
421 {
422         d0_iobuf_t *in;
423
424         REPLACING(schnorr_s); REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
425
426         in = d0_iobuf_open_read(inbuf, inbuflen);
427
428         CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
429         CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
430         CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
431
432         return d0_iobuf_close(in, NULL);
433
434 fail:
435         d0_iobuf_close(in, NULL);
436         return 0;
437 }
438
439 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
440 {
441         d0_iobuf_t *in;
442
443         REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
444
445         in = d0_iobuf_open_read(inbuf, inbuflen);
446
447         CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
448         CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
449
450         return d0_iobuf_close(in, NULL);
451
452 fail:
453         d0_iobuf_close(in, NULL);
454         return 0;
455 }
456
457 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
458 {
459         d0_iobuf_t *out;
460
461         USING(schnorr_s); USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
462
463         out = d0_iobuf_open_write(outbuf, *outbuflen);
464
465         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
466         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
467         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
468
469         return d0_iobuf_close(out, outbuflen);
470
471 fail:
472         d0_iobuf_close(out, outbuflen);
473         return 0;
474 }
475
476 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
477 {
478         d0_iobuf_t *out;
479
480         USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
481
482         out = d0_iobuf_open_write(outbuf, *outbuflen);
483
484         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
485         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
486
487         return d0_iobuf_close(out, outbuflen);
488
489 fail:
490         d0_iobuf_close(out, outbuflen);
491         return 0;
492 }
493
494 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_id_t *ctx, BOOL is_first, BOOL send_modulus, char *msg, size_t msglen, char *outbuf, size_t *outbuflen)
495 // start =
496 //   first run: send 4^s, 4^s signature
497 //   1. get random r, send HASH(4^r)
498 {
499         d0_iobuf_t *out;
500         unsigned char convbuf[1024];
501         d0_iobuf_t *conv;
502         size_t sz;
503
504         if(is_first)
505         {
506                 USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
507         }
508         USING(schnorr_G);
509         REPLACING(r);
510
511         out = d0_iobuf_open_write(outbuf, *outbuflen);
512
513         if(is_first)
514         {
515                 // send ID
516                 if(send_modulus)
517                         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
518                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
519                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
520         }
521
522         // start schnorr ID scheme
523         // generate random number r; x = g^r; send hash of x, remember r, forget x
524         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
525         CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
526         CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
527
528         // hash it, hash it, everybody hash it
529         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
530         CHECK(d0_iobuf_write_bignum(conv, temp0));
531         CHECK(d0_iobuf_write_packet(conv, msg, msglen));
532         CHECK(d0_iobuf_write_bignum(conv, temp0));
533         d0_iobuf_close(conv, &sz);
534         conv = NULL;
535         CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
536         CHECK(d0_iobuf_write_packet(out, msg, msglen));
537
538         return d0_iobuf_close(out, outbuflen);
539
540 fail:
541         d0_iobuf_close(out, outbuflen);
542         return 0;
543 }
544
545 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_blind_id_t *ctx, BOOL is_first, BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen, BOOL *status)
546 //   first run: get 4^s, 4^s signature
547 //   1. check sig
548 //   2. save HASH(4^r)
549 //   3. send challenge e of SCHNORR_BITS
550 {
551         d0_iobuf_t *in;
552         d0_iobuf_t *out;
553
554         if(is_first)
555         {
556                 REPLACING(schnorr_4_to_s); REPLACING(k); REPLACING(schnorr_4_to_s_signature);
557                 USING(rsa_n);
558                 if(!recv_modulus)
559                         USING(schnorr_G);
560         }
561         else
562         {
563                 USING(schnorr_4_to_s_signature); USING(schnorr_4_to_s);
564                 USING(schnorr_G);
565         }
566         USING(rsa_e); USING(rsa_n);
567         REPLACING(e); REPLACING(msg); REPLACING(msglen);
568
569         in = d0_iobuf_open_read(inbuf, inbuflen);
570         out = d0_iobuf_open_write(outbuf, *outbuflen);
571
572         if(is_first)
573         {
574                 if(recv_modulus)
575                 {
576                         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
577                         CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
578                         CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
579                 }
580                 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
581                 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, zero) > 0);
582                 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, ctx->schnorr_G) < 0);
583                 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
584                 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero) >= 0);
585                 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, ctx->rsa_n) < 0);
586
587                 // check signature of key (t = k^d, so, t^e = k)
588                 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_4_to_s_signature, ctx->rsa_e, ctx->rsa_n));
589                 if(d0_bignum_cmp(temp0, ctx->schnorr_4_to_s))
590                 {
591                         // accept the key anyway, but mark as failed signature! will later return 0 in status
592                         CHECK(d0_bignum_zero(ctx->schnorr_4_to_s_signature));
593                 }
594         }
595
596         CHECK(d0_iobuf_read_raw(in, ctx->xnbh, SCHNORR_HASHSIZE));
597         ctx->msglen = MSGSIZE;
598         CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
599
600         // send challenge
601         CHECK_ASSIGN(ctx->e, d0_bignum_rand_bit_atmost(ctx->e, SCHNORR_BITS));
602
603         CHECK(d0_iobuf_write_bignum(out, ctx->e));
604
605         if(status)
606                 *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
607
608         d0_iobuf_close(in, NULL);
609         return d0_iobuf_close(out, outbuflen);
610
611 fail:
612         d0_iobuf_close(in, NULL);
613         d0_iobuf_close(out, outbuflen);
614         return 0;
615 }
616
617 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_response(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen)
618 //   1. read challenge e of SCHNORR_BITS
619 //   2. reply with r + s * e mod order
620 {
621         d0_iobuf_t *in;
622         d0_iobuf_t *out;
623
624         // temps: 0 order, 1 prod, 2 y, 3 e
625         USING(schnorr_G); USING(schnorr_s); USING(r);
626
627         in = d0_iobuf_open_read(inbuf, inbuflen);
628         out = d0_iobuf_open_write(outbuf, *outbuflen);
629
630         CHECK(d0_iobuf_read_bignum(in, temp3));
631         // TODO check if >= 2^SCHNORR_BITS or < 0, if yes, then fail (needed for zero knowledge)
632         CHECK(d0_bignum_cmp(temp3, zero) >= 0);
633         CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
634
635         // send response for schnorr ID scheme
636         // i.e. r + ctx->schnorr_s * temp3
637         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
638         CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
639         CHECK(d0_bignum_mod_add(temp2, temp1, ctx->r, temp0));
640         CHECK(d0_iobuf_write_bignum(out, temp2));
641
642         d0_iobuf_close(in, NULL);
643         return d0_iobuf_close(out, outbuflen);
644
645 fail:
646         d0_iobuf_close(in, NULL);
647         d0_iobuf_close(out, outbuflen);
648         return 0;
649 }
650
651 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_verify(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *msg, ssize_t *msglen, BOOL *status)
652 //   1. read y = r + s * e mod order
653 //   2. verify: g^y (g^s)^-e = g^(r+s*e-s*e) = g^r
654 //      (check using H(g^r) which we know)
655 {
656         d0_iobuf_t *in;
657         unsigned char convbuf[1024];
658         d0_iobuf_t *conv;
659         size_t sz;
660
661         // temps: 0 y 1 order
662         USING(e); USING(schnorr_G);
663
664         in = d0_iobuf_open_read(inbuf, inbuflen);
665
666         *msglen = -1;
667         CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
668         CHECK(d0_iobuf_read_bignum(in, temp0));
669         CHECK(d0_bignum_cmp(temp0, zero) >= 0);
670         CHECK(d0_bignum_cmp(temp0, temp1) < 0);
671
672         // verify schnorr ID scheme
673         // we need 4^temp0 (g^s)^-e
674         CHECK(d0_bignum_neg(temp1, ctx->e));
675         CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_4_to_s, temp1, ctx->schnorr_G));
676         CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
677         CHECK(d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G));
678         // hash must be equal to xnbh
679
680         // hash it, hash it, everybody hash it
681         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
682         CHECK(d0_iobuf_write_bignum(conv, temp3));
683         CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
684         CHECK(d0_iobuf_write_bignum(conv, temp3));
685         d0_iobuf_close(conv, &sz);
686         conv = NULL;
687         if(memcmp(sha(convbuf, sz), ctx->xnbh, SCHNORR_HASHSIZE))
688         {
689                 // FAIL (not owned by player)
690                 goto fail;
691         }
692
693         if(status)
694                 *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
695
696         if(ctx->msglen <= (size_t) *msglen)
697                 memcpy(msg, ctx->msg, ctx->msglen);
698         else
699                 memcpy(msg, ctx->msg, *msglen);
700         *msglen = ctx->msglen;
701
702         d0_iobuf_close(in, NULL);
703         return 1;
704
705 fail:
706         d0_iobuf_close(in, NULL);
707         return 0;
708 }
709
710 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
711 {
712         d0_iobuf_t *out;
713         d0_iobuf_t *conv;
714         static unsigned char convbuf[1024];
715         size_t sz, n;
716
717         USING(schnorr_4_to_s);
718
719         out = d0_iobuf_open_write(outbuf, *outbuflen);
720         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
721
722         CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_4_to_s));
723         CHECK(d0_iobuf_close(conv, &sz));
724         conv = NULL;
725
726         n = (*outbuflen / 4) * 3;
727         if(n > SHA_DIGESTSIZE)
728                 n = SHA_DIGESTSIZE;
729         if(d0_iobuf_write_raw(out, sha(convbuf, sz), n) != n)
730                 goto fail;
731         if(!d0_iobuf_conv_base64_out(out))
732                 goto fail;
733
734         return d0_iobuf_close(out, outbuflen);
735
736 fail:
737         if(conv)
738                 if(!d0_iobuf_close(conv, &sz)) { }
739         if(!d0_iobuf_close(out, outbuflen))
740                 return 0;
741         return 0;
742 }
743
744 d0_blind_id_t *d0_blind_id_new(void)
745 {
746         d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
747         memset(b, 0, sizeof(*b));
748         return b;
749 }
750
751 void d0_blind_id_free(d0_blind_id_t *a)
752 {
753         d0_blind_id_clear(a);
754         d0_free(a);
755 }