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