annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
1 /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved.
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
2 Redistribution and use in source and binary forms, with or without modification, are
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
3 permitted provided that the following conditions are met:
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
4
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
5 1. Redistributions of source code must retain the above copyright notice, this list of
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
6 conditions and the following disclaimer.
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
7
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
8 2. Redistributions in binary form must reproduce the above copyright notice, this list
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
9 of conditions and the following disclaimer in the documentation and/or other materials
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
10 provided with the distribution.
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
11
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
12 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
13 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
15 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
16 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
17 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
18 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
19 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
20 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
21
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
22 The views and conclusions contained in the software and documentation are those of the
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
23 authors and should not be interpreted as representing official policies, either expressed
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
24 or implied, of Ivo Smits.*/
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
25
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
26 #include "common.c"
24
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 8
diff changeset
27 #include "crypto_box_curve25519xsalsa20poly1305.h"
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 8
diff changeset
28 #include "crypto_scalarmult_curve25519.h"
8
6d86596d8884 Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents: 5
diff changeset
29 #include <fcntl.h>
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
30 #include <unistd.h>
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
31
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
32 int main(int argc, char** argv) {
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
33 print_header();
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
34
24
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 8
diff changeset
35 unsigned char cpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES];
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 8
diff changeset
36 unsigned char csecretkey[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES];
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
37 int input_mode = 0; //0=generate random, 1=read from argument
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
38 int output_mode = 0; //0=human readable, 1=space separated, 2=concatenated binary
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
39 int i;
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
40
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
41 for (i = 1; i < argc; i++) {
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
42 char* a = argv[i];
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
43 if (!strcmp(a, "-h") || !strcmp(a, "--help")) {
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
44 printf("Please read the documentation at http://wiki.ucis.nl/QuickTun\n");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
45 return 0;
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
46 } else if (!strcmp(a, "-v") || !strcmp(a, "--version")) {
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
47 printf("UCIS QuickTun "QT_VERSION"\n");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
48 return 0;
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
49 } else if (!strcmp(a, "-i")) {
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
50 i++;
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
51 if (i >= argc) return errorexit("Missing argument for -i");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
52 if (!hex2bin(csecretkey, argv[i], crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES)) return errorexit("Invalid secret key argument");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
53 input_mode = 1;
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
54 } else if (!strcmp(a, "-f")) {
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
55 int len = fread(csecretkey, 1, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES, stdin);
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
56 if (len < crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) return errorexitp("Error or end of file on STDIN");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
57 input_mode = 1;
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
58 } else if (!strcmp(a, "-o")) {
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
59 i++;
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
60 a = argv[i];
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
61 if (i >= argc) return errorexit("Missing argument for -o");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
62 if (!strcmp(a, "human")) output_mode = 0;
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
63 else if (!strcmp(a, "space")) output_mode = 1;
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
64 else if (!strcmp(a, "bin")) output_mode = 2;
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
65 else return errorexit("Invalid argument specified for -o");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
66 } else {
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
67 return errorexit("Unexpected command line argument");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
68 }
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
69 }
24
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 8
diff changeset
70
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
71 if (input_mode == 0) {
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
72 crypto_box_curve25519xsalsa20poly1305_keypair(cpublickey, csecretkey);
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
73 } else {
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
74 crypto_scalarmult_curve25519_base(cpublickey, csecretkey);
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
75 }
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
76
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
77 if (output_mode == 2) {
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
78 fwrite(csecretkey, 1, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES, stdout);
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
79 fwrite(cpublickey, 1, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES, stdout);
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
80 } else if (output_mode == 1) {
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
81 for (i = 0; i < crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES; i++) printf("%02x", csecretkey[i]);
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
82 printf(" ");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
83 for (i = 0; i < crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES; i++) printf("%02x", cpublickey[i]);
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
84 printf("\n");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
85 } else {
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
86 printf("SECRET: ");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
87 for (i = 0; i < crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES; i++) printf("%02x", csecretkey[i]);
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
88 printf("\n");
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
89
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
90 printf("PUBLIC: ");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
91 for (i = 0; i < crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES; i++) printf("%02x", cpublickey[i]);
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
92 printf("\n");
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
Ivo Smits <Ivo@UFO-Net.nl>
parents: 24
diff changeset
93 }
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
94
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
95 return 0;
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
96 }