summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-08-01 23:37:48 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-08-10 14:53:27 +0000
commit463cbcb19a68bc4109285872258d494332bdeaf6 (patch)
treebca1abb46cc91d3dbe462eed773ed54a0ccba164
parentafab28f0ff0bd71efcf39bd633770053da99e04a (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()`.
-rw-r--r--toxcore/DHT.c14
-rw-r--r--toxcore/TCP_connection.c13
-rw-r--r--toxcore/TCP_connection.h2
-rw-r--r--toxcore/group.c4
-rw-r--r--toxcore/mono_time.c23
-rw-r--r--toxcore/mono_time.h3
-rw-r--r--toxcore/network.c1
-rw-r--r--toxcore/onion_client.c26
8 files changed, 30 insertions, 56 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,
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index 2d2dd470..6763328d 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -371,10 +371,10 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, c
371 */ 371 */
372int get_random_tcp_onion_conn_number(TCP_Connections *tcp_c) 372int get_random_tcp_onion_conn_number(TCP_Connections *tcp_c)
373{ 373{
374 unsigned int i, r = rand(); 374 const uint32_t r = random_u32();
375 375
376 for (i = 0; i < tcp_c->tcp_connections_length; ++i) { 376 for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
377 unsigned int index = ((i + r) % tcp_c->tcp_connections_length); 377 uint32_t index = ((i + r) % tcp_c->tcp_connections_length);
378 378
379 if (tcp_c->tcp_connections[index].onion && tcp_c->tcp_connections[index].status == TCP_CONN_CONNECTED) { 379 if (tcp_c->tcp_connections[index].onion && tcp_c->tcp_connections[index].status == TCP_CONN_CONNECTED) {
380 return index; 380 return index;
@@ -1283,11 +1283,12 @@ unsigned int tcp_connection_to_online_tcp_relays(TCP_Connections *tcp_c, int con
1283 * return number of relays copied to tcp_relays on success. 1283 * return number of relays copied to tcp_relays on success.
1284 * return 0 on failure. 1284 * return 0 on failure.
1285 */ 1285 */
1286unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_relays, uint16_t max_num) 1286uint32_t tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_relays, uint16_t max_num)
1287{ 1287{
1288 unsigned int i, copied = 0, r = rand(); 1288 const uint32_t r = random_u32();
1289 uint32_t copied = 0;
1289 1290
1290 for (i = 0; (i < tcp_c->tcp_connections_length) && (copied < max_num); ++i) { 1291 for (uint32_t i = 0; (i < tcp_c->tcp_connections_length) && (copied < max_num); ++i) {
1291 TCP_con *tcp_con = get_tcp_connection(tcp_c, (i + r) % tcp_c->tcp_connections_length); 1292 TCP_con *tcp_con = get_tcp_connection(tcp_c, (i + r) % tcp_c->tcp_connections_length);
1292 1293
1293 if (!tcp_con) { 1294 if (!tcp_con) {
diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h
index 658ee1f4..2d99e122 100644
--- a/toxcore/TCP_connection.h
+++ b/toxcore/TCP_connection.h
@@ -210,7 +210,7 @@ int add_tcp_relay_global(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t
210 * return number of relays copied to tcp_relays on success. 210 * return number of relays copied to tcp_relays on success.
211 * return 0 on failure. 211 * return 0 on failure.
212 */ 212 */
213unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_relays, uint16_t max_num); 213uint32_t tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_relays, uint16_t max_num);
214 214
215/* Returns a new TCP_Connections object associated with the secret_key. 215/* Returns a new TCP_Connections object associated with the secret_key.
216 * 216 *
diff --git a/toxcore/group.c b/toxcore/group.c
index 317b885f..f62684ed 100644
--- a/toxcore/group.c
+++ b/toxcore/group.c
@@ -1490,12 +1490,12 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con
1490 1490
1491 /* TODO(irungentoo): what if two people enter the group at the same time and 1491 /* TODO(irungentoo): what if two people enter the group at the same time and
1492 are given the same peer_number by different nodes? */ 1492 are given the same peer_number by different nodes? */
1493 uint16_t peer_number = rand(); 1493 uint16_t peer_number = random_u16();
1494 1494
1495 unsigned int tries = 0; 1495 unsigned int tries = 0;
1496 1496
1497 while (get_peer_index(g, peer_number) != -1) { 1497 while (get_peer_index(g, peer_number) != -1) {
1498 peer_number = rand(); 1498 peer_number = random_u16();
1499 ++tries; 1499 ++tries;
1500 1500
1501 if (tries > 32) { 1501 if (tries > 32) {
diff --git a/toxcore/mono_time.c b/toxcore/mono_time.c
index 415981d9..0beb7254 100644
--- a/toxcore/mono_time.c
+++ b/toxcore/mono_time.c
@@ -90,29 +90,6 @@ int is_timeout(uint64_t timestamp, uint64_t timeout)
90} 90}
91 91
92 92
93
94/* return current UNIX time in microseconds (us). */
95uint64_t current_time_actual(void)
96{
97 uint64_t time;
98#ifdef OS_WIN32
99 /* This probably works fine */
100 FILETIME ft;
101 GetSystemTimeAsFileTime(&ft);
102 time = ft.dwHighDateTime;
103 time <<= 32;
104 time |= ft.dwLowDateTime;
105 time -= 116444736000000000ULL;
106 return time / 10;
107#else
108 struct timeval a;
109 gettimeofday(&a, nullptr);
110 time = 1000000ULL * a.tv_sec + a.tv_usec;
111 return time;
112#endif
113}
114
115
116//!TOKSTYLE- 93//!TOKSTYLE-
117// No global mutable state in Tokstyle. 94// No global mutable state in Tokstyle.
118#ifdef OS_WIN32 95#ifdef OS_WIN32
diff --git a/toxcore/mono_time.h b/toxcore/mono_time.h
index 63e0f49d..97759560 100644
--- a/toxcore/mono_time.h
+++ b/toxcore/mono_time.h
@@ -22,9 +22,6 @@ void unix_time_update(void);
22uint64_t unix_time(void); 22uint64_t unix_time(void);
23int is_timeout(uint64_t timestamp, uint64_t timeout); 23int is_timeout(uint64_t timestamp, uint64_t timeout);
24 24
25/* return current UNIX time in microseconds (us). */
26uint64_t current_time_actual(void);
27
28/* return current monotonic time in milliseconds (ms). */ 25/* return current monotonic time in milliseconds (ms). */
29uint64_t current_time_monotonic(void); 26uint64_t current_time_monotonic(void);
30 27
diff --git a/toxcore/network.c b/toxcore/network.c
index 1c1459db..68ca43e5 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -698,7 +698,6 @@ int networking_at_startup(void)
698 } 698 }
699 699
700#endif 700#endif
701 srand((uint32_t)current_time_actual());
702 at_startup_ran = 1; 701 at_startup_ran = 1;
703 return 0; 702 return 0;
704} 703}
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)
371static int random_path(const Onion_Client *onion_c, Onion_Client_Paths *onion_paths, uint32_t pathnum, Onion_Path *path) 371static 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 }