3 * AUTHOR: Rudolf Polzer - divVerent@xonotic.org
5 * Copyright (c) 2010, Rudolf Polzer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "d0_blind_id.h"
40 #include "d0_bignum.h"
43 // old "positive" protocol, uses one extra mod_inv in verify stages
44 // #define D0_BLIND_ID_POSITIVE_PROTOCOL
47 #define SHA_DIGESTSIZE 32
48 const unsigned char *sha(unsigned char *h, const unsigned char *in, size_t len)
50 d0_blind_id_util_sha256((char *) h, (const char *) in, len);
54 // for zero knowledge, we need multiple instances of schnorr ID scheme... should normally be sequential
55 // parallel schnorr ID is not provably zero knowledge :(
56 // (evil verifier can know all questions in advance, so sequential is disadvantage for him)
57 // we'll just live with a 1:1048576 chance of cheating, and support reauthenticating
59 #define SCHNORR_BITS 20
60 // probability of cheat: 2^(-bits+1)
62 #define SCHNORR_HASHSIZE SHA_DIGESTSIZE
63 // cannot be >= SHA_DIGESTSIZE
64 // *8 must be >= SCHNORR_BITS
65 // no need to save bits here
67 #define MSGSIZE 640 // ought to be enough for anyone
71 // signing (Xonotic pub and priv key)
72 d0_bignum_t *rsa_n, *rsa_e, *rsa_d;
74 // public data (Schnorr ID)
75 d0_bignum_t *schnorr_G;
77 // private data (player ID private key)
78 d0_bignum_t *schnorr_s;
80 // public data (player ID public key, this is what the server gets to know)
81 d0_bignum_t *schnorr_g_to_s;
82 d0_bignum_t *schnorr_H_g_to_s_signature; // 0 when signature is invalid
83 // as hash function H, we get the SHA1 and reinterpret as bignum - yes, it always is < 160 bits
86 d0_bignum_t *rsa_blind_signature_camouflage; // random number blind signature
88 d0_bignum_t *r; // random number for schnorr ID
89 d0_bignum_t *t; // for DH key exchange
90 d0_bignum_t *g_to_t; // for DH key exchange
91 d0_bignum_t *other_g_to_t; // for DH key exchange
92 d0_bignum_t *challenge; // challenge
94 char msghash[SCHNORR_HASHSIZE]; // init hash
95 char msg[MSGSIZE]; // message
96 size_t msglen; // message length
101 #define CHECK(x) do { if(!(x)) { fprintf(stderr, "CHECK FAILED (%s:%d): %s\n", __FILE__, __LINE__, #x); goto fail; } } while(0)
102 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) { fprintf(stderr, "CHECK FAILED (%s:%d): %s\n", __FILE__, __LINE__, #value); goto fail; } var = val; } while(0)
104 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
105 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
108 #define USING(x) if(!(ctx->x)) return 0
112 static d0_bignum_t *zero, *one, *four;
114 static d0_bignum_t *temp0, *temp1, *temp2, *temp3, *temp4;
115 static void *tempmutex = NULL; // hold this mutex when using temp0 to temp4
116 #define USINGTEMPS() int locked = 0
117 #define LOCKTEMPS() do { if(!locked) d0_lockmutex(tempmutex); locked = 1; } while(0)
118 #define UNLOCKTEMPS() do { if(locked) d0_unlockmutex(tempmutex); locked = 0; } while(0);
120 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_INITIALIZE(void)
124 tempmutex = d0_createmutex();
126 CHECK(d0_bignum_INITIALIZE());
127 CHECK_ASSIGN(zero, d0_bignum_int(zero, 0));
128 CHECK_ASSIGN(one, d0_bignum_int(one, 1));
129 CHECK_ASSIGN(four, d0_bignum_int(four, 4));
130 CHECK_ASSIGN(temp0, d0_bignum_int(temp0, 0));
131 CHECK_ASSIGN(temp1, d0_bignum_int(temp1, 0));
132 CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0));
133 CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0));
134 CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0));
142 void d0_blind_id_SHUTDOWN(void)
146 d0_bignum_free(zero);
148 d0_bignum_free(four);
149 d0_bignum_free(temp0);
150 d0_bignum_free(temp1);
151 d0_bignum_free(temp2);
152 d0_bignum_free(temp3);
153 d0_bignum_free(temp4);
154 d0_bignum_SHUTDOWN();
156 d0_destroymutex(tempmutex);
161 static d0_bignum_t *d0_dl_get_order(d0_bignum_t *o, const d0_bignum_t *G)
163 CHECK_ASSIGN(o, d0_bignum_sub(o, G, one));
164 CHECK(d0_bignum_shl(o, o, -1)); // order o = (G-1)/2
170 d0_bignum_t *d0_dl_get_from_order(d0_bignum_t *G, const d0_bignum_t *o)
172 CHECK_ASSIGN(G, d0_bignum_shl(G, o, 1));
173 CHECK(d0_bignum_add(G, G, one));
179 // temps must NOT be locked when calling this
180 static D0_BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
182 USINGTEMPS(); // using: temp0
188 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
189 if(d0_bignum_isprime(temp0, 0) == 0)
191 CHECK(d0_dl_get_from_order(G, temp0));
192 if(d0_bignum_isprime(G, 10) == 0)
194 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
205 // temps must NOT be locked when calling this
206 static D0_BOOL d0_rsa_generate_key(size_t size, d0_blind_id_t *ctx)
208 USINGTEMPS(); // uses temp1 to temp4
211 int pb = (size + 1)/2;
218 // we use ctx->rsa_d for the first result so that we can unlock temps later
222 CHECK(d0_bignum_rand_bit_exact(ctx->rsa_d, pb));
223 if(d0_bignum_isprime(ctx->rsa_d, 10) == 0)
228 CHECK(d0_bignum_sub(temp2, ctx->rsa_d, one));
229 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
230 if(!d0_bignum_cmp(temp4, one))
242 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
243 if(!d0_bignum_cmp(temp1, ctx->rsa_d))
251 if(d0_bignum_isprime(temp1, 10) == 0)
256 CHECK(d0_bignum_sub(temp3, temp1, one));
257 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
258 if(!d0_bignum_cmp(temp4, one))
260 // we do NOT unlock, as we still need temp1 and temp3
269 // ctx->rsa_n = ctx->rsa_d*temp1
270 CHECK(d0_bignum_mul(ctx->rsa_n, ctx->rsa_d, temp1));
272 // ctx->rsa_d = ctx->rsa_e^-1 mod (ctx->rsa_d-1)(temp1-1)
273 CHECK(d0_bignum_sub(temp2, ctx->rsa_d, one)); // we can't reuse the value from above because temps were unlocked
274 CHECK(d0_bignum_mul(temp0, temp2, temp3));
275 CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp0));
283 // temps must NOT be locked when calling this
284 static D0_BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass)
286 USINGTEMPS(); // uses temp1 to temp4
289 int pb = (size + 1)/2;
296 // we use ctx->rsa_d for the first result so that we can unlock temps later
300 CHECK(d0_bignum_rand_bit_exact(ctx->rsa_d, pb));
301 if(d0_bignum_isprime(ctx->rsa_d, 10) == 0)
306 CHECK(d0_bignum_sub(temp2, ctx->rsa_d, one));
307 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
308 if(!d0_bignum_cmp(temp4, one))
320 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
321 if(!d0_bignum_cmp(temp1, ctx->rsa_d))
330 // n = ctx->rsa_d*temp1
331 CHECK(d0_bignum_mul(ctx->rsa_n, ctx->rsa_d, temp1));
332 if(reject(ctx, pass))
338 if(d0_bignum_isprime(temp1, 10) == 0)
343 CHECK(d0_bignum_sub(temp3, temp1, one));
344 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
345 if(!d0_bignum_cmp(temp4, one))
347 // we do NOT unlock, as we still need temp3
356 // ctx->rsa_d = ctx->rsa_e^-1 mod (ctx->rsa_d-1)(temp1-1)
357 CHECK(d0_bignum_sub(temp2, ctx->rsa_d, one)); // we can't reuse the value from above because temps were unlocked
358 CHECK(d0_bignum_mul(ctx->rsa_d, temp2, temp3));
359 CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp0));
367 D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_destructive(unsigned char *convbuf, size_t sz, unsigned char *outbuf, size_t outbuflen)
373 while(n > SHA_DIGESTSIZE)
375 memcpy(outbuf, sha(shabuf, convbuf, sz), SHA_DIGESTSIZE);
376 outbuf += SHA_DIGESTSIZE;
378 for(i = 0; i < sz; ++i)
380 break; // stop until no carry
382 memcpy(outbuf, sha(shabuf, convbuf, sz), n);
386 D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_bignum(const d0_bignum_t *in, unsigned char *outbuf, size_t outbuflen)
388 unsigned char convbuf[1024];
391 CHECK(d0_bignum_export_unsigned(in, convbuf, sizeof(convbuf)) >= 0);
392 sz = (d0_bignum_size(in) + 7) / 8;
393 CHECK(d0_longhash_destructive(convbuf, sz, outbuf, outbuflen));
400 void d0_blind_id_clear(d0_blind_id_t *ctx)
402 if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
403 if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
404 if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
405 if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
406 if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
407 if(ctx->schnorr_g_to_s) d0_bignum_free(ctx->schnorr_g_to_s);
408 if(ctx->schnorr_H_g_to_s_signature) d0_bignum_free(ctx->schnorr_H_g_to_s_signature);
409 if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
410 if(ctx->r) d0_bignum_free(ctx->r);
411 if(ctx->challenge) d0_bignum_free(ctx->challenge);
412 if(ctx->t) d0_bignum_free(ctx->t);
413 if(ctx->g_to_t) d0_bignum_free(ctx->g_to_t);
414 if(ctx->other_g_to_t) d0_bignum_free(ctx->other_g_to_t);
415 memset(ctx, 0, sizeof(*ctx));
418 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
420 d0_blind_id_clear(ctx);
421 if(src->rsa_n) CHECK_ASSIGN(ctx->rsa_n, d0_bignum_mov(NULL, src->rsa_n));
422 if(src->rsa_e) CHECK_ASSIGN(ctx->rsa_e, d0_bignum_mov(NULL, src->rsa_e));
423 if(src->rsa_d) CHECK_ASSIGN(ctx->rsa_d, d0_bignum_mov(NULL, src->rsa_d));
424 if(src->schnorr_G) CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_mov(NULL, src->schnorr_G));
425 if(src->schnorr_s) CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_mov(NULL, src->schnorr_s));
426 if(src->schnorr_g_to_s) CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mov(NULL, src->schnorr_g_to_s));
427 if(src->schnorr_H_g_to_s_signature) CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_mov(NULL, src->schnorr_H_g_to_s_signature));
428 if(src->rsa_blind_signature_camouflage) CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage));
429 if(src->r) CHECK_ASSIGN(ctx->r, d0_bignum_mov(NULL, src->r));
430 if(src->challenge) CHECK_ASSIGN(ctx->challenge, d0_bignum_mov(NULL, src->challenge));
431 if(src->t) CHECK_ASSIGN(ctx->t, d0_bignum_mov(NULL, src->t));
432 if(src->g_to_t) CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mov(NULL, src->g_to_t));
433 if(src->other_g_to_t) CHECK_ASSIGN(ctx->other_g_to_t, d0_bignum_mov(NULL, src->other_g_to_t));
434 memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
435 ctx->msglen = src->msglen;
436 memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
439 d0_blind_id_clear(ctx);
443 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_key_fastreject(d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass)
445 REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
447 CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
448 CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
449 CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
451 CHECK(d0_rsa_generate_key_fastreject(k+1, reject, ctx, pass)); // must fit G for sure
453 CHECK(d0_rsa_generate_key(k+1, ctx)); // must fit G for sure
459 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
461 return d0_blind_id_generate_private_key_fastreject(ctx, k, NULL, NULL);
464 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
466 d0_iobuf_t *in = NULL;
468 REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
470 in = d0_iobuf_open_read(inbuf, inbuflen);
472 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
473 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
474 CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
475 return d0_iobuf_close(in, NULL);
478 d0_iobuf_close(in, NULL);
482 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
484 d0_iobuf_t *in = NULL;
486 REPLACING(rsa_n); REPLACING(rsa_e);
488 in = d0_iobuf_open_read(inbuf, inbuflen);
489 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
490 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
491 return d0_iobuf_close(in, NULL);
494 d0_iobuf_close(in, NULL);
498 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
500 d0_iobuf_t *out = NULL;
502 USING(rsa_n); USING(rsa_e); USING(rsa_d);
504 out = d0_iobuf_open_write(outbuf, *outbuflen);
505 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
506 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
507 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
508 return d0_iobuf_close(out, outbuflen);
511 d0_iobuf_close(out, outbuflen);
515 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
517 d0_iobuf_t *out = NULL;
519 USING(rsa_n); USING(rsa_e);
521 out = d0_iobuf_open_write(outbuf, *outbuflen);
522 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
523 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
524 return d0_iobuf_close(out, outbuflen);
527 if(!d0_iobuf_close(out, outbuflen))
532 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
534 d0_iobuf_t *out = NULL;
535 unsigned char convbuf[2048];
536 d0_iobuf_t *conv = NULL;
540 USING(rsa_n); USING(rsa_e);
542 out = d0_iobuf_open_write(outbuf, *outbuflen);
543 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
545 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
546 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
547 CHECK(d0_iobuf_close(conv, &sz));
550 n = (*outbuflen / 4) * 3;
551 if(n > SHA_DIGESTSIZE)
553 CHECK(d0_iobuf_write_raw(out, sha(shabuf, convbuf, sz), n) == n);
554 CHECK(d0_iobuf_conv_base64_out(out));
556 return d0_iobuf_close(out, outbuflen);
560 d0_iobuf_close(conv, &sz);
561 d0_iobuf_close(out, outbuflen);
565 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
568 REPLACING(schnorr_G);
570 CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
571 CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
577 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
579 d0_iobuf_t *in = NULL;
581 REPLACING(schnorr_G);
583 in = d0_iobuf_open_read(inbuf, inbuflen);
584 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
585 return d0_iobuf_close(in, NULL);
588 d0_iobuf_close(in, NULL);
592 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
594 d0_iobuf_t *out = NULL;
598 out = d0_iobuf_open_write(outbuf, *outbuflen);
599 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
600 return d0_iobuf_close(out, outbuflen);
603 d0_iobuf_close(out, outbuflen);
607 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
609 USINGTEMPS(); // temps: temp0 = order
611 REPLACING(schnorr_s); REPLACING(schnorr_g_to_s);
614 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
615 CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
616 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mod_pow(ctx->schnorr_g_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
617 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
626 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
628 d0_iobuf_t *out = NULL;
629 unsigned char hashbuf[2048];
632 USINGTEMPS(); // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
633 USING(rsa_n); USING(rsa_e); USING(schnorr_g_to_s);
634 REPLACING(rsa_blind_signature_camouflage);
636 out = d0_iobuf_open_write(outbuf, *outbuflen);
638 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
639 CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
641 // we will actually sign HA(4^s) to prevent a malleability attack!
643 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
644 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
645 if(sz > sizeof(hashbuf))
646 sz = sizeof(hashbuf);
647 CHECK(d0_longhash_bignum(temp2, hashbuf, sz));
648 CHECK(d0_bignum_import_unsigned(temp2, hashbuf, sz));
651 CHECK(d0_bignum_mod_mul(temp1, temp2, temp0, ctx->rsa_n));
652 CHECK(d0_iobuf_write_bignum(out, temp1));
654 return d0_iobuf_close(out, outbuflen);
658 d0_iobuf_close(out, outbuflen);
662 D0_WARN_UNUSED_RESULT D0_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)
664 d0_iobuf_t *in = NULL;
665 d0_iobuf_t *out = NULL;
667 USINGTEMPS(); // temps: temp0 input, temp1 temp0^d
668 USING(rsa_d); USING(rsa_n);
670 in = d0_iobuf_open_read(inbuf, inbuflen);
671 out = d0_iobuf_open_write(outbuf, *outbuflen);
674 CHECK(d0_iobuf_read_bignum(in, temp0));
675 CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
676 CHECK(d0_iobuf_write_bignum(out, temp1));
679 d0_iobuf_close(in, NULL);
680 return d0_iobuf_close(out, outbuflen);
684 d0_iobuf_close(in, NULL);
685 d0_iobuf_close(out, outbuflen);
689 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
691 d0_iobuf_t *in = NULL;
693 USINGTEMPS(); // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
694 USING(rsa_blind_signature_camouflage); USING(rsa_n);
695 REPLACING(schnorr_H_g_to_s_signature);
697 in = d0_iobuf_open_read(inbuf, inbuflen);
701 CHECK(d0_iobuf_read_bignum(in, temp0));
702 CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
703 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_H_g_to_s_signature, temp0, temp1, ctx->rsa_n));
706 return d0_iobuf_close(in, NULL);
710 d0_iobuf_close(in, NULL);
714 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id_request_camouflage(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
716 d0_iobuf_t *in = NULL;
718 REPLACING(rsa_blind_signature_camouflage);
720 in = d0_iobuf_open_read(inbuf, inbuflen);
722 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_iobuf_read_bignum(in, ctx->rsa_blind_signature_camouflage));
724 return d0_iobuf_close(in, NULL);
727 d0_iobuf_close(in, NULL);
731 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id_request_camouflage(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
733 d0_iobuf_t *out = NULL;
735 USING(rsa_blind_signature_camouflage);
737 out = d0_iobuf_open_write(outbuf, *outbuflen);
739 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_blind_signature_camouflage));
741 return d0_iobuf_close(out, outbuflen);
744 d0_iobuf_close(out, outbuflen);
748 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
750 d0_iobuf_t *in = NULL;
752 REPLACING(schnorr_s); REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
754 in = d0_iobuf_open_read(inbuf, inbuflen);
756 CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
757 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
758 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
760 return d0_iobuf_close(in, NULL);
763 d0_iobuf_close(in, NULL);
767 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
769 d0_iobuf_t *in = NULL;
771 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
773 in = d0_iobuf_open_read(inbuf, inbuflen);
775 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
776 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
778 return d0_iobuf_close(in, NULL);
781 d0_iobuf_close(in, NULL);
785 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
787 d0_iobuf_t *out = NULL;
789 USING(schnorr_s); USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
791 out = d0_iobuf_open_write(outbuf, *outbuflen);
793 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
794 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
795 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
797 return d0_iobuf_close(out, outbuflen);
800 d0_iobuf_close(out, outbuflen);
804 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
806 d0_iobuf_t *out = NULL;
808 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
810 out = d0_iobuf_open_write(outbuf, *outbuflen);
812 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
813 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
815 return d0_iobuf_close(out, outbuflen);
818 d0_iobuf_close(out, outbuflen);
822 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *msg, size_t msglen, char *outbuf, size_t *outbuflen)
824 // first run: send 4^s, 4^s signature
825 // 1. get random r, send HASH(4^r)
827 d0_iobuf_t *out = NULL;
828 unsigned char convbuf[1024];
829 d0_iobuf_t *conv = NULL;
834 USINGTEMPS(); // temps: temp0 order, temp0 4^r
837 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
840 REPLACING(r); REPLACING(t); REPLACING(g_to_t);
842 out = d0_iobuf_open_write(outbuf, *outbuflen);
848 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
849 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
850 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
853 // start schnorr ID scheme
854 // generate random number r; x = g^r; send hash of x, remember r, forget x
856 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
858 CHECK_ASSIGN(ctx->r, d0_bignum_int(ctx->r, 4)); // decided by fair dice roll
860 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
863 // initialize Signed Diffie Hellmann
864 // we already have the group order in temp1
866 CHECK_ASSIGN(ctx->t, d0_bignum_int(ctx->t, 4)); // decided by fair dice roll
868 CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
870 // can we SOMEHOW do this with just one mod_pow?
872 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
873 CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mod_pow(ctx->g_to_t, four, ctx->t, ctx->schnorr_G));
876 // hash it, hash it, everybody hash it
877 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
878 CHECK(d0_iobuf_write_bignum(conv, temp0));
879 CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
880 CHECK(d0_iobuf_write_packet(conv, msg, msglen));
881 CHECK(d0_iobuf_write_bignum(conv, temp0));
883 CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
884 d0_iobuf_close(conv, &sz);
886 CHECK(d0_iobuf_write_raw(out, sha(shabuf, convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
887 CHECK(d0_iobuf_write_packet(out, msg, msglen));
889 return d0_iobuf_close(out, outbuflen);
893 d0_iobuf_close(out, outbuflen);
897 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen, D0_BOOL *status)
898 // first run: get 4^s, 4^s signature
901 // 3. send challenge challenge of SCHNORR_BITS
903 d0_iobuf_t *in = NULL;
904 d0_iobuf_t *out = NULL;
905 unsigned char hashbuf[2048];
908 USINGTEMPS(); // temps: temp0 order, temp0 signature check
911 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
913 REPLACING(schnorr_G);
919 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
922 USING(rsa_e); USING(rsa_n);
923 REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r); REPLACING(t);
925 in = d0_iobuf_open_read(inbuf, inbuflen);
926 out = d0_iobuf_open_write(outbuf, *outbuflen);
932 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
933 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
934 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
936 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
937 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
938 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
939 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
940 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
941 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
943 // check signature of key (t = k^d, so, t^challenge = k)
945 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
947 // we will actually sign SHA(4^s) to prevent a malleability attack!
948 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
949 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
950 if(sz > sizeof(hashbuf))
951 sz = sizeof(hashbuf);
952 CHECK(d0_longhash_bignum(temp2, hashbuf, sz));
953 CHECK(d0_bignum_import_unsigned(temp2, hashbuf, sz));
955 // + 7 / 8 is too large, so let's mod it
956 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
959 if(d0_bignum_cmp(temp0, temp1))
961 // accept the key anyway, but mark as failed signature! will later return 0 in status
962 CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
966 CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
967 ctx->msglen = MSGSIZE;
968 CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
972 CHECK_ASSIGN(ctx->challenge, d0_bignum_int(ctx->challenge, 4)); // decided by fair dice roll
974 CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
976 CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
978 // Diffie Hellmann send
980 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
982 CHECK_ASSIGN(ctx->t, d0_bignum_int(ctx->t, 4)); // decided by fair dice roll
984 CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
986 CHECK(d0_bignum_mod_pow(temp0, four, ctx->t, ctx->schnorr_G));
987 CHECK(d0_iobuf_write_bignum(out, temp0));
991 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
993 d0_iobuf_close(in, NULL);
994 return d0_iobuf_close(out, outbuflen);
998 d0_iobuf_close(in, NULL);
999 d0_iobuf_close(out, outbuflen);
1003 D0_WARN_UNUSED_RESULT D0_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)
1004 // 1. read challenge challenge of SCHNORR_BITS
1005 // 2. reply with r + s * challenge mod order
1007 d0_iobuf_t *in = NULL;
1008 d0_iobuf_t *out = NULL;
1010 USINGTEMPS(); // temps: 0 order, 1 prod, 2 y, 3 challenge
1011 REPLACING(other_g_to_t); REPLACING(t);
1012 USING(schnorr_G); USING(schnorr_s); USING(r); USING(g_to_t);
1014 in = d0_iobuf_open_read(inbuf, inbuflen);
1015 out = d0_iobuf_open_write(outbuf, *outbuflen);
1018 CHECK(d0_iobuf_read_bignum(in, temp3));
1019 CHECK(d0_bignum_cmp(temp3, zero) >= 0);
1020 CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
1022 // send response for schnorr ID scheme
1023 // i.challenge. r + ctx->schnorr_s * temp3
1024 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
1025 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
1026 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1027 CHECK(d0_bignum_mod_add(temp2, ctx->r, temp1, temp0));
1029 CHECK(d0_bignum_mod_sub(temp2, ctx->r, temp1, temp0));
1031 CHECK(d0_iobuf_write_bignum(out, temp2));
1034 // Diffie Hellmann recv
1035 CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
1036 CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
1037 CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
1038 // Diffie Hellmann send
1039 CHECK(d0_iobuf_write_bignum(out, ctx->g_to_t));
1041 d0_iobuf_close(in, NULL);
1042 return d0_iobuf_close(out, outbuflen);
1046 d0_iobuf_close(in, NULL);
1047 d0_iobuf_close(out, outbuflen);
1051 D0_WARN_UNUSED_RESULT D0_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, D0_BOOL *status)
1052 // 1. read y = r + s * challenge mod order
1053 // 2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
1054 // (check using H(g^r) which we know)
1056 d0_iobuf_t *in = NULL;
1057 unsigned char convbuf[1024];
1058 d0_iobuf_t *conv = NULL;
1062 USINGTEMPS(); // temps: 0 y 1 order
1063 USING(challenge); USING(schnorr_G);
1064 REPLACING(other_g_to_t);
1066 in = d0_iobuf_open_read(inbuf, inbuflen);
1070 CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
1071 CHECK(d0_iobuf_read_bignum(in, temp0));
1072 CHECK(d0_bignum_cmp(temp0, zero) >= 0);
1073 CHECK(d0_bignum_cmp(temp0, temp1) < 0);
1075 // verify schnorr ID scheme
1076 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1077 // we need 4^r = 4^temp0 (g^s)^-challenge
1078 CHECK(d0_bignum_mod_inv(temp1, ctx->schnorr_g_to_s, ctx->schnorr_G));
1079 CHECK(d0_bignum_mod_pow(temp2, temp1, ctx->challenge, ctx->schnorr_G));
1081 // we need 4^r = 4^temp0 (g^s)^challenge
1082 CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_g_to_s, ctx->challenge, ctx->schnorr_G));
1084 CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
1085 CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G));
1087 // Diffie Hellmann recv
1088 CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
1089 CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
1090 CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
1092 // hash it, hash it, everybody hash it
1093 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1094 CHECK(d0_iobuf_write_bignum(conv, temp3));
1095 CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
1096 CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
1097 CHECK(d0_iobuf_write_bignum(conv, temp3));
1099 CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
1100 d0_iobuf_close(conv, &sz);
1102 if(memcmp(sha(shabuf, convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
1104 // FAIL (not owned by player)
1109 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
1111 if(ctx->msglen <= *msglen)
1112 memcpy(msg, ctx->msg, ctx->msglen);
1114 memcpy(msg, ctx->msg, *msglen);
1115 *msglen = ctx->msglen;
1117 d0_iobuf_close(in, NULL);
1122 d0_iobuf_close(in, NULL);
1126 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_generate_missing_signature(d0_blind_id_t *ctx)
1129 unsigned char hashbuf[2048];
1131 USINGTEMPS(); // temps: 2 hash
1132 REPLACING(schnorr_H_g_to_s_signature);
1133 USING(schnorr_g_to_s); USING(rsa_d); USING(rsa_n);
1137 // we will actually sign SHA(4^s) to prevent a malleability attack!
1138 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1139 if(sz > sizeof(hashbuf))
1140 sz = sizeof(hashbuf);
1141 CHECK(d0_longhash_bignum(ctx->schnorr_g_to_s, hashbuf, sz));
1143 CHECK(d0_bignum_import_unsigned(temp2, hashbuf, sz));
1145 // + 7 / 8 is too large, so let's mod it
1146 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1147 CHECK(d0_bignum_mod_pow(ctx->schnorr_H_g_to_s_signature, temp1, ctx->rsa_d, ctx->rsa_n));
1157 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_sign_internal(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, D0_BOOL with_msg, const char *message, size_t msglen, char *outbuf, size_t *outbuflen)
1159 d0_iobuf_t *out = NULL;
1160 unsigned char *convbuf = NULL;
1161 unsigned char hashbuf[2048];
1162 d0_iobuf_t *conv = NULL;
1165 USINGTEMPS(); // temps: 0 order 1 4^r 2 hash
1168 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
1174 out = d0_iobuf_open_write(outbuf, *outbuflen);
1180 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
1181 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
1182 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
1185 // start schnorr SIGNATURE scheme
1186 // generate random number r; x = g^r; send hash of H(m||r), remember r, forget x
1188 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
1189 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
1190 CHECK(d0_bignum_mod_pow(temp1, four, ctx->r, ctx->schnorr_G));
1192 // hash it, hash it, everybody hash it
1193 conv = d0_iobuf_open_write_p((void **) &convbuf, 0);
1194 CHECK(d0_iobuf_write_packet(conv, message, msglen));
1195 CHECK(d0_iobuf_write_bignum(conv, temp1));
1196 d0_iobuf_close(conv, &sz);
1198 CHECK(d0_longhash_destructive(convbuf, sz, hashbuf, (d0_bignum_size(temp0) + 7) / 8));
1201 CHECK(d0_bignum_import_unsigned(temp2, hashbuf, (d0_bignum_size(temp0) + 7) / 8));
1202 CHECK(d0_iobuf_write_bignum(out, temp2));
1204 // multiply with secret, sub k, modulo order
1205 CHECK(d0_bignum_mod_mul(temp1, temp2, ctx->schnorr_s, temp0));
1206 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1207 CHECK(d0_bignum_mod_add(temp2, ctx->r, temp1, temp0));
1209 CHECK(d0_bignum_mod_sub(temp2, ctx->r, temp1, temp0));
1211 CHECK(d0_iobuf_write_bignum(out, temp2));
1214 // write the message itself
1216 CHECK(d0_iobuf_write_packet(out, message, msglen));
1218 return d0_iobuf_close(out, outbuflen);
1222 d0_iobuf_close(out, outbuflen);
1225 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_sign(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *message, size_t msglen, char *outbuf, size_t *outbuflen)
1227 return d0_blind_id_sign_with_private_id_sign_internal(ctx, is_first, send_modulus, 1, message, msglen, outbuf, outbuflen);
1229 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_sign_detached(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *message, size_t msglen, char *outbuf, size_t *outbuflen)
1231 return d0_blind_id_sign_with_private_id_sign_internal(ctx, is_first, send_modulus, 0, message, msglen, outbuf, outbuflen);
1234 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_verify_internal(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, D0_BOOL with_msg, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, D0_BOOL *status)
1236 d0_iobuf_t *in = NULL;
1237 d0_iobuf_t *conv = NULL;
1238 unsigned char *convbuf = NULL;
1239 unsigned char hashbuf[2048];
1242 USINGTEMPS(); // temps: 0 sig^e 2 g^s 3 g^-s 4 order
1245 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
1247 REPLACING(schnorr_G);
1253 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
1256 USING(rsa_e); USING(rsa_n);
1258 in = d0_iobuf_open_read(inbuf, inbuflen);
1264 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
1265 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
1266 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
1268 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
1269 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
1270 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
1271 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
1272 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
1273 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
1275 // check signature of key (t = k^d, so, t^challenge = k)
1277 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
1279 // we will actually sign SHA(4^s) to prevent a malleability attack!
1280 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1281 if(sz > sizeof(hashbuf))
1282 sz = sizeof(hashbuf);
1283 CHECK(d0_longhash_bignum(ctx->schnorr_g_to_s, hashbuf, sz));
1284 CHECK(d0_bignum_import_unsigned(temp2, hashbuf, sz));
1286 // + 7 / 8 is too large, so let's mod it
1287 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1290 if(d0_bignum_cmp(temp0, temp1))
1292 // accept the key anyway, but mark as failed signature! will later return 0 in status
1293 CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
1297 CHECK(d0_dl_get_order(temp4, ctx->schnorr_G));
1298 CHECK(d0_iobuf_read_bignum(in, temp0)); // e == H(m || g^r)
1299 CHECK(d0_iobuf_read_bignum(in, temp1)); // x == (r - s*e) mod |G|
1301 CHECK(d0_iobuf_read_packet(in, msg, msglen));
1303 // VERIFY: g^x * (g^s)^-e = g^(x - s*e) = g^r
1305 // verify schnorr ID scheme
1306 // we need g^r = g^x (g^s)^e
1307 CHECK(d0_bignum_mod_pow(temp2, four, temp1, ctx->schnorr_G));
1308 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1309 CHECK(d0_bignum_mod_inv(temp3, ctx->schnorr_g_to_s, ctx->schnorr_G));
1310 CHECK(d0_bignum_mod_pow(temp1, temp3, temp0, ctx->schnorr_G));
1312 CHECK(d0_bignum_mod_pow(temp1, ctx->schnorr_g_to_s, temp0, ctx->schnorr_G));
1314 CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G)); // temp3 now is g^r
1316 // hash it, hash it, everybody hash it
1317 conv = d0_iobuf_open_write_p((void **) &convbuf, 0);
1318 CHECK(d0_iobuf_write_packet(conv, msg, *msglen));
1319 CHECK(d0_iobuf_write_bignum(conv, temp3));
1320 d0_iobuf_close(conv, &sz);
1322 CHECK(d0_longhash_destructive(convbuf, sz, hashbuf, (d0_bignum_size(temp4) + 7) / 8));
1325 CHECK(d0_bignum_import_unsigned(temp1, hashbuf, (d0_bignum_size(temp4) + 7) / 8));
1328 CHECK(!d0_bignum_cmp(temp0, temp1));
1332 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
1334 d0_iobuf_close(in, NULL);
1339 d0_iobuf_close(in, NULL);
1342 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_verify(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, D0_BOOL *status)
1344 return d0_blind_id_sign_with_private_id_verify_internal(ctx, is_first, recv_modulus, 1, inbuf, inbuflen, msg, msglen, status);
1346 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_verify_detached(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, const char *inbuf, size_t inbuflen, const char *msg, size_t msglen, D0_BOOL *status)
1348 return d0_blind_id_sign_with_private_id_verify_internal(ctx, is_first, recv_modulus, 0, inbuf, inbuflen, (char *) msg, &msglen, status);
1351 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1353 d0_iobuf_t *out = NULL;
1354 unsigned char convbuf[1024];
1355 d0_iobuf_t *conv = NULL;
1361 USING(schnorr_g_to_s);
1363 out = d0_iobuf_open_write(outbuf, *outbuflen);
1364 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1366 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
1367 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
1368 CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_g_to_s));
1369 CHECK(d0_iobuf_close(conv, &sz));
1372 n = (*outbuflen / 4) * 3;
1373 if(n > SHA_DIGESTSIZE)
1375 CHECK(d0_iobuf_write_raw(out, sha(shabuf, convbuf, sz), n) == n);
1376 CHECK(d0_iobuf_conv_base64_out(out));
1378 return d0_iobuf_close(out, outbuflen);
1382 d0_iobuf_close(conv, &sz);
1383 d0_iobuf_close(out, outbuflen);
1387 D0_BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1391 USINGTEMPS(); // temps: temp0 result
1392 USING(t); USING(other_g_to_t); USING(schnorr_G);
1395 CHECK(d0_bignum_mod_pow(temp0, ctx->other_g_to_t, ctx->t, ctx->schnorr_G));
1396 ret = d0_longhash_bignum(temp0, (unsigned char *) outbuf, *outbuflen);
1405 d0_blind_id_t *d0_blind_id_new(void)
1407 d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
1408 memset(b, 0, sizeof(*b));
1412 void d0_blind_id_free(d0_blind_id_t *a)
1414 d0_blind_id_clear(a);
1418 void d0_blind_id_util_sha256(char *out, const char *in, size_t n)
1421 SHA256_Init(&context);
1422 SHA256_Update(&context, (const unsigned char *) in, n);
1423 return SHA256_Final((unsigned char *) out, &context);
1426 void d0_blind_id_setmallocfuncs(d0_malloc_t *m, d0_free_t *f)
1428 d0_setmallocfuncs(m, f);
1430 void d0_blind_id_setmutexfuncs(d0_createmutex_t *c, d0_destroymutex_t *d, d0_lockmutex_t *l, d0_unlockmutex_t *u)
1432 d0_setmutexfuncs(c, d, l, u);