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 b7e3ced7..0a7ff665 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 | #define CONN_NO_CONNECTION 0 | 34 | #define CONN_NO_CONNECTION 0 |
34 | #define CONN_HANDSHAKE_SENT 1 | 35 | #define CONN_HANDSHAKE_SENT 1 |
@@ -268,8 +269,8 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t * | |||
268 | return -1; | 269 | return -1; |
269 | 270 | ||
270 | packet[0] = NET_PACKET_CRYPTO; | 271 | packet[0] = NET_PACKET_CRYPTO; |
271 | memcpy(packet + 1, recv_public_key, crypto_box_PUBLICKEYBYTES); | 272 | id_copy(packet + 1, recv_public_key); |
272 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, send_public_key, crypto_box_PUBLICKEYBYTES); | 273 | id_copy(packet + 1 + crypto_box_PUBLICKEYBYTES, send_public_key); |
273 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES); | 274 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES); |
274 | 275 | ||
275 | return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES; | 276 | return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES; |
@@ -286,8 +287,8 @@ int handle_request(uint8_t *self_public_key, uint8_t *self_secret_key, uint8_t * | |||
286 | { | 287 | { |
287 | if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | 288 | if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && |
288 | length <= MAX_DATA_SIZE) { | 289 | length <= MAX_DATA_SIZE) { |
289 | if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { | 290 | if (id_equal(packet + 1, self_public_key)) { |
290 | memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES); | 291 | id_copy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES); |
291 | uint8_t nonce[crypto_box_NONCEBYTES]; | 292 | uint8_t nonce[crypto_box_NONCEBYTES]; |
292 | uint8_t temp[MAX_DATA_SIZE]; | 293 | uint8_t temp[MAX_DATA_SIZE]; |
293 | memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES); | 294 | memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES); |
@@ -323,7 +324,7 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui | |||
323 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) | 324 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) |
324 | return 1; | 325 | return 1; |
325 | 326 | ||
326 | if (memcmp(packet + 1, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { // Check if request is for us. | 327 | if (id_equal(packet + 1, dht->c->self_public_key)) { // Check if request is for us. |
327 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 328 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
328 | uint8_t data[MAX_DATA_SIZE]; | 329 | uint8_t data[MAX_DATA_SIZE]; |
329 | uint8_t number; | 330 | uint8_t number; |
@@ -361,7 +362,7 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi | |||
361 | 362 | ||
362 | new_nonce(nonce); | 363 | new_nonce(nonce); |
363 | memcpy(temp, secret_nonce, crypto_box_NONCEBYTES); | 364 | memcpy(temp, secret_nonce, crypto_box_NONCEBYTES); |
364 | memcpy(temp + crypto_box_NONCEBYTES, session_key, crypto_box_PUBLICKEYBYTES); | 365 | id_copy(temp + crypto_box_NONCEBYTES, session_key); |
365 | 366 | ||
366 | int len = encrypt_data(public_key, c->self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, | 367 | int len = encrypt_data(public_key, c->self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, |
367 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); | 368 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); |
@@ -370,7 +371,7 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi | |||
370 | return 0; | 371 | return 0; |
371 | 372 | ||
372 | temp_data[0] = 2; | 373 | temp_data[0] = 2; |
373 | memcpy(temp_data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); | 374 | id_copy(temp_data + 1, c->self_public_key); |
374 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); | 375 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); |
375 | return write_packet(c->lossless_udp, connection_id, temp_data, | 376 | return write_packet(c->lossless_udp, connection_id, temp_data, |
376 | len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); | 377 | len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); |
@@ -396,7 +397,7 @@ static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *s | |||
396 | 397 | ||
397 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; | 398 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; |
398 | 399 | ||
399 | memcpy(public_key, data + 1, crypto_box_PUBLICKEYBYTES); | 400 | id_copy(public_key, data + 1); |
400 | 401 | ||
401 | int len = decrypt_data(public_key, c->self_secret_key, data + 1 + crypto_box_PUBLICKEYBYTES, | 402 | int len = decrypt_data(public_key, c->self_secret_key, data + 1 + crypto_box_PUBLICKEYBYTES, |
402 | data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, | 403 | data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, |
@@ -406,7 +407,7 @@ static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *s | |||
406 | return 0; | 407 | return 0; |
407 | 408 | ||
408 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); | 409 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); |
409 | memcpy(session_key, temp + crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES); | 410 | id_copy(session_key, temp + crypto_box_NONCEBYTES); |
410 | return 1; | 411 | return 1; |
411 | } | 412 | } |
412 | 413 | ||
@@ -419,11 +420,10 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) | |||
419 | { | 420 | { |
420 | uint32_t i; | 421 | uint32_t i; |
421 | 422 | ||
422 | for (i = 0; i < c->crypto_connections_length; ++i) { | 423 | for (i = 0; i < c->crypto_connections_length; ++i) |
423 | if (c->crypto_connections[i].status != CONN_NO_CONNECTION) | 424 | if (c->crypto_connections[i].status != CONN_NO_CONNECTION) |
424 | if (memcmp(public_key, c->crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) | 425 | if (id_equal(public_key, c->crypto_connections[i].public_key)) |
425 | return i; | 426 | return i; |
426 | } | ||
427 | 427 | ||
428 | return -1; | 428 | return -1; |
429 | } | 429 | } |
@@ -483,7 +483,7 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) | |||
483 | c->crypto_connections[i].number = id_new; | 483 | c->crypto_connections[i].number = id_new; |
484 | c->crypto_connections[i].status = CONN_HANDSHAKE_SENT; | 484 | c->crypto_connections[i].status = CONN_HANDSHAKE_SENT; |
485 | random_nonce(c->crypto_connections[i].recv_nonce); | 485 | random_nonce(c->crypto_connections[i].recv_nonce); |
486 | memcpy(c->crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); | 486 | id_copy(c->crypto_connections[i].public_key, public_key); |
487 | crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key); | 487 | crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key); |
488 | c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT; | 488 | c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT; |
489 | 489 | ||
@@ -604,9 +604,9 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
604 | c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT; | 604 | c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT; |
605 | random_nonce(c->crypto_connections[i].recv_nonce); | 605 | random_nonce(c->crypto_connections[i].recv_nonce); |
606 | memcpy(c->crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); | 606 | memcpy(c->crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); |
607 | memcpy(c->crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | 607 | id_copy(c->crypto_connections[i].peersessionpublic_key, session_key); |
608 | increment_nonce(c->crypto_connections[i].sent_nonce); | 608 | increment_nonce(c->crypto_connections[i].sent_nonce); |
609 | memcpy(c->crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); | 609 | id_copy(c->crypto_connections[i].public_key, public_key); |
610 | 610 | ||
611 | crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key); | 611 | crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key); |
612 | 612 | ||
@@ -658,7 +658,7 @@ void new_keys(Net_Crypto *c) | |||
658 | */ | 658 | */ |
659 | void save_keys(Net_Crypto *c, uint8_t *keys) | 659 | void save_keys(Net_Crypto *c, uint8_t *keys) |
660 | { | 660 | { |
661 | memcpy(keys, c->self_public_key, crypto_box_PUBLICKEYBYTES); | 661 | id_copy(keys, c->self_public_key); |
662 | memcpy(keys + crypto_box_PUBLICKEYBYTES, c->self_secret_key, crypto_box_SECRETKEYBYTES); | 662 | memcpy(keys + crypto_box_PUBLICKEYBYTES, c->self_secret_key, crypto_box_SECRETKEYBYTES); |
663 | } | 663 | } |
664 | 664 | ||
@@ -667,7 +667,7 @@ void save_keys(Net_Crypto *c, uint8_t *keys) | |||
667 | */ | 667 | */ |
668 | void load_keys(Net_Crypto *c, uint8_t *keys) | 668 | void load_keys(Net_Crypto *c, uint8_t *keys) |
669 | { | 669 | { |
670 | memcpy(c->self_public_key, keys, crypto_box_PUBLICKEYBYTES); | 670 | id_copy(c->self_public_key, keys); |
671 | memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); | 671 | memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); |
672 | } | 672 | } |
673 | 673 | ||
@@ -692,9 +692,9 @@ static void receive_crypto(Net_Crypto *c) | |||
692 | len = read_packet(c->lossless_udp, c->crypto_connections[i].number, temp_data); | 692 | len = read_packet(c->lossless_udp, c->crypto_connections[i].number, temp_data); |
693 | 693 | ||
694 | if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { | 694 | if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { |
695 | if (memcmp(public_key, c->crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) { | 695 | if (id_equal(public_key, c->crypto_connections[i].public_key)) { |
696 | memcpy(c->crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); | 696 | memcpy(c->crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); |
697 | memcpy(c->crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | 697 | id_copy(c->crypto_connections[i].peersessionpublic_key, session_key); |
698 | increment_nonce(c->crypto_connections[i].sent_nonce); | 698 | increment_nonce(c->crypto_connections[i].sent_nonce); |
699 | uint32_t zero = 0; | 699 | uint32_t zero = 0; |
700 | encrypt_precompute(c->crypto_connections[i].peersessionpublic_key, | 700 | encrypt_precompute(c->crypto_connections[i].peersessionpublic_key, |