summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-11-02 21:27:46 +0000
committeriphydf <iphydf@users.noreply.github.com>2016-11-03 11:56:29 +0000
commit64870b6fd2646837b8d61aee712491209cec2864 (patch)
tree4c0fb39dac905625a4ba0e514069afbdfdd24a96 /toxcore/DHT.c
parent96c672aef59ac785f3d351698311bb358820cc3c (diff)
Move packing and unpacking DHT request packets to DHT module.
These definitely don't belong in a module called "crypto core". The DHT module seems like the best place to put them, since they are sent to DHT nodes.
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index b3e68288..b4ba3fd5 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -176,6 +176,86 @@ void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *publi
176 get_shared_key(&dht->shared_keys_sent, shared_key, dht->self_secret_key, public_key); 176 get_shared_key(&dht->shared_keys_sent, shared_key, dht->self_secret_key, public_key);
177} 177}
178 178
179/* Create a request to peer.
180 * send_public_key and send_secret_key are the pub/secret keys of the sender.
181 * recv_public_key is public key of receiver.
182 * packet must be an array of MAX_CRYPTO_REQUEST_SIZE big.
183 * Data represents the data we send with the request with length being the length of the data.
184 * request_id is the id of the request (32 = friend request, 254 = ping request).
185 *
186 * return -1 on failure.
187 * return the length of the created packet on success.
188 */
189int create_request(const uint8_t *send_public_key, const uint8_t *send_secret_key, uint8_t *packet,
190 const uint8_t *recv_public_key, const uint8_t *data, uint32_t length, uint8_t request_id)
191{
192 if (!send_public_key || !packet || !recv_public_key || !data) {
193 return -1;
194 }
195
196 if (MAX_CRYPTO_REQUEST_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 +
197 crypto_box_MACBYTES) {
198 return -1;
199 }
200
201 uint8_t *nonce = packet + 1 + crypto_box_PUBLICKEYBYTES * 2;
202 new_nonce(nonce);
203 uint8_t temp[MAX_CRYPTO_REQUEST_SIZE]; // TODO(irungentoo): sodium_memzero before exit function
204 memcpy(temp + 1, data, length);
205 temp[0] = request_id;
206 int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1,
207 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet);
208
209 if (len == -1) {
210 return -1;
211 }
212
213 packet[0] = NET_PACKET_CRYPTO;
214 memcpy(packet + 1, recv_public_key, crypto_box_PUBLICKEYBYTES);
215 memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, send_public_key, crypto_box_PUBLICKEYBYTES);
216
217 return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES;
218}
219
220/* Puts the senders public key in the request in public_key, the data from the request
221 * in data if a friend or ping request was sent to us and returns the length of the data.
222 * packet is the request packet and length is its length.
223 *
224 * return -1 if not valid request.
225 */
226int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
227 uint8_t *request_id, const uint8_t *packet, uint16_t length)
228{
229 if (!self_public_key || !public_key || !data || !request_id || !packet) {
230 return -1;
231 }
232
233 if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES ||
234 length > MAX_CRYPTO_REQUEST_SIZE) {
235 return -1;
236 }
237
238 if (public_key_cmp(packet + 1, self_public_key) != 0) {
239 return -1;
240 }
241
242 memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES);
243 const uint8_t *nonce = packet + 1 + crypto_box_PUBLICKEYBYTES * 2;
244 uint8_t temp[MAX_CRYPTO_REQUEST_SIZE]; // TODO(irungentoo): sodium_memzero before exit function
245 int len1 = decrypt_data(public_key, self_secret_key, nonce,
246 packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES,
247 length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), temp);
248
249 if (len1 == -1 || len1 == 0) {
250 return -1;
251 }
252
253 request_id[0] = temp[0];
254 --len1;
255 memcpy(data, temp + 1, len1);
256 return len1;
257}
258
179void to_net_family(IP *ip) 259void to_net_family(IP *ip)
180{ 260{
181 if (ip->family == AF_INET) { 261 if (ip->family == AF_INET) {