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 char *sha(const unsigned char *in, size_t len)
50 static __thread char h[32];
51 d0_blind_id_util_sha256(h, (const char *) in, len);
55 // for zero knowledge, we need multiple instances of schnorr ID scheme... should normally be sequential
56 // parallel schnorr ID is not provably zero knowledge :(
57 // (evil verifier can know all questions in advance, so sequential is disadvantage for him)
58 // we'll just live with a 1:1048576 chance of cheating, and support reauthenticating
60 #define SCHNORR_BITS 20
61 // probability of cheat: 2^(-bits+1)
63 #define SCHNORR_HASHSIZE SHA_DIGESTSIZE
64 // cannot be >= SHA_DIGESTSIZE
65 // *8 must be >= SCHNORR_BITS
66 // no need to save bits here
68 #define MSGSIZE 640 // ought to be enough for anyone
72 // signing (Xonotic pub and priv key)
73 d0_bignum_t *rsa_n, *rsa_e, *rsa_d;
75 // public data (Schnorr ID)
76 d0_bignum_t *schnorr_G;
78 // private data (player ID private key)
79 d0_bignum_t *schnorr_s;
81 // public data (player ID public key, this is what the server gets to know)
82 d0_bignum_t *schnorr_g_to_s;
83 d0_bignum_t *schnorr_H_g_to_s_signature; // 0 when signature is invalid
84 // as hash function H, we get the SHA1 and reinterpret as bignum - yes, it always is < 160 bits
87 d0_bignum_t *rsa_blind_signature_camouflage; // random number blind signature
89 d0_bignum_t *r; // random number for schnorr ID
90 d0_bignum_t *t; // for DH key exchange
91 d0_bignum_t *g_to_t; // for DH key exchange
92 d0_bignum_t *other_g_to_t; // for DH key exchange
93 d0_bignum_t *challenge; // challenge
95 char msghash[SCHNORR_HASHSIZE]; // init hash
96 char msg[MSGSIZE]; // message
97 size_t msglen; // message length
102 #define CHECK(x) do { if(!(x)) { fprintf(stderr, "CHECK FAILED (%s:%d): %s\n", __FILE__, __LINE__, #x); goto fail; } } while(0)
103 #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)
105 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
106 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
109 #define USING(x) if(!(ctx->x)) return 0
113 static d0_bignum_t *zero, *one, *four;
115 static d0_bignum_t *temp0, *temp1, *temp2, *temp3, *temp4;
116 static void *tempmutex = NULL; // hold this mutex when using temp0 to temp4
117 #define USINGTEMPS() int locked = 0
118 #define LOCKTEMPS() do { if(!locked) d0_lockmutex(tempmutex); locked = 1; } while(0)
119 #define UNLOCKTEMPS() do { if(locked) d0_unlockmutex(tempmutex); locked = 0; } while(0);
121 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 static D0_BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
186 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
187 if(d0_bignum_isprime(temp0, 0) == 0)
189 CHECK(d0_dl_get_from_order(G, temp0));
190 if(d0_bignum_isprime(G, 10) == 0)
192 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
201 static D0_BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *challenge, d0_bignum_t *d, d0_bignum_t *n)
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, challenge));
219 if(!d0_bignum_cmp(temp4, one))
228 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
229 if(!d0_bignum_cmp(temp1, temp0))
235 if(d0_bignum_isprime(temp1, 10) == 0)
237 CHECK(d0_bignum_sub(temp3, temp1, one));
238 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, challenge));
239 if(!d0_bignum_cmp(temp4, one))
247 CHECK(d0_bignum_mul(n, temp0, temp1));
249 // d = challenge^-1 mod (temp0-1)(temp1-1)
250 CHECK(d0_bignum_mul(temp0, temp2, temp3));
251 CHECK(d0_bignum_mod_inv(d, challenge, temp0));
257 static D0_BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass)
259 // uses temp0 to temp4
262 int pb = (size + 1)/2;
270 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
271 if(d0_bignum_isprime(temp0, 10) == 0)
273 CHECK(d0_bignum_sub(temp2, temp0, one));
274 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
275 if(!d0_bignum_cmp(temp4, one))
284 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
285 if(!d0_bignum_cmp(temp1, temp0))
293 CHECK(d0_bignum_mul(ctx->rsa_n, temp0, temp1));
294 if(reject(ctx, pass))
297 if(d0_bignum_isprime(temp1, 10) == 0)
299 CHECK(d0_bignum_sub(temp3, temp1, one));
300 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
301 if(!d0_bignum_cmp(temp4, one))
308 // ctx->rsa_d = ctx->rsa_e^-1 mod (temp0-1)(temp1-1)
309 CHECK(d0_bignum_mul(temp0, temp2, temp3));
310 CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp0));
316 D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_destructive(unsigned char *convbuf, size_t sz, unsigned char *outbuf, size_t outbuflen)
321 while(n > SHA_DIGESTSIZE)
323 memcpy(outbuf, sha(convbuf, sz), SHA_DIGESTSIZE);
324 outbuf += SHA_DIGESTSIZE;
326 for(i = 0; i < sz; ++i)
328 break; // stop until no carry
330 memcpy(outbuf, sha(convbuf, sz), n);
334 D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_bignum(const d0_bignum_t *in, unsigned char *outbuf, size_t outbuflen)
336 static __thread unsigned char convbuf[1024];
339 CHECK(d0_bignum_export_unsigned(in, convbuf, sizeof(convbuf)) >= 0);
340 sz = (d0_bignum_size(in) + 7) / 8;
341 CHECK(d0_longhash_destructive(convbuf, sz, outbuf, outbuflen));
348 void d0_blind_id_clear(d0_blind_id_t *ctx)
350 if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
351 if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
352 if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
353 if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
354 if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
355 if(ctx->schnorr_g_to_s) d0_bignum_free(ctx->schnorr_g_to_s);
356 if(ctx->schnorr_H_g_to_s_signature) d0_bignum_free(ctx->schnorr_H_g_to_s_signature);
357 if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
358 if(ctx->r) d0_bignum_free(ctx->r);
359 if(ctx->challenge) d0_bignum_free(ctx->challenge);
360 if(ctx->t) d0_bignum_free(ctx->t);
361 if(ctx->g_to_t) d0_bignum_free(ctx->g_to_t);
362 if(ctx->other_g_to_t) d0_bignum_free(ctx->other_g_to_t);
363 memset(ctx, 0, sizeof(*ctx));
366 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
368 d0_blind_id_clear(ctx);
369 if(src->rsa_n) CHECK_ASSIGN(ctx->rsa_n, d0_bignum_mov(NULL, src->rsa_n));
370 if(src->rsa_e) CHECK_ASSIGN(ctx->rsa_e, d0_bignum_mov(NULL, src->rsa_e));
371 if(src->rsa_d) CHECK_ASSIGN(ctx->rsa_d, d0_bignum_mov(NULL, src->rsa_d));
372 if(src->schnorr_G) CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_mov(NULL, src->schnorr_G));
373 if(src->schnorr_s) CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_mov(NULL, src->schnorr_s));
374 if(src->schnorr_g_to_s) CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mov(NULL, src->schnorr_g_to_s));
375 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));
376 if(src->rsa_blind_signature_camouflage) CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage));
377 if(src->r) CHECK_ASSIGN(ctx->r, d0_bignum_mov(NULL, src->r));
378 if(src->challenge) CHECK_ASSIGN(ctx->challenge, d0_bignum_mov(NULL, src->challenge));
379 if(src->t) CHECK_ASSIGN(ctx->t, d0_bignum_mov(NULL, src->t));
380 if(src->g_to_t) CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mov(NULL, src->g_to_t));
381 if(src->other_g_to_t) CHECK_ASSIGN(ctx->other_g_to_t, d0_bignum_mov(NULL, src->other_g_to_t));
382 memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
383 ctx->msglen = src->msglen;
384 memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
387 d0_blind_id_clear(ctx);
391 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)
394 REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
396 CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
397 CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
398 CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
401 CHECK(d0_rsa_generate_key_fastreject(k+1, reject, ctx, pass)); // must fit G for sure
403 CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
411 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
413 return d0_blind_id_generate_private_key_fastreject(ctx, k, NULL, NULL);
416 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
418 d0_iobuf_t *in = NULL;
420 REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
422 in = d0_iobuf_open_read(inbuf, inbuflen);
424 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
425 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
426 CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
427 return d0_iobuf_close(in, NULL);
430 d0_iobuf_close(in, NULL);
434 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
436 d0_iobuf_t *in = NULL;
438 REPLACING(rsa_n); REPLACING(rsa_e);
440 in = d0_iobuf_open_read(inbuf, inbuflen);
441 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
442 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
443 return d0_iobuf_close(in, NULL);
446 d0_iobuf_close(in, NULL);
450 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
452 d0_iobuf_t *out = NULL;
454 USING(rsa_n); USING(rsa_e); USING(rsa_d);
456 out = d0_iobuf_open_write(outbuf, *outbuflen);
457 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
458 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
459 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
460 return d0_iobuf_close(out, outbuflen);
463 d0_iobuf_close(out, outbuflen);
467 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
469 d0_iobuf_t *out = NULL;
471 USING(rsa_n); USING(rsa_e);
473 out = d0_iobuf_open_write(outbuf, *outbuflen);
474 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
475 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
476 return d0_iobuf_close(out, outbuflen);
479 if(!d0_iobuf_close(out, outbuflen))
484 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
486 d0_iobuf_t *out = NULL;
487 static __thread unsigned char convbuf[2048];
488 d0_iobuf_t *conv = NULL;
491 USING(rsa_n); USING(rsa_e);
493 out = d0_iobuf_open_write(outbuf, *outbuflen);
494 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
496 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
497 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
498 CHECK(d0_iobuf_close(conv, &sz));
501 n = (*outbuflen / 4) * 3;
502 if(n > SHA_DIGESTSIZE)
504 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
505 CHECK(d0_iobuf_conv_base64_out(out));
507 return d0_iobuf_close(out, outbuflen);
511 d0_iobuf_close(conv, &sz);
512 d0_iobuf_close(out, outbuflen);
516 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
520 REPLACING(schnorr_G);
522 CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
524 CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
532 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
534 d0_iobuf_t *in = NULL;
536 REPLACING(schnorr_G);
538 in = d0_iobuf_open_read(inbuf, inbuflen);
539 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
540 return d0_iobuf_close(in, NULL);
543 d0_iobuf_close(in, NULL);
547 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
549 d0_iobuf_t *out = NULL;
553 out = d0_iobuf_open_write(outbuf, *outbuflen);
554 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
555 return d0_iobuf_close(out, outbuflen);
558 d0_iobuf_close(out, outbuflen);
562 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
564 USINGTEMPS(); // temps: temp0 = order
566 REPLACING(schnorr_s); REPLACING(schnorr_g_to_s);
569 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
570 CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
571 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mod_pow(ctx->schnorr_g_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
572 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
581 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
583 d0_iobuf_t *out = NULL;
584 static __thread unsigned char shabuf[2048];
587 USINGTEMPS(); // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
588 USING(rsa_n); USING(rsa_e); USING(schnorr_g_to_s);
589 REPLACING(rsa_blind_signature_camouflage);
591 out = d0_iobuf_open_write(outbuf, *outbuflen);
593 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
594 CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
596 // we will actually sign HA(4^s) to prevent a malleability attack!
598 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
599 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
600 if(sz > sizeof(shabuf))
602 CHECK(d0_longhash_bignum(temp2, shabuf, sz));
603 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
606 CHECK(d0_bignum_mod_mul(temp1, temp2, temp0, ctx->rsa_n));
607 CHECK(d0_iobuf_write_bignum(out, temp1));
609 return d0_iobuf_close(out, outbuflen);
613 d0_iobuf_close(out, outbuflen);
617 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)
619 d0_iobuf_t *in = NULL;
620 d0_iobuf_t *out = NULL;
622 USINGTEMPS(); // temps: temp0 input, temp1 temp0^d
623 USING(rsa_d); USING(rsa_n);
625 in = d0_iobuf_open_read(inbuf, inbuflen);
626 out = d0_iobuf_open_write(outbuf, *outbuflen);
629 CHECK(d0_iobuf_read_bignum(in, temp0));
630 CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
631 CHECK(d0_iobuf_write_bignum(out, temp1));
634 d0_iobuf_close(in, NULL);
635 return d0_iobuf_close(out, outbuflen);
639 d0_iobuf_close(in, NULL);
640 d0_iobuf_close(out, outbuflen);
644 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
646 d0_iobuf_t *in = NULL;
648 USINGTEMPS(); // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
649 USING(rsa_blind_signature_camouflage); USING(rsa_n);
650 REPLACING(schnorr_H_g_to_s_signature);
652 in = d0_iobuf_open_read(inbuf, inbuflen);
656 CHECK(d0_iobuf_read_bignum(in, temp0));
657 CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
658 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));
661 return d0_iobuf_close(in, NULL);
665 d0_iobuf_close(in, NULL);
669 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)
671 d0_iobuf_t *in = NULL;
673 REPLACING(rsa_blind_signature_camouflage);
675 in = d0_iobuf_open_read(inbuf, inbuflen);
677 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_iobuf_read_bignum(in, ctx->rsa_blind_signature_camouflage));
679 return d0_iobuf_close(in, NULL);
682 d0_iobuf_close(in, NULL);
686 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)
688 d0_iobuf_t *out = NULL;
690 USING(rsa_blind_signature_camouflage);
692 out = d0_iobuf_open_write(outbuf, *outbuflen);
694 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_blind_signature_camouflage));
696 return d0_iobuf_close(out, outbuflen);
699 d0_iobuf_close(out, outbuflen);
703 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
705 d0_iobuf_t *in = NULL;
707 REPLACING(schnorr_s); REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
709 in = d0_iobuf_open_read(inbuf, inbuflen);
711 CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
712 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
713 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
715 return d0_iobuf_close(in, NULL);
718 d0_iobuf_close(in, NULL);
722 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
724 d0_iobuf_t *in = NULL;
726 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
728 in = d0_iobuf_open_read(inbuf, inbuflen);
730 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
731 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
733 return d0_iobuf_close(in, NULL);
736 d0_iobuf_close(in, NULL);
740 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
742 d0_iobuf_t *out = NULL;
744 USING(schnorr_s); USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
746 out = d0_iobuf_open_write(outbuf, *outbuflen);
748 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
749 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
750 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
752 return d0_iobuf_close(out, outbuflen);
755 d0_iobuf_close(out, outbuflen);
759 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
761 d0_iobuf_t *out = NULL;
763 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
765 out = d0_iobuf_open_write(outbuf, *outbuflen);
767 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
768 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
770 return d0_iobuf_close(out, outbuflen);
773 d0_iobuf_close(out, outbuflen);
777 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)
779 // first run: send 4^s, 4^s signature
780 // 1. get random r, send HASH(4^r)
782 d0_iobuf_t *out = NULL;
783 static __thread unsigned char convbuf[1024];
784 d0_iobuf_t *conv = NULL;
788 USINGTEMPS(); // temps: temp0 order, temp0 4^r
791 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
794 REPLACING(r); REPLACING(t); REPLACING(g_to_t);
796 out = d0_iobuf_open_write(outbuf, *outbuflen);
802 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
803 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
804 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
807 // start schnorr ID scheme
808 // generate random number r; x = g^r; send hash of x, remember r, forget x
810 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
812 CHECK_ASSIGN(ctx->r, d0_bignum_int(ctx->r, 4)); // decided by fair dice roll
814 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
817 // initialize Signed Diffie Hellmann
818 // we already have the group order in temp1
820 CHECK_ASSIGN(ctx->t, d0_bignum_int(ctx->t, 4)); // decided by fair dice roll
822 CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
824 // can we SOMEHOW do this with just one mod_pow?
826 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
827 CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mod_pow(ctx->g_to_t, four, ctx->t, ctx->schnorr_G));
830 // hash it, hash it, everybody hash it
831 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
832 CHECK(d0_iobuf_write_bignum(conv, temp0));
833 CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
834 CHECK(d0_iobuf_write_packet(conv, msg, msglen));
835 CHECK(d0_iobuf_write_bignum(conv, temp0));
837 CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
838 d0_iobuf_close(conv, &sz);
840 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
841 CHECK(d0_iobuf_write_packet(out, msg, msglen));
843 return d0_iobuf_close(out, outbuflen);
847 d0_iobuf_close(out, outbuflen);
851 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)
852 // first run: get 4^s, 4^s signature
855 // 3. send challenge challenge of SCHNORR_BITS
857 d0_iobuf_t *in = NULL;
858 d0_iobuf_t *out = NULL;
859 static __thread unsigned char shabuf[2048];
862 USINGTEMPS(); // temps: temp0 order, temp0 signature check
865 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
867 REPLACING(schnorr_G);
873 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
876 USING(rsa_e); USING(rsa_n);
877 REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r); REPLACING(t);
879 in = d0_iobuf_open_read(inbuf, inbuflen);
880 out = d0_iobuf_open_write(outbuf, *outbuflen);
886 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
887 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
888 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
890 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
891 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
892 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
893 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
894 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
895 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
897 // check signature of key (t = k^d, so, t^challenge = k)
899 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
901 // we will actually sign SHA(4^s) to prevent a malleability attack!
902 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
903 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
904 if(sz > sizeof(shabuf))
906 CHECK(d0_longhash_bignum(temp2, shabuf, sz));
907 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
909 // + 7 / 8 is too large, so let's mod it
910 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
913 if(d0_bignum_cmp(temp0, temp1))
915 // accept the key anyway, but mark as failed signature! will later return 0 in status
916 CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
920 CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
921 ctx->msglen = MSGSIZE;
922 CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
926 CHECK_ASSIGN(ctx->challenge, d0_bignum_int(ctx->challenge, 4)); // decided by fair dice roll
928 CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
930 CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
932 // Diffie Hellmann send
934 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
936 CHECK_ASSIGN(ctx->t, d0_bignum_int(ctx->t, 4)); // decided by fair dice roll
938 CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
940 CHECK(d0_bignum_mod_pow(temp0, four, ctx->t, ctx->schnorr_G));
941 CHECK(d0_iobuf_write_bignum(out, temp0));
945 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
947 d0_iobuf_close(in, NULL);
948 return d0_iobuf_close(out, outbuflen);
952 d0_iobuf_close(in, NULL);
953 d0_iobuf_close(out, outbuflen);
957 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)
958 // 1. read challenge challenge of SCHNORR_BITS
959 // 2. reply with r + s * challenge mod order
961 d0_iobuf_t *in = NULL;
962 d0_iobuf_t *out = NULL;
964 USINGTEMPS(); // temps: 0 order, 1 prod, 2 y, 3 challenge
965 REPLACING(other_g_to_t); REPLACING(t);
966 USING(schnorr_G); USING(schnorr_s); USING(r); USING(g_to_t);
968 in = d0_iobuf_open_read(inbuf, inbuflen);
969 out = d0_iobuf_open_write(outbuf, *outbuflen);
972 CHECK(d0_iobuf_read_bignum(in, temp3));
973 CHECK(d0_bignum_cmp(temp3, zero) >= 0);
974 CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
976 // send response for schnorr ID scheme
977 // i.challenge. r + ctx->schnorr_s * temp3
978 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
979 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
980 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
981 CHECK(d0_bignum_mod_add(temp2, ctx->r, temp1, temp0));
983 CHECK(d0_bignum_mod_sub(temp2, ctx->r, temp1, temp0));
985 CHECK(d0_iobuf_write_bignum(out, temp2));
988 // Diffie Hellmann recv
989 CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
990 CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
991 CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
992 // Diffie Hellmann send
993 CHECK(d0_iobuf_write_bignum(out, ctx->g_to_t));
995 d0_iobuf_close(in, NULL);
996 return d0_iobuf_close(out, outbuflen);
1000 d0_iobuf_close(in, NULL);
1001 d0_iobuf_close(out, outbuflen);
1005 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)
1006 // 1. read y = r + s * challenge mod order
1007 // 2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
1008 // (check using H(g^r) which we know)
1010 d0_iobuf_t *in = NULL;
1011 static __thread unsigned char convbuf[1024];
1012 d0_iobuf_t *conv = NULL;
1015 USINGTEMPS(); // temps: 0 y 1 order
1016 USING(challenge); USING(schnorr_G);
1017 REPLACING(other_g_to_t);
1019 in = d0_iobuf_open_read(inbuf, inbuflen);
1023 CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
1024 CHECK(d0_iobuf_read_bignum(in, temp0));
1025 CHECK(d0_bignum_cmp(temp0, zero) >= 0);
1026 CHECK(d0_bignum_cmp(temp0, temp1) < 0);
1028 // verify schnorr ID scheme
1029 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1030 // we need 4^r = 4^temp0 (g^s)^-challenge
1031 CHECK(d0_bignum_mod_inv(temp1, ctx->schnorr_g_to_s, ctx->schnorr_G));
1032 CHECK(d0_bignum_mod_pow(temp2, temp1, ctx->challenge, ctx->schnorr_G));
1034 // we need 4^r = 4^temp0 (g^s)^challenge
1035 CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_g_to_s, ctx->challenge, ctx->schnorr_G));
1037 CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
1038 CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G));
1040 // Diffie Hellmann recv
1041 CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
1042 CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
1043 CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
1045 // hash it, hash it, everybody hash it
1046 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1047 CHECK(d0_iobuf_write_bignum(conv, temp3));
1048 CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
1049 CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
1050 CHECK(d0_iobuf_write_bignum(conv, temp3));
1052 CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
1053 d0_iobuf_close(conv, &sz);
1055 if(memcmp(sha(convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
1057 // FAIL (not owned by player)
1062 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
1064 if(ctx->msglen <= *msglen)
1065 memcpy(msg, ctx->msg, ctx->msglen);
1067 memcpy(msg, ctx->msg, *msglen);
1068 *msglen = ctx->msglen;
1070 d0_iobuf_close(in, NULL);
1075 d0_iobuf_close(in, NULL);
1079 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_generate_missing_signature(d0_blind_id_t *ctx)
1082 static __thread unsigned char shabuf[2048];
1084 USINGTEMPS(); // temps: 2 hash
1085 REPLACING(schnorr_H_g_to_s_signature);
1086 USING(schnorr_g_to_s); USING(rsa_d); USING(rsa_n);
1090 // we will actually sign SHA(4^s) to prevent a malleability attack!
1091 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1092 if(sz > sizeof(shabuf))
1093 sz = sizeof(shabuf);
1094 CHECK(d0_longhash_bignum(ctx->schnorr_g_to_s, shabuf, sz));
1096 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
1098 // + 7 / 8 is too large, so let's mod it
1099 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1100 CHECK(d0_bignum_mod_pow(ctx->schnorr_H_g_to_s_signature, temp1, ctx->rsa_d, ctx->rsa_n));
1110 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)
1112 d0_iobuf_t *out = NULL;
1113 unsigned char *convbuf = NULL;
1114 static __thread unsigned char shabuf[2048];
1115 d0_iobuf_t *conv = NULL;
1118 USINGTEMPS(); // temps: 0 order 1 4^r 2 hash
1121 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
1127 out = d0_iobuf_open_write(outbuf, *outbuflen);
1133 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
1134 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
1135 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
1138 // start schnorr SIGNATURE scheme
1139 // generate random number r; x = g^r; send hash of H(m||r), remember r, forget x
1141 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
1142 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
1143 CHECK(d0_bignum_mod_pow(temp1, four, ctx->r, ctx->schnorr_G));
1145 // hash it, hash it, everybody hash it
1146 conv = d0_iobuf_open_write_p((void **) &convbuf, 0);
1147 CHECK(d0_iobuf_write_packet(conv, message, msglen));
1148 CHECK(d0_iobuf_write_bignum(conv, temp1));
1149 d0_iobuf_close(conv, &sz);
1151 CHECK(d0_longhash_destructive(convbuf, sz, shabuf, (d0_bignum_size(temp0) + 7) / 8));
1154 CHECK(d0_bignum_import_unsigned(temp2, shabuf, (d0_bignum_size(temp0) + 7) / 8));
1155 CHECK(d0_iobuf_write_bignum(out, temp2));
1157 // multiply with secret, sub k, modulo order
1158 CHECK(d0_bignum_mod_mul(temp1, temp2, ctx->schnorr_s, temp0));
1159 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1160 CHECK(d0_bignum_mod_add(temp2, ctx->r, temp1, temp0));
1162 CHECK(d0_bignum_mod_sub(temp2, ctx->r, temp1, temp0));
1164 CHECK(d0_iobuf_write_bignum(out, temp2));
1167 // write the message itself
1169 CHECK(d0_iobuf_write_packet(out, message, msglen));
1171 return d0_iobuf_close(out, outbuflen);
1175 d0_iobuf_close(out, outbuflen);
1178 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)
1180 return d0_blind_id_sign_with_private_id_sign_internal(ctx, is_first, send_modulus, 1, message, msglen, outbuf, outbuflen);
1182 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)
1184 return d0_blind_id_sign_with_private_id_sign_internal(ctx, is_first, send_modulus, 0, message, msglen, outbuf, outbuflen);
1187 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)
1189 d0_iobuf_t *in = NULL;
1190 d0_iobuf_t *conv = NULL;
1191 unsigned char *convbuf = NULL;
1192 static __thread unsigned char shabuf[2048];
1195 USINGTEMPS(); // temps: 0 sig^e 2 g^s 3 g^-s 4 order
1198 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
1200 REPLACING(schnorr_G);
1206 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
1209 USING(rsa_e); USING(rsa_n);
1211 in = d0_iobuf_open_read(inbuf, inbuflen);
1217 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
1218 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
1219 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
1221 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
1222 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
1223 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
1224 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
1225 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
1226 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
1228 // check signature of key (t = k^d, so, t^challenge = k)
1230 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
1232 // we will actually sign SHA(4^s) to prevent a malleability attack!
1233 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1234 if(sz > sizeof(shabuf))
1235 sz = sizeof(shabuf);
1236 CHECK(d0_longhash_bignum(ctx->schnorr_g_to_s, shabuf, sz));
1237 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
1239 // + 7 / 8 is too large, so let's mod it
1240 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1243 if(d0_bignum_cmp(temp0, temp1))
1245 // accept the key anyway, but mark as failed signature! will later return 0 in status
1246 CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
1250 CHECK(d0_dl_get_order(temp4, ctx->schnorr_G));
1251 CHECK(d0_iobuf_read_bignum(in, temp0)); // e == H(m || g^r)
1252 CHECK(d0_iobuf_read_bignum(in, temp1)); // x == (r - s*e) mod |G|
1254 CHECK(d0_iobuf_read_packet(in, msg, msglen));
1256 // VERIFY: g^x * (g^s)^-e = g^(x - s*e) = g^r
1258 // verify schnorr ID scheme
1259 // we need g^r = g^x (g^s)^e
1260 CHECK(d0_bignum_mod_pow(temp2, four, temp1, ctx->schnorr_G));
1261 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1262 CHECK(d0_bignum_mod_inv(temp3, ctx->schnorr_g_to_s, ctx->schnorr_G));
1263 CHECK(d0_bignum_mod_pow(temp1, temp3, temp0, ctx->schnorr_G));
1265 CHECK(d0_bignum_mod_pow(temp1, ctx->schnorr_g_to_s, temp0, ctx->schnorr_G));
1267 CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G)); // temp3 now is g^r
1269 // hash it, hash it, everybody hash it
1270 conv = d0_iobuf_open_write_p((void **) &convbuf, 0);
1271 CHECK(d0_iobuf_write_packet(conv, msg, *msglen));
1272 CHECK(d0_iobuf_write_bignum(conv, temp3));
1273 d0_iobuf_close(conv, &sz);
1275 CHECK(d0_longhash_destructive(convbuf, sz, shabuf, (d0_bignum_size(temp4) + 7) / 8));
1278 CHECK(d0_bignum_import_unsigned(temp1, shabuf, (d0_bignum_size(temp4) + 7) / 8));
1281 CHECK(!d0_bignum_cmp(temp0, temp1));
1285 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
1287 d0_iobuf_close(in, NULL);
1292 d0_iobuf_close(in, NULL);
1295 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)
1297 return d0_blind_id_sign_with_private_id_verify_internal(ctx, is_first, recv_modulus, 1, inbuf, inbuflen, msg, msglen, status);
1299 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)
1301 return d0_blind_id_sign_with_private_id_verify_internal(ctx, is_first, recv_modulus, 0, inbuf, inbuflen, (char *) msg, &msglen, status);
1304 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1306 d0_iobuf_t *out = NULL;
1307 static __thread unsigned char convbuf[1024];
1308 d0_iobuf_t *conv = NULL;
1313 USING(schnorr_g_to_s);
1315 out = d0_iobuf_open_write(outbuf, *outbuflen);
1316 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1318 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
1319 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
1320 CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_g_to_s));
1321 CHECK(d0_iobuf_close(conv, &sz));
1324 n = (*outbuflen / 4) * 3;
1325 if(n > SHA_DIGESTSIZE)
1327 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
1328 CHECK(d0_iobuf_conv_base64_out(out));
1330 return d0_iobuf_close(out, outbuflen);
1334 d0_iobuf_close(conv, &sz);
1335 d0_iobuf_close(out, outbuflen);
1339 D0_BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1343 USINGTEMPS(); // temps: temp0 result
1344 USING(t); USING(other_g_to_t); USING(schnorr_G);
1347 CHECK(d0_bignum_mod_pow(temp0, ctx->other_g_to_t, ctx->t, ctx->schnorr_G));
1348 ret = d0_longhash_bignum(temp0, (unsigned char *) outbuf, *outbuflen);
1357 d0_blind_id_t *d0_blind_id_new(void)
1359 d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
1360 memset(b, 0, sizeof(*b));
1364 void d0_blind_id_free(d0_blind_id_t *a)
1366 d0_blind_id_clear(a);
1370 void d0_blind_id_util_sha256(char *out, const char *in, size_t n)
1373 SHA256_Init(&context);
1374 SHA256_Update(&context, (const unsigned char *) in, n);
1375 return SHA256_Final((unsigned char *) out, &context);