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 *rn; // random number blind signature
58 d0_bignum_t *r; // random number for schnorr ID
59 char xnbh[SCHNORR_HASHSIZE]; // init hash
60 d0_bignum_t *e; // challenge
61 char msg[MSGSIZE]; // message
62 size_t msglen; // message length
63 d0_bignum_t *other_4_to_r; // for DH key exchange
66 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
67 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
69 static d0_bignum_t *zero, *one, *four, *temp0, *temp1, *temp2, *temp3, *temp4;
71 void d0_blind_id_INITIALIZE(void)
73 d0_bignum_INITIALIZE();
74 CHECK_ASSIGN(zero, d0_bignum_int(zero, 0));
75 CHECK_ASSIGN(one, d0_bignum_int(one, 1));
76 CHECK_ASSIGN(four, d0_bignum_int(four, 4));
77 CHECK_ASSIGN(temp0, d0_bignum_int(temp0, 0));
78 CHECK_ASSIGN(temp1, d0_bignum_int(temp1, 0));
79 CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0));
80 CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0));
81 CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0));
86 void d0_blind_id_SHUTDOWN(void)
91 d0_bignum_free(temp0);
92 d0_bignum_free(temp1);
93 d0_bignum_free(temp2);
94 d0_bignum_free(temp3);
95 d0_bignum_free(temp4);
100 d0_bignum_t *d0_dl_get_order(d0_bignum_t *o, const d0_bignum_t *G)
102 CHECK_ASSIGN(o, d0_bignum_sub(o, G, one));
103 CHECK(d0_bignum_shl(o, o, -1)); // order o = (G-1)/2
109 d0_bignum_t *d0_dl_get_from_order(d0_bignum_t *G, const d0_bignum_t *o)
111 CHECK_ASSIGN(G, d0_bignum_shl(G, o, 1));
112 CHECK(d0_bignum_add(G, G, one));
118 BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
125 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
126 if(d0_bignum_isprime(temp0, 0) == 0)
128 CHECK(d0_dl_get_from_order(G, temp0));
129 if(d0_bignum_isprime(G, 10) == 0)
131 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
140 BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *e, d0_bignum_t *d, d0_bignum_t *n)
142 // uses temp0 to temp4
145 int pb = (size + 1)/2;
153 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
154 if(d0_bignum_isprime(temp0, 10) == 0)
156 CHECK(d0_bignum_sub(temp2, temp0, one));
157 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, e));
158 if(!d0_bignum_cmp(temp4, one))
167 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
168 if(!d0_bignum_cmp(temp1, temp0))
174 if(d0_bignum_isprime(temp1, 10) == 0)
176 CHECK(d0_bignum_sub(temp3, temp1, one));
177 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, e));
178 if(!d0_bignum_cmp(temp4, one))
186 CHECK(d0_bignum_mul(n, temp0, temp1));
188 // d = e^-1 mod (temp0-1)(temp1-1)
189 CHECK(d0_bignum_mul(temp0, temp2, temp3));
190 CHECK(d0_bignum_mod_inv(d, e, temp0));
196 void d0_blind_id_clear(d0_blind_id_t *ctx)
198 if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
199 if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
200 if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
201 if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
202 if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
203 if(ctx->schnorr_4_to_s) d0_bignum_free(ctx->schnorr_4_to_s);
204 if(ctx->schnorr_4_to_s_signature) d0_bignum_free(ctx->schnorr_4_to_s_signature);
205 if(ctx->rn) d0_bignum_free(ctx->rn);
206 if(ctx->r) d0_bignum_free(ctx->r);
207 if(ctx->e) d0_bignum_free(ctx->e);
208 memset(ctx, 0, sizeof(*ctx));
211 void d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
213 d0_blind_id_clear(ctx);
214 if(src->rsa_n) ctx->rsa_n = d0_bignum_mov(NULL, src->rsa_n);
215 if(src->rsa_e) ctx->rsa_e = d0_bignum_mov(NULL, src->rsa_e);
216 if(src->rsa_d) ctx->rsa_d = d0_bignum_mov(NULL, src->rsa_d);
217 if(src->schnorr_G) ctx->schnorr_G = d0_bignum_mov(NULL, src->schnorr_G);
218 if(src->schnorr_s) ctx->schnorr_s = d0_bignum_mov(NULL, src->schnorr_s);
219 if(src->schnorr_4_to_s) ctx->schnorr_4_to_s = d0_bignum_mov(NULL, ctx->schnorr_G);
220 if(src->schnorr_4_to_s_signature) ctx->schnorr_4_to_s_signature = d0_bignum_mov(NULL, src->schnorr_4_to_s_signature);
221 if(src->rn) ctx->rn = d0_bignum_mov(NULL, src->rn);
222 if(src->r) ctx->r = d0_bignum_mov(NULL, src->r);
223 if(src->e) ctx->e = d0_bignum_mov(NULL, src->e);
224 if(src->other_4_to_r) ctx->other_4_to_r = d0_bignum_mov(NULL, src->other_4_to_r);
225 // TODO xnbh, msg, msglen?
228 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
230 d0_blind_id_clear(ctx);
231 CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
232 CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
233 CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
234 CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
240 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
242 d0_iobuf_t *in = d0_iobuf_open_read(inbuf, inbuflen);
243 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
244 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
245 CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
246 return d0_iobuf_close(in, NULL);
249 d0_iobuf_close(in, NULL);
253 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
255 d0_iobuf_t *in = d0_iobuf_open_read(inbuf, inbuflen);
256 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
257 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
258 return d0_iobuf_close(in, NULL);
261 d0_iobuf_close(in, NULL);
265 #define USING(x) if(!(ctx->x)) return 0
268 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_key(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
270 d0_iobuf_t *out = NULL;
272 USING(rsa_n); USING(rsa_e); USING(rsa_d);
274 out = d0_iobuf_open_write(outbuf, *outbuflen);
275 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
276 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
277 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
278 return d0_iobuf_close(out, outbuflen);
281 d0_iobuf_close(out, outbuflen);
285 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_key(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
287 d0_iobuf_t *out = NULL;
289 USING(rsa_n); USING(rsa_e);
291 out = d0_iobuf_open_write(outbuf, *outbuflen);
292 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
293 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
294 return d0_iobuf_close(out, outbuflen);
297 if(!d0_iobuf_close(out, outbuflen))
302 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
305 REPLACING(schnorr_G);
307 CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
308 CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
314 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
316 d0_iobuf_t *in = d0_iobuf_open_read(inbuf, inbuflen);
317 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
318 return d0_iobuf_close(in, NULL);
321 d0_iobuf_close(in, NULL);
325 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_modulus(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
327 d0_iobuf_t *out = NULL;
331 out = d0_iobuf_open_write(outbuf, *outbuflen);
332 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
333 return d0_iobuf_close(out, outbuflen);
336 d0_iobuf_close(out, outbuflen);
340 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
342 // temps: temp0 order
344 REPLACING(schnorr_s); REPLACING(schnorr_4_to_s);
346 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
347 CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
348 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_bignum_mod_pow(ctx->schnorr_4_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
349 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_zero(ctx->schnorr_4_to_s_signature));
356 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
358 d0_iobuf_t *out = NULL;
360 // temps: temp0 temp1
361 USING(rsa_n); USING(rsa_e); USING(schnorr_4_to_s);
364 out = d0_iobuf_open_write(outbuf, *outbuflen);
366 CHECK_ASSIGN(ctx->rn, d0_bignum_rand_bit_atmost(ctx->rn, d0_bignum_size(ctx->rsa_n)));
367 CHECK(d0_bignum_mod_pow(temp0, ctx->rn, ctx->rsa_e, ctx->rsa_n));
368 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_4_to_s, temp0, ctx->rsa_n));
369 CHECK(d0_iobuf_write_bignum(out, temp1));
370 return d0_iobuf_close(out, outbuflen);
373 d0_iobuf_close(out, outbuflen);
377 WARN_UNUSED_RESULT BOOL d0_blind_id_answer_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen)
379 d0_iobuf_t *in = NULL;
380 d0_iobuf_t *out = NULL;
382 // temps: temp0 temp1
383 USING(rsa_d); USING(rsa_n);
385 in = d0_iobuf_open_read(inbuf, inbuflen);
386 out = d0_iobuf_open_write(outbuf, *outbuflen);
388 CHECK(d0_iobuf_read_bignum(in, temp0));
389 CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
390 CHECK(d0_iobuf_write_bignum(out, temp1));
392 d0_iobuf_close(in, NULL);
393 return d0_iobuf_close(out, outbuflen);
396 d0_iobuf_close(in, NULL);
397 d0_iobuf_close(out, outbuflen);
401 WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
403 d0_iobuf_t *in = NULL;
405 // temps: temp0 temp1
406 USING(rn); USING(rsa_n);
407 REPLACING(schnorr_4_to_s_signature);
409 in = d0_iobuf_open_read(inbuf, inbuflen);
411 CHECK(d0_iobuf_read_bignum(in, temp0));
412 CHECK(d0_bignum_mod_inv(temp1, ctx->rn, ctx->rsa_n));
413 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_4_to_s_signature, temp0, temp1, ctx->rsa_n));
415 return d0_iobuf_close(in, NULL);
418 d0_iobuf_close(in, NULL);
422 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
424 d0_iobuf_t *in = NULL;
426 REPLACING(schnorr_s); REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
428 in = d0_iobuf_open_read(inbuf, inbuflen);
430 CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
431 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
432 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
434 return d0_iobuf_close(in, NULL);
437 d0_iobuf_close(in, NULL);
441 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
443 d0_iobuf_t *in = NULL;
445 REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
447 in = d0_iobuf_open_read(inbuf, inbuflen);
449 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
450 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
452 return d0_iobuf_close(in, NULL);
455 d0_iobuf_close(in, NULL);
459 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
461 d0_iobuf_t *out = NULL;
463 USING(schnorr_s); USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
465 out = d0_iobuf_open_write(outbuf, *outbuflen);
467 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
468 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
469 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
471 return d0_iobuf_close(out, outbuflen);
474 d0_iobuf_close(out, outbuflen);
478 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
480 d0_iobuf_t *out = NULL;
482 USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
484 out = d0_iobuf_open_write(outbuf, *outbuflen);
486 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
487 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
489 return d0_iobuf_close(out, outbuflen);
492 d0_iobuf_close(out, outbuflen);
496 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)
498 // first run: send 4^s, 4^s signature
499 // 1. get random r, send HASH(4^r)
501 d0_iobuf_t *out = NULL;
502 static unsigned char convbuf[1024];
503 d0_iobuf_t *conv = NULL;
508 USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
513 out = d0_iobuf_open_write(outbuf, *outbuflen);
519 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
520 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
521 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
524 // start schnorr ID scheme
525 // generate random number r; x = g^r; send hash of x, remember r, forget x
526 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
527 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
528 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
530 // hash it, hash it, everybody hash it
531 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
532 CHECK(d0_iobuf_write_bignum(conv, temp0));
533 CHECK(d0_iobuf_write_packet(conv, msg, msglen));
534 CHECK(d0_iobuf_write_bignum(conv, temp0));
535 d0_iobuf_close(conv, &sz);
537 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
538 CHECK(d0_iobuf_write_packet(out, msg, msglen));
540 return d0_iobuf_close(out, outbuflen);
543 d0_iobuf_close(out, outbuflen);
547 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)
548 // first run: get 4^s, 4^s signature
551 // 3. send challenge e of SCHNORR_BITS
553 d0_iobuf_t *in = NULL;
554 d0_iobuf_t *out = NULL;
558 REPLACING(schnorr_4_to_s); REPLACING(k); REPLACING(schnorr_4_to_s_signature);
565 USING(schnorr_4_to_s_signature); USING(schnorr_4_to_s);
568 USING(rsa_e); USING(rsa_n);
569 REPLACING(e); REPLACING(msg); REPLACING(msglen);
571 in = d0_iobuf_open_read(inbuf, inbuflen);
572 out = d0_iobuf_open_write(outbuf, *outbuflen);
578 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
579 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
580 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
582 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
583 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, zero) > 0);
584 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, ctx->schnorr_G) < 0);
585 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
586 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero) >= 0);
587 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, ctx->rsa_n) < 0);
589 // check signature of key (t = k^d, so, t^e = k)
590 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_4_to_s_signature, ctx->rsa_e, ctx->rsa_n));
591 if(d0_bignum_cmp(temp0, ctx->schnorr_4_to_s))
593 // accept the key anyway, but mark as failed signature! will later return 0 in status
594 CHECK(d0_bignum_zero(ctx->schnorr_4_to_s_signature));
598 CHECK(d0_iobuf_read_raw(in, ctx->xnbh, SCHNORR_HASHSIZE));
599 ctx->msglen = MSGSIZE;
600 CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
603 CHECK_ASSIGN(ctx->e, d0_bignum_rand_bit_atmost(ctx->e, SCHNORR_BITS));
605 CHECK(d0_iobuf_write_bignum(out, ctx->e));
610 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
611 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
612 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
613 CHECK(d0_iobuf_write_bignum(out, temp0));
617 *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
619 d0_iobuf_close(in, NULL);
620 return d0_iobuf_close(out, outbuflen);
623 d0_iobuf_close(in, NULL);
624 d0_iobuf_close(out, outbuflen);
628 WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_response(d0_blind_id_t *ctx, BOOL is_first, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen)
629 // 1. read challenge e of SCHNORR_BITS
630 // 2. reply with r + s * e mod order
632 d0_iobuf_t *in = NULL;
633 d0_iobuf_t *out = NULL;
635 // temps: 0 order, 1 prod, 2 y, 3 e
636 USING(schnorr_G); USING(schnorr_s); USING(r);
638 in = d0_iobuf_open_read(inbuf, inbuflen);
639 out = d0_iobuf_open_write(outbuf, *outbuflen);
641 CHECK(d0_iobuf_read_bignum(in, temp3));
642 CHECK(d0_bignum_cmp(temp3, zero) >= 0);
643 CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
648 CHECK_ASSIGN(ctx->other_4_to_r, d0_iobuf_read_bignum(in, ctx->other_4_to_r));
649 CHECK(d0_bignum_cmp(ctx->other_4_to_r, zero) > 0);
650 CHECK(d0_bignum_cmp(ctx->other_4_to_r, ctx->schnorr_G) < 0);
653 // send response for schnorr ID scheme
654 // i.e. r + ctx->schnorr_s * temp3
655 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
656 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
657 CHECK(d0_bignum_mod_add(temp2, temp1, ctx->r, temp0));
658 CHECK(d0_iobuf_write_bignum(out, temp2));
660 d0_iobuf_close(in, NULL);
661 return d0_iobuf_close(out, outbuflen);
664 d0_iobuf_close(in, NULL);
665 d0_iobuf_close(out, outbuflen);
669 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)
670 // 1. read y = r + s * e mod order
671 // 2. verify: g^y (g^s)^-e = g^(r+s*e-s*e) = g^r
672 // (check using H(g^r) which we know)
674 d0_iobuf_t *in = NULL;
675 static unsigned char convbuf[1024];
676 d0_iobuf_t *conv = NULL;
679 // temps: 0 y 1 order
680 USING(e); USING(schnorr_G);
682 in = d0_iobuf_open_read(inbuf, inbuflen);
684 CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
685 CHECK(d0_iobuf_read_bignum(in, temp0));
686 CHECK(d0_bignum_cmp(temp0, zero) >= 0);
687 CHECK(d0_bignum_cmp(temp0, temp1) < 0);
689 // verify schnorr ID scheme
690 // we need 4^temp0 (g^s)^-e
691 CHECK(d0_bignum_neg(temp1, ctx->e));
692 CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_4_to_s, temp1, ctx->schnorr_G));
693 CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
694 CHECK_ASSIGN(ctx->other_4_to_r, d0_bignum_mod_mul(ctx->other_4_to_r, temp1, temp2, ctx->schnorr_G));
695 // hash must be equal to xnbh
697 // hash it, hash it, everybody hash it
698 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
699 CHECK(d0_iobuf_write_bignum(conv, ctx->other_4_to_r));
700 CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
701 CHECK(d0_iobuf_write_bignum(conv, ctx->other_4_to_r));
702 d0_iobuf_close(conv, &sz);
704 if(memcmp(sha(convbuf, sz), ctx->xnbh, SCHNORR_HASHSIZE))
706 // FAIL (not owned by player)
711 *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
713 if(ctx->msglen <= *msglen)
714 memcpy(msg, ctx->msg, ctx->msglen);
716 memcpy(msg, ctx->msg, *msglen);
717 *msglen = ctx->msglen;
719 d0_iobuf_close(in, NULL);
723 d0_iobuf_close(in, NULL);
727 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
729 d0_iobuf_t *out = NULL;
730 static unsigned char convbuf[1024];
731 d0_iobuf_t *conv = NULL;
734 USING(schnorr_4_to_s);
736 out = d0_iobuf_open_write(outbuf, *outbuflen);
737 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
739 CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_4_to_s));
740 CHECK(d0_iobuf_close(conv, &sz));
743 n = (*outbuflen / 4) * 3;
744 if(n > SHA_DIGESTSIZE)
746 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
747 CHECK(d0_iobuf_conv_base64_out(out));
749 return d0_iobuf_close(out, outbuflen);
753 d0_iobuf_close(conv, &sz);
754 d0_iobuf_close(out, outbuflen);
758 BOOL d0_blind_id_sessionkey_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
760 d0_iobuf_t *out = NULL;
761 static unsigned char convbuf[1024];
762 d0_iobuf_t *conv = NULL;
765 USING(r); USING(other_4_to_r);
767 out = d0_iobuf_open_write(outbuf, *outbuflen);
768 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
770 // temps: temp0 result
771 CHECK(d0_bignum_mod_pow(temp0, ctx->other_4_to_r, ctx->r, ctx->schnorr_G));
772 CHECK(d0_iobuf_write_bignum(conv, temp0));
773 CHECK(d0_iobuf_close(conv, &sz));
777 if(n > SHA_DIGESTSIZE)
779 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
781 return d0_iobuf_close(out, outbuflen);
785 d0_iobuf_close(conv, &sz);
786 d0_iobuf_close(out, outbuflen);
790 d0_blind_id_t *d0_blind_id_new(void)
792 d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
793 memset(b, 0, sizeof(*b));
797 void d0_blind_id_free(d0_blind_id_t *a)
799 d0_blind_id_clear(a);