summaryrefslogtreecommitdiff
path: root/toxcore/ping.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/ping.c')
-rw-r--r--toxcore/ping.c44
1 files changed, 22 insertions, 22 deletions
diff --git a/toxcore/ping.c b/toxcore/ping.c
index bde28b59..bf61b095 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 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + PING_PLAIN_SIZE + crypto_box_MACBYTES) 58#define DHT_PING_SIZE (1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + PING_PLAIN_SIZE + CRYPTO_MAC_SIZE)
59#define PING_DATA_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(IP_Port)) 59#define PING_DATA_SIZE (CRYPTO_PUBLIC_KEY_SIZE + 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{
@@ -68,14 +68,14 @@ int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key)
68 return 1; 68 return 1;
69 } 69 }
70 70
71 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 71 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
72 72
73 // generate key to encrypt ping_id with recipient privkey 73 // generate key to encrypt ping_id with recipient privkey
74 DHT_get_shared_key_sent(ping->dht, shared_key, public_key); 74 DHT_get_shared_key_sent(ping->dht, shared_key, public_key);
75 // Generate random ping_id. 75 // Generate random ping_id.
76 uint8_t data[PING_DATA_SIZE]; 76 uint8_t data[PING_DATA_SIZE];
77 id_copy(data, public_key); 77 id_copy(data, public_key);
78 memcpy(data + crypto_box_PUBLICKEYBYTES, &ipp, sizeof(IP_Port)); 78 memcpy(data + CRYPTO_PUBLIC_KEY_SIZE, &ipp, sizeof(IP_Port));
79 ping_id = ping_array_add(&ping->ping_array, data, sizeof(data)); 79 ping_id = ping_array_add(&ping->ping_array, data, sizeof(data));
80 80
81 if (ping_id == 0) { 81 if (ping_id == 0) {
@@ -88,15 +88,15 @@ int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key)
88 88
89 pk[0] = NET_PACKET_PING_REQUEST; 89 pk[0] = NET_PACKET_PING_REQUEST;
90 id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey 90 id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey
91 random_nonce(pk + 1 + crypto_box_PUBLICKEYBYTES); // Generate new nonce 91 random_nonce(pk + 1 + CRYPTO_PUBLIC_KEY_SIZE); // Generate new nonce
92 92
93 93
94 rc = encrypt_data_symmetric(shared_key, 94 rc = encrypt_data_symmetric(shared_key,
95 pk + 1 + crypto_box_PUBLICKEYBYTES, 95 pk + 1 + CRYPTO_PUBLIC_KEY_SIZE,
96 ping_plain, sizeof(ping_plain), 96 ping_plain, sizeof(ping_plain),
97 pk + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); 97 pk + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
98 98
99 if (rc != PING_PLAIN_SIZE + crypto_box_MACBYTES) { 99 if (rc != PING_PLAIN_SIZE + CRYPTO_MAC_SIZE) {
100 return 1; 100 return 1;
101 } 101 }
102 102
@@ -119,15 +119,15 @@ static int send_ping_response(PING *ping, IP_Port ipp, const uint8_t *public_key
119 119
120 pk[0] = NET_PACKET_PING_RESPONSE; 120 pk[0] = NET_PACKET_PING_RESPONSE;
121 id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey 121 id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey
122 random_nonce(pk + 1 + crypto_box_PUBLICKEYBYTES); // Generate new nonce 122 random_nonce(pk + 1 + CRYPTO_PUBLIC_KEY_SIZE); // Generate new nonce
123 123
124 // Encrypt ping_id using recipient privkey 124 // Encrypt ping_id using recipient privkey
125 rc = encrypt_data_symmetric(shared_encryption_key, 125 rc = encrypt_data_symmetric(shared_encryption_key,
126 pk + 1 + crypto_box_PUBLICKEYBYTES, 126 pk + 1 + CRYPTO_PUBLIC_KEY_SIZE,
127 ping_plain, sizeof(ping_plain), 127 ping_plain, sizeof(ping_plain),
128 pk + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); 128 pk + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
129 129
130 if (rc != PING_PLAIN_SIZE + crypto_box_MACBYTES) { 130 if (rc != PING_PLAIN_SIZE + CRYPTO_MAC_SIZE) {
131 return 1; 131 return 1;
132 } 132 }
133 133
@@ -149,15 +149,15 @@ static int handle_ping_request(void *object, IP_Port source, const uint8_t *pack
149 return 1; 149 return 1;
150 } 150 }
151 151
152 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 152 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
153 153
154 uint8_t ping_plain[PING_PLAIN_SIZE]; 154 uint8_t ping_plain[PING_PLAIN_SIZE];
155 // Decrypt ping_id 155 // Decrypt ping_id
156 DHT_get_shared_key_recv(dht, shared_key, packet + 1); 156 DHT_get_shared_key_recv(dht, shared_key, packet + 1);
157 rc = decrypt_data_symmetric(shared_key, 157 rc = decrypt_data_symmetric(shared_key,
158 packet + 1 + crypto_box_PUBLICKEYBYTES, 158 packet + 1 + CRYPTO_PUBLIC_KEY_SIZE,
159 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, 159 packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE,
160 PING_PLAIN_SIZE + crypto_box_MACBYTES, 160 PING_PLAIN_SIZE + CRYPTO_MAC_SIZE,
161 ping_plain); 161 ping_plain);
162 162
163 if (rc != sizeof(ping_plain)) { 163 if (rc != sizeof(ping_plain)) {
@@ -192,7 +192,7 @@ static int handle_ping_response(void *object, IP_Port source, const uint8_t *pac
192 return 1; 192 return 1;
193 } 193 }
194 194
195 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 195 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
196 196
197 // generate key to encrypt ping_id with recipient privkey 197 // generate key to encrypt ping_id with recipient privkey
198 DHT_get_shared_key_sent(ping->dht, shared_key, packet + 1); 198 DHT_get_shared_key_sent(ping->dht, shared_key, packet + 1);
@@ -200,9 +200,9 @@ static int handle_ping_response(void *object, IP_Port source, const uint8_t *pac
200 uint8_t ping_plain[PING_PLAIN_SIZE]; 200 uint8_t ping_plain[PING_PLAIN_SIZE];
201 // Decrypt ping_id 201 // Decrypt ping_id
202 rc = decrypt_data_symmetric(shared_key, 202 rc = decrypt_data_symmetric(shared_key,
203 packet + 1 + crypto_box_PUBLICKEYBYTES, 203 packet + 1 + CRYPTO_PUBLIC_KEY_SIZE,
204 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, 204 packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE,
205 PING_PLAIN_SIZE + crypto_box_MACBYTES, 205 PING_PLAIN_SIZE + CRYPTO_MAC_SIZE,
206 ping_plain); 206 ping_plain);
207 207
208 if (rc != sizeof(ping_plain)) { 208 if (rc != sizeof(ping_plain)) {
@@ -226,7 +226,7 @@ static int handle_ping_response(void *object, IP_Port source, const uint8_t *pac
226 } 226 }
227 227
228 IP_Port ipp; 228 IP_Port ipp;
229 memcpy(&ipp, data + crypto_box_PUBLICKEYBYTES, sizeof(IP_Port)); 229 memcpy(&ipp, data + CRYPTO_PUBLIC_KEY_SIZE, sizeof(IP_Port));
230 230
231 if (!ipport_equal(&ipp, &source)) { 231 if (!ipport_equal(&ipp, &source)) {
232 return 1; 232 return 1;
@@ -299,7 +299,7 @@ int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port)
299 299
300 for (i = 0; i < MAX_TO_PING; ++i) { 300 for (i = 0; i < MAX_TO_PING; ++i) {
301 if (!ip_isset(&ping->to_ping[i].ip_port.ip)) { 301 if (!ip_isset(&ping->to_ping[i].ip_port.ip)) {
302 memcpy(ping->to_ping[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); 302 memcpy(ping->to_ping[i].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
303 ipport_copy(&ping->to_ping[i].ip_port, &ip_port); 303 ipport_copy(&ping->to_ping[i].ip_port, &ip_port);
304 return 0; 304 return 0;
305 } 305 }