annotate src/proto.nacltai.c @ 55:5685fad38195

Fixed compiler warnings from clang (including small bug in private key loading)
author Ivo Smits <Ivo@UCIS.nl>
date Fri, 31 Jan 2014 22:52:46 +0100
parents 3115f8af98bb
children 66d9d80215f0
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: 22
diff changeset
27 #include "crypto_box_curve25519xsalsa20poly1305.h"
19
3400045a57b8 Version 2.1.6, changed sourcecode archive generation, use NaCl version 20110221, do not duplicate NaCl sourcecode, support iproute2 for persistent tunnel creation on Debian, support running as root on Debian, removed autogenerated header files
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
28 #include "crypto_scalarmult_curve25519.h"
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
29 #include <sys/types.h>
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
30 #include <sys/time.h>
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
31
52
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
32 struct packedtaia {
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
33 unsigned char buffer[16];
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
34 };
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
35
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
36 struct qt_proto_data_nacltai {
24
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
37 unsigned char cenonce[crypto_box_curve25519xsalsa20poly1305_NONCEBYTES], cdnonce[crypto_box_curve25519xsalsa20poly1305_NONCEBYTES];
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
38 unsigned char cbefore[crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES];
52
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
39 struct packedtaia cdtailog[5];
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
40 };
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
41
6
cf9b44b46be5 Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents: 4
diff changeset
42 #define noncelength 16
24
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
43 #define nonceoffset (crypto_box_curve25519xsalsa20poly1305_NONCEBYTES - noncelength)
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
44 static const int overhead = crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES + noncelength;
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
45
44
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
46 static void taia_now_packed(unsigned char* b, int secoffset) {
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
47 struct timeval now;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
48 gettimeofday(&now, NULL);
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
49 u_int64_t sec = 4611686018427387914ULL + (u_int64_t)now.tv_sec + secoffset;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
50 b[0] = (sec >> 56) & 0xff;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
51 b[1] = (sec >> 48) & 0xff;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
52 b[2] = (sec >> 40) & 0xff;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
53 b[3] = (sec >> 32) & 0xff;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
54 b[4] = (sec >> 24) & 0xff;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
55 b[5] = (sec >> 16) & 0xff;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
56 b[6] = (sec >> 8) & 0xff;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
57 b[7] = (sec >> 0) & 0xff;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
58 u_int32_t nano = 1000 * now.tv_usec + 500;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
59 b[8] = (nano >> 24) & 0xff;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
60 b[9] = (nano >> 16) & 0xff;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
61 b[10] = (nano >> 8) & 0xff;
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
62 b[11] = (nano >> 0) & 0xff;
55
5685fad38195 Fixed compiler warnings from clang (including small bug in private key loading)
Ivo Smits <Ivo@UCIS.nl>
parents: 52
diff changeset
63 if (++b[15] == 0 && ++b[14] == 0 && ++b[13] == 0) ++b[12];
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
64 }
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
65
44
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
66 //Packet format: <16 bytes taia packed timestamp><16 bytes checksum><n bytes encrypted data>
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
67
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
68 static int encode(struct qtsession* sess, char* raw, char* enc, int len) {
30
6f0e6b7dc088 Fixed build script to support multiple abis on one machine, bugfix in code, minor improvements
Ivo Smits <Ivo@UCIS.nl>
parents: 27
diff changeset
69 if (debug) fprintf(stderr, "Encoding packet of %d bytes from %p to %p\n", len, raw, enc);
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
70 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data;
24
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
71 memset(raw, 0, crypto_box_curve25519xsalsa20poly1305_ZEROBYTES);
44
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
72 taia_now_packed(d->cenonce + nonceoffset, 0);
55
5685fad38195 Fixed compiler warnings from clang (including small bug in private key loading)
Ivo Smits <Ivo@UCIS.nl>
parents: 52
diff changeset
73 if (crypto_box_curve25519xsalsa20poly1305_afternm((unsigned char*)enc, (unsigned char*)raw, len + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, d->cenonce, d->cbefore)) return errorexit("Encryption failed");
24
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
74 memcpy((void*)(enc + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES - noncelength), d->cenonce + nonceoffset, noncelength);
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
75 len += overhead;
30
6f0e6b7dc088 Fixed build script to support multiple abis on one machine, bugfix in code, minor improvements
Ivo Smits <Ivo@UCIS.nl>
parents: 27
diff changeset
76 if (debug) fprintf(stderr, "Encoded packet of %d bytes from %p to %p\n", len, raw, enc);
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
77 return len;
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
78 }
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
79
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
80 static int decode(struct qtsession* sess, char* enc, char* raw, int len) {
30
6f0e6b7dc088 Fixed build script to support multiple abis on one machine, bugfix in code, minor improvements
Ivo Smits <Ivo@UCIS.nl>
parents: 27
diff changeset
81 if (debug) fprintf(stderr, "Decoding packet of %d bytes from %p to %p\n", len, enc, raw);
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
82 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data;
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
83 int i;
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
84 if (len < overhead) {
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
85 fprintf(stderr, "Short packet received: %d\n", len);
41
54d28a81ca99 Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents: 38
diff changeset
86 return -1;
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
87 }
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
88 len -= overhead;
52
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
89 struct packedtaia* tailog = &d->cdtailog[0];
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
90 struct packedtaia* taiold = tailog;
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
91 for (i = 0; i < 5; i++) {
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
92 if (memcmp(enc, tailog, 16) == 0) {
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
93 fprintf(stderr, "Duplicate timestamp received\n");
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
94 return -1;
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
95 }
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
96 if (memcmp(tailog, taiold, 16) < 0) taiold = tailog;
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
97 tailog++;
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
98 }
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
99 if (memcmp(enc, taiold, 16) <= 0) {
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
100 fprintf(stderr, "Timestamp going back, ignoring packet\n");
41
54d28a81ca99 Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents: 38
diff changeset
101 return -1;
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
102 }
44
55f379f0a650 Fixed/improved handling of timestamp in nonce in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 43
diff changeset
103 memcpy(d->cdnonce + nonceoffset, enc, noncelength);
24
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
104 memset(enc, 0, crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES);
55
5685fad38195 Fixed compiler warnings from clang (including small bug in private key loading)
Ivo Smits <Ivo@UCIS.nl>
parents: 52
diff changeset
105 if (crypto_box_curve25519xsalsa20poly1305_open_afternm((unsigned char*)raw, (unsigned char*)enc, len + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, d->cdnonce, d->cbefore)) {
5685fad38195 Fixed compiler warnings from clang (including small bug in private key loading)
Ivo Smits <Ivo@UCIS.nl>
parents: 52
diff changeset
106 fprintf(stderr, "Decryption failed len=%d\n", len);
41
54d28a81ca99 Small updates in preparation for stateful protocols
Ivo Smits <Ivo@UCIS.nl>
parents: 38
diff changeset
107 return -1;
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
108 }
52
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
109 memcpy(taiold, d->cdnonce + nonceoffset, 16);
30
6f0e6b7dc088 Fixed build script to support multiple abis on one machine, bugfix in code, minor improvements
Ivo Smits <Ivo@UCIS.nl>
parents: 27
diff changeset
110 if (debug) fprintf(stderr, "Decoded packet of %d bytes from %p to %p\n", len, enc, raw);
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
111 return len;
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
112 }
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
113
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
114 static int init(struct qtsession* sess) {
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
115 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data;
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
116 char* envval;
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
117 printf("Initializing cryptography...\n");
24
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
118 unsigned char cownpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], cpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], csecretkey[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES];
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
119 if (!(envval = getconf("PUBLIC_KEY"))) return errorexit("Missing PUBLIC_KEY");
24
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
120 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PUBLIC_KEY length");
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
121 hex2bin(cpublickey, envval, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES);
55
5685fad38195 Fixed compiler warnings from clang (including small bug in private key loading)
Ivo Smits <Ivo@UCIS.nl>
parents: 52
diff changeset
122 if ((envval = getconf("PRIVATE_KEY"))) {
37
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
123 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PRIVATE_KEY length");
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
124 hex2bin(csecretkey, envval, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES);
55
5685fad38195 Fixed compiler warnings from clang (including small bug in private key loading)
Ivo Smits <Ivo@UCIS.nl>
parents: 52
diff changeset
125 } else if ((envval = getconf("PRIVATE_KEY_FILE"))) {
37
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
126 FILE* pkfile = fopen(envval, "rb");
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
127 if (!pkfile) return errorexitp("Could not open PRIVATE_KEY_FILE");
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
128 char pktextbuf[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES * 2];
55
5685fad38195 Fixed compiler warnings from clang (including small bug in private key loading)
Ivo Smits <Ivo@UCIS.nl>
parents: 52
diff changeset
129 const size_t pktextsize = fread(pktextbuf, 1, sizeof(pktextbuf), pkfile);
37
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
130 if (pktextsize == crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) {
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
131 memcpy(csecretkey, pktextbuf, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES);
55
5685fad38195 Fixed compiler warnings from clang (including small bug in private key loading)
Ivo Smits <Ivo@UCIS.nl>
parents: 52
diff changeset
132 } else if (pktextsize == 2 * crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) {
37
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
133 hex2bin(csecretkey, pktextbuf, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES);
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
134 } else {
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
135 return errorexit("PRIVATE_KEY length");
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
136 }
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
137 fclose(pkfile);
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
138 } else {
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
139 return errorexit("Missing PRIVATE_KEY");
bb4bbf380938 Added option PRIVATE_KEY_FILE to read private key from file
Ivo Smits <Ivo@UCIS.nl>
parents: 32
diff changeset
140 }
24
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
141 crypto_box_curve25519xsalsa20poly1305_beforenm(d->cbefore, cpublickey, csecretkey);
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
142
24
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
143 memset(d->cenonce, 0, crypto_box_curve25519xsalsa20poly1305_NONCEBYTES);
dfac56805c77 Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
Ivo Smits <Ivo@UCIS.nl>
parents: 22
diff changeset
144 memset(d->cdnonce, 0, crypto_box_curve25519xsalsa20poly1305_NONCEBYTES);
52
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
145 memset(d->cdtailog, 0, 5 * 16);
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
146
19
3400045a57b8 Version 2.1.6, changed sourcecode archive generation, use NaCl version 20110221, do not duplicate NaCl sourcecode, support iproute2 for persistent tunnel creation on Debian, support running as root on Debian, removed autogenerated header files
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
147 crypto_scalarmult_curve25519_base(cownpublickey, csecretkey);
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
148
55
5685fad38195 Fixed compiler warnings from clang (including small bug in private key loading)
Ivo Smits <Ivo@UCIS.nl>
parents: 52
diff changeset
149 if ((envval = getconf("TIME_WINDOW"))) {
52
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
150 struct packedtaia* tailog = d->cdtailog;
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
151 taia_now_packed((unsigned char*)&tailog[0], -atol(envval));
3115f8af98bb Added support for libsodium, fixed bug in USE_PI compatibility mode, improved timestamp checking in nacltai protocol
Ivo Smits <Ivo@UCIS.nl>
parents: 44
diff changeset
152 tailog[4] = tailog[3] = tailog[2] = tailog[1] = tailog[0];
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
153 } else {
4
a989ecbd5f53 Fixed nacltai implementation
ivo <ivo@UFO-Net.nl>
parents: 0
diff changeset
154 fprintf(stderr, "Warning: TIME_WINDOW not set, risking an initial replay attack\n");
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
155 }
43
4adbd9b67fe2 Fix nonce calculation when both sides use the same key in nacltai and salty protocols
Ivo Smits <Ivo@UCIS.nl>
parents: 41
diff changeset
156 int role = memcmp(cownpublickey, cpublickey, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES);
55
5685fad38195 Fixed compiler warnings from clang (including small bug in private key loading)
Ivo Smits <Ivo@UCIS.nl>
parents: 52
diff changeset
157 if ((envval = getconf("ROLE"))) role = atoi(envval) ? 1 : -1;
43
4adbd9b67fe2 Fix nonce calculation when both sides use the same key in nacltai and salty protocols
Ivo Smits <Ivo@UCIS.nl>
parents: 41
diff changeset
158 role = (role == 0) ? 0 : ((role > 0) ? 1 : 2);
4adbd9b67fe2 Fix nonce calculation when both sides use the same key in nacltai and salty protocols
Ivo Smits <Ivo@UCIS.nl>
parents: 41
diff changeset
159 d->cenonce[nonceoffset-1] = role & 1;
4adbd9b67fe2 Fix nonce calculation when both sides use the same key in nacltai and salty protocols
Ivo Smits <Ivo@UCIS.nl>
parents: 41
diff changeset
160 d->cdnonce[nonceoffset-1] = (role >> 1) & 1;
6
cf9b44b46be5 Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents: 4
diff changeset
161 return 0;
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
162 }
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
163
27
5ba185ca7102 Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents: 26
diff changeset
164 struct qtproto qtproto_nacltai = {
5ba185ca7102 Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents: 26
diff changeset
165 1,
5ba185ca7102 Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents: 26
diff changeset
166 MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
5ba185ca7102 Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents: 26
diff changeset
167 MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
5ba185ca7102 Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents: 26
diff changeset
168 crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
5ba185ca7102 Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents: 26
diff changeset
169 crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES - noncelength,
5ba185ca7102 Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents: 26
diff changeset
170 encode,
5ba185ca7102 Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents: 26
diff changeset
171 decode,
5ba185ca7102 Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents: 26
diff changeset
172 init,
5ba185ca7102 Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents: 26
diff changeset
173 sizeof(struct qt_proto_data_nacltai),
5ba185ca7102 Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents: 26
diff changeset
174 };
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
175
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
176 #ifndef COMBINED_BINARY
38
d9f5caa13898 Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents: 37
diff changeset
177 int main(int argc, char** argv) {
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
178 print_header();
38
d9f5caa13898 Added support for NetBSD, added command line parsing to provide configuration options
Ivo Smits <Ivo@UCIS.nl>
parents: 37
diff changeset
179 if (qtprocessargs(argc, argv) < 0) return -1;
27
5ba185ca7102 Fixed error checking during initialization, restructured code a bit to make it even simpler
Ivo Smits <Ivo@UCIS.nl>
parents: 26
diff changeset
180 return qtrun(&qtproto_nacltai);
0
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
181 }
65c01f57bdce Initial commit
ivo <ivo@UFO-Net.nl>
parents:
diff changeset
182 #endif