Mercurial > hg > quicktun
comparison 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 |
comparison
equal
deleted
inserted
replaced
54:4ff8003d0973 | 55:5685fad38195 |
---|---|
58 u_int32_t nano = 1000 * now.tv_usec + 500; | 58 u_int32_t nano = 1000 * now.tv_usec + 500; |
59 b[8] = (nano >> 24) & 0xff; | 59 b[8] = (nano >> 24) & 0xff; |
60 b[9] = (nano >> 16) & 0xff; | 60 b[9] = (nano >> 16) & 0xff; |
61 b[10] = (nano >> 8) & 0xff; | 61 b[10] = (nano >> 8) & 0xff; |
62 b[11] = (nano >> 0) & 0xff; | 62 b[11] = (nano >> 0) & 0xff; |
63 ++b[15] == 0 && ++b[14] == 0 && ++b[13] == 0 && ++b[12] == 0; | 63 if (++b[15] == 0 && ++b[14] == 0 && ++b[13] == 0) ++b[12]; |
64 } | 64 } |
65 | 65 |
66 //Packet format: <16 bytes taia packed timestamp><16 bytes checksum><n bytes encrypted data> | 66 //Packet format: <16 bytes taia packed timestamp><16 bytes checksum><n bytes encrypted data> |
67 | 67 |
68 static int encode(struct qtsession* sess, char* raw, char* enc, int len) { | 68 static int encode(struct qtsession* sess, char* raw, char* enc, int len) { |
69 if (debug) fprintf(stderr, "Encoding packet of %d bytes from %p to %p\n", len, raw, enc); | 69 if (debug) fprintf(stderr, "Encoding packet of %d bytes from %p to %p\n", len, raw, enc); |
70 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data; | 70 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data; |
71 memset(raw, 0, crypto_box_curve25519xsalsa20poly1305_ZEROBYTES); | 71 memset(raw, 0, crypto_box_curve25519xsalsa20poly1305_ZEROBYTES); |
72 taia_now_packed(d->cenonce + nonceoffset, 0); | 72 taia_now_packed(d->cenonce + nonceoffset, 0); |
73 if (crypto_box_curve25519xsalsa20poly1305_afternm(enc, raw, len + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, d->cenonce, d->cbefore)) return errorexit("Encryption failed"); | 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"); |
74 memcpy((void*)(enc + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES - noncelength), d->cenonce + nonceoffset, noncelength); | 74 memcpy((void*)(enc + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES - noncelength), d->cenonce + nonceoffset, noncelength); |
75 len += overhead; | 75 len += overhead; |
76 if (debug) fprintf(stderr, "Encoded packet of %d bytes from %p to %p\n", len, raw, enc); | 76 if (debug) fprintf(stderr, "Encoded packet of %d bytes from %p to %p\n", len, raw, enc); |
77 return len; | 77 return len; |
78 } | 78 } |
100 fprintf(stderr, "Timestamp going back, ignoring packet\n"); | 100 fprintf(stderr, "Timestamp going back, ignoring packet\n"); |
101 return -1; | 101 return -1; |
102 } | 102 } |
103 memcpy(d->cdnonce + nonceoffset, enc, noncelength); | 103 memcpy(d->cdnonce + nonceoffset, enc, noncelength); |
104 memset(enc, 0, crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES); | 104 memset(enc, 0, crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES); |
105 if (i = crypto_box_curve25519xsalsa20poly1305_open_afternm(raw, enc, len + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, d->cdnonce, d->cbefore)) { | 105 if (crypto_box_curve25519xsalsa20poly1305_open_afternm((unsigned char*)raw, (unsigned char*)enc, len + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, d->cdnonce, d->cbefore)) { |
106 fprintf(stderr, "Decryption failed len=%d result=%d\n", len, i); | 106 fprintf(stderr, "Decryption failed len=%d\n", len); |
107 return -1; | 107 return -1; |
108 } | 108 } |
109 memcpy(taiold, d->cdnonce + nonceoffset, 16); | 109 memcpy(taiold, d->cdnonce + nonceoffset, 16); |
110 if (debug) fprintf(stderr, "Decoded packet of %d bytes from %p to %p\n", len, enc, raw); | 110 if (debug) fprintf(stderr, "Decoded packet of %d bytes from %p to %p\n", len, enc, raw); |
111 return len; | 111 return len; |
117 printf("Initializing cryptography...\n"); | 117 printf("Initializing cryptography...\n"); |
118 unsigned char cownpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], cpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], csecretkey[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES]; | 118 unsigned char cownpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], cpublickey[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES], csecretkey[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES]; |
119 if (!(envval = getconf("PUBLIC_KEY"))) return errorexit("Missing PUBLIC_KEY"); | 119 if (!(envval = getconf("PUBLIC_KEY"))) return errorexit("Missing PUBLIC_KEY"); |
120 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PUBLIC_KEY length"); | 120 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PUBLIC_KEY length"); |
121 hex2bin(cpublickey, envval, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES); | 121 hex2bin(cpublickey, envval, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES); |
122 if (envval = getconf("PRIVATE_KEY")) { | 122 if ((envval = getconf("PRIVATE_KEY"))) { |
123 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PRIVATE_KEY length"); | 123 if (strlen(envval) != 2*crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) return errorexit("PRIVATE_KEY length"); |
124 hex2bin(csecretkey, envval, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); | 124 hex2bin(csecretkey, envval, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); |
125 } else if (envval = getconf("PRIVATE_KEY_FILE")) { | 125 } else if ((envval = getconf("PRIVATE_KEY_FILE"))) { |
126 FILE* pkfile = fopen(envval, "rb"); | 126 FILE* pkfile = fopen(envval, "rb"); |
127 if (!pkfile) return errorexitp("Could not open PRIVATE_KEY_FILE"); | 127 if (!pkfile) return errorexitp("Could not open PRIVATE_KEY_FILE"); |
128 char pktextbuf[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES * 2]; | 128 char pktextbuf[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES * 2]; |
129 size_t pktextsize = fread(pktextbuf, 1, sizeof(pktextbuf), pkfile); | 129 const size_t pktextsize = fread(pktextbuf, 1, sizeof(pktextbuf), pkfile); |
130 if (pktextsize == crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) { | 130 if (pktextsize == crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) { |
131 memcpy(csecretkey, pktextbuf, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); | 131 memcpy(csecretkey, pktextbuf, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); |
132 } else if (pktextsize = 2 * crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) { | 132 } else if (pktextsize == 2 * crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) { |
133 hex2bin(csecretkey, pktextbuf, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); | 133 hex2bin(csecretkey, pktextbuf, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); |
134 } else { | 134 } else { |
135 return errorexit("PRIVATE_KEY length"); | 135 return errorexit("PRIVATE_KEY length"); |
136 } | 136 } |
137 fclose(pkfile); | 137 fclose(pkfile); |
144 memset(d->cdnonce, 0, crypto_box_curve25519xsalsa20poly1305_NONCEBYTES); | 144 memset(d->cdnonce, 0, crypto_box_curve25519xsalsa20poly1305_NONCEBYTES); |
145 memset(d->cdtailog, 0, 5 * 16); | 145 memset(d->cdtailog, 0, 5 * 16); |
146 | 146 |
147 crypto_scalarmult_curve25519_base(cownpublickey, csecretkey); | 147 crypto_scalarmult_curve25519_base(cownpublickey, csecretkey); |
148 | 148 |
149 if (envval = getconf("TIME_WINDOW")) { | 149 if ((envval = getconf("TIME_WINDOW"))) { |
150 struct packedtaia* tailog = d->cdtailog; | 150 struct packedtaia* tailog = d->cdtailog; |
151 taia_now_packed((unsigned char*)&tailog[0], -atol(envval)); | 151 taia_now_packed((unsigned char*)&tailog[0], -atol(envval)); |
152 tailog[4] = tailog[3] = tailog[2] = tailog[1] = tailog[0]; | 152 tailog[4] = tailog[3] = tailog[2] = tailog[1] = tailog[0]; |
153 } else { | 153 } else { |
154 fprintf(stderr, "Warning: TIME_WINDOW not set, risking an initial replay attack\n"); | 154 fprintf(stderr, "Warning: TIME_WINDOW not set, risking an initial replay attack\n"); |
155 } | 155 } |
156 int role = memcmp(cownpublickey, cpublickey, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES); | 156 int role = memcmp(cownpublickey, cpublickey, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES); |
157 if (envval = getconf("ROLE")) role = atoi(envval) ? 1 : -1; | 157 if ((envval = getconf("ROLE"))) role = atoi(envval) ? 1 : -1; |
158 role = (role == 0) ? 0 : ((role > 0) ? 1 : 2); | 158 role = (role == 0) ? 0 : ((role > 0) ? 1 : 2); |
159 d->cenonce[nonceoffset-1] = role & 1; | 159 d->cenonce[nonceoffset-1] = role & 1; |
160 d->cdnonce[nonceoffset-1] = (role >> 1) & 1; | 160 d->cdnonce[nonceoffset-1] = (role >> 1) & 1; |
161 return 0; | 161 return 0; |
162 } | 162 } |