summaryrefslogtreecommitdiff
path: root/toxcore/TCP_server.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-12-19 02:47:42 +0000
committeriphydf <iphydf@users.noreply.github.com>2016-12-22 10:26:59 +0000
commitce29c8e7ec91d95167b2dea3aee9fd1ae1aac254 (patch)
treea288df55c44e8edf816e6abbde19a70faef73394 /toxcore/TCP_server.c
parent7122d2e862e028a730478d88cd61557fbed16ebf (diff)
Wrap all sodium/nacl functions in crypto_core.c.
Diffstat (limited to 'toxcore/TCP_server.c')
-rw-r--r--toxcore/TCP_server.c72
1 files changed, 36 insertions, 36 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index 63c1577d..0e2d0085 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -42,8 +42,8 @@ struct TCP_Server {
42 sock_t *socks_listening; 42 sock_t *socks_listening;
43 unsigned int num_listening_socks; 43 unsigned int num_listening_socks;
44 44
45 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 45 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
46 uint8_t secret_key[crypto_box_SECRETKEYBYTES]; 46 uint8_t secret_key[CRYPTO_SECRET_KEY_SIZE];
47 TCP_Secure_Connection incomming_connection_queue[MAX_INCOMMING_CONNECTIONS]; 47 TCP_Secure_Connection incomming_connection_queue[MAX_INCOMMING_CONNECTIONS];
48 uint16_t incomming_connection_queue_index; 48 uint16_t incomming_connection_queue_index;
49 TCP_Secure_Connection unconfirmed_connection_queue[MAX_INCOMMING_CONNECTIONS]; 49 TCP_Secure_Connection unconfirmed_connection_queue[MAX_INCOMMING_CONNECTIONS];
@@ -212,7 +212,7 @@ static int del_accepted(TCP_Server *TCP_server, int index)
212 return -1; 212 return -1;
213 } 213 }
214 214
215 sodium_memzero(&TCP_server->accepted_connection_array[index], sizeof(TCP_Secure_Connection)); 215 crypto_memzero(&TCP_server->accepted_connection_array[index], sizeof(TCP_Secure_Connection));
216 --TCP_server->num_accepted_connections; 216 --TCP_server->num_accepted_connections;
217 217
218 if (TCP_server->num_accepted_connections == 0) { 218 if (TCP_server->num_accepted_connections == 0) {
@@ -314,7 +314,7 @@ int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length,
314 *next_packet_length = len; 314 *next_packet_length = len;
315 } 315 }
316 316
317 if (max_len + crypto_box_MACBYTES < *next_packet_length) { 317 if (max_len + CRYPTO_MAC_SIZE < *next_packet_length) {
318 return -1; 318 return -1;
319 } 319 }
320 320
@@ -329,7 +329,7 @@ int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length,
329 329
330 int len = decrypt_data_symmetric(shared_key, recv_nonce, data_encrypted, len_packet, data); 330 int len = decrypt_data_symmetric(shared_key, recv_nonce, data_encrypted, len_packet, data);
331 331
332 if (len + crypto_box_MACBYTES != len_packet) { 332 if (len + CRYPTO_MAC_SIZE != len_packet) {
333 return -1; 333 return -1;
334 } 334 }
335 335
@@ -437,7 +437,7 @@ static bool add_priority(TCP_Secure_Connection *con, const uint8_t *packet, uint
437static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length, 437static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length,
438 bool priority) 438 bool priority)
439{ 439{
440 if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) { 440 if (length + CRYPTO_MAC_SIZE > MAX_PACKET_SIZE) {
441 return -1; 441 return -1;
442 } 442 }
443 443
@@ -451,9 +451,9 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
451 } 451 }
452 } 452 }
453 453
454 uint8_t packet[sizeof(uint16_t) + length + crypto_box_MACBYTES]; 454 uint8_t packet[sizeof(uint16_t) + length + CRYPTO_MAC_SIZE];
455 455
456 uint16_t c_length = htons(length + crypto_box_MACBYTES); 456 uint16_t c_length = htons(length + CRYPTO_MAC_SIZE);
457 memcpy(packet, &c_length, sizeof(uint16_t)); 457 memcpy(packet, &c_length, sizeof(uint16_t));
458 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); 458 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
459 459
@@ -500,7 +500,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
500static void kill_TCP_connection(TCP_Secure_Connection *con) 500static void kill_TCP_connection(TCP_Secure_Connection *con)
501{ 501{
502 kill_sock(con->sock); 502 kill_sock(con->sock);
503 sodium_memzero(con, sizeof(TCP_Secure_Connection)); 503 crypto_memzero(con, sizeof(TCP_Secure_Connection));
504} 504}
505 505
506static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number); 506static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number);
@@ -546,31 +546,31 @@ static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data,
546 return -1; 546 return -1;
547 } 547 }
548 548
549 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 549 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
550 encrypt_precompute(data, self_secret_key, shared_key); 550 encrypt_precompute(data, self_secret_key, shared_key);
551 uint8_t plain[TCP_HANDSHAKE_PLAIN_SIZE]; 551 uint8_t plain[TCP_HANDSHAKE_PLAIN_SIZE];
552 int len = decrypt_data_symmetric(shared_key, data + crypto_box_PUBLICKEYBYTES, 552 int len = decrypt_data_symmetric(shared_key, data + CRYPTO_PUBLIC_KEY_SIZE,
553 data + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, TCP_HANDSHAKE_PLAIN_SIZE + crypto_box_MACBYTES, plain); 553 data + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, TCP_HANDSHAKE_PLAIN_SIZE + CRYPTO_MAC_SIZE, plain);
554 554
555 if (len != TCP_HANDSHAKE_PLAIN_SIZE) { 555 if (len != TCP_HANDSHAKE_PLAIN_SIZE) {
556 return -1; 556 return -1;
557 } 557 }
558 558
559 memcpy(con->public_key, data, crypto_box_PUBLICKEYBYTES); 559 memcpy(con->public_key, data, CRYPTO_PUBLIC_KEY_SIZE);
560 uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES]; 560 uint8_t temp_secret_key[CRYPTO_SECRET_KEY_SIZE];
561 uint8_t resp_plain[TCP_HANDSHAKE_PLAIN_SIZE]; 561 uint8_t resp_plain[TCP_HANDSHAKE_PLAIN_SIZE];
562 crypto_box_keypair(resp_plain, temp_secret_key); 562 crypto_new_keypair(resp_plain, temp_secret_key);
563 random_nonce(con->sent_nonce); 563 random_nonce(con->sent_nonce);
564 memcpy(resp_plain + crypto_box_PUBLICKEYBYTES, con->sent_nonce, crypto_box_NONCEBYTES); 564 memcpy(resp_plain + CRYPTO_PUBLIC_KEY_SIZE, con->sent_nonce, CRYPTO_NONCE_SIZE);
565 memcpy(con->recv_nonce, plain + crypto_box_PUBLICKEYBYTES, crypto_box_NONCEBYTES); 565 memcpy(con->recv_nonce, plain + CRYPTO_PUBLIC_KEY_SIZE, CRYPTO_NONCE_SIZE);
566 566
567 uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; 567 uint8_t response[TCP_SERVER_HANDSHAKE_SIZE];
568 random_nonce(response); 568 random_nonce(response);
569 569
570 len = encrypt_data_symmetric(shared_key, response, resp_plain, TCP_HANDSHAKE_PLAIN_SIZE, 570 len = encrypt_data_symmetric(shared_key, response, resp_plain, TCP_HANDSHAKE_PLAIN_SIZE,
571 response + crypto_box_NONCEBYTES); 571 response + CRYPTO_NONCE_SIZE);
572 572
573 if (len != TCP_HANDSHAKE_PLAIN_SIZE + crypto_box_MACBYTES) { 573 if (len != TCP_HANDSHAKE_PLAIN_SIZE + CRYPTO_MAC_SIZE) {
574 return -1; 574 return -1;
575 } 575 }
576 576
@@ -605,10 +605,10 @@ static int read_connection_handshake(TCP_Secure_Connection *con, const uint8_t *
605 */ 605 */
606static int send_routing_response(TCP_Secure_Connection *con, uint8_t rpid, const uint8_t *public_key) 606static int send_routing_response(TCP_Secure_Connection *con, uint8_t rpid, const uint8_t *public_key)
607{ 607{
608 uint8_t data[1 + 1 + crypto_box_PUBLICKEYBYTES]; 608 uint8_t data[1 + 1 + CRYPTO_PUBLIC_KEY_SIZE];
609 data[0] = TCP_PACKET_ROUTING_RESPONSE; 609 data[0] = TCP_PACKET_ROUTING_RESPONSE;
610 data[1] = rpid; 610 data[1] = rpid;
611 memcpy(data + 2, public_key, crypto_box_PUBLICKEYBYTES); 611 memcpy(data + 2, public_key, CRYPTO_PUBLIC_KEY_SIZE);
612 612
613 return write_packet_TCP_secure_connection(con, data, sizeof(data), 1); 613 return write_packet_TCP_secure_connection(con, data, sizeof(data), 1);
614} 614}
@@ -684,7 +684,7 @@ static int handle_TCP_routing_req(TCP_Server *TCP_server, uint32_t con_id, const
684 } 684 }
685 685
686 con->connections[index].status = 1; 686 con->connections[index].status = 1;
687 memcpy(con->connections[index].public_key, public_key, crypto_box_PUBLICKEYBYTES); 687 memcpy(con->connections[index].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
688 int other_index = get_TCP_connection_index(TCP_server, public_key); 688 int other_index = get_TCP_connection_index(TCP_server, public_key);
689 689
690 if (other_index != -1) { 690 if (other_index != -1) {
@@ -730,10 +730,10 @@ static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, const ui
730 int other_index = get_TCP_connection_index(TCP_server, public_key); 730 int other_index = get_TCP_connection_index(TCP_server, public_key);
731 731
732 if (other_index != -1) { 732 if (other_index != -1) {
733 uint8_t resp_packet[1 + crypto_box_PUBLICKEYBYTES + length]; 733 uint8_t resp_packet[1 + CRYPTO_PUBLIC_KEY_SIZE + length];
734 resp_packet[0] = TCP_PACKET_OOB_RECV; 734 resp_packet[0] = TCP_PACKET_OOB_RECV;
735 memcpy(resp_packet + 1, con->public_key, crypto_box_PUBLICKEYBYTES); 735 memcpy(resp_packet + 1, con->public_key, CRYPTO_PUBLIC_KEY_SIZE);
736 memcpy(resp_packet + 1 + crypto_box_PUBLICKEYBYTES, data, length); 736 memcpy(resp_packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length);
737 write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[other_index], resp_packet, 737 write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[other_index], resp_packet,
738 sizeof(resp_packet), 0); 738 sizeof(resp_packet), 0);
739 } 739 }
@@ -817,7 +817,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint
817 817
818 switch (data[0]) { 818 switch (data[0]) {
819 case TCP_PACKET_ROUTING_REQUEST: { 819 case TCP_PACKET_ROUTING_REQUEST: {
820 if (length != 1 + crypto_box_PUBLICKEYBYTES) { 820 if (length != 1 + CRYPTO_PUBLIC_KEY_SIZE) {
821 return -1; 821 return -1;
822 } 822 }
823 823
@@ -872,17 +872,17 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint
872 } 872 }
873 873
874 case TCP_PACKET_OOB_SEND: { 874 case TCP_PACKET_OOB_SEND: {
875 if (length <= 1 + crypto_box_PUBLICKEYBYTES) { 875 if (length <= 1 + CRYPTO_PUBLIC_KEY_SIZE) {
876 return -1; 876 return -1;
877 } 877 }
878 878
879 return handle_TCP_oob_send(TCP_server, con_id, data + 1, data + 1 + crypto_box_PUBLICKEYBYTES, 879 return handle_TCP_oob_send(TCP_server, con_id, data + 1, data + 1 + CRYPTO_PUBLIC_KEY_SIZE,
880 length - (1 + crypto_box_PUBLICKEYBYTES)); 880 length - (1 + CRYPTO_PUBLIC_KEY_SIZE));
881 } 881 }
882 882
883 case TCP_PACKET_ONION_REQUEST: { 883 case TCP_PACKET_ONION_REQUEST: {
884 if (TCP_server->onion) { 884 if (TCP_server->onion) {
885 if (length <= 1 + crypto_box_NONCEBYTES + ONION_SEND_BASE * 2) { 885 if (length <= 1 + CRYPTO_NONCE_SIZE + ONION_SEND_BASE * 2) {
886 return -1; 886 return -1;
887 } 887 }
888 888
@@ -892,7 +892,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint
892 source.ip.ip6.uint32[0] = con_id; 892 source.ip.ip6.uint32[0] = con_id;
893 source.ip.ip6.uint32[1] = 0; 893 source.ip.ip6.uint32[1] = 0;
894 source.ip.ip6.uint64[1] = con->identifier; 894 source.ip.ip6.uint64[1] = con->identifier;
895 onion_send_1(TCP_server->onion, data + 1 + crypto_box_NONCEBYTES, length - (1 + crypto_box_NONCEBYTES), source, 895 onion_send_1(TCP_server->onion, data + 1 + CRYPTO_NONCE_SIZE, length - (1 + CRYPTO_NONCE_SIZE), source,
896 data + 1); 896 data + 1);
897 } 897 }
898 898
@@ -951,7 +951,7 @@ static int confirm_TCP_connection(TCP_Server *TCP_server, TCP_Secure_Connection
951 return -1; 951 return -1;
952 } 952 }
953 953
954 sodium_memzero(con, sizeof(TCP_Secure_Connection)); 954 crypto_memzero(con, sizeof(TCP_Secure_Connection));
955 955
956 if (handle_TCP_packet(TCP_server, index, data, length) == -1) { 956 if (handle_TCP_packet(TCP_server, index, data, length) == -1) {
957 kill_accepted(TCP_server, index); 957 kill_accepted(TCP_server, index);
@@ -1102,10 +1102,10 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin
1102 set_callback_handle_recv_1(onion, &handle_onion_recv_1, temp); 1102 set_callback_handle_recv_1(onion, &handle_onion_recv_1, temp);
1103 } 1103 }
1104 1104
1105 memcpy(temp->secret_key, secret_key, crypto_box_SECRETKEYBYTES); 1105 memcpy(temp->secret_key, secret_key, CRYPTO_SECRET_KEY_SIZE);
1106 crypto_scalarmult_curve25519_base(temp->public_key, temp->secret_key); 1106 crypto_derive_public_key(temp->public_key, temp->secret_key);
1107 1107
1108 bs_list_init(&temp->accepted_key_list, crypto_box_PUBLICKEYBYTES, 8); 1108 bs_list_init(&temp->accepted_key_list, CRYPTO_PUBLIC_KEY_SIZE, 8);
1109 1109
1110 return temp; 1110 return temp;
1111} 1111}
@@ -1145,7 +1145,7 @@ static int do_incoming(TCP_Server *TCP_server, uint32_t i)
1145 } 1145 }
1146 1146
1147 memcpy(conn_new, conn_old, sizeof(TCP_Secure_Connection)); 1147 memcpy(conn_new, conn_old, sizeof(TCP_Secure_Connection));
1148 sodium_memzero(conn_old, sizeof(TCP_Secure_Connection)); 1148 crypto_memzero(conn_old, sizeof(TCP_Secure_Connection));
1149 ++TCP_server->unconfirmed_connection_queue_index; 1149 ++TCP_server->unconfirmed_connection_queue_index;
1150 1150
1151 return index_new; 1151 return index_new;