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