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 void d0_blind_id_clear(d0_blind_id_t *ctx)
203 if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
204 if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
205 if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
206 if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
207 if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
208 if(ctx->schnorr_4_to_s) d0_bignum_free(ctx->schnorr_4_to_s);
209 if(ctx->schnorr_4_to_s_signature) d0_bignum_free(ctx->schnorr_4_to_s_signature);
210 if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
211 if(ctx->r) d0_bignum_free(ctx->r);
212 if(ctx->challenge) d0_bignum_free(ctx->challenge);
213 if(ctx->other_4_to_r) d0_bignum_free(ctx->other_4_to_r);
214 memset(ctx, 0, sizeof(*ctx));
217 void d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
219 d0_blind_id_clear(ctx);
220 if(src->rsa_n) ctx->rsa_n = d0_bignum_mov(NULL, src->rsa_n);
221 if(src->rsa_e) ctx->rsa_e = d0_bignum_mov(NULL, src->rsa_e);
222 if(src->rsa_d) ctx->rsa_d = d0_bignum_mov(NULL, src->rsa_d);
223 if(src->schnorr_G) ctx->schnorr_G = d0_bignum_mov(NULL, src->schnorr_G);
224 if(src->schnorr_s) ctx->schnorr_s = d0_bignum_mov(NULL, src->schnorr_s);
225 if(src->schnorr_4_to_s) ctx->schnorr_4_to_s = d0_bignum_mov(NULL, ctx->schnorr_G);
226 if(src->schnorr_4_to_s_signature) ctx->schnorr_4_to_s_signature = d0_bignum_mov(NULL, src->schnorr_4_to_s_signature);
227 if(src->rsa_blind_signature_camouflage) ctx->rsa_blind_signature_camouflage = d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage);
228 if(src->r) ctx->r = d0_bignum_mov(NULL, src->r);
229 if(src->challenge) ctx->challenge = d0_bignum_mov(NULL, src->challenge);
230 if(src->other_4_to_r) ctx->other_4_to_r = d0_bignum_mov(NULL, src->other_4_to_r);
231 memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
232 ctx->msglen = src->msglen;
233 memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
236 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
238 REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
240 CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
241 CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
242 CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
243 CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
249 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
251 d0_iobuf_t *in = NULL;
253 REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
255 in = d0_iobuf_open_read(inbuf, inbuflen);
257 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
258 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
259 CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
260 return d0_iobuf_close(in, NULL);
263 d0_iobuf_close(in, NULL);
267 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
269 d0_iobuf_t *in = NULL;
271 REPLACING(rsa_n); REPLACING(rsa_e);
273 in = d0_iobuf_open_read(inbuf, inbuflen);
274 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
275 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
276 return d0_iobuf_close(in, NULL);
279 d0_iobuf_close(in, NULL);
283 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_key(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
285 d0_iobuf_t *out = NULL;
287 USING(rsa_n); USING(rsa_e); USING(rsa_d);
289 out = d0_iobuf_open_write(outbuf, *outbuflen);
290 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
291 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
292 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
293 return d0_iobuf_close(out, outbuflen);
296 d0_iobuf_close(out, outbuflen);
300 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_key(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
302 d0_iobuf_t *out = NULL;
304 USING(rsa_n); USING(rsa_e);
306 out = d0_iobuf_open_write(outbuf, *outbuflen);
307 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
308 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
309 return d0_iobuf_close(out, outbuflen);
312 if(!d0_iobuf_close(out, outbuflen))
317 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
320 REPLACING(schnorr_G);
322 CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
323 CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
329 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
331 d0_iobuf_t *in = NULL;
333 REPLACING(schnorr_G);
335 in = d0_iobuf_open_read(inbuf, inbuflen);
336 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
337 return d0_iobuf_close(in, NULL);
340 d0_iobuf_close(in, NULL);
344 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_modulus(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
346 d0_iobuf_t *out = NULL;
350 out = d0_iobuf_open_write(outbuf, *outbuflen);
351 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
352 return d0_iobuf_close(out, outbuflen);
355 d0_iobuf_close(out, outbuflen);
359 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
361 // temps: temp0 = order
363 REPLACING(schnorr_s); REPLACING(schnorr_4_to_s);
365 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
366 CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
367 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_bignum_mod_pow(ctx->schnorr_4_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
368 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_zero(ctx->schnorr_4_to_s_signature));
375 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
377 d0_iobuf_t *out = NULL;
379 // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
380 USING(rsa_n); USING(rsa_e); USING(schnorr_4_to_s);
381 REPLACING(rsa_blind_signature_camouflage);
383 out = d0_iobuf_open_write(outbuf, *outbuflen);
385 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
386 CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
387 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_4_to_s, temp0, ctx->rsa_n));
388 CHECK(d0_iobuf_write_bignum(out, temp1));
389 return d0_iobuf_close(out, outbuflen);
392 d0_iobuf_close(out, outbuflen);
396 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)
398 d0_iobuf_t *in = NULL;
399 d0_iobuf_t *out = NULL;
401 // temps: temp0 input, temp1 temp0^d
402 USING(rsa_d); USING(rsa_n);
404 in = d0_iobuf_open_read(inbuf, inbuflen);
405 out = d0_iobuf_open_write(outbuf, *outbuflen);
407 CHECK(d0_iobuf_read_bignum(in, temp0));
408 CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
409 CHECK(d0_iobuf_write_bignum(out, temp1));
411 d0_iobuf_close(in, NULL);
412 return d0_iobuf_close(out, outbuflen);
415 d0_iobuf_close(in, NULL);
416 d0_iobuf_close(out, outbuflen);
420 WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
422 d0_iobuf_t *in = NULL;
424 // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
425 USING(rsa_blind_signature_camouflage); USING(rsa_n);
426 REPLACING(schnorr_4_to_s_signature);
428 in = d0_iobuf_open_read(inbuf, inbuflen);
430 CHECK(d0_iobuf_read_bignum(in, temp0));
431 CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
432 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_4_to_s_signature, temp0, temp1, ctx->rsa_n));
434 return d0_iobuf_close(in, NULL);
437 d0_iobuf_close(in, NULL);
441 WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
443 d0_iobuf_t *in = NULL;
445 REPLACING(schnorr_s); REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
447 in = d0_iobuf_open_read(inbuf, inbuflen);
449 CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
450 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
451 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
453 return d0_iobuf_close(in, NULL);
456 d0_iobuf_close(in, NULL);
460 WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
462 d0_iobuf_t *in = NULL;
464 REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
466 in = d0_iobuf_open_read(inbuf, inbuflen);
468 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
469 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
471 return d0_iobuf_close(in, NULL);
474 d0_iobuf_close(in, NULL);
478 WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
480 d0_iobuf_t *out = NULL;
482 USING(schnorr_s); 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_s));
487 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
488 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
490 return d0_iobuf_close(out, outbuflen);
493 d0_iobuf_close(out, outbuflen);
497 WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
499 d0_iobuf_t *out = NULL;
501 USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
503 out = d0_iobuf_open_write(outbuf, *outbuflen);
505 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
506 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
508 return d0_iobuf_close(out, outbuflen);
511 d0_iobuf_close(out, outbuflen);
515 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)
517 // first run: send 4^s, 4^s signature
518 // 1. get random r, send HASH(4^r)
520 d0_iobuf_t *out = NULL;
521 static unsigned char convbuf[1024];
522 d0_iobuf_t *conv = NULL;
525 // temps: temp0 order, temp0 4^r
528 USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
533 out = d0_iobuf_open_write(outbuf, *outbuflen);
539 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
540 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
541 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
544 // start schnorr ID scheme
545 // generate random number r; x = g^r; send hash of x, remember r, forget x
546 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
547 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
548 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
550 // hash it, hash it, everybody hash it
551 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
552 CHECK(d0_iobuf_write_bignum(conv, temp0));
553 CHECK(d0_iobuf_write_packet(conv, msg, msglen));
554 CHECK(d0_iobuf_write_bignum(conv, temp0));
555 d0_iobuf_close(conv, &sz);
557 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
558 CHECK(d0_iobuf_write_packet(out, msg, msglen));
560 return d0_iobuf_close(out, outbuflen);
563 d0_iobuf_close(out, outbuflen);
567 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)
568 // first run: get 4^s, 4^s signature
571 // 3. send challenge challenge of SCHNORR_BITS
573 d0_iobuf_t *in = NULL;
574 d0_iobuf_t *out = NULL;
576 // temps: temp0 order, temp0 signature check
579 REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
581 REPLACING(schnorr_G);
587 USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
590 USING(rsa_e); USING(rsa_n);
591 REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r);
593 in = d0_iobuf_open_read(inbuf, inbuflen);
594 out = d0_iobuf_open_write(outbuf, *outbuflen);
600 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
601 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
602 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
604 CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
605 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, zero) > 0);
606 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, ctx->schnorr_G) < 0);
607 CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
608 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero) >= 0);
609 CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, ctx->rsa_n) < 0);
611 // check signature of key (t = k^d, so, t^challenge = k)
612 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_4_to_s_signature, ctx->rsa_e, ctx->rsa_n));
613 if(d0_bignum_cmp(temp0, ctx->schnorr_4_to_s))
615 // accept the key anyway, but mark as failed signature! will later return 0 in status
616 CHECK(d0_bignum_zero(ctx->schnorr_4_to_s_signature));
620 CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
621 ctx->msglen = MSGSIZE;
622 CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
625 CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
627 CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
630 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
631 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
632 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
633 CHECK(d0_iobuf_write_bignum(out, temp0));
636 *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
638 d0_iobuf_close(in, NULL);
639 return d0_iobuf_close(out, outbuflen);
642 d0_iobuf_close(in, NULL);
643 d0_iobuf_close(out, outbuflen);
647 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)
648 // 1. read challenge challenge of SCHNORR_BITS
649 // 2. reply with r + s * challenge mod order
651 d0_iobuf_t *in = NULL;
652 d0_iobuf_t *out = NULL;
654 // temps: 0 order, 1 prod, 2 y, 3 challenge
655 REPLACING(other_4_to_r);
656 USING(schnorr_G); USING(schnorr_s); USING(r);
658 in = d0_iobuf_open_read(inbuf, inbuflen);
659 out = d0_iobuf_open_write(outbuf, *outbuflen);
661 CHECK(d0_iobuf_read_bignum(in, temp3));
662 CHECK(d0_bignum_cmp(temp3, zero) >= 0);
663 CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
666 CHECK_ASSIGN(ctx->other_4_to_r, d0_iobuf_read_bignum(in, ctx->other_4_to_r));
667 CHECK(d0_bignum_cmp(ctx->other_4_to_r, zero) > 0);
668 CHECK(d0_bignum_cmp(ctx->other_4_to_r, ctx->schnorr_G) < 0);
670 // send response for schnorr ID scheme
671 // i.challenge. r + ctx->schnorr_s * temp3
672 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
673 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
674 CHECK(d0_bignum_mod_add(temp2, temp1, ctx->r, temp0));
675 CHECK(d0_iobuf_write_bignum(out, temp2));
677 d0_iobuf_close(in, NULL);
678 return d0_iobuf_close(out, outbuflen);
681 d0_iobuf_close(in, NULL);
682 d0_iobuf_close(out, outbuflen);
686 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)
687 // 1. read y = r + s * challenge mod order
688 // 2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
689 // (check using H(g^r) which we know)
691 d0_iobuf_t *in = NULL;
692 static unsigned char convbuf[1024];
693 d0_iobuf_t *conv = NULL;
696 // temps: 0 y 1 order
697 USING(challenge); USING(schnorr_G);
698 REPLACING(other_4_to_r);
700 in = d0_iobuf_open_read(inbuf, inbuflen);
702 CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
703 CHECK(d0_iobuf_read_bignum(in, temp0));
704 CHECK(d0_bignum_cmp(temp0, zero) >= 0);
705 CHECK(d0_bignum_cmp(temp0, temp1) < 0);
707 // verify schnorr ID scheme
708 // we need 4^temp0 (g^s)^-challenge
709 CHECK(d0_bignum_neg(temp1, ctx->challenge));
710 CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_4_to_s, temp1, ctx->schnorr_G));
711 CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
712 CHECK_ASSIGN(ctx->other_4_to_r, d0_bignum_mod_mul(ctx->other_4_to_r, temp1, temp2, ctx->schnorr_G));
713 // hash must be equal to msghash
715 // hash it, hash it, everybody hash it
716 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
717 CHECK(d0_iobuf_write_bignum(conv, ctx->other_4_to_r));
718 CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
719 CHECK(d0_iobuf_write_bignum(conv, ctx->other_4_to_r));
720 d0_iobuf_close(conv, &sz);
722 if(memcmp(sha(convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
724 // FAIL (not owned by player)
729 *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
731 if(ctx->msglen <= *msglen)
732 memcpy(msg, ctx->msg, ctx->msglen);
734 memcpy(msg, ctx->msg, *msglen);
735 *msglen = ctx->msglen;
737 d0_iobuf_close(in, NULL);
741 d0_iobuf_close(in, NULL);
745 WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
747 d0_iobuf_t *out = NULL;
748 static unsigned char convbuf[1024];
749 d0_iobuf_t *conv = NULL;
752 USING(schnorr_4_to_s);
754 out = d0_iobuf_open_write(outbuf, *outbuflen);
755 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
757 CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_4_to_s));
758 CHECK(d0_iobuf_close(conv, &sz));
761 n = (*outbuflen / 4) * 3;
762 if(n > SHA_DIGESTSIZE)
764 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
765 CHECK(d0_iobuf_conv_base64_out(out));
767 return d0_iobuf_close(out, outbuflen);
771 d0_iobuf_close(conv, &sz);
772 d0_iobuf_close(out, outbuflen);
776 BOOL d0_blind_id_sessionkey_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
778 d0_iobuf_t *out = NULL;
779 static unsigned char convbuf[1024];
780 d0_iobuf_t *conv = NULL;
783 USING(r); USING(other_4_to_r); USING(schnorr_G);
785 out = d0_iobuf_open_write(outbuf, *outbuflen);
786 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
788 // temps: temp0 result
789 CHECK(d0_bignum_mod_pow(temp0, ctx->other_4_to_r, ctx->r, ctx->schnorr_G));
790 CHECK(d0_iobuf_write_bignum(conv, temp0));
791 CHECK(d0_iobuf_close(conv, &sz));
795 if(n > SHA_DIGESTSIZE)
797 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
799 return d0_iobuf_close(out, outbuflen);
803 d0_iobuf_close(conv, &sz);
804 d0_iobuf_close(out, outbuflen);
808 d0_blind_id_t *d0_blind_id_new(void)
810 d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
811 memset(b, 0, sizeof(*b));
815 void d0_blind_id_free(d0_blind_id_t *a)
817 d0_blind_id_clear(a);