Mercurial > hg > quicktun
comparison src/proto.nacltai.c @ 24:dfac56805c77
Fixed support for shared NaCl library, explicitly refer to cryptographic primitives
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Mon, 28 Feb 2011 19:28:18 +0100 |
parents | 38d495566d1c |
children | 24e09485a8a3 |
comparison
equal
deleted
inserted
replaced
23:bd4259de8459 | 24:dfac56805c77 |
---|---|
22 The views and conclusions contained in the software and documentation are those of the | 22 The views and conclusions contained in the software and documentation are those of the |
23 authors and should not be interpreted as representing official policies, either expressed | 23 authors and should not be interpreted as representing official policies, either expressed |
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.h" | 27 #include "crypto_box_curve25519xsalsa20poly1305.h" |
28 #include "crypto_scalarmult_curve25519.h" | 28 #include "crypto_scalarmult_curve25519.h" |
29 #include <sys/types.h> | 29 #include <sys/types.h> |
30 #include <sys/time.h> | 30 #include <sys/time.h> |
31 | 31 |
32 #define uint64 unsigned long long //typedef unsigned long long uint64; | 32 #define uint64 unsigned long long //typedef unsigned long long uint64; |
39 unsigned long nano; /* 0...999999999 */ | 39 unsigned long nano; /* 0...999999999 */ |
40 unsigned long atto; /* 0...999999999 */ | 40 unsigned long atto; /* 0...999999999 */ |
41 }; | 41 }; |
42 | 42 |
43 struct qt_proto_data_nacltai { | 43 struct qt_proto_data_nacltai { |
44 unsigned char cenonce[crypto_box_NONCEBYTES], cdnonce[crypto_box_NONCEBYTES]; | 44 unsigned char cenonce[crypto_box_curve25519xsalsa20poly1305_NONCEBYTES], cdnonce[crypto_box_curve25519xsalsa20poly1305_NONCEBYTES]; |
45 unsigned char cbefore[crypto_box_BEFORENMBYTES]; | 45 unsigned char cbefore[crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES]; |
46 struct taia cdtaip, cdtaic; | 46 struct taia cdtaip, cdtaic; |
47 }; | 47 }; |
48 | 48 |
49 #define noncelength 16 | 49 #define noncelength 16 |
50 #define nonceoffset (crypto_box_NONCEBYTES - noncelength) | 50 #define nonceoffset (crypto_box_curve25519xsalsa20poly1305_NONCEBYTES - noncelength) |
51 /*static unsigned char cbefore[crypto_box_BEFORENMBYTES]; | 51 /*static unsigned char cbefore[crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES]; |
52 static unsigned char buffer1[MAX_PACKET_LEN+crypto_box_ZEROBYTES], buffer2[MAX_PACKET_LEN+crypto_box_ZEROBYTES]; | 52 static unsigned char buffer1[MAX_PACKET_LEN+crypto_box_curve25519xsalsa20poly1305_ZEROBYTES], buffer2[MAX_PACKET_LEN+crypto_box_curve25519xsalsa20poly1305_ZEROBYTES]; |
53 static const unsigned char* buffer1offset = buffer1 + crypto_box_ZEROBYTES; | 53 static const unsigned char* buffer1offset = buffer1 + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES; |
54 static const unsigned char* buffer2offset = buffer2 + crypto_box_BOXZEROBYTES - noncelength;*/ | 54 static const unsigned char* buffer2offset = buffer2 + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES - noncelength;*/ |
55 static const int overhead = crypto_box_BOXZEROBYTES + noncelength; | 55 static const int overhead = crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES + noncelength; |
56 | 56 |
57 void tai_pack(char *s, struct tai *t) { | 57 void tai_pack(char *s, struct tai *t) { |
58 uint64 x; | 58 uint64 x; |
59 x = t->x; | 59 x = t->x; |
60 s[7] = x & 255; x >>= 8; | 60 s[7] = x & 255; x >>= 8; |
118 } | 118 } |
119 | 119 |
120 static int encode(struct qtsession* sess, char* raw, char* enc, int len) { | 120 static int encode(struct qtsession* sess, char* raw, char* enc, int len) { |
121 if (debug) fprintf(stderr, "Encoding packet of %d bytes from %d to %d\n", len, raw, enc); | 121 if (debug) fprintf(stderr, "Encoding packet of %d bytes from %d to %d\n", len, raw, enc); |
122 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data; | 122 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data; |
123 memset(raw, 0, crypto_box_ZEROBYTES); | 123 memset(raw, 0, crypto_box_curve25519xsalsa20poly1305_ZEROBYTES); |
124 taia_now(&d->cdtaic); | 124 taia_now(&d->cdtaic); |
125 taia_pack(d->cenonce + nonceoffset, &(d->cdtaic)); | 125 taia_pack(d->cenonce + nonceoffset, &(d->cdtaic)); |
126 if (crypto_box_afternm(enc, raw, len + crypto_box_ZEROBYTES, d->cenonce, d->cbefore)) return errorexit("Encryption failed"); | 126 if (crypto_box_curve25519xsalsa20poly1305_afternm(enc, raw, len + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, d->cenonce, d->cbefore)) return errorexit("Encryption failed"); |
127 memcpy((void*)(enc + crypto_box_BOXZEROBYTES - noncelength), d->cenonce + nonceoffset, noncelength); | 127 memcpy((void*)(enc + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES - noncelength), d->cenonce + nonceoffset, noncelength); |
128 len += overhead; | 128 len += overhead; |
129 if (debug) fprintf(stderr, "Encoded packet of %d bytes from %d to %d\n", len, raw, enc); | 129 if (debug) fprintf(stderr, "Encoded packet of %d bytes from %d to %d\n", len, raw, enc); |
130 return len; | 130 return len; |
131 } | 131 } |
132 | 132 |
137 if (len < overhead) { | 137 if (len < overhead) { |
138 fprintf(stderr, "Short packet received: %d\n", len); | 138 fprintf(stderr, "Short packet received: %d\n", len); |
139 return 0; | 139 return 0; |
140 } | 140 } |
141 len -= overhead; | 141 len -= overhead; |
142 taia_unpack((char*)(enc + crypto_box_BOXZEROBYTES - noncelength), &d->cdtaic); | 142 taia_unpack((char*)(enc + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES - noncelength), &d->cdtaic); |
143 if (d->cdtaic.sec.x <= d->cdtaip.sec.x && d->cdtaic.nano <= d->cdtaip.nano && d->cdtaic.atto <= d->cdtaip.atto) { | 143 if (d->cdtaic.sec.x <= d->cdtaip.sec.x && d->cdtaic.nano <= d->cdtaip.nano && d->cdtaic.atto <= d->cdtaip.atto) { |
144 fprintf(stderr, "Timestamp going back, ignoring packet\n"); | 144 fprintf(stderr, "Timestamp going back, ignoring packet\n"); |
145 return 0; | 145 return 0; |
146 } | 146 } |
147 memcpy(d->cdnonce + nonceoffset, enc + crypto_box_BOXZEROBYTES - noncelength, noncelength); | 147 memcpy(d->cdnonce + nonceoffset, enc + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES - noncelength, noncelength); |
148 memset(enc, 0, crypto_box_BOXZEROBYTES); | 148 memset(enc, 0, crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES); |
149 if (i = crypto_box_open_afternm(raw, enc, len + crypto_box_ZEROBYTES, d->cdnonce, d->cbefore)) { | 149 if (i = crypto_box_curve25519xsalsa20poly1305_open_afternm(raw, enc, len + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, d->cdnonce, d->cbefore)) { |
150 fprintf(stderr, "Decryption failed len=%d result=%d\n", len, i); | 150 fprintf(stderr, "Decryption failed len=%d result=%d\n", len, i); |
151 return 0; | 151 return 0; |
152 } | 152 } |
153 d->cdtaip = d->cdtaic; | 153 d->cdtaip = d->cdtaic; |
154 if (debug) fprintf(stderr, "Decoded packet of %d bytes from %d to %d\n", len, enc, raw); | 154 if (debug) fprintf(stderr, "Decoded packet of %d bytes from %d to %d\n", len, enc, raw); |
157 | 157 |
158 static int init(struct qtsession* sess) { | 158 static int init(struct qtsession* sess) { |
159 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data; | 159 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data; |
160 char* envval; | 160 char* envval; |
161 printf("Initializing cryptography...\n"); | 161 printf("Initializing cryptography...\n"); |
162 unsigned char cownpublickey[crypto_box_PUBLICKEYBYTES], cpublickey[crypto_box_PUBLICKEYBYTES], csecretkey[crypto_box_SECRETKEYBYTES]; | 162 unsigned char cownpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], cpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], csecretkey[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES]; |
163 if (!(envval = getconf("PUBLIC_KEY"))) return errorexit("Missing PUBLIC_KEY"); | 163 if (!(envval = getconf("PUBLIC_KEY"))) return errorexit("Missing PUBLIC_KEY"); |
164 if (strlen(envval) != 2*crypto_box_PUBLICKEYBYTES) return errorexit("PUBLIC_KEY length"); | 164 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PUBLIC_KEY length"); |
165 hex2bin(cpublickey, envval, crypto_box_PUBLICKEYBYTES); | 165 hex2bin(cpublickey, envval, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES); |
166 if (!(envval = getconf("PRIVATE_KEY"))) return errorexit("Missing PRIVATE_KEY"); | 166 if (!(envval = getconf("PRIVATE_KEY"))) return errorexit("Missing PRIVATE_KEY"); |
167 if (strlen(envval) != 2*crypto_box_PUBLICKEYBYTES) return errorexit("PRIVATE_KEY length"); | 167 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PRIVATE_KEY length"); |
168 hex2bin(csecretkey, envval, crypto_box_SECRETKEYBYTES); | 168 hex2bin(csecretkey, envval, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); |
169 crypto_box_beforenm(d->cbefore, cpublickey, csecretkey); | 169 crypto_box_curve25519xsalsa20poly1305_beforenm(d->cbefore, cpublickey, csecretkey); |
170 | 170 |
171 memset(d->cenonce, 0, crypto_box_NONCEBYTES); | 171 memset(d->cenonce, 0, crypto_box_curve25519xsalsa20poly1305_NONCEBYTES); |
172 memset(d->cdnonce, 0, crypto_box_NONCEBYTES); | 172 memset(d->cdnonce, 0, crypto_box_curve25519xsalsa20poly1305_NONCEBYTES); |
173 | 173 |
174 crypto_scalarmult_curve25519_base(cownpublickey, csecretkey); | 174 crypto_scalarmult_curve25519_base(cownpublickey, csecretkey); |
175 | 175 |
176 if (envval = getenv("TIME_WINDOW")) { | 176 if (envval = getenv("TIME_WINDOW")) { |
177 taia_now(&d->cdtaip); | 177 taia_now(&d->cdtaip); |
180 fprintf(stderr, "Warning: TIME_WINDOW not set, risking an initial replay attack\n"); | 180 fprintf(stderr, "Warning: TIME_WINDOW not set, risking an initial replay attack\n"); |
181 } | 181 } |
182 if (envval = getenv("ROLE")) { | 182 if (envval = getenv("ROLE")) { |
183 d->cenonce[nonceoffset-1] = atoi(envval) ? 1 : 0; | 183 d->cenonce[nonceoffset-1] = atoi(envval) ? 1 : 0; |
184 } else { | 184 } else { |
185 d->cenonce[nonceoffset-1] = memcmp(cownpublickey, cpublickey, crypto_box_PUBLICKEYBYTES) > 0 ? 1 : 0; | 185 d->cenonce[nonceoffset-1] = memcmp(cownpublickey, cpublickey, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) > 0 ? 1 : 0; |
186 } | 186 } |
187 d->cdnonce[nonceoffset-1] = d->cenonce[nonceoffset-1] ? 0 : 1; | 187 d->cdnonce[nonceoffset-1] = d->cenonce[nonceoffset-1] ? 0 : 1; |
188 return 0; | 188 return 0; |
189 } | 189 } |
190 | 190 |
193 #else | 193 #else |
194 int tunmain() { | 194 int tunmain() { |
195 #endif | 195 #endif |
196 struct qtproto p = { | 196 struct qtproto p = { |
197 1, | 197 1, |
198 MAX_PACKET_LEN + crypto_box_ZEROBYTES, | 198 MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, |
199 MAX_PACKET_LEN + crypto_box_ZEROBYTES, | 199 MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, |
200 crypto_box_ZEROBYTES, | 200 crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, |
201 crypto_box_BOXZEROBYTES - noncelength, | 201 crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES - noncelength, |
202 encode, | 202 encode, |
203 decode, | 203 decode, |
204 init, | 204 init, |
205 sizeof(struct qt_proto_data_nacltai), | 205 sizeof(struct qt_proto_data_nacltai), |
206 }; | 206 }; |