diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index a9aa77f9..7ae7c502 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -29,6 +29,7 @@ | |||
29 | #endif | 29 | #endif |
30 | 30 | ||
31 | #include "net_crypto.h" | 31 | #include "net_crypto.h" |
32 | #include "util.h" | ||
32 | 33 | ||
33 | static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) | 34 | static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) |
34 | { | 35 | { |
@@ -262,8 +263,8 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t * | |||
262 | return -1; | 263 | return -1; |
263 | 264 | ||
264 | packet[0] = NET_PACKET_CRYPTO; | 265 | packet[0] = NET_PACKET_CRYPTO; |
265 | memcpy(packet + 1, recv_public_key, crypto_box_PUBLICKEYBYTES); | 266 | id_copy(packet + 1, recv_public_key); |
266 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, send_public_key, crypto_box_PUBLICKEYBYTES); | 267 | id_copy(packet + 1 + crypto_box_PUBLICKEYBYTES, send_public_key); |
267 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES); | 268 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES); |
268 | 269 | ||
269 | return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES; | 270 | return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES; |
@@ -280,8 +281,8 @@ int handle_request(uint8_t *self_public_key, uint8_t *self_secret_key, uint8_t * | |||
280 | { | 281 | { |
281 | if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | 282 | if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && |
282 | length <= MAX_DATA_SIZE) { | 283 | length <= MAX_DATA_SIZE) { |
283 | if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { | 284 | if (id_equal(packet + 1, self_public_key)) { |
284 | memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES); | 285 | id_copy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES); |
285 | uint8_t nonce[crypto_box_NONCEBYTES]; | 286 | uint8_t nonce[crypto_box_NONCEBYTES]; |
286 | uint8_t temp[MAX_DATA_SIZE]; | 287 | uint8_t temp[MAX_DATA_SIZE]; |
287 | memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES); | 288 | memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES); |
@@ -317,7 +318,7 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui | |||
317 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) | 318 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) |
318 | return 1; | 319 | return 1; |
319 | 320 | ||
320 | if (memcmp(packet + 1, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { // Check if request is for us. | 321 | if (id_equal(packet + 1, dht->c->self_public_key)) { // Check if request is for us. |
321 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 322 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
322 | uint8_t data[MAX_DATA_SIZE]; | 323 | uint8_t data[MAX_DATA_SIZE]; |
323 | uint8_t number; | 324 | uint8_t number; |
@@ -355,7 +356,7 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi | |||
355 | 356 | ||
356 | new_nonce(nonce); | 357 | new_nonce(nonce); |
357 | memcpy(temp, secret_nonce, crypto_box_NONCEBYTES); | 358 | memcpy(temp, secret_nonce, crypto_box_NONCEBYTES); |
358 | memcpy(temp + crypto_box_NONCEBYTES, session_key, crypto_box_PUBLICKEYBYTES); | 359 | id_copy(temp + crypto_box_NONCEBYTES, session_key); |
359 | 360 | ||
360 | int len = encrypt_data(public_key, c->self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, | 361 | int len = encrypt_data(public_key, c->self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, |
361 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); | 362 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); |
@@ -364,7 +365,7 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi | |||
364 | return 0; | 365 | return 0; |
365 | 366 | ||
366 | temp_data[0] = 2; | 367 | temp_data[0] = 2; |
367 | memcpy(temp_data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); | 368 | id_copy(temp_data + 1, c->self_public_key); |
368 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); | 369 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); |
369 | return write_packet(c->lossless_udp, connection_id, temp_data, | 370 | return write_packet(c->lossless_udp, connection_id, temp_data, |
370 | len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); | 371 | len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); |
@@ -390,7 +391,7 @@ static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *s | |||
390 | 391 | ||
391 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; | 392 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; |
392 | 393 | ||
393 | memcpy(public_key, data + 1, crypto_box_PUBLICKEYBYTES); | 394 | id_copy(public_key, data + 1); |
394 | 395 | ||
395 | int len = decrypt_data(public_key, c->self_secret_key, data + 1 + crypto_box_PUBLICKEYBYTES, | 396 | int len = decrypt_data(public_key, c->self_secret_key, data + 1 + crypto_box_PUBLICKEYBYTES, |
396 | data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, | 397 | data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, |
@@ -400,7 +401,7 @@ static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *s | |||
400 | return 0; | 401 | return 0; |
401 | 402 | ||
402 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); | 403 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); |
403 | memcpy(session_key, temp + crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES); | 404 | id_copy(session_key, temp + crypto_box_NONCEBYTES); |
404 | return 1; | 405 | return 1; |
405 | } | 406 | } |
406 | 407 | ||
@@ -413,11 +414,10 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) | |||
413 | { | 414 | { |
414 | uint32_t i; | 415 | uint32_t i; |
415 | 416 | ||
416 | for (i = 0; i < c->crypto_connections_length; ++i) { | 417 | for (i = 0; i < c->crypto_connections_length; ++i) |
417 | if (c->crypto_connections[i].status != CRYPTO_CONN_NO_CONNECTION) | 418 | if (c->crypto_connections[i].status != CRYPTO_CONN_NO_CONNECTION) |
418 | if (memcmp(public_key, c->crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) | 419 | if (id_equal(public_key, c->crypto_connections[i].public_key)) |
419 | return i; | 420 | return i; |
420 | } | ||
421 | 421 | ||
422 | return -1; | 422 | return -1; |
423 | } | 423 | } |
@@ -477,7 +477,7 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) | |||
477 | c->crypto_connections[i].number = id_new; | 477 | c->crypto_connections[i].number = id_new; |
478 | c->crypto_connections[i].status = CRYPTO_CONN_HANDSHAKE_SENT; | 478 | c->crypto_connections[i].status = CRYPTO_CONN_HANDSHAKE_SENT; |
479 | random_nonce(c->crypto_connections[i].recv_nonce); | 479 | random_nonce(c->crypto_connections[i].recv_nonce); |
480 | memcpy(c->crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); | 480 | id_copy(c->crypto_connections[i].public_key, public_key); |
481 | crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key); | 481 | crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key); |
482 | c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT; | 482 | c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT; |
483 | 483 | ||
@@ -598,9 +598,9 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
598 | c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT; | 598 | c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT; |
599 | random_nonce(c->crypto_connections[i].recv_nonce); | 599 | random_nonce(c->crypto_connections[i].recv_nonce); |
600 | memcpy(c->crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); | 600 | memcpy(c->crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); |
601 | memcpy(c->crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | 601 | id_copy(c->crypto_connections[i].peersessionpublic_key, session_key); |
602 | increment_nonce(c->crypto_connections[i].sent_nonce); | 602 | increment_nonce(c->crypto_connections[i].sent_nonce); |
603 | memcpy(c->crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); | 603 | id_copy(c->crypto_connections[i].public_key, public_key); |
604 | 604 | ||
605 | crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key); | 605 | crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key); |
606 | 606 | ||
@@ -652,7 +652,7 @@ void new_keys(Net_Crypto *c) | |||
652 | */ | 652 | */ |
653 | void save_keys(Net_Crypto *c, uint8_t *keys) | 653 | void save_keys(Net_Crypto *c, uint8_t *keys) |
654 | { | 654 | { |
655 | memcpy(keys, c->self_public_key, crypto_box_PUBLICKEYBYTES); | 655 | id_copy(keys, c->self_public_key); |
656 | memcpy(keys + crypto_box_PUBLICKEYBYTES, c->self_secret_key, crypto_box_SECRETKEYBYTES); | 656 | memcpy(keys + crypto_box_PUBLICKEYBYTES, c->self_secret_key, crypto_box_SECRETKEYBYTES); |
657 | } | 657 | } |
658 | 658 | ||
@@ -661,7 +661,7 @@ void save_keys(Net_Crypto *c, uint8_t *keys) | |||
661 | */ | 661 | */ |
662 | void load_keys(Net_Crypto *c, uint8_t *keys) | 662 | void load_keys(Net_Crypto *c, uint8_t *keys) |
663 | { | 663 | { |
664 | memcpy(c->self_public_key, keys, crypto_box_PUBLICKEYBYTES); | 664 | id_copy(c->self_public_key, keys); |
665 | memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); | 665 | memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); |
666 | } | 666 | } |
667 | 667 | ||
@@ -686,9 +686,9 @@ static void receive_crypto(Net_Crypto *c) | |||
686 | len = read_packet(c->lossless_udp, c->crypto_connections[i].number, temp_data); | 686 | len = read_packet(c->lossless_udp, c->crypto_connections[i].number, temp_data); |
687 | 687 | ||
688 | if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { | 688 | if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { |
689 | if (memcmp(public_key, c->crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) { | 689 | if (id_equal(public_key, c->crypto_connections[i].public_key)) { |
690 | memcpy(c->crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); | 690 | memcpy(c->crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); |
691 | memcpy(c->crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | 691 | id_copy(c->crypto_connections[i].peersessionpublic_key, session_key); |
692 | increment_nonce(c->crypto_connections[i].sent_nonce); | 692 | increment_nonce(c->crypto_connections[i].sent_nonce); |
693 | uint32_t zero = 0; | 693 | uint32_t zero = 0; |
694 | encrypt_precompute(c->crypto_connections[i].peersessionpublic_key, | 694 | encrypt_precompute(c->crypto_connections[i].peersessionpublic_key, |