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/DHT.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/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 543ee191..37dd9385 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -1740,10 +1740,10 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1740 | } | 1740 | } |
1741 | 1741 | ||
1742 | if ((num_nodes != 0) && (is_timeout(*lastgetnode, GET_NODE_INTERVAL) || *bootstrap_times < MAX_BOOTSTRAP_TIMES)) { | 1742 | if ((num_nodes != 0) && (is_timeout(*lastgetnode, GET_NODE_INTERVAL) || *bootstrap_times < MAX_BOOTSTRAP_TIMES)) { |
1743 | uint32_t rand_node = rand() % num_nodes; | 1743 | uint32_t rand_node = random_u32() % num_nodes; |
1744 | 1744 | ||
1745 | if ((num_nodes - 1) != rand_node) { | 1745 | if ((num_nodes - 1) != rand_node) { |
1746 | rand_node += rand() % (num_nodes - (rand_node + 1)); | 1746 | rand_node += random_u32() % (num_nodes - (rand_node + 1)); |
1747 | } | 1747 | } |
1748 | 1748 | ||
1749 | getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key, public_key, nullptr); | 1749 | getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key, public_key, nullptr); |
@@ -2050,7 +2050,7 @@ static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t * | |||
2050 | return 0; | 2050 | return 0; |
2051 | } | 2051 | } |
2052 | 2052 | ||
2053 | const int retval = sendpacket(dht->net, ip_list[rand() % n], packet, length); | 2053 | const int retval = sendpacket(dht->net, ip_list[random_u32() % n], packet, length); |
2054 | 2054 | ||
2055 | if ((unsigned int)retval == length) { | 2055 | if ((unsigned int)retval == length) { |
2056 | return 1; | 2056 | return 1; |
@@ -2492,7 +2492,7 @@ static Node_format random_node(DHT *dht, Family sa_family) | |||
2492 | uint8_t id[CRYPTO_PUBLIC_KEY_SIZE]; | 2492 | uint8_t id[CRYPTO_PUBLIC_KEY_SIZE]; |
2493 | 2493 | ||
2494 | for (uint32_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE / 4; ++i) { /* populate the id with pseudorandom bytes.*/ | 2494 | for (uint32_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE / 4; ++i) { /* populate the id with pseudorandom bytes.*/ |
2495 | const uint32_t t = rand(); | 2495 | const uint32_t t = random_u32(); |
2496 | memcpy(id + i * sizeof(t), &t, sizeof(t)); | 2496 | memcpy(id + i * sizeof(t), &t, sizeof(t)); |
2497 | } | 2497 | } |
2498 | 2498 | ||
@@ -2504,7 +2504,7 @@ static Node_format random_node(DHT *dht, Family sa_family) | |||
2504 | return nodes_list[0]; | 2504 | return nodes_list[0]; |
2505 | } | 2505 | } |
2506 | 2506 | ||
2507 | return nodes_list[rand() % num_nodes]; | 2507 | return nodes_list[random_u32() % num_nodes]; |
2508 | } | 2508 | } |
2509 | #endif | 2509 | #endif |
2510 | 2510 | ||
@@ -2530,7 +2530,7 @@ static uint16_t list_nodes(Client_data *list, size_t length, Node_format *nodes, | |||
2530 | if (!is_timeout(list[i - 1].assoc6.timestamp, BAD_NODE_TIMEOUT)) { | 2530 | if (!is_timeout(list[i - 1].assoc6.timestamp, BAD_NODE_TIMEOUT)) { |
2531 | if (assoc == nullptr) { | 2531 | if (assoc == nullptr) { |
2532 | assoc = &list[i - 1].assoc6; | 2532 | assoc = &list[i - 1].assoc6; |
2533 | } else if (rand() % 2) { | 2533 | } else if (random_u08() % 2) { |
2534 | assoc = &list[i - 1].assoc6; | 2534 | assoc = &list[i - 1].assoc6; |
2535 | } | 2535 | } |
2536 | } | 2536 | } |
@@ -2560,7 +2560,7 @@ uint16_t randfriends_nodes(DHT *dht, Node_format *nodes, uint16_t max_num) | |||
2560 | } | 2560 | } |
2561 | 2561 | ||
2562 | uint16_t count = 0; | 2562 | uint16_t count = 0; |
2563 | const unsigned int r = rand(); | 2563 | const uint32_t r = random_u32(); |
2564 | 2564 | ||
2565 | for (size_t i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) { | 2565 | for (size_t i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) { |
2566 | count += list_nodes(dht->friends_list[(i + r) % DHT_FAKE_FRIEND_NUMBER].client_list, MAX_FRIEND_CLIENTS, nodes + count, | 2566 | count += list_nodes(dht->friends_list[(i + r) % DHT_FAKE_FRIEND_NUMBER].client_list, MAX_FRIEND_CLIENTS, nodes + count, |