3 #include <d0_blind_id/d0_blind_id.h>
13 #include <sys/types.h>
16 // BEGIN stuff shared with crypto.c
17 #define FOURCC_D0PK (('d' << 0) | ('0' << 8) | ('p' << 16) | ('k' << 24))
18 #define FOURCC_D0SK (('d' << 0) | ('0' << 8) | ('s' << 16) | ('k' << 24))
19 #define FOURCC_D0PI (('d' << 0) | ('0' << 8) | ('p' << 16) | ('i' << 24))
20 #define FOURCC_D0SI (('d' << 0) | ('0' << 8) | ('s' << 16) | ('i' << 24))
21 #define FOURCC_D0IQ (('d' << 0) | ('0' << 8) | ('i' << 16) | ('q' << 24))
22 #define FOURCC_D0IR (('d' << 0) | ('0' << 8) | ('i' << 16) | ('r' << 24))
23 #define FOURCC_D0ER (('d' << 0) | ('0' << 8) | ('e' << 16) | ('r' << 24))
24 #define FOURCC_D0IC (('d' << 0) | ('0' << 8) | ('i' << 16) | ('c' << 24))
26 static unsigned long Crypto_LittleLong(const char *data)
29 ((unsigned char) data[0]) |
30 (((unsigned char) data[1]) << 8) |
31 (((unsigned char) data[2]) << 16) |
32 (((unsigned char) data[3]) << 24);
35 static void Crypto_UnLittleLong(char *data, unsigned long l)
38 data[1] = (l >> 8) & 0xFF;
39 data[2] = (l >> 16) & 0xFF;
40 data[3] = (l >> 24) & 0xFF;
43 static size_t Crypto_ParsePack(const char *buf, size_t len, unsigned long header, const char **lumps, size_t *lumpsize, size_t nlumps)
52 if(Crypto_LittleLong(buf) != header)
56 for(i = 0; i < nlumps; ++i)
60 lumpsize[i] = Crypto_LittleLong(&buf[pos]);
62 if(pos + lumpsize[i] > len)
70 static size_t Crypto_UnParsePack(char *buf, size_t len, unsigned long header, const char *const *lumps, const size_t *lumpsize, size_t nlumps)
79 Crypto_UnLittleLong(buf, header);
82 for(i = 0; i < nlumps; ++i)
84 if(pos + 4 + lumpsize[i] > len)
86 Crypto_UnLittleLong(&buf[pos], lumpsize[i]);
88 memcpy(&buf[pos], lumps[i], lumpsize[i]);
94 void file2buf(const char *fn, char **data, size_t *datasize)
100 if(!strncmp(fn, "/dev/fd/", 8))
101 f = fdopen(atoi(fn + 8), "rb");
110 *data = realloc(*data, *datasize += 8192);
117 dn = fread(*data + n, 1, *datasize - n, f);
126 int buf2file(const char *fn, const char *data, size_t n)
129 if(!strncmp(fn, "/dev/fd/", 8))
130 f = fdopen(atoi(fn + 8), "wb");
135 n = fwrite(data, n, 1, f);
141 void file2lumps(const char *fn, unsigned long header, const char **lumps, size_t *lumpsize, size_t nlumps)
145 file2buf(fn, &buf, &n);
148 fprintf(stderr, "could not open %s\n", fn);
151 if(!Crypto_ParsePack(buf, n, header, lumps, lumpsize, nlumps))
153 fprintf(stderr, "could not parse %s as %c%c%c%c (%d lumps expected)\n", fn, (int) header & 0xFF, (int) (header >> 8) & 0xFF, (int) (header >> 16) & 0xFF, (int) (header >> 24) & 0xFF, (int) nlumps);
161 void lumps2file(const char *fn, unsigned long header, const char *const *lumps, size_t *lumpsize, size_t nlumps, D0_BOOL private)
166 umask(umask_save | 0077);
169 if(!(n = Crypto_UnParsePack(buf, sizeof(buf), header, lumps, lumpsize, nlumps)))
171 fprintf(stderr, "could not unparse for %s\n", fn);
174 if(!buf2file(fn, buf, n))
176 fprintf(stderr, "could not write %s\n", fn);
181 void USAGE(const char *me)
184 "%s [-F] [-b bits] [-n progress-denominator] [-x prefix] [-X infix] [-C] -o private.d0sk\n"
185 "%s -P private.d0sk -o public.d0pk\n"
186 "%s [-n progress-denominator] [-x prefix] [-X infix] [-C] -p public.d0pk -o idkey-unsigned.d0si\n"
187 "%s -p public.d0pk -I idkey-unsigned.d0si -o request.d0iq -O camouflage.d0ic\n"
188 "%s -P private.d0sk -j request.d0iq -o response.d0ir\n"
189 "%s -p public.d0pk -I idkey-unsigned.d0si -c camouflage.d0ic -J response.d0ir -o idkey.d0si\n"
190 "%s -P private.d0sk -I idkey-unsigned.d0si -o idkey.d0si\n"
191 "%s -I idkey.d0si -o id.d0pi\n"
192 "%s -p public.d0pk\n"
193 "%s -P private.d0sk\n"
194 "%s -p public.d0pk -i id.d0pi\n"
195 "%s -p public.d0pk -I idkey.d0si\n"
196 "%s -0 -p public.d0pk -I idkey.d0si\n"
197 "%s -0 -p public.d0pk\n"
198 "%s -p public.d0pk -I idkey.d0si -d file-to-sign.dat -o file-signed.dat\n"
199 "%s -p public.d0pk -s file-signed.dat -o file-content.dat [-O id.d0pi]\n"
200 "%s -p public.d0pk -I idkey.d0si -d file-to-sign.dat -O signature.dat\n"
201 "%s -p public.d0pk -d file-to-sign.dat -s signature.dat [-O id.d0pi]\n",
202 me, me, me, me, me, me, me, me, me, me, me, me, me, me, me, me, me, me
206 unsigned int seconds;
207 unsigned int generated;
208 unsigned int ntasks = 1;
209 double generated_offset;
212 void print_generated(int signo)
216 if(generated >= 1000000000)
218 generated_offset += generated;
221 fprintf(stderr, "Generated: %.0f (about %.0f, %.1f/s, about %.2f hours for %.0f)\n",
222 // nasty and dishonest hack:
223 // we are adjusting the values "back", so the total count is
224 // divided by guessfactor (as the check function is called
225 // guessfactor as often as it would be if no fastreject were
227 // so the values indicate the relative speed of fastreject vs
229 (generated + generated_offset) / guessfactor,
230 (generated + generated_offset) * ntasks / guessfactor,
231 (generated + generated_offset) * ntasks / (guessfactor * seconds),
232 guesscount * ((guessfactor * seconds) / (generated + generated_offset) / ntasks) / 3600.0,
237 #define CHECK(x) if(!(x)) { fprintf(stderr, "error exit: error returned by %s\n", #x); exit(2); }
239 const char *prefix = NULL, *infix = NULL;
240 size_t prefixlen = 0;
242 typedef D0_BOOL (*fingerprint_func) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
243 D0_BOOL fastreject(const d0_blind_id_t *ctx, void *pass)
245 static char fp64[513]; size_t fp64size = 512;
246 CHECK(((fingerprint_func) pass)(ctx, fp64, &fp64size));
251 if(strncasecmp(fp64, prefix, prefixlen))
256 if(!strcasestr(fp64, infix))
263 if(memcmp(fp64, prefix, prefixlen))
268 if(!strstr(fp64, infix))
275 int main(int argc, char **argv)
279 const char *lumps[2];
280 char *databuf_in; size_t databufsize_in;
281 char *databuf_out; size_t databufsize_out;
282 char *databuf_sig; size_t databufsize_sig;
283 char lumps_w0[65536];
284 char lumps_w1[65536];
285 const char *pubkeyfile = NULL, *privkeyfile = NULL, *pubidfile = NULL, *prividfile = NULL, *idreqfile = NULL, *idresfile = NULL, *outfile = NULL, *outfile2 = NULL, *camouflagefile = NULL, *datafile = NULL, *sigfile = NULL;
286 char fp64[513]; size_t fp64size = 512;
290 D0_BOOL do_fastreject = 1;
292 if(!d0_blind_id_INITIALIZE())
294 fprintf(stderr, "could not initialize\n");
298 umask_save = umask(0022);
300 ctx = d0_blind_id_new();
301 while((opt = getopt(argc, argv, "d:s:p:P:i:I:j:J:o:O:c:b:x:X:y:Fn:C0")) != -1)
309 ntasks = atoi(optarg);
314 case 'p': // d0pk = <pubkey> <modulus>
318 case 'P': // d0sk = <privkey> <modulus>
319 privkeyfile = optarg;
322 case 'i': // d0pi = <pubid>
326 case 'I': // d0si = <privid>
330 case 'j': // d0iq = <req>
334 case 'J': // d0ir = <resp>
347 camouflagefile = optarg;
352 prefixlen = strlen(prefix);
378 // fastreject is a slight slowdown when rejecting nothing at all
379 if(!infix && !prefixlen)
382 guesscount = pow(64.0, prefixlen);
384 guesscount /= (1 - pow(1 - pow(1/64.0, strlen(infix)), 44 - prefixlen - strlen(infix)));
385 // 44 chars; prefix is assumed to not match the infix (although it theoretically could)
386 // 43'th char however is always '=' and does not count
390 for(i = 0; infix[i]; ++i)
391 if(toupper(infix[i]) != tolower(infix[i]))
393 for(i = 0; i < (int)prefixlen; ++i)
394 if(toupper(prefix[i]) != tolower(prefix[i]))
400 // fastreject: reject function gets called about log(2^bits) times more often
401 guessfactor = bits * log(2) / 2;
402 // so guess function gets called guesscount * guessfactor times, and it tests as many valid keys as guesscount
407 file2lumps(pubkeyfile, FOURCC_D0PK, lumps, lumpsize, 2);
408 if(!d0_blind_id_read_public_key(ctx, lumps[0], lumpsize[0]))
410 fprintf(stderr, "could not decode public key\n");
413 if(!d0_blind_id_read_private_id_modulus(ctx, lumps[1], lumpsize[1]))
415 fprintf(stderr, "could not decode modulus\n");
421 file2lumps(privkeyfile, FOURCC_D0SK, lumps, lumpsize, 2);
422 if(!d0_blind_id_read_private_key(ctx, lumps[0], lumpsize[0]))
424 fprintf(stderr, "could not decode private key\n");
427 if(!d0_blind_id_read_private_id_modulus(ctx, lumps[1], lumpsize[1]))
429 fprintf(stderr, "could not decode modulus\n");
436 file2lumps(pubidfile, FOURCC_D0PI, lumps, lumpsize, 1);
437 if(!d0_blind_id_read_public_id(ctx, lumps[0], lumpsize[0]))
439 fprintf(stderr, "could not decode public ID\n");
445 file2lumps(prividfile, FOURCC_D0SI, lumps, lumpsize, 1);
446 if(!d0_blind_id_read_private_id(ctx, lumps[0], lumpsize[0]))
448 fprintf(stderr, "could not decode private ID\n");
455 file2lumps(idreqfile, FOURCC_D0IQ, lumps, lumpsize, 1);
456 lumpsize[1] = sizeof(lumps_w1);
458 if(!d0_blind_id_answer_private_id_request(ctx, lumps[0], lumpsize[0], lumps_w1, &lumpsize[1]))
460 fprintf(stderr, "could not answer private ID request\n");
464 else if((mask & 0x120) == 0x120)
466 file2lumps(camouflagefile, FOURCC_D0IC, lumps, lumpsize, 1);
467 if(!d0_blind_id_read_private_id_request_camouflage(ctx, lumps[0], lumpsize[0]))
469 fprintf(stderr, "could not decode camouflage\n");
473 file2lumps(idresfile, FOURCC_D0IR, lumps, lumpsize, 1);
474 if(!d0_blind_id_finish_private_id_request(ctx, lumps[0], lumpsize[0]))
476 fprintf(stderr, "could not finish private ID request\n");
483 file2buf(datafile, &databuf_in, &databufsize_in);
486 fprintf(stderr, "could not decode data\n");
493 file2buf(sigfile, &databuf_sig, &databufsize_sig);
496 fprintf(stderr, "could not decode signature\n");
503 // modes of operation:
505 // nothing -> private key file (incl modulus), print fingerprint
507 generated_offset = 0;
509 signal(SIGALRM, print_generated);
513 CHECK(d0_blind_id_generate_private_key_fastreject(ctx, bits, fastreject, d0_blind_id_fingerprint64_public_key));
517 guessfactor = 1; // no fastreject here
520 CHECK(d0_blind_id_generate_private_key(ctx, bits));
522 while(fastreject(ctx, d0_blind_id_fingerprint64_public_key));
525 signal(SIGALRM, NULL);
526 CHECK(d0_blind_id_generate_private_id_modulus(ctx));
528 lumpsize[0] = sizeof(lumps_w0);
530 lumpsize[1] = sizeof(lumps_w1);
531 CHECK(d0_blind_id_write_private_key(ctx, lumps_w0, &lumpsize[0]));
532 CHECK(d0_blind_id_write_private_id_modulus(ctx, lumps_w1, &lumpsize[1]));
533 lumps2file(outfile, FOURCC_D0SK, lumps, lumpsize, 2, 1);
536 // private key file -> public key file (incl modulus)
538 lumpsize[0] = sizeof(lumps_w0);
540 lumpsize[1] = sizeof(lumps_w1);
541 CHECK(d0_blind_id_write_public_key(ctx, lumps_w0, &lumpsize[0]));
542 CHECK(d0_blind_id_write_private_id_modulus(ctx, lumps_w1, &lumpsize[1]));
543 lumps2file(outfile, FOURCC_D0PK, lumps, lumpsize, 2, 0);
546 // public key file -> unsigned private ID file
548 generated_offset = 0;
550 signal(SIGALRM, print_generated);
552 guessfactor = 1; // no fastreject here
555 CHECK(d0_blind_id_generate_private_id_start(ctx));
557 while(fastreject(ctx, d0_blind_id_fingerprint64_public_id));
561 lumpsize[0] = sizeof(lumps_w0);
562 CHECK(d0_blind_id_write_private_id(ctx, lumps_w0, &lumpsize[0]));
563 lumps2file(outfile, FOURCC_D0SI, lumps, lumpsize, 1, 1);
566 // public key file, unsigned private ID file -> ID request file and camouflage file
568 lumpsize[0] = sizeof(lumps_w0);
569 CHECK(d0_blind_id_generate_private_id_request(ctx, lumps_w0, &lumpsize[0]));
570 lumps2file(outfile, FOURCC_D0IQ, lumps, lumpsize, 1, 0);
571 lumpsize[0] = sizeof(lumps_w0);
572 CHECK(d0_blind_id_write_private_id_request_camouflage(ctx, lumps_w0, &lumpsize[0]));
573 lumps2file(outfile2, FOURCC_D0IC, lumps, lumpsize, 1, 1);
576 // private key file, ID request file -> ID response file
577 lumps2file(outfile, FOURCC_D0IR, lumps+1, lumpsize+1, 1, 0);
580 // public key file, ID response file, private ID file -> signed private ID file
582 lumpsize[0] = sizeof(lumps_w0);
583 CHECK(d0_blind_id_write_private_id(ctx, lumps_w0, &lumpsize[0]));
584 lumps2file(outfile, FOURCC_D0SI, lumps, lumpsize, 1, 1);
587 // private key file, private ID file -> signed private ID file
589 char buf[65536]; size_t bufsize;
590 char buf2[65536]; size_t buf2size;
592 d0_blind_id_t *ctx2 = d0_blind_id_new();
593 CHECK(d0_blind_id_copy(ctx2, ctx));
594 bufsize = sizeof(buf);
595 CHECK(d0_blind_id_authenticate_with_private_id_start(ctx, 1, 1, "hello world", 11, buf, &bufsize));
596 buf2size = sizeof(buf2);
597 CHECK(d0_blind_id_authenticate_with_private_id_challenge(ctx2, 1, 1, buf, bufsize, buf2, &buf2size, &status));
598 bufsize = sizeof(buf);
599 CHECK(d0_blind_id_authenticate_with_private_id_response(ctx, buf2, buf2size, buf, &bufsize));
600 buf2size = sizeof(buf2);
601 CHECK(d0_blind_id_authenticate_with_private_id_verify(ctx2, buf, bufsize, buf2, &buf2size, &status));
603 CHECK(d0_blind_id_authenticate_with_private_id_generate_missing_signature(ctx2));
605 lumpsize[0] = sizeof(lumps_w0);
606 CHECK(d0_blind_id_write_private_id(ctx2, lumps_w0, &lumpsize[0]));
607 lumps2file(outfile, FOURCC_D0SI, lumps, lumpsize, 1, 1);
611 // private ID file -> public ID file
613 lumpsize[0] = sizeof(lumps_w0);
614 CHECK(d0_blind_id_write_public_id(ctx, lumps_w0, &lumpsize[0]));
615 lumps2file(outfile, FOURCC_D0PI, lumps, lumpsize, 1, 0);
619 // public/private key file -> fingerprint
620 CHECK(d0_blind_id_fingerprint64_public_key(ctx, fp64, &fp64size));
621 printf("%.*s\n", (int)fp64size, fp64);
625 // public/private ID file -> fingerprint
626 CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size));
627 printf("%.*s\n", (int)fp64size, fp64);
630 // public key, private ID, data -> signed data
631 databufsize_out = databufsize_in + 8192;
632 databuf_out = malloc(databufsize_out);
633 CHECK(d0_blind_id_sign_with_private_id_sign(ctx, 1, 0, databuf_in, databufsize_in, databuf_out, &databufsize_out));
634 buf2file(outfile, databuf_out, databufsize_out);
637 // public key, private ID, data -> signature
638 databufsize_out = databufsize_in + 8192;
639 databuf_out = malloc(databufsize_out);
640 CHECK(d0_blind_id_sign_with_private_id_sign_detached(ctx, 1, 0, databuf_in, databufsize_in, databuf_out, &databufsize_out));
641 buf2file(outfile2, databuf_out, databufsize_out);
645 // public key, signed data -> data, optional public ID
648 databufsize_out = databufsize_sig;
649 databuf_out = malloc(databufsize_out);
650 CHECK(d0_blind_id_sign_with_private_id_verify(ctx, 1, 0, databuf_sig, databufsize_sig, databuf_out, &databufsize_out, &status));
651 CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size));
652 printf("%d\n", (int)status);
653 printf("%.*s\n", (int)fp64size, fp64);
654 buf2file(outfile, databuf_out, databufsize_out);
659 lumpsize[0] = sizeof(lumps_w0);
661 lumpsize[1] = sizeof(lumps_w1);
662 CHECK(d0_blind_id_write_public_key(ctx, lumps_w0, &lumpsize[0]));
663 CHECK(d0_blind_id_write_private_id_modulus(ctx, lumps_w1, &lumpsize[1]));
664 lumps2file(outfile2, FOURCC_D0PK, lumps, lumpsize, 2, 0);
670 // public key, signature, signed data -> optional public ID
673 CHECK(d0_blind_id_sign_with_private_id_verify_detached(ctx, 1, 0, databuf_sig, databufsize_sig, databuf_in, databufsize_in, &status));
674 CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size));
675 printf("%d\n", (int)status);
676 printf("%.*s\n", (int)fp64size, fp64);
681 lumpsize[0] = sizeof(lumps_w0);
683 lumpsize[1] = sizeof(lumps_w1);
684 CHECK(d0_blind_id_write_public_key(ctx, lumps_w0, &lumpsize[0]));
685 CHECK(d0_blind_id_write_private_id_modulus(ctx, lumps_w1, &lumpsize[1]));
686 lumps2file(outfile2, FOURCC_D0PK, lumps, lumpsize, 2, 0);
692 // public key, private ID file -> test whether key is properly signed
694 char buf[65536]; size_t bufsize;
695 char buf2[65536]; size_t buf2size;
697 d0_blind_id_t *ctx2 = d0_blind_id_new();
698 CHECK(d0_blind_id_copy(ctx2, ctx));
699 bufsize = sizeof(buf);
700 CHECK(d0_blind_id_authenticate_with_private_id_start(ctx, 1, 1, "hello world", 11, buf, &bufsize));
701 buf2size = sizeof(buf2);
702 CHECK(d0_blind_id_authenticate_with_private_id_challenge(ctx2, 1, 1, buf, bufsize, buf2, &buf2size, &status));
703 bufsize = sizeof(buf);
704 CHECK(d0_blind_id_authenticate_with_private_id_response(ctx, buf2, buf2size, buf, &bufsize));
705 buf2size = sizeof(buf2);
706 CHECK(d0_blind_id_authenticate_with_private_id_verify(ctx2, buf, bufsize, buf2, &buf2size, &status));
710 printf("EPIC FAIL\n");
718 const char hex[] = "0123456789abcdef";
719 char buf[65536]; size_t bufsize;
720 char buf2[65536]; size_t buf2size;
721 bufsize = sizeof(buf);
722 CHECK(d0_blind_id_authenticate_with_private_id_start(ctx, 1, 1, "hello world", 11, buf, &bufsize));
723 for(i = 0; i < (int)bufsize; ++i)
724 sprintf(&hexbuf[2*i], "%02x", (unsigned char)buf[i]);
725 printf("%s\n", hexbuf);
726 fgets(hexbuf, sizeof(hexbuf), stdin);
727 buf2size = strlen(hexbuf) / 2;
728 for(i = 0; i < (int)buf2size; ++i)
729 buf2[i] = ((strchr(hex, hexbuf[2*i]) - hex) << 4) | (strchr(hex, hexbuf[2*i+1]) - hex);
730 bufsize = sizeof(buf);
731 CHECK(d0_blind_id_authenticate_with_private_id_response(ctx, buf2, buf2size, buf, &bufsize));
732 for(i = 0; i < (int)bufsize; ++i)
733 sprintf(&hexbuf[2*i], "%02x", (unsigned char)buf[i]);
734 printf("%s\n", hexbuf);
741 const char hex[] = "0123456789abcdef";
742 char buf[65536]; size_t bufsize;
743 char buf2[65536]; size_t buf2size;
745 fgets(hexbuf, sizeof(hexbuf), stdin);
746 buf2size = strlen(hexbuf) / 2;
747 for(i = 0; i < (int)buf2size; ++i)
748 buf2[i] = ((strchr(hex, hexbuf[2*i]) - hex) << 4) | (strchr(hex, hexbuf[2*i+1]) - hex);
749 bufsize = sizeof(buf);
750 CHECK(d0_blind_id_authenticate_with_private_id_challenge(ctx, 1, 1, buf2, buf2size, buf, &bufsize, &status));
751 for(i = 0; i < (int)bufsize; ++i)
752 sprintf(&hexbuf[2*i], "%02x", (unsigned char)buf[i]);
753 printf("%s\n", hexbuf);
754 fgets(hexbuf, sizeof(hexbuf), stdin);
755 buf2size = strlen(hexbuf) / 2;
756 for(i = 0; i < (int)buf2size; ++i)
757 buf2[i] = ((strchr(hex, hexbuf[2*i]) - hex) << 4) | (strchr(hex, hexbuf[2*i+1]) - hex);
758 bufsize = sizeof(buf);
759 CHECK(d0_blind_id_authenticate_with_private_id_verify(ctx, buf2, buf2size, buf, &bufsize, &status));
760 printf("verify status: %d\n", status);
762 CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size));
763 printf("%.*s\n", (int)fp64size, fp64);
771 d0_blind_id_SHUTDOWN();