summaryrefslogtreecommitdiff
path: root/toxcore/crypto_core.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/crypto_core.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/crypto_core.c')
-rw-r--r--toxcore/crypto_core.c80
1 files changed, 0 insertions, 80 deletions
diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c
index 8f7572ab..2ecac662 100644
--- a/toxcore/crypto_core.c
+++ b/toxcore/crypto_core.c
@@ -215,83 +215,3 @@ void new_nonce(uint8_t *nonce)
215{ 215{
216 random_nonce(nonce); 216 random_nonce(nonce);
217} 217}
218
219/* Create a request to peer.
220 * send_public_key and send_secret_key are the pub/secret keys of the sender.
221 * recv_public_key is public key of receiver.
222 * packet must be an array of MAX_CRYPTO_REQUEST_SIZE big.
223 * Data represents the data we send with the request with length being the length of the data.
224 * request_id is the id of the request (32 = friend request, 254 = ping request).
225 *
226 * return -1 on failure.
227 * return the length of the created packet on success.
228 */
229int create_request(const uint8_t *send_public_key, const uint8_t *send_secret_key, uint8_t *packet,
230 const uint8_t *recv_public_key, const uint8_t *data, uint32_t length, uint8_t request_id)
231{
232 if (!send_public_key || !packet || !recv_public_key || !data) {
233 return -1;
234 }
235
236 if (MAX_CRYPTO_REQUEST_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 +
237 crypto_box_MACBYTES) {
238 return -1;
239 }
240
241 uint8_t *nonce = packet + 1 + crypto_box_PUBLICKEYBYTES * 2;
242 new_nonce(nonce);
243 uint8_t temp[MAX_CRYPTO_REQUEST_SIZE]; // TODO(irungentoo): sodium_memzero before exit function
244 memcpy(temp + 1, data, length);
245 temp[0] = request_id;
246 int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1,
247 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet);
248
249 if (len == -1) {
250 return -1;
251 }
252
253 packet[0] = NET_PACKET_CRYPTO;
254 memcpy(packet + 1, recv_public_key, crypto_box_PUBLICKEYBYTES);
255 memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, send_public_key, crypto_box_PUBLICKEYBYTES);
256
257 return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES;
258}
259
260/* Puts the senders public key in the request in public_key, the data from the request
261 * in data if a friend or ping request was sent to us and returns the length of the data.
262 * packet is the request packet and length is its length.
263 *
264 * return -1 if not valid request.
265 */
266int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
267 uint8_t *request_id, const uint8_t *packet, uint16_t length)
268{
269 if (!self_public_key || !public_key || !data || !request_id || !packet) {
270 return -1;
271 }
272
273 if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES ||
274 length > MAX_CRYPTO_REQUEST_SIZE) {
275 return -1;
276 }
277
278 if (public_key_cmp(packet + 1, self_public_key) != 0) {
279 return -1;
280 }
281
282 memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES);
283 const uint8_t *nonce = packet + 1 + crypto_box_PUBLICKEYBYTES * 2;
284 uint8_t temp[MAX_CRYPTO_REQUEST_SIZE]; // TODO(irungentoo): sodium_memzero before exit function
285 int len1 = decrypt_data(public_key, self_secret_key, nonce,
286 packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES,
287 length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), temp);
288
289 if (len1 == -1 || len1 == 0) {
290 return -1;
291 }
292
293 request_id[0] = temp[0];
294 --len1;
295 memcpy(data, temp + 1, len1);
296 return len1;
297}