3 * AUTHOR: Rudolf Polzer - divVerent@xonotic.org
5 * Copyright (c) 2010, Rudolf Polzer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 #include "d0_blind_id.h"
40 #include "d0_bignum.h"
44 #define SHA_DIGESTSIZE 32
45 const char *sha(const char *in, size_t len)
48 d0_blind_id_util_sha256(h, in, len);
52 // for zero knowledge, we need multiple instances of schnorr ID scheme... should normally be sequential
53 // parallel schnorr ID is not provably zero knowledge :(
54 // (evil verifier can know all questions in advance, so sequential is disadvantage for him)
55 // we'll just live with a 1:1048576 chance of cheating, and support reauthenticating
57 #define SCHNORR_BITS 20
58 // probability of cheat: 2^(-bits+1)
60 #define SCHNORR_HASHSIZE SHA_DIGESTSIZE
61 // cannot be >= SHA_DIGESTSIZE
62 // *8 must be >= SCHNORR_BITS
63 // no need to save bits here
65 #define MSGSIZE 640 // ought to be enough for anyone
69 // signing (Xonotic pub and priv key)
70 d0_bignum_t *rsa_n, *rsa_e, *rsa_d;
72 // public data (Schnorr ID)
73 d0_bignum_t *schnorr_G;
75 // private data (player ID private key)
76 d0_bignum_t *schnorr_s;
78 // public data (player ID public key, this is what the server gets to know)
79 d0_bignum_t *schnorr_g_to_s;
80 d0_bignum_t *schnorr_H_g_to_s_signature; // 0 when signature is invalid
81 // as hash function H, we get the SHA1 and reinterpret as bignum - yes, it always is < 160 bits
84 d0_bignum_t *rsa_blind_signature_camouflage; // random number blind signature
86 d0_bignum_t *r; // random number for schnorr ID
87 d0_bignum_t *t; // for DH key exchange
88 d0_bignum_t *g_to_t; // for DH key exchange
89 d0_bignum_t *other_g_to_t; // for DH key exchange
90 d0_bignum_t *challenge; // challenge
92 char msghash[SCHNORR_HASHSIZE]; // init hash
93 char msg[MSGSIZE]; // message
94 size_t msglen; // message length
97 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
98 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
99 #define MPCHECK(x) do { if(!failed) if(!(x)) failed = 1; } while(0)
100 #define MPCHECK_ASSIGN(var, value) do { if(!failed) { d0_bignum_t *val; val = value; if(val) var = val; else failed = 1; } } while(0)
102 #define USING(x) if(!(ctx->x)) return 0
105 static d0_bignum_t *zero, *one, *four, *temp0, *temp1, *temp2, *temp3, *temp4;
107 WARN_UNUSED_RESULT BOOL d0_blind_id_INITIALIZE(void)
109 CHECK(d0_bignum_INITIALIZE());
110 CHECK_ASSIGN(zero, d0_bignum_int(zero, 0));
111 CHECK_ASSIGN(one, d0_bignum_int(one, 1));
112 CHECK_ASSIGN(four, d0_bignum_int(four, 4));
113 CHECK_ASSIGN(temp0, d0_bignum_int(temp0, 0));
114 CHECK_ASSIGN(temp1, d0_bignum_int(temp1, 0));
115 CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0));
116 CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0));
117 CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0));
123 void d0_blind_id_SHUTDOWN(void)
125 d0_bignum_free(zero);
127 d0_bignum_free(four);
128 d0_bignum_free(temp0);
129 d0_bignum_free(temp1);
130 d0_bignum_free(temp2);
131 d0_bignum_free(temp3);
132 d0_bignum_free(temp4);
133 d0_bignum_SHUTDOWN();
137 d0_bignum_t *d0_dl_get_order(d0_bignum_t *o, const d0_bignum_t *G)
139 CHECK_ASSIGN(o, d0_bignum_sub(o, G, one));
140 CHECK(d0_bignum_shl(o, o, -1)); // order o = (G-1)/2
146 d0_bignum_t *d0_dl_get_from_order(d0_bignum_t *G, const d0_bignum_t *o)
148 CHECK_ASSIGN(G, d0_bignum_shl(G, o, 1));
149 CHECK(d0_bignum_add(G, G, one));
155 BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
162 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
163 if(d0_bignum_isprime(temp0, 0) == 0)
165 CHECK(d0_dl_get_from_order(G, temp0));
166 if(d0_bignum_isprime(G, 10) == 0)
168 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
177 BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *challenge, d0_bignum_t *d, d0_bignum_t *n)
179 // uses temp0 to temp4
182 int pb = (size + 1)/2;
190 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
191 if(d0_bignum_isprime(temp0, 10) == 0)
193 CHECK(d0_bignum_sub(temp2, temp0, one));
194 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, challenge));
195 if(!d0_bignum_cmp(temp4, one))
204 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
205 if(!d0_bignum_cmp(temp1, temp0))
211 if(d0_bignum_isprime(temp1, 10) == 0)
213 CHECK(d0_bignum_sub(temp3, temp1, one));
214 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, challenge));
215 if(!d0_bignum_cmp(temp4, one))
223 CHECK(d0_bignum_mul(n, temp0, temp1));
225 // d = challenge^-1 mod (temp0-1)(temp1-1)
226 CHECK(d0_bignum_mul(temp0, temp2, temp3));
227 CHECK(d0_bignum_mod_inv(d, challenge, temp0));
233 BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass)
235 // uses temp0 to temp4
238 int pb = (size + 1)/2;
246 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
247 if(d0_bignum_isprime(temp0, 10) == 0)
249 CHECK(d0_bignum_sub(temp2, temp0, one));
250 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
251 if(!d0_bignum_cmp(temp4, one))
260 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
261 if(!d0_bignum_cmp(temp1, temp0))
269 CHECK(d0_bignum_mul(ctx->rsa_n, temp0, temp1));
270 if(reject(ctx, pass))
273 if(d0_bignum_isprime(temp1, 10) == 0)
275 CHECK(d0_bignum_sub(temp3, temp1, one));
276 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
277 if(!d0_bignum_cmp(temp4, one))
284 // ctx->rsa_d = ctx->rsa_e^-1 mod (temp0-1)(temp1-1)
285 CHECK(d0_bignum_mul(temp0, temp2, temp3));
286 CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp0));
292 WARN_UNUSED_RESULT BOOL d0_longhash_destructive(d0_bignum_t *clobberme, char *outbuf, size_t outbuflen)
294 d0_iobuf_t *out = NULL;
295 static unsigned char convbuf[1024];
299 while(n > SHA_DIGESTSIZE)
301 sz = (d0_bignum_size(clobberme) + 7) / 8;
302 CHECK(d0_bignum_export_unsigned(clobberme, convbuf, sizeof(convbuf)) >= 0);
303 memcpy(outbuf, sha(convbuf, sz), SHA_DIGESTSIZE);
304 outbuf += SHA_DIGESTSIZE;
306 CHECK(d0_bignum_add(clobberme, clobberme, one));
308 sz = (d0_bignum_size(clobberme) + 7) / 8;
309 CHECK(d0_bignum_export_unsigned(clobberme, convbuf, sizeof(convbuf)) >= 0);
310 memcpy(outbuf, sha(convbuf, sz), n);
317 void d0_blind_id_clear(d0_blind_id_t *ctx)
319 if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
320 if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
321 if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
322 if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
323 if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
324 if(ctx->schnorr_g_to_s) d0_bignum_free(ctx->schnorr_g_to_s);
325 if(ctx->schnorr_H_g_to_s_signature) d0_bignum_free(ctx->schnorr_H_g_to_s_signature);
326 if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
327 if(ctx->r) d0_bignum_free(ctx->r);
328 if(ctx->challenge) d0_bignum_free(ctx->challenge);
329 if(ctx->t) d0_bignum_free(ctx->t);
330 if(ctx->g_to_t) d0_bignum_free(ctx->g_to_t);
331 if(ctx->other_g_to_t) d0_bignum_free(ctx->other_g_to_t);
332 memset(ctx, 0, sizeof(*ctx));
335 WARN_UNUSED_RESULT BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
337 d0_blind_id_clear(ctx);
338 if(src->rsa_n) CHECK_ASSIGN(ctx->rsa_n, d0_bignum_mov(NULL, src->rsa_n));
339 if(src->rsa_e) CHECK_ASSIGN(ctx->rsa_e, d0_bignum_mov(NULL, src->rsa_e));
340 if(src->rsa_d) CHECK_ASSIGN(ctx->rsa_d, d0_bignum_mov(NULL, src->rsa_d));
341 if(src->schnorr_G) CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_mov(NULL, src->schnorr_G));
342 if(src->schnorr_s) CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_mov(NULL, src->schnorr_s));
343 if(src->schnorr_g_to_s) CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mov(NULL, src->schnorr_g_to_s));
344 if(src->schnorr_H_g_to_s_signature) CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_mov(NULL, src->schnorr_H_g_to_s_signature));
345 if(src->rsa_blind_signature_camouflage) CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage));
346 if(src->r) CHECK_ASSIGN(ctx->r, d0_bignum_mov(NULL, src->r));
347 if(src->challenge) CHECK_ASSIGN(ctx->challenge, d0_bignum_mov(NULL, src->challenge));
348 if(src->t) CHECK_ASSIGN(ctx->t, d0_bignum_mov(NULL, src->t));
349 if(src->g_to_t) CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mov(NULL, src->g_to_t));
350 if(src->other_g_to_t) CHECK_ASSIGN(ctx->other_g_to_t, d0_bignum_mov(NULL, src->other_g_to_t));
351 memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
352 ctx->msglen = src->msglen;
353 memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
356 d0_blind_id_clear(ctx);
360 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key_fastreject(d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass)
362 REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
364 CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
365 CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
366 CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
368 CHECK(d0_rsa_generate_key_fastreject(k+1, reject, ctx, pass)); // must fit G for sure
370 CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
376 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
378 return d0_blind_id_generate_private_key_fastreject(ctx, k, NULL, NULL);
381 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
383 d0_iobuf_t *in = NULL;
385 REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
387 in = d0_iobuf_open_read(inbuf, inbuflen);
389 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
390 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
391 CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
392 return d0_iobuf_close(in, NULL);
395 d0_iobuf_close(in, NULL);
399 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
401 d0_iobuf_t *in = NULL;
403 REPLACING(rsa_n); REPLACING(rsa_e);
405 in = d0_iobuf_open_read(inbuf, inbuflen);
406 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
407 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
408 return d0_iobuf_close(in, NULL);
411 d0_iobuf_close(in, NULL);
415 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
417 d0_iobuf_t *out = NULL;
419 USING(rsa_n); USING(rsa_e); USING(rsa_d);
421 out = d0_iobuf_open_write(outbuf, *outbuflen);
422 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
423 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
424 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
425 return d0_iobuf_close(out, outbuflen);
428 d0_iobuf_close(out, outbuflen);
432 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
434 d0_iobuf_t *out = NULL;
436 USING(rsa_n); USING(rsa_e);
438 out = d0_iobuf_open_write(outbuf, *outbuflen);
439 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
440 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
441 return d0_iobuf_close(out, outbuflen);
444 if(!d0_iobuf_close(out, outbuflen))
449 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
451 d0_iobuf_t *out = NULL;
452 static unsigned char convbuf[2048];
453 d0_iobuf_t *conv = NULL;
456 USING(rsa_n); USING(rsa_e);
458 out = d0_iobuf_open_write(outbuf, *outbuflen);
459 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
461 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
462 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
463 CHECK(d0_iobuf_close(conv, &sz));
466 n = (*outbuflen / 4) * 3;
467 if(n > SHA_DIGESTSIZE)
469 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
470 CHECK(d0_iobuf_conv_base64_out(out));
472 return d0_iobuf_close(out, outbuflen);
476 d0_iobuf_close(conv, &sz);
477 d0_iobuf_close(out, outbuflen);
481 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
484 REPLACING(schnorr_G);
486 CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
487 CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
493 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
495 d0_iobuf_t *in = NULL;
497 REPLACING(schnorr_G);
499 in = d0_iobuf_open_read(inbuf, inbuflen);
500 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
501 return d0_iobuf_close(in, NULL);
504 d0_iobuf_close(in, NULL);
508 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
510 d0_iobuf_t *out = NULL;
514 out = d0_iobuf_open_write(outbuf, *outbuflen);
515 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
516 return d0_iobuf_close(out, outbuflen);
519 d0_iobuf_close(out, outbuflen);
523 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
525 // temps: temp0 = order
527 REPLACING(schnorr_s); REPLACING(schnorr_g_to_s);
529 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
530 CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
531 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mod_pow(ctx->schnorr_g_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
532 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
539 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
541 d0_iobuf_t *out = NULL;
542 static unsigned char convbuf[2048], shabuf[2048];
545 // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
546 USING(rsa_n); USING(rsa_e); USING(schnorr_g_to_s);
547 REPLACING(rsa_blind_signature_camouflage);
549 out = d0_iobuf_open_write(outbuf, *outbuflen);
551 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
552 CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
554 // we will actually sign HA(4^s) to prevent a malleability attack!
555 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
556 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
557 if(sz > sizeof(shabuf))
559 CHECK(d0_longhash_destructive(temp2, shabuf, sz));
560 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
563 CHECK(d0_bignum_mod_mul(temp1, temp2, temp0, ctx->rsa_n));
564 CHECK(d0_iobuf_write_bignum(out, temp1));
565 return d0_iobuf_close(out, outbuflen);
568 d0_iobuf_close(out, outbuflen);
572 WARN_UNUSED_RESULT BOOL d0_blind_id_answer_private_id_request(const d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen)
574 d0_iobuf_t *in = NULL;
575 d0_iobuf_t *out = NULL;
577 // temps: temp0 input, temp1 temp0^d
578 USING(rsa_d); USING(rsa_n);
580 in = d0_iobuf_open_read(inbuf, inbuflen);
581 out = d0_iobuf_open_write(outbuf, *outbuflen);
583 CHECK(d0_iobuf_read_bignum(in, temp0));
584 CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
585 CHECK(d0_iobuf_write_bignum(out, temp1));
587 d0_iobuf_close(in, NULL);
588 return d0_iobuf_close(out, outbuflen);
591 d0_iobuf_close(in, NULL);
592 d0_iobuf_close(out, outbuflen);
596 WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
598 d0_iobuf_t *in = NULL;
600 // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
601 USING(rsa_blind_signature_camouflage); USING(rsa_n);
602 REPLACING(schnorr_H_g_to_s_signature);
604 in = d0_iobuf_open_read(inbuf, inbuflen);
606 CHECK(d0_iobuf_read_bignum(in, temp0));
607 CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
608 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_H_g_to_s_signature, temp0, temp1, ctx->rsa_n));
610 return d0_iobuf_close(in, NULL);
613 d0_iobuf_close(in, NULL);
617 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_request_camouflage(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
619 d0_iobuf_t *in = NULL;
621 REPLACING(rsa_blind_signature_camouflage);
623 in = d0_iobuf_open_read(inbuf, inbuflen);
625 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_iobuf_read_bignum(in, ctx->rsa_blind_signature_camouflage));
627 return d0_iobuf_close(in, NULL);
630 d0_iobuf_close(in, NULL);
634 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_request_camouflage(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
636 d0_iobuf_t *out = NULL;
638 USING(rsa_blind_signature_camouflage);
640 out = d0_iobuf_open_write(outbuf, *outbuflen);
642 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_blind_signature_camouflage));
644 return d0_iobuf_close(out, outbuflen);
647 d0_iobuf_close(out, outbuflen);
651 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
653 d0_iobuf_t *in = NULL;
655 REPLACING(schnorr_s); REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
657 in = d0_iobuf_open_read(inbuf, inbuflen);
659 CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
660 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
661 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
663 return d0_iobuf_close(in, NULL);
666 d0_iobuf_close(in, NULL);
670 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
672 d0_iobuf_t *in = NULL;
674 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
676 in = d0_iobuf_open_read(inbuf, inbuflen);
678 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
679 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
681 return d0_iobuf_close(in, NULL);
684 d0_iobuf_close(in, NULL);
688 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
690 d0_iobuf_t *out = NULL;
692 USING(schnorr_s); USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
694 out = d0_iobuf_open_write(outbuf, *outbuflen);
696 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
697 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
698 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
700 return d0_iobuf_close(out, outbuflen);
703 d0_iobuf_close(out, outbuflen);
707 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
709 d0_iobuf_t *out = NULL;
711 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
713 out = d0_iobuf_open_write(outbuf, *outbuflen);
715 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
716 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
718 return d0_iobuf_close(out, outbuflen);
721 d0_iobuf_close(out, outbuflen);
725 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_id_t *ctx, BOOL is_first, BOOL send_modulus, char *msg, size_t msglen, char *outbuf, size_t *outbuflen)
727 // first run: send 4^s, 4^s signature
728 // 1. get random r, send HASH(4^r)
730 d0_iobuf_t *out = NULL;
731 static unsigned char convbuf[1024];
732 d0_iobuf_t *conv = NULL;
736 // temps: temp0 order, temp0 4^r
739 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
742 REPLACING(r); REPLACING(t); REPLACING(g_to_t);
744 out = d0_iobuf_open_write(outbuf, *outbuflen);
750 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
751 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
752 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
755 // start schnorr ID scheme
756 // generate random number r; x = g^r; send hash of x, remember r, forget x
757 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
758 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
759 //CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
761 // initialize Signed Diffie Hellmann
762 // we already have the group order in temp1
763 CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
764 // can we SOMEHOW do this with just one mod_pow?
766 MPCHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
767 MPCHECK_ASSIGN(ctx->g_to_t, d0_bignum_mod_pow(ctx->g_to_t, four, ctx->t, ctx->schnorr_G));
770 // hash it, hash it, everybody hash it
771 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
772 CHECK(d0_iobuf_write_bignum(conv, temp0));
773 CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
774 CHECK(d0_iobuf_write_packet(conv, msg, msglen));
775 CHECK(d0_iobuf_write_bignum(conv, temp0));
776 CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
777 d0_iobuf_close(conv, &sz);
779 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
780 CHECK(d0_iobuf_write_packet(out, msg, msglen));
782 return d0_iobuf_close(out, outbuflen);
785 d0_iobuf_close(out, outbuflen);
789 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_blind_id_t *ctx, BOOL is_first, BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen, BOOL *status)
790 // first run: get 4^s, 4^s signature
793 // 3. send challenge challenge of SCHNORR_BITS
795 d0_iobuf_t *in = NULL;
796 d0_iobuf_t *out = NULL;
797 static unsigned char shabuf[2048];
800 // temps: temp0 order, temp0 signature check
803 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
805 REPLACING(schnorr_G);
811 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
814 USING(rsa_e); USING(rsa_n);
815 REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r); REPLACING(t);
817 in = d0_iobuf_open_read(inbuf, inbuflen);
818 out = d0_iobuf_open_write(outbuf, *outbuflen);
824 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
825 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
826 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
828 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
829 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
830 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
831 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
832 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
833 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
835 // check signature of key (t = k^d, so, t^challenge = k)
836 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
838 // we will actually sign SHA(4^s) to prevent a malleability attack!
839 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
840 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
841 if(sz > sizeof(shabuf))
843 CHECK(d0_longhash_destructive(temp2, shabuf, sz));
844 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
846 // + 7 / 8 is too large, so let's mod it
847 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
850 if(d0_bignum_cmp(temp0, temp1))
852 // accept the key anyway, but mark as failed signature! will later return 0 in status
853 CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
857 CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
858 ctx->msglen = MSGSIZE;
859 CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
862 CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
863 CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
865 // Diffie Hellmann send
866 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
867 CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
868 CHECK(d0_bignum_mod_pow(temp0, four, ctx->t, ctx->schnorr_G));
869 CHECK(d0_iobuf_write_bignum(out, temp0));
872 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
874 d0_iobuf_close(in, NULL);
875 return d0_iobuf_close(out, outbuflen);
878 d0_iobuf_close(in, NULL);
879 d0_iobuf_close(out, outbuflen);
883 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_response(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen)
884 // 1. read challenge challenge of SCHNORR_BITS
885 // 2. reply with r + s * challenge mod order
887 d0_iobuf_t *in = NULL;
888 d0_iobuf_t *out = NULL;
890 // temps: 0 order, 1 prod, 2 y, 3 challenge
891 REPLACING(other_g_to_t); REPLACING(t);
892 USING(schnorr_G); USING(schnorr_s); USING(r); USING(g_to_t);
894 in = d0_iobuf_open_read(inbuf, inbuflen);
895 out = d0_iobuf_open_write(outbuf, *outbuflen);
897 CHECK(d0_iobuf_read_bignum(in, temp3));
898 CHECK(d0_bignum_cmp(temp3, zero) >= 0);
899 CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
901 // send response for schnorr ID scheme
902 // i.challenge. r + ctx->schnorr_s * temp3
903 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
904 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
905 CHECK(d0_bignum_mod_add(temp2, temp1, ctx->r, temp0));
906 CHECK(d0_iobuf_write_bignum(out, temp2));
908 // Diffie Hellmann recv
909 CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
910 CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
911 CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
912 // Diffie Hellmann send
913 CHECK(d0_iobuf_write_bignum(out, ctx->g_to_t));
915 d0_iobuf_close(in, NULL);
916 return d0_iobuf_close(out, outbuflen);
919 d0_iobuf_close(in, NULL);
920 d0_iobuf_close(out, outbuflen);
924 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_verify(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, BOOL *status)
925 // 1. read y = r + s * challenge mod order
926 // 2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
927 // (check using H(g^r) which we know)
929 d0_iobuf_t *in = NULL;
930 static unsigned char convbuf[1024];
931 d0_iobuf_t *conv = NULL;
934 // temps: 0 y 1 order
935 USING(challenge); USING(schnorr_G);
936 REPLACING(other_g_to_t);
938 in = d0_iobuf_open_read(inbuf, inbuflen);
940 CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
941 CHECK(d0_iobuf_read_bignum(in, temp0));
942 CHECK(d0_bignum_cmp(temp0, zero) >= 0);
943 CHECK(d0_bignum_cmp(temp0, temp1) < 0);
945 // verify schnorr ID scheme
946 // we need 4^r = 4^temp0 (g^s)^-challenge
947 CHECK(d0_bignum_mod_inv(temp1, ctx->schnorr_g_to_s, ctx->schnorr_G));
948 CHECK(d0_bignum_mod_pow(temp2, temp1, ctx->challenge, ctx->schnorr_G));
949 CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
950 CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G));
952 // Diffie Hellmann recv
953 CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
954 CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
955 CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
957 // hash it, hash it, everybody hash it
958 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
959 CHECK(d0_iobuf_write_bignum(conv, temp3));
960 CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
961 CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
962 CHECK(d0_iobuf_write_bignum(conv, temp3));
963 CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
964 d0_iobuf_close(conv, &sz);
966 if(memcmp(sha(convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
968 // FAIL (not owned by player)
973 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
975 if(ctx->msglen <= *msglen)
976 memcpy(msg, ctx->msg, ctx->msglen);
978 memcpy(msg, ctx->msg, *msglen);
979 *msglen = ctx->msglen;
981 d0_iobuf_close(in, NULL);
985 d0_iobuf_close(in, NULL);
989 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_generate_missing_signature(d0_blind_id_t *ctx)
992 static unsigned char shabuf[2048];
994 REPLACING(schnorr_H_g_to_s_signature);
995 USING(schnorr_g_to_s); USING(rsa_d); USING(rsa_n);
997 // we will actually sign SHA(4^s) to prevent a malleability attack!
998 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
999 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1000 if(sz > sizeof(shabuf))
1001 sz = sizeof(shabuf);
1002 CHECK(d0_longhash_destructive(temp2, shabuf, sz));
1003 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
1005 // + 7 / 8 is too large, so let's mod it
1006 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1007 CHECK(d0_bignum_mod_pow(ctx->schnorr_H_g_to_s_signature, temp1, ctx->rsa_d, ctx->rsa_n));
1014 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1016 d0_iobuf_t *out = NULL;
1017 static unsigned char convbuf[1024];
1018 d0_iobuf_t *conv = NULL;
1023 USING(schnorr_g_to_s);
1025 out = d0_iobuf_open_write(outbuf, *outbuflen);
1026 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1028 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
1029 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
1030 CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_g_to_s));
1031 CHECK(d0_iobuf_close(conv, &sz));
1034 n = (*outbuflen / 4) * 3;
1035 if(n > SHA_DIGESTSIZE)
1037 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
1038 CHECK(d0_iobuf_conv_base64_out(out));
1040 return d0_iobuf_close(out, outbuflen);
1044 d0_iobuf_close(conv, &sz);
1045 d0_iobuf_close(out, outbuflen);
1049 BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1051 USING(t); USING(other_g_to_t); USING(schnorr_G);
1053 // temps: temp0 result
1054 CHECK(d0_bignum_mod_pow(temp0, ctx->other_g_to_t, ctx->t, ctx->schnorr_G));
1055 return d0_longhash_destructive(temp0, outbuf, *outbuflen);
1061 d0_blind_id_t *d0_blind_id_new(void)
1063 d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
1064 memset(b, 0, sizeof(*b));
1068 void d0_blind_id_free(d0_blind_id_t *a)
1070 d0_blind_id_clear(a);
1074 void d0_blind_id_util_sha256(char *out, const char *in, size_t n)
1077 SHA256_Init(&context);
1078 SHA256_Update(&context, (const unsigned char *) in, n);
1079 return SHA256_Final((unsigned char *) out, &context);