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"
27 // for zero knowledge, we need multiple instances of schnorr ID scheme... should normally be sequential
28 // parallel schnorr ID is not provably zero knowledge :(
29 // (evil verifier can know all questions in advance, so sequential is disadvantage for him)
30 // we'll just live with a 1:1048576 chance of cheating, and support reauthenticating
32 #define SCHNORR_BITS 20
33 // probability of cheat: 2^(-bits+1)
35 #define SCHNORR_HASHSIZE 3
36 // cannot be >= SHA_DIGEST_LENGTH
37 // *8 must be >= SCHNORR_BITS
39 #define MSGSIZE 640 // ought to be enough for anyone
43 // signing (Xonotic pub and priv key)
44 d0_bignum_t *rsa_n, *rsa_e, *rsa_d;
46 // public data (Schnorr ID)
47 d0_bignum_t *schnorr_G;
49 // private data (player ID private key)
50 d0_bignum_t *schnorr_s;
52 // public data (player ID public key, this is what the server gets to know)
53 d0_bignum_t *schnorr_4_to_s;
54 d0_bignum_t *schnorr_H_4_to_s_signature; // 0 when signature is invalid
55 // as hash function H, we get the SHA1 and reinterpret as bignum - yes, it always is < 160 bits
58 d0_bignum_t *rsa_blind_signature_camouflage; // random number blind signature
60 d0_bignum_t *r; // random number for schnorr ID
61 d0_bignum_t *other_4_to_r; // for DH key exchange
62 d0_bignum_t *challenge; // challenge
64 char msghash[SCHNORR_HASHSIZE]; // init hash
65 char msg[MSGSIZE]; // message
66 size_t msglen; // message length
69 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
70 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
72 #define USING(x) if(!(ctx->x)) return 0
75 static d0_bignum_t *zero, *one, *four, *temp0, *temp1, *temp2, *temp3, *temp4;
77 void d0_blind_id_INITIALIZE(void)
79 d0_bignum_INITIALIZE();
80 CHECK_ASSIGN(zero, d0_bignum_int(zero, 0));
81 CHECK_ASSIGN(one, d0_bignum_int(one, 1));
82 CHECK_ASSIGN(four, d0_bignum_int(four, 4));
83 CHECK_ASSIGN(temp0, d0_bignum_int(temp0, 0));
84 CHECK_ASSIGN(temp1, d0_bignum_int(temp1, 0));
85 CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0));
86 CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0));
87 CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0));
92 void d0_blind_id_SHUTDOWN(void)
97 d0_bignum_free(temp0);
98 d0_bignum_free(temp1);
99 d0_bignum_free(temp2);
100 d0_bignum_free(temp3);
101 d0_bignum_free(temp4);
102 d0_bignum_SHUTDOWN();
106 d0_bignum_t *d0_dl_get_order(d0_bignum_t *o, const d0_bignum_t *G)
108 CHECK_ASSIGN(o, d0_bignum_sub(o, G, one));
109 CHECK(d0_bignum_shl(o, o, -1)); // order o = (G-1)/2
115 d0_bignum_t *d0_dl_get_from_order(d0_bignum_t *G, const d0_bignum_t *o)
117 CHECK_ASSIGN(G, d0_bignum_shl(G, o, 1));
118 CHECK(d0_bignum_add(G, G, one));
124 BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
131 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
132 if(d0_bignum_isprime(temp0, 0) == 0)
134 CHECK(d0_dl_get_from_order(G, temp0));
135 if(d0_bignum_isprime(G, 10) == 0)
137 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
146 BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *challenge, d0_bignum_t *d, d0_bignum_t *n)
148 // uses temp0 to temp4
151 int pb = (size + 1)/2;
159 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
160 if(d0_bignum_isprime(temp0, 10) == 0)
162 CHECK(d0_bignum_sub(temp2, temp0, one));
163 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, challenge));
164 if(!d0_bignum_cmp(temp4, one))
173 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
174 if(!d0_bignum_cmp(temp1, temp0))
180 if(d0_bignum_isprime(temp1, 10) == 0)
182 CHECK(d0_bignum_sub(temp3, temp1, one));
183 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, challenge));
184 if(!d0_bignum_cmp(temp4, one))
192 CHECK(d0_bignum_mul(n, temp0, temp1));
194 // d = challenge^-1 mod (temp0-1)(temp1-1)
195 CHECK(d0_bignum_mul(temp0, temp2, temp3));
196 CHECK(d0_bignum_mod_inv(d, challenge, temp0));
202 BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass)
204 // uses temp0 to temp4
207 int pb = (size + 1)/2;
215 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
216 if(d0_bignum_isprime(temp0, 10) == 0)
218 CHECK(d0_bignum_sub(temp2, temp0, one));
219 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
220 if(!d0_bignum_cmp(temp4, one))
229 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
230 if(!d0_bignum_cmp(temp1, temp0))
238 CHECK(d0_bignum_mul(ctx->rsa_n, temp0, temp1));
239 if(reject(ctx, pass))
242 if(d0_bignum_isprime(temp1, 10) == 0)
244 CHECK(d0_bignum_sub(temp3, temp1, one));
245 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
246 if(!d0_bignum_cmp(temp4, one))
253 // ctx->rsa_d = ctx->rsa_e^-1 mod (temp0-1)(temp1-1)
254 CHECK(d0_bignum_mul(temp0, temp2, temp3));
255 CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp0));
261 void d0_blind_id_clear(d0_blind_id_t *ctx)
263 if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
264 if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
265 if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
266 if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
267 if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
268 if(ctx->schnorr_4_to_s) d0_bignum_free(ctx->schnorr_4_to_s);
269 if(ctx->schnorr_H_4_to_s_signature) d0_bignum_free(ctx->schnorr_H_4_to_s_signature);
270 if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
271 if(ctx->r) d0_bignum_free(ctx->r);
272 if(ctx->challenge) d0_bignum_free(ctx->challenge);
273 if(ctx->other_4_to_r) d0_bignum_free(ctx->other_4_to_r);
274 memset(ctx, 0, sizeof(*ctx));
277 WARN_UNUSED_RESULT BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
279 d0_blind_id_clear(ctx);
280 if(src->rsa_n) CHECK_ASSIGN(ctx->rsa_n, d0_bignum_mov(NULL, src->rsa_n));
281 if(src->rsa_e) CHECK_ASSIGN(ctx->rsa_e, d0_bignum_mov(NULL, src->rsa_e));
282 if(src->rsa_d) CHECK_ASSIGN(ctx->rsa_d, d0_bignum_mov(NULL, src->rsa_d));
283 if(src->schnorr_G) CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_mov(NULL, src->schnorr_G));
284 if(src->schnorr_s) CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_mov(NULL, src->schnorr_s));
285 if(src->schnorr_4_to_s) CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_bignum_mov(NULL, src->schnorr_4_to_s));
286 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));
287 if(src->rsa_blind_signature_camouflage) CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage));
288 if(src->r) CHECK_ASSIGN(ctx->r, d0_bignum_mov(NULL, src->r));
289 if(src->challenge) CHECK_ASSIGN(ctx->challenge, d0_bignum_mov(NULL, src->challenge));
290 if(src->other_4_to_r) CHECK_ASSIGN(ctx->other_4_to_r, d0_bignum_mov(NULL, src->other_4_to_r));
291 memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
292 ctx->msglen = src->msglen;
293 memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
296 d0_blind_id_clear(ctx);
300 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key_fastreject(d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass)
302 REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
304 CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
305 CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
306 CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
308 CHECK(d0_rsa_generate_key_fastreject(k+1, reject, ctx, pass)); // must fit G for sure
310 CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
316 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
318 return d0_blind_id_generate_private_key_fastreject(ctx, k, NULL, NULL);
321 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
323 d0_iobuf_t *in = NULL;
325 REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
327 in = d0_iobuf_open_read(inbuf, inbuflen);
329 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
330 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
331 CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
332 return d0_iobuf_close(in, NULL);
335 d0_iobuf_close(in, NULL);
339 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
341 d0_iobuf_t *in = NULL;
343 REPLACING(rsa_n); REPLACING(rsa_e);
345 in = d0_iobuf_open_read(inbuf, inbuflen);
346 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
347 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
348 return d0_iobuf_close(in, NULL);
351 d0_iobuf_close(in, NULL);
355 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
357 d0_iobuf_t *out = NULL;
359 USING(rsa_n); USING(rsa_e); USING(rsa_d);
361 out = d0_iobuf_open_write(outbuf, *outbuflen);
362 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
363 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
364 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
365 return d0_iobuf_close(out, outbuflen);
368 d0_iobuf_close(out, outbuflen);
372 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
374 d0_iobuf_t *out = NULL;
376 USING(rsa_n); USING(rsa_e);
378 out = d0_iobuf_open_write(outbuf, *outbuflen);
379 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
380 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
381 return d0_iobuf_close(out, outbuflen);
384 if(!d0_iobuf_close(out, outbuflen))
389 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
391 d0_iobuf_t *out = NULL;
392 static unsigned char convbuf[2048];
393 d0_iobuf_t *conv = NULL;
396 USING(rsa_n); USING(rsa_e);
398 out = d0_iobuf_open_write(outbuf, *outbuflen);
399 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
401 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
402 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
403 CHECK(d0_iobuf_close(conv, &sz));
406 n = (*outbuflen / 4) * 3;
407 if(n > SHA_DIGESTSIZE)
409 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
410 CHECK(d0_iobuf_conv_base64_out(out));
412 return d0_iobuf_close(out, outbuflen);
416 d0_iobuf_close(conv, &sz);
417 d0_iobuf_close(out, outbuflen);
421 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
424 REPLACING(schnorr_G);
426 CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
427 CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
433 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
435 d0_iobuf_t *in = NULL;
437 REPLACING(schnorr_G);
439 in = d0_iobuf_open_read(inbuf, inbuflen);
440 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
441 return d0_iobuf_close(in, NULL);
444 d0_iobuf_close(in, NULL);
448 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
450 d0_iobuf_t *out = NULL;
454 out = d0_iobuf_open_write(outbuf, *outbuflen);
455 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
456 return d0_iobuf_close(out, outbuflen);
459 d0_iobuf_close(out, outbuflen);
463 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
465 // temps: temp0 = order
467 REPLACING(schnorr_s); REPLACING(schnorr_4_to_s);
469 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
470 CHECK(d0_bignum_shl(temp1, ctx->schnorr_G, -1));
471 CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
472 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_bignum_mod_pow(ctx->schnorr_4_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
473 CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_bignum_zero(ctx->schnorr_H_4_to_s_signature));
480 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
482 d0_iobuf_t *out = NULL;
483 static unsigned char convbuf[2048];
486 // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
487 USING(rsa_n); USING(rsa_e); USING(schnorr_4_to_s);
488 REPLACING(rsa_blind_signature_camouflage);
490 out = d0_iobuf_open_write(outbuf, *outbuflen);
492 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
493 CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
494 // we will actually sign SHA(4^s) to prevent a malleability attack!
495 sz = (d0_bignum_size(ctx->schnorr_4_to_s) + 7) / 8;
496 CHECK(d0_bignum_export_unsigned(ctx->schnorr_4_to_s, convbuf, sz) >= 0);
497 CHECK(d0_bignum_import_unsigned(temp2, sha(convbuf, sz), SHA_DIGESTSIZE));
499 CHECK(d0_bignum_mod_mul(temp1, temp2, temp0, ctx->rsa_n));
500 CHECK(d0_iobuf_write_bignum(out, temp1));
501 return d0_iobuf_close(out, outbuflen);
504 d0_iobuf_close(out, outbuflen);
508 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)
510 d0_iobuf_t *in = NULL;
511 d0_iobuf_t *out = NULL;
513 // temps: temp0 input, temp1 temp0^d
514 USING(rsa_d); USING(rsa_n);
516 in = d0_iobuf_open_read(inbuf, inbuflen);
517 out = d0_iobuf_open_write(outbuf, *outbuflen);
519 CHECK(d0_iobuf_read_bignum(in, temp0));
520 CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
521 CHECK(d0_iobuf_write_bignum(out, temp1));
523 d0_iobuf_close(in, NULL);
524 return d0_iobuf_close(out, outbuflen);
527 d0_iobuf_close(in, NULL);
528 d0_iobuf_close(out, outbuflen);
532 WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
534 d0_iobuf_t *in = NULL;
536 // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
537 USING(rsa_blind_signature_camouflage); USING(rsa_n);
538 REPLACING(schnorr_H_4_to_s_signature);
540 in = d0_iobuf_open_read(inbuf, inbuflen);
542 CHECK(d0_iobuf_read_bignum(in, temp0));
543 CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
544 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));
546 return d0_iobuf_close(in, NULL);
549 d0_iobuf_close(in, NULL);
553 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_request_camouflage(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
555 d0_iobuf_t *in = NULL;
557 REPLACING(rsa_blind_signature_camouflage);
559 in = d0_iobuf_open_read(inbuf, inbuflen);
561 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_iobuf_read_bignum(in, ctx->rsa_blind_signature_camouflage));
563 return d0_iobuf_close(in, NULL);
566 d0_iobuf_close(in, NULL);
570 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_request_camouflage(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
572 d0_iobuf_t *out = NULL;
574 USING(rsa_blind_signature_camouflage);
576 out = d0_iobuf_open_write(outbuf, *outbuflen);
578 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_blind_signature_camouflage));
580 return d0_iobuf_close(out, outbuflen);
583 d0_iobuf_close(out, outbuflen);
587 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
589 d0_iobuf_t *in = NULL;
591 REPLACING(schnorr_s); REPLACING(schnorr_4_to_s); REPLACING(schnorr_H_4_to_s_signature);
593 in = d0_iobuf_open_read(inbuf, inbuflen);
595 CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
596 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
597 CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_4_to_s_signature));
599 return d0_iobuf_close(in, NULL);
602 d0_iobuf_close(in, NULL);
606 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
608 d0_iobuf_t *in = NULL;
610 REPLACING(schnorr_4_to_s); REPLACING(schnorr_H_4_to_s_signature);
612 in = d0_iobuf_open_read(inbuf, inbuflen);
614 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
615 CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_4_to_s_signature));
617 return d0_iobuf_close(in, NULL);
620 d0_iobuf_close(in, NULL);
624 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
626 d0_iobuf_t *out = NULL;
628 USING(schnorr_s); USING(schnorr_4_to_s); USING(schnorr_H_4_to_s_signature);
630 out = d0_iobuf_open_write(outbuf, *outbuflen);
632 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
633 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
634 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_4_to_s_signature));
636 return d0_iobuf_close(out, outbuflen);
639 d0_iobuf_close(out, outbuflen);
643 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
645 d0_iobuf_t *out = NULL;
647 USING(schnorr_4_to_s); USING(schnorr_H_4_to_s_signature);
649 out = d0_iobuf_open_write(outbuf, *outbuflen);
651 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
652 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_4_to_s_signature));
654 return d0_iobuf_close(out, outbuflen);
657 d0_iobuf_close(out, outbuflen);
661 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)
663 // first run: send 4^s, 4^s signature
664 // 1. get random r, send HASH(4^r)
666 d0_iobuf_t *out = NULL;
667 static unsigned char convbuf[1024];
668 d0_iobuf_t *conv = NULL;
671 // temps: temp0 order, temp0 4^r
674 USING(schnorr_4_to_s); USING(schnorr_H_4_to_s_signature);
679 out = d0_iobuf_open_write(outbuf, *outbuflen);
685 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
686 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
687 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_4_to_s_signature));
690 // start schnorr ID scheme
691 // generate random number r; x = g^r; send hash of x, remember r, forget x
692 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
693 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
694 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
696 // hash it, hash it, everybody hash it
697 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
698 CHECK(d0_iobuf_write_bignum(conv, temp0));
699 CHECK(d0_iobuf_write_packet(conv, msg, msglen));
700 CHECK(d0_iobuf_write_bignum(conv, temp0));
701 d0_iobuf_close(conv, &sz);
703 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
704 CHECK(d0_iobuf_write_packet(out, msg, msglen));
706 return d0_iobuf_close(out, outbuflen);
709 d0_iobuf_close(out, outbuflen);
713 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)
714 // first run: get 4^s, 4^s signature
717 // 3. send challenge challenge of SCHNORR_BITS
719 d0_iobuf_t *in = NULL;
720 d0_iobuf_t *out = NULL;
721 static unsigned char convbuf[2048];
724 // temps: temp0 order, temp0 signature check
727 REPLACING(schnorr_4_to_s); REPLACING(schnorr_H_4_to_s_signature);
729 REPLACING(schnorr_G);
735 USING(schnorr_4_to_s); USING(schnorr_H_4_to_s_signature);
738 USING(rsa_e); USING(rsa_n);
739 REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r);
741 in = d0_iobuf_open_read(inbuf, inbuflen);
742 out = d0_iobuf_open_write(outbuf, *outbuflen);
748 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
749 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
750 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
752 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
753 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, zero) >= 0);
754 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, ctx->schnorr_G) < 0);
755 CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_4_to_s_signature));
756 CHECK(d0_bignum_cmp(ctx->schnorr_H_4_to_s_signature, zero) >= 0);
757 CHECK(d0_bignum_cmp(ctx->schnorr_H_4_to_s_signature, ctx->rsa_n) < 0);
759 // check signature of key (t = k^d, so, t^challenge = k)
760 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_4_to_s_signature, ctx->rsa_e, ctx->rsa_n));
761 // we will actually sign SHA(4^s) to prevent a malleability attack!
762 sz = (d0_bignum_size(ctx->schnorr_4_to_s) + 7) / 8;
763 CHECK(d0_bignum_export_unsigned(ctx->schnorr_4_to_s, convbuf, sz) >= 0);
764 CHECK(d0_bignum_import_unsigned(temp2, sha(convbuf, sz), SHA_DIGESTSIZE));
765 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
767 if(d0_bignum_cmp(temp0, temp1))
769 // accept the key anyway, but mark as failed signature! will later return 0 in status
770 CHECK(d0_bignum_zero(ctx->schnorr_H_4_to_s_signature));
774 CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
775 ctx->msglen = MSGSIZE;
776 CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
779 CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
781 CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
784 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
785 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
786 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
787 CHECK(d0_iobuf_write_bignum(out, temp0));
790 *status = !!d0_bignum_cmp(ctx->schnorr_H_4_to_s_signature, zero);
792 d0_iobuf_close(in, NULL);
793 return d0_iobuf_close(out, outbuflen);
796 d0_iobuf_close(in, NULL);
797 d0_iobuf_close(out, outbuflen);
801 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)
802 // 1. read challenge challenge of SCHNORR_BITS
803 // 2. reply with r + s * challenge mod order
805 d0_iobuf_t *in = NULL;
806 d0_iobuf_t *out = NULL;
808 // temps: 0 order, 1 prod, 2 y, 3 challenge
809 REPLACING(other_4_to_r);
810 USING(schnorr_G); USING(schnorr_s); USING(r);
812 in = d0_iobuf_open_read(inbuf, inbuflen);
813 out = d0_iobuf_open_write(outbuf, *outbuflen);
815 CHECK(d0_iobuf_read_bignum(in, temp3));
816 CHECK(d0_bignum_cmp(temp3, zero) >= 0);
817 CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
820 CHECK_ASSIGN(ctx->other_4_to_r, d0_iobuf_read_bignum(in, ctx->other_4_to_r));
821 CHECK(d0_bignum_cmp(ctx->other_4_to_r, zero) > 0);
822 CHECK(d0_bignum_cmp(ctx->other_4_to_r, ctx->schnorr_G) < 0);
824 // send response for schnorr ID scheme
825 // i.challenge. r + ctx->schnorr_s * temp3
826 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
827 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
828 CHECK(d0_bignum_mod_add(temp2, temp1, ctx->r, temp0));
829 CHECK(d0_iobuf_write_bignum(out, temp2));
831 d0_iobuf_close(in, NULL);
832 return d0_iobuf_close(out, outbuflen);
835 d0_iobuf_close(in, NULL);
836 d0_iobuf_close(out, outbuflen);
840 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)
841 // 1. read y = r + s * challenge mod order
842 // 2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
843 // (check using H(g^r) which we know)
845 d0_iobuf_t *in = NULL;
846 static unsigned char convbuf[1024];
847 d0_iobuf_t *conv = NULL;
850 // temps: 0 y 1 order
851 USING(challenge); USING(schnorr_G);
852 REPLACING(other_4_to_r);
854 in = d0_iobuf_open_read(inbuf, inbuflen);
856 CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
857 CHECK(d0_iobuf_read_bignum(in, temp0));
858 CHECK(d0_bignum_cmp(temp0, zero) >= 0);
859 CHECK(d0_bignum_cmp(temp0, temp1) < 0);
861 // verify schnorr ID scheme
862 // we need 4^temp0 (g^s)^-challenge
863 CHECK(d0_bignum_neg(temp1, ctx->challenge));
864 CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_4_to_s, temp1, ctx->schnorr_G));
865 CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
866 CHECK_ASSIGN(ctx->other_4_to_r, d0_bignum_mod_mul(ctx->other_4_to_r, temp1, temp2, ctx->schnorr_G));
867 // hash must be equal to msghash
869 // hash it, hash it, everybody hash it
870 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
871 CHECK(d0_iobuf_write_bignum(conv, ctx->other_4_to_r));
872 CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
873 CHECK(d0_iobuf_write_bignum(conv, ctx->other_4_to_r));
874 d0_iobuf_close(conv, &sz);
876 if(memcmp(sha(convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
878 // FAIL (not owned by player)
883 *status = !!d0_bignum_cmp(ctx->schnorr_H_4_to_s_signature, zero);
885 if(ctx->msglen <= *msglen)
886 memcpy(msg, ctx->msg, ctx->msglen);
888 memcpy(msg, ctx->msg, *msglen);
889 *msglen = ctx->msglen;
891 d0_iobuf_close(in, NULL);
895 d0_iobuf_close(in, NULL);
899 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
901 d0_iobuf_t *out = NULL;
902 static unsigned char convbuf[1024];
903 d0_iobuf_t *conv = NULL;
906 USING(schnorr_4_to_s);
908 out = d0_iobuf_open_write(outbuf, *outbuflen);
909 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
911 CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_4_to_s));
912 CHECK(d0_iobuf_close(conv, &sz));
915 n = (*outbuflen / 4) * 3;
916 if(n > SHA_DIGESTSIZE)
918 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
919 CHECK(d0_iobuf_conv_base64_out(out));
921 return d0_iobuf_close(out, outbuflen);
925 d0_iobuf_close(conv, &sz);
926 d0_iobuf_close(out, outbuflen);
930 BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
932 d0_iobuf_t *out = NULL;
933 static unsigned char convbuf[1024];
934 d0_iobuf_t *conv = NULL;
937 USING(r); USING(other_4_to_r); USING(schnorr_G);
939 out = d0_iobuf_open_write(outbuf, *outbuflen);
940 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
942 // temps: temp0 result
943 CHECK(d0_bignum_mod_pow(temp0, ctx->other_4_to_r, ctx->r, ctx->schnorr_G));
944 CHECK(d0_iobuf_write_bignum(conv, temp0));
945 CHECK(d0_iobuf_close(conv, &sz));
949 if(n > SHA_DIGESTSIZE)
951 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
953 return d0_iobuf_close(out, outbuflen);
957 d0_iobuf_close(conv, &sz);
958 d0_iobuf_close(out, outbuflen);
962 d0_blind_id_t *d0_blind_id_new(void)
964 d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
965 memset(b, 0, sizeof(*b));
969 void d0_blind_id_free(d0_blind_id_t *a)
971 d0_blind_id_clear(a);