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
35 #include "d0_blind_id.h"
39 #include "d0_bignum.h"
43 #define SHA_DIGESTSIZE 32
44 const char *sha(const char *in, size_t len)
47 d0_blind_id_util_sha256(h, in, len);
51 // for zero knowledge, we need multiple instances of schnorr ID scheme... should normally be sequential
52 // parallel schnorr ID is not provably zero knowledge :(
53 // (evil verifier can know all questions in advance, so sequential is disadvantage for him)
54 // we'll just live with a 1:1048576 chance of cheating, and support reauthenticating
56 #define SCHNORR_BITS 20
57 // probability of cheat: 2^(-bits+1)
59 #define SCHNORR_HASHSIZE SHA_DIGESTSIZE
60 // cannot be >= SHA_DIGESTSIZE
61 // *8 must be >= SCHNORR_BITS
62 // no need to save bits here
64 #define MSGSIZE 640 // ought to be enough for anyone
68 // signing (Xonotic pub and priv key)
69 d0_bignum_t *rsa_n, *rsa_e, *rsa_d;
71 // public data (Schnorr ID)
72 d0_bignum_t *schnorr_G;
74 // private data (player ID private key)
75 d0_bignum_t *schnorr_s;
77 // public data (player ID public key, this is what the server gets to know)
78 d0_bignum_t *schnorr_g_to_s;
79 d0_bignum_t *schnorr_H_g_to_s_signature; // 0 when signature is invalid
80 // as hash function H, we get the SHA1 and reinterpret as bignum - yes, it always is < 160 bits
83 d0_bignum_t *rsa_blind_signature_camouflage; // random number blind signature
85 d0_bignum_t *r; // random number for schnorr ID
86 d0_bignum_t *t; // for DH key exchange
87 d0_bignum_t *g_to_t; // for DH key exchange
88 d0_bignum_t *other_g_to_t; // for DH key exchange
89 d0_bignum_t *challenge; // challenge
91 char msghash[SCHNORR_HASHSIZE]; // init hash
92 char msg[MSGSIZE]; // message
93 size_t msglen; // message length
96 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
97 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
98 #define MPCHECK(x) do { if(!failed) if(!(x)) failed = 1; } while(0)
99 #define MPCHECK_ASSIGN(var, value) do { if(!failed) { d0_bignum_t *val; val = value; if(val) var = val; else failed = 1; } } while(0)
101 #define USING(x) if(!(ctx->x)) return 0
104 static d0_bignum_t *zero, *one, *four, *temp0, *temp1, *temp2, *temp3, *temp4;
106 WARN_UNUSED_RESULT BOOL d0_blind_id_INITIALIZE(void)
108 CHECK(d0_bignum_INITIALIZE());
109 CHECK_ASSIGN(zero, d0_bignum_int(zero, 0));
110 CHECK_ASSIGN(one, d0_bignum_int(one, 1));
111 CHECK_ASSIGN(four, d0_bignum_int(four, 4));
112 CHECK_ASSIGN(temp0, d0_bignum_int(temp0, 0));
113 CHECK_ASSIGN(temp1, d0_bignum_int(temp1, 0));
114 CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0));
115 CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0));
116 CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0));
122 void d0_blind_id_SHUTDOWN(void)
124 d0_bignum_free(zero);
126 d0_bignum_free(four);
127 d0_bignum_free(temp0);
128 d0_bignum_free(temp1);
129 d0_bignum_free(temp2);
130 d0_bignum_free(temp3);
131 d0_bignum_free(temp4);
132 d0_bignum_SHUTDOWN();
136 d0_bignum_t *d0_dl_get_order(d0_bignum_t *o, const d0_bignum_t *G)
138 CHECK_ASSIGN(o, d0_bignum_sub(o, G, one));
139 CHECK(d0_bignum_shl(o, o, -1)); // order o = (G-1)/2
145 d0_bignum_t *d0_dl_get_from_order(d0_bignum_t *G, const d0_bignum_t *o)
147 CHECK_ASSIGN(G, d0_bignum_shl(G, o, 1));
148 CHECK(d0_bignum_add(G, G, one));
154 BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
161 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
162 if(d0_bignum_isprime(temp0, 0) == 0)
164 CHECK(d0_dl_get_from_order(G, temp0));
165 if(d0_bignum_isprime(G, 10) == 0)
167 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
176 BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *challenge, d0_bignum_t *d, d0_bignum_t *n)
178 // uses temp0 to temp4
181 int pb = (size + 1)/2;
189 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
190 if(d0_bignum_isprime(temp0, 10) == 0)
192 CHECK(d0_bignum_sub(temp2, temp0, one));
193 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, challenge));
194 if(!d0_bignum_cmp(temp4, one))
203 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
204 if(!d0_bignum_cmp(temp1, temp0))
210 if(d0_bignum_isprime(temp1, 10) == 0)
212 CHECK(d0_bignum_sub(temp3, temp1, one));
213 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, challenge));
214 if(!d0_bignum_cmp(temp4, one))
222 CHECK(d0_bignum_mul(n, temp0, temp1));
224 // d = challenge^-1 mod (temp0-1)(temp1-1)
225 CHECK(d0_bignum_mul(temp0, temp2, temp3));
226 CHECK(d0_bignum_mod_inv(d, challenge, temp0));
232 BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass)
234 // uses temp0 to temp4
237 int pb = (size + 1)/2;
245 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
246 if(d0_bignum_isprime(temp0, 10) == 0)
248 CHECK(d0_bignum_sub(temp2, temp0, one));
249 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
250 if(!d0_bignum_cmp(temp4, one))
259 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
260 if(!d0_bignum_cmp(temp1, temp0))
268 CHECK(d0_bignum_mul(ctx->rsa_n, temp0, temp1));
269 if(reject(ctx, pass))
272 if(d0_bignum_isprime(temp1, 10) == 0)
274 CHECK(d0_bignum_sub(temp3, temp1, one));
275 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
276 if(!d0_bignum_cmp(temp4, one))
283 // ctx->rsa_d = ctx->rsa_e^-1 mod (temp0-1)(temp1-1)
284 CHECK(d0_bignum_mul(temp0, temp2, temp3));
285 CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp0));
291 WARN_UNUSED_RESULT BOOL d0_longhash_destructive(d0_bignum_t *clobberme, char *outbuf, size_t outbuflen)
293 d0_iobuf_t *out = NULL;
294 static unsigned char convbuf[1024];
298 while(n > SHA_DIGESTSIZE)
300 sz = (d0_bignum_size(clobberme) + 7) / 8;
301 CHECK(d0_bignum_export_unsigned(clobberme, convbuf, sizeof(convbuf)) >= 0);
302 memcpy(outbuf, sha(convbuf, sz), SHA_DIGESTSIZE);
303 outbuf += SHA_DIGESTSIZE;
305 CHECK(d0_bignum_add(clobberme, clobberme, one));
307 sz = (d0_bignum_size(clobberme) + 7) / 8;
308 CHECK(d0_bignum_export_unsigned(clobberme, convbuf, sizeof(convbuf)) >= 0);
309 memcpy(outbuf, sha(convbuf, sz), n);
316 void d0_blind_id_clear(d0_blind_id_t *ctx)
318 if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
319 if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
320 if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
321 if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
322 if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
323 if(ctx->schnorr_g_to_s) d0_bignum_free(ctx->schnorr_g_to_s);
324 if(ctx->schnorr_H_g_to_s_signature) d0_bignum_free(ctx->schnorr_H_g_to_s_signature);
325 if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
326 if(ctx->r) d0_bignum_free(ctx->r);
327 if(ctx->challenge) d0_bignum_free(ctx->challenge);
328 if(ctx->t) d0_bignum_free(ctx->t);
329 if(ctx->g_to_t) d0_bignum_free(ctx->g_to_t);
330 if(ctx->other_g_to_t) d0_bignum_free(ctx->other_g_to_t);
331 memset(ctx, 0, sizeof(*ctx));
334 WARN_UNUSED_RESULT BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
336 d0_blind_id_clear(ctx);
337 if(src->rsa_n) CHECK_ASSIGN(ctx->rsa_n, d0_bignum_mov(NULL, src->rsa_n));
338 if(src->rsa_e) CHECK_ASSIGN(ctx->rsa_e, d0_bignum_mov(NULL, src->rsa_e));
339 if(src->rsa_d) CHECK_ASSIGN(ctx->rsa_d, d0_bignum_mov(NULL, src->rsa_d));
340 if(src->schnorr_G) CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_mov(NULL, src->schnorr_G));
341 if(src->schnorr_s) CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_mov(NULL, src->schnorr_s));
342 if(src->schnorr_g_to_s) CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mov(NULL, src->schnorr_g_to_s));
343 if(src->schnorr_H_g_to_s_signature) CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_mov(NULL, src->schnorr_H_g_to_s_signature));
344 if(src->rsa_blind_signature_camouflage) CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage));
345 if(src->r) CHECK_ASSIGN(ctx->r, d0_bignum_mov(NULL, src->r));
346 if(src->challenge) CHECK_ASSIGN(ctx->challenge, d0_bignum_mov(NULL, src->challenge));
347 if(src->t) CHECK_ASSIGN(ctx->t, d0_bignum_mov(NULL, src->t));
348 if(src->g_to_t) CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mov(NULL, src->g_to_t));
349 if(src->other_g_to_t) CHECK_ASSIGN(ctx->other_g_to_t, d0_bignum_mov(NULL, src->other_g_to_t));
350 memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
351 ctx->msglen = src->msglen;
352 memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
355 d0_blind_id_clear(ctx);
359 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key_fastreject(d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass)
361 REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
363 CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
364 CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
365 CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
367 CHECK(d0_rsa_generate_key_fastreject(k+1, reject, ctx, pass)); // must fit G for sure
369 CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
375 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
377 return d0_blind_id_generate_private_key_fastreject(ctx, k, NULL, NULL);
380 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
382 d0_iobuf_t *in = NULL;
384 REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
386 in = d0_iobuf_open_read(inbuf, inbuflen);
388 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
389 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
390 CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
391 return d0_iobuf_close(in, NULL);
394 d0_iobuf_close(in, NULL);
398 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
400 d0_iobuf_t *in = NULL;
402 REPLACING(rsa_n); REPLACING(rsa_e);
404 in = d0_iobuf_open_read(inbuf, inbuflen);
405 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
406 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
407 return d0_iobuf_close(in, NULL);
410 d0_iobuf_close(in, NULL);
414 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
416 d0_iobuf_t *out = NULL;
418 USING(rsa_n); USING(rsa_e); USING(rsa_d);
420 out = d0_iobuf_open_write(outbuf, *outbuflen);
421 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
422 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
423 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
424 return d0_iobuf_close(out, outbuflen);
427 d0_iobuf_close(out, outbuflen);
431 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
433 d0_iobuf_t *out = NULL;
435 USING(rsa_n); USING(rsa_e);
437 out = d0_iobuf_open_write(outbuf, *outbuflen);
438 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
439 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
440 return d0_iobuf_close(out, outbuflen);
443 if(!d0_iobuf_close(out, outbuflen))
448 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
450 d0_iobuf_t *out = NULL;
451 static unsigned char convbuf[2048];
452 d0_iobuf_t *conv = NULL;
455 USING(rsa_n); USING(rsa_e);
457 out = d0_iobuf_open_write(outbuf, *outbuflen);
458 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
460 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
461 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
462 CHECK(d0_iobuf_close(conv, &sz));
465 n = (*outbuflen / 4) * 3;
466 if(n > SHA_DIGESTSIZE)
468 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
469 CHECK(d0_iobuf_conv_base64_out(out));
471 return d0_iobuf_close(out, outbuflen);
475 d0_iobuf_close(conv, &sz);
476 d0_iobuf_close(out, outbuflen);
480 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
483 REPLACING(schnorr_G);
485 CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
486 CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
492 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
494 d0_iobuf_t *in = NULL;
496 REPLACING(schnorr_G);
498 in = d0_iobuf_open_read(inbuf, inbuflen);
499 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
500 return d0_iobuf_close(in, NULL);
503 d0_iobuf_close(in, NULL);
507 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
509 d0_iobuf_t *out = NULL;
513 out = d0_iobuf_open_write(outbuf, *outbuflen);
514 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
515 return d0_iobuf_close(out, outbuflen);
518 d0_iobuf_close(out, outbuflen);
522 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
524 // temps: temp0 = order
526 REPLACING(schnorr_s); REPLACING(schnorr_g_to_s);
528 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
529 CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
530 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mod_pow(ctx->schnorr_g_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
531 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
538 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
540 d0_iobuf_t *out = NULL;
541 static unsigned char convbuf[2048], shabuf[2048];
544 // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
545 USING(rsa_n); USING(rsa_e); USING(schnorr_g_to_s);
546 REPLACING(rsa_blind_signature_camouflage);
548 out = d0_iobuf_open_write(outbuf, *outbuflen);
550 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
551 CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
553 // we will actually sign HA(4^s) to prevent a malleability attack!
554 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
555 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
556 if(sz > sizeof(shabuf))
558 CHECK(d0_longhash_destructive(temp2, shabuf, sz));
559 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
562 CHECK(d0_bignum_mod_mul(temp1, temp2, temp0, ctx->rsa_n));
563 CHECK(d0_iobuf_write_bignum(out, temp1));
564 return d0_iobuf_close(out, outbuflen);
567 d0_iobuf_close(out, outbuflen);
571 WARN_UNUSED_RESULT BOOL d0_blind_id_answer_private_id_request(const d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen)
573 d0_iobuf_t *in = NULL;
574 d0_iobuf_t *out = NULL;
576 // temps: temp0 input, temp1 temp0^d
577 USING(rsa_d); USING(rsa_n);
579 in = d0_iobuf_open_read(inbuf, inbuflen);
580 out = d0_iobuf_open_write(outbuf, *outbuflen);
582 CHECK(d0_iobuf_read_bignum(in, temp0));
583 CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
584 CHECK(d0_iobuf_write_bignum(out, temp1));
586 d0_iobuf_close(in, NULL);
587 return d0_iobuf_close(out, outbuflen);
590 d0_iobuf_close(in, NULL);
591 d0_iobuf_close(out, outbuflen);
595 WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
597 d0_iobuf_t *in = NULL;
599 // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
600 USING(rsa_blind_signature_camouflage); USING(rsa_n);
601 REPLACING(schnorr_H_g_to_s_signature);
603 in = d0_iobuf_open_read(inbuf, inbuflen);
605 CHECK(d0_iobuf_read_bignum(in, temp0));
606 CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
607 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_H_g_to_s_signature, temp0, temp1, ctx->rsa_n));
609 return d0_iobuf_close(in, NULL);
612 d0_iobuf_close(in, NULL);
616 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_request_camouflage(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
618 d0_iobuf_t *in = NULL;
620 REPLACING(rsa_blind_signature_camouflage);
622 in = d0_iobuf_open_read(inbuf, inbuflen);
624 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_iobuf_read_bignum(in, ctx->rsa_blind_signature_camouflage));
626 return d0_iobuf_close(in, NULL);
629 d0_iobuf_close(in, NULL);
633 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_request_camouflage(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
635 d0_iobuf_t *out = NULL;
637 USING(rsa_blind_signature_camouflage);
639 out = d0_iobuf_open_write(outbuf, *outbuflen);
641 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_blind_signature_camouflage));
643 return d0_iobuf_close(out, outbuflen);
646 d0_iobuf_close(out, outbuflen);
650 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
652 d0_iobuf_t *in = NULL;
654 REPLACING(schnorr_s); REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
656 in = d0_iobuf_open_read(inbuf, inbuflen);
658 CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
659 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
660 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
662 return d0_iobuf_close(in, NULL);
665 d0_iobuf_close(in, NULL);
669 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
671 d0_iobuf_t *in = NULL;
673 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
675 in = d0_iobuf_open_read(inbuf, inbuflen);
677 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
678 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
680 return d0_iobuf_close(in, NULL);
683 d0_iobuf_close(in, NULL);
687 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
689 d0_iobuf_t *out = NULL;
691 USING(schnorr_s); USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
693 out = d0_iobuf_open_write(outbuf, *outbuflen);
695 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
696 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
697 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
699 return d0_iobuf_close(out, outbuflen);
702 d0_iobuf_close(out, outbuflen);
706 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
708 d0_iobuf_t *out = NULL;
710 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
712 out = d0_iobuf_open_write(outbuf, *outbuflen);
714 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
715 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
717 return d0_iobuf_close(out, outbuflen);
720 d0_iobuf_close(out, outbuflen);
724 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_id_t *ctx, BOOL is_first, BOOL send_modulus, char *msg, size_t msglen, char *outbuf, size_t *outbuflen)
726 // first run: send 4^s, 4^s signature
727 // 1. get random r, send HASH(4^r)
729 d0_iobuf_t *out = NULL;
730 static unsigned char convbuf[1024];
731 d0_iobuf_t *conv = NULL;
735 // temps: temp0 order, temp0 4^r
738 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
741 REPLACING(r); REPLACING(t); REPLACING(g_to_t);
743 out = d0_iobuf_open_write(outbuf, *outbuflen);
749 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
750 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
751 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
754 // start schnorr ID scheme
755 // generate random number r; x = g^r; send hash of x, remember r, forget x
756 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
757 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
758 //CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
760 // initialize Signed Diffie Hellmann
761 // we already have the group order in temp1
762 CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
763 // can we SOMEHOW do this with just one mod_pow?
765 MPCHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
766 MPCHECK_ASSIGN(ctx->g_to_t, d0_bignum_mod_pow(ctx->g_to_t, four, ctx->t, ctx->schnorr_G));
769 // hash it, hash it, everybody hash it
770 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
771 CHECK(d0_iobuf_write_bignum(conv, temp0));
772 CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
773 CHECK(d0_iobuf_write_packet(conv, msg, msglen));
774 CHECK(d0_iobuf_write_bignum(conv, temp0));
775 CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
776 d0_iobuf_close(conv, &sz);
778 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
779 CHECK(d0_iobuf_write_packet(out, msg, msglen));
781 return d0_iobuf_close(out, outbuflen);
784 d0_iobuf_close(out, outbuflen);
788 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_blind_id_t *ctx, BOOL is_first, BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen, BOOL *status)
789 // first run: get 4^s, 4^s signature
792 // 3. send challenge challenge of SCHNORR_BITS
794 d0_iobuf_t *in = NULL;
795 d0_iobuf_t *out = NULL;
796 static unsigned char shabuf[2048];
799 // temps: temp0 order, temp0 signature check
802 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
804 REPLACING(schnorr_G);
810 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
813 USING(rsa_e); USING(rsa_n);
814 REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r); REPLACING(t);
816 in = d0_iobuf_open_read(inbuf, inbuflen);
817 out = d0_iobuf_open_write(outbuf, *outbuflen);
823 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
824 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
825 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
827 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
828 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
829 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
830 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
831 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
832 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
834 // check signature of key (t = k^d, so, t^challenge = k)
835 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
837 // we will actually sign SHA(4^s) to prevent a malleability attack!
838 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
839 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
840 if(sz > sizeof(shabuf))
842 CHECK(d0_longhash_destructive(temp2, shabuf, sz));
843 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
845 // + 7 / 8 is too large, so let's mod it
846 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
849 if(d0_bignum_cmp(temp0, temp1))
851 // accept the key anyway, but mark as failed signature! will later return 0 in status
852 CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
856 CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
857 ctx->msglen = MSGSIZE;
858 CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
861 CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
862 CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
864 // Diffie Hellmann send
865 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
866 CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
867 CHECK(d0_bignum_mod_pow(temp0, four, ctx->t, ctx->schnorr_G));
868 CHECK(d0_iobuf_write_bignum(out, temp0));
871 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
873 d0_iobuf_close(in, NULL);
874 return d0_iobuf_close(out, outbuflen);
877 d0_iobuf_close(in, NULL);
878 d0_iobuf_close(out, outbuflen);
882 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_response(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen)
883 // 1. read challenge challenge of SCHNORR_BITS
884 // 2. reply with r + s * challenge mod order
886 d0_iobuf_t *in = NULL;
887 d0_iobuf_t *out = NULL;
889 // temps: 0 order, 1 prod, 2 y, 3 challenge
890 REPLACING(other_g_to_t); REPLACING(t);
891 USING(schnorr_G); USING(schnorr_s); USING(r); USING(g_to_t);
893 in = d0_iobuf_open_read(inbuf, inbuflen);
894 out = d0_iobuf_open_write(outbuf, *outbuflen);
896 CHECK(d0_iobuf_read_bignum(in, temp3));
897 CHECK(d0_bignum_cmp(temp3, zero) >= 0);
898 CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
900 // send response for schnorr ID scheme
901 // i.challenge. r + ctx->schnorr_s * temp3
902 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
903 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
904 CHECK(d0_bignum_mod_add(temp2, temp1, ctx->r, temp0));
905 CHECK(d0_iobuf_write_bignum(out, temp2));
907 // Diffie Hellmann recv
908 CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
909 CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
910 CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
911 // Diffie Hellmann send
912 CHECK(d0_iobuf_write_bignum(out, ctx->g_to_t));
914 d0_iobuf_close(in, NULL);
915 return d0_iobuf_close(out, outbuflen);
918 d0_iobuf_close(in, NULL);
919 d0_iobuf_close(out, outbuflen);
923 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_verify(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, BOOL *status)
924 // 1. read y = r + s * challenge mod order
925 // 2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
926 // (check using H(g^r) which we know)
928 d0_iobuf_t *in = NULL;
929 static unsigned char convbuf[1024];
930 d0_iobuf_t *conv = NULL;
933 // temps: 0 y 1 order
934 USING(challenge); USING(schnorr_G);
935 REPLACING(other_g_to_t);
937 in = d0_iobuf_open_read(inbuf, inbuflen);
939 CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
940 CHECK(d0_iobuf_read_bignum(in, temp0));
941 CHECK(d0_bignum_cmp(temp0, zero) >= 0);
942 CHECK(d0_bignum_cmp(temp0, temp1) < 0);
944 // verify schnorr ID scheme
945 // we need 4^r = 4^temp0 (g^s)^-challenge
946 CHECK(d0_bignum_mod_inv(temp1, ctx->schnorr_g_to_s, ctx->schnorr_G));
947 CHECK(d0_bignum_mod_pow(temp2, temp1, ctx->challenge, ctx->schnorr_G));
948 CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
949 CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G));
951 // Diffie Hellmann recv
952 CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
953 CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
954 CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
956 // hash it, hash it, everybody hash it
957 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
958 CHECK(d0_iobuf_write_bignum(conv, temp3));
959 CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
960 CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
961 CHECK(d0_iobuf_write_bignum(conv, temp3));
962 CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
963 d0_iobuf_close(conv, &sz);
965 if(memcmp(sha(convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
967 // FAIL (not owned by player)
972 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
974 if(ctx->msglen <= *msglen)
975 memcpy(msg, ctx->msg, ctx->msglen);
977 memcpy(msg, ctx->msg, *msglen);
978 *msglen = ctx->msglen;
980 d0_iobuf_close(in, NULL);
984 d0_iobuf_close(in, NULL);
988 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_generate_missing_signature(d0_blind_id_t *ctx)
991 static unsigned char shabuf[2048];
993 REPLACING(schnorr_H_g_to_s_signature);
994 USING(schnorr_g_to_s); USING(rsa_d); USING(rsa_n);
996 // we will actually sign SHA(4^s) to prevent a malleability attack!
997 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
998 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
999 if(sz > sizeof(shabuf))
1000 sz = sizeof(shabuf);
1001 CHECK(d0_longhash_destructive(temp2, shabuf, sz));
1002 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
1004 // + 7 / 8 is too large, so let's mod it
1005 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1006 CHECK(d0_bignum_mod_pow(ctx->schnorr_H_g_to_s_signature, temp1, ctx->rsa_d, ctx->rsa_n));
1013 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1015 d0_iobuf_t *out = NULL;
1016 static unsigned char convbuf[1024];
1017 d0_iobuf_t *conv = NULL;
1022 USING(schnorr_g_to_s);
1024 out = d0_iobuf_open_write(outbuf, *outbuflen);
1025 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1027 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
1028 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
1029 CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_g_to_s));
1030 CHECK(d0_iobuf_close(conv, &sz));
1033 n = (*outbuflen / 4) * 3;
1034 if(n > SHA_DIGESTSIZE)
1036 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
1037 CHECK(d0_iobuf_conv_base64_out(out));
1039 return d0_iobuf_close(out, outbuflen);
1043 d0_iobuf_close(conv, &sz);
1044 d0_iobuf_close(out, outbuflen);
1048 BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1050 USING(t); USING(other_g_to_t); USING(schnorr_G);
1052 // temps: temp0 result
1053 CHECK(d0_bignum_mod_pow(temp0, ctx->other_g_to_t, ctx->t, ctx->schnorr_G));
1054 return d0_longhash_destructive(temp0, outbuf, *outbuflen);
1060 d0_blind_id_t *d0_blind_id_new(void)
1062 d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
1063 memset(b, 0, sizeof(*b));
1067 void d0_blind_id_free(d0_blind_id_t *a)
1069 d0_blind_id_clear(a);
1073 void d0_blind_id_util_sha256(char *out, const char *in, size_t n)
1076 SHA256_Init(&context);
1077 SHA256_Update(&context, (const unsigned char *) in, n);
1078 return SHA256_Final((unsigned char *) out, &context);