diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-08-01 23:37:48 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-08-10 14:53:27 +0000 |
commit | 463cbcb19a68bc4109285872258d494332bdeaf6 (patch) | |
tree | bca1abb46cc91d3dbe462eed773ed54a0ccba164 /toxcore/onion_client.c | |
parent | afab28f0ff0bd71efcf39bd633770053da99e04a (diff) |
Use the crypto random functions instead of `rand()`.
Presumably the uses of `rand()` were fine because they were not used in
security-sensitive places, but having to think about whether a crappy RNG
is acceptable in each situation requires effort that could better be
spent elsewhere.
Also, this means that once we have a custom deterministic RNG for
testing, that RNG is used everywhere, so all the code is deterministic.
It also allowed us to delete a system-specific function that wasn't used
anywhere except in a call to `srand()`.
Diffstat (limited to 'toxcore/onion_client.c')
-rw-r--r-- | toxcore/onion_client.c | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 9c59343d..819bd23f 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c | |||
@@ -267,7 +267,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format | |||
267 | return 0; | 267 | return 0; |
268 | } | 268 | } |
269 | 269 | ||
270 | unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; | 270 | const uint32_t num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; |
271 | 271 | ||
272 | // if (dht_non_lan_connected(onion_c->dht)) { | 272 | // if (dht_non_lan_connected(onion_c->dht)) { |
273 | if (dht_isconnected(onion_c->dht)) { | 273 | if (dht_isconnected(onion_c->dht)) { |
@@ -276,7 +276,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format | |||
276 | } | 276 | } |
277 | 277 | ||
278 | for (i = 0; i < max_num; ++i) { | 278 | for (i = 0; i < max_num; ++i) { |
279 | nodes[i] = onion_c->path_nodes[rand() % num_nodes]; | 279 | nodes[i] = onion_c->path_nodes[random_u32() % num_nodes]; |
280 | } | 280 | } |
281 | } else { | 281 | } else { |
282 | int random_tcp = get_random_tcp_con_number(onion_c->c); | 282 | int random_tcp = get_random_tcp_con_number(onion_c->c); |
@@ -290,7 +290,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format | |||
290 | nodes[0].ip_port.ip.ip.v4.uint32 = random_tcp; | 290 | nodes[0].ip_port.ip.ip.v4.uint32 = random_tcp; |
291 | 291 | ||
292 | for (i = 1; i < max_num; ++i) { | 292 | for (i = 1; i < max_num; ++i) { |
293 | nodes[i] = onion_c->path_nodes[rand() % num_nodes]; | 293 | nodes[i] = onion_c->path_nodes[random_u32() % num_nodes]; |
294 | } | 294 | } |
295 | } else { | 295 | } else { |
296 | unsigned int num_nodes_bs = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs : | 296 | unsigned int num_nodes_bs = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs : |
@@ -304,7 +304,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format | |||
304 | nodes[0].ip_port.ip.ip.v4.uint32 = random_tcp; | 304 | nodes[0].ip_port.ip.ip.v4.uint32 = random_tcp; |
305 | 305 | ||
306 | for (i = 1; i < max_num; ++i) { | 306 | for (i = 1; i < max_num; ++i) { |
307 | nodes[i] = onion_c->path_nodes_bs[rand() % num_nodes_bs]; | 307 | nodes[i] = onion_c->path_nodes_bs[random_u32() % num_nodes_bs]; |
308 | } | 308 | } |
309 | } | 309 | } |
310 | } | 310 | } |
@@ -371,7 +371,7 @@ static bool onion_node_timed_out(const Onion_Node *node) | |||
371 | static int random_path(const Onion_Client *onion_c, Onion_Client_Paths *onion_paths, uint32_t pathnum, Onion_Path *path) | 371 | static int random_path(const Onion_Client *onion_c, Onion_Client_Paths *onion_paths, uint32_t pathnum, Onion_Path *path) |
372 | { | 372 | { |
373 | if (pathnum == UINT32_MAX) { | 373 | if (pathnum == UINT32_MAX) { |
374 | pathnum = rand() % NUMBER_ONION_PATHS; | 374 | pathnum = random_u32() % NUMBER_ONION_PATHS; |
375 | } else { | 375 | } else { |
376 | pathnum = pathnum % NUMBER_ONION_PATHS; | 376 | pathnum = pathnum % NUMBER_ONION_PATHS; |
377 | } | 377 | } |
@@ -394,7 +394,7 @@ static int random_path(const Onion_Client *onion_c, Onion_Client_Paths *onion_pa | |||
394 | onion_paths->last_path_success[pathnum] = onion_paths->path_creation_time[pathnum]; | 394 | onion_paths->last_path_success[pathnum] = onion_paths->path_creation_time[pathnum]; |
395 | onion_paths->last_path_used_times[pathnum] = ONION_PATH_MAX_NO_RESPONSE_USES / 2; | 395 | onion_paths->last_path_used_times[pathnum] = ONION_PATH_MAX_NO_RESPONSE_USES / 2; |
396 | 396 | ||
397 | uint32_t path_num = rand(); | 397 | uint32_t path_num = random_u32(); |
398 | path_num /= NUMBER_ONION_PATHS; | 398 | path_num /= NUMBER_ONION_PATHS; |
399 | path_num *= NUMBER_ONION_PATHS; | 399 | path_num *= NUMBER_ONION_PATHS; |
400 | path_num += pathnum; | 400 | path_num += pathnum; |
@@ -1597,7 +1597,7 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) | |||
1597 | } | 1597 | } |
1598 | 1598 | ||
1599 | if (is_timeout(list_nodes[i].last_pinged, interval) | 1599 | if (is_timeout(list_nodes[i].last_pinged, interval) |
1600 | || (ping_random && rand() % (MAX_ONION_CLIENTS - i) == 0)) { | 1600 | || (ping_random && random_u32() % (MAX_ONION_CLIENTS - i) == 0)) { |
1601 | if (client_send_announce_request(onion_c, friendnum + 1, list_nodes[i].ip_port, | 1601 | if (client_send_announce_request(onion_c, friendnum + 1, list_nodes[i].ip_port, |
1602 | list_nodes[i].public_key, nullptr, ~0) == 0) { | 1602 | list_nodes[i].public_key, nullptr, ~0) == 0) { |
1603 | list_nodes[i].last_pinged = unix_time(); | 1603 | list_nodes[i].last_pinged = unix_time(); |
@@ -1616,12 +1616,12 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) | |||
1616 | n = (MAX_ONION_CLIENTS / 2); | 1616 | n = (MAX_ONION_CLIENTS / 2); |
1617 | } | 1617 | } |
1618 | 1618 | ||
1619 | if (count <= (uint32_t)rand() % MAX_ONION_CLIENTS) { | 1619 | if (count <= random_u32() % MAX_ONION_CLIENTS) { |
1620 | if (num_nodes != 0) { | 1620 | if (num_nodes != 0) { |
1621 | unsigned int j; | 1621 | unsigned int j; |
1622 | 1622 | ||
1623 | for (j = 0; j < n; ++j) { | 1623 | for (j = 0; j < n; ++j) { |
1624 | unsigned int num = rand() % num_nodes; | 1624 | const uint32_t num = random_u32() % num_nodes; |
1625 | client_send_announce_request(onion_c, friendnum + 1, onion_c->path_nodes[num].ip_port, | 1625 | client_send_announce_request(onion_c, friendnum + 1, onion_c->path_nodes[num].ip_port, |
1626 | onion_c->path_nodes[num].public_key, nullptr, ~0); | 1626 | onion_c->path_nodes[num].public_key, nullptr, ~0); |
1627 | } | 1627 | } |
@@ -1708,7 +1708,7 @@ static void do_announce(Onion_Client *onion_c) | |||
1708 | 1708 | ||
1709 | if (is_timeout(list_nodes[i].last_pinged, interval) | 1709 | if (is_timeout(list_nodes[i].last_pinged, interval) |
1710 | || (is_timeout(onion_c->last_announce, ONION_NODE_PING_INTERVAL) | 1710 | || (is_timeout(onion_c->last_announce, ONION_NODE_PING_INTERVAL) |
1711 | && rand() % (MAX_ONION_CLIENTS_ANNOUNCE - i) == 0)) { | 1711 | && random_u32() % (MAX_ONION_CLIENTS_ANNOUNCE - i) == 0)) { |
1712 | uint32_t path_to_use = list_nodes[i].path_used; | 1712 | uint32_t path_to_use = list_nodes[i].path_used; |
1713 | 1713 | ||
1714 | if (list_nodes[i].unsuccessful_pings == ONION_NODE_MAX_PINGS - 1 | 1714 | if (list_nodes[i].unsuccessful_pings == ONION_NODE_MAX_PINGS - 1 |
@@ -1730,7 +1730,7 @@ static void do_announce(Onion_Client *onion_c) | |||
1730 | unsigned int num_nodes; | 1730 | unsigned int num_nodes; |
1731 | Node_format *path_nodes; | 1731 | Node_format *path_nodes; |
1732 | 1732 | ||
1733 | if (rand() % 2 == 0 || onion_c->path_nodes_index == 0) { | 1733 | if (random_u08() % 2 == 0 || onion_c->path_nodes_index == 0) { |
1734 | num_nodes = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs : MAX_PATH_NODES; | 1734 | num_nodes = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs : MAX_PATH_NODES; |
1735 | path_nodes = onion_c->path_nodes_bs; | 1735 | path_nodes = onion_c->path_nodes_bs; |
1736 | } else { | 1736 | } else { |
@@ -1738,10 +1738,10 @@ static void do_announce(Onion_Client *onion_c) | |||
1738 | path_nodes = onion_c->path_nodes; | 1738 | path_nodes = onion_c->path_nodes; |
1739 | } | 1739 | } |
1740 | 1740 | ||
1741 | if (count <= (uint32_t)rand() % MAX_ONION_CLIENTS_ANNOUNCE) { | 1741 | if (count <= random_u32() % MAX_ONION_CLIENTS_ANNOUNCE) { |
1742 | if (num_nodes != 0) { | 1742 | if (num_nodes != 0) { |
1743 | for (i = 0; i < (MAX_ONION_CLIENTS_ANNOUNCE / 2); ++i) { | 1743 | for (i = 0; i < (MAX_ONION_CLIENTS_ANNOUNCE / 2); ++i) { |
1744 | unsigned int num = rand() % num_nodes; | 1744 | const uint32_t num = random_u32() % num_nodes; |
1745 | client_send_announce_request(onion_c, 0, path_nodes[num].ip_port, path_nodes[num].public_key, nullptr, ~0); | 1745 | client_send_announce_request(onion_c, 0, path_nodes[num].ip_port, path_nodes[num].public_key, nullptr, ~0); |
1746 | } | 1746 | } |
1747 | } | 1747 | } |