comparison src/proto.nacltai.c @ 6:cf9b44b46be5

Use stderr for output instead of stdout, added debugging code to nacltai
author root <root@Really.UFO-Net.nl>
date Fri, 08 Oct 2010 23:29:56 +0000
parents a989ecbd5f53
children 1e4ba8d8ffc2
comparison
equal deleted inserted replaced
5:9d449e899402 6:cf9b44b46be5
43 unsigned char cenonce[crypto_box_NONCEBYTES], cdnonce[crypto_box_NONCEBYTES]; 43 unsigned char cenonce[crypto_box_NONCEBYTES], cdnonce[crypto_box_NONCEBYTES];
44 unsigned char cbefore[crypto_box_BEFORENMBYTES]; 44 unsigned char cbefore[crypto_box_BEFORENMBYTES];
45 struct taia cdtaip, cdtaic; 45 struct taia cdtaip, cdtaic;
46 }; 46 };
47 47
48 #define noncelength sizeof(struct taia) 48 #define noncelength 16
49 #define nonceoffset (crypto_box_NONCEBYTES - noncelength) 49 #define nonceoffset (crypto_box_NONCEBYTES - noncelength)
50 /*static unsigned char cbefore[crypto_box_BEFORENMBYTES]; 50 /*static unsigned char cbefore[crypto_box_BEFORENMBYTES];
51 static unsigned char buffer1[MAX_PACKET_LEN+crypto_box_ZEROBYTES], buffer2[MAX_PACKET_LEN+crypto_box_ZEROBYTES]; 51 static unsigned char buffer1[MAX_PACKET_LEN+crypto_box_ZEROBYTES], buffer2[MAX_PACKET_LEN+crypto_box_ZEROBYTES];
52 static const unsigned char* buffer1offset = buffer1 + crypto_box_ZEROBYTES; 52 static const unsigned char* buffer1offset = buffer1 + crypto_box_ZEROBYTES;
53 static const unsigned char* buffer2offset = buffer2 + crypto_box_BOXZEROBYTES - noncelength;*/ 53 static const unsigned char* buffer2offset = buffer2 + crypto_box_BOXZEROBYTES - noncelength;*/
117 } 117 }
118 118
119 extern crypto_scalarmult_curve25519_base(unsigned char *pk, unsigned char *sk); 119 extern crypto_scalarmult_curve25519_base(unsigned char *pk, unsigned char *sk);
120 120
121 static int encode(struct qtsession* sess, char* raw, char* enc, int len) { 121 static int encode(struct qtsession* sess, char* raw, char* enc, int len) {
122 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; 123 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data;
123 memset(raw, 0, crypto_box_ZEROBYTES); 124 memset(raw, 0, crypto_box_ZEROBYTES);
124 taia_now(&d->cdtaic); 125 taia_now(&d->cdtaic);
125 taia_pack(d->cenonce + nonceoffset, &(d->cdtaic)); 126 taia_pack(d->cenonce + nonceoffset, &(d->cdtaic));
126 if (crypto_box_afternm(enc, raw, len + crypto_box_ZEROBYTES, d->cenonce, d->cbefore)) return errorexit("Crypto failed"); 127 if (crypto_box_afternm(enc, raw, len + crypto_box_ZEROBYTES, d->cenonce, d->cbefore)) return errorexit("Crypto failed");
127 memcpy((void*)(enc + crypto_box_BOXZEROBYTES - noncelength), d->cenonce + nonceoffset, noncelength); 128 memcpy((void*)(enc + crypto_box_BOXZEROBYTES - noncelength), d->cenonce + nonceoffset, noncelength);
128 len += overhead; 129 len += overhead;
130 fprintf(stderr, "Encoded packet of %d bytes from %d to %d\n", len, raw, enc);
129 return len; 131 return len;
130 } 132 }
131 133
132 static int decode(struct qtsession* sess, char* enc, char* raw, int len) { 134 static int decode(struct qtsession* sess, char* enc, char* raw, int len) {
135 fprintf(stderr, "Decoding packet of %d bytes from %d to %d\n", len, enc, raw);
133 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data; 136 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data;
134 int i; 137 int i;
135 if (len < overhead) { 138 if (len < overhead) {
136 fprintf(stderr, "Short packet received: %d\n", len); 139 fprintf(stderr, "Short packet received: %d\n", len);
137 return 0; 140 return 0;
138 } 141 }
139 len -= overhead; 142 len -= overhead;
140 taia_unpack((char*)(enc + crypto_box_BOXZEROBYTES - noncelength), &(d->cdtaic)); 143 taia_unpack((char*)(enc + crypto_box_BOXZEROBYTES - noncelength), &d->cdtaic);
141 if (d->cdtaic.sec.x <= d->cdtaip.sec.x || d->cdtaic.nano <= d->cdtaip.nano || d->cdtaic.atto <= d->cdtaip.atto) { 144 if (d->cdtaic.sec.x <= d->cdtaip.sec.x || d->cdtaic.nano <= d->cdtaip.nano || d->cdtaic.atto <= d->cdtaip.atto) {
142 fprintf(stderr, "Timestamp going back, ignoring packet\n"); 145 fprintf(stderr, "Timestamp going back, ignoring packet\n");
143 return 0; 146 return 0;
144 } 147 }
145 memcpy(d->cdnonce + nonceoffset, enc + crypto_box_BOXZEROBYTES - noncelength, noncelength); 148 memcpy(d->cdnonce + nonceoffset, enc + crypto_box_BOXZEROBYTES - noncelength, noncelength);
147 if (i = crypto_box_open_afternm(raw, enc, len + crypto_box_ZEROBYTES, d->cdnonce, d->cbefore)) { 150 if (i = crypto_box_open_afternm(raw, enc, len + crypto_box_ZEROBYTES, d->cdnonce, d->cbefore)) {
148 fprintf(stderr, "Decryption failed len=%d result=%d\n", len, i); 151 fprintf(stderr, "Decryption failed len=%d result=%d\n", len, i);
149 return 0; 152 return 0;
150 } 153 }
151 d->cdtaip = d->cdtaic; 154 d->cdtaip = d->cdtaic;
155 fprintf(stderr, "Decoded packet of %d bytes from %d to %d\n", len, enc, raw);
152 return len; 156 return len;
153 } 157 }
154 158
155 static int init(struct qtsession* sess) { 159 static int init(struct qtsession* sess) {
156 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data; 160 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data;
178 fprintf(stderr, "Warning: TIME_WINDOW not set, risking an initial replay attack\n"); 182 fprintf(stderr, "Warning: TIME_WINDOW not set, risking an initial replay attack\n");
179 } 183 }
180 if (envval = getenv("ROLE")) { 184 if (envval = getenv("ROLE")) {
181 d->cenonce[nonceoffset-1] = atoi(envval) ? 1 : 0; 185 d->cenonce[nonceoffset-1] = atoi(envval) ? 1 : 0;
182 } else { 186 } else {
183 d->cenonce[nonceoffset-1] = memcmp(cpublickey, cownpublickey, crypto_box_PUBLICKEYBYTES) ? 1 : 0; 187 d->cenonce[nonceoffset-1] = memcmp(cownpublickey, cpublickey, crypto_box_PUBLICKEYBYTES) > 0 ? 1 : 0;
184 } 188 }
185 d->cdnonce[nonceoffset-1] = d->cenonce[nonceoffset-1] ? 0 : 1; 189 d->cdnonce[nonceoffset-1] = d->cenonce[nonceoffset-1] ? 0 : 1;
190 return 0;
186 } 191 }
187 192
188 #ifdef COMBINED_BINARY 193 #ifdef COMBINED_BINARY
189 int tunmain_nacltai() { 194 int tunmain_nacltai() {
190 #else 195 #else