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 file2lumps(const char *fn, unsigned long header, const char **lumps, size_t *lumpsize, size_t nlumps)
102 fprintf(stderr, "could not open %s\n", fn);
105 n = fread(buf, 1, sizeof(buf), f);
107 if(!Crypto_ParsePack(buf, n, header, lumps, lumpsize, nlumps))
109 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);
115 void lumps2file(const char *fn, unsigned long header, const char *const *lumps, size_t *lumpsize, size_t nlumps, D0_BOOL private)
121 umask(umask_save | 0077);
127 fprintf(stderr, "could not open %s\n", fn);
130 if(!(n = Crypto_UnParsePack(buf, sizeof(buf), header, lumps, lumpsize, nlumps)))
132 fprintf(stderr, "could not unparse for %s\n", fn);
135 n = fwrite(buf, n, 1, f);
138 fprintf(stderr, "could not write %s\n", fn);
143 void USAGE(const char *me)
146 "%s [-F] [-b bits] [-n progress-denominator] [-x prefix] [-X infix] [-C] -o private.d0sk\n"
147 "%s -P private.d0sk -o public.d0pk\n"
148 "%s [-n progress-denominator] [-x prefix] [-X infix] [-C] -p public.d0pk -o idkey-unsigned.d0si\n"
149 "%s -p public.d0pk -I idkey-unsigned.d0si -o request.d0iq -O camouflage.d0ic\n"
150 "%s -P private.d0sk -j request.d0iq -o response.d0ir\n"
151 "%s -p public.d0pk -I idkey-unsigned.d0si -c camouflage.d0ic -J response.d0ir -o idkey.d0si\n"
152 "%s -P private.d0sk -I idkey-unsigned.d0si -o idkey.d0si\n"
153 "%s -I idkey.d0si -o id.d0pi\n"
154 "%s -p public.d0pk\n"
155 "%s -P private.d0sk\n"
156 "%s -p public.d0pk -i id.d0pi\n"
157 "%s -p public.d0pk -I idkey.d0si\n"
158 "%s -0 -p public.d0pk -I idkey.d0si\n"
159 "%s -0 -p public.d0pk\n",
160 me, me, me, me, me, me, me, me, me, me, me, me, me, me
164 unsigned int seconds;
165 unsigned int generated;
166 unsigned int ntasks = 1;
167 double generated_offset;
170 void print_generated(int signo)
174 if(generated >= 1000000000)
176 generated_offset += generated;
179 fprintf(stderr, "Generated: %.0f (about %.0f, %.1f/s, about %.2f hours for %.0f)\n",
180 // nasty and dishonest hack:
181 // we are adjusting the values "back", so the total count is
182 // divided by guessfactor (as the check function is called
183 // guessfactor as often as it would be if no fastreject were
185 // so the values indicate the relative speed of fastreject vs
187 (generated + generated_offset) / guessfactor,
188 (generated + generated_offset) * ntasks / guessfactor,
189 (generated + generated_offset) * ntasks / (guessfactor * seconds),
190 guesscount * ((guessfactor * seconds) / (generated + generated_offset) / ntasks) / 3600.0,
195 #define CHECK(x) if(!(x)) { fprintf(stderr, "error exit: error returned by %s\n", #x); exit(2); }
197 const char *prefix = NULL, *infix = NULL;
198 size_t prefixlen = 0;
200 typedef D0_BOOL (*fingerprint_func) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
201 D0_BOOL fastreject(const d0_blind_id_t *ctx, void *pass)
203 static char fp64[513]; size_t fp64size = 512;
204 CHECK(((fingerprint_func) pass)(ctx, fp64, &fp64size));
209 if(strncasecmp(fp64, prefix, prefixlen))
214 if(!strcasestr(fp64, infix))
221 if(memcmp(fp64, prefix, prefixlen))
226 if(!strstr(fp64, infix))
233 int main(int argc, char **argv)
237 const char *lumps[2];
238 char lumps_w0[65536];
239 char lumps_w1[65536];
240 const char *pubkeyfile = NULL, *privkeyfile = NULL, *pubidfile = NULL, *prividfile = NULL, *idreqfile = NULL, *idresfile = NULL, *outfile = NULL, *outfile2 = NULL, *camouflagefile = NULL;
241 char fp64[513]; size_t fp64size = 512;
245 D0_BOOL do_fastreject = 1;
247 if(!d0_blind_id_INITIALIZE())
249 fprintf(stderr, "could not initialize\n");
253 umask_save = umask(0022);
255 ctx = d0_blind_id_new();
256 while((opt = getopt(argc, argv, "p:P:i:I:j:J:o:O:c:b:x:X:y:Fn:C0")) != -1)
264 ntasks = atoi(optarg);
269 case 'p': // d0pk = <pubkey> <modulus>
273 case 'P': // d0sk = <privkey> <modulus>
274 privkeyfile = optarg;
277 case 'i': // d0pi = <pubid>
281 case 'I': // d0si = <privid>
285 case 'j': // d0iq = <req>
289 case 'J': // d0ir = <resp>
302 camouflagefile = optarg;
307 prefixlen = strlen(prefix);
325 // fastreject is a slight slowdown when rejecting nothing at all
326 if(!infix && !prefixlen)
329 guesscount = pow(64.0, prefixlen);
331 guesscount /= (1 - pow(1 - pow(1/64.0, strlen(infix)), 44 - prefixlen - strlen(infix)));
332 // 44 chars; prefix is assumed to not match the infix (although it theoretically could)
333 // 43'th char however is always '=' and does not count
337 for(i = 0; infix[i]; ++i)
338 if(toupper(infix[i]) != tolower(infix[i]))
340 for(i = 0; i < (int)prefixlen; ++i)
341 if(toupper(prefix[i]) != tolower(prefix[i]))
347 // fastreject: reject function gets called about log(2^bits) times more often
348 guessfactor = bits * log(2) / 2;
349 // so guess function gets called guesscount * guessfactor times, and it tests as many valid keys as guesscount
354 file2lumps(pubkeyfile, FOURCC_D0PK, lumps, lumpsize, 2);
355 if(!d0_blind_id_read_public_key(ctx, lumps[0], lumpsize[0]))
357 fprintf(stderr, "could not decode public key\n");
360 if(!d0_blind_id_read_private_id_modulus(ctx, lumps[1], lumpsize[1]))
362 fprintf(stderr, "could not decode modulus\n");
368 file2lumps(privkeyfile, FOURCC_D0SK, lumps, lumpsize, 2);
369 if(!d0_blind_id_read_private_key(ctx, lumps[0], lumpsize[0]))
371 fprintf(stderr, "could not decode private key\n");
374 if(!d0_blind_id_read_private_id_modulus(ctx, lumps[1], lumpsize[1]))
376 fprintf(stderr, "could not decode modulus\n");
383 file2lumps(pubidfile, FOURCC_D0PI, lumps, lumpsize, 1);
384 if(!d0_blind_id_read_public_id(ctx, lumps[0], lumpsize[0]))
386 fprintf(stderr, "could not decode public ID\n");
392 file2lumps(prividfile, FOURCC_D0SI, lumps, lumpsize, 1);
393 if(!d0_blind_id_read_private_id(ctx, lumps[0], lumpsize[0]))
395 fprintf(stderr, "could not decode private ID\n");
402 file2lumps(idreqfile, FOURCC_D0IQ, lumps, lumpsize, 1);
403 lumpsize[1] = sizeof(lumps_w1);
405 if(!d0_blind_id_answer_private_id_request(ctx, lumps[0], lumpsize[0], lumps_w1, &lumpsize[1]))
407 fprintf(stderr, "could not answer private ID request\n");
411 else if((mask & 0x120) == 0x120)
413 file2lumps(camouflagefile, FOURCC_D0IC, lumps, lumpsize, 1);
414 if(!d0_blind_id_read_private_id_request_camouflage(ctx, lumps[0], lumpsize[0]))
416 fprintf(stderr, "could not decode camouflage\n");
420 file2lumps(idresfile, FOURCC_D0IR, lumps, lumpsize, 1);
421 if(!d0_blind_id_finish_private_id_request(ctx, lumps[0], lumpsize[0]))
423 fprintf(stderr, "could not finish private ID request\n");
430 // modes of operation:
432 // nothing -> private key file (incl modulus), print fingerprint
434 generated_offset = 0;
436 signal(SIGALRM, print_generated);
440 CHECK(d0_blind_id_generate_private_key_fastreject(ctx, bits, fastreject, d0_blind_id_fingerprint64_public_key));
444 guessfactor = 1; // no fastreject here
447 CHECK(d0_blind_id_generate_private_key(ctx, bits));
449 while(fastreject(ctx, d0_blind_id_fingerprint64_public_key));
452 signal(SIGALRM, NULL);
453 CHECK(d0_blind_id_generate_private_id_modulus(ctx));
455 lumpsize[0] = sizeof(lumps_w0);
457 lumpsize[1] = sizeof(lumps_w1);
458 CHECK(d0_blind_id_write_private_key(ctx, lumps_w0, &lumpsize[0]));
459 CHECK(d0_blind_id_write_private_id_modulus(ctx, lumps_w1, &lumpsize[1]));
460 lumps2file(outfile, FOURCC_D0SK, lumps, lumpsize, 2, 1);
463 // private key file -> public key file (incl modulus)
465 lumpsize[0] = sizeof(lumps_w0);
467 lumpsize[1] = sizeof(lumps_w1);
468 CHECK(d0_blind_id_write_public_key(ctx, lumps_w0, &lumpsize[0]));
469 CHECK(d0_blind_id_write_private_id_modulus(ctx, lumps_w1, &lumpsize[1]));
470 lumps2file(outfile, FOURCC_D0PK, lumps, lumpsize, 2, 0);
473 // public key file -> unsigned private ID file
475 generated_offset = 0;
477 signal(SIGALRM, print_generated);
479 guessfactor = 1; // no fastreject here
482 CHECK(d0_blind_id_generate_private_id_start(ctx));
484 while(fastreject(ctx, d0_blind_id_fingerprint64_public_id));
488 lumpsize[0] = sizeof(lumps_w0);
489 CHECK(d0_blind_id_write_private_id(ctx, lumps_w0, &lumpsize[0]));
490 lumps2file(outfile, FOURCC_D0SI, lumps, lumpsize, 1, 1);
493 // public key file, unsigned private ID file -> ID request file and camouflage file
495 lumpsize[0] = sizeof(lumps_w0);
496 CHECK(d0_blind_id_generate_private_id_request(ctx, lumps_w0, &lumpsize[0]));
497 lumps2file(outfile, FOURCC_D0IQ, lumps, lumpsize, 1, 0);
498 lumpsize[0] = sizeof(lumps_w0);
499 CHECK(d0_blind_id_write_private_id_request_camouflage(ctx, lumps_w0, &lumpsize[0]));
500 lumps2file(outfile2, FOURCC_D0IC, lumps, lumpsize, 1, 1);
503 // private key file, ID request file -> ID response file
504 lumps2file(outfile, FOURCC_D0IR, lumps+1, lumpsize+1, 1, 0);
507 // public key file, ID response file, private ID file -> signed private ID file
509 lumpsize[0] = sizeof(lumps_w0);
510 CHECK(d0_blind_id_write_private_id(ctx, lumps_w0, &lumpsize[0]));
511 lumps2file(outfile, FOURCC_D0SI, lumps, lumpsize, 1, 1);
514 // private key file, private ID file -> signed private ID file
516 char buf[65536]; size_t bufsize;
517 char buf2[65536]; size_t buf2size;
519 d0_blind_id_t *ctx2 = d0_blind_id_new();
520 CHECK(d0_blind_id_copy(ctx2, ctx));
521 bufsize = sizeof(buf);
522 CHECK(d0_blind_id_authenticate_with_private_id_start(ctx, 1, 1, "hello world", 11, buf, &bufsize));
523 buf2size = sizeof(buf2);
524 CHECK(d0_blind_id_authenticate_with_private_id_challenge(ctx2, 1, 1, buf, bufsize, buf2, &buf2size, &status));
525 bufsize = sizeof(buf);
526 CHECK(d0_blind_id_authenticate_with_private_id_response(ctx, buf2, buf2size, buf, &bufsize));
527 buf2size = sizeof(buf2);
528 CHECK(d0_blind_id_authenticate_with_private_id_verify(ctx2, buf, bufsize, buf2, &buf2size, &status));
530 CHECK(d0_blind_id_authenticate_with_private_id_generate_missing_signature(ctx2));
532 lumpsize[0] = sizeof(lumps_w0);
533 CHECK(d0_blind_id_write_private_id(ctx2, lumps_w0, &lumpsize[0]));
534 lumps2file(outfile, FOURCC_D0SI, lumps, lumpsize, 1, 1);
538 // private ID file -> public ID file
540 lumpsize[0] = sizeof(lumps_w0);
541 CHECK(d0_blind_id_write_public_id(ctx, lumps_w0, &lumpsize[0]));
542 lumps2file(outfile, FOURCC_D0PI, lumps, lumpsize, 1, 0);
546 // public/private key file -> fingerprint
547 CHECK(d0_blind_id_fingerprint64_public_key(ctx, fp64, &fp64size));
548 printf("%.*s\n", (int)fp64size, fp64);
552 // public/private ID file -> fingerprint
553 CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size));
554 printf("%.*s\n", (int)fp64size, fp64);
558 // public key, private ID file -> test whether key is properly signed
560 char buf[65536]; size_t bufsize;
561 char buf2[65536]; size_t buf2size;
563 d0_blind_id_t *ctx2 = d0_blind_id_new();
564 CHECK(d0_blind_id_copy(ctx2, ctx));
565 bufsize = sizeof(buf);
566 CHECK(d0_blind_id_authenticate_with_private_id_start(ctx, 1, 1, "hello world", 11, buf, &bufsize));
567 buf2size = sizeof(buf2);
568 CHECK(d0_blind_id_authenticate_with_private_id_challenge(ctx2, 1, 1, buf, bufsize, buf2, &buf2size, &status));
569 bufsize = sizeof(buf);
570 CHECK(d0_blind_id_authenticate_with_private_id_response(ctx, buf2, buf2size, buf, &bufsize));
571 buf2size = sizeof(buf2);
572 CHECK(d0_blind_id_authenticate_with_private_id_verify(ctx2, buf, bufsize, buf2, &buf2size, &status));
576 printf("EPIC FAIL\n");
584 const char hex[] = "0123456789abcdef";
585 char buf[65536]; size_t bufsize;
586 char buf2[65536]; size_t buf2size;
587 bufsize = sizeof(buf);
588 CHECK(d0_blind_id_authenticate_with_private_id_start(ctx, 1, 1, "hello world", 11, buf, &bufsize));
589 for(i = 0; i < (int)bufsize; ++i)
590 sprintf(&hexbuf[2*i], "%02x", (unsigned char)buf[i]);
591 printf("%s\n", hexbuf);
592 fgets(hexbuf, sizeof(hexbuf), stdin);
593 buf2size = strlen(hexbuf) / 2;
594 for(i = 0; i < (int)buf2size; ++i)
595 buf2[i] = ((strchr(hex, hexbuf[2*i]) - hex) << 4) | (strchr(hex, hexbuf[2*i+1]) - hex);
596 bufsize = sizeof(buf);
597 CHECK(d0_blind_id_authenticate_with_private_id_response(ctx, buf2, buf2size, buf, &bufsize));
598 for(i = 0; i < (int)bufsize; ++i)
599 sprintf(&hexbuf[2*i], "%02x", (unsigned char)buf[i]);
600 printf("%s\n", hexbuf);
607 const char hex[] = "0123456789abcdef";
608 char buf[65536]; size_t bufsize;
609 char buf2[65536]; size_t buf2size;
611 fgets(hexbuf, sizeof(hexbuf), stdin);
612 buf2size = strlen(hexbuf) / 2;
613 for(i = 0; i < (int)buf2size; ++i)
614 buf2[i] = ((strchr(hex, hexbuf[2*i]) - hex) << 4) | (strchr(hex, hexbuf[2*i+1]) - hex);
615 bufsize = sizeof(buf);
616 CHECK(d0_blind_id_authenticate_with_private_id_challenge(ctx, 1, 1, buf2, buf2size, buf, &bufsize, &status));
617 for(i = 0; i < (int)bufsize; ++i)
618 sprintf(&hexbuf[2*i], "%02x", (unsigned char)buf[i]);
619 printf("%s\n", hexbuf);
620 fgets(hexbuf, sizeof(hexbuf), stdin);
621 buf2size = strlen(hexbuf) / 2;
622 for(i = 0; i < (int)buf2size; ++i)
623 buf2[i] = ((strchr(hex, hexbuf[2*i]) - hex) << 4) | (strchr(hex, hexbuf[2*i+1]) - hex);
624 bufsize = sizeof(buf);
625 CHECK(d0_blind_id_authenticate_with_private_id_verify(ctx, buf2, buf2size, buf, &bufsize, &status));
626 printf("verify status: %d\n", status);
628 CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size));
629 printf("%.*s\n", (int)fp64size, fp64);
637 d0_blind_id_SHUTDOWN();