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_4_to_s_signature; // 0 when signature is invalid
57 d0_bignum_t *rsa_blind_signature_camouflage; // random number blind signature
59 d0_bignum_t *r; // random number for schnorr ID
60 d0_bignum_t *other_4_to_r; // for DH key exchange
61 d0_bignum_t *challenge; // challenge
63 char msghash[SCHNORR_HASHSIZE]; // init hash
64 char msg[MSGSIZE]; // message
65 size_t msglen; // message length
68 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
69 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
71 #define USING(x) if(!(ctx->x)) return 0
74 static d0_bignum_t *zero, *one, *four, *temp0, *temp1, *temp2, *temp3, *temp4;
76 void d0_blind_id_INITIALIZE(void)
78 d0_bignum_INITIALIZE();
79 CHECK_ASSIGN(zero, d0_bignum_int(zero, 0));
80 CHECK_ASSIGN(one, d0_bignum_int(one, 1));
81 CHECK_ASSIGN(four, d0_bignum_int(four, 4));
82 CHECK_ASSIGN(temp0, d0_bignum_int(temp0, 0));
83 CHECK_ASSIGN(temp1, d0_bignum_int(temp1, 0));
84 CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0));
85 CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0));
86 CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0));
91 void d0_blind_id_SHUTDOWN(void)
96 d0_bignum_free(temp0);
97 d0_bignum_free(temp1);
98 d0_bignum_free(temp2);
99 d0_bignum_free(temp3);
100 d0_bignum_free(temp4);
101 d0_bignum_SHUTDOWN();
105 d0_bignum_t *d0_dl_get_order(d0_bignum_t *o, const d0_bignum_t *G)
107 CHECK_ASSIGN(o, d0_bignum_sub(o, G, one));
108 CHECK(d0_bignum_shl(o, o, -1)); // order o = (G-1)/2
114 d0_bignum_t *d0_dl_get_from_order(d0_bignum_t *G, const d0_bignum_t *o)
116 CHECK_ASSIGN(G, d0_bignum_shl(G, o, 1));
117 CHECK(d0_bignum_add(G, G, one));
123 BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
130 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
131 if(d0_bignum_isprime(temp0, 0) == 0)
133 CHECK(d0_dl_get_from_order(G, temp0));
134 if(d0_bignum_isprime(G, 10) == 0)
136 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
145 BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *challenge, d0_bignum_t *d, d0_bignum_t *n)
147 // uses temp0 to temp4
150 int pb = (size + 1)/2;
158 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
159 if(d0_bignum_isprime(temp0, 10) == 0)
161 CHECK(d0_bignum_sub(temp2, temp0, one));
162 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, challenge));
163 if(!d0_bignum_cmp(temp4, one))
172 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
173 if(!d0_bignum_cmp(temp1, temp0))
179 if(d0_bignum_isprime(temp1, 10) == 0)
181 CHECK(d0_bignum_sub(temp3, temp1, one));
182 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, challenge));
183 if(!d0_bignum_cmp(temp4, one))
191 CHECK(d0_bignum_mul(n, temp0, temp1));
193 // d = challenge^-1 mod (temp0-1)(temp1-1)
194 CHECK(d0_bignum_mul(temp0, temp2, temp3));
195 CHECK(d0_bignum_mod_inv(d, challenge, temp0));
201 BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass)
203 // uses temp0 to temp4
206 int pb = (size + 1)/2;
214 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
215 if(d0_bignum_isprime(temp0, 10) == 0)
217 CHECK(d0_bignum_sub(temp2, temp0, one));
218 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
219 if(!d0_bignum_cmp(temp4, one))
228 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
229 if(!d0_bignum_cmp(temp1, temp0))
237 CHECK(d0_bignum_mul(ctx->rsa_n, temp0, temp1));
238 if(reject(ctx, pass))
241 if(d0_bignum_isprime(temp1, 10) == 0)
243 CHECK(d0_bignum_sub(temp3, temp1, one));
244 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
245 if(!d0_bignum_cmp(temp4, one))
252 // ctx->rsa_d = ctx->rsa_e^-1 mod (temp0-1)(temp1-1)
253 CHECK(d0_bignum_mul(temp0, temp2, temp3));
254 CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp0));
260 void d0_blind_id_clear(d0_blind_id_t *ctx)
262 if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
263 if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
264 if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
265 if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
266 if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
267 if(ctx->schnorr_4_to_s) d0_bignum_free(ctx->schnorr_4_to_s);
268 if(ctx->schnorr_4_to_s_signature) d0_bignum_free(ctx->schnorr_4_to_s_signature);
269 if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
270 if(ctx->r) d0_bignum_free(ctx->r);
271 if(ctx->challenge) d0_bignum_free(ctx->challenge);
272 if(ctx->other_4_to_r) d0_bignum_free(ctx->other_4_to_r);
273 memset(ctx, 0, sizeof(*ctx));
276 void d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
278 d0_blind_id_clear(ctx);
279 if(src->rsa_n) ctx->rsa_n = d0_bignum_mov(NULL, src->rsa_n);
280 if(src->rsa_e) ctx->rsa_e = d0_bignum_mov(NULL, src->rsa_e);
281 if(src->rsa_d) ctx->rsa_d = d0_bignum_mov(NULL, src->rsa_d);
282 if(src->schnorr_G) ctx->schnorr_G = d0_bignum_mov(NULL, src->schnorr_G);
283 if(src->schnorr_s) ctx->schnorr_s = d0_bignum_mov(NULL, src->schnorr_s);
284 if(src->schnorr_4_to_s) ctx->schnorr_4_to_s = d0_bignum_mov(NULL, ctx->schnorr_G);
285 if(src->schnorr_4_to_s_signature) ctx->schnorr_4_to_s_signature = d0_bignum_mov(NULL, src->schnorr_4_to_s_signature);
286 if(src->rsa_blind_signature_camouflage) ctx->rsa_blind_signature_camouflage = d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage);
287 if(src->r) ctx->r = d0_bignum_mov(NULL, src->r);
288 if(src->challenge) ctx->challenge = d0_bignum_mov(NULL, src->challenge);
289 if(src->other_4_to_r) ctx->other_4_to_r = d0_bignum_mov(NULL, src->other_4_to_r);
290 memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
291 ctx->msglen = src->msglen;
292 memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
295 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key_fastreject(d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass)
297 REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
299 CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
300 CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
301 CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
303 CHECK(d0_rsa_generate_key_fastreject(k+1, reject, ctx, pass)); // must fit G for sure
305 CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
311 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
313 return d0_blind_id_generate_private_key_fastreject(ctx, k, NULL, NULL);
316 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
318 d0_iobuf_t *in = NULL;
320 REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
322 in = d0_iobuf_open_read(inbuf, inbuflen);
324 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
325 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
326 CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
327 return d0_iobuf_close(in, NULL);
330 d0_iobuf_close(in, NULL);
334 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
336 d0_iobuf_t *in = NULL;
338 REPLACING(rsa_n); REPLACING(rsa_e);
340 in = d0_iobuf_open_read(inbuf, inbuflen);
341 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
342 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
343 return d0_iobuf_close(in, NULL);
346 d0_iobuf_close(in, NULL);
350 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
352 d0_iobuf_t *out = NULL;
354 USING(rsa_n); USING(rsa_e); USING(rsa_d);
356 out = d0_iobuf_open_write(outbuf, *outbuflen);
357 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
358 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
359 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
360 return d0_iobuf_close(out, outbuflen);
363 d0_iobuf_close(out, outbuflen);
367 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
369 d0_iobuf_t *out = NULL;
371 USING(rsa_n); USING(rsa_e);
373 out = d0_iobuf_open_write(outbuf, *outbuflen);
374 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
375 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
376 return d0_iobuf_close(out, outbuflen);
379 if(!d0_iobuf_close(out, outbuflen))
384 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
386 d0_iobuf_t *out = NULL;
387 static unsigned char convbuf[2048];
388 d0_iobuf_t *conv = NULL;
391 USING(rsa_n); USING(rsa_e);
393 out = d0_iobuf_open_write(outbuf, *outbuflen);
394 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
396 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
397 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
398 CHECK(d0_iobuf_close(conv, &sz));
401 n = (*outbuflen / 4) * 3;
402 if(n > SHA_DIGESTSIZE)
404 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
405 CHECK(d0_iobuf_conv_base64_out(out));
407 return d0_iobuf_close(out, outbuflen);
411 d0_iobuf_close(conv, &sz);
412 d0_iobuf_close(out, outbuflen);
416 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
419 REPLACING(schnorr_G);
421 CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
422 CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
428 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
430 d0_iobuf_t *in = NULL;
432 REPLACING(schnorr_G);
434 in = d0_iobuf_open_read(inbuf, inbuflen);
435 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
436 return d0_iobuf_close(in, NULL);
439 d0_iobuf_close(in, NULL);
443 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
445 d0_iobuf_t *out = NULL;
449 out = d0_iobuf_open_write(outbuf, *outbuflen);
450 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
451 return d0_iobuf_close(out, outbuflen);
454 d0_iobuf_close(out, outbuflen);
458 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
460 // temps: temp0 = order
462 REPLACING(schnorr_s); REPLACING(schnorr_4_to_s);
464 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
465 CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
466 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_bignum_mod_pow(ctx->schnorr_4_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
467 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_zero(ctx->schnorr_4_to_s_signature));
474 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
476 d0_iobuf_t *out = NULL;
478 // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
479 USING(rsa_n); USING(rsa_e); USING(schnorr_4_to_s);
480 REPLACING(rsa_blind_signature_camouflage);
482 out = d0_iobuf_open_write(outbuf, *outbuflen);
484 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
485 CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
486 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_4_to_s, temp0, ctx->rsa_n));
487 CHECK(d0_iobuf_write_bignum(out, temp1));
488 return d0_iobuf_close(out, outbuflen);
491 d0_iobuf_close(out, outbuflen);
495 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)
497 d0_iobuf_t *in = NULL;
498 d0_iobuf_t *out = NULL;
500 // temps: temp0 input, temp1 temp0^d
501 USING(rsa_d); USING(rsa_n);
503 in = d0_iobuf_open_read(inbuf, inbuflen);
504 out = d0_iobuf_open_write(outbuf, *outbuflen);
506 CHECK(d0_iobuf_read_bignum(in, temp0));
507 CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
508 CHECK(d0_iobuf_write_bignum(out, temp1));
510 d0_iobuf_close(in, NULL);
511 return d0_iobuf_close(out, outbuflen);
514 d0_iobuf_close(in, NULL);
515 d0_iobuf_close(out, outbuflen);
519 WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
521 d0_iobuf_t *in = NULL;
523 // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
524 USING(rsa_blind_signature_camouflage); USING(rsa_n);
525 REPLACING(schnorr_4_to_s_signature);
527 in = d0_iobuf_open_read(inbuf, inbuflen);
529 CHECK(d0_iobuf_read_bignum(in, temp0));
530 CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
531 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_4_to_s_signature, temp0, temp1, ctx->rsa_n));
533 return d0_iobuf_close(in, NULL);
536 d0_iobuf_close(in, NULL);
540 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_request_camouflage(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
542 d0_iobuf_t *in = NULL;
544 REPLACING(rsa_blind_signature_camouflage);
546 in = d0_iobuf_open_read(inbuf, inbuflen);
548 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_iobuf_read_bignum(in, ctx->rsa_blind_signature_camouflage));
550 return d0_iobuf_close(in, NULL);
553 d0_iobuf_close(in, NULL);
557 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_request_camouflage(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
559 d0_iobuf_t *out = NULL;
561 USING(rsa_blind_signature_camouflage);
563 out = d0_iobuf_open_write(outbuf, *outbuflen);
565 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_blind_signature_camouflage));
567 return d0_iobuf_close(out, outbuflen);
570 d0_iobuf_close(out, outbuflen);
574 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
576 d0_iobuf_t *in = NULL;
578 REPLACING(schnorr_s); REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
580 in = d0_iobuf_open_read(inbuf, inbuflen);
582 CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
583 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
584 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
586 return d0_iobuf_close(in, NULL);
589 d0_iobuf_close(in, NULL);
593 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
595 d0_iobuf_t *in = NULL;
597 REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
599 in = d0_iobuf_open_read(inbuf, inbuflen);
601 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
602 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
604 return d0_iobuf_close(in, NULL);
607 d0_iobuf_close(in, NULL);
611 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
613 d0_iobuf_t *out = NULL;
615 USING(schnorr_s); USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
617 out = d0_iobuf_open_write(outbuf, *outbuflen);
619 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
620 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
621 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
623 return d0_iobuf_close(out, outbuflen);
626 d0_iobuf_close(out, outbuflen);
630 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
632 d0_iobuf_t *out = NULL;
634 USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
636 out = d0_iobuf_open_write(outbuf, *outbuflen);
638 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
639 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
641 return d0_iobuf_close(out, outbuflen);
644 d0_iobuf_close(out, outbuflen);
648 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)
650 // first run: send 4^s, 4^s signature
651 // 1. get random r, send HASH(4^r)
653 d0_iobuf_t *out = NULL;
654 static unsigned char convbuf[1024];
655 d0_iobuf_t *conv = NULL;
658 // temps: temp0 order, temp0 4^r
661 USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
666 out = d0_iobuf_open_write(outbuf, *outbuflen);
672 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
673 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
674 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
677 // start schnorr ID scheme
678 // generate random number r; x = g^r; send hash of x, remember r, forget x
679 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
680 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
681 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
683 // hash it, hash it, everybody hash it
684 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
685 CHECK(d0_iobuf_write_bignum(conv, temp0));
686 CHECK(d0_iobuf_write_packet(conv, msg, msglen));
687 CHECK(d0_iobuf_write_bignum(conv, temp0));
688 d0_iobuf_close(conv, &sz);
690 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
691 CHECK(d0_iobuf_write_packet(out, msg, msglen));
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_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)
701 // first run: get 4^s, 4^s signature
704 // 3. send challenge challenge of SCHNORR_BITS
706 d0_iobuf_t *in = NULL;
707 d0_iobuf_t *out = NULL;
709 // temps: temp0 order, temp0 signature check
712 REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
714 REPLACING(schnorr_G);
720 USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
723 USING(rsa_e); USING(rsa_n);
724 REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r);
726 in = d0_iobuf_open_read(inbuf, inbuflen);
727 out = d0_iobuf_open_write(outbuf, *outbuflen);
733 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
734 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
735 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
737 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
738 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, zero) > 0);
739 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, ctx->schnorr_G) < 0);
740 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
741 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero) >= 0);
742 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, ctx->rsa_n) < 0);
744 // check signature of key (t = k^d, so, t^challenge = k)
745 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_4_to_s_signature, ctx->rsa_e, ctx->rsa_n));
746 if(d0_bignum_cmp(temp0, ctx->schnorr_4_to_s))
748 // accept the key anyway, but mark as failed signature! will later return 0 in status
749 CHECK(d0_bignum_zero(ctx->schnorr_4_to_s_signature));
753 CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
754 ctx->msglen = MSGSIZE;
755 CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
758 CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
760 CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
763 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
764 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
765 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
766 CHECK(d0_iobuf_write_bignum(out, temp0));
769 *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
771 d0_iobuf_close(in, NULL);
772 return d0_iobuf_close(out, outbuflen);
775 d0_iobuf_close(in, NULL);
776 d0_iobuf_close(out, outbuflen);
780 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)
781 // 1. read challenge challenge of SCHNORR_BITS
782 // 2. reply with r + s * challenge mod order
784 d0_iobuf_t *in = NULL;
785 d0_iobuf_t *out = NULL;
787 // temps: 0 order, 1 prod, 2 y, 3 challenge
788 REPLACING(other_4_to_r);
789 USING(schnorr_G); USING(schnorr_s); USING(r);
791 in = d0_iobuf_open_read(inbuf, inbuflen);
792 out = d0_iobuf_open_write(outbuf, *outbuflen);
794 CHECK(d0_iobuf_read_bignum(in, temp3));
795 CHECK(d0_bignum_cmp(temp3, zero) >= 0);
796 CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
799 CHECK_ASSIGN(ctx->other_4_to_r, d0_iobuf_read_bignum(in, ctx->other_4_to_r));
800 CHECK(d0_bignum_cmp(ctx->other_4_to_r, zero) > 0);
801 CHECK(d0_bignum_cmp(ctx->other_4_to_r, ctx->schnorr_G) < 0);
803 // send response for schnorr ID scheme
804 // i.challenge. r + ctx->schnorr_s * temp3
805 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
806 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
807 CHECK(d0_bignum_mod_add(temp2, temp1, ctx->r, temp0));
808 CHECK(d0_iobuf_write_bignum(out, temp2));
810 d0_iobuf_close(in, NULL);
811 return d0_iobuf_close(out, outbuflen);
814 d0_iobuf_close(in, NULL);
815 d0_iobuf_close(out, outbuflen);
819 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)
820 // 1. read y = r + s * challenge mod order
821 // 2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
822 // (check using H(g^r) which we know)
824 d0_iobuf_t *in = NULL;
825 static unsigned char convbuf[1024];
826 d0_iobuf_t *conv = NULL;
829 // temps: 0 y 1 order
830 USING(challenge); USING(schnorr_G);
831 REPLACING(other_4_to_r);
833 in = d0_iobuf_open_read(inbuf, inbuflen);
835 CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
836 CHECK(d0_iobuf_read_bignum(in, temp0));
837 CHECK(d0_bignum_cmp(temp0, zero) >= 0);
838 CHECK(d0_bignum_cmp(temp0, temp1) < 0);
840 // verify schnorr ID scheme
841 // we need 4^temp0 (g^s)^-challenge
842 CHECK(d0_bignum_neg(temp1, ctx->challenge));
843 CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_4_to_s, temp1, ctx->schnorr_G));
844 CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
845 CHECK_ASSIGN(ctx->other_4_to_r, d0_bignum_mod_mul(ctx->other_4_to_r, temp1, temp2, ctx->schnorr_G));
846 // hash must be equal to msghash
848 // hash it, hash it, everybody hash it
849 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
850 CHECK(d0_iobuf_write_bignum(conv, ctx->other_4_to_r));
851 CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
852 CHECK(d0_iobuf_write_bignum(conv, ctx->other_4_to_r));
853 d0_iobuf_close(conv, &sz);
855 if(memcmp(sha(convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
857 // FAIL (not owned by player)
862 *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
864 if(ctx->msglen <= *msglen)
865 memcpy(msg, ctx->msg, ctx->msglen);
867 memcpy(msg, ctx->msg, *msglen);
868 *msglen = ctx->msglen;
870 d0_iobuf_close(in, NULL);
874 d0_iobuf_close(in, NULL);
878 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
880 d0_iobuf_t *out = NULL;
881 static unsigned char convbuf[1024];
882 d0_iobuf_t *conv = NULL;
885 USING(schnorr_4_to_s);
887 out = d0_iobuf_open_write(outbuf, *outbuflen);
888 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
890 CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_4_to_s));
891 CHECK(d0_iobuf_close(conv, &sz));
894 n = (*outbuflen / 4) * 3;
895 if(n > SHA_DIGESTSIZE)
897 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
898 CHECK(d0_iobuf_conv_base64_out(out));
900 return d0_iobuf_close(out, outbuflen);
904 d0_iobuf_close(conv, &sz);
905 d0_iobuf_close(out, outbuflen);
909 BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
911 d0_iobuf_t *out = NULL;
912 static unsigned char convbuf[1024];
913 d0_iobuf_t *conv = NULL;
916 USING(r); USING(other_4_to_r); USING(schnorr_G);
918 out = d0_iobuf_open_write(outbuf, *outbuflen);
919 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
921 // temps: temp0 result
922 CHECK(d0_bignum_mod_pow(temp0, ctx->other_4_to_r, ctx->r, ctx->schnorr_G));
923 CHECK(d0_iobuf_write_bignum(conv, temp0));
924 CHECK(d0_iobuf_close(conv, &sz));
928 if(n > SHA_DIGESTSIZE)
930 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
932 return d0_iobuf_close(out, outbuflen);
936 d0_iobuf_close(conv, &sz);
937 d0_iobuf_close(out, outbuflen);
941 d0_blind_id_t *d0_blind_id_new(void)
943 d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
944 memset(b, 0, sizeof(*b));
948 void d0_blind_id_free(d0_blind_id_t *a)
950 d0_blind_id_clear(a);