]> git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_blind_id.c
also handle temps here
[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 __thread 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 CHECKDEBUG
101 #ifdef CHECKDEBUG
102 #define CHECK(x) do { if(!(x)) { fprintf(stderr, "CHECK FAILED (%s:%d): %s\n", __FILE__, __LINE__, #x); goto fail; } } while(0)
103 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) { fprintf(stderr, "CHECK FAILED (%s:%d): %s\n", __FILE__, __LINE__, #value); goto fail; } var = val; } while(0)
104 #else
105 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
106 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
107 #endif
108
109 #define USING(x) if(!(ctx->x)) return 0
110 #define REPLACING(x)
111
112 // safe to use
113 static d0_bignum_t *zero, *one, *four;
114
115 static d0_bignum_t *temp0, *temp1, *temp2, *temp3, *temp4;
116 static void *tempmutex = NULL; // hold this mutex when using temp0 to temp4
117 #define USINGTEMPS() int locked = 0
118 #define LOCKTEMPS() do { if(!locked) d0_lockmutex(tempmutex); locked = 1; } while(0)
119 #define UNLOCKTEMPS() do { if(locked) d0_unlockmutex(tempmutex); locked = 0; } while(0);
120
121 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_INITIALIZE(void)
122 {
123         USINGTEMPS();
124         tempmutex = d0_createmutex();
125         LOCKTEMPS();
126         CHECK(d0_bignum_INITIALIZE());
127         CHECK_ASSIGN(zero, d0_bignum_int(zero, 0));
128         CHECK_ASSIGN(one, d0_bignum_int(one, 1));
129         CHECK_ASSIGN(four, d0_bignum_int(four, 4));
130         CHECK_ASSIGN(temp0, d0_bignum_int(temp0, 0));
131         CHECK_ASSIGN(temp1, d0_bignum_int(temp1, 0));
132         CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0));
133         CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0));
134         CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0));
135         UNLOCKTEMPS();
136         return 1;
137 fail:
138         UNLOCKTEMPS();
139         return 0;
140 }
141
142 void d0_blind_id_SHUTDOWN(void)
143 {
144         USINGTEMPS();
145         LOCKTEMPS();
146         d0_bignum_free(zero);
147         d0_bignum_free(one);
148         d0_bignum_free(four);
149         d0_bignum_free(temp0);
150         d0_bignum_free(temp1);
151         d0_bignum_free(temp2);
152         d0_bignum_free(temp3);
153         d0_bignum_free(temp4);
154         d0_bignum_SHUTDOWN();
155         UNLOCKTEMPS();
156         d0_destroymutex(tempmutex);
157         tempmutex = NULL;
158 }
159
160 // (G-1)/2
161 static d0_bignum_t *d0_dl_get_order(d0_bignum_t *o, const d0_bignum_t *G)
162 {
163         CHECK_ASSIGN(o, d0_bignum_sub(o, G, one));
164         CHECK(d0_bignum_shl(o, o, -1)); // order o = (G-1)/2
165         return o;
166 fail:
167         return NULL;
168 }
169 // 2o+1
170 d0_bignum_t *d0_dl_get_from_order(d0_bignum_t *G, const d0_bignum_t *o)
171 {
172         CHECK_ASSIGN(G, d0_bignum_shl(G, o, 1));
173         CHECK(d0_bignum_add(G, G, one));
174         return G;
175 fail:
176         return NULL;
177 }
178
179 static D0_BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
180 {
181         // using: temp0
182         if(size < 16)
183                 size = 16;
184         for(;;)
185         {
186                 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
187                 if(d0_bignum_isprime(temp0, 0) == 0)
188                         continue;
189                 CHECK(d0_dl_get_from_order(G, temp0));
190                 if(d0_bignum_isprime(G, 10) == 0)
191                         continue;
192                 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
193                         continue;
194                 break;
195         }
196         return 1;
197 fail:
198         return 0;
199 }
200
201 static D0_BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *challenge, d0_bignum_t *d, d0_bignum_t *n)
202 {
203         // uses temp0 to temp4
204         int fail = 0;
205         int gcdfail = 0;
206         int pb = (size + 1)/2;
207         int qb = size - pb;
208         if(pb < 8)
209                 pb = 8;
210         if(qb < 8)
211                 qb = 8;
212         for (;;)
213         {
214                 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
215                 if(d0_bignum_isprime(temp0, 10) == 0)
216                         continue;
217                 CHECK(d0_bignum_sub(temp2, temp0, one));
218                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, challenge));
219                 if(!d0_bignum_cmp(temp4, one))
220                         break;
221                 if(++gcdfail == 3)
222                         goto fail;
223                 ++gcdfail;
224         }
225         gcdfail = 0;
226         for (;;)
227         {
228                 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
229                 if(!d0_bignum_cmp(temp1, temp0))
230                 {
231                         if(++fail == 3)
232                                 goto fail;
233                 }
234                 fail = 0;
235                 if(d0_bignum_isprime(temp1, 10) == 0)
236                         continue;
237                 CHECK(d0_bignum_sub(temp3, temp1, one));
238                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, challenge));
239                 if(!d0_bignum_cmp(temp4, one))
240                         break;
241                 if(++gcdfail == 3)
242                         goto fail;
243                 ++gcdfail;
244         }
245
246         // n = temp0*temp1
247         CHECK(d0_bignum_mul(n, temp0, temp1));
248
249         // d = challenge^-1 mod (temp0-1)(temp1-1)
250         CHECK(d0_bignum_mul(temp0, temp2, temp3));
251         CHECK(d0_bignum_mod_inv(d, challenge, temp0));
252         return 1;
253 fail:
254         return 0;
255 }
256
257 static D0_BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass)
258 {
259         // uses temp0 to temp4
260         int fail = 0;
261         int gcdfail = 0;
262         int pb = (size + 1)/2;
263         int qb = size - pb;
264         if(pb < 8)
265                 pb = 8;
266         if(qb < 8)
267                 qb = 8;
268         for (;;)
269         {
270                 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
271                 if(d0_bignum_isprime(temp0, 10) == 0)
272                         continue;
273                 CHECK(d0_bignum_sub(temp2, temp0, one));
274                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
275                 if(!d0_bignum_cmp(temp4, one))
276                         break;
277                 if(++gcdfail == 3)
278                         return 0;
279                 ++gcdfail;
280         }
281         gcdfail = 0;
282         for (;;)
283         {
284                 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
285                 if(!d0_bignum_cmp(temp1, temp0))
286                 {
287                         if(++fail == 3)
288                                 return 0;
289                 }
290                 fail = 0;
291
292                 // n = temp0*temp1
293                 CHECK(d0_bignum_mul(ctx->rsa_n, temp0, temp1));
294                 if(reject(ctx, pass))
295                         continue;
296
297                 if(d0_bignum_isprime(temp1, 10) == 0)
298                         continue;
299                 CHECK(d0_bignum_sub(temp3, temp1, one));
300                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
301                 if(!d0_bignum_cmp(temp4, one))
302                         break;
303                 if(++gcdfail == 3)
304                         return 0;
305                 ++gcdfail;
306         }
307
308         // ctx->rsa_d = ctx->rsa_e^-1 mod (temp0-1)(temp1-1)
309         CHECK(d0_bignum_mul(temp0, temp2, temp3));
310         CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp0));
311         return 1;
312 fail:
313         return 0;
314 }
315
316 D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_destructive(unsigned char *convbuf, size_t sz, unsigned char *outbuf, size_t outbuflen)
317 {
318         size_t n, i;
319
320         n = outbuflen;
321         while(n > SHA_DIGESTSIZE)
322         {
323                 memcpy(outbuf, sha(convbuf, sz), SHA_DIGESTSIZE);
324                 outbuf += SHA_DIGESTSIZE;
325                 n -= SHA_DIGESTSIZE;
326                 for(i = 0; i < sz; ++i)
327                         if(++convbuf[i])
328                                 break; // stop until no carry
329         }
330         memcpy(outbuf, sha(convbuf, sz), n);
331         return 1;
332 }
333
334 D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_bignum(const d0_bignum_t *in, unsigned char *outbuf, size_t outbuflen)
335 {
336         static __thread unsigned char convbuf[1024];
337         size_t sz;
338
339         CHECK(d0_bignum_export_unsigned(in, convbuf, sizeof(convbuf)) >= 0);
340         sz = (d0_bignum_size(in) + 7) / 8;
341         CHECK(d0_longhash_destructive(convbuf, sz, outbuf, outbuflen));
342         return 1;
343
344 fail:
345         return 0;
346 }
347
348 void d0_blind_id_clear(d0_blind_id_t *ctx)
349 {
350         if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
351         if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
352         if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
353         if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
354         if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
355         if(ctx->schnorr_g_to_s) d0_bignum_free(ctx->schnorr_g_to_s);
356         if(ctx->schnorr_H_g_to_s_signature) d0_bignum_free(ctx->schnorr_H_g_to_s_signature);
357         if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
358         if(ctx->r) d0_bignum_free(ctx->r);
359         if(ctx->challenge) d0_bignum_free(ctx->challenge);
360         if(ctx->t) d0_bignum_free(ctx->t);
361         if(ctx->g_to_t) d0_bignum_free(ctx->g_to_t);
362         if(ctx->other_g_to_t) d0_bignum_free(ctx->other_g_to_t);
363         memset(ctx, 0, sizeof(*ctx));
364 }
365
366 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
367 {
368         d0_blind_id_clear(ctx);
369         if(src->rsa_n) CHECK_ASSIGN(ctx->rsa_n, d0_bignum_mov(NULL, src->rsa_n));
370         if(src->rsa_e) CHECK_ASSIGN(ctx->rsa_e, d0_bignum_mov(NULL, src->rsa_e));
371         if(src->rsa_d) CHECK_ASSIGN(ctx->rsa_d, d0_bignum_mov(NULL, src->rsa_d));
372         if(src->schnorr_G) CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_mov(NULL, src->schnorr_G));
373         if(src->schnorr_s) CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_mov(NULL, src->schnorr_s));
374         if(src->schnorr_g_to_s) CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mov(NULL, src->schnorr_g_to_s));
375         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));
376         if(src->rsa_blind_signature_camouflage) CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage));
377         if(src->r) CHECK_ASSIGN(ctx->r, d0_bignum_mov(NULL, src->r));
378         if(src->challenge) CHECK_ASSIGN(ctx->challenge, d0_bignum_mov(NULL, src->challenge));
379         if(src->t) CHECK_ASSIGN(ctx->t, d0_bignum_mov(NULL, src->t));
380         if(src->g_to_t) CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mov(NULL, src->g_to_t));
381         if(src->other_g_to_t) CHECK_ASSIGN(ctx->other_g_to_t, d0_bignum_mov(NULL, src->other_g_to_t));
382         memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
383         ctx->msglen = src->msglen;
384         memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
385         return 1;
386 fail:
387         d0_blind_id_clear(ctx);
388         return 0;
389 }
390
391 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)
392 {
393         USINGTEMPS();
394         REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
395
396         CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
397         CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
398         CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
399         LOCKTEMPS();
400         if(reject)
401                 CHECK(d0_rsa_generate_key_fastreject(k+1, reject, ctx, pass)); // must fit G for sure
402         else
403                 CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
404         UNLOCKTEMPS();
405         return 1;
406 fail:
407         UNLOCKTEMPS();
408         return 0;
409 }
410
411 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
412 {
413         return d0_blind_id_generate_private_key_fastreject(ctx, k, NULL, NULL);
414 }
415
416 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
417 {
418         d0_iobuf_t *in = NULL;
419
420         REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
421
422         in = d0_iobuf_open_read(inbuf, inbuflen);
423
424         CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
425         CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
426         CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
427         return d0_iobuf_close(in, NULL);
428
429 fail:
430         d0_iobuf_close(in, NULL);
431         return 0;
432 }
433
434 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
435 {
436         d0_iobuf_t *in = NULL;
437
438         REPLACING(rsa_n); REPLACING(rsa_e);
439
440         in = d0_iobuf_open_read(inbuf, inbuflen);
441         CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
442         CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
443         return d0_iobuf_close(in, NULL);
444
445 fail:
446         d0_iobuf_close(in, NULL);
447         return 0;
448 }
449
450 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
451 {
452         d0_iobuf_t *out = NULL;
453
454         USING(rsa_n); USING(rsa_e); USING(rsa_d);
455
456         out = d0_iobuf_open_write(outbuf, *outbuflen);
457         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
458         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
459         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
460         return d0_iobuf_close(out, outbuflen);
461
462 fail:
463         d0_iobuf_close(out, outbuflen);
464         return 0;
465 }
466
467 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
468 {
469         d0_iobuf_t *out = NULL;
470
471         USING(rsa_n); USING(rsa_e);
472
473         out = d0_iobuf_open_write(outbuf, *outbuflen);
474         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
475         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
476         return d0_iobuf_close(out, outbuflen);
477
478 fail:
479         if(!d0_iobuf_close(out, outbuflen))
480                 return 0;
481         return 0;
482 }
483
484 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
485 {
486         d0_iobuf_t *out = NULL;
487         static __thread unsigned char convbuf[2048];
488         d0_iobuf_t *conv = NULL;
489         size_t sz, n;
490
491         USING(rsa_n); USING(rsa_e);
492
493         out = d0_iobuf_open_write(outbuf, *outbuflen);
494         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
495
496         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
497         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
498         CHECK(d0_iobuf_close(conv, &sz));
499         conv = NULL;
500
501         n = (*outbuflen / 4) * 3;
502         if(n > SHA_DIGESTSIZE)
503                 n = SHA_DIGESTSIZE;
504         CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
505         CHECK(d0_iobuf_conv_base64_out(out));
506
507         return d0_iobuf_close(out, outbuflen);
508
509 fail:
510         if(conv)
511                 d0_iobuf_close(conv, &sz);
512         d0_iobuf_close(out, outbuflen);
513         return 0;
514 }
515
516 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
517 {
518         USINGTEMPS();
519         USING(rsa_n);
520         REPLACING(schnorr_G);
521
522         CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
523         LOCKTEMPS();
524         CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
525         UNLOCKTEMPS();
526         return 1;
527 fail:
528         UNLOCKTEMPS();
529         return 0;
530 }
531
532 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
533 {
534         d0_iobuf_t *in = NULL;
535
536         REPLACING(schnorr_G);
537
538         in = d0_iobuf_open_read(inbuf, inbuflen);
539         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
540         return d0_iobuf_close(in, NULL);
541
542 fail:
543         d0_iobuf_close(in, NULL);
544         return 0;
545 }
546
547 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
548 {
549         d0_iobuf_t *out = NULL;
550
551         USING(schnorr_G);
552
553         out = d0_iobuf_open_write(outbuf, *outbuflen);
554         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
555         return d0_iobuf_close(out, outbuflen);
556
557 fail:
558         d0_iobuf_close(out, outbuflen);
559         return 0;
560 }
561
562 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
563 {
564         USINGTEMPS(); // temps: temp0 = order
565         USING(schnorr_G);
566         REPLACING(schnorr_s); REPLACING(schnorr_g_to_s);
567
568         LOCKTEMPS();
569         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
570         CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
571         CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mod_pow(ctx->schnorr_g_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
572         CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
573         UNLOCKTEMPS();
574         return 1;
575
576 fail:
577         UNLOCKTEMPS();
578         return 0;
579 }
580
581 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
582 {
583         d0_iobuf_t *out = NULL;
584         static __thread unsigned char shabuf[2048];
585         size_t sz;
586
587         USINGTEMPS(); // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
588         USING(rsa_n); USING(rsa_e); USING(schnorr_g_to_s);
589         REPLACING(rsa_blind_signature_camouflage);
590
591         out = d0_iobuf_open_write(outbuf, *outbuflen);
592
593         CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
594         CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
595
596         // we will actually sign HA(4^s) to prevent a malleability attack!
597         LOCKTEMPS();
598         CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
599         sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
600         if(sz > sizeof(shabuf))
601                 sz = sizeof(shabuf);
602         CHECK(d0_longhash_bignum(temp2, shabuf, sz));
603         CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
604
605         // hash complete
606         CHECK(d0_bignum_mod_mul(temp1, temp2, temp0, ctx->rsa_n));
607         CHECK(d0_iobuf_write_bignum(out, temp1));
608         UNLOCKTEMPS();
609         return d0_iobuf_close(out, outbuflen);
610
611 fail:
612         UNLOCKTEMPS();
613         d0_iobuf_close(out, outbuflen);
614         return 0;
615 }
616
617 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)
618 {
619         d0_iobuf_t *in = NULL;
620         d0_iobuf_t *out = NULL;
621
622         USINGTEMPS(); // temps: temp0 input, temp1 temp0^d
623         USING(rsa_d); USING(rsa_n);
624
625         in = d0_iobuf_open_read(inbuf, inbuflen);
626         out = d0_iobuf_open_write(outbuf, *outbuflen);
627
628         LOCKTEMPS();
629         CHECK(d0_iobuf_read_bignum(in, temp0));
630         CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
631         CHECK(d0_iobuf_write_bignum(out, temp1));
632
633         UNLOCKTEMPS();
634         d0_iobuf_close(in, NULL);
635         return d0_iobuf_close(out, outbuflen);
636
637 fail:
638         UNLOCKTEMPS();
639         d0_iobuf_close(in, NULL);
640         d0_iobuf_close(out, outbuflen);
641         return 0;
642 }
643
644 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
645 {
646         d0_iobuf_t *in = NULL;
647
648         USINGTEMPS(); // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
649         USING(rsa_blind_signature_camouflage); USING(rsa_n);
650         REPLACING(schnorr_H_g_to_s_signature);
651
652         in = d0_iobuf_open_read(inbuf, inbuflen);
653
654         LOCKTEMPS();
655
656         CHECK(d0_iobuf_read_bignum(in, temp0));
657         CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
658         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));
659
660         UNLOCKTEMPS();
661         return d0_iobuf_close(in, NULL);
662
663 fail:
664         UNLOCKTEMPS();
665         d0_iobuf_close(in, NULL);
666         return 0;
667 }
668
669 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)
670 {
671         d0_iobuf_t *in = NULL;
672
673         REPLACING(rsa_blind_signature_camouflage);
674
675         in = d0_iobuf_open_read(inbuf, inbuflen);
676
677         CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_iobuf_read_bignum(in, ctx->rsa_blind_signature_camouflage));
678
679         return d0_iobuf_close(in, NULL);
680
681 fail:
682         d0_iobuf_close(in, NULL);
683         return 0;
684 }
685
686 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)
687 {
688         d0_iobuf_t *out = NULL;
689
690         USING(rsa_blind_signature_camouflage);
691
692         out = d0_iobuf_open_write(outbuf, *outbuflen);
693
694         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_blind_signature_camouflage));
695
696         return d0_iobuf_close(out, outbuflen);
697
698 fail:
699         d0_iobuf_close(out, outbuflen);
700         return 0;
701 }
702
703 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
704 {
705         d0_iobuf_t *in = NULL;
706
707         REPLACING(schnorr_s); REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
708
709         in = d0_iobuf_open_read(inbuf, inbuflen);
710
711         CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
712         CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
713         CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
714
715         return d0_iobuf_close(in, NULL);
716
717 fail:
718         d0_iobuf_close(in, NULL);
719         return 0;
720 }
721
722 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
723 {
724         d0_iobuf_t *in = NULL;
725
726         REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
727
728         in = d0_iobuf_open_read(inbuf, inbuflen);
729
730         CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
731         CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
732
733         return d0_iobuf_close(in, NULL);
734
735 fail:
736         d0_iobuf_close(in, NULL);
737         return 0;
738 }
739
740 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
741 {
742         d0_iobuf_t *out = NULL;
743
744         USING(schnorr_s); USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
745
746         out = d0_iobuf_open_write(outbuf, *outbuflen);
747
748         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
749         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
750         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
751
752         return d0_iobuf_close(out, outbuflen);
753
754 fail:
755         d0_iobuf_close(out, outbuflen);
756         return 0;
757 }
758
759 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
760 {
761         d0_iobuf_t *out = NULL;
762
763         USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
764
765         out = d0_iobuf_open_write(outbuf, *outbuflen);
766
767         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
768         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
769
770         return d0_iobuf_close(out, outbuflen);
771
772 fail:
773         d0_iobuf_close(out, outbuflen);
774         return 0;
775 }
776
777 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)
778 // start =
779 //   first run: send 4^s, 4^s signature
780 //   1. get random r, send HASH(4^r)
781 {
782         d0_iobuf_t *out = NULL;
783         static __thread unsigned char convbuf[1024];
784         d0_iobuf_t *conv = NULL;
785         size_t sz = 0;
786         D0_BOOL failed = 0;
787
788         USINGTEMPS(); // temps: temp0 order, temp0 4^r
789         if(is_first)
790         {
791                 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
792         }
793         USING(schnorr_G);
794         REPLACING(r); REPLACING(t); REPLACING(g_to_t);
795
796         out = d0_iobuf_open_write(outbuf, *outbuflen);
797
798         if(is_first)
799         {
800                 // send ID
801                 if(send_modulus)
802                         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
803                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
804                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
805         }
806
807         // start schnorr ID scheme
808         // generate random number r; x = g^r; send hash of x, remember r, forget x
809         LOCKTEMPS();
810         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
811 #ifdef RNG_XKCD
812         CHECK_ASSIGN(ctx->r, d0_bignum_int(ctx->r, 4)); // decided by fair dice roll
813 #else
814         CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
815 #endif
816
817         // initialize Signed Diffie Hellmann
818         // we already have the group order in temp1
819 #ifdef RNG_XKCD
820         CHECK_ASSIGN(ctx->t, d0_bignum_int(ctx->t, 4)); // decided by fair dice roll
821 #else
822         CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
823 #endif
824         // can we SOMEHOW do this with just one mod_pow?
825
826         CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
827         CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mod_pow(ctx->g_to_t, four, ctx->t, ctx->schnorr_G));
828         CHECK(!failed);
829
830         // hash it, hash it, everybody hash it
831         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
832         CHECK(d0_iobuf_write_bignum(conv, temp0));
833         CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
834         CHECK(d0_iobuf_write_packet(conv, msg, msglen));
835         CHECK(d0_iobuf_write_bignum(conv, temp0));
836         UNLOCKTEMPS();
837         CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
838         d0_iobuf_close(conv, &sz);
839         conv = NULL;
840         CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
841         CHECK(d0_iobuf_write_packet(out, msg, msglen));
842
843         return d0_iobuf_close(out, outbuflen);
844
845 fail:
846         UNLOCKTEMPS();
847         d0_iobuf_close(out, outbuflen);
848         return 0;
849 }
850
851 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)
852 //   first run: get 4^s, 4^s signature
853 //   1. check sig
854 //   2. save HASH(4^r)
855 //   3. send challenge challenge of SCHNORR_BITS
856 {
857         d0_iobuf_t *in = NULL;
858         d0_iobuf_t *out = NULL;
859         static __thread unsigned char shabuf[2048];
860         size_t sz;
861
862         USINGTEMPS(); // temps: temp0 order, temp0 signature check
863         if(is_first)
864         {
865                 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
866                 if(recv_modulus)
867                         REPLACING(schnorr_G);
868                 else
869                         USING(schnorr_G);
870         }
871         else
872         {
873                 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
874                 USING(schnorr_G);
875         }
876         USING(rsa_e); USING(rsa_n);
877         REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r); REPLACING(t);
878
879         in = d0_iobuf_open_read(inbuf, inbuflen);
880         out = d0_iobuf_open_write(outbuf, *outbuflen);
881
882         if(is_first)
883         {
884                 if(recv_modulus)
885                 {
886                         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
887                         CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
888                         CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
889                 }
890                 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
891                 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
892                 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
893                 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
894                 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
895                 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
896
897                 // check signature of key (t = k^d, so, t^challenge = k)
898                 LOCKTEMPS();
899                 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
900
901                 // we will actually sign SHA(4^s) to prevent a malleability attack!
902                 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
903                 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
904                 if(sz > sizeof(shabuf))
905                         sz = sizeof(shabuf);
906                 CHECK(d0_longhash_bignum(temp2, shabuf, sz));
907                 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
908
909                 // + 7 / 8 is too large, so let's mod it
910                 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
911
912                 // hash complete
913                 if(d0_bignum_cmp(temp0, temp1))
914                 {
915                         // accept the key anyway, but mark as failed signature! will later return 0 in status
916                         CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
917                 }
918         }
919
920         CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
921         ctx->msglen = MSGSIZE;
922         CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
923
924         // send challenge
925 #ifdef RNG_XKCD
926         CHECK_ASSIGN(ctx->challenge, d0_bignum_int(ctx->challenge, 4)); // decided by fair dice roll
927 #else
928         CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
929 #endif
930         CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
931
932         // Diffie Hellmann send
933         LOCKTEMPS();
934         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
935 #ifdef RNG_XKCD
936         CHECK_ASSIGN(ctx->t, d0_bignum_int(ctx->t, 4)); // decided by fair dice roll
937 #else
938         CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
939 #endif
940         CHECK(d0_bignum_mod_pow(temp0, four, ctx->t, ctx->schnorr_G));
941         CHECK(d0_iobuf_write_bignum(out, temp0));
942         UNLOCKTEMPS();
943
944         if(status)
945                 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
946
947         d0_iobuf_close(in, NULL);
948         return d0_iobuf_close(out, outbuflen);
949
950 fail:
951         UNLOCKTEMPS();
952         d0_iobuf_close(in, NULL);
953         d0_iobuf_close(out, outbuflen);
954         return 0;
955 }
956
957 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)
958 //   1. read challenge challenge of SCHNORR_BITS
959 //   2. reply with r + s * challenge mod order
960 {
961         d0_iobuf_t *in = NULL;
962         d0_iobuf_t *out = NULL;
963
964         USINGTEMPS(); // temps: 0 order, 1 prod, 2 y, 3 challenge
965         REPLACING(other_g_to_t); REPLACING(t);
966         USING(schnorr_G); USING(schnorr_s); USING(r); USING(g_to_t);
967
968         in = d0_iobuf_open_read(inbuf, inbuflen);
969         out = d0_iobuf_open_write(outbuf, *outbuflen);
970
971         LOCKTEMPS();
972         CHECK(d0_iobuf_read_bignum(in, temp3));
973         CHECK(d0_bignum_cmp(temp3, zero) >= 0);
974         CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
975
976         // send response for schnorr ID scheme
977         // i.challenge. r + ctx->schnorr_s * temp3
978         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
979         CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
980 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
981         CHECK(d0_bignum_mod_add(temp2, ctx->r, temp1, temp0));
982 #else
983         CHECK(d0_bignum_mod_sub(temp2, ctx->r, temp1, temp0));
984 #endif
985         CHECK(d0_iobuf_write_bignum(out, temp2));
986         UNLOCKTEMPS();
987
988         // Diffie Hellmann recv
989         CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
990         CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
991         CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
992         // Diffie Hellmann send
993         CHECK(d0_iobuf_write_bignum(out, ctx->g_to_t));
994
995         d0_iobuf_close(in, NULL);
996         return d0_iobuf_close(out, outbuflen);
997
998 fail:
999         UNLOCKTEMPS();
1000         d0_iobuf_close(in, NULL);
1001         d0_iobuf_close(out, outbuflen);
1002         return 0;
1003 }
1004
1005 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)
1006 //   1. read y = r + s * challenge mod order
1007 //   2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
1008 //      (check using H(g^r) which we know)
1009 {
1010         d0_iobuf_t *in = NULL;
1011         static __thread unsigned char convbuf[1024];
1012         d0_iobuf_t *conv = NULL;
1013         size_t sz;
1014
1015         USINGTEMPS(); // temps: 0 y 1 order
1016         USING(challenge); USING(schnorr_G);
1017         REPLACING(other_g_to_t);
1018
1019         in = d0_iobuf_open_read(inbuf, inbuflen);
1020
1021         LOCKTEMPS();
1022
1023         CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
1024         CHECK(d0_iobuf_read_bignum(in, temp0));
1025         CHECK(d0_bignum_cmp(temp0, zero) >= 0);
1026         CHECK(d0_bignum_cmp(temp0, temp1) < 0);
1027
1028         // verify schnorr ID scheme
1029 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1030         // we need 4^r = 4^temp0 (g^s)^-challenge
1031         CHECK(d0_bignum_mod_inv(temp1, ctx->schnorr_g_to_s, ctx->schnorr_G));
1032         CHECK(d0_bignum_mod_pow(temp2, temp1, ctx->challenge, ctx->schnorr_G));
1033 #else
1034         // we need 4^r = 4^temp0 (g^s)^challenge
1035         CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_g_to_s, ctx->challenge, ctx->schnorr_G));
1036 #endif
1037         CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
1038         CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G));
1039
1040         // Diffie Hellmann recv
1041         CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
1042         CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
1043         CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
1044
1045         // hash it, hash it, everybody hash it
1046         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1047         CHECK(d0_iobuf_write_bignum(conv, temp3));
1048         CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
1049         CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
1050         CHECK(d0_iobuf_write_bignum(conv, temp3));
1051         UNLOCKTEMPS();
1052         CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
1053         d0_iobuf_close(conv, &sz);
1054         conv = NULL;
1055         if(memcmp(sha(convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
1056         {
1057                 // FAIL (not owned by player)
1058                 goto fail;
1059         }
1060
1061         if(status)
1062                 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
1063
1064         if(ctx->msglen <= *msglen)
1065                 memcpy(msg, ctx->msg, ctx->msglen);
1066         else
1067                 memcpy(msg, ctx->msg, *msglen);
1068         *msglen = ctx->msglen;
1069
1070         d0_iobuf_close(in, NULL);
1071         return 1;
1072
1073 fail:
1074         UNLOCKTEMPS();
1075         d0_iobuf_close(in, NULL);
1076         return 0;
1077 }
1078
1079 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_generate_missing_signature(d0_blind_id_t *ctx)
1080 {
1081         size_t sz;
1082         static __thread unsigned char shabuf[2048];
1083
1084         USINGTEMPS(); // temps: 2 hash
1085         REPLACING(schnorr_H_g_to_s_signature);
1086         USING(schnorr_g_to_s); USING(rsa_d); USING(rsa_n);
1087
1088         LOCKTEMPS();
1089
1090         // we will actually sign SHA(4^s) to prevent a malleability attack!
1091         sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1092         if(sz > sizeof(shabuf))
1093                 sz = sizeof(shabuf);
1094         CHECK(d0_longhash_bignum(ctx->schnorr_g_to_s, shabuf, sz));
1095         LOCKTEMPS();
1096         CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
1097
1098         // + 7 / 8 is too large, so let's mod it
1099         CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1100         CHECK(d0_bignum_mod_pow(ctx->schnorr_H_g_to_s_signature, temp1, ctx->rsa_d, ctx->rsa_n));
1101
1102         UNLOCKTEMPS();
1103         return 1;
1104
1105 fail:
1106         UNLOCKTEMPS();
1107         return 0;
1108 }
1109
1110 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)
1111 {
1112         d0_iobuf_t *out = NULL;
1113         unsigned char *convbuf = NULL;
1114         static __thread unsigned char shabuf[2048];
1115         d0_iobuf_t *conv = NULL;
1116         size_t sz = 0;
1117
1118         USINGTEMPS(); // temps: 0 order 1 4^r 2 hash
1119         if(is_first)
1120         {
1121                 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
1122         }
1123         USING(schnorr_G);
1124         USING(schnorr_s);
1125         REPLACING(r);
1126
1127         out = d0_iobuf_open_write(outbuf, *outbuflen);
1128
1129         if(is_first)
1130         {
1131                 // send ID
1132                 if(send_modulus)
1133                         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
1134                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
1135                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
1136         }
1137
1138         // start schnorr SIGNATURE scheme
1139         // generate random number r; x = g^r; send hash of H(m||r), remember r, forget x
1140         LOCKTEMPS();
1141         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
1142         CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
1143         CHECK(d0_bignum_mod_pow(temp1, four, ctx->r, ctx->schnorr_G));
1144
1145         // hash it, hash it, everybody hash it
1146         conv = d0_iobuf_open_write_p((void **) &convbuf, 0);
1147         CHECK(d0_iobuf_write_packet(conv, message, msglen));
1148         CHECK(d0_iobuf_write_bignum(conv, temp1));
1149         d0_iobuf_close(conv, &sz);
1150         conv = NULL;
1151         CHECK(d0_longhash_destructive(convbuf, sz, shabuf, (d0_bignum_size(temp0) + 7) / 8));
1152         d0_free(convbuf);
1153         convbuf = NULL;
1154         CHECK(d0_bignum_import_unsigned(temp2, shabuf, (d0_bignum_size(temp0) + 7) / 8));
1155         CHECK(d0_iobuf_write_bignum(out, temp2));
1156
1157         // multiply with secret, sub k, modulo order
1158         CHECK(d0_bignum_mod_mul(temp1, temp2, ctx->schnorr_s, temp0));
1159 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1160         CHECK(d0_bignum_mod_add(temp2, ctx->r, temp1, temp0));
1161 #else
1162         CHECK(d0_bignum_mod_sub(temp2, ctx->r, temp1, temp0));
1163 #endif
1164         CHECK(d0_iobuf_write_bignum(out, temp2));
1165         UNLOCKTEMPS();
1166
1167         // write the message itself
1168         if(with_msg)
1169                 CHECK(d0_iobuf_write_packet(out, message, msglen));
1170
1171         return d0_iobuf_close(out, outbuflen);
1172
1173 fail:
1174         UNLOCKTEMPS();
1175         d0_iobuf_close(out, outbuflen);
1176         return 0;
1177 }
1178 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)
1179 {
1180         return d0_blind_id_sign_with_private_id_sign_internal(ctx, is_first, send_modulus, 1, message, msglen, outbuf, outbuflen);
1181 }
1182 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)
1183 {
1184         return d0_blind_id_sign_with_private_id_sign_internal(ctx, is_first, send_modulus, 0, message, msglen, outbuf, outbuflen);
1185 }
1186
1187 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)
1188 {
1189         d0_iobuf_t *in = NULL;
1190         d0_iobuf_t *conv = NULL;
1191         unsigned char *convbuf = NULL;
1192         static __thread unsigned char shabuf[2048];
1193         size_t sz;
1194
1195         USINGTEMPS(); // temps: 0 sig^e 2 g^s 3 g^-s 4 order
1196         if(is_first)
1197         {
1198                 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
1199                 if(recv_modulus)
1200                         REPLACING(schnorr_G);
1201                 else
1202                         USING(schnorr_G);
1203         }
1204         else
1205         {
1206                 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
1207                 USING(schnorr_G);
1208         }
1209         USING(rsa_e); USING(rsa_n);
1210
1211         in = d0_iobuf_open_read(inbuf, inbuflen);
1212
1213         if(is_first)
1214         {
1215                 if(recv_modulus)
1216                 {
1217                         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
1218                         CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
1219                         CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
1220                 }
1221                 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
1222                 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
1223                 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
1224                 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
1225                 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
1226                 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
1227
1228                 // check signature of key (t = k^d, so, t^challenge = k)
1229                 LOCKTEMPS();
1230                 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
1231
1232                 // we will actually sign SHA(4^s) to prevent a malleability attack!
1233                 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1234                 if(sz > sizeof(shabuf))
1235                         sz = sizeof(shabuf);
1236                 CHECK(d0_longhash_bignum(ctx->schnorr_g_to_s, shabuf, sz));
1237                 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
1238
1239                 // + 7 / 8 is too large, so let's mod it
1240                 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1241
1242                 // hash complete
1243                 if(d0_bignum_cmp(temp0, temp1))
1244                 {
1245                         // accept the key anyway, but mark as failed signature! will later return 0 in status
1246                         CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
1247                 }
1248         }
1249
1250         CHECK(d0_dl_get_order(temp4, ctx->schnorr_G));
1251         CHECK(d0_iobuf_read_bignum(in, temp0)); // e == H(m || g^r)
1252         CHECK(d0_iobuf_read_bignum(in, temp1)); // x == (r - s*e) mod |G|
1253         if(with_msg)
1254                 CHECK(d0_iobuf_read_packet(in, msg, msglen));
1255
1256         // VERIFY: g^x * (g^s)^-e = g^(x - s*e) = g^r
1257
1258         // verify schnorr ID scheme
1259         // we need g^r = g^x (g^s)^e
1260         CHECK(d0_bignum_mod_pow(temp2, four, temp1, ctx->schnorr_G));
1261 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1262         CHECK(d0_bignum_mod_inv(temp3, ctx->schnorr_g_to_s, ctx->schnorr_G));
1263         CHECK(d0_bignum_mod_pow(temp1, temp3, temp0, ctx->schnorr_G));
1264 #else
1265         CHECK(d0_bignum_mod_pow(temp1, ctx->schnorr_g_to_s, temp0, ctx->schnorr_G));
1266 #endif
1267         CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G)); // temp3 now is g^r
1268
1269         // hash it, hash it, everybody hash it
1270         conv = d0_iobuf_open_write_p((void **) &convbuf, 0);
1271         CHECK(d0_iobuf_write_packet(conv, msg, *msglen));
1272         CHECK(d0_iobuf_write_bignum(conv, temp3));
1273         d0_iobuf_close(conv, &sz);
1274         conv = NULL;
1275         CHECK(d0_longhash_destructive(convbuf, sz, shabuf, (d0_bignum_size(temp4) + 7) / 8));
1276         d0_free(convbuf);
1277         convbuf = NULL;
1278         CHECK(d0_bignum_import_unsigned(temp1, shabuf, (d0_bignum_size(temp4) + 7) / 8));
1279
1280         // verify signature
1281         CHECK(!d0_bignum_cmp(temp0, temp1));
1282         UNLOCKTEMPS();
1283
1284         if(status)
1285                 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
1286
1287         d0_iobuf_close(in, NULL);
1288         return 1;
1289
1290 fail:
1291         UNLOCKTEMPS();
1292         d0_iobuf_close(in, NULL);
1293         return 0;
1294 }
1295 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)
1296 {
1297         return d0_blind_id_sign_with_private_id_verify_internal(ctx, is_first, recv_modulus, 1, inbuf, inbuflen, msg, msglen, status);
1298 }
1299 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)
1300 {
1301         return d0_blind_id_sign_with_private_id_verify_internal(ctx, is_first, recv_modulus, 0, inbuf, inbuflen, (char *) msg, &msglen, status);
1302 }
1303
1304 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1305 {
1306         d0_iobuf_t *out = NULL;
1307         static __thread unsigned char convbuf[1024];
1308         d0_iobuf_t *conv = NULL;
1309         size_t sz, n;
1310
1311         USING(rsa_n);
1312         USING(rsa_e);
1313         USING(schnorr_g_to_s);
1314
1315         out = d0_iobuf_open_write(outbuf, *outbuflen);
1316         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1317
1318         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
1319         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
1320         CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_g_to_s));
1321         CHECK(d0_iobuf_close(conv, &sz));
1322         conv = NULL;
1323
1324         n = (*outbuflen / 4) * 3;
1325         if(n > SHA_DIGESTSIZE)
1326                 n = SHA_DIGESTSIZE;
1327         CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
1328         CHECK(d0_iobuf_conv_base64_out(out));
1329
1330         return d0_iobuf_close(out, outbuflen);
1331
1332 fail:
1333         if(conv)
1334                 d0_iobuf_close(conv, &sz);
1335         d0_iobuf_close(out, outbuflen);
1336         return 0;
1337 }
1338
1339 D0_BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1340 {
1341         D0_BOOL ret;
1342
1343         USINGTEMPS(); // temps: temp0 result
1344         USING(t); USING(other_g_to_t); USING(schnorr_G);
1345
1346         LOCKTEMPS();
1347         CHECK(d0_bignum_mod_pow(temp0, ctx->other_g_to_t, ctx->t, ctx->schnorr_G));
1348         ret = d0_longhash_bignum(temp0, (unsigned char *) outbuf, *outbuflen);
1349         UNLOCKTEMPS();
1350         return ret;
1351
1352 fail:
1353         UNLOCKTEMPS();
1354         return 0;
1355 }
1356
1357 d0_blind_id_t *d0_blind_id_new(void)
1358 {
1359         d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
1360         memset(b, 0, sizeof(*b));
1361         return b;
1362 }
1363
1364 void d0_blind_id_free(d0_blind_id_t *a)
1365 {
1366         d0_blind_id_clear(a);
1367         d0_free(a);
1368 }
1369
1370 void d0_blind_id_util_sha256(char *out, const char *in, size_t n)
1371 {
1372         SHA256_CTX context;
1373         SHA256_Init(&context);
1374         SHA256_Update(&context, (const unsigned char *) in, n);
1375         return SHA256_Final((unsigned char *) out, &context);
1376 }