]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_blind_id.c
bump versioninfo
[xonotic/d0_blind_id.git] / d0_blind_id.c
1 /*
2  * FILE:        d0_blind_id.c
3  * AUTHOR:      Rudolf Polzer - divVerent@xonotic.org
4  * 
5  * Copyright (c) 2010, Rudolf Polzer
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holder nor the names of contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Format:commit %H$
33  * $Id$
34  */
35
36 #include "d0_blind_id.h"
37
38 #include <stdio.h>
39 #include <string.h>
40 #include "d0_bignum.h"
41 #include "sha2.h"
42
43 // old "positive" protocol, uses one extra mod_inv in verify stages
44 // #define D0_BLIND_ID_POSITIVE_PROTOCOL
45
46 // our SHA is SHA-256
47 #define SHA_DIGESTSIZE 32
48 const char *sha(const unsigned char *in, size_t len)
49 {
50         static char h[32];
51         d0_blind_id_util_sha256(h, (const char *) in, len);
52         return h;
53 }
54
55 // for zero knowledge, we need multiple instances of schnorr ID scheme... should normally be sequential
56 // parallel schnorr ID is not provably zero knowledge :(
57 //   (evil verifier can know all questions in advance, so sequential is disadvantage for him)
58 // we'll just live with a 1:1048576 chance of cheating, and support reauthenticating
59
60 #define SCHNORR_BITS 20
61 // probability of cheat: 2^(-bits+1)
62
63 #define SCHNORR_HASHSIZE SHA_DIGESTSIZE
64 // cannot be >= SHA_DIGESTSIZE
65 // *8 must be >= SCHNORR_BITS
66 // no need to save bits here
67
68 #define MSGSIZE 640 // ought to be enough for anyone
69
70 struct d0_blind_id_s
71 {
72         // signing (Xonotic pub and priv key)
73         d0_bignum_t *rsa_n, *rsa_e, *rsa_d;
74
75         // public data (Schnorr ID)
76         d0_bignum_t *schnorr_G;
77
78         // private data (player ID private key)
79         d0_bignum_t *schnorr_s;
80
81         // public data (player ID public key, this is what the server gets to know)
82         d0_bignum_t *schnorr_g_to_s;
83         d0_bignum_t *schnorr_H_g_to_s_signature; // 0 when signature is invalid
84         // as hash function H, we get the SHA1 and reinterpret as bignum - yes, it always is < 160 bits
85
86         // temp data
87         d0_bignum_t *rsa_blind_signature_camouflage; // random number blind signature
88
89         d0_bignum_t *r; // random number for schnorr ID
90         d0_bignum_t *t; // for DH key exchange
91         d0_bignum_t *g_to_t; // for DH key exchange
92         d0_bignum_t *other_g_to_t; // for DH key exchange
93         d0_bignum_t *challenge; // challenge
94
95         char msghash[SCHNORR_HASHSIZE]; // init hash
96         char msg[MSGSIZE]; // message
97         size_t msglen; // message length
98 };
99
100 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
101 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
102 #define MPCHECK(x) do { if(!failed) if(!(x)) failed = 1; } while(0)
103 #define MPCHECK_ASSIGN(var, value) do { if(!failed) { d0_bignum_t *val; val = value; if(val) var = val; else failed = 1; } } while(0)
104
105 #define USING(x) if(!(ctx->x)) return 0
106 #define REPLACING(x)
107
108 static d0_bignum_t *zero, *one, *four, *temp0, *temp1, *temp2, *temp3, *temp4;
109
110 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_INITIALIZE(void)
111 {
112         CHECK(d0_bignum_INITIALIZE());
113         CHECK_ASSIGN(zero, d0_bignum_int(zero, 0));
114         CHECK_ASSIGN(one, d0_bignum_int(one, 1));
115         CHECK_ASSIGN(four, d0_bignum_int(four, 4));
116         CHECK_ASSIGN(temp0, d0_bignum_int(temp0, 0));
117         CHECK_ASSIGN(temp1, d0_bignum_int(temp1, 0));
118         CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0));
119         CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0));
120         CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0));
121         return 1;
122 fail:
123         return 0;
124 }
125
126 void d0_blind_id_SHUTDOWN(void)
127 {
128         d0_bignum_free(zero);
129         d0_bignum_free(one);
130         d0_bignum_free(four);
131         d0_bignum_free(temp0);
132         d0_bignum_free(temp1);
133         d0_bignum_free(temp2);
134         d0_bignum_free(temp3);
135         d0_bignum_free(temp4);
136         d0_bignum_SHUTDOWN();
137 }
138
139 // (G-1)/2
140 d0_bignum_t *d0_dl_get_order(d0_bignum_t *o, const d0_bignum_t *G)
141 {
142         CHECK_ASSIGN(o, d0_bignum_sub(o, G, one));
143         CHECK(d0_bignum_shl(o, o, -1)); // order o = (G-1)/2
144         return o;
145 fail:
146         return NULL;
147 }
148 // 2o+1
149 d0_bignum_t *d0_dl_get_from_order(d0_bignum_t *G, const d0_bignum_t *o)
150 {
151         CHECK_ASSIGN(G, d0_bignum_shl(G, o, 1));
152         CHECK(d0_bignum_add(G, G, one));
153         return G;
154 fail:
155         return NULL;
156 }
157
158 D0_BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
159 {
160         // using: temp0
161         if(size < 16)
162                 size = 16;
163         for(;;)
164         {
165                 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
166                 if(d0_bignum_isprime(temp0, 0) == 0)
167                         continue;
168                 CHECK(d0_dl_get_from_order(G, temp0));
169                 if(d0_bignum_isprime(G, 10) == 0)
170                         continue;
171                 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
172                         continue;
173                 break;
174         }
175         return 1;
176 fail:
177         return 0;
178 }
179
180 D0_BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *challenge, d0_bignum_t *d, d0_bignum_t *n)
181 {
182         // uses temp0 to temp4
183         int fail = 0;
184         int gcdfail = 0;
185         int pb = (size + 1)/2;
186         int qb = size - pb;
187         if(pb < 8)
188                 pb = 8;
189         if(qb < 8)
190                 qb = 8;
191         for (;;)
192         {
193                 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
194                 if(d0_bignum_isprime(temp0, 10) == 0)
195                         continue;
196                 CHECK(d0_bignum_sub(temp2, temp0, one));
197                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, challenge));
198                 if(!d0_bignum_cmp(temp4, one))
199                         break;
200                 if(++gcdfail == 3)
201                         return 0;
202                 ++gcdfail;
203         }
204         gcdfail = 0;
205         for (;;)
206         {
207                 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
208                 if(!d0_bignum_cmp(temp1, temp0))
209                 {
210                         if(++fail == 3)
211                                 return 0;
212                 }
213                 fail = 0;
214                 if(d0_bignum_isprime(temp1, 10) == 0)
215                         continue;
216                 CHECK(d0_bignum_sub(temp3, temp1, one));
217                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, challenge));
218                 if(!d0_bignum_cmp(temp4, one))
219                         break;
220                 if(++gcdfail == 3)
221                         return 0;
222                 ++gcdfail;
223         }
224
225         // n = temp0*temp1
226         CHECK(d0_bignum_mul(n, temp0, temp1));
227
228         // d = challenge^-1 mod (temp0-1)(temp1-1)
229         CHECK(d0_bignum_mul(temp0, temp2, temp3));
230         CHECK(d0_bignum_mod_inv(d, challenge, temp0));
231         return 1;
232 fail:
233         return 0;
234 }
235
236 D0_BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass)
237 {
238         // uses temp0 to temp4
239         int fail = 0;
240         int gcdfail = 0;
241         int pb = (size + 1)/2;
242         int qb = size - pb;
243         if(pb < 8)
244                 pb = 8;
245         if(qb < 8)
246                 qb = 8;
247         for (;;)
248         {
249                 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
250                 if(d0_bignum_isprime(temp0, 10) == 0)
251                         continue;
252                 CHECK(d0_bignum_sub(temp2, temp0, one));
253                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
254                 if(!d0_bignum_cmp(temp4, one))
255                         break;
256                 if(++gcdfail == 3)
257                         return 0;
258                 ++gcdfail;
259         }
260         gcdfail = 0;
261         for (;;)
262         {
263                 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
264                 if(!d0_bignum_cmp(temp1, temp0))
265                 {
266                         if(++fail == 3)
267                                 return 0;
268                 }
269                 fail = 0;
270
271                 // n = temp0*temp1
272                 CHECK(d0_bignum_mul(ctx->rsa_n, temp0, temp1));
273                 if(reject(ctx, pass))
274                         continue;
275
276                 if(d0_bignum_isprime(temp1, 10) == 0)
277                         continue;
278                 CHECK(d0_bignum_sub(temp3, temp1, one));
279                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
280                 if(!d0_bignum_cmp(temp4, one))
281                         break;
282                 if(++gcdfail == 3)
283                         return 0;
284                 ++gcdfail;
285         }
286
287         // ctx->rsa_d = ctx->rsa_e^-1 mod (temp0-1)(temp1-1)
288         CHECK(d0_bignum_mul(temp0, temp2, temp3));
289         CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp0));
290         return 1;
291 fail:
292         return 0;
293 }
294
295 D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_destructive(unsigned char *convbuf, size_t sz, unsigned char *outbuf, size_t outbuflen)
296 {
297         size_t n, i;
298
299         n = outbuflen;
300         while(n > SHA_DIGESTSIZE)
301         {
302                 memcpy(outbuf, sha(convbuf, sz), SHA_DIGESTSIZE);
303                 outbuf += SHA_DIGESTSIZE;
304                 n -= SHA_DIGESTSIZE;
305                 for(i = 0; i < sz; ++i)
306                         if(++convbuf[i])
307                                 break; // stop until no carry
308         }
309         memcpy(outbuf, sha(convbuf, sz), n);
310         return 1;
311 }
312
313 D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_bignum(const d0_bignum_t *in, unsigned char *outbuf, size_t outbuflen)
314 {
315         static unsigned char convbuf[1024];
316         size_t sz;
317
318         CHECK(d0_bignum_export_unsigned(in, convbuf, sizeof(convbuf)) >= 0);
319         sz = (d0_bignum_size(in) + 7) / 8;
320         CHECK(d0_longhash_destructive(convbuf, sz, outbuf, outbuflen));
321         return 1;
322
323 fail:
324         return 0;
325 }
326
327 void d0_blind_id_clear(d0_blind_id_t *ctx)
328 {
329         if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
330         if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
331         if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
332         if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
333         if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
334         if(ctx->schnorr_g_to_s) d0_bignum_free(ctx->schnorr_g_to_s);
335         if(ctx->schnorr_H_g_to_s_signature) d0_bignum_free(ctx->schnorr_H_g_to_s_signature);
336         if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
337         if(ctx->r) d0_bignum_free(ctx->r);
338         if(ctx->challenge) d0_bignum_free(ctx->challenge);
339         if(ctx->t) d0_bignum_free(ctx->t);
340         if(ctx->g_to_t) d0_bignum_free(ctx->g_to_t);
341         if(ctx->other_g_to_t) d0_bignum_free(ctx->other_g_to_t);
342         memset(ctx, 0, sizeof(*ctx));
343 }
344
345 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
346 {
347         d0_blind_id_clear(ctx);
348         if(src->rsa_n) CHECK_ASSIGN(ctx->rsa_n, d0_bignum_mov(NULL, src->rsa_n));
349         if(src->rsa_e) CHECK_ASSIGN(ctx->rsa_e, d0_bignum_mov(NULL, src->rsa_e));
350         if(src->rsa_d) CHECK_ASSIGN(ctx->rsa_d, d0_bignum_mov(NULL, src->rsa_d));
351         if(src->schnorr_G) CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_mov(NULL, src->schnorr_G));
352         if(src->schnorr_s) CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_mov(NULL, src->schnorr_s));
353         if(src->schnorr_g_to_s) CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mov(NULL, src->schnorr_g_to_s));
354         if(src->schnorr_H_g_to_s_signature) CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_mov(NULL, src->schnorr_H_g_to_s_signature));
355         if(src->rsa_blind_signature_camouflage) CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage));
356         if(src->r) CHECK_ASSIGN(ctx->r, d0_bignum_mov(NULL, src->r));
357         if(src->challenge) CHECK_ASSIGN(ctx->challenge, d0_bignum_mov(NULL, src->challenge));
358         if(src->t) CHECK_ASSIGN(ctx->t, d0_bignum_mov(NULL, src->t));
359         if(src->g_to_t) CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mov(NULL, src->g_to_t));
360         if(src->other_g_to_t) CHECK_ASSIGN(ctx->other_g_to_t, d0_bignum_mov(NULL, src->other_g_to_t));
361         memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
362         ctx->msglen = src->msglen;
363         memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
364         return 1;
365 fail:
366         d0_blind_id_clear(ctx);
367         return 0;
368 }
369
370 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_key_fastreject(d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass)
371 {
372         REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
373
374         CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
375         CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
376         CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
377         if(reject)
378                 CHECK(d0_rsa_generate_key_fastreject(k+1, reject, ctx, pass)); // must fit G for sure
379         else
380                 CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
381         return 1;
382 fail:
383         return 0;
384 }
385
386 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
387 {
388         return d0_blind_id_generate_private_key_fastreject(ctx, k, NULL, NULL);
389 }
390
391 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
392 {
393         d0_iobuf_t *in = NULL;
394
395         REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
396
397         in = d0_iobuf_open_read(inbuf, inbuflen);
398
399         CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
400         CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
401         CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
402         return d0_iobuf_close(in, NULL);
403
404 fail:
405         d0_iobuf_close(in, NULL);
406         return 0;
407 }
408
409 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
410 {
411         d0_iobuf_t *in = NULL;
412
413         REPLACING(rsa_n); REPLACING(rsa_e);
414
415         in = d0_iobuf_open_read(inbuf, inbuflen);
416         CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
417         CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
418         return d0_iobuf_close(in, NULL);
419
420 fail:
421         d0_iobuf_close(in, NULL);
422         return 0;
423 }
424
425 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
426 {
427         d0_iobuf_t *out = NULL;
428
429         USING(rsa_n); USING(rsa_e); USING(rsa_d);
430
431         out = d0_iobuf_open_write(outbuf, *outbuflen);
432         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
433         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
434         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
435         return d0_iobuf_close(out, outbuflen);
436
437 fail:
438         d0_iobuf_close(out, outbuflen);
439         return 0;
440 }
441
442 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
443 {
444         d0_iobuf_t *out = NULL;
445
446         USING(rsa_n); USING(rsa_e);
447
448         out = d0_iobuf_open_write(outbuf, *outbuflen);
449         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
450         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
451         return d0_iobuf_close(out, outbuflen);
452
453 fail:
454         if(!d0_iobuf_close(out, outbuflen))
455                 return 0;
456         return 0;
457 }
458
459 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
460 {
461         d0_iobuf_t *out = NULL;
462         static unsigned char convbuf[2048];
463         d0_iobuf_t *conv = NULL;
464         size_t sz, n;
465
466         USING(rsa_n); USING(rsa_e);
467
468         out = d0_iobuf_open_write(outbuf, *outbuflen);
469         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
470
471         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
472         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
473         CHECK(d0_iobuf_close(conv, &sz));
474         conv = NULL;
475
476         n = (*outbuflen / 4) * 3;
477         if(n > SHA_DIGESTSIZE)
478                 n = SHA_DIGESTSIZE;
479         CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
480         CHECK(d0_iobuf_conv_base64_out(out));
481
482         return d0_iobuf_close(out, outbuflen);
483
484 fail:
485         if(conv)
486                 d0_iobuf_close(conv, &sz);
487         d0_iobuf_close(out, outbuflen);
488         return 0;
489 }
490
491 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
492 {
493         USING(rsa_n);
494         REPLACING(schnorr_G);
495
496         CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
497         CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
498         return 1;
499 fail:
500         return 0;
501 }
502
503 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
504 {
505         d0_iobuf_t *in = NULL;
506
507         REPLACING(schnorr_G);
508
509         in = d0_iobuf_open_read(inbuf, inbuflen);
510         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
511         return d0_iobuf_close(in, NULL);
512
513 fail:
514         d0_iobuf_close(in, NULL);
515         return 0;
516 }
517
518 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
519 {
520         d0_iobuf_t *out = NULL;
521
522         USING(schnorr_G);
523
524         out = d0_iobuf_open_write(outbuf, *outbuflen);
525         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
526         return d0_iobuf_close(out, outbuflen);
527
528 fail:
529         d0_iobuf_close(out, outbuflen);
530         return 0;
531 }
532
533 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
534 {
535         // temps: temp0 = order
536         USING(schnorr_G);
537         REPLACING(schnorr_s); REPLACING(schnorr_g_to_s);
538
539         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
540         CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
541         CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mod_pow(ctx->schnorr_g_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
542         CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
543         return 1;
544
545 fail:
546         return 0;
547 }
548
549 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
550 {
551         d0_iobuf_t *out = NULL;
552         static unsigned char shabuf[2048];
553         size_t sz;
554
555         // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
556         USING(rsa_n); USING(rsa_e); USING(schnorr_g_to_s);
557         REPLACING(rsa_blind_signature_camouflage);
558
559         out = d0_iobuf_open_write(outbuf, *outbuflen);
560
561         CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
562         CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
563
564         // we will actually sign HA(4^s) to prevent a malleability attack!
565         CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
566         sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
567         if(sz > sizeof(shabuf))
568                 sz = sizeof(shabuf);
569         CHECK(d0_longhash_bignum(temp2, shabuf, sz));
570         CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
571
572         // hash complete
573         CHECK(d0_bignum_mod_mul(temp1, temp2, temp0, ctx->rsa_n));
574         CHECK(d0_iobuf_write_bignum(out, temp1));
575         return d0_iobuf_close(out, outbuflen);
576
577 fail:
578         d0_iobuf_close(out, outbuflen);
579         return 0;
580 }
581
582 D0_WARN_UNUSED_RESULT D0_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)
583 {
584         d0_iobuf_t *in = NULL;
585         d0_iobuf_t *out = NULL;
586
587         // temps: temp0 input, temp1 temp0^d
588         USING(rsa_d); USING(rsa_n);
589
590         in = d0_iobuf_open_read(inbuf, inbuflen);
591         out = d0_iobuf_open_write(outbuf, *outbuflen);
592
593         CHECK(d0_iobuf_read_bignum(in, temp0));
594         CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
595         CHECK(d0_iobuf_write_bignum(out, temp1));
596
597         d0_iobuf_close(in, NULL);
598         return d0_iobuf_close(out, outbuflen);
599
600 fail:
601         d0_iobuf_close(in, NULL);
602         d0_iobuf_close(out, outbuflen);
603         return 0;
604 }
605
606 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
607 {
608         d0_iobuf_t *in = NULL;
609
610         // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
611         USING(rsa_blind_signature_camouflage); USING(rsa_n);
612         REPLACING(schnorr_H_g_to_s_signature);
613
614         in = d0_iobuf_open_read(inbuf, inbuflen);
615
616         CHECK(d0_iobuf_read_bignum(in, temp0));
617         CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
618         CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_H_g_to_s_signature, temp0, temp1, ctx->rsa_n));
619
620         return d0_iobuf_close(in, NULL);
621
622 fail:
623         d0_iobuf_close(in, NULL);
624         return 0;
625 }
626
627 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id_request_camouflage(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
628 {
629         d0_iobuf_t *in = NULL;
630
631         REPLACING(rsa_blind_signature_camouflage);
632
633         in = d0_iobuf_open_read(inbuf, inbuflen);
634
635         CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_iobuf_read_bignum(in, ctx->rsa_blind_signature_camouflage));
636
637         return d0_iobuf_close(in, NULL);
638
639 fail:
640         d0_iobuf_close(in, NULL);
641         return 0;
642 }
643
644 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id_request_camouflage(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
645 {
646         d0_iobuf_t *out = NULL;
647
648         USING(rsa_blind_signature_camouflage);
649
650         out = d0_iobuf_open_write(outbuf, *outbuflen);
651
652         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_blind_signature_camouflage));
653
654         return d0_iobuf_close(out, outbuflen);
655
656 fail:
657         d0_iobuf_close(out, outbuflen);
658         return 0;
659 }
660
661 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
662 {
663         d0_iobuf_t *in = NULL;
664
665         REPLACING(schnorr_s); REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
666
667         in = d0_iobuf_open_read(inbuf, inbuflen);
668
669         CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
670         CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
671         CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
672
673         return d0_iobuf_close(in, NULL);
674
675 fail:
676         d0_iobuf_close(in, NULL);
677         return 0;
678 }
679
680 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
681 {
682         d0_iobuf_t *in = NULL;
683
684         REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
685
686         in = d0_iobuf_open_read(inbuf, inbuflen);
687
688         CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
689         CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
690
691         return d0_iobuf_close(in, NULL);
692
693 fail:
694         d0_iobuf_close(in, NULL);
695         return 0;
696 }
697
698 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
699 {
700         d0_iobuf_t *out = NULL;
701
702         USING(schnorr_s); USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
703
704         out = d0_iobuf_open_write(outbuf, *outbuflen);
705
706         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
707         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
708         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
709
710         return d0_iobuf_close(out, outbuflen);
711
712 fail:
713         d0_iobuf_close(out, outbuflen);
714         return 0;
715 }
716
717 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
718 {
719         d0_iobuf_t *out = NULL;
720
721         USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
722
723         out = d0_iobuf_open_write(outbuf, *outbuflen);
724
725         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
726         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
727
728         return d0_iobuf_close(out, outbuflen);
729
730 fail:
731         d0_iobuf_close(out, outbuflen);
732         return 0;
733 }
734
735 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *msg, size_t msglen, char *outbuf, size_t *outbuflen)
736 // start =
737 //   first run: send 4^s, 4^s signature
738 //   1. get random r, send HASH(4^r)
739 {
740         d0_iobuf_t *out = NULL;
741         static unsigned char convbuf[1024];
742         d0_iobuf_t *conv = NULL;
743         size_t sz = 0;
744         D0_BOOL failed = 0;
745
746         // temps: temp0 order, temp0 4^r
747         if(is_first)
748         {
749                 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
750         }
751         USING(schnorr_G);
752         REPLACING(r); REPLACING(t); REPLACING(g_to_t);
753
754         out = d0_iobuf_open_write(outbuf, *outbuflen);
755
756         if(is_first)
757         {
758                 // send ID
759                 if(send_modulus)
760                         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
761                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
762                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
763         }
764
765         // start schnorr ID scheme
766         // generate random number r; x = g^r; send hash of x, remember r, forget x
767         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
768 #ifdef RNG_XKCD
769         CHECK_ASSIGN(ctx->r, d0_bignum_int(ctx->r, 4)); // decided by fair dice roll
770 #else
771         CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
772 #endif
773
774         // initialize Signed Diffie Hellmann
775         // we already have the group order in temp1
776 #ifdef RNG_XKCD
777         CHECK_ASSIGN(ctx->t, d0_bignum_int(ctx->t, 4)); // decided by fair dice roll
778 #else
779         CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
780 #endif
781         // can we SOMEHOW do this with just one mod_pow?
782
783         CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
784         CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mod_pow(ctx->g_to_t, four, ctx->t, ctx->schnorr_G));
785         CHECK(!failed);
786
787         // hash it, hash it, everybody hash it
788         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
789         CHECK(d0_iobuf_write_bignum(conv, temp0));
790         CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
791         CHECK(d0_iobuf_write_packet(conv, msg, msglen));
792         CHECK(d0_iobuf_write_bignum(conv, temp0));
793         CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
794         d0_iobuf_close(conv, &sz);
795         conv = NULL;
796         CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
797         CHECK(d0_iobuf_write_packet(out, msg, msglen));
798
799         return d0_iobuf_close(out, outbuflen);
800
801 fail:
802         d0_iobuf_close(out, outbuflen);
803         return 0;
804 }
805
806 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen, D0_BOOL *status)
807 //   first run: get 4^s, 4^s signature
808 //   1. check sig
809 //   2. save HASH(4^r)
810 //   3. send challenge challenge of SCHNORR_BITS
811 {
812         d0_iobuf_t *in = NULL;
813         d0_iobuf_t *out = NULL;
814         static unsigned char shabuf[2048];
815         size_t sz;
816
817         // temps: temp0 order, temp0 signature check
818         if(is_first)
819         {
820                 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
821                 if(recv_modulus)
822                         REPLACING(schnorr_G);
823                 else
824                         USING(schnorr_G);
825         }
826         else
827         {
828                 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
829                 USING(schnorr_G);
830         }
831         USING(rsa_e); USING(rsa_n);
832         REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r); REPLACING(t);
833
834         in = d0_iobuf_open_read(inbuf, inbuflen);
835         out = d0_iobuf_open_write(outbuf, *outbuflen);
836
837         if(is_first)
838         {
839                 if(recv_modulus)
840                 {
841                         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
842                         CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
843                         CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
844                 }
845                 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
846                 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
847                 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
848                 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
849                 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
850                 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
851
852                 // check signature of key (t = k^d, so, t^challenge = k)
853                 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
854
855                 // we will actually sign SHA(4^s) to prevent a malleability attack!
856                 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
857                 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
858                 if(sz > sizeof(shabuf))
859                         sz = sizeof(shabuf);
860                 CHECK(d0_longhash_bignum(temp2, shabuf, sz));
861                 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
862
863                 // + 7 / 8 is too large, so let's mod it
864                 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
865
866                 // hash complete
867                 if(d0_bignum_cmp(temp0, temp1))
868                 {
869                         // accept the key anyway, but mark as failed signature! will later return 0 in status
870                         CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
871                 }
872         }
873
874         CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
875         ctx->msglen = MSGSIZE;
876         CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
877
878         // send challenge
879 #ifdef RNG_XKCD
880         CHECK_ASSIGN(ctx->challenge, d0_bignum_int(ctx->challenge, 4)); // decided by fair dice roll
881 #else
882         CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
883 #endif
884         CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
885
886         // Diffie Hellmann send
887         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
888 #ifdef RNG_XKCD
889         CHECK_ASSIGN(ctx->t, d0_bignum_int(ctx->t, 4)); // decided by fair dice roll
890 #else
891         CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
892 #endif
893         CHECK(d0_bignum_mod_pow(temp0, four, ctx->t, ctx->schnorr_G));
894         CHECK(d0_iobuf_write_bignum(out, temp0));
895
896         if(status)
897                 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
898
899         d0_iobuf_close(in, NULL);
900         return d0_iobuf_close(out, outbuflen);
901
902 fail:
903         d0_iobuf_close(in, NULL);
904         d0_iobuf_close(out, outbuflen);
905         return 0;
906 }
907
908 D0_WARN_UNUSED_RESULT D0_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)
909 //   1. read challenge challenge of SCHNORR_BITS
910 //   2. reply with r + s * challenge mod order
911 {
912         d0_iobuf_t *in = NULL;
913         d0_iobuf_t *out = NULL;
914
915         // temps: 0 order, 1 prod, 2 y, 3 challenge
916         REPLACING(other_g_to_t); REPLACING(t);
917         USING(schnorr_G); USING(schnorr_s); USING(r); USING(g_to_t);
918
919         in = d0_iobuf_open_read(inbuf, inbuflen);
920         out = d0_iobuf_open_write(outbuf, *outbuflen);
921
922         CHECK(d0_iobuf_read_bignum(in, temp3));
923         CHECK(d0_bignum_cmp(temp3, zero) >= 0);
924         CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
925
926         // send response for schnorr ID scheme
927         // i.challenge. r + ctx->schnorr_s * temp3
928         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
929         CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
930 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
931         CHECK(d0_bignum_mod_add(temp2, ctx->r, temp1, temp0));
932 #else
933         CHECK(d0_bignum_mod_sub(temp2, ctx->r, temp1, temp0));
934 #endif
935         CHECK(d0_iobuf_write_bignum(out, temp2));
936
937         // Diffie Hellmann recv
938         CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
939         CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
940         CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
941         // Diffie Hellmann send
942         CHECK(d0_iobuf_write_bignum(out, ctx->g_to_t));
943
944         d0_iobuf_close(in, NULL);
945         return d0_iobuf_close(out, outbuflen);
946
947 fail:
948         d0_iobuf_close(in, NULL);
949         d0_iobuf_close(out, outbuflen);
950         return 0;
951 }
952
953 D0_WARN_UNUSED_RESULT D0_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, D0_BOOL *status)
954 //   1. read y = r + s * challenge mod order
955 //   2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
956 //      (check using H(g^r) which we know)
957 {
958         d0_iobuf_t *in = NULL;
959         static unsigned char convbuf[1024];
960         d0_iobuf_t *conv = NULL;
961         size_t sz;
962
963         // temps: 0 y 1 order
964         USING(challenge); USING(schnorr_G);
965         REPLACING(other_g_to_t);
966
967         in = d0_iobuf_open_read(inbuf, inbuflen);
968
969         CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
970         CHECK(d0_iobuf_read_bignum(in, temp0));
971         CHECK(d0_bignum_cmp(temp0, zero) >= 0);
972         CHECK(d0_bignum_cmp(temp0, temp1) < 0);
973
974         // verify schnorr ID scheme
975 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
976         // we need 4^r = 4^temp0 (g^s)^-challenge
977         CHECK(d0_bignum_mod_inv(temp1, ctx->schnorr_g_to_s, ctx->schnorr_G));
978         CHECK(d0_bignum_mod_pow(temp2, temp1, ctx->challenge, ctx->schnorr_G));
979 #else
980         // we need 4^r = 4^temp0 (g^s)^challenge
981         CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_g_to_s, ctx->challenge, ctx->schnorr_G));
982 #endif
983         CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
984         CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G));
985
986         // Diffie Hellmann recv
987         CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
988         CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
989         CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
990
991         // hash it, hash it, everybody hash it
992         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
993         CHECK(d0_iobuf_write_bignum(conv, temp3));
994         CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
995         CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
996         CHECK(d0_iobuf_write_bignum(conv, temp3));
997         CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
998         d0_iobuf_close(conv, &sz);
999         conv = NULL;
1000         if(memcmp(sha(convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
1001         {
1002                 // FAIL (not owned by player)
1003                 goto fail;
1004         }
1005
1006         if(status)
1007                 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
1008
1009         if(ctx->msglen <= *msglen)
1010                 memcpy(msg, ctx->msg, ctx->msglen);
1011         else
1012                 memcpy(msg, ctx->msg, *msglen);
1013         *msglen = ctx->msglen;
1014
1015         d0_iobuf_close(in, NULL);
1016         return 1;
1017
1018 fail:
1019         d0_iobuf_close(in, NULL);
1020         return 0;
1021 }
1022
1023 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_generate_missing_signature(d0_blind_id_t *ctx)
1024 {
1025         size_t sz;
1026         static unsigned char shabuf[2048];
1027
1028         REPLACING(schnorr_H_g_to_s_signature);
1029         USING(schnorr_g_to_s); USING(rsa_d); USING(rsa_n);
1030
1031         // we will actually sign SHA(4^s) to prevent a malleability attack!
1032         CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
1033         sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1034         if(sz > sizeof(shabuf))
1035                 sz = sizeof(shabuf);
1036         CHECK(d0_longhash_bignum(temp2, shabuf, sz));
1037         CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
1038
1039         // + 7 / 8 is too large, so let's mod it
1040         CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1041         CHECK(d0_bignum_mod_pow(ctx->schnorr_H_g_to_s_signature, temp1, ctx->rsa_d, ctx->rsa_n));
1042         return 1;
1043
1044 fail:
1045         return 0;
1046 }
1047
1048 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_sign_internal(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, D0_BOOL with_msg, const char *message, size_t msglen, char *outbuf, size_t *outbuflen)
1049 {
1050         d0_iobuf_t *out = NULL;
1051         static unsigned char convbuf[1024];
1052         static unsigned char shabuf[1024];
1053         d0_iobuf_t *conv = NULL;
1054         size_t sz = 0;
1055
1056         if(is_first)
1057         {
1058                 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
1059         }
1060         USING(schnorr_G);
1061         USING(schnorr_s);
1062         REPLACING(r);
1063
1064         out = d0_iobuf_open_write(outbuf, *outbuflen);
1065
1066         if(is_first)
1067         {
1068                 // send ID
1069                 if(send_modulus)
1070                         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
1071                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
1072                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
1073         }
1074
1075         // start schnorr SIGNATURE scheme
1076         // generate random number r; x = g^r; send hash of H(m||r), remember r, forget x
1077         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
1078         CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
1079         CHECK(d0_bignum_mod_pow(temp1, four, ctx->r, ctx->schnorr_G));
1080
1081         // hash it, hash it, everybody hash it
1082         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1083         CHECK(d0_iobuf_write_packet(conv, message, msglen));
1084         CHECK(d0_iobuf_write_bignum(conv, temp1));
1085         d0_iobuf_close(conv, &sz);
1086         conv = NULL;
1087         CHECK(d0_longhash_destructive(convbuf, sz, shabuf, (d0_bignum_size(temp0) + 7) / 8));
1088         CHECK(d0_bignum_import_unsigned(temp2, shabuf, (d0_bignum_size(temp0) + 7) / 8));
1089         CHECK(d0_iobuf_write_bignum(out, temp2));
1090
1091         // multiply with secret, sub k, modulo order
1092         CHECK(d0_bignum_mod_mul(temp1, temp2, ctx->schnorr_s, temp0));
1093 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1094         CHECK(d0_bignum_mod_add(temp2, ctx->r, temp1, temp0));
1095 #else
1096         CHECK(d0_bignum_mod_sub(temp2, ctx->r, temp1, temp0));
1097 #endif
1098         CHECK(d0_iobuf_write_bignum(out, temp2));
1099
1100         // write the message itself
1101         if(with_msg)
1102                 CHECK(d0_iobuf_write_packet(out, message, msglen));
1103
1104         return d0_iobuf_close(out, outbuflen);
1105
1106 fail:
1107         d0_iobuf_close(out, outbuflen);
1108         return 0;
1109 }
1110 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_sign(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *message, size_t msglen, char *outbuf, size_t *outbuflen)
1111 {
1112         return d0_blind_id_sign_with_private_id_sign_internal(ctx, is_first, send_modulus, 1, message, msglen, outbuf, outbuflen);
1113 }
1114 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_sign_detached(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *message, size_t msglen, char *outbuf, size_t *outbuflen)
1115 {
1116         return d0_blind_id_sign_with_private_id_sign_internal(ctx, is_first, send_modulus, 0, message, msglen, outbuf, outbuflen);
1117 }
1118
1119 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_verify_internal(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, D0_BOOL with_msg, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, D0_BOOL *status)
1120 {
1121         d0_iobuf_t *in = NULL;
1122         d0_iobuf_t *conv = NULL;
1123         static unsigned char convbuf[2048];
1124         static unsigned char shabuf[2048];
1125         size_t sz;
1126
1127         if(is_first)
1128         {
1129                 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
1130                 if(recv_modulus)
1131                         REPLACING(schnorr_G);
1132                 else
1133                         USING(schnorr_G);
1134         }
1135         else
1136         {
1137                 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
1138                 USING(schnorr_G);
1139         }
1140         USING(rsa_e); USING(rsa_n);
1141
1142         in = d0_iobuf_open_read(inbuf, inbuflen);
1143
1144         if(is_first)
1145         {
1146                 if(recv_modulus)
1147                 {
1148                         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
1149                         CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
1150                         CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
1151                 }
1152                 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
1153                 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
1154                 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
1155                 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
1156                 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
1157                 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
1158
1159                 // check signature of key (t = k^d, so, t^challenge = k)
1160                 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
1161
1162                 // we will actually sign SHA(4^s) to prevent a malleability attack!
1163                 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
1164                 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1165                 if(sz > sizeof(shabuf))
1166                         sz = sizeof(shabuf);
1167                 CHECK(d0_longhash_bignum(temp2, shabuf, sz));
1168                 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
1169
1170                 // + 7 / 8 is too large, so let's mod it
1171                 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1172
1173                 // hash complete
1174                 if(d0_bignum_cmp(temp0, temp1))
1175                 {
1176                         // accept the key anyway, but mark as failed signature! will later return 0 in status
1177                         CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
1178                 }
1179         }
1180
1181         CHECK(d0_dl_get_order(temp4, ctx->schnorr_G));
1182         CHECK(d0_iobuf_read_bignum(in, temp0)); // e == H(m || g^r)
1183         CHECK(d0_iobuf_read_bignum(in, temp1)); // x == (r - s*e) mod |G|
1184         if(with_msg)
1185                 CHECK(d0_iobuf_read_packet(in, msg, msglen));
1186
1187         // VERIFY: g^x * (g^s)^-e = g^(x - s*e) = g^r
1188
1189         // verify schnorr ID scheme
1190         // we need g^r = g^x (g^s)^e
1191         CHECK(d0_bignum_mod_pow(temp2, four, temp1, ctx->schnorr_G));
1192 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1193         CHECK(d0_bignum_mod_inv(temp3, ctx->schnorr_g_to_s, ctx->schnorr_G));
1194         CHECK(d0_bignum_mod_pow(temp1, temp3, temp0, ctx->schnorr_G));
1195 #else
1196         CHECK(d0_bignum_mod_pow(temp1, ctx->schnorr_g_to_s, temp0, ctx->schnorr_G));
1197 #endif
1198         CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G)); // temp3 now is g^r
1199
1200         // hash it, hash it, everybody hash it
1201         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1202         CHECK(d0_iobuf_write_packet(conv, msg, *msglen));
1203         CHECK(d0_iobuf_write_bignum(conv, temp3));
1204         d0_iobuf_close(conv, &sz);
1205         conv = NULL;
1206         CHECK(d0_longhash_destructive(convbuf, sz, shabuf, (d0_bignum_size(temp4) + 7) / 8));
1207         CHECK(d0_bignum_import_unsigned(temp1, shabuf, (d0_bignum_size(temp4) + 7) / 8));
1208
1209         // verify signature
1210         CHECK(!d0_bignum_cmp(temp0, temp1));
1211
1212         if(status)
1213                 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
1214
1215         d0_iobuf_close(in, NULL);
1216         return 1;
1217
1218 fail:
1219         d0_iobuf_close(in, NULL);
1220         return 0;
1221 }
1222 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_verify(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, D0_BOOL *status)
1223 {
1224         return d0_blind_id_sign_with_private_id_verify_internal(ctx, is_first, recv_modulus, 1, inbuf, inbuflen, msg, msglen, status);
1225 }
1226 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_verify_detached(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, const char *inbuf, size_t inbuflen, const char *msg, size_t msglen, D0_BOOL *status)
1227 {
1228         return d0_blind_id_sign_with_private_id_verify_internal(ctx, is_first, recv_modulus, 0, inbuf, inbuflen, (char *) msg, &msglen, status);
1229 }
1230
1231 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1232 {
1233         d0_iobuf_t *out = NULL;
1234         static unsigned char convbuf[1024];
1235         d0_iobuf_t *conv = NULL;
1236         size_t sz, n;
1237
1238         USING(rsa_n);
1239         USING(rsa_e);
1240         USING(schnorr_g_to_s);
1241
1242         out = d0_iobuf_open_write(outbuf, *outbuflen);
1243         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1244
1245         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
1246         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
1247         CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_g_to_s));
1248         CHECK(d0_iobuf_close(conv, &sz));
1249         conv = NULL;
1250
1251         n = (*outbuflen / 4) * 3;
1252         if(n > SHA_DIGESTSIZE)
1253                 n = SHA_DIGESTSIZE;
1254         CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
1255         CHECK(d0_iobuf_conv_base64_out(out));
1256
1257         return d0_iobuf_close(out, outbuflen);
1258
1259 fail:
1260         if(conv)
1261                 d0_iobuf_close(conv, &sz);
1262         d0_iobuf_close(out, outbuflen);
1263         return 0;
1264 }
1265
1266 D0_BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1267 {
1268         USING(t); USING(other_g_to_t); USING(schnorr_G);
1269
1270         // temps: temp0 result
1271         CHECK(d0_bignum_mod_pow(temp0, ctx->other_g_to_t, ctx->t, ctx->schnorr_G));
1272         return d0_longhash_bignum(temp0, (unsigned char *) outbuf, *outbuflen);
1273
1274 fail:
1275         return 0;
1276 }
1277
1278 d0_blind_id_t *d0_blind_id_new(void)
1279 {
1280         d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
1281         memset(b, 0, sizeof(*b));
1282         return b;
1283 }
1284
1285 void d0_blind_id_free(d0_blind_id_t *a)
1286 {
1287         d0_blind_id_clear(a);
1288         d0_free(a);
1289 }
1290
1291 void d0_blind_id_util_sha256(char *out, const char *in, size_t n)
1292 {
1293         SHA256_CTX context;
1294         SHA256_Init(&context);
1295         SHA256_Update(&context, (const unsigned char *) in, n);
1296         return SHA256_Final((unsigned char *) out, &context);
1297 }