X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=crypto-keygen-standalone.c;h=d9206915a775453043d36bc3817e9f0fccd362da;hb=46964b3848eb7471d2f0e2284ae389b4b53337c1;hp=732d8d9d9d01526dbc388c7b0d15ba8fc557d996;hpb=82955f5db504bac6134c865d8a5efa40196724cd;p=xonotic%2Fdarkplaces.git diff --git a/crypto-keygen-standalone.c b/crypto-keygen-standalone.c index 732d8d9d..d9206915 100644 --- a/crypto-keygen-standalone.c +++ b/crypto-keygen-standalone.c @@ -1,5 +1,8 @@ +#define _GNU_SOURCE + #include +#include #include #include #include @@ -88,49 +91,87 @@ static size_t Crypto_UnParsePack(char *buf, size_t len, unsigned long header, co return pos; } -void file2lumps(const char *fn, unsigned long header, const char **lumps, size_t *lumpsize, size_t nlumps) +void file2buf(const char *fn, char **data, size_t *datasize) { FILE *f; - char buf[65536]; - size_t n; - f = fopen(fn, "rb"); + *data = NULL; + *datasize = 0; + size_t n = 0, dn = 0; + if(!strncmp(fn, "/dev/fd/", 8)) + f = fdopen(atoi(fn + 8), "rb"); + else + f = fopen(fn, "rb"); + if(!f) + { + return; + } + for(;;) + { + *data = realloc(*data, *datasize += 8192); + if(!*data) + { + *datasize = 0; + fclose(f); + return; + } + dn = fread(*data + n, 1, *datasize - n, f); + if(!dn) + break; + n += dn; + } + fclose(f); + *datasize = n; +} + +int buf2file(const char *fn, const char *data, size_t n) +{ + FILE *f; + if(!strncmp(fn, "/dev/fd/", 8)) + f = fdopen(atoi(fn + 8), "wb"); + else + f = fopen(fn, "wb"); if(!f) + return 0; + n = fwrite(data, n, 1, f); + if(fclose(f) || !n) + return 0; + return 1; +} + +void file2lumps(const char *fn, unsigned long header, const char **lumps, size_t *lumpsize, size_t nlumps) +{ + char *buf; + size_t n; + file2buf(fn, &buf, &n); + if(!buf) { fprintf(stderr, "could not open %s\n", fn); exit(1); } - n = fread(buf, 1, sizeof(buf), f); - fclose(f); if(!Crypto_ParsePack(buf, n, header, lumps, lumpsize, nlumps)) { 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); + free(buf); exit(1); } + free(buf); } mode_t umask_save; void lumps2file(const char *fn, unsigned long header, const char *const *lumps, size_t *lumpsize, size_t nlumps, D0_BOOL private) { - FILE *f; char buf[65536]; size_t n; if(private) umask(umask_save | 0077); else umask(umask_save); - f = fopen(fn, "wb"); - if(!f) - { - fprintf(stderr, "could not open %s\n", fn); - exit(1); - } if(!(n = Crypto_UnParsePack(buf, sizeof(buf), header, lumps, lumpsize, nlumps))) { fprintf(stderr, "could not unparse for %s\n", fn); exit(1); } - n = fwrite(buf, n, 1, f); - if(fclose(f) || !n) + if(!buf2file(fn, buf, n)) { fprintf(stderr, "could not write %s\n", fn); exit(1); @@ -153,8 +194,12 @@ void USAGE(const char *me) "%s -p public.d0pk -i id.d0pi\n" "%s -p public.d0pk -I idkey.d0si\n" "%s -0 -p public.d0pk -I idkey.d0si\n" - "%s -0 -p public.d0pk\n", - me, me, me, me, me, me, me, me, me, me, me, me, me, me + "%s -0 -p public.d0pk\n" + "%s -p public.d0pk -I idkey.d0si -d file-to-sign.dat -o file-signed.dat\n" + "%s -p public.d0pk -s file-signed.dat -o file-content.dat [-O id.d0pi]\n" + "%s -p public.d0pk -I idkey.d0si -d file-to-sign.dat -O signature.dat\n" + "%s -p public.d0pk -d file-to-sign.dat -s signature.dat [-O id.d0pi]\n", + me, me, me, me, me, me, me, me, me, me, me, me, me, me, me, me, me, me ); } @@ -232,9 +277,12 @@ int main(int argc, char **argv) int opt; size_t lumpsize[2]; const char *lumps[2]; + char *databuf_in; size_t databufsize_in; + char *databuf_out; size_t databufsize_out; + char *databuf_sig; size_t databufsize_sig; char lumps_w0[65536]; char lumps_w1[65536]; - const char *pubkeyfile = NULL, *privkeyfile = NULL, *pubidfile = NULL, *prividfile = NULL, *idreqfile = NULL, *idresfile = NULL, *outfile = NULL, *outfile2 = NULL, *camouflagefile = NULL; + const char *pubkeyfile = NULL, *privkeyfile = NULL, *pubidfile = NULL, *prividfile = NULL, *idreqfile = NULL, *idresfile = NULL, *outfile = NULL, *outfile2 = NULL, *camouflagefile = NULL, *datafile = NULL, *sigfile = NULL; char fp64[513]; size_t fp64size = 512; int mask = 0; int bits = 1024; @@ -250,7 +298,7 @@ int main(int argc, char **argv) umask_save = umask(0022); ctx = d0_blind_id_new(); - while((opt = getopt(argc, argv, "p:P:i:I:j:J:o:O:c:b:x:X:y:Fn:C0")) != -1) + while((opt = getopt(argc, argv, "d:s:p:P:i:I:j:J:o:O:c:b:x:X:y:Fn:C0")) != -1) { switch(opt) { @@ -307,6 +355,14 @@ int main(int argc, char **argv) // test mode mask |= 0x200; break; + case 'd': + datafile = optarg; + mask |= 0x400; + break; + case 's': + sigfile = optarg; + mask |= 0x800; + break; case 'X': infix = optarg; break; @@ -334,7 +390,7 @@ int main(int argc, char **argv) for(i = 0; infix[i]; ++i) if(toupper(infix[i]) != tolower(infix[i])) guesscount /= 2; - for(i = 0; i < prefixlen; ++i) + for(i = 0; i < (int)prefixlen; ++i) if(toupper(prefix[i]) != tolower(prefix[i])) guesscount /= 2; } @@ -422,6 +478,26 @@ int main(int argc, char **argv) } } + if(mask & 0x400) + { + file2buf(datafile, &databuf_in, &databufsize_in); + if(!databuf_in) + { + fprintf(stderr, "could not decode data\n"); + exit(1); + } + } + + if(mask & 0x800) + { + file2buf(sigfile, &databuf_sig, &databufsize_sig); + if(!databuf_sig) + { + fprintf(stderr, "could not decode signature\n"); + exit(1); + } + } + switch(mask) { // modes of operation: @@ -550,6 +626,67 @@ int main(int argc, char **argv) CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size)); printf("%.*s\n", (int)fp64size, fp64); break; + case 0x449: + // public key, private ID, data -> signed data + databufsize_out = databufsize_in + 8192; + databuf_out = malloc(databufsize_out); + CHECK(d0_blind_id_sign_with_private_id_sign(ctx, 1, 0, databuf_in, databufsize_in, databuf_out, &databufsize_out)); + buf2file(outfile, databuf_out, databufsize_out); + break; + case 0x489: + // public key, private ID, data -> signature + databufsize_out = databufsize_in + 8192; + databuf_out = malloc(databufsize_out); + CHECK(d0_blind_id_sign_with_private_id_sign_detached(ctx, 1, 0, databuf_in, databufsize_in, databuf_out, &databufsize_out)); + buf2file(outfile2, databuf_out, databufsize_out); + break; + case 0x841: + case 0x8C1: + // public key, signed data -> data, optional public ID + { + D0_BOOL status; + databufsize_out = databufsize_sig; + databuf_out = malloc(databufsize_out); + CHECK(d0_blind_id_sign_with_private_id_verify(ctx, 1, 0, databuf_sig, databufsize_sig, databuf_out, &databufsize_out, &status)); + CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size)); + printf("%d\n", (int)status); + printf("%.*s\n", (int)fp64size, fp64); + buf2file(outfile, databuf_out, databufsize_out); + + if(outfile2) + { + lumps[0] = lumps_w0; + lumpsize[0] = sizeof(lumps_w0); + lumps[1] = lumps_w1; + lumpsize[1] = sizeof(lumps_w1); + CHECK(d0_blind_id_write_public_key(ctx, lumps_w0, &lumpsize[0])); + CHECK(d0_blind_id_write_private_id_modulus(ctx, lumps_w1, &lumpsize[1])); + lumps2file(outfile2, FOURCC_D0PK, lumps, lumpsize, 2, 0); + } + } + break; + case 0xC01: + case 0xC81: + // public key, signature, signed data -> optional public ID + { + D0_BOOL status; + CHECK(d0_blind_id_sign_with_private_id_verify_detached(ctx, 1, 0, databuf_sig, databufsize_sig, databuf_in, databufsize_in, &status)); + CHECK(d0_blind_id_fingerprint64_public_id(ctx, fp64, &fp64size)); + printf("%d\n", (int)status); + printf("%.*s\n", (int)fp64size, fp64); + + if(outfile2) + { + lumps[0] = lumps_w0; + lumpsize[0] = sizeof(lumps_w0); + lumps[1] = lumps_w1; + lumpsize[1] = sizeof(lumps_w1); + CHECK(d0_blind_id_write_public_key(ctx, lumps_w0, &lumpsize[0])); + CHECK(d0_blind_id_write_private_id_modulus(ctx, lumps_w1, &lumpsize[1])); + lumps2file(outfile2, FOURCC_D0PK, lumps, lumpsize, 2, 0); + } + } + break; /* case 0x09: // public key, private ID file -> test whether key is properly signed @@ -583,16 +720,16 @@ int main(int argc, char **argv) char buf2[65536]; size_t buf2size; bufsize = sizeof(buf); CHECK(d0_blind_id_authenticate_with_private_id_start(ctx, 1, 1, "hello world", 11, buf, &bufsize)); - for(i = 0; i < bufsize; ++i) + for(i = 0; i < (int)bufsize; ++i) sprintf(&hexbuf[2*i], "%02x", (unsigned char)buf[i]); printf("%s\n", hexbuf); fgets(hexbuf, sizeof(hexbuf), stdin); buf2size = strlen(hexbuf) / 2; - for(i = 0; i < buf2size; ++i) + for(i = 0; i < (int)buf2size; ++i) buf2[i] = ((strchr(hex, hexbuf[2*i]) - hex) << 4) | (strchr(hex, hexbuf[2*i+1]) - hex); bufsize = sizeof(buf); CHECK(d0_blind_id_authenticate_with_private_id_response(ctx, buf2, buf2size, buf, &bufsize)); - for(i = 0; i < bufsize; ++i) + for(i = 0; i < (int)bufsize; ++i) sprintf(&hexbuf[2*i], "%02x", (unsigned char)buf[i]); printf("%s\n", hexbuf); } @@ -607,16 +744,16 @@ int main(int argc, char **argv) D0_BOOL status; fgets(hexbuf, sizeof(hexbuf), stdin); buf2size = strlen(hexbuf) / 2; - for(i = 0; i < buf2size; ++i) + for(i = 0; i < (int)buf2size; ++i) buf2[i] = ((strchr(hex, hexbuf[2*i]) - hex) << 4) | (strchr(hex, hexbuf[2*i+1]) - hex); bufsize = sizeof(buf); CHECK(d0_blind_id_authenticate_with_private_id_challenge(ctx, 1, 1, buf2, buf2size, buf, &bufsize, &status)); - for(i = 0; i < bufsize; ++i) + for(i = 0; i < (int)bufsize; ++i) sprintf(&hexbuf[2*i], "%02x", (unsigned char)buf[i]); printf("%s\n", hexbuf); fgets(hexbuf, sizeof(hexbuf), stdin); buf2size = strlen(hexbuf) / 2; - for(i = 0; i < buf2size; ++i) + for(i = 0; i < (int)buf2size; ++i) buf2[i] = ((strchr(hex, hexbuf[2*i]) - hex) << 4) | (strchr(hex, hexbuf[2*i+1]) - hex); bufsize = sizeof(buf); CHECK(d0_blind_id_authenticate_with_private_id_verify(ctx, buf2, buf2size, buf, &bufsize, &status));