]> git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_blind_id.c
make the signature on the private ID optional
[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_keys(d0_blind_id_t *ctx, int k)
227 {
228         d0_blind_id_clear(ctx);
229         CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_new());
230         CHECK(d0_dl_generate_key(k, ctx->schnorr_G));
231         CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(NULL, 65537));
232         CHECK_ASSIGN(ctx->rsa_d, d0_bignum_new());
233         CHECK_ASSIGN(ctx->rsa_n, d0_bignum_new());
234         CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
235         return 1;
236 fail:
237         return 0;
238 }
239
240 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_keys(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
241 {
242         d0_iobuf_t *in = d0_iobuf_open_read(inbuf, inbuflen);
243         d0_blind_id_clear(ctx);
244         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, NULL));
245         CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, NULL));
246         CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, NULL));
247         CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, NULL));
248         return d0_iobuf_close(in, NULL);
249
250 fail:
251         d0_iobuf_close(in, NULL);
252         return 0;
253 }
254
255 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_keys(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
256 {
257         d0_iobuf_t *in = d0_iobuf_open_read(inbuf, inbuflen);
258         d0_blind_id_clear(ctx);
259         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, NULL));
260         CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, NULL));
261         CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, NULL));
262         return d0_iobuf_close(in, NULL);
263
264 fail:
265         d0_iobuf_close(in, NULL);
266         return 0;
267 }
268
269 #define USING(x) if(!(ctx->x)) return 0
270 #define REPLACING(x)
271
272 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_keys(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
273 {
274         d0_iobuf_t *out;
275
276         USING(rsa_n); USING(rsa_e); USING(rsa_d);
277
278         out = d0_iobuf_open_write(outbuf, *outbuflen);
279         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
280         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
281         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
282         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
283         return d0_iobuf_close(out, outbuflen);
284
285 fail:
286         d0_iobuf_close(out, outbuflen);
287         return 0;
288 }
289
290 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_keys(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
291 {
292         d0_iobuf_t *out;
293
294         USING(rsa_n); USING(rsa_e); USING(rsa_d);
295
296         out = d0_iobuf_open_write(outbuf, *outbuflen);
297         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
298         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
299         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
300         return d0_iobuf_close(out, outbuflen);
301
302 fail:
303         if(!d0_iobuf_close(out, outbuflen))
304                 return 0;
305         return 0;
306 }
307
308 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
309 {
310         // temps: temp0 order
311         USING(schnorr_G);
312         REPLACING(schnorr_s); REPLACING(schnorr_4_to_s);
313
314         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
315         CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
316         CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_bignum_mod_pow(ctx->schnorr_4_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
317         CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_zero(ctx->schnorr_4_to_s_signature));
318         return 1;
319
320 fail:
321         return 0;
322 }
323
324 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
325 {
326         d0_iobuf_t *out;
327
328         // temps: temp0 temp1
329         USING(rsa_n); USING(rsa_e); USING(schnorr_4_to_s);
330         REPLACING(rn);
331
332         out = d0_iobuf_open_write(outbuf, *outbuflen);
333
334         CHECK_ASSIGN(ctx->rn, d0_bignum_rand_bit_atmost(ctx->rn, d0_bignum_size(ctx->rsa_n)));
335         CHECK(d0_bignum_mod_pow(temp0, ctx->rn, ctx->rsa_e, ctx->rsa_n));
336         CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_4_to_s, temp0, ctx->rsa_n));
337         CHECK(d0_iobuf_write_bignum(out, temp1));
338         return d0_iobuf_close(out, outbuflen);
339
340 fail:
341         d0_iobuf_close(out, outbuflen);
342         return 0;
343 }
344
345 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)
346 {
347         d0_iobuf_t *in;
348         d0_iobuf_t *out;
349
350         // temps: temp0 temp1
351         USING(rsa_d); USING(rsa_n);
352
353         in = d0_iobuf_open_read(inbuf, inbuflen);
354         out = d0_iobuf_open_write(outbuf, *outbuflen);
355
356         CHECK(d0_iobuf_read_bignum(in, temp0));
357         CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
358         CHECK(d0_iobuf_write_bignum(out, temp1));
359
360         d0_iobuf_close(in, NULL);
361         return d0_iobuf_close(out, outbuflen);
362
363 fail:
364         d0_iobuf_close(in, NULL);
365         d0_iobuf_close(out, outbuflen);
366         return 0;
367 }
368
369 WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
370 {
371         d0_iobuf_t *in;
372
373         // temps: temp0 temp1
374         USING(rn); USING(rsa_n);
375         REPLACING(schnorr_4_to_s_signature);
376
377         in = d0_iobuf_open_read(inbuf, inbuflen);
378
379         CHECK(d0_iobuf_read_bignum(in, temp0));
380         CHECK(d0_bignum_mod_inv(temp1, ctx->rn, ctx->rsa_n));
381         CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_4_to_s_signature, temp0, temp1, ctx->rsa_n));
382
383         return d0_iobuf_close(in, NULL);
384
385 fail:
386         d0_iobuf_close(in, NULL);
387         return 0;
388 }
389
390 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
391 {
392         d0_iobuf_t *in;
393
394         REPLACING(schnorr_s); REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
395
396         in = d0_iobuf_open_read(inbuf, inbuflen);
397
398         CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
399         CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
400         CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
401
402         return d0_iobuf_close(in, NULL);
403
404 fail:
405         d0_iobuf_close(in, NULL);
406         return 0;
407 }
408
409 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
410 {
411         d0_iobuf_t *in;
412
413         REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
414
415         in = d0_iobuf_open_read(inbuf, inbuflen);
416
417         CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
418         CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
419
420         return d0_iobuf_close(in, NULL);
421
422 fail:
423         d0_iobuf_close(in, NULL);
424         return 0;
425 }
426
427 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
428 {
429         d0_iobuf_t *out;
430
431         USING(schnorr_s); USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
432
433         out = d0_iobuf_open_write(outbuf, *outbuflen);
434
435         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
436         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
437         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
438
439         return d0_iobuf_close(out, outbuflen);
440
441 fail:
442         d0_iobuf_close(out, outbuflen);
443         return 0;
444 }
445
446 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
447 {
448         d0_iobuf_t *out;
449
450         USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
451
452         out = d0_iobuf_open_write(outbuf, *outbuflen);
453
454         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
455         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
456
457         return d0_iobuf_close(out, outbuflen);
458
459 fail:
460         d0_iobuf_close(out, outbuflen);
461         return 0;
462 }
463
464 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_id_t *ctx, int is_first, char *msg, size_t msglen, char *outbuf, size_t *outbuflen)
465 // start =
466 //   first run: send 4^s, 4^s signature
467 //   1. get random r, send HASH(4^r)
468 {
469         d0_iobuf_t *out;
470         unsigned char convbuf[1024];
471         d0_iobuf_t *conv;
472         size_t sz;
473
474         if(is_first)
475         {
476                 USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
477         }
478         USING(schnorr_G);
479         REPLACING(r);
480
481         out = d0_iobuf_open_write(outbuf, *outbuflen);
482
483         if(is_first)
484         {
485                 // send ID
486                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
487                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
488         }
489
490         // start schnorr ID scheme
491         // generate random number r; x = g^r; send hash of x, remember r, forget x
492         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
493         CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
494         CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
495
496         // hash it, hash it, everybody hash it
497         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
498         CHECK(d0_iobuf_write_bignum(conv, temp0));
499         CHECK(d0_iobuf_write_packet(conv, msg, msglen));
500         CHECK(d0_iobuf_write_bignum(conv, temp0));
501         d0_iobuf_close(conv, &sz);
502         conv = NULL;
503         CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
504         CHECK(d0_iobuf_write_packet(out, msg, msglen));
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_challenge(d0_blind_id_t *ctx, int is_first, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen, BOOL *status)
514 //   first run: get 4^s, 4^s signature
515 //   1. check sig
516 //   2. save HASH(4^r)
517 //   3. send challenge e of SCHNORR_BITS
518 {
519         d0_iobuf_t *in;
520         d0_iobuf_t *out;
521
522         if(is_first)
523         {
524                 REPLACING(schnorr_4_to_s); REPLACING(k); REPLACING(schnorr_4_to_s_signature);
525                 USING(schnorr_G); USING(rsa_n);
526         }
527         else
528         {
529                 USING(schnorr_4_to_s_signature); USING(schnorr_4_to_s);
530         }
531         USING(rsa_e); USING(rsa_n);
532         REPLACING(e); REPLACING(msg); REPLACING(msglen);
533
534         in = d0_iobuf_open_read(inbuf, inbuflen);
535         out = d0_iobuf_open_write(outbuf, *outbuflen);
536
537         if(is_first)
538         {
539                 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, NULL));
540                 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, zero) >= 0);
541                 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, ctx->schnorr_G) < 0);
542                 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
543                 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero) >= 0);
544                 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, ctx->rsa_n) < 0);
545
546                 // check signature of key (t = k^d, so, t^e = k)
547                 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_4_to_s_signature, ctx->rsa_e, ctx->rsa_n));
548                 if(d0_bignum_cmp(temp0, ctx->schnorr_4_to_s))
549                 {
550                         // accept the key anyway, but mark as failed signature! will later return 0 in status
551                         CHECK(d0_bignum_zero(ctx->schnorr_4_to_s_signature));
552                 }
553         }
554
555         CHECK(d0_iobuf_read_raw(in, ctx->xnbh, SCHNORR_HASHSIZE));
556         ctx->msglen = MSGSIZE;
557         CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
558
559         // send challenge
560         CHECK_ASSIGN(ctx->e, d0_bignum_rand_bit_atmost(ctx->e, SCHNORR_BITS));
561
562         CHECK(d0_iobuf_write_bignum(out, ctx->e));
563
564         if(status)
565                 *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
566
567         d0_iobuf_close(in, NULL);
568         return d0_iobuf_close(out, outbuflen);
569
570 fail:
571         d0_iobuf_close(in, NULL);
572         d0_iobuf_close(out, outbuflen);
573         return 0;
574 }
575
576 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)
577 //   1. read challenge e of SCHNORR_BITS
578 //   2. reply with r + s * e mod order
579 {
580         d0_iobuf_t *in;
581         d0_iobuf_t *out;
582
583         // temps: 0 order, 1 prod, 2 y, 3 e
584         USING(schnorr_G); USING(schnorr_s); USING(r);
585
586         in = d0_iobuf_open_read(inbuf, inbuflen);
587         out = d0_iobuf_open_write(outbuf, *outbuflen);
588
589         CHECK(d0_iobuf_read_bignum(in, temp3));
590         // TODO check if >= 2^SCHNORR_BITS or < 0, if yes, then fail (needed for zero knowledge)
591         CHECK(d0_bignum_cmp(temp3, zero) >= 0);
592         CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
593
594         // send response for schnorr ID scheme
595         // i.e. r + ctx->schnorr_s * temp3
596         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
597         CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
598         CHECK(d0_bignum_mod_add(temp2, temp1, ctx->r, temp0));
599         CHECK(d0_iobuf_write_bignum(out, temp2));
600
601         d0_iobuf_close(in, NULL);
602         return d0_iobuf_close(out, outbuflen);
603
604 fail:
605         d0_iobuf_close(in, NULL);
606         d0_iobuf_close(out, outbuflen);
607         return 0;
608 }
609
610 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)
611 //   1. read y = r + s * e mod order
612 //   2. verify: g^y (g^s)^-e = g^(r+s*e-s*e) = g^r
613 //      (check using H(g^r) which we know)
614 {
615         d0_iobuf_t *in;
616         unsigned char convbuf[1024];
617         d0_iobuf_t *conv;
618         size_t sz;
619
620         // temps: 0 y 1 order
621         USING(e); USING(schnorr_G);
622
623         in = d0_iobuf_open_read(inbuf, inbuflen);
624
625         *msglen = -1;
626         CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
627         CHECK(d0_iobuf_read_bignum(in, temp0));
628         CHECK(d0_bignum_cmp(temp0, zero) >= 0);
629         CHECK(d0_bignum_cmp(temp0, temp1) < 0);
630
631         // verify schnorr ID scheme
632         // we need 4^temp0 (g^s)^-e
633         CHECK(d0_bignum_neg(temp1, ctx->e));
634         CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_4_to_s, temp1, ctx->schnorr_G));
635         CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
636         CHECK(d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G));
637         // hash must be equal to xnbh
638
639         // hash it, hash it, everybody hash it
640         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
641         CHECK(d0_iobuf_write_bignum(conv, temp3));
642         CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
643         CHECK(d0_iobuf_write_bignum(conv, temp3));
644         d0_iobuf_close(conv, &sz);
645         conv = NULL;
646         if(memcmp(sha(convbuf, sz), ctx->xnbh, SCHNORR_HASHSIZE))
647         {
648                 // FAIL (not owned by player)
649                 goto fail;
650         }
651
652         if(status)
653                 *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
654
655         if(ctx->msglen <= (size_t) *msglen)
656                 memcpy(msg, ctx->msg, ctx->msglen);
657         else
658                 memcpy(msg, ctx->msg, *msglen);
659         *msglen = ctx->msglen;
660
661         d0_iobuf_close(in, NULL);
662         return 1;
663
664 fail:
665         d0_iobuf_close(in, NULL);
666         return 0;
667 }
668
669 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
670 {
671         d0_iobuf_t *out;
672         d0_iobuf_t *conv;
673         static unsigned char convbuf[1024];
674         size_t sz, n;
675
676         USING(schnorr_4_to_s);
677
678         out = d0_iobuf_open_write(outbuf, *outbuflen);
679         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
680
681         CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_4_to_s));
682         CHECK(d0_iobuf_close(conv, &sz));
683         conv = NULL;
684
685         n = (*outbuflen / 4) * 3;
686         if(n > SHA_DIGESTSIZE)
687                 n = SHA_DIGESTSIZE;
688         if(d0_iobuf_write_raw(out, sha(convbuf, sz), n) != n)
689                 goto fail;
690         if(!d0_iobuf_conv_base64_out(out))
691                 goto fail;
692
693         return d0_iobuf_close(out, outbuflen);
694
695 fail:
696         if(conv)
697                 if(!d0_iobuf_close(conv, &sz)) { }
698         if(!d0_iobuf_close(out, outbuflen))
699                 return 0;
700         return 0;
701 }
702
703 d0_blind_id_t *d0_blind_id_new(void)
704 {
705         d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
706         memset(b, 0, sizeof(*b));
707         return b;
708 }
709
710 void d0_blind_id_free(d0_blind_id_t *a)
711 {
712         d0_blind_id_clear(a);
713         d0_free(a);
714 }