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)
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
103 #define CHECK(x) do { if(!(x)) { fprintf(stderr, "CHECK FAILED (%s:%d): %s\n", __FILE__, __LINE__, #x); goto fail; } } while(0)
104 #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)
106 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
107 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
110 #define USING(x) if(!(ctx->x)) return 0
113 static d0_bignum_t *zero, *one, *four, *temp0, *temp1, *temp2, *temp3, *temp4;
115 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_INITIALIZE(void)
117 CHECK(d0_bignum_INITIALIZE());
118 CHECK_ASSIGN(zero, d0_bignum_int(zero, 0));
119 CHECK_ASSIGN(one, d0_bignum_int(one, 1));
120 CHECK_ASSIGN(four, d0_bignum_int(four, 4));
121 CHECK_ASSIGN(temp0, d0_bignum_int(temp0, 0));
122 CHECK_ASSIGN(temp1, d0_bignum_int(temp1, 0));
123 CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0));
124 CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0));
125 CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0));
131 void d0_blind_id_SHUTDOWN(void)
133 d0_bignum_free(zero);
135 d0_bignum_free(four);
136 d0_bignum_free(temp0);
137 d0_bignum_free(temp1);
138 d0_bignum_free(temp2);
139 d0_bignum_free(temp3);
140 d0_bignum_free(temp4);
141 d0_bignum_SHUTDOWN();
145 d0_bignum_t *d0_dl_get_order(d0_bignum_t *o, const d0_bignum_t *G)
147 CHECK_ASSIGN(o, d0_bignum_sub(o, G, one));
148 CHECK(d0_bignum_shl(o, o, -1)); // order o = (G-1)/2
154 d0_bignum_t *d0_dl_get_from_order(d0_bignum_t *G, const d0_bignum_t *o)
156 CHECK_ASSIGN(G, d0_bignum_shl(G, o, 1));
157 CHECK(d0_bignum_add(G, G, one));
163 D0_BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
170 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
171 if(d0_bignum_isprime(temp0, 0) == 0)
173 CHECK(d0_dl_get_from_order(G, temp0));
174 if(d0_bignum_isprime(G, 10) == 0)
176 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
185 D0_BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *challenge, d0_bignum_t *d, d0_bignum_t *n)
187 // uses temp0 to temp4
190 int pb = (size + 1)/2;
198 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
199 if(d0_bignum_isprime(temp0, 10) == 0)
201 CHECK(d0_bignum_sub(temp2, temp0, one));
202 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, challenge));
203 if(!d0_bignum_cmp(temp4, one))
212 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
213 if(!d0_bignum_cmp(temp1, temp0))
219 if(d0_bignum_isprime(temp1, 10) == 0)
221 CHECK(d0_bignum_sub(temp3, temp1, one));
222 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, challenge));
223 if(!d0_bignum_cmp(temp4, one))
231 CHECK(d0_bignum_mul(n, temp0, temp1));
233 // d = challenge^-1 mod (temp0-1)(temp1-1)
234 CHECK(d0_bignum_mul(temp0, temp2, temp3));
235 CHECK(d0_bignum_mod_inv(d, challenge, temp0));
241 D0_BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass)
243 // uses temp0 to temp4
246 int pb = (size + 1)/2;
254 CHECK(d0_bignum_rand_bit_exact(temp0, pb));
255 if(d0_bignum_isprime(temp0, 10) == 0)
257 CHECK(d0_bignum_sub(temp2, temp0, one));
258 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
259 if(!d0_bignum_cmp(temp4, one))
268 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
269 if(!d0_bignum_cmp(temp1, temp0))
277 CHECK(d0_bignum_mul(ctx->rsa_n, temp0, temp1));
278 if(reject(ctx, pass))
281 if(d0_bignum_isprime(temp1, 10) == 0)
283 CHECK(d0_bignum_sub(temp3, temp1, one));
284 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
285 if(!d0_bignum_cmp(temp4, one))
292 // ctx->rsa_d = ctx->rsa_e^-1 mod (temp0-1)(temp1-1)
293 CHECK(d0_bignum_mul(temp0, temp2, temp3));
294 CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp0));
300 D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_destructive(unsigned char *convbuf, size_t sz, unsigned char *outbuf, size_t outbuflen)
305 while(n > SHA_DIGESTSIZE)
307 memcpy(outbuf, sha(convbuf, sz), SHA_DIGESTSIZE);
308 outbuf += SHA_DIGESTSIZE;
310 for(i = 0; i < sz; ++i)
312 break; // stop until no carry
314 memcpy(outbuf, sha(convbuf, sz), n);
318 D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_bignum(const d0_bignum_t *in, unsigned char *outbuf, size_t outbuflen)
320 static unsigned char convbuf[1024];
323 CHECK(d0_bignum_export_unsigned(in, convbuf, sizeof(convbuf)) >= 0);
324 sz = (d0_bignum_size(in) + 7) / 8;
325 CHECK(d0_longhash_destructive(convbuf, sz, outbuf, outbuflen));
332 void d0_blind_id_clear(d0_blind_id_t *ctx)
334 if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
335 if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
336 if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
337 if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
338 if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
339 if(ctx->schnorr_g_to_s) d0_bignum_free(ctx->schnorr_g_to_s);
340 if(ctx->schnorr_H_g_to_s_signature) d0_bignum_free(ctx->schnorr_H_g_to_s_signature);
341 if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
342 if(ctx->r) d0_bignum_free(ctx->r);
343 if(ctx->challenge) d0_bignum_free(ctx->challenge);
344 if(ctx->t) d0_bignum_free(ctx->t);
345 if(ctx->g_to_t) d0_bignum_free(ctx->g_to_t);
346 if(ctx->other_g_to_t) d0_bignum_free(ctx->other_g_to_t);
347 memset(ctx, 0, sizeof(*ctx));
350 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
352 d0_blind_id_clear(ctx);
353 if(src->rsa_n) CHECK_ASSIGN(ctx->rsa_n, d0_bignum_mov(NULL, src->rsa_n));
354 if(src->rsa_e) CHECK_ASSIGN(ctx->rsa_e, d0_bignum_mov(NULL, src->rsa_e));
355 if(src->rsa_d) CHECK_ASSIGN(ctx->rsa_d, d0_bignum_mov(NULL, src->rsa_d));
356 if(src->schnorr_G) CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_mov(NULL, src->schnorr_G));
357 if(src->schnorr_s) CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_mov(NULL, src->schnorr_s));
358 if(src->schnorr_g_to_s) CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mov(NULL, src->schnorr_g_to_s));
359 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));
360 if(src->rsa_blind_signature_camouflage) CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage));
361 if(src->r) CHECK_ASSIGN(ctx->r, d0_bignum_mov(NULL, src->r));
362 if(src->challenge) CHECK_ASSIGN(ctx->challenge, d0_bignum_mov(NULL, src->challenge));
363 if(src->t) CHECK_ASSIGN(ctx->t, d0_bignum_mov(NULL, src->t));
364 if(src->g_to_t) CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mov(NULL, src->g_to_t));
365 if(src->other_g_to_t) CHECK_ASSIGN(ctx->other_g_to_t, d0_bignum_mov(NULL, src->other_g_to_t));
366 memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
367 ctx->msglen = src->msglen;
368 memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
371 d0_blind_id_clear(ctx);
375 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)
377 REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
379 CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
380 CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
381 CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
383 CHECK(d0_rsa_generate_key_fastreject(k+1, reject, ctx, pass)); // must fit G for sure
385 CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure
391 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
393 return d0_blind_id_generate_private_key_fastreject(ctx, k, NULL, NULL);
396 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
398 d0_iobuf_t *in = NULL;
400 REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
402 in = d0_iobuf_open_read(inbuf, inbuflen);
404 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
405 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
406 CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
407 return d0_iobuf_close(in, NULL);
410 d0_iobuf_close(in, NULL);
414 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
416 d0_iobuf_t *in = NULL;
418 REPLACING(rsa_n); REPLACING(rsa_e);
420 in = d0_iobuf_open_read(inbuf, inbuflen);
421 CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
422 CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
423 return d0_iobuf_close(in, NULL);
426 d0_iobuf_close(in, NULL);
430 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
432 d0_iobuf_t *out = NULL;
434 USING(rsa_n); USING(rsa_e); USING(rsa_d);
436 out = d0_iobuf_open_write(outbuf, *outbuflen);
437 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
438 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
439 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
440 return d0_iobuf_close(out, outbuflen);
443 d0_iobuf_close(out, outbuflen);
447 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
449 d0_iobuf_t *out = NULL;
451 USING(rsa_n); USING(rsa_e);
453 out = d0_iobuf_open_write(outbuf, *outbuflen);
454 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
455 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
456 return d0_iobuf_close(out, outbuflen);
459 if(!d0_iobuf_close(out, outbuflen))
464 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
466 d0_iobuf_t *out = NULL;
467 static unsigned char convbuf[2048];
468 d0_iobuf_t *conv = NULL;
471 USING(rsa_n); USING(rsa_e);
473 out = d0_iobuf_open_write(outbuf, *outbuflen);
474 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
476 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
477 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
478 CHECK(d0_iobuf_close(conv, &sz));
481 n = (*outbuflen / 4) * 3;
482 if(n > SHA_DIGESTSIZE)
484 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
485 CHECK(d0_iobuf_conv_base64_out(out));
487 return d0_iobuf_close(out, outbuflen);
491 d0_iobuf_close(conv, &sz);
492 d0_iobuf_close(out, outbuflen);
496 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
499 REPLACING(schnorr_G);
501 CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
502 CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
508 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
510 d0_iobuf_t *in = NULL;
512 REPLACING(schnorr_G);
514 in = d0_iobuf_open_read(inbuf, inbuflen);
515 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
516 return d0_iobuf_close(in, NULL);
519 d0_iobuf_close(in, NULL);
523 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
525 d0_iobuf_t *out = NULL;
529 out = d0_iobuf_open_write(outbuf, *outbuflen);
530 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
531 return d0_iobuf_close(out, outbuflen);
534 d0_iobuf_close(out, outbuflen);
538 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
540 // temps: temp0 = order
542 REPLACING(schnorr_s); REPLACING(schnorr_g_to_s);
544 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
545 CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
546 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mod_pow(ctx->schnorr_g_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
547 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
554 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
556 d0_iobuf_t *out = NULL;
557 static unsigned char shabuf[2048];
560 // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
561 USING(rsa_n); USING(rsa_e); USING(schnorr_g_to_s);
562 REPLACING(rsa_blind_signature_camouflage);
564 out = d0_iobuf_open_write(outbuf, *outbuflen);
566 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
567 CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
569 // we will actually sign HA(4^s) to prevent a malleability attack!
570 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
571 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
572 if(sz > sizeof(shabuf))
574 CHECK(d0_longhash_bignum(temp2, shabuf, sz));
575 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
578 CHECK(d0_bignum_mod_mul(temp1, temp2, temp0, ctx->rsa_n));
579 CHECK(d0_iobuf_write_bignum(out, temp1));
580 return d0_iobuf_close(out, outbuflen);
583 d0_iobuf_close(out, outbuflen);
587 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)
589 d0_iobuf_t *in = NULL;
590 d0_iobuf_t *out = NULL;
592 // temps: temp0 input, temp1 temp0^d
593 USING(rsa_d); USING(rsa_n);
595 in = d0_iobuf_open_read(inbuf, inbuflen);
596 out = d0_iobuf_open_write(outbuf, *outbuflen);
598 CHECK(d0_iobuf_read_bignum(in, temp0));
599 CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
600 CHECK(d0_iobuf_write_bignum(out, temp1));
602 d0_iobuf_close(in, NULL);
603 return d0_iobuf_close(out, outbuflen);
606 d0_iobuf_close(in, NULL);
607 d0_iobuf_close(out, outbuflen);
611 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
613 d0_iobuf_t *in = NULL;
615 // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
616 USING(rsa_blind_signature_camouflage); USING(rsa_n);
617 REPLACING(schnorr_H_g_to_s_signature);
619 in = d0_iobuf_open_read(inbuf, inbuflen);
621 CHECK(d0_iobuf_read_bignum(in, temp0));
622 CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
623 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));
625 return d0_iobuf_close(in, NULL);
628 d0_iobuf_close(in, NULL);
632 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)
634 d0_iobuf_t *in = NULL;
636 REPLACING(rsa_blind_signature_camouflage);
638 in = d0_iobuf_open_read(inbuf, inbuflen);
640 CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_iobuf_read_bignum(in, ctx->rsa_blind_signature_camouflage));
642 return d0_iobuf_close(in, NULL);
645 d0_iobuf_close(in, NULL);
649 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)
651 d0_iobuf_t *out = NULL;
653 USING(rsa_blind_signature_camouflage);
655 out = d0_iobuf_open_write(outbuf, *outbuflen);
657 CHECK(d0_iobuf_write_bignum(out, ctx->rsa_blind_signature_camouflage));
659 return d0_iobuf_close(out, outbuflen);
662 d0_iobuf_close(out, outbuflen);
666 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
668 d0_iobuf_t *in = NULL;
670 REPLACING(schnorr_s); REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
672 in = d0_iobuf_open_read(inbuf, inbuflen);
674 CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
675 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
676 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
678 return d0_iobuf_close(in, NULL);
681 d0_iobuf_close(in, NULL);
685 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
687 d0_iobuf_t *in = NULL;
689 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
691 in = d0_iobuf_open_read(inbuf, inbuflen);
693 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
694 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
696 return d0_iobuf_close(in, NULL);
699 d0_iobuf_close(in, NULL);
703 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
705 d0_iobuf_t *out = NULL;
707 USING(schnorr_s); USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
709 out = d0_iobuf_open_write(outbuf, *outbuflen);
711 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
712 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
713 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
715 return d0_iobuf_close(out, outbuflen);
718 d0_iobuf_close(out, outbuflen);
722 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
724 d0_iobuf_t *out = NULL;
726 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
728 out = d0_iobuf_open_write(outbuf, *outbuflen);
730 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
731 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
733 return d0_iobuf_close(out, outbuflen);
736 d0_iobuf_close(out, outbuflen);
740 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)
742 // first run: send 4^s, 4^s signature
743 // 1. get random r, send HASH(4^r)
745 d0_iobuf_t *out = NULL;
746 static unsigned char convbuf[1024];
747 d0_iobuf_t *conv = NULL;
751 // temps: temp0 order, temp0 4^r
754 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
757 REPLACING(r); REPLACING(t); REPLACING(g_to_t);
759 out = d0_iobuf_open_write(outbuf, *outbuflen);
765 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
766 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
767 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
770 // start schnorr ID scheme
771 // generate random number r; x = g^r; send hash of x, remember r, forget x
772 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
774 CHECK_ASSIGN(ctx->r, d0_bignum_int(ctx->r, 4)); // decided by fair dice roll
776 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
779 // initialize Signed Diffie Hellmann
780 // we already have the group order in temp1
782 CHECK_ASSIGN(ctx->t, d0_bignum_int(ctx->t, 4)); // decided by fair dice roll
784 CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
786 // can we SOMEHOW do this with just one mod_pow?
788 CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
789 CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mod_pow(ctx->g_to_t, four, ctx->t, ctx->schnorr_G));
792 // hash it, hash it, everybody hash it
793 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
794 CHECK(d0_iobuf_write_bignum(conv, temp0));
795 CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
796 CHECK(d0_iobuf_write_packet(conv, msg, msglen));
797 CHECK(d0_iobuf_write_bignum(conv, temp0));
798 CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
799 d0_iobuf_close(conv, &sz);
801 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
802 CHECK(d0_iobuf_write_packet(out, msg, msglen));
804 return d0_iobuf_close(out, outbuflen);
807 d0_iobuf_close(out, outbuflen);
811 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)
812 // first run: get 4^s, 4^s signature
815 // 3. send challenge challenge of SCHNORR_BITS
817 d0_iobuf_t *in = NULL;
818 d0_iobuf_t *out = NULL;
819 static unsigned char shabuf[2048];
822 // temps: temp0 order, temp0 signature check
825 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
827 REPLACING(schnorr_G);
833 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
836 USING(rsa_e); USING(rsa_n);
837 REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r); REPLACING(t);
839 in = d0_iobuf_open_read(inbuf, inbuflen);
840 out = d0_iobuf_open_write(outbuf, *outbuflen);
846 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
847 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
848 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
850 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
851 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
852 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
853 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
854 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
855 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
857 // check signature of key (t = k^d, so, t^challenge = k)
858 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
860 // we will actually sign SHA(4^s) to prevent a malleability attack!
861 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
862 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
863 if(sz > sizeof(shabuf))
865 CHECK(d0_longhash_bignum(temp2, shabuf, sz));
866 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
868 // + 7 / 8 is too large, so let's mod it
869 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
872 if(d0_bignum_cmp(temp0, temp1))
874 // accept the key anyway, but mark as failed signature! will later return 0 in status
875 CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
879 CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
880 ctx->msglen = MSGSIZE;
881 CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
885 CHECK_ASSIGN(ctx->challenge, d0_bignum_int(ctx->challenge, 4)); // decided by fair dice roll
887 CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
889 CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
891 // Diffie Hellmann send
892 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
894 CHECK_ASSIGN(ctx->t, d0_bignum_int(ctx->t, 4)); // decided by fair dice roll
896 CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
898 CHECK(d0_bignum_mod_pow(temp0, four, ctx->t, ctx->schnorr_G));
899 CHECK(d0_iobuf_write_bignum(out, temp0));
902 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
904 d0_iobuf_close(in, NULL);
905 return d0_iobuf_close(out, outbuflen);
908 d0_iobuf_close(in, NULL);
909 d0_iobuf_close(out, outbuflen);
913 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)
914 // 1. read challenge challenge of SCHNORR_BITS
915 // 2. reply with r + s * challenge mod order
917 d0_iobuf_t *in = NULL;
918 d0_iobuf_t *out = NULL;
920 // temps: 0 order, 1 prod, 2 y, 3 challenge
921 REPLACING(other_g_to_t); REPLACING(t);
922 USING(schnorr_G); USING(schnorr_s); USING(r); USING(g_to_t);
924 in = d0_iobuf_open_read(inbuf, inbuflen);
925 out = d0_iobuf_open_write(outbuf, *outbuflen);
927 CHECK(d0_iobuf_read_bignum(in, temp3));
928 CHECK(d0_bignum_cmp(temp3, zero) >= 0);
929 CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
931 // send response for schnorr ID scheme
932 // i.challenge. r + ctx->schnorr_s * temp3
933 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
934 CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
935 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
936 CHECK(d0_bignum_mod_add(temp2, ctx->r, temp1, temp0));
938 CHECK(d0_bignum_mod_sub(temp2, ctx->r, temp1, temp0));
940 CHECK(d0_iobuf_write_bignum(out, temp2));
942 // Diffie Hellmann recv
943 CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
944 CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
945 CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
946 // Diffie Hellmann send
947 CHECK(d0_iobuf_write_bignum(out, ctx->g_to_t));
949 d0_iobuf_close(in, NULL);
950 return d0_iobuf_close(out, outbuflen);
953 d0_iobuf_close(in, NULL);
954 d0_iobuf_close(out, outbuflen);
958 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)
959 // 1. read y = r + s * challenge mod order
960 // 2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
961 // (check using H(g^r) which we know)
963 d0_iobuf_t *in = NULL;
964 static unsigned char convbuf[1024];
965 d0_iobuf_t *conv = NULL;
968 // temps: 0 y 1 order
969 USING(challenge); USING(schnorr_G);
970 REPLACING(other_g_to_t);
972 in = d0_iobuf_open_read(inbuf, inbuflen);
974 CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
975 CHECK(d0_iobuf_read_bignum(in, temp0));
976 CHECK(d0_bignum_cmp(temp0, zero) >= 0);
977 CHECK(d0_bignum_cmp(temp0, temp1) < 0);
979 // verify schnorr ID scheme
980 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
981 // we need 4^r = 4^temp0 (g^s)^-challenge
982 CHECK(d0_bignum_mod_inv(temp1, ctx->schnorr_g_to_s, ctx->schnorr_G));
983 CHECK(d0_bignum_mod_pow(temp2, temp1, ctx->challenge, ctx->schnorr_G));
985 // we need 4^r = 4^temp0 (g^s)^challenge
986 CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_g_to_s, ctx->challenge, ctx->schnorr_G));
988 CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
989 CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G));
991 // Diffie Hellmann recv
992 CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
993 CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
994 CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
996 // hash it, hash it, everybody hash it
997 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
998 CHECK(d0_iobuf_write_bignum(conv, temp3));
999 CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
1000 CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
1001 CHECK(d0_iobuf_write_bignum(conv, temp3));
1002 CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
1003 d0_iobuf_close(conv, &sz);
1005 if(memcmp(sha(convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
1007 // FAIL (not owned by player)
1012 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
1014 if(ctx->msglen <= *msglen)
1015 memcpy(msg, ctx->msg, ctx->msglen);
1017 memcpy(msg, ctx->msg, *msglen);
1018 *msglen = ctx->msglen;
1020 d0_iobuf_close(in, NULL);
1024 d0_iobuf_close(in, NULL);
1028 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_generate_missing_signature(d0_blind_id_t *ctx)
1031 static unsigned char shabuf[2048];
1033 REPLACING(schnorr_H_g_to_s_signature);
1034 USING(schnorr_g_to_s); USING(rsa_d); USING(rsa_n);
1036 // we will actually sign SHA(4^s) to prevent a malleability attack!
1037 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
1038 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1039 if(sz > sizeof(shabuf))
1040 sz = sizeof(shabuf);
1041 CHECK(d0_longhash_bignum(temp2, shabuf, sz));
1042 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
1044 // + 7 / 8 is too large, so let's mod it
1045 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1046 CHECK(d0_bignum_mod_pow(ctx->schnorr_H_g_to_s_signature, temp1, ctx->rsa_d, ctx->rsa_n));
1053 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)
1055 d0_iobuf_t *out = NULL;
1056 static unsigned char convbuf[1024];
1057 static unsigned char shabuf[1024];
1058 d0_iobuf_t *conv = NULL;
1063 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
1069 out = d0_iobuf_open_write(outbuf, *outbuflen);
1075 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
1076 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
1077 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
1080 // start schnorr SIGNATURE scheme
1081 // generate random number r; x = g^r; send hash of H(m||r), remember r, forget x
1082 CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
1083 CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
1084 CHECK(d0_bignum_mod_pow(temp1, four, ctx->r, ctx->schnorr_G));
1086 // hash it, hash it, everybody hash it
1087 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1088 CHECK(d0_iobuf_write_packet(conv, message, msglen));
1089 CHECK(d0_iobuf_write_bignum(conv, temp1));
1090 d0_iobuf_close(conv, &sz);
1092 CHECK(d0_longhash_destructive(convbuf, sz, shabuf, (d0_bignum_size(temp0) + 7) / 8));
1093 CHECK(d0_bignum_import_unsigned(temp2, shabuf, (d0_bignum_size(temp0) + 7) / 8));
1094 CHECK(d0_iobuf_write_bignum(out, temp2));
1096 // multiply with secret, sub k, modulo order
1097 CHECK(d0_bignum_mod_mul(temp1, temp2, ctx->schnorr_s, temp0));
1098 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1099 CHECK(d0_bignum_mod_add(temp2, ctx->r, temp1, temp0));
1101 CHECK(d0_bignum_mod_sub(temp2, ctx->r, temp1, temp0));
1103 CHECK(d0_iobuf_write_bignum(out, temp2));
1105 // write the message itself
1107 CHECK(d0_iobuf_write_packet(out, message, msglen));
1109 return d0_iobuf_close(out, outbuflen);
1112 d0_iobuf_close(out, outbuflen);
1115 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)
1117 return d0_blind_id_sign_with_private_id_sign_internal(ctx, is_first, send_modulus, 1, message, msglen, outbuf, outbuflen);
1119 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)
1121 return d0_blind_id_sign_with_private_id_sign_internal(ctx, is_first, send_modulus, 0, message, msglen, outbuf, outbuflen);
1124 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)
1126 d0_iobuf_t *in = NULL;
1127 d0_iobuf_t *conv = NULL;
1128 static unsigned char convbuf[2048];
1129 static unsigned char shabuf[2048];
1134 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
1136 REPLACING(schnorr_G);
1142 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
1145 USING(rsa_e); USING(rsa_n);
1147 in = d0_iobuf_open_read(inbuf, inbuflen);
1153 CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
1154 CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
1155 CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
1157 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
1158 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
1159 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
1160 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
1161 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
1162 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
1164 // check signature of key (t = k^d, so, t^challenge = k)
1165 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
1167 // we will actually sign SHA(4^s) to prevent a malleability attack!
1168 CHECK(d0_bignum_mov(temp2, ctx->schnorr_g_to_s));
1169 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1170 if(sz > sizeof(shabuf))
1171 sz = sizeof(shabuf);
1172 CHECK(d0_longhash_bignum(temp2, shabuf, sz));
1173 CHECK(d0_bignum_import_unsigned(temp2, shabuf, sz));
1175 // + 7 / 8 is too large, so let's mod it
1176 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1179 if(d0_bignum_cmp(temp0, temp1))
1181 // accept the key anyway, but mark as failed signature! will later return 0 in status
1182 CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
1186 CHECK(d0_dl_get_order(temp4, ctx->schnorr_G));
1187 CHECK(d0_iobuf_read_bignum(in, temp0)); // e == H(m || g^r)
1188 CHECK(d0_iobuf_read_bignum(in, temp1)); // x == (r - s*e) mod |G|
1190 CHECK(d0_iobuf_read_packet(in, msg, msglen));
1192 // VERIFY: g^x * (g^s)^-e = g^(x - s*e) = g^r
1194 // verify schnorr ID scheme
1195 // we need g^r = g^x (g^s)^e
1196 CHECK(d0_bignum_mod_pow(temp2, four, temp1, ctx->schnorr_G));
1197 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1198 CHECK(d0_bignum_mod_inv(temp3, ctx->schnorr_g_to_s, ctx->schnorr_G));
1199 CHECK(d0_bignum_mod_pow(temp1, temp3, temp0, ctx->schnorr_G));
1201 CHECK(d0_bignum_mod_pow(temp1, ctx->schnorr_g_to_s, temp0, ctx->schnorr_G));
1203 CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G)); // temp3 now is g^r
1205 // hash it, hash it, everybody hash it
1206 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1207 CHECK(d0_iobuf_write_packet(conv, msg, *msglen));
1208 CHECK(d0_iobuf_write_bignum(conv, temp3));
1209 d0_iobuf_close(conv, &sz);
1211 CHECK(d0_longhash_destructive(convbuf, sz, shabuf, (d0_bignum_size(temp4) + 7) / 8));
1212 CHECK(d0_bignum_import_unsigned(temp1, shabuf, (d0_bignum_size(temp4) + 7) / 8));
1215 CHECK(!d0_bignum_cmp(temp0, temp1));
1218 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
1220 d0_iobuf_close(in, NULL);
1224 d0_iobuf_close(in, NULL);
1227 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)
1229 return d0_blind_id_sign_with_private_id_verify_internal(ctx, is_first, recv_modulus, 1, inbuf, inbuflen, msg, msglen, status);
1231 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)
1233 return d0_blind_id_sign_with_private_id_verify_internal(ctx, is_first, recv_modulus, 0, inbuf, inbuflen, (char *) msg, &msglen, status);
1236 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1238 d0_iobuf_t *out = NULL;
1239 static unsigned char convbuf[1024];
1240 d0_iobuf_t *conv = NULL;
1245 USING(schnorr_g_to_s);
1247 out = d0_iobuf_open_write(outbuf, *outbuflen);
1248 conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1250 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
1251 CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
1252 CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_g_to_s));
1253 CHECK(d0_iobuf_close(conv, &sz));
1256 n = (*outbuflen / 4) * 3;
1257 if(n > SHA_DIGESTSIZE)
1259 CHECK(d0_iobuf_write_raw(out, sha(convbuf, sz), n) == n);
1260 CHECK(d0_iobuf_conv_base64_out(out));
1262 return d0_iobuf_close(out, outbuflen);
1266 d0_iobuf_close(conv, &sz);
1267 d0_iobuf_close(out, outbuflen);
1271 D0_BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1273 USING(t); USING(other_g_to_t); USING(schnorr_G);
1275 // temps: temp0 result
1276 CHECK(d0_bignum_mod_pow(temp0, ctx->other_g_to_t, ctx->t, ctx->schnorr_G));
1277 return d0_longhash_bignum(temp0, (unsigned char *) outbuf, *outbuflen);
1283 d0_blind_id_t *d0_blind_id_new(void)
1285 d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
1286 memset(b, 0, sizeof(*b));
1290 void d0_blind_id_free(d0_blind_id_t *a)
1292 d0_blind_id_clear(a);
1296 void d0_blind_id_util_sha256(char *out, const char *in, size_t n)
1299 SHA256_Init(&context);
1300 SHA256_Update(&context, (const unsigned char *) in, n);
1301 return SHA256_Final((unsigned char *) out, &context);