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