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