Mercurial > hg > quicktun
annotate src/proto.nacl0.c @ 23:bd4259de8459
Support shared NaCl library in build script (untested)
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Mon, 28 Feb 2011 16:32:48 +0100 |
parents | cf9b44b46be5 |
children | dfac56805c77 |
rev | line source |
---|---|
0 | 1 /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved. |
2 Redistribution and use in source and binary forms, with or without modification, are | |
3 permitted provided that the following conditions are met: | |
4 | |
5 1. Redistributions of source code must retain the above copyright notice, this list of | |
6 conditions and the following disclaimer. | |
7 | |
8 2. Redistributions in binary form must reproduce the above copyright notice, this list | |
9 of conditions and the following disclaimer in the documentation and/or other materials | |
10 provided with the distribution. | |
11 | |
12 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
13 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
14 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR | |
15 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
16 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
17 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
18 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
19 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
20 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
21 | |
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 | |
24 or implied, of Ivo Smits.*/ | |
25 | |
26 #include "common.c" | |
27 #include "crypto_box.h" | |
28 | |
29 struct qt_proto_data_nacl0 { | |
30 unsigned char cnonce[crypto_box_NONCEBYTES], cbefore[crypto_box_BEFORENMBYTES]; | |
31 }; | |
32 | |
33 /*static unsigned char cnonce[crypto_box_NONCEBYTES], cbefore[crypto_box_BEFORENMBYTES]; | |
34 static unsigned char buffer1[MAX_PACKET_LEN+crypto_box_ZEROBYTES], buffer2[MAX_PACKET_LEN+crypto_box_ZEROBYTES]; | |
35 static const unsigned char* buffer1offset = buffer1 + crypto_box_ZEROBYTES; | |
36 static const unsigned char* buffer2offset = buffer2 + crypto_box_BOXZEROBYTES; | |
37 static const int overhead = crypto_box_BOXZEROBYTES;*/ | |
38 | |
39 static int encode(struct qtsession* sess, char* raw, char* enc, int len) { | |
40 struct qt_proto_data_nacl0* d = (struct qt_proto_data_nacl0*)sess->protocol_data; | |
41 memset(raw, 0, crypto_box_ZEROBYTES); | |
42 if (crypto_box_afternm(enc, raw, len+crypto_box_ZEROBYTES, d->cnonce, d->cbefore)) return errorexit("Crypto failed"); | |
43 return len + crypto_box_BOXZEROBYTES; | |
44 } | |
45 | |
46 static int decode(struct qtsession* sess, char* enc, char* raw, int len) { | |
47 struct qt_proto_data_nacl0* d = (struct qt_proto_data_nacl0*)sess->protocol_data; | |
48 int i; | |
49 if (len < crypto_box_BOXZEROBYTES) { | |
50 fprintf(stderr, "Short packet received: %d\n", len); | |
51 return 0; | |
52 } | |
53 len -= crypto_box_BOXZEROBYTES; | |
54 memset(enc, 0, crypto_box_BOXZEROBYTES); | |
55 if (i = crypto_box_open_afternm(raw, enc, len+crypto_box_ZEROBYTES, d->cnonce, d->cbefore)) { | |
56 fprintf(stderr, "Decryption failed len=%d result=%d\n", len, i); | |
57 return 0; | |
58 } | |
59 return len; | |
60 } | |
61 | |
62 static int init(struct qtsession* sess) { | |
63 char* envval; | |
64 struct qt_proto_data_nacl0* d = (struct qt_proto_data_nacl0*)sess->protocol_data; | |
65 printf("Initializing cryptography...\n"); | |
66 memset(d->cnonce, 0, crypto_box_NONCEBYTES); | |
67 unsigned char cpublickey[crypto_box_PUBLICKEYBYTES], csecretkey[crypto_box_SECRETKEYBYTES]; | |
68 if (!(envval = getconf("PUBLIC_KEY"))) return errorexit("Missing PUBLIC_KEY"); | |
69 if (strlen(envval) != 2*crypto_box_PUBLICKEYBYTES) return errorexit("PUBLIC_KEY length"); | |
70 hex2bin(cpublickey, envval, crypto_box_PUBLICKEYBYTES); | |
71 if (!(envval = getconf("PRIVATE_KEY"))) return errorexit("Missing PRIVATE_KEY"); | |
72 if (strlen(envval) != 2*crypto_box_PUBLICKEYBYTES) return errorexit("PRIVATE_KEY length"); | |
73 hex2bin(csecretkey, envval, crypto_box_SECRETKEYBYTES); | |
74 crypto_box_beforenm(d->cbefore, cpublickey, csecretkey); | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
0
diff
changeset
|
75 return 0; |
0 | 76 } |
77 | |
78 #ifdef COMBINED_BINARY | |
79 int tunmain_nacl0() { | |
80 #else | |
81 int tunmain() { | |
82 #endif | |
83 struct qtproto p = { | |
84 1, | |
85 MAX_PACKET_LEN + crypto_box_ZEROBYTES, | |
86 MAX_PACKET_LEN + crypto_box_BOXZEROBYTES + crypto_box_BOXZEROBYTES, | |
87 crypto_box_ZEROBYTES, | |
88 crypto_box_BOXZEROBYTES, | |
89 encode, | |
90 decode, | |
91 init, | |
92 sizeof(struct qt_proto_data_nacl0), | |
93 }; | |
94 return qtrun(&p); | |
95 } | |
96 | |
97 #ifndef COMBINED_BINARY | |
98 int main() { | |
99 print_header(); | |
100 return tunmain(); | |
101 } | |
102 #endif |