Mercurial > hg > quicktun
comparison src/keypair.c @ 64:fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
author | Ivo Smits <Ivo@UFO-Net.nl> |
---|---|
date | Sat, 07 Jan 2017 18:07:27 +0100 |
parents | dfac56805c77 |
children |
comparison
equal
deleted
inserted
replaced
63:fa4983c5f7ea | 64:fa53d1c54886 |
---|---|
24 or implied, of Ivo Smits.*/ | 24 or implied, of Ivo Smits.*/ |
25 | 25 |
26 #include "common.c" | 26 #include "common.c" |
27 #include "crypto_box_curve25519xsalsa20poly1305.h" | 27 #include "crypto_box_curve25519xsalsa20poly1305.h" |
28 #include "crypto_scalarmult_curve25519.h" | 28 #include "crypto_scalarmult_curve25519.h" |
29 #include <time.h> | |
30 #include <fcntl.h> | 29 #include <fcntl.h> |
30 #include <unistd.h> | |
31 | 31 |
32 int main() { | 32 int main(int argc, char** argv) { |
33 print_header(); | 33 print_header(); |
34 | 34 |
35 unsigned char cpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES]; | 35 unsigned char cpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES]; |
36 unsigned char csecretkey[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES]; | 36 unsigned char csecretkey[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES]; |
37 int input_mode = 0; //0=generate random, 1=read from argument | |
38 int output_mode = 0; //0=human readable, 1=space separated, 2=concatenated binary | |
37 int i; | 39 int i; |
38 | 40 |
39 fprintf(stderr, "Please feed 32 bytes of random data to stdin.\n"); | 41 for (i = 1; i < argc; i++) { |
40 fprintf(stderr, "Example (slow but secure): quicktun.keypair < /dev/random\n"); | 42 char* a = argv[i]; |
41 fprintf(stderr, "Example (fast but insecure): quicktun.keypair < /dev/urandom\n"); | 43 if (!strcmp(a, "-h") || !strcmp(a, "--help")) { |
44 printf("Please read the documentation at http://wiki.ucis.nl/QuickTun\n"); | |
45 return 0; | |
46 } else if (!strcmp(a, "-v") || !strcmp(a, "--version")) { | |
47 printf("UCIS QuickTun "QT_VERSION"\n"); | |
48 return 0; | |
49 } else if (!strcmp(a, "-i")) { | |
50 i++; | |
51 if (i >= argc) return errorexit("Missing argument for -i"); | |
52 if (!hex2bin(csecretkey, argv[i], crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES)) return errorexit("Invalid secret key argument"); | |
53 input_mode = 1; | |
54 } else if (!strcmp(a, "-f")) { | |
55 int len = fread(csecretkey, 1, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES, stdin); | |
56 if (len < crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) return errorexitp("Error or end of file on STDIN"); | |
57 input_mode = 1; | |
58 } else if (!strcmp(a, "-o")) { | |
59 i++; | |
60 a = argv[i]; | |
61 if (i >= argc) return errorexit("Missing argument for -o"); | |
62 if (!strcmp(a, "human")) output_mode = 0; | |
63 else if (!strcmp(a, "space")) output_mode = 1; | |
64 else if (!strcmp(a, "bin")) output_mode = 2; | |
65 else return errorexit("Invalid argument specified for -o"); | |
66 } else { | |
67 return errorexit("Unexpected command line argument"); | |
68 } | |
69 } | |
42 | 70 |
43 int len = fread(csecretkey, 1, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES, stdin); | 71 if (input_mode == 0) { |
44 if (len < crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) return errorexitp("Error or end of file on STDIN"); | 72 crypto_box_curve25519xsalsa20poly1305_keypair(cpublickey, csecretkey); |
45 /* char* b; | 73 } else { |
46 srand(time(NULL)); | 74 crypto_scalarmult_curve25519_base(cpublickey, csecretkey); |
47 for (b = csecretkey; b < csecretkey + crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES; b++) *b = rand() % 255;*/ | 75 } |
48 | 76 |
49 crypto_scalarmult_curve25519_base(cpublickey, csecretkey); | 77 if (output_mode == 2) { |
78 fwrite(csecretkey, 1, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES, stdout); | |
79 fwrite(cpublickey, 1, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES, stdout); | |
80 } else if (output_mode == 1) { | |
81 for (i = 0; i < crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES; i++) printf("%02x", csecretkey[i]); | |
82 printf(" "); | |
83 for (i = 0; i < crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES; i++) printf("%02x", cpublickey[i]); | |
84 printf("\n"); | |
85 } else { | |
86 printf("SECRET: "); | |
87 for (i = 0; i < crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES; i++) printf("%02x", csecretkey[i]); | |
88 printf("\n"); | |
50 | 89 |
51 printf("SECRET: "); | 90 printf("PUBLIC: "); |
52 for (i = 0; i < crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES; i++) printf("%02x", csecretkey[i]); | 91 for (i = 0; i < crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES; i++) printf("%02x", cpublickey[i]); |
53 printf("\n"); | 92 printf("\n"); |
54 | 93 } |
55 printf("PUBLIC: "); | |
56 for (i = 0; i < crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES; i++) printf("%02x", cpublickey[i]); | |
57 printf("\n"); | |
58 | 94 |
59 return 0; | 95 return 0; |
60 } | 96 } |