]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_blind_id.c
9a1ddc36c6b989264d5bea2db58995e8c56f05af
[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 WARN_UNUSED_RESULT BOOL d0_longhash_destructive(d0_bignum_t *clobberme, char *outbuf, size_t *outbuflen)
202 {
203         d0_iobuf_t *out = NULL;
204         static unsigned char convbuf[1024];
205         d0_iobuf_t *conv = NULL;
206         size_t n, sz;
207
208         n = *outbuflen;
209         while(n > SHA_DIGESTSIZE)
210         {
211                 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
212                 CHECK(d0_iobuf_write_bignum(conv, temp0));
213                 CHECK(d0_iobuf_close(conv, &sz));
214                 conv = NULL;
215                 memcpy(outbuf, sha(convbuf, sz), SHA_DIGESTSIZE);
216                 outbuf += SHA_DIGESTSIZE;
217                 n -= SHA_DIGESTSIZE;
218                 CHECK(d0_bignum_add(temp0, temp0, one));
219         }
220         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
221         CHECK(d0_iobuf_write_bignum(conv, temp0));
222         CHECK(d0_iobuf_close(conv, &sz));
223         conv = NULL;
224         memcpy(outbuf, sha(convbuf, sz), n);
225
226         return d0_iobuf_close(out, outbuflen);
227
228 fail:
229         if(conv)
230                 d0_iobuf_close(conv, &sz);
231         return 0;
232 }
233
234 void d0_blind_id_clear(d0_blind_id_t *ctx)
235 {
236         if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
237         if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
238         if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
239         if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
240         if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
241         if(ctx->schnorr_4_to_s) d0_bignum_free(ctx->schnorr_4_to_s);
242         if(ctx->schnorr_4_to_s_signature) d0_bignum_free(ctx->schnorr_4_to_s_signature);
243         if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
244         if(ctx->r) d0_bignum_free(ctx->r);
245         if(ctx->challenge) d0_bignum_free(ctx->challenge);
246         if(ctx->other_4_to_r) d0_bignum_free(ctx->other_4_to_r);
247         memset(ctx, 0, sizeof(*ctx));
248 }
249
250 void d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
251 {
252         d0_blind_id_clear(ctx);
253         if(src->rsa_n) ctx->rsa_n = d0_bignum_mov(NULL, src->rsa_n);
254         if(src->rsa_e) ctx->rsa_e = d0_bignum_mov(NULL, src->rsa_e);
255         if(src->rsa_d) ctx->rsa_d = d0_bignum_mov(NULL, src->rsa_d);
256         if(src->schnorr_G) ctx->schnorr_G = d0_bignum_mov(NULL, src->schnorr_G);
257         if(src->schnorr_s) ctx->schnorr_s = d0_bignum_mov(NULL, src->schnorr_s);
258         if(src->schnorr_4_to_s) ctx->schnorr_4_to_s = d0_bignum_mov(NULL, ctx->schnorr_G);
259         if(src->schnorr_4_to_s_signature) ctx->schnorr_4_to_s_signature = d0_bignum_mov(NULL, src->schnorr_4_to_s_signature);
260         if(src->rsa_blind_signature_camouflage) ctx->rsa_blind_signature_camouflage = d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage);
261         if(src->r) ctx->r = d0_bignum_mov(NULL, src->r);
262         if(src->challenge) ctx->challenge = d0_bignum_mov(NULL, src->challenge);
263         if(src->other_4_to_r) ctx->other_4_to_r = d0_bignum_mov(NULL, src->other_4_to_r);
264         memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
265         ctx->msglen = src->msglen;
266         memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
267 }
268
269 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
270 {
271         REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
272
273         CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
274         CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
275         CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
276         CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
277         return 1;
278 fail:
279         return 0;
280 }
281
282 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
283 {
284         d0_iobuf_t *in = NULL;
285
286         REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
287
288         in = d0_iobuf_open_read(inbuf, inbuflen);
289
290         CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
291         CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
292         CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
293         return d0_iobuf_close(in, NULL);
294
295 fail:
296         d0_iobuf_close(in, NULL);
297         return 0;
298 }
299
300 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
301 {
302         d0_iobuf_t *in = NULL;
303
304         REPLACING(rsa_n); REPLACING(rsa_e);
305
306         in = d0_iobuf_open_read(inbuf, inbuflen);
307         CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
308         CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
309         return d0_iobuf_close(in, NULL);
310
311 fail:
312         d0_iobuf_close(in, NULL);
313         return 0;
314 }
315
316 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_key(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
317 {
318         d0_iobuf_t *out = NULL;
319
320         USING(rsa_n); USING(rsa_e); USING(rsa_d);
321
322         out = d0_iobuf_open_write(outbuf, *outbuflen);
323         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
324         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
325         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
326         return d0_iobuf_close(out, outbuflen);
327
328 fail:
329         d0_iobuf_close(out, outbuflen);
330         return 0;
331 }
332
333 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_key(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
334 {
335         d0_iobuf_t *out = NULL;
336
337         USING(rsa_n); USING(rsa_e);
338
339         out = d0_iobuf_open_write(outbuf, *outbuflen);
340         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
341         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
342         return d0_iobuf_close(out, outbuflen);
343
344 fail:
345         if(!d0_iobuf_close(out, outbuflen))
346                 return 0;
347         return 0;
348 }
349
350 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_key(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
351 {
352         d0_iobuf_t *out = NULL;
353         static unsigned char convbuf[2048];
354         d0_iobuf_t *conv = NULL;
355         size_t sz, n;
356
357         USING(rsa_n); USING(rsa_e);
358
359         out = d0_iobuf_open_write(outbuf, *outbuflen);
360         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
361
362         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
363         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
364         CHECK(d0_iobuf_close(conv, &sz));
365         conv = NULL;
366
367         n = (*outbuflen / 4) * 3;
368         if(n > SHA_DIGESTSIZE)
369                 n = SHA_DIGESTSIZE;
370         CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
371         CHECK(d0_iobuf_conv_base64_out(out));
372
373         return d0_iobuf_close(out, outbuflen);
374
375 fail:
376         if(conv)
377                 d0_iobuf_close(conv, &sz);
378         d0_iobuf_close(out, outbuflen);
379         return 0;
380 }
381
382 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
383 {
384         USING(rsa_n);
385         REPLACING(schnorr_G);
386
387         CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
388         CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
389         return 1;
390 fail:
391         return 0;
392 }
393
394 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
395 {
396         d0_iobuf_t *in = NULL;
397
398         REPLACING(schnorr_G);
399
400         in = d0_iobuf_open_read(inbuf, inbuflen);
401         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
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_write_private_id_modulus(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
410 {
411         d0_iobuf_t *out = NULL;
412
413         USING(schnorr_G);
414
415         out = d0_iobuf_open_write(outbuf, *outbuflen);
416         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
417         return d0_iobuf_close(out, outbuflen);
418
419 fail:
420         d0_iobuf_close(out, outbuflen);
421         return 0;
422 }
423
424 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
425 {
426         // temps: temp0 = order
427         USING(schnorr_G);
428         REPLACING(schnorr_s); REPLACING(schnorr_4_to_s);
429
430         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
431         CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
432         CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_bignum_mod_pow(ctx->schnorr_4_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
433         CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_zero(ctx->schnorr_4_to_s_signature));
434         return 1;
435
436 fail:
437         return 0;
438 }
439
440 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
441 {
442         d0_iobuf_t *out = NULL;
443
444         // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
445         USING(rsa_n); USING(rsa_e); USING(schnorr_4_to_s);
446         REPLACING(rsa_blind_signature_camouflage);
447
448         out = d0_iobuf_open_write(outbuf, *outbuflen);
449
450         CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
451         CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
452         CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_4_to_s, temp0, ctx->rsa_n));
453         CHECK(d0_iobuf_write_bignum(out, temp1));
454         return d0_iobuf_close(out, outbuflen);
455
456 fail:
457         d0_iobuf_close(out, outbuflen);
458         return 0;
459 }
460
461 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)
462 {
463         d0_iobuf_t *in = NULL;
464         d0_iobuf_t *out = NULL;
465
466         // temps: temp0 input, temp1 temp0^d
467         USING(rsa_d); USING(rsa_n);
468
469         in = d0_iobuf_open_read(inbuf, inbuflen);
470         out = d0_iobuf_open_write(outbuf, *outbuflen);
471
472         CHECK(d0_iobuf_read_bignum(in, temp0));
473         CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
474         CHECK(d0_iobuf_write_bignum(out, temp1));
475
476         d0_iobuf_close(in, NULL);
477         return d0_iobuf_close(out, outbuflen);
478
479 fail:
480         d0_iobuf_close(in, NULL);
481         d0_iobuf_close(out, outbuflen);
482         return 0;
483 }
484
485 WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
486 {
487         d0_iobuf_t *in = NULL;
488
489         // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
490         USING(rsa_blind_signature_camouflage); USING(rsa_n);
491         REPLACING(schnorr_4_to_s_signature);
492
493         in = d0_iobuf_open_read(inbuf, inbuflen);
494
495         CHECK(d0_iobuf_read_bignum(in, temp0));
496         CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
497         CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_4_to_s_signature, temp0, temp1, ctx->rsa_n));
498
499         return d0_iobuf_close(in, NULL);
500
501 fail:
502         d0_iobuf_close(in, NULL);
503         return 0;
504 }
505
506 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_request_camouflage(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
507 {
508         d0_iobuf_t *in = NULL;
509
510         REPLACING(rsa_blind_signature_camouflage);
511
512         in = d0_iobuf_open_read(inbuf, inbuflen);
513
514         CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_iobuf_read_bignum(in, ctx->rsa_blind_signature_camouflage));
515
516         return d0_iobuf_close(in, NULL);
517
518 fail:
519         d0_iobuf_close(in, NULL);
520         return 0;
521 }
522
523 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_request_camouflage(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
524 {
525         d0_iobuf_t *out = NULL;
526
527         USING(rsa_blind_signature_camouflage);
528
529         out = d0_iobuf_open_write(outbuf, *outbuflen);
530
531         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_blind_signature_camouflage));
532
533         return d0_iobuf_close(out, outbuflen);
534
535 fail:
536         d0_iobuf_close(out, outbuflen);
537         return 0;
538 }
539
540 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
541 {
542         d0_iobuf_t *in = NULL;
543
544         REPLACING(schnorr_s); REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
545
546         in = d0_iobuf_open_read(inbuf, inbuflen);
547
548         CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
549         CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
550         CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
551
552         return d0_iobuf_close(in, NULL);
553
554 fail:
555         d0_iobuf_close(in, NULL);
556         return 0;
557 }
558
559 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
560 {
561         d0_iobuf_t *in = NULL;
562
563         REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
564
565         in = d0_iobuf_open_read(inbuf, inbuflen);
566
567         CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
568         CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
569
570         return d0_iobuf_close(in, NULL);
571
572 fail:
573         d0_iobuf_close(in, NULL);
574         return 0;
575 }
576
577 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
578 {
579         d0_iobuf_t *out = NULL;
580
581         USING(schnorr_s); USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
582
583         out = d0_iobuf_open_write(outbuf, *outbuflen);
584
585         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
586         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
587         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
588
589         return d0_iobuf_close(out, outbuflen);
590
591 fail:
592         d0_iobuf_close(out, outbuflen);
593         return 0;
594 }
595
596 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
597 {
598         d0_iobuf_t *out = NULL;
599
600         USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
601
602         out = d0_iobuf_open_write(outbuf, *outbuflen);
603
604         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
605         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
606
607         return d0_iobuf_close(out, outbuflen);
608
609 fail:
610         d0_iobuf_close(out, outbuflen);
611         return 0;
612 }
613
614 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)
615 // start =
616 //   first run: send 4^s, 4^s signature
617 //   1. get random r, send HASH(4^r)
618 {
619         d0_iobuf_t *out = NULL;
620         static unsigned char convbuf[1024];
621         d0_iobuf_t *conv = NULL;
622         size_t sz = 0;
623
624         // temps: temp0 order, temp0 4^r
625         if(is_first)
626         {
627                 USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
628         }
629         USING(schnorr_G);
630         REPLACING(r);
631
632         out = d0_iobuf_open_write(outbuf, *outbuflen);
633
634         if(is_first)
635         {
636                 // send ID
637                 if(send_modulus)
638                         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
639                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
640                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
641         }
642
643         // start schnorr ID scheme
644         // generate random number r; x = g^r; send hash of x, remember r, forget x
645         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
646         CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
647         CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
648
649         // hash it, hash it, everybody hash it
650         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
651         CHECK(d0_iobuf_write_bignum(conv, temp0));
652         CHECK(d0_iobuf_write_packet(conv, msg, msglen));
653         CHECK(d0_iobuf_write_bignum(conv, temp0));
654         d0_iobuf_close(conv, &sz);
655         conv = NULL;
656         CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
657         CHECK(d0_iobuf_write_packet(out, msg, msglen));
658
659         return d0_iobuf_close(out, outbuflen);
660
661 fail:
662         d0_iobuf_close(out, outbuflen);
663         return 0;
664 }
665
666 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)
667 //   first run: get 4^s, 4^s signature
668 //   1. check sig
669 //   2. save HASH(4^r)
670 //   3. send challenge challenge of SCHNORR_BITS
671 {
672         d0_iobuf_t *in = NULL;
673         d0_iobuf_t *out = NULL;
674
675         // temps: temp0 order, temp0 signature check
676         if(is_first)
677         {
678                 REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
679                 if(recv_modulus)
680                         REPLACING(schnorr_G);
681                 else
682                         USING(schnorr_G);
683         }
684         else
685         {
686                 USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
687                 USING(schnorr_G);
688         }
689         USING(rsa_e); USING(rsa_n);
690         REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r);
691
692         in = d0_iobuf_open_read(inbuf, inbuflen);
693         out = d0_iobuf_open_write(outbuf, *outbuflen);
694
695         if(is_first)
696         {
697                 if(recv_modulus)
698                 {
699                         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
700                         CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
701                         CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
702                 }
703                 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
704                 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, zero) > 0);
705                 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, ctx->schnorr_G) < 0);
706                 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
707                 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero) >= 0);
708                 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, ctx->rsa_n) < 0);
709
710                 // check signature of key (t = k^d, so, t^challenge = k)
711                 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_4_to_s_signature, ctx->rsa_e, ctx->rsa_n));
712                 if(d0_bignum_cmp(temp0, ctx->schnorr_4_to_s))
713                 {
714                         // accept the key anyway, but mark as failed signature! will later return 0 in status
715                         CHECK(d0_bignum_zero(ctx->schnorr_4_to_s_signature));
716                 }
717         }
718
719         CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
720         ctx->msglen = MSGSIZE;
721         CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
722
723         // send challenge
724         CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
725
726         CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
727
728         // Diffie Hellmann
729         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
730         CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
731         CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
732         CHECK(d0_iobuf_write_bignum(out, temp0));
733
734         if(status)
735                 *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
736
737         d0_iobuf_close(in, NULL);
738         return d0_iobuf_close(out, outbuflen);
739
740 fail:
741         d0_iobuf_close(in, NULL);
742         d0_iobuf_close(out, outbuflen);
743         return 0;
744 }
745
746 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)
747 //   1. read challenge challenge of SCHNORR_BITS
748 //   2. reply with r + s * challenge mod order
749 {
750         d0_iobuf_t *in = NULL;
751         d0_iobuf_t *out = NULL;
752
753         // temps: 0 order, 1 prod, 2 y, 3 challenge
754         REPLACING(other_4_to_r);
755         USING(schnorr_G); USING(schnorr_s); USING(r);
756
757         in = d0_iobuf_open_read(inbuf, inbuflen);
758         out = d0_iobuf_open_write(outbuf, *outbuflen);
759
760         CHECK(d0_iobuf_read_bignum(in, temp3));
761         CHECK(d0_bignum_cmp(temp3, zero) >= 0);
762         CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
763
764         // Diffie Hellmann
765         CHECK_ASSIGN(ctx->other_4_to_r, d0_iobuf_read_bignum(in, ctx->other_4_to_r));
766         CHECK(d0_bignum_cmp(ctx->other_4_to_r, zero) > 0);
767         CHECK(d0_bignum_cmp(ctx->other_4_to_r, ctx->schnorr_G) < 0);
768
769         // send response for schnorr ID scheme
770         // i.challenge. r + ctx->schnorr_s * temp3
771         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
772         CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
773         CHECK(d0_bignum_mod_add(temp2, temp1, ctx->r, temp0));
774         CHECK(d0_iobuf_write_bignum(out, temp2));
775
776         d0_iobuf_close(in, NULL);
777         return d0_iobuf_close(out, outbuflen);
778
779 fail:
780         d0_iobuf_close(in, NULL);
781         d0_iobuf_close(out, outbuflen);
782         return 0;
783 }
784
785 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)
786 //   1. read y = r + s * challenge mod order
787 //   2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
788 //      (check using H(g^r) which we know)
789 {
790         d0_iobuf_t *in = NULL;
791         static unsigned char convbuf[1024];
792         d0_iobuf_t *conv = NULL;
793         size_t sz;
794
795         // temps: 0 y 1 order
796         USING(challenge); USING(schnorr_G);
797         REPLACING(other_4_to_r);
798
799         in = d0_iobuf_open_read(inbuf, inbuflen);
800
801         CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
802         CHECK(d0_iobuf_read_bignum(in, temp0));
803         CHECK(d0_bignum_cmp(temp0, zero) >= 0);
804         CHECK(d0_bignum_cmp(temp0, temp1) < 0);
805
806         // verify schnorr ID scheme
807         // we need 4^temp0 (g^s)^-challenge
808         CHECK(d0_bignum_neg(temp1, ctx->challenge));
809         CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_4_to_s, temp1, ctx->schnorr_G));
810         CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
811         CHECK_ASSIGN(ctx->other_4_to_r, d0_bignum_mod_mul(ctx->other_4_to_r, temp1, temp2, ctx->schnorr_G));
812         // hash must be equal to msghash
813
814         // hash it, hash it, everybody hash it
815         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
816         CHECK(d0_iobuf_write_bignum(conv, ctx->other_4_to_r));
817         CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
818         CHECK(d0_iobuf_write_bignum(conv, ctx->other_4_to_r));
819         d0_iobuf_close(conv, &sz);
820         conv = NULL;
821         if(memcmp(sha(convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
822         {
823                 // FAIL (not owned by player)
824                 goto fail;
825         }
826
827         if(status)
828                 *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
829
830         if(ctx->msglen <= *msglen)
831                 memcpy(msg, ctx->msg, ctx->msglen);
832         else
833                 memcpy(msg, ctx->msg, *msglen);
834         *msglen = ctx->msglen;
835
836         d0_iobuf_close(in, NULL);
837         return 1;
838
839 fail:
840         d0_iobuf_close(in, NULL);
841         return 0;
842 }
843
844 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
845 {
846         d0_iobuf_t *out = NULL;
847         static unsigned char convbuf[1024];
848         d0_iobuf_t *conv = NULL;
849         size_t sz, n;
850
851         USING(schnorr_4_to_s);
852
853         out = d0_iobuf_open_write(outbuf, *outbuflen);
854         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
855
856         CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_4_to_s));
857         CHECK(d0_iobuf_close(conv, &sz));
858         conv = NULL;
859
860         n = (*outbuflen / 4) * 3;
861         if(n > SHA_DIGESTSIZE)
862                 n = SHA_DIGESTSIZE;
863         CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
864         CHECK(d0_iobuf_conv_base64_out(out));
865
866         return d0_iobuf_close(out, outbuflen);
867
868 fail:
869         if(conv)
870                 d0_iobuf_close(conv, &sz);
871         d0_iobuf_close(out, outbuflen);
872         return 0;
873 }
874
875 BOOL d0_blind_id_sessionkey_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
876 {
877         USING(r); USING(other_4_to_r); USING(schnorr_G);
878
879         // temps: temp0 result
880         CHECK(d0_bignum_mod_pow(temp0, ctx->other_4_to_r, ctx->r, ctx->schnorr_G));
881         return d0_longhash_destructive(temp0, outbuf, outbuflen);
882
883 fail:
884         return 0;
885 }
886
887 d0_blind_id_t *d0_blind_id_new(void)
888 {
889         d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
890         memset(b, 0, sizeof(*b));
891         return b;
892 }
893
894 void d0_blind_id_free(d0_blind_id_t *a)
895 {
896         d0_blind_id_clear(a);
897         d0_free(a);
898 }