summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--auto_tests/assoc_test.c12
-rw-r--r--auto_tests/dht_test.c10
-rw-r--r--testing/DHT_test.c8
-rw-r--r--toxcore/DHT.c81
-rw-r--r--toxcore/DHT.h9
-rw-r--r--toxcore/assoc.c12
-rw-r--r--toxcore/ping.c34
-rw-r--r--toxcore/util.c10
8 files changed, 87 insertions, 89 deletions
diff --git a/auto_tests/assoc_test.c b/auto_tests/assoc_test.c
index 9de95136..165724e9 100644
--- a/auto_tests/assoc_test.c
+++ b/auto_tests/assoc_test.c
@@ -18,7 +18,7 @@
18START_TEST(test_basics) 18START_TEST(test_basics)
19{ 19{
20 /* TODO: real test */ 20 /* TODO: real test */
21 uint8_t id[CLIENT_ID_SIZE]; 21 uint8_t id[crypto_box_PUBLICKEYBYTES];
22 Assoc *assoc = new_Assoc_default(id); 22 Assoc *assoc = new_Assoc_default(id);
23 ck_assert_msg(assoc != NULL, "failed to create default assoc"); 23 ck_assert_msg(assoc != NULL, "failed to create default assoc");
24 24
@@ -63,19 +63,19 @@ START_TEST(test_fillup)
63{ 63{
64 /* TODO: real test */ 64 /* TODO: real test */
65 int i, j; 65 int i, j;
66 uint8_t id[CLIENT_ID_SIZE]; 66 uint8_t id[crypto_box_PUBLICKEYBYTES];
67 //uint32_t a = current_time(); 67 //uint32_t a = current_time();
68 uint32_t a = 2710106197; 68 uint32_t a = 2710106197;
69 srand(a); 69 srand(a);
70 70
71 for (i = 0; i < CLIENT_ID_SIZE; ++i) { 71 for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) {
72 id[i] = rand(); 72 id[i] = rand();
73 } 73 }
74 74
75 Assoc *assoc = new_Assoc(6, 15, id); 75 Assoc *assoc = new_Assoc(6, 15, id);
76 ck_assert_msg(assoc != NULL, "failed to create default assoc"); 76 ck_assert_msg(assoc != NULL, "failed to create default assoc");
77 struct entry { 77 struct entry {
78 uint8_t id[CLIENT_ID_SIZE]; 78 uint8_t id[crypto_box_PUBLICKEYBYTES];
79 IPPTs ippts_send; 79 IPPTs ippts_send;
80 IP_Port ipp_recv; 80 IP_Port ipp_recv;
81 }; 81 };
@@ -85,7 +85,7 @@ START_TEST(test_fillup)
85 85
86 for (j = 0; j < 128; ++j) { 86 for (j = 0; j < 128; ++j) {
87 87
88 for (i = 0; i < CLIENT_ID_SIZE; ++i) { 88 for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) {
89 entries[j].id[i] = rand(); 89 entries[j].id[i] = rand();
90 } 90 }
91 91
@@ -100,7 +100,7 @@ START_TEST(test_fillup)
100 entries[j].ipp_recv = ipp; 100 entries[j].ipp_recv = ipp;
101 101
102 if (j % 16 == 0) { 102 if (j % 16 == 0) {
103 memcpy(entries[j].id, id, CLIENT_ID_SIZE - 30); 103 memcpy(entries[j].id, id, crypto_box_PUBLICKEYBYTES - 30);
104 memcpy(&closest[j / 16], &entries[j], sizeof(struct entry)); 104 memcpy(&closest[j / 16], &entries[j], sizeof(struct entry));
105 105
106 } 106 }
diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c
index 479eb330..32985bca 100644
--- a/auto_tests/dht_test.c
+++ b/auto_tests/dht_test.c
@@ -85,7 +85,7 @@ void test_addto_lists_update(DHT *dht,
85{ 85{
86 int used, test, test1, test2, found; 86 int used, test, test1, test2, found;
87 IP_Port test_ipp; 87 IP_Port test_ipp;
88 uint8_t test_id[CLIENT_ID_SIZE]; 88 uint8_t test_id[crypto_box_PUBLICKEYBYTES];
89 uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; 89 uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0;
90 90
91 // check id update for existing ip_port 91 // check id update for existing ip_port
@@ -153,7 +153,7 @@ void test_addto_lists_bad(DHT *dht,
153{ 153{
154 // check "bad" clients replacement 154 // check "bad" clients replacement
155 int used, test1, test2, test3; 155 int used, test1, test2, test3;
156 uint8_t public_key[CLIENT_ID_SIZE], test_id1[CLIENT_ID_SIZE], test_id2[CLIENT_ID_SIZE], test_id3[CLIENT_ID_SIZE]; 156 uint8_t public_key[crypto_box_PUBLICKEYBYTES], test_id1[crypto_box_PUBLICKEYBYTES], test_id2[crypto_box_PUBLICKEYBYTES], test_id3[crypto_box_PUBLICKEYBYTES];
157 uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; 157 uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0;
158 158
159 randombytes(public_key, sizeof(public_key)); 159 randombytes(public_key, sizeof(public_key));
@@ -196,7 +196,7 @@ void test_addto_lists_possible_bad(DHT *dht,
196{ 196{
197 // check "possibly bad" clients replacement 197 // check "possibly bad" clients replacement
198 int used, test1, test2, test3; 198 int used, test1, test2, test3;
199 uint8_t public_key[CLIENT_ID_SIZE], test_id1[CLIENT_ID_SIZE], test_id2[CLIENT_ID_SIZE], test_id3[CLIENT_ID_SIZE]; 199 uint8_t public_key[crypto_box_PUBLICKEYBYTES], test_id1[crypto_box_PUBLICKEYBYTES], test_id2[crypto_box_PUBLICKEYBYTES], test_id3[crypto_box_PUBLICKEYBYTES];
200 uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; 200 uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0;
201 201
202 randombytes(public_key, sizeof(public_key)); 202 randombytes(public_key, sizeof(public_key));
@@ -258,7 +258,7 @@ void test_addto_lists_good(DHT *dht,
258 IP_Port *ip_port, 258 IP_Port *ip_port,
259 const uint8_t *comp_client_id) 259 const uint8_t *comp_client_id)
260{ 260{
261 uint8_t public_key[CLIENT_ID_SIZE]; 261 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
262 uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; 262 uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0;
263 263
264 mark_all_good(list, length, ipv6); 264 mark_all_good(list, length, ipv6);
@@ -291,7 +291,7 @@ void test_addto_lists(IP ip)
291 ck_assert_msg(dht != 0, "Failed to create DHT"); 291 ck_assert_msg(dht != 0, "Failed to create DHT");
292 292
293 IP_Port ip_port = { .ip = ip, .port = TOX_PORT_DEFAULT }; 293 IP_Port ip_port = { .ip = ip, .port = TOX_PORT_DEFAULT };
294 uint8_t public_key[CLIENT_ID_SIZE]; 294 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
295 int i, used; 295 int i, used;
296 296
297 // check lists filling 297 // check lists filling
diff --git a/testing/DHT_test.c b/testing/DHT_test.c
index 2f99902f..d07e3959 100644
--- a/testing/DHT_test.c
+++ b/testing/DHT_test.c
@@ -52,13 +52,13 @@
52 52
53#define PORT 33445 53#define PORT 33445
54 54
55uint8_t zeroes_cid[CLIENT_ID_SIZE]; 55uint8_t zeroes_cid[crypto_box_PUBLICKEYBYTES];
56 56
57void print_client_id(uint8_t *public_key) 57void print_client_id(uint8_t *public_key)
58{ 58{
59 uint32_t j; 59 uint32_t j;
60 60
61 for (j = 0; j < CLIENT_ID_SIZE; j++) { 61 for (j = 0; j < crypto_box_PUBLICKEYBYTES; j++) {
62 printf("%02hhX", public_key[j]); 62 printf("%02hhX", public_key[j]);
63 } 63 }
64} 64}
@@ -108,7 +108,7 @@ void print_clientlist(DHT *dht)
108 for (i = 0; i < LCLIENT_LIST; i++) { 108 for (i = 0; i < LCLIENT_LIST; i++) {
109 Client_data *client = &dht->close_clientlist[i]; 109 Client_data *client = &dht->close_clientlist[i];
110 110
111 if (memcmp(client->public_key, zeroes_cid, CLIENT_ID_SIZE) == 0) 111 if (memcmp(client->public_key, zeroes_cid, crypto_box_PUBLICKEYBYTES) == 0)
112 continue; 112 continue;
113 113
114 printf("ClientID: "); 114 printf("ClientID: ");
@@ -139,7 +139,7 @@ void print_friendlist(DHT *dht)
139 for (i = 0; i < MAX_FRIEND_CLIENTS; i++) { 139 for (i = 0; i < MAX_FRIEND_CLIENTS; i++) {
140 Client_data *client = &dht->friends_list[k].client_list[i]; 140 Client_data *client = &dht->friends_list[k].client_list[i];
141 141
142 if (memcmp(client->public_key, zeroes_cid, CLIENT_ID_SIZE) == 0) 142 if (memcmp(client->public_key, zeroes_cid, crypto_box_PUBLICKEYBYTES) == 0)
143 continue; 143 continue;
144 144
145 printf("ClientID: "); 145 printf("ClientID: ");
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 467fff22..ae823302 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -919,7 +919,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const
919 uint8_t plain_message[sizeof(Node_format) * 2] = {0}; 919 uint8_t plain_message[sizeof(Node_format) * 2] = {0};
920 920
921 Node_format receiver; 921 Node_format receiver;
922 memcpy(receiver.public_key, public_key, CLIENT_ID_SIZE); 922 memcpy(receiver.public_key, public_key, crypto_box_PUBLICKEYBYTES);
923 receiver.ip_port = ip_port; 923 receiver.ip_port = ip_port;
924 memcpy(plain_message, &receiver, sizeof(receiver)); 924 memcpy(plain_message, &receiver, sizeof(receiver));
925 925
@@ -935,12 +935,12 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const
935 if (ping_id == 0) 935 if (ping_id == 0)
936 return -1; 936 return -1;
937 937
938 uint8_t plain[CLIENT_ID_SIZE + sizeof(ping_id)]; 938 uint8_t plain[crypto_box_PUBLICKEYBYTES + sizeof(ping_id)];
939 uint8_t encrypt[sizeof(plain) + crypto_box_MACBYTES]; 939 uint8_t encrypt[sizeof(plain) + crypto_box_MACBYTES];
940 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(encrypt)]; 940 uint8_t data[1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + sizeof(encrypt)];
941 941
942 memcpy(plain, client_id, CLIENT_ID_SIZE); 942 memcpy(plain, client_id, crypto_box_PUBLICKEYBYTES);
943 memcpy(plain + CLIENT_ID_SIZE, &ping_id, sizeof(ping_id)); 943 memcpy(plain + crypto_box_PUBLICKEYBYTES, &ping_id, sizeof(ping_id));
944 944
945 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 945 uint8_t shared_key[crypto_box_BEFORENMBYTES];
946 DHT_get_shared_key_sent(dht, shared_key, public_key); 946 DHT_get_shared_key_sent(dht, shared_key, public_key);
@@ -958,9 +958,9 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const
958 return -1; 958 return -1;
959 959
960 data[0] = NET_PACKET_GET_NODES; 960 data[0] = NET_PACKET_GET_NODES;
961 memcpy(data + 1, dht->self_public_key, CLIENT_ID_SIZE); 961 memcpy(data + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES);
962 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); 962 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
963 memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); 963 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, encrypt, len);
964 964
965 return sendpacket(dht->net, ip_port, data, sizeof(data)); 965 return sendpacket(dht->net, ip_port, data, sizeof(data));
966} 966}
@@ -977,7 +977,7 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
977 return -1; 977 return -1;
978 978
979 size_t Node_format_size = sizeof(Node_format); 979 size_t Node_format_size = sizeof(Node_format);
980 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES 980 uint8_t data[1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES
981 + Node_format_size * MAX_SENT_NODES + length + crypto_box_MACBYTES]; 981 + Node_format_size * MAX_SENT_NODES + length + crypto_box_MACBYTES];
982 982
983 Node_format nodes_list[MAX_SENT_NODES]; 983 Node_format nodes_list[MAX_SENT_NODES];
@@ -1008,16 +1008,17 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
1008 return -1; 1008 return -1;
1009 1009
1010 data[0] = NET_PACKET_SEND_NODES_IPV6; 1010 data[0] = NET_PACKET_SEND_NODES_IPV6;
1011 memcpy(data + 1, dht->self_public_key, CLIENT_ID_SIZE); 1011 memcpy(data + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES);
1012 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); 1012 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
1013 memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); 1013 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, encrypt, len);
1014 1014
1015 return sendpacket(dht->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); 1015 return sendpacket(dht->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + len);
1016} 1016}
1017 1017
1018static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length) 1018static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length)
1019{ 1019{
1020 if (length != (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + CLIENT_ID_SIZE + sizeof(uint64_t) + crypto_box_MACBYTES)) 1020 if (length != (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + sizeof(
1021 uint64_t) + crypto_box_MACBYTES))
1021 return 1; 1022 return 1;
1022 1023
1023 DHT *dht = object; 1024 DHT *dht = object;
@@ -1026,20 +1027,20 @@ static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet,
1026 if (id_equal(packet + 1, dht->self_public_key)) 1027 if (id_equal(packet + 1, dht->self_public_key))
1027 return 1; 1028 return 1;
1028 1029
1029 uint8_t plain[CLIENT_ID_SIZE + sizeof(uint64_t)]; 1030 uint8_t plain[crypto_box_PUBLICKEYBYTES + sizeof(uint64_t)];
1030 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 1031 uint8_t shared_key[crypto_box_BEFORENMBYTES];
1031 1032
1032 DHT_get_shared_key_recv(dht, shared_key, packet + 1); 1033 DHT_get_shared_key_recv(dht, shared_key, packet + 1);
1033 int len = decrypt_data_symmetric( shared_key, 1034 int len = decrypt_data_symmetric( shared_key,
1034 packet + 1 + CLIENT_ID_SIZE, 1035 packet + 1 + crypto_box_PUBLICKEYBYTES,
1035 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 1036 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
1036 CLIENT_ID_SIZE + sizeof(uint64_t) + crypto_box_MACBYTES, 1037 crypto_box_PUBLICKEYBYTES + sizeof(uint64_t) + crypto_box_MACBYTES,
1037 plain ); 1038 plain );
1038 1039
1039 if (len != CLIENT_ID_SIZE + sizeof(uint64_t)) 1040 if (len != crypto_box_PUBLICKEYBYTES + sizeof(uint64_t))
1040 return 1; 1041 return 1;
1041 1042
1042 sendnodes_ipv6(dht, source, packet + 1, plain, plain + CLIENT_ID_SIZE, sizeof(uint64_t), shared_key); 1043 sendnodes_ipv6(dht, source, packet + 1, plain, plain + crypto_box_PUBLICKEYBYTES, sizeof(uint64_t), shared_key);
1043 1044
1044 add_to_ping(dht->ping, packet + 1, source); 1045 add_to_ping(dht->ping, packet + 1, source);
1045 1046
@@ -1077,7 +1078,7 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa
1077 Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out) 1078 Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out)
1078{ 1079{
1079 DHT *dht = object; 1080 DHT *dht = object;
1080 uint32_t cid_size = 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + 1 + sizeof(uint64_t) + crypto_box_MACBYTES; 1081 uint32_t cid_size = 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + 1 + sizeof(uint64_t) + crypto_box_MACBYTES;
1081 1082
1082 if (length <= cid_size) /* too short */ 1083 if (length <= cid_size) /* too short */
1083 return 1; 1084 return 1;
@@ -1095,8 +1096,8 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa
1095 DHT_get_shared_key_sent(dht, shared_key, packet + 1); 1096 DHT_get_shared_key_sent(dht, shared_key, packet + 1);
1096 int len = decrypt_data_symmetric( 1097 int len = decrypt_data_symmetric(
1097 shared_key, 1098 shared_key,
1098 packet + 1 + CLIENT_ID_SIZE, 1099 packet + 1 + crypto_box_PUBLICKEYBYTES,
1099 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 1100 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
1100 1 + data_size + sizeof(uint64_t) + crypto_box_MACBYTES, 1101 1 + data_size + sizeof(uint64_t) + crypto_box_MACBYTES,
1101 plain); 1102 plain);
1102 1103
@@ -1197,7 +1198,7 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void
1197 dht->friends_list = temp; 1198 dht->friends_list = temp;
1198 DHT_Friend *friend = &dht->friends_list[dht->num_friends]; 1199 DHT_Friend *friend = &dht->friends_list[dht->num_friends];
1199 memset(friend, 0, sizeof(DHT_Friend)); 1200 memset(friend, 0, sizeof(DHT_Friend));
1200 memcpy(friend->public_key, public_key, CLIENT_ID_SIZE); 1201 memcpy(friend->public_key, public_key, crypto_box_PUBLICKEYBYTES);
1201 1202
1202 friend->nat.NATping_id = random_64b(); 1203 friend->nat.NATping_id = random_64b();
1203 ++dht->num_friends; 1204 ++dht->num_friends;
@@ -1893,10 +1894,10 @@ static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8
1893/* Send a get node hardening request */ 1894/* Send a get node hardening request */
1894static int send_hardening_getnode_req(DHT *dht, Node_format *dest, Node_format *node_totest, uint8_t *search_id) 1895static int send_hardening_getnode_req(DHT *dht, Node_format *dest, Node_format *node_totest, uint8_t *search_id)
1895{ 1896{
1896 uint8_t data[sizeof(Node_format) + CLIENT_ID_SIZE]; 1897 uint8_t data[sizeof(Node_format) + crypto_box_PUBLICKEYBYTES];
1897 memcpy(data, node_totest, sizeof(Node_format)); 1898 memcpy(data, node_totest, sizeof(Node_format));
1898 memcpy(data + sizeof(Node_format), search_id, CLIENT_ID_SIZE); 1899 memcpy(data + sizeof(Node_format), search_id, crypto_box_PUBLICKEYBYTES);
1899 return send_hardening_req(dht, dest, CHECK_TYPE_GETNODE_REQ, data, sizeof(Node_format) + CLIENT_ID_SIZE); 1900 return send_hardening_req(dht, dest, CHECK_TYPE_GETNODE_REQ, data, sizeof(Node_format) + crypto_box_PUBLICKEYBYTES);
1900} 1901}
1901 1902
1902/* Send a get node hardening response */ 1903/* Send a get node hardening response */
@@ -1907,10 +1908,10 @@ static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto,
1907 return -1; 1908 return -1;
1908 1909
1909 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE]; 1910 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE];
1910 uint8_t data[1 + CLIENT_ID_SIZE + nodes_data_length]; 1911 uint8_t data[1 + crypto_box_PUBLICKEYBYTES + nodes_data_length];
1911 data[0] = CHECK_TYPE_GETNODE_RES; 1912 data[0] = CHECK_TYPE_GETNODE_RES;
1912 memcpy(data + 1, queried_client_id, CLIENT_ID_SIZE); 1913 memcpy(data + 1, queried_client_id, crypto_box_PUBLICKEYBYTES);
1913 memcpy(data + 1 + CLIENT_ID_SIZE, nodes_data, nodes_data_length); 1914 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES, nodes_data, nodes_data_length);
1914 int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data, 1915 int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data,
1915 sizeof(data), CRYPTO_PACKET_HARDENING); 1916 sizeof(data), CRYPTO_PACKET_HARDENING);
1916 1917
@@ -1926,7 +1927,7 @@ static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, sa_
1926 uint32_t i; 1927 uint32_t i;
1927 1928
1928 for (i = 0; i < LCLIENT_LIST; ++i) { 1929 for (i = 0; i < LCLIENT_LIST; ++i) {
1929 if (memcmp(dht->close_clientlist[i].public_key, public_key, CLIENT_ID_SIZE) != 0) 1930 if (memcmp(dht->close_clientlist[i].public_key, public_key, crypto_box_PUBLICKEYBYTES) != 0)
1930 continue; 1931 continue;
1931 1932
1932 if (sa_family == AF_INET) 1933 if (sa_family == AF_INET)
@@ -1996,15 +1997,15 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
1996 } 1997 }
1997 1998
1998 case CHECK_TYPE_GETNODE_RES: { 1999 case CHECK_TYPE_GETNODE_RES: {
1999 if (length <= CLIENT_ID_SIZE + 1) 2000 if (length <= crypto_box_PUBLICKEYBYTES + 1)
2000 return 1; 2001 return 1;
2001 2002
2002 if (length > 1 + CLIENT_ID_SIZE + sizeof(Node_format) * MAX_SENT_NODES) 2003 if (length > 1 + crypto_box_PUBLICKEYBYTES + sizeof(Node_format) * MAX_SENT_NODES)
2003 return 1; 2004 return 1;
2004 2005
2005 uint16_t length_nodes = length - 1 - CLIENT_ID_SIZE; 2006 uint16_t length_nodes = length - 1 - crypto_box_PUBLICKEYBYTES;
2006 Node_format nodes[MAX_SENT_NODES]; 2007 Node_format nodes[MAX_SENT_NODES];
2007 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, packet + 1 + CLIENT_ID_SIZE, length_nodes, 0); 2008 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, packet + 1 + crypto_box_PUBLICKEYBYTES, length_nodes, 0);
2008 2009
2009 /* TODO: MAX_SENT_NODES nodes should be returned at all times 2010 /* TODO: MAX_SENT_NODES nodes should be returned at all times
2010 (right now we have a small network size so it could cause problems for testing and etc..) */ 2011 (right now we have a small network size so it could cause problems for testing and etc..) */
@@ -2023,7 +2024,7 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
2023 if (is_timeout(temp->hardening.send_nodes_timestamp, HARDENING_INTERVAL)) 2024 if (is_timeout(temp->hardening.send_nodes_timestamp, HARDENING_INTERVAL))
2024 return 1; 2025 return 1;
2025 2026
2026 if (memcmp(temp->hardening.send_nodes_pingedid, source_pubkey, CLIENT_ID_SIZE) != 0) 2027 if (memcmp(temp->hardening.send_nodes_pingedid, source_pubkey, crypto_box_PUBLICKEYBYTES) != 0)
2027 return 1; 2028 return 1;
2028 2029
2029 /* If Nodes look good and the request checks out */ 2030 /* If Nodes look good and the request checks out */
@@ -2040,10 +2041,10 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
2040 */ 2041 */
2041Node_format random_node(DHT *dht, sa_family_t sa_family) 2042Node_format random_node(DHT *dht, sa_family_t sa_family)
2042{ 2043{
2043 uint8_t id[CLIENT_ID_SIZE]; 2044 uint8_t id[crypto_box_PUBLICKEYBYTES];
2044 uint32_t i; 2045 uint32_t i;
2045 2046
2046 for (i = 0; i < CLIENT_ID_SIZE / 4; ++i) { /* populate the id with pseudorandom bytes.*/ 2047 for (i = 0; i < crypto_box_PUBLICKEYBYTES / 4; ++i) { /* populate the id with pseudorandom bytes.*/
2047 uint32_t t = rand(); 2048 uint32_t t = rand();
2048 memcpy(id + i * sizeof(t), &t, sizeof(t)); 2049 memcpy(id + i * sizeof(t), &t, sizeof(t));
2049 } 2050 }
@@ -2086,7 +2087,7 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num)
2086 } 2087 }
2087 2088
2088 if (assoc != NULL) { 2089 if (assoc != NULL) {
2089 memcpy(nodes[count].public_key, list[i - 1].public_key, CLIENT_ID_SIZE); 2090 memcpy(nodes[count].public_key, list[i - 1].public_key, crypto_box_PUBLICKEYBYTES);
2090 nodes[count].ip_port = assoc->ip_port; 2091 nodes[count].ip_port = assoc->ip_port;
2091 ++count; 2092 ++count;
2092 2093
@@ -2230,7 +2231,7 @@ DHT *new_DHT(Networking_Core *net)
2230 uint32_t i; 2231 uint32_t i;
2231 2232
2232 for (i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) { 2233 for (i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) {
2233 uint8_t random_key_bytes[CLIENT_ID_SIZE]; 2234 uint8_t random_key_bytes[crypto_box_PUBLICKEYBYTES];
2234 randombytes(random_key_bytes, sizeof(random_key_bytes)); 2235 randombytes(random_key_bytes, sizeof(random_key_bytes));
2235 DHT_addfriend(dht, random_key_bytes, 0, 0, 0, 0); 2236 DHT_addfriend(dht, random_key_bytes, 0, 0, 0, 0);
2236 } 2237 }
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index a36ea252..50ab92dc 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -28,9 +28,6 @@
28#include "network.h" 28#include "network.h"
29#include "ping_array.h" 29#include "ping_array.h"
30 30
31/* Size of the client_id in bytes. */
32#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
33
34/* Maximum number of clients stored per friend. */ 31/* Maximum number of clients stored per friend. */
35#define MAX_FRIEND_CLIENTS 8 32#define MAX_FRIEND_CLIENTS 8
36 33
@@ -79,17 +76,17 @@ typedef struct {
79 uint8_t routes_requests_ok; 76 uint8_t routes_requests_ok;
80 /* Time which we last checked this.*/ 77 /* Time which we last checked this.*/
81 uint64_t routes_requests_timestamp; 78 uint64_t routes_requests_timestamp;
82 uint8_t routes_requests_pingedid[CLIENT_ID_SIZE]; 79 uint8_t routes_requests_pingedid[crypto_box_PUBLICKEYBYTES];
83 /* Node sends correct send_node (true (1) or false/didn't check (0)) */ 80 /* Node sends correct send_node (true (1) or false/didn't check (0)) */
84 uint8_t send_nodes_ok; 81 uint8_t send_nodes_ok;
85 /* Time which we last checked this.*/ 82 /* Time which we last checked this.*/
86 uint64_t send_nodes_timestamp; 83 uint64_t send_nodes_timestamp;
87 uint8_t send_nodes_pingedid[CLIENT_ID_SIZE]; 84 uint8_t send_nodes_pingedid[crypto_box_PUBLICKEYBYTES];
88 /* Node can be used to test other nodes (true (1) or false/didn't check (0)) */ 85 /* Node can be used to test other nodes (true (1) or false/didn't check (0)) */
89 uint8_t testing_requests; 86 uint8_t testing_requests;
90 /* Time which we last checked this.*/ 87 /* Time which we last checked this.*/
91 uint64_t testing_timestamp; 88 uint64_t testing_timestamp;
92 uint8_t testing_pingedid[CLIENT_ID_SIZE]; 89 uint8_t testing_pingedid[crypto_box_PUBLICKEYBYTES];
93} Hardening; 90} Hardening;
94 91
95typedef struct { 92typedef struct {
diff --git a/toxcore/assoc.c b/toxcore/assoc.c
index 553f4339..44c4cc30 100644
--- a/toxcore/assoc.c
+++ b/toxcore/assoc.c
@@ -87,7 +87,7 @@ typedef struct candidates_bucket {
87 87
88struct Assoc { 88struct Assoc {
89 hash_t self_hash; /* hash of self_client_id */ 89 hash_t self_hash; /* hash of self_client_id */
90 uint8_t self_client_id[CLIENT_ID_SIZE]; /* don't store entries for this */ 90 uint8_t self_client_id[crypto_box_PUBLICKEYBYTES]; /* don't store entries for this */
91 91
92 /* association centralization: clients not in use */ 92 /* association centralization: clients not in use */
93 size_t candidates_bucket_bits; 93 size_t candidates_bucket_bits;
@@ -101,7 +101,7 @@ struct Assoc {
101/* HELPER FUNCTIONS */ 101/* HELPER FUNCTIONS */
102/*****************************************************************************/ 102/*****************************************************************************/
103 103
104/* the complete distance would be CLIENT_ID_SIZE long... 104/* the complete distance would be crypto_box_PUBLICKEYBYTES long...
105 * returns DISTANCE_INDEX_DISTANCE_BITS valid bits */ 105 * returns DISTANCE_INDEX_DISTANCE_BITS valid bits */
106static uint64_t id_distance(const Assoc *assoc, void *callback_data, const uint8_t *id_ref, const uint8_t *id_test) 106static uint64_t id_distance(const Assoc *assoc, void *callback_data, const uint8_t *id_ref, const uint8_t *id_test)
107{ 107{
@@ -200,7 +200,7 @@ static hash_t id_hash(const Assoc *assoc, const uint8_t *id)
200{ 200{
201 uint32_t i, res = 0x19a64e82; 201 uint32_t i, res = 0x19a64e82;
202 202
203 for (i = 0; i < CLIENT_ID_SIZE; i++) 203 for (i = 0; i < crypto_box_PUBLICKEYBYTES; i++)
204 res = ((res << 1) ^ id[i]) + (res >> 31); 204 res = ((res << 1) ^ id[i]) + (res >> 31);
205 205
206 /* can't have zero as hash, a) marks an unused spot, 206 /* can't have zero as hash, a) marks an unused spot,
@@ -976,11 +976,11 @@ void kill_Assoc(Assoc *assoc)
976 976
977#ifdef LOGGING 977#ifdef LOGGING
978 978
979static char buffer[CLIENT_ID_SIZE * 2 + 1]; 979static char buffer[crypto_box_PUBLICKEYBYTES * 2 + 1];
980static char *idpart2str(uint8_t *id, size_t len) 980static char *idpart2str(uint8_t *id, size_t len)
981{ 981{
982 if (len > CLIENT_ID_SIZE) 982 if (len > crypto_box_PUBLICKEYBYTES)
983 len = CLIENT_ID_SIZE; 983 len = crypto_box_PUBLICKEYBYTES;
984 984
985 size_t i; 985 size_t i;
986 986
diff --git a/toxcore/ping.c b/toxcore/ping.c
index 543ceec5..877aaca2 100644
--- a/toxcore/ping.c
+++ b/toxcore/ping.c
@@ -55,8 +55,8 @@ struct PING {
55 55
56 56
57#define PING_PLAIN_SIZE (1 + sizeof(uint64_t)) 57#define PING_PLAIN_SIZE (1 + sizeof(uint64_t))
58#define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + PING_PLAIN_SIZE + crypto_box_MACBYTES) 58#define DHT_PING_SIZE (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + PING_PLAIN_SIZE + crypto_box_MACBYTES)
59#define PING_DATA_SIZE (CLIENT_ID_SIZE + sizeof(IP_Port)) 59#define PING_DATA_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(IP_Port))
60 60
61int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key) 61int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key)
62{ 62{
@@ -74,7 +74,7 @@ int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key)
74 // Generate random ping_id. 74 // Generate random ping_id.
75 uint8_t data[PING_DATA_SIZE]; 75 uint8_t data[PING_DATA_SIZE];
76 id_copy(data, public_key); 76 id_copy(data, public_key);
77 memcpy(data + CLIENT_ID_SIZE, &ipp, sizeof(IP_Port)); 77 memcpy(data + crypto_box_PUBLICKEYBYTES, &ipp, sizeof(IP_Port));
78 ping_id = ping_array_add(&ping->ping_array, data, sizeof(data)); 78 ping_id = ping_array_add(&ping->ping_array, data, sizeof(data));
79 79
80 if (ping_id == 0) 80 if (ping_id == 0)
@@ -86,13 +86,13 @@ int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key)
86 86
87 pk[0] = NET_PACKET_PING_REQUEST; 87 pk[0] = NET_PACKET_PING_REQUEST;
88 id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey 88 id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey
89 new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce 89 new_nonce(pk + 1 + crypto_box_PUBLICKEYBYTES); // Generate new nonce
90 90
91 91
92 rc = encrypt_data_symmetric(shared_key, 92 rc = encrypt_data_symmetric(shared_key,
93 pk + 1 + CLIENT_ID_SIZE, 93 pk + 1 + crypto_box_PUBLICKEYBYTES,
94 ping_plain, sizeof(ping_plain), 94 ping_plain, sizeof(ping_plain),
95 pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES); 95 pk + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
96 96
97 if (rc != PING_PLAIN_SIZE + crypto_box_MACBYTES) 97 if (rc != PING_PLAIN_SIZE + crypto_box_MACBYTES)
98 return 1; 98 return 1;
@@ -115,13 +115,13 @@ static int send_ping_response(PING *ping, IP_Port ipp, const uint8_t *public_key
115 115
116 pk[0] = NET_PACKET_PING_RESPONSE; 116 pk[0] = NET_PACKET_PING_RESPONSE;
117 id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey 117 id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey
118 new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce 118 new_nonce(pk + 1 + crypto_box_PUBLICKEYBYTES); // Generate new nonce
119 119
120 // Encrypt ping_id using recipient privkey 120 // Encrypt ping_id using recipient privkey
121 rc = encrypt_data_symmetric(shared_encryption_key, 121 rc = encrypt_data_symmetric(shared_encryption_key,
122 pk + 1 + CLIENT_ID_SIZE, 122 pk + 1 + crypto_box_PUBLICKEYBYTES,
123 ping_plain, sizeof(ping_plain), 123 ping_plain, sizeof(ping_plain),
124 pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES ); 124 pk + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES );
125 125
126 if (rc != PING_PLAIN_SIZE + crypto_box_MACBYTES) 126 if (rc != PING_PLAIN_SIZE + crypto_box_MACBYTES)
127 return 1; 127 return 1;
@@ -148,8 +148,8 @@ static int handle_ping_request(void *_dht, IP_Port source, const uint8_t *packet
148 // Decrypt ping_id 148 // Decrypt ping_id
149 DHT_get_shared_key_recv(dht, shared_key, packet + 1); 149 DHT_get_shared_key_recv(dht, shared_key, packet + 1);
150 rc = decrypt_data_symmetric(shared_key, 150 rc = decrypt_data_symmetric(shared_key,
151 packet + 1 + CLIENT_ID_SIZE, 151 packet + 1 + crypto_box_PUBLICKEYBYTES,
152 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 152 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
153 PING_PLAIN_SIZE + crypto_box_MACBYTES, 153 PING_PLAIN_SIZE + crypto_box_MACBYTES,
154 ping_plain ); 154 ping_plain );
155 155
@@ -189,8 +189,8 @@ static int handle_ping_response(void *_dht, IP_Port source, const uint8_t *packe
189 uint8_t ping_plain[PING_PLAIN_SIZE]; 189 uint8_t ping_plain[PING_PLAIN_SIZE];
190 // Decrypt ping_id 190 // Decrypt ping_id
191 rc = decrypt_data_symmetric(shared_key, 191 rc = decrypt_data_symmetric(shared_key,
192 packet + 1 + CLIENT_ID_SIZE, 192 packet + 1 + crypto_box_PUBLICKEYBYTES,
193 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 193 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
194 PING_PLAIN_SIZE + crypto_box_MACBYTES, 194 PING_PLAIN_SIZE + crypto_box_MACBYTES,
195 ping_plain); 195 ping_plain);
196 196
@@ -211,7 +211,7 @@ static int handle_ping_response(void *_dht, IP_Port source, const uint8_t *packe
211 return 1; 211 return 1;
212 212
213 IP_Port ipp; 213 IP_Port ipp;
214 memcpy(&ipp, data + CLIENT_ID_SIZE, sizeof(IP_Port)); 214 memcpy(&ipp, data + crypto_box_PUBLICKEYBYTES, sizeof(IP_Port));
215 215
216 if (!ipport_equal(&ipp, &source)) 216 if (!ipport_equal(&ipp, &source))
217 return 1; 217 return 1;
@@ -269,12 +269,12 @@ int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port)
269 269
270 for (i = 0; i < MAX_TO_PING; ++i) { 270 for (i = 0; i < MAX_TO_PING; ++i) {
271 if (!ip_isset(&ping->to_ping[i].ip_port.ip)) { 271 if (!ip_isset(&ping->to_ping[i].ip_port.ip)) {
272 memcpy(ping->to_ping[i].public_key, public_key, CLIENT_ID_SIZE); 272 memcpy(ping->to_ping[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
273 ipport_copy(&ping->to_ping[i].ip_port, &ip_port); 273 ipport_copy(&ping->to_ping[i].ip_port, &ip_port);
274 return 0; 274 return 0;
275 } 275 }
276 276
277 if (memcmp(ping->to_ping[i].public_key, public_key, CLIENT_ID_SIZE) == 0) { 277 if (memcmp(ping->to_ping[i].public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0) {
278 return -1; 278 return -1;
279 } 279 }
280 } 280 }
@@ -283,7 +283,7 @@ int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port)
283 283
284 for (i = 0; i < MAX_TO_PING; ++i) { 284 for (i = 0; i < MAX_TO_PING; ++i) {
285 if (id_closest(ping->dht->self_public_key, ping->to_ping[(i + r) % MAX_TO_PING].public_key, public_key) == 2) { 285 if (id_closest(ping->dht->self_public_key, ping->to_ping[(i + r) % MAX_TO_PING].public_key, public_key) == 2) {
286 memcpy(ping->to_ping[(i + r) % MAX_TO_PING].public_key, public_key, CLIENT_ID_SIZE); 286 memcpy(ping->to_ping[(i + r) % MAX_TO_PING].public_key, public_key, crypto_box_PUBLICKEYBYTES);
287 ipport_copy(&ping->to_ping[(i + r) % MAX_TO_PING].ip_port, &ip_port); 287 ipport_copy(&ping->to_ping[(i + r) % MAX_TO_PING].ip_port, &ip_port);
288 return 0; 288 return 0;
289 } 289 }
diff --git a/toxcore/util.c b/toxcore/util.c
index 5caf5855..c320cdc4 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -28,8 +28,8 @@
28 28
29#include <time.h> 29#include <time.h>
30 30
31/* for CLIENT_ID_SIZE */ 31/* for crypto_box_PUBLICKEYBYTES */
32#include "DHT.h" 32#include "crypto_core.h"
33 33
34#include "util.h" 34#include "util.h"
35 35
@@ -60,13 +60,13 @@ int is_timeout(uint64_t timestamp, uint64_t timeout)
60/* id functions */ 60/* id functions */
61bool id_equal(const uint8_t *dest, const uint8_t *src) 61bool id_equal(const uint8_t *dest, const uint8_t *src)
62{ 62{
63 return memcmp(dest, src, CLIENT_ID_SIZE) == 0; 63 return memcmp(dest, src, crypto_box_PUBLICKEYBYTES) == 0;
64} 64}
65 65
66uint32_t id_copy(uint8_t *dest, const uint8_t *src) 66uint32_t id_copy(uint8_t *dest, const uint8_t *src)
67{ 67{
68 memcpy(dest, src, CLIENT_ID_SIZE); 68 memcpy(dest, src, crypto_box_PUBLICKEYBYTES);
69 return CLIENT_ID_SIZE; 69 return crypto_box_PUBLICKEYBYTES;
70} 70}
71 71
72void host_to_net(uint8_t *num, uint16_t numbytes) 72void host_to_net(uint8_t *num, uint16_t numbytes)