2 Blind-ID library for user identification using RSA blind signatures
3 Copyright (C) 2010 Rudolf Polzer
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.
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.
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
20 #include "d0_blind_id.h"
24 #include "d0_bignum.h"
28 #define SHA_DIGESTSIZE 32
29 const char *sha(const char *in, size_t len)
32 d0_blind_id_util_sha256(h, in, len);
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
41 #define SCHNORR_BITS 20
42 // probability of cheat: 2^(-bits+1)
44 #define SCHNORR_HASHSIZE SHA_DIGESTSIZE
45 // cannot be >= SHA_DIGESTSIZE
46 // *8 must be >= SCHNORR_BITS
47 // no need to save bits here
49 #define MSGSIZE 640 // ought to be enough for anyone
53 // signing (Xonotic pub and priv key)
54 d0_bignum_t *rsa_n, *rsa_e, *rsa_d;
56 // public data (Schnorr ID)
57 d0_bignum_t *schnorr_G;
59 // private data (player ID private key)
60 d0_bignum_t *schnorr_s;
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
68 d0_bignum_t *rsa_blind_signature_camouflage; // random number blind signature
70 d0_bignum_t *r; // random number for schnorr ID
71 d0_bignum_t *other_4_to_r; // for DH key exchange
72 d0_bignum_t *challenge; // challenge
74 char msghash[SCHNORR_HASHSIZE]; // init hash
75 char msg[MSGSIZE]; // message
76 size_t msglen; // message length
79 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
80 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
82 #define USING(x) if(!(ctx->x)) return 0
85 static d0_bignum_t *zero, *one, *four, *temp0, *temp1, *temp2, *temp3, *temp4;
87 void d0_blind_id_INITIALIZE(void)
89 d0_bignum_INITIALIZE();
90 CHECK_ASSIGN(zero, d0_bignum_int(zero, 0));
91 CHECK_ASSIGN(one, d0_bignum_int(one, 1));
92 CHECK_ASSIGN(four, d0_bignum_int(four, 4));
93 CHECK_ASSIGN(temp0, d0_bignum_int(temp0, 0));
94 CHECK_ASSIGN(temp1, d0_bignum_int(temp1, 0));
95 CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0));
96 CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0));
97 CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0));
102 void d0_blind_id_SHUTDOWN(void)
104 d0_bignum_free(zero);
106 d0_bignum_free(four);
107 d0_bignum_free(temp0);
108 d0_bignum_free(temp1);
109 d0_bignum_free(temp2);
110 d0_bignum_free(temp3);
111 d0_bignum_free(temp4);
112 d0_bignum_SHUTDOWN();
116 d0_bignum_t *d0_dl_get_order(d0_bignum_t *o, const d0_bignum_t *G)
118 CHECK_ASSIGN(o, d0_bignum_sub(o, G, one));
119 CHECK(d0_bignum_shl(o, o, -1)); // order o = (G-1)/2
125 d0_bignum_t *d0_dl_get_from_order(d0_bignum_t *G, const d0_bignum_t *o)
127 CHECK_ASSIGN(G, d0_bignum_shl(G, o, 1));
128 CHECK(d0_bignum_add(G, G, one));
134 BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
141 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
142 if(d0_bignum_isprime(temp0, 0) == 0)
144 CHECK(d0_dl_get_from_order(G, temp0));
145 if(d0_bignum_isprime(G, 10) == 0)
147 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
156 BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *challenge, d0_bignum_t *d, d0_bignum_t *n)
158 // uses temp0 to temp4
161 int pb = (size + 1)/2;
169 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
170 if(d0_bignum_isprime(temp0, 10) == 0)
172 CHECK(d0_bignum_sub(temp2, temp0, one));
173 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, challenge));
174 if(!d0_bignum_cmp(temp4, one))
183 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
184 if(!d0_bignum_cmp(temp1, temp0))
190 if(d0_bignum_isprime(temp1, 10) == 0)
192 CHECK(d0_bignum_sub(temp3, temp1, one));
193 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, challenge));
194 if(!d0_bignum_cmp(temp4, one))
202 CHECK(d0_bignum_mul(n, temp0, temp1));
204 // d = challenge^-1 mod (temp0-1)(temp1-1)
205 CHECK(d0_bignum_mul(temp0, temp2, temp3));
206 CHECK(d0_bignum_mod_inv(d, challenge, temp0));
212 BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass)
214 // uses temp0 to temp4
217 int pb = (size + 1)/2;
225 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
226 if(d0_bignum_isprime(temp0, 10) == 0)
228 CHECK(d0_bignum_sub(temp2, temp0, one));
229 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
230 if(!d0_bignum_cmp(temp4, one))
239 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
240 if(!d0_bignum_cmp(temp1, temp0))
248 CHECK(d0_bignum_mul(ctx->rsa_n, temp0, temp1));
249 if(reject(ctx, pass))
252 if(d0_bignum_isprime(temp1, 10) == 0)
254 CHECK(d0_bignum_sub(temp3, temp1, one));
255 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
256 if(!d0_bignum_cmp(temp4, one))
263 // ctx->rsa_d = ctx->rsa_e^-1 mod (temp0-1)(temp1-1)
264 CHECK(d0_bignum_mul(temp0, temp2, temp3));
265 CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp0));
271 WARN_UNUSED_RESULT BOOL d0_longhash_destructive(d0_bignum_t *clobberme, char *outbuf, size_t outbuflen)
273 d0_iobuf_t *out = NULL;
274 static unsigned char convbuf[1024];
278 while(n > SHA_DIGESTSIZE)
280 sz = (d0_bignum_size(clobberme) + 7) / 8;
281 CHECK(d0_bignum_export_unsigned(clobberme, convbuf, sizeof(convbuf)) >= 0);
282 memcpy(outbuf, sha(convbuf, sz), SHA_DIGESTSIZE);
283 outbuf += SHA_DIGESTSIZE;
285 CHECK(d0_bignum_add(clobberme, clobberme, one));
287 sz = (d0_bignum_size(clobberme) + 7) / 8;
288 CHECK(d0_bignum_export_unsigned(clobberme, convbuf, sizeof(convbuf)) >= 0);
289 memcpy(outbuf, sha(convbuf, sz), n);
296 void d0_blind_id_clear(d0_blind_id_t *ctx)
298 if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
299 if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
300 if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
301 if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
302 if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
303 if(ctx->schnorr_4_to_s) d0_bignum_free(ctx->schnorr_4_to_s);
304 if(ctx->schnorr_H_4_to_s_signature) d0_bignum_free(ctx->schnorr_H_4_to_s_signature);
305 if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
306 if(ctx->r) d0_bignum_free(ctx->r);
307 if(ctx->challenge) d0_bignum_free(ctx->challenge);
308 if(ctx->other_4_to_r) d0_bignum_free(ctx->other_4_to_r);
309 memset(ctx, 0, sizeof(*ctx));
312 WARN_UNUSED_RESULT BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
314 d0_blind_id_clear(ctx);
315 if(src->rsa_n) CHECK_ASSIGN(ctx->rsa_n, d0_bignum_mov(NULL, src->rsa_n));
316 if(src->rsa_e) CHECK_ASSIGN(ctx->rsa_e, d0_bignum_mov(NULL, src->rsa_e));
317 if(src->rsa_d) CHECK_ASSIGN(ctx->rsa_d, d0_bignum_mov(NULL, src->rsa_d));
318 if(src->schnorr_G) CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_mov(NULL, src->schnorr_G));
319 if(src->schnorr_s) CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_mov(NULL, src->schnorr_s));
320 if(src->schnorr_4_to_s) CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_bignum_mov(NULL, src->schnorr_4_to_s));
321 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));
322 if(src->rsa_blind_signature_camouflage) CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage));
323 if(src->r) CHECK_ASSIGN(ctx->r, d0_bignum_mov(NULL, src->r));
324 if(src->challenge) CHECK_ASSIGN(ctx->challenge, d0_bignum_mov(NULL, src->challenge));
325 if(src->other_4_to_r) CHECK_ASSIGN(ctx->other_4_to_r, d0_bignum_mov(NULL, src->other_4_to_r));
326 memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
327 ctx->msglen = src->msglen;
328 memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
331 d0_blind_id_clear(ctx);
335 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key_fastreject(d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass)
337 REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
339 CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
340 CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
341 CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
343 CHECK(d0_rsa_generate_key_fastreject(k+1, reject, ctx, pass)); // must fit G for sure
345 CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
351 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
353 return d0_blind_id_generate_private_key_fastreject(ctx, k, NULL, NULL);
356 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
358 d0_iobuf_t *in = NULL;
360 REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
362 in = d0_iobuf_open_read(inbuf, inbuflen);
364 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
365 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
366 CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
367 return d0_iobuf_close(in, NULL);
370 d0_iobuf_close(in, NULL);
374 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
376 d0_iobuf_t *in = NULL;
378 REPLACING(rsa_n); REPLACING(rsa_e);
380 in = d0_iobuf_open_read(inbuf, inbuflen);
381 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
382 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
383 return d0_iobuf_close(in, NULL);
386 d0_iobuf_close(in, NULL);
390 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
392 d0_iobuf_t *out = NULL;
394 USING(rsa_n); USING(rsa_e); USING(rsa_d);
396 out = d0_iobuf_open_write(outbuf, *outbuflen);
397 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
398 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
399 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
400 return d0_iobuf_close(out, outbuflen);
403 d0_iobuf_close(out, outbuflen);
407 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
409 d0_iobuf_t *out = NULL;
411 USING(rsa_n); USING(rsa_e);
413 out = d0_iobuf_open_write(outbuf, *outbuflen);
414 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
415 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
416 return d0_iobuf_close(out, outbuflen);
419 if(!d0_iobuf_close(out, outbuflen))
424 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
426 d0_iobuf_t *out = NULL;
427 static unsigned char convbuf[2048];
428 d0_iobuf_t *conv = NULL;
431 USING(rsa_n); USING(rsa_e);
433 out = d0_iobuf_open_write(outbuf, *outbuflen);
434 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
436 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
437 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
438 CHECK(d0_iobuf_close(conv, &sz));
441 n = (*outbuflen / 4) * 3;
442 if(n > SHA_DIGESTSIZE)
444 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
445 CHECK(d0_iobuf_conv_base64_out(out));
447 return d0_iobuf_close(out, outbuflen);
451 d0_iobuf_close(conv, &sz);
452 d0_iobuf_close(out, outbuflen);
456 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
459 REPLACING(schnorr_G);
461 CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
462 CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
468 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
470 d0_iobuf_t *in = NULL;
472 REPLACING(schnorr_G);
474 in = d0_iobuf_open_read(inbuf, inbuflen);
475 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
476 return d0_iobuf_close(in, NULL);
479 d0_iobuf_close(in, NULL);
483 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
485 d0_iobuf_t *out = NULL;
489 out = d0_iobuf_open_write(outbuf, *outbuflen);
490 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
491 return d0_iobuf_close(out, outbuflen);
494 d0_iobuf_close(out, outbuflen);
498 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
500 // temps: temp0 = order
502 REPLACING(schnorr_s); REPLACING(schnorr_4_to_s);
504 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
505 CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
506 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_bignum_mod_pow(ctx->schnorr_4_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
507 CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_bignum_zero(ctx->schnorr_H_4_to_s_signature));
514 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
516 d0_iobuf_t *out = NULL;
517 static unsigned char convbuf[2048], shabuf[2048];
520 // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
521 USING(rsa_n); USING(rsa_e); USING(schnorr_4_to_s);
522 REPLACING(rsa_blind_signature_camouflage);
524 out = d0_iobuf_open_write(outbuf, *outbuflen);
526 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
527 CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
529 // we will actually sign HA(4^s) to prevent a malleability attack!
530 CHECK(d0_bignum_mov(temp2, ctx->schnorr_4_to_s));
531 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
532 if(sz > sizeof(shabuf))
534 CHECK(d0_longhash_destructive(temp2, shabuf, sz));
535 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
538 CHECK(d0_bignum_mod_mul(temp1, temp2, temp0, ctx->rsa_n));
539 CHECK(d0_iobuf_write_bignum(out, temp1));
540 return d0_iobuf_close(out, outbuflen);
543 d0_iobuf_close(out, outbuflen);
547 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)
549 d0_iobuf_t *in = NULL;
550 d0_iobuf_t *out = NULL;
552 // temps: temp0 input, temp1 temp0^d
553 USING(rsa_d); USING(rsa_n);
555 in = d0_iobuf_open_read(inbuf, inbuflen);
556 out = d0_iobuf_open_write(outbuf, *outbuflen);
558 CHECK(d0_iobuf_read_bignum(in, temp0));
559 CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
560 CHECK(d0_iobuf_write_bignum(out, temp1));
562 d0_iobuf_close(in, NULL);
563 return d0_iobuf_close(out, outbuflen);
566 d0_iobuf_close(in, NULL);
567 d0_iobuf_close(out, outbuflen);
571 WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
573 d0_iobuf_t *in = NULL;
575 // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
576 USING(rsa_blind_signature_camouflage); USING(rsa_n);
577 REPLACING(schnorr_H_4_to_s_signature);
579 in = d0_iobuf_open_read(inbuf, inbuflen);
581 CHECK(d0_iobuf_read_bignum(in, temp0));
582 CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
583 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));
585 return d0_iobuf_close(in, NULL);
588 d0_iobuf_close(in, NULL);
592 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_request_camouflage(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
594 d0_iobuf_t *in = NULL;
596 REPLACING(rsa_blind_signature_camouflage);
598 in = d0_iobuf_open_read(inbuf, inbuflen);
600 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_iobuf_read_bignum(in, ctx->rsa_blind_signature_camouflage));
602 return d0_iobuf_close(in, NULL);
605 d0_iobuf_close(in, NULL);
609 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_request_camouflage(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
611 d0_iobuf_t *out = NULL;
613 USING(rsa_blind_signature_camouflage);
615 out = d0_iobuf_open_write(outbuf, *outbuflen);
617 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_blind_signature_camouflage));
619 return d0_iobuf_close(out, outbuflen);
622 d0_iobuf_close(out, outbuflen);
626 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
628 d0_iobuf_t *in = NULL;
630 REPLACING(schnorr_s); REPLACING(schnorr_4_to_s); REPLACING(schnorr_H_4_to_s_signature);
632 in = d0_iobuf_open_read(inbuf, inbuflen);
634 CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
635 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
636 CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_4_to_s_signature));
638 return d0_iobuf_close(in, NULL);
641 d0_iobuf_close(in, NULL);
645 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
647 d0_iobuf_t *in = NULL;
649 REPLACING(schnorr_4_to_s); REPLACING(schnorr_H_4_to_s_signature);
651 in = d0_iobuf_open_read(inbuf, inbuflen);
653 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
654 CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_4_to_s_signature));
656 return d0_iobuf_close(in, NULL);
659 d0_iobuf_close(in, NULL);
663 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
665 d0_iobuf_t *out = NULL;
667 USING(schnorr_s); USING(schnorr_4_to_s); USING(schnorr_H_4_to_s_signature);
669 out = d0_iobuf_open_write(outbuf, *outbuflen);
671 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
672 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
673 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_4_to_s_signature));
675 return d0_iobuf_close(out, outbuflen);
678 d0_iobuf_close(out, outbuflen);
682 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
684 d0_iobuf_t *out = NULL;
686 USING(schnorr_4_to_s); USING(schnorr_H_4_to_s_signature);
688 out = d0_iobuf_open_write(outbuf, *outbuflen);
690 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
691 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_4_to_s_signature));
693 return d0_iobuf_close(out, outbuflen);
696 d0_iobuf_close(out, outbuflen);
700 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)
702 // first run: send 4^s, 4^s signature
703 // 1. get random r, send HASH(4^r)
705 d0_iobuf_t *out = NULL;
706 static unsigned char convbuf[1024];
707 d0_iobuf_t *conv = NULL;
710 // temps: temp0 order, temp0 4^r
713 USING(schnorr_4_to_s); USING(schnorr_H_4_to_s_signature);
718 out = d0_iobuf_open_write(outbuf, *outbuflen);
724 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
725 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
726 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_4_to_s_signature));
729 // start schnorr ID scheme
730 // generate random number r; x = g^r; send hash of x, remember r, forget x
731 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
732 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
733 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
735 // hash it, hash it, everybody hash it
736 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
737 CHECK(d0_iobuf_write_bignum(conv, temp0));
738 CHECK(d0_iobuf_write_packet(conv, msg, msglen));
739 CHECK(d0_iobuf_write_bignum(conv, temp0));
740 d0_iobuf_close(conv, &sz);
742 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
743 CHECK(d0_iobuf_write_packet(out, msg, msglen));
745 return d0_iobuf_close(out, outbuflen);
748 d0_iobuf_close(out, outbuflen);
752 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)
753 // first run: get 4^s, 4^s signature
756 // 3. send challenge challenge of SCHNORR_BITS
758 d0_iobuf_t *in = NULL;
759 d0_iobuf_t *out = NULL;
760 static unsigned char shabuf[2048];
763 // temps: temp0 order, temp0 signature check
766 REPLACING(schnorr_4_to_s); REPLACING(schnorr_H_4_to_s_signature);
768 REPLACING(schnorr_G);
774 USING(schnorr_4_to_s); USING(schnorr_H_4_to_s_signature);
777 USING(rsa_e); USING(rsa_n);
778 REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r);
780 in = d0_iobuf_open_read(inbuf, inbuflen);
781 out = d0_iobuf_open_write(outbuf, *outbuflen);
787 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
788 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
789 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
791 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
792 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, zero) >= 0);
793 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, ctx->schnorr_G) < 0);
794 CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_4_to_s_signature));
795 CHECK(d0_bignum_cmp(ctx->schnorr_H_4_to_s_signature, zero) >= 0);
796 CHECK(d0_bignum_cmp(ctx->schnorr_H_4_to_s_signature, ctx->rsa_n) < 0);
798 // check signature of key (t = k^d, so, t^challenge = k)
799 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_4_to_s_signature, ctx->rsa_e, ctx->rsa_n));
801 // we will actually sign SHA(4^s) to prevent a malleability attack!
802 CHECK(d0_bignum_mov(temp2, ctx->schnorr_4_to_s));
803 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
804 if(sz > sizeof(shabuf))
806 CHECK(d0_longhash_destructive(temp2, shabuf, sz));
807 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
809 // + 7 / 8 is too large, so let's mod it
810 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
813 if(d0_bignum_cmp(temp0, temp1))
815 // accept the key anyway, but mark as failed signature! will later return 0 in status
816 CHECK(d0_bignum_zero(ctx->schnorr_H_4_to_s_signature));
820 CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
821 ctx->msglen = MSGSIZE;
822 CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
825 CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
827 CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
830 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
831 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
832 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
833 CHECK(d0_iobuf_write_bignum(out, temp0));
836 *status = !!d0_bignum_cmp(ctx->schnorr_H_4_to_s_signature, zero);
838 d0_iobuf_close(in, NULL);
839 return d0_iobuf_close(out, outbuflen);
842 d0_iobuf_close(in, NULL);
843 d0_iobuf_close(out, outbuflen);
847 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)
848 // 1. read challenge challenge of SCHNORR_BITS
849 // 2. reply with r + s * challenge mod order
851 d0_iobuf_t *in = NULL;
852 d0_iobuf_t *out = NULL;
854 // temps: 0 order, 1 prod, 2 y, 3 challenge
855 REPLACING(other_4_to_r);
856 USING(schnorr_G); USING(schnorr_s); USING(r);
858 in = d0_iobuf_open_read(inbuf, inbuflen);
859 out = d0_iobuf_open_write(outbuf, *outbuflen);
861 CHECK(d0_iobuf_read_bignum(in, temp3));
862 CHECK(d0_bignum_cmp(temp3, zero) >= 0);
863 CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
866 CHECK_ASSIGN(ctx->other_4_to_r, d0_iobuf_read_bignum(in, ctx->other_4_to_r));
867 CHECK(d0_bignum_cmp(ctx->other_4_to_r, zero) > 0);
868 CHECK(d0_bignum_cmp(ctx->other_4_to_r, ctx->schnorr_G) < 0);
870 // send response for schnorr ID scheme
871 // i.challenge. r + ctx->schnorr_s * temp3
872 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
873 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
874 CHECK(d0_bignum_mod_add(temp2, temp1, ctx->r, temp0));
875 CHECK(d0_iobuf_write_bignum(out, temp2));
877 d0_iobuf_close(in, NULL);
878 return d0_iobuf_close(out, outbuflen);
881 d0_iobuf_close(in, NULL);
882 d0_iobuf_close(out, outbuflen);
886 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)
887 // 1. read y = r + s * challenge mod order
888 // 2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
889 // (check using H(g^r) which we know)
891 d0_iobuf_t *in = NULL;
892 static unsigned char convbuf[1024];
893 d0_iobuf_t *conv = NULL;
896 // temps: 0 y 1 order
897 USING(challenge); USING(schnorr_G);
898 REPLACING(other_4_to_r);
900 in = d0_iobuf_open_read(inbuf, inbuflen);
902 CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
903 CHECK(d0_iobuf_read_bignum(in, temp0));
904 CHECK(d0_bignum_cmp(temp0, zero) >= 0);
905 CHECK(d0_bignum_cmp(temp0, temp1) < 0);
907 // verify schnorr ID scheme
908 // we need 4^temp0 (g^s)^-challenge
909 CHECK(d0_bignum_neg(temp1, ctx->challenge));
910 CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_4_to_s, temp1, ctx->schnorr_G));
911 CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
912 CHECK_ASSIGN(ctx->other_4_to_r, d0_bignum_mod_mul(ctx->other_4_to_r, temp1, temp2, ctx->schnorr_G));
913 // hash must be equal to msghash
915 // hash it, hash it, everybody hash it
916 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
917 CHECK(d0_iobuf_write_bignum(conv, ctx->other_4_to_r));
918 CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
919 CHECK(d0_iobuf_write_bignum(conv, ctx->other_4_to_r));
920 d0_iobuf_close(conv, &sz);
922 if(memcmp(sha(convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
924 // FAIL (not owned by player)
929 *status = !!d0_bignum_cmp(ctx->schnorr_H_4_to_s_signature, zero);
931 if(ctx->msglen <= *msglen)
932 memcpy(msg, ctx->msg, ctx->msglen);
934 memcpy(msg, ctx->msg, *msglen);
935 *msglen = ctx->msglen;
937 d0_iobuf_close(in, NULL);
941 d0_iobuf_close(in, NULL);
945 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
947 d0_iobuf_t *out = NULL;
948 static unsigned char convbuf[1024];
949 d0_iobuf_t *conv = NULL;
952 USING(schnorr_4_to_s);
954 out = d0_iobuf_open_write(outbuf, *outbuflen);
955 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
957 CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_4_to_s));
958 CHECK(d0_iobuf_close(conv, &sz));
961 n = (*outbuflen / 4) * 3;
962 if(n > SHA_DIGESTSIZE)
964 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
965 CHECK(d0_iobuf_conv_base64_out(out));
967 return d0_iobuf_close(out, outbuflen);
971 d0_iobuf_close(conv, &sz);
972 d0_iobuf_close(out, outbuflen);
976 BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
978 USING(r); USING(other_4_to_r); USING(schnorr_G);
980 // temps: temp0 result
981 CHECK(d0_bignum_mod_pow(temp0, ctx->other_4_to_r, ctx->r, ctx->schnorr_G));
982 return d0_longhash_destructive(temp0, outbuf, *outbuflen);
988 d0_blind_id_t *d0_blind_id_new(void)
990 d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
991 memset(b, 0, sizeof(*b));
995 void d0_blind_id_free(d0_blind_id_t *a)
997 d0_blind_id_clear(a);
1001 void d0_blind_id_util_sha256(char *out, const char *in, size_t n)
1004 SHA256_Init(&context);
1005 SHA256_Update(&context, (const unsigned char *) in, n);
1006 return SHA256_Final((unsigned char *) out, &context);