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