1 #include <d0_blind_id/d0_blind_id.h>
10 #include <sys/types.h>
13 // BEGIN stuff shared with crypto.c
14 #define FOURCC_D0PK (('d' << 0) | ('0' << 8) | ('p' << 16) | ('k' << 24))
15 #define FOURCC_D0SK (('d' << 0) | ('0' << 8) | ('s' << 16) | ('k' << 24))
16 #define FOURCC_D0PI (('d' << 0) | ('0' << 8) | ('p' << 16) | ('i' << 24))
17 #define FOURCC_D0SI (('d' << 0) | ('0' << 8) | ('s' << 16) | ('i' << 24))
18 #define FOURCC_D0IQ (('d' << 0) | ('0' << 8) | ('i' << 16) | ('q' << 24))
19 #define FOURCC_D0IR (('d' << 0) | ('0' << 8) | ('i' << 16) | ('r' << 24))
20 #define FOURCC_D0ER (('d' << 0) | ('0' << 8) | ('e' << 16) | ('r' << 24))
21 #define FOURCC_D0IC (('d' << 0) | ('0' << 8) | ('i' << 16) | ('c' << 24))
23 static unsigned long Crypto_LittleLong(const char *data)
26 ((unsigned char) data[0]) |
27 (((unsigned char) data[1]) << 8) |
28 (((unsigned char) data[2]) << 16) |
29 (((unsigned char) data[3]) << 24);
32 static void Crypto_UnLittleLong(char *data, unsigned long l)
35 data[1] = (l >> 8) & 0xFF;
36 data[2] = (l >> 16) & 0xFF;
37 data[3] = (l >> 24) & 0xFF;
40 static size_t Crypto_ParsePack(const char *buf, size_t len, unsigned long header, const char **lumps, size_t *lumpsize, size_t nlumps)
49 if(Crypto_LittleLong(buf) != header)
53 for(i = 0; i < nlumps; ++i)
57 lumpsize[i] = Crypto_LittleLong(&buf[pos]);
59 if(pos + lumpsize[i] > len)
67 static size_t Crypto_UnParsePack(char *buf, size_t len, unsigned long header, const char *const *lumps, const size_t *lumpsize, size_t nlumps)
76 Crypto_UnLittleLong(buf, header);
79 for(i = 0; i < nlumps; ++i)
81 if(pos + 4 + lumpsize[i] > len)
83 Crypto_UnLittleLong(&buf[pos], lumpsize[i]);
85 memcpy(&buf[pos], lumps[i], lumpsize[i]);
91 void file2lumps(const char *fn, unsigned long header, const char **lumps, size_t *lumpsize, size_t nlumps)
99 fprintf(stderr, "could not open %s\n", fn);
102 n = fread(buf, 1, sizeof(buf), f);
104 if(!Crypto_ParsePack(buf, n, header, lumps, lumpsize, nlumps))
106 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);
112 void lumps2file(const char *fn, unsigned long header, const char *const *lumps, size_t *lumpsize, size_t nlumps, D0_BOOL private)
118 umask(umask_save | 0077);
124 fprintf(stderr, "could not open %s\n", fn);
127 if(!(n = Crypto_UnParsePack(buf, sizeof(buf), header, lumps, lumpsize, nlumps)))
129 fprintf(stderr, "could not unparse for %s\n", fn);
132 n = fwrite(buf, n, 1, f);
135 fprintf(stderr, "could not write %s\n", fn);
140 void USAGE(const char *me)
143 "%s [-F] [-b bits] [-n progress-denominator] [-x prefix] [-X infix] [-C] -o private.d0sk\n"
144 "%s -P private.d0sk -o public.d0pk\n"
145 "%s [-n progress-denominator] [-x prefix] [-X infix] [-C] -p public.d0pk -o idkey-unsigned.d0si\n"
146 "%s -p public.d0pk -I idkey-unsigned.d0si -o request.d0iq -O camouflage.d0ic\n"
147 "%s -P private.d0sk -j request.d0iq -o response.d0ir\n"
148 "%s -p public.d0pk -I idkey-unsigned.d0si -c camouflage.d0ic -J response.d0ir -o idkey.d0si\n"
149 "%s -P private.d0sk -I idkey-unsigned.d0si -o idkey.d0si\n"
150 "%s -I idkey.d0si -o id.d0pi\n"
151 "%s -p public.d0pk\n"
152 "%s -P private.d0sk\n"
153 "%s -p public.d0pk -i id.d0pi\n"
154 "%s -p public.d0pk -I idkey.d0si\n"
155 "%s -0 -p public.d0pk -I idkey.d0si\n"
156 "%s -0 -p public.d0pk\n",
157 me, me, me, me, me, me, me, me, me, me, me, me, me, me
161 unsigned int seconds;
162 unsigned int generated;
163 unsigned int ntasks = 1;
164 double generated_offset;
167 void print_generated(int signo)
171 if(generated >= 1000000000)
173 generated_offset += generated;
176 fprintf(stderr, "Generated: %.0f (about %.0f, %.1f/s, about %.2f hours for %.0f)\n",
177 // nasty and dishonest hack:
178 // we are adjusting the values "back", so the total count is
179 // divided by guessfactor (as the check function is called
180 // guessfactor as often as it would be if no fastreject were
182 // so the values indicate the relative speed of fastreject vs
184 (generated + generated_offset) / guessfactor,
185 (generated + generated_offset) * ntasks / guessfactor,
186 (generated + generated_offset) * ntasks / (guessfactor * seconds),
187 guesscount * ((guessfactor * seconds) / (generated + generated_offset) / ntasks) / 3600.0,
192 #define CHECK(x) if(!(x)) { fprintf(stderr, "error exit: error returned by %s\n", #x); exit(2); }
194 const char *prefix = NULL, *infix = NULL;
195 size_t prefixlen = 0;
197 typedef D0_BOOL (*fingerprint_func) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
198 D0_BOOL fastreject(const d0_blind_id_t *ctx, void *pass)
200 static char fp64[513]; size_t fp64size = 512;
201 CHECK(((fingerprint_func) pass)(ctx, fp64, &fp64size));
206 if(strncasecmp(fp64, prefix, prefixlen))
211 if(!strcasestr(fp64, infix))
218 if(memcmp(fp64, prefix, prefixlen))
223 if(!strstr(fp64, infix))
230 int main(int argc, char **argv)
234 const char *lumps[2];
235 char lumps_w0[65536];
236 char lumps_w1[65536];
237 const char *pubkeyfile = NULL, *privkeyfile = NULL, *pubidfile = NULL, *prividfile = NULL, *idreqfile = NULL, *idresfile = NULL, *outfile = NULL, *outfile2 = NULL, *camouflagefile = NULL;
238 char fp64[513]; size_t fp64size = 512;
242 D0_BOOL do_fastreject = 1;
244 if(!d0_blind_id_INITIALIZE())
246 fprintf(stderr, "could not initialize\n");
250 umask_save = umask(0022);
252 ctx = d0_blind_id_new();
253 while((opt = getopt(argc, argv, "p:P:i:I:j:J:o:O:c:b:x:X:y:Fn:C0")) != -1)
261 ntasks = atoi(optarg);
266 case 'p': // d0pk = <pubkey> <modulus>
270 case 'P': // d0sk = <privkey> <modulus>
271 privkeyfile = optarg;
274 case 'i': // d0pi = <pubid>
278 case 'I': // d0si = <privid>
282 case 'j': // d0iq = <req>
286 case 'J': // d0ir = <resp>
299 camouflagefile = optarg;
304 prefixlen = strlen(prefix);
322 // fastreject is a slight slowdown when rejecting nothing at all
323 if(!infix && !prefixlen)
326 guesscount = pow(64.0, prefixlen);
328 guesscount /= (1 - pow(1 - pow(1/64.0, strlen(infix)), 44 - prefixlen - strlen(infix)));
329 // 44 chars; prefix is assumed to not match the infix (although it theoretically could)
330 // 43'th char however is always '=' and does not count
334 for(i = 0; infix[i]; ++i)
335 if(toupper(infix[i]) != tolower(infix[i]))
337 for(i = 0; i < prefixlen; ++i)
338 if(toupper(prefix[i]) != tolower(prefix[i]))
344 // fastreject: reject function gets called about log(2^bits) times more often
345 guessfactor = bits * log(2) / 2;
346 // so guess function gets called guesscount * guessfactor times, and it tests as many valid keys as guesscount
351 file2lumps(pubkeyfile, FOURCC_D0PK, lumps, lumpsize, 2);
352 if(!d0_blind_id_read_public_key(ctx, lumps[0], lumpsize[0]))
354 fprintf(stderr, "could not decode public key\n");
357 if(!d0_blind_id_read_private_id_modulus(ctx, lumps[1], lumpsize[1]))
359 fprintf(stderr, "could not decode modulus\n");
365 file2lumps(privkeyfile, FOURCC_D0SK, lumps, lumpsize, 2);
366 if(!d0_blind_id_read_private_key(ctx, lumps[0], lumpsize[0]))
368 fprintf(stderr, "could not decode private key\n");
371 if(!d0_blind_id_read_private_id_modulus(ctx, lumps[1], lumpsize[1]))
373 fprintf(stderr, "could not decode modulus\n");
380 file2lumps(pubidfile, FOURCC_D0PI, lumps, lumpsize, 1);
381 if(!d0_blind_id_read_public_id(ctx, lumps[0], lumpsize[0]))
383 fprintf(stderr, "could not decode public ID\n");
389 file2lumps(prividfile, FOURCC_D0SI, lumps, lumpsize, 1);
390 if(!d0_blind_id_read_private_id(ctx, lumps[0], lumpsize[0]))
392 fprintf(stderr, "could not decode private ID\n");
399 file2lumps(idreqfile, FOURCC_D0IQ, lumps, lumpsize, 1);
400 lumpsize[1] = sizeof(lumps_w1);
402 if(!d0_blind_id_answer_private_id_request(ctx, lumps[0], lumpsize[0], lumps_w1, &lumpsize[1]))
404 fprintf(stderr, "could not answer private ID request\n");
408 else if((mask & 0x120) == 0x120)
410 file2lumps(camouflagefile, FOURCC_D0IC, lumps, lumpsize, 1);
411 if(!d0_blind_id_read_private_id_request_camouflage(ctx, lumps[0], lumpsize[0]))
413 fprintf(stderr, "could not decode camouflage\n");
417 file2lumps(idresfile, FOURCC_D0IR, lumps, lumpsize, 1);
418 if(!d0_blind_id_finish_private_id_request(ctx, lumps[0], lumpsize[0]))
420 fprintf(stderr, "could not finish private ID request\n");
427 // modes of operation:
429 // nothing -> private key file (incl modulus), print fingerprint
431 generated_offset = 0;
433 signal(SIGALRM, print_generated);
437 CHECK(d0_blind_id_generate_private_key_fastreject(ctx, bits, fastreject, d0_blind_id_fingerprint64_public_key));
441 guessfactor = 1; // no fastreject here
444 CHECK(d0_blind_id_generate_private_key(ctx, bits));
446 while(fastreject(ctx, d0_blind_id_fingerprint64_public_key));
449 signal(SIGALRM, NULL);
450 CHECK(d0_blind_id_generate_private_id_modulus(ctx));
452 lumpsize[0] = sizeof(lumps_w0);
454 lumpsize[1] = sizeof(lumps_w1);
455 CHECK(d0_blind_id_write_private_key(ctx, lumps_w0, &lumpsize[0]));
456 CHECK(d0_blind_id_write_private_id_modulus(ctx, lumps_w1, &lumpsize[1]));
457 lumps2file(outfile, FOURCC_D0SK, lumps, lumpsize, 2, 1);
460 // private key file -> public key file (incl modulus)
462 lumpsize[0] = sizeof(lumps_w0);
464 lumpsize[1] = sizeof(lumps_w1);
465 CHECK(d0_blind_id_write_public_key(ctx, lumps_w0, &lumpsize[0]));
466 CHECK(d0_blind_id_write_private_id_modulus(ctx, lumps_w1, &lumpsize[1]));
467 lumps2file(outfile, FOURCC_D0PK, lumps, lumpsize, 2, 0);
470 // public key file -> unsigned private ID file
472 generated_offset = 0;
474 signal(SIGALRM, print_generated);
476 guessfactor = 1; // no fastreject here
479 CHECK(d0_blind_id_generate_private_id_start(ctx));
481 while(fastreject(ctx, d0_blind_id_fingerprint64_public_id));
485 lumpsize[0] = sizeof(lumps_w0);
486 CHECK(d0_blind_id_write_private_id(ctx, lumps_w0, &lumpsize[0]));
487 lumps2file(outfile, FOURCC_D0SI, lumps, lumpsize, 1, 1);
490 // public key file, unsigned private ID file -> ID request file and camouflage file
492 lumpsize[0] = sizeof(lumps_w0);
493 CHECK(d0_blind_id_generate_private_id_request(ctx, lumps_w0, &lumpsize[0]));
494 lumps2file(outfile, FOURCC_D0IQ, lumps, lumpsize, 1, 0);
495 lumpsize[0] = sizeof(lumps_w0);
496 CHECK(d0_blind_id_write_private_id_request_camouflage(ctx, lumps_w0, &lumpsize[0]));
497 lumps2file(outfile2, FOURCC_D0IC, lumps, lumpsize, 1, 1);
500 // private key file, ID request file -> ID response file
501 lumps2file(outfile, FOURCC_D0IR, lumps+1, lumpsize+1, 1, 0);
504 // public key file, ID response file, private ID file -> signed private ID file
506 lumpsize[0] = sizeof(lumps_w0);
507 CHECK(d0_blind_id_write_private_id(ctx, lumps_w0, &lumpsize[0]));
508 lumps2file(outfile, FOURCC_D0SI, lumps, lumpsize, 1, 1);
511 // private key file, private ID file -> signed private ID file
513 char buf[65536]; size_t bufsize;
514 char buf2[65536]; size_t buf2size;
516 d0_blind_id_t *ctx2 = d0_blind_id_new();
517 CHECK(d0_blind_id_copy(ctx2, ctx));
518 bufsize = sizeof(buf);
519 CHECK(d0_blind_id_authenticate_with_private_id_start(ctx, 1, 1, "hello world", 11, buf, &bufsize));
520 buf2size = sizeof(buf2);
521 CHECK(d0_blind_id_authenticate_with_private_id_challenge(ctx2, 1, 1, buf, bufsize, buf2, &buf2size, &status));
522 bufsize = sizeof(buf);
523 CHECK(d0_blind_id_authenticate_with_private_id_response(ctx, buf2, buf2size, buf, &bufsize));
524 buf2size = sizeof(buf2);
525 CHECK(d0_blind_id_authenticate_with_private_id_verify(ctx2, buf, bufsize, buf2, &buf2size, &status));
527 CHECK(d0_blind_id_authenticate_with_private_id_generate_missing_signature(ctx2));
529 lumpsize[0] = sizeof(lumps_w0);
530 CHECK(d0_blind_id_write_private_id(ctx2, lumps_w0, &lumpsize[0]));
531 lumps2file(outfile, FOURCC_D0SI, lumps, lumpsize, 1, 1);
535 // private ID file -> public ID file
537 lumpsize[0] = sizeof(lumps_w0);
538 CHECK(d0_blind_id_write_public_id(ctx, lumps_w0, &lumpsize[0]));
539 lumps2file(outfile, FOURCC_D0PI, lumps, lumpsize, 1, 0);
543 // public/private key file -> fingerprint
544 CHECK(d0_blind_id_fingerprint64_public_key(ctx, fp64, &fp64size));
545 printf("%.*s\n", (int)fp64size, fp64);
549 // public/private ID file -> fingerprint
550 CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size));
551 printf("%.*s\n", (int)fp64size, fp64);
555 // public key, private ID file -> test whether key is properly signed
557 char buf[65536]; size_t bufsize;
558 char buf2[65536]; size_t buf2size;
560 d0_blind_id_t *ctx2 = d0_blind_id_new();
561 CHECK(d0_blind_id_copy(ctx2, ctx));
562 bufsize = sizeof(buf);
563 CHECK(d0_blind_id_authenticate_with_private_id_start(ctx, 1, 1, "hello world", 11, buf, &bufsize));
564 buf2size = sizeof(buf2);
565 CHECK(d0_blind_id_authenticate_with_private_id_challenge(ctx2, 1, 1, buf, bufsize, buf2, &buf2size, &status));
566 bufsize = sizeof(buf);
567 CHECK(d0_blind_id_authenticate_with_private_id_response(ctx, buf2, buf2size, buf, &bufsize));
568 buf2size = sizeof(buf2);
569 CHECK(d0_blind_id_authenticate_with_private_id_verify(ctx2, buf, bufsize, buf2, &buf2size, &status));
573 printf("EPIC FAIL\n");
581 const char hex[] = "0123456789abcdef";
582 char buf[65536]; size_t bufsize;
583 char buf2[65536]; size_t buf2size;
584 bufsize = sizeof(buf);
585 CHECK(d0_blind_id_authenticate_with_private_id_start(ctx, 1, 1, "hello world", 11, buf, &bufsize));
586 for(i = 0; i < bufsize; ++i)
587 sprintf(&hexbuf[2*i], "%02x", (unsigned char)buf[i]);
588 printf("%s\n", hexbuf);
589 fgets(hexbuf, sizeof(hexbuf), stdin);
590 buf2size = strlen(hexbuf) / 2;
591 for(i = 0; i < buf2size; ++i)
592 buf2[i] = ((strchr(hex, hexbuf[2*i]) - hex) << 4) | (strchr(hex, hexbuf[2*i+1]) - hex);
593 bufsize = sizeof(buf);
594 CHECK(d0_blind_id_authenticate_with_private_id_response(ctx, buf2, buf2size, buf, &bufsize));
595 for(i = 0; i < bufsize; ++i)
596 sprintf(&hexbuf[2*i], "%02x", (unsigned char)buf[i]);
597 printf("%s\n", hexbuf);
604 const char hex[] = "0123456789abcdef";
605 char buf[65536]; size_t bufsize;
606 char buf2[65536]; size_t buf2size;
608 fgets(hexbuf, sizeof(hexbuf), stdin);
609 buf2size = strlen(hexbuf) / 2;
610 for(i = 0; i < buf2size; ++i)
611 buf2[i] = ((strchr(hex, hexbuf[2*i]) - hex) << 4) | (strchr(hex, hexbuf[2*i+1]) - hex);
612 bufsize = sizeof(buf);
613 CHECK(d0_blind_id_authenticate_with_private_id_challenge(ctx, 1, 1, buf2, buf2size, buf, &bufsize, &status));
614 for(i = 0; i < bufsize; ++i)
615 sprintf(&hexbuf[2*i], "%02x", (unsigned char)buf[i]);
616 printf("%s\n", hexbuf);
617 fgets(hexbuf, sizeof(hexbuf), stdin);
618 buf2size = strlen(hexbuf) / 2;
619 for(i = 0; i < buf2size; ++i)
620 buf2[i] = ((strchr(hex, hexbuf[2*i]) - hex) << 4) | (strchr(hex, hexbuf[2*i+1]) - hex);
621 bufsize = sizeof(buf);
622 CHECK(d0_blind_id_authenticate_with_private_id_verify(ctx, buf2, buf2size, buf, &bufsize, &status));
623 printf("verify status: %d\n", status);
625 CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size));
626 printf("%.*s\n", (int)fp64size, fp64);
634 d0_blind_id_SHUTDOWN();