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