Mercurial > hg > quicktun
comparison src/proto.nacl0.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 | cf9b44b46be5 |
children | 5ba185ca7102 |
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 | 28 |
29 struct qt_proto_data_nacl0 { | 29 struct qt_proto_data_nacl0 { |
30 unsigned char cnonce[crypto_box_NONCEBYTES], cbefore[crypto_box_BEFORENMBYTES]; | 30 unsigned char cnonce[crypto_box_curve25519xsalsa20poly1305_NONCEBYTES], cbefore[crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES]; |
31 }; | 31 }; |
32 | 32 |
33 /*static unsigned char cnonce[crypto_box_NONCEBYTES], cbefore[crypto_box_BEFORENMBYTES]; | 33 /*static unsigned char cnonce[crypto_box_curve25519xsalsa20poly1305_NONCEBYTES], cbefore[crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES]; |
34 static unsigned char buffer1[MAX_PACKET_LEN+crypto_box_ZEROBYTES], buffer2[MAX_PACKET_LEN+crypto_box_ZEROBYTES]; | 34 static unsigned char buffer1[MAX_PACKET_LEN+crypto_box_curve25519xsalsa20poly1305_ZEROBYTES], buffer2[MAX_PACKET_LEN+crypto_box_curve25519xsalsa20poly1305_ZEROBYTES]; |
35 static const unsigned char* buffer1offset = buffer1 + crypto_box_ZEROBYTES; | 35 static const unsigned char* buffer1offset = buffer1 + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES; |
36 static const unsigned char* buffer2offset = buffer2 + crypto_box_BOXZEROBYTES; | 36 static const unsigned char* buffer2offset = buffer2 + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES; |
37 static const int overhead = crypto_box_BOXZEROBYTES;*/ | 37 static const int overhead = crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES;*/ |
38 | 38 |
39 static int encode(struct qtsession* sess, char* raw, char* enc, int len) { | 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; | 40 struct qt_proto_data_nacl0* d = (struct qt_proto_data_nacl0*)sess->protocol_data; |
41 memset(raw, 0, crypto_box_ZEROBYTES); | 41 memset(raw, 0, crypto_box_curve25519xsalsa20poly1305_ZEROBYTES); |
42 if (crypto_box_afternm(enc, raw, len+crypto_box_ZEROBYTES, d->cnonce, d->cbefore)) return errorexit("Crypto failed"); | 42 if (crypto_box_curve25519xsalsa20poly1305_afternm(enc, raw, len+crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, d->cnonce, d->cbefore)) return errorexit("Crypto failed"); |
43 return len + crypto_box_BOXZEROBYTES; | 43 return len + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES; |
44 } | 44 } |
45 | 45 |
46 static int decode(struct qtsession* sess, char* enc, char* raw, int len) { | 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; | 47 struct qt_proto_data_nacl0* d = (struct qt_proto_data_nacl0*)sess->protocol_data; |
48 int i; | 48 int i; |
49 if (len < crypto_box_BOXZEROBYTES) { | 49 if (len < crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES) { |
50 fprintf(stderr, "Short packet received: %d\n", len); | 50 fprintf(stderr, "Short packet received: %d\n", len); |
51 return 0; | 51 return 0; |
52 } | 52 } |
53 len -= crypto_box_BOXZEROBYTES; | 53 len -= crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES; |
54 memset(enc, 0, crypto_box_BOXZEROBYTES); | 54 memset(enc, 0, crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES); |
55 if (i = crypto_box_open_afternm(raw, enc, len+crypto_box_ZEROBYTES, d->cnonce, d->cbefore)) { | 55 if (i = crypto_box_curve25519xsalsa20poly1305_open_afternm(raw, enc, len+crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, d->cnonce, d->cbefore)) { |
56 fprintf(stderr, "Decryption failed len=%d result=%d\n", len, i); | 56 fprintf(stderr, "Decryption failed len=%d result=%d\n", len, i); |
57 return 0; | 57 return 0; |
58 } | 58 } |
59 return len; | 59 return len; |
60 } | 60 } |
61 | 61 |
62 static int init(struct qtsession* sess) { | 62 static int init(struct qtsession* sess) { |
63 char* envval; | 63 char* envval; |
64 struct qt_proto_data_nacl0* d = (struct qt_proto_data_nacl0*)sess->protocol_data; | 64 struct qt_proto_data_nacl0* d = (struct qt_proto_data_nacl0*)sess->protocol_data; |
65 printf("Initializing cryptography...\n"); | 65 printf("Initializing cryptography...\n"); |
66 memset(d->cnonce, 0, crypto_box_NONCEBYTES); | 66 memset(d->cnonce, 0, crypto_box_curve25519xsalsa20poly1305_NONCEBYTES); |
67 unsigned char cpublickey[crypto_box_PUBLICKEYBYTES], csecretkey[crypto_box_SECRETKEYBYTES]; | 67 unsigned char cpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], csecretkey[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES]; |
68 if (!(envval = getconf("PUBLIC_KEY"))) return errorexit("Missing PUBLIC_KEY"); | 68 if (!(envval = getconf("PUBLIC_KEY"))) return errorexit("Missing PUBLIC_KEY"); |
69 if (strlen(envval) != 2*crypto_box_PUBLICKEYBYTES) return errorexit("PUBLIC_KEY length"); | 69 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PUBLIC_KEY length"); |
70 hex2bin(cpublickey, envval, crypto_box_PUBLICKEYBYTES); | 70 hex2bin(cpublickey, envval, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES); |
71 if (!(envval = getconf("PRIVATE_KEY"))) return errorexit("Missing PRIVATE_KEY"); | 71 if (!(envval = getconf("PRIVATE_KEY"))) return errorexit("Missing PRIVATE_KEY"); |
72 if (strlen(envval) != 2*crypto_box_PUBLICKEYBYTES) return errorexit("PRIVATE_KEY length"); | 72 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PRIVATE_KEY length"); |
73 hex2bin(csecretkey, envval, crypto_box_SECRETKEYBYTES); | 73 hex2bin(csecretkey, envval, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); |
74 crypto_box_beforenm(d->cbefore, cpublickey, csecretkey); | 74 crypto_box_curve25519xsalsa20poly1305_beforenm(d->cbefore, cpublickey, csecretkey); |
75 return 0; | 75 return 0; |
76 } | 76 } |
77 | 77 |
78 #ifdef COMBINED_BINARY | 78 #ifdef COMBINED_BINARY |
79 int tunmain_nacl0() { | 79 int tunmain_nacl0() { |
80 #else | 80 #else |
81 int tunmain() { | 81 int tunmain() { |
82 #endif | 82 #endif |
83 struct qtproto p = { | 83 struct qtproto p = { |
84 1, | 84 1, |
85 MAX_PACKET_LEN + crypto_box_ZEROBYTES, | 85 MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, |
86 MAX_PACKET_LEN + crypto_box_BOXZEROBYTES + crypto_box_BOXZEROBYTES, | 86 MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, |
87 crypto_box_ZEROBYTES, | 87 crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, |
88 crypto_box_BOXZEROBYTES, | 88 crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, |
89 encode, | 89 encode, |
90 decode, | 90 decode, |
91 init, | 91 init, |
92 sizeof(struct qt_proto_data_nacl0), | 92 sizeof(struct qt_proto_data_nacl0), |
93 }; | 93 }; |