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