summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c4
-rw-r--r--toxcore/Messenger.c2
-rw-r--r--toxcore/TCP_client.c2
-rw-r--r--toxcore/TCP_server.c2
-rw-r--r--toxcore/crypto_core.api.h33
-rw-r--r--toxcore/crypto_core.c11
-rw-r--r--toxcore/crypto_core.h29
-rw-r--r--toxcore/net_crypto.c2
-rw-r--r--toxcore/ping_array.c2
9 files changed, 54 insertions, 33 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 4e089180..8eaf7a4d 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -1454,7 +1454,7 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void
1454 memset(dht_friend, 0, sizeof(DHT_Friend)); 1454 memset(dht_friend, 0, sizeof(DHT_Friend));
1455 memcpy(dht_friend->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); 1455 memcpy(dht_friend->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
1456 1456
1457 dht_friend->nat.NATping_id = random_64b(); 1457 dht_friend->nat.NATping_id = random_u64();
1458 ++dht->num_friends; 1458 ++dht->num_friends;
1459 1459
1460 lock_num = dht_friend->lock_count; 1460 lock_num = dht_friend->lock_count;
@@ -1985,7 +1985,7 @@ static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pu
1985 1985
1986 if (packet[0] == NAT_PING_RESPONSE) { 1986 if (packet[0] == NAT_PING_RESPONSE) {
1987 if (dht_friend->nat.NATping_id == ping_id) { 1987 if (dht_friend->nat.NATping_id == ping_id) {
1988 dht_friend->nat.NATping_id = random_64b(); 1988 dht_friend->nat.NATping_id = random_u64();
1989 dht_friend->nat.hole_punching = 1; 1989 dht_friend->nat.hole_punching = 1;
1990 return 0; 1990 return 0;
1991 } 1991 }
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 2686b7b8..455494bc 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -2032,7 +2032,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
2032 2032
2033 m->options = *options; 2033 m->options = *options;
2034 friendreq_init(m->fr, m->fr_c); 2034 friendreq_init(m->fr, m->fr_c);
2035 set_nospam(m->fr, random_int()); 2035 set_nospam(m->fr, random_u32());
2036 set_filter_function(m->fr, &friend_already_added, m); 2036 set_filter_function(m->fr, &friend_already_added, m);
2037 2037
2038 m->lastdump = 0; 2038 m->lastdump = 0;
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index 6e6e6b7b..b4092f80 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -938,7 +938,7 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn, void *userdata)
938 int len; 938 int len;
939 939
940 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) { 940 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) {
941 uint64_t ping_id = random_64b(); 941 uint64_t ping_id = random_u64();
942 942
943 if (!ping_id) { 943 if (!ping_id) {
944 ++ping_id; 944 ++ping_id;
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index 9c6506bb..e86776e5 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -1246,7 +1246,7 @@ static void do_TCP_confirmed(TCP_Server *TCP_server)
1246 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) { 1246 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) {
1247 uint8_t ping[1 + sizeof(uint64_t)]; 1247 uint8_t ping[1 + sizeof(uint64_t)];
1248 ping[0] = TCP_PACKET_PING; 1248 ping[0] = TCP_PACKET_PING;
1249 uint64_t ping_id = random_64b(); 1249 uint64_t ping_id = random_u64();
1250 1250
1251 if (!ping_id) { 1251 if (!ping_id) {
1252 ++ping_id; 1252 ++ping_id;
diff --git a/toxcore/crypto_core.api.h b/toxcore/crypto_core.api.h
index cef1a52c..0ba28c0c 100644
--- a/toxcore/crypto_core.api.h
+++ b/toxcore/crypto_core.api.h
@@ -110,15 +110,34 @@ static int32_t public_key_cmp(
110 const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] pk1, 110 const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] pk1,
111 const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] pk2); 111 const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] pk2);
112 112
113namespace random {
114
115/**
116 * Return a random 16 bit integer.
117 */
118static uint16_t u16();
119
113/** 120/**
114 * Return a random 32 bit integer. 121 * Return a random 32 bit integer.
115 */ 122 */
116static uint32_t random_int(); 123static uint32_t u32();
117 124
118/** 125/**
119 * Return a random 64 bit integer. 126 * Return a random 64 bit integer.
120 */ 127 */
121static uint64_t random_64b(); 128static uint64_t u64();
129
130/**
131 * Fill the given nonce with random bytes.
132 */
133static void nonce(uint8_t[CRYPTO_NONCE_SIZE] nonce);
134
135/**
136 * Fill an array of bytes with random values.
137 */
138static void bytes(uint8_t[length] bytes);
139
140}
122 141
123/** 142/**
124 * Check if a Tox public key CRYPTO_PUBLIC_KEY_SIZE is valid or not. This 143 * Check if a Tox public key CRYPTO_PUBLIC_KEY_SIZE is valid or not. This
@@ -227,20 +246,10 @@ static void increment_nonce(uint8_t[CRYPTO_NONCE_SIZE] nonce);
227static void increment_nonce_number(uint8_t[CRYPTO_NONCE_SIZE] nonce, uint32_t host_order_num); 246static void increment_nonce_number(uint8_t[CRYPTO_NONCE_SIZE] nonce, uint32_t host_order_num);
228 247
229/** 248/**
230 * Fill the given nonce with random bytes.
231 */
232static void random_nonce(uint8_t[CRYPTO_NONCE_SIZE] nonce);
233
234/**
235 * Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes. 249 * Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes.
236 */ 250 */
237static void new_symmetric_key(uint8_t[CRYPTO_SYMMETRIC_KEY_SIZE] key); 251static void new_symmetric_key(uint8_t[CRYPTO_SYMMETRIC_KEY_SIZE] key);
238 252
239/**
240 * Fill an array of bytes with random values.
241 */
242static void random_bytes(uint8_t[length] bytes);
243
244%{ 253%{
245#endif /* CRYPTO_CORE_H */ 254#endif /* CRYPTO_CORE_H */
246%} 255%}
diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c
index d10fa88d..d5333e67 100644
--- a/toxcore/crypto_core.c
+++ b/toxcore/crypto_core.c
@@ -86,14 +86,21 @@ int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2)
86 return crypto_verify_32(pk1, pk2); 86 return crypto_verify_32(pk1, pk2);
87} 87}
88 88
89uint32_t random_int(void) 89uint16_t random_u16(void)
90{
91 uint16_t randnum;
92 randombytes((uint8_t *)&randnum, sizeof(randnum));
93 return randnum;
94}
95
96uint32_t random_u32(void)
90{ 97{
91 uint32_t randnum; 98 uint32_t randnum;
92 randombytes((uint8_t *)&randnum, sizeof(randnum)); 99 randombytes((uint8_t *)&randnum, sizeof(randnum));
93 return randnum; 100 return randnum;
94} 101}
95 102
96uint64_t random_64b(void) 103uint64_t random_u64(void)
97{ 104{
98 uint64_t randnum; 105 uint64_t randnum;
99 randombytes((uint8_t *)&randnum, sizeof(randnum)); 106 randombytes((uint8_t *)&randnum, sizeof(randnum));
diff --git a/toxcore/crypto_core.h b/toxcore/crypto_core.h
index fc5756c1..d2742a8b 100644
--- a/toxcore/crypto_core.h
+++ b/toxcore/crypto_core.h
@@ -123,14 +123,29 @@ void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length);
123int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2); 123int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2);
124 124
125/** 125/**
126 * Return a random 16 bit integer.
127 */
128uint16_t random_u16(void);
129
130/**
126 * Return a random 32 bit integer. 131 * Return a random 32 bit integer.
127 */ 132 */
128uint32_t random_int(void); 133uint32_t random_u32(void);
129 134
130/** 135/**
131 * Return a random 64 bit integer. 136 * Return a random 64 bit integer.
132 */ 137 */
133uint64_t random_64b(void); 138uint64_t random_u64(void);
139
140/**
141 * Fill the given nonce with random bytes.
142 */
143void random_nonce(uint8_t *nonce);
144
145/**
146 * Fill an array of bytes with random values.
147 */
148void random_bytes(uint8_t *bytes, size_t length);
134 149
135/** 150/**
136 * Check if a Tox public key CRYPTO_PUBLIC_KEY_SIZE is valid or not. This 151 * Check if a Tox public key CRYPTO_PUBLIC_KEY_SIZE is valid or not. This
@@ -217,18 +232,8 @@ void increment_nonce(uint8_t *nonce);
217void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num); 232void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num);
218 233
219/** 234/**
220 * Fill the given nonce with random bytes.
221 */
222void random_nonce(uint8_t *nonce);
223
224/**
225 * Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes. 235 * Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes.
226 */ 236 */
227void new_symmetric_key(uint8_t *key); 237void new_symmetric_key(uint8_t *key);
228 238
229/**
230 * Fill an array of bytes with random values.
231 */
232void random_bytes(uint8_t *bytes, size_t length);
233
234#endif /* CRYPTO_CORE_H */ 239#endif /* CRYPTO_CORE_H */
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 99e6701b..b3cf2cbf 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -1892,7 +1892,7 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const u
1892 conn->rtt_time = DEFAULT_PING_CONNECTION; 1892 conn->rtt_time = DEFAULT_PING_CONNECTION;
1893 memcpy(conn->dht_public_key, dht_public_key, CRYPTO_PUBLIC_KEY_SIZE); 1893 memcpy(conn->dht_public_key, dht_public_key, CRYPTO_PUBLIC_KEY_SIZE);
1894 1894
1895 conn->cookie_request_number = random_64b(); 1895 conn->cookie_request_number = random_u64();
1896 uint8_t cookie_request[COOKIE_REQUEST_LENGTH]; 1896 uint8_t cookie_request[COOKIE_REQUEST_LENGTH];
1897 1897
1898 if (create_cookie_request(c, cookie_request, conn->dht_public_key, conn->cookie_request_number, 1898 if (create_cookie_request(c, cookie_request, conn->dht_public_key, conn->cookie_request_number,
diff --git a/toxcore/ping_array.c b/toxcore/ping_array.c
index eafedc77..627f8d5a 100644
--- a/toxcore/ping_array.c
+++ b/toxcore/ping_array.c
@@ -143,7 +143,7 @@ uint64_t ping_array_add(Ping_Array *array, const uint8_t *data, uint32_t length)
143 array->entries[index].length = length; 143 array->entries[index].length = length;
144 array->entries[index].time = unix_time(); 144 array->entries[index].time = unix_time();
145 ++array->last_added; 145 ++array->last_added;
146 uint64_t ping_id = random_64b(); 146 uint64_t ping_id = random_u64();
147 ping_id /= array->total_size; 147 ping_id /= array->total_size;
148 ping_id *= array->total_size; 148 ping_id *= array->total_size;
149 ping_id += index; 149 ping_id += index;