diff options
author | irungentoo <irungentoo@gmail.com> | 2014-03-01 21:18:53 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-03-01 21:18:53 -0500 |
commit | de69dcef241d54dc75ce01cdeae60f445660608f (patch) | |
tree | 82cbee0d2e7349faf030dafa0793191733e744a2 /toxcore/ping.c | |
parent | 16b93e823bffaf68213d2f66d5af12c5a07ef290 (diff) |
Some small DHT CPU optimizations.
Only compute the shared key once instead of twice for received DHT
requests/responses.
Diffstat (limited to 'toxcore/ping.c')
-rw-r--r-- | toxcore/ping.c | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/toxcore/ping.c b/toxcore/ping.c index 6b1b906a..f521542f 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c | |||
@@ -171,7 +171,8 @@ int send_ping_request(PING *ping, IP_Port ipp, uint8_t *client_id) | |||
171 | return sendpacket(ping->dht->net, ipp, pk, sizeof(pk)); | 171 | return sendpacket(ping->dht->net, ipp, pk, sizeof(pk)); |
172 | } | 172 | } |
173 | 173 | ||
174 | static int send_ping_response(PING *ping, IP_Port ipp, uint8_t *client_id, uint64_t ping_id) | 174 | static int send_ping_response(PING *ping, IP_Port ipp, uint8_t *client_id, uint64_t ping_id, |
175 | uint8_t *shared_encryption_key) | ||
175 | { | 176 | { |
176 | uint8_t pk[DHT_PING_SIZE]; | 177 | uint8_t pk[DHT_PING_SIZE]; |
177 | int rc; | 178 | int rc; |
@@ -184,11 +185,10 @@ static int send_ping_response(PING *ping, IP_Port ipp, uint8_t *client_id, uint6 | |||
184 | new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce | 185 | new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce |
185 | 186 | ||
186 | // Encrypt ping_id using recipient privkey | 187 | // Encrypt ping_id using recipient privkey |
187 | rc = encrypt_data(client_id, | 188 | rc = encrypt_data_fast(shared_encryption_key, |
188 | ping->dht->self_secret_key, | 189 | pk + 1 + CLIENT_ID_SIZE, |
189 | pk + 1 + CLIENT_ID_SIZE, | 190 | (uint8_t *) &ping_id, sizeof(ping_id), |
190 | (uint8_t *) &ping_id, sizeof(ping_id), | 191 | pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES ); |
191 | pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES); | ||
192 | 192 | ||
193 | if (rc != sizeof(ping_id) + crypto_box_MACBYTES) | 193 | if (rc != sizeof(ping_id) + crypto_box_MACBYTES) |
194 | return 1; | 194 | return 1; |
@@ -210,19 +210,21 @@ static int handle_ping_request(void *_dht, IP_Port source, uint8_t *packet, uint | |||
210 | if (id_equal(packet + 1, ping->dht->self_public_key)) | 210 | if (id_equal(packet + 1, ping->dht->self_public_key)) |
211 | return 1; | 211 | return 1; |
212 | 212 | ||
213 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; | ||
214 | |||
213 | // Decrypt ping_id | 215 | // Decrypt ping_id |
214 | rc = decrypt_data(packet + 1, | 216 | encrypt_precompute(packet + 1, ping->dht->self_secret_key, shared_key); |
215 | ping->dht->self_secret_key, | 217 | rc = decrypt_data_fast(shared_key, |
216 | packet + 1 + CLIENT_ID_SIZE, | 218 | packet + 1 + CLIENT_ID_SIZE, |
217 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 219 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
218 | sizeof(ping_id) + crypto_box_MACBYTES, | 220 | sizeof(ping_id) + crypto_box_MACBYTES, |
219 | (uint8_t *) &ping_id); | 221 | (uint8_t *) &ping_id ); |
220 | 222 | ||
221 | if (rc != sizeof(ping_id)) | 223 | if (rc != sizeof(ping_id)) |
222 | return 1; | 224 | return 1; |
223 | 225 | ||
224 | // Send response | 226 | // Send response |
225 | send_ping_response(ping, source, packet + 1, ping_id); | 227 | send_ping_response(ping, source, packet + 1, ping_id, shared_key); |
226 | add_toping(ping, packet + 1, source); | 228 | add_toping(ping, packet + 1, source); |
227 | 229 | ||
228 | return 0; | 230 | return 0; |