Add random_u16 function and rename the others to match.

This commit is contained in:
iphydf 2018-01-14 21:08:02 +00:00
parent d79b15c52d
commit da739a9438
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
10 changed files with 55 additions and 34 deletions

View File

@ -52,7 +52,7 @@ RTPSession *rtp_new(int payload_type, Messenger *m, uint32_t friendnumber,
return NULL; return NULL;
} }
retu->ssrc = random_int(); retu->ssrc = random_u32();
retu->payload_type = payload_type; retu->payload_type = payload_type;
retu->m = m; retu->m = m;

View File

@ -1454,7 +1454,7 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void
memset(dht_friend, 0, sizeof(DHT_Friend)); memset(dht_friend, 0, sizeof(DHT_Friend));
memcpy(dht_friend->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); memcpy(dht_friend->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
dht_friend->nat.NATping_id = random_64b(); dht_friend->nat.NATping_id = random_u64();
++dht->num_friends; ++dht->num_friends;
lock_num = dht_friend->lock_count; lock_num = dht_friend->lock_count;
@ -1985,7 +1985,7 @@ static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pu
if (packet[0] == NAT_PING_RESPONSE) { if (packet[0] == NAT_PING_RESPONSE) {
if (dht_friend->nat.NATping_id == ping_id) { if (dht_friend->nat.NATping_id == ping_id) {
dht_friend->nat.NATping_id = random_64b(); dht_friend->nat.NATping_id = random_u64();
dht_friend->nat.hole_punching = 1; dht_friend->nat.hole_punching = 1;
return 0; return 0;
} }

View File

@ -2032,7 +2032,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
m->options = *options; m->options = *options;
friendreq_init(m->fr, m->fr_c); friendreq_init(m->fr, m->fr_c);
set_nospam(m->fr, random_int()); set_nospam(m->fr, random_u32());
set_filter_function(m->fr, &friend_already_added, m); set_filter_function(m->fr, &friend_already_added, m);
m->lastdump = 0; m->lastdump = 0;

View File

@ -938,7 +938,7 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn, void *userdata)
int len; int len;
if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) { if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) {
uint64_t ping_id = random_64b(); uint64_t ping_id = random_u64();
if (!ping_id) { if (!ping_id) {
++ping_id; ++ping_id;

View File

@ -1246,7 +1246,7 @@ static void do_TCP_confirmed(TCP_Server *TCP_server)
if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) { if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) {
uint8_t ping[1 + sizeof(uint64_t)]; uint8_t ping[1 + sizeof(uint64_t)];
ping[0] = TCP_PACKET_PING; ping[0] = TCP_PACKET_PING;
uint64_t ping_id = random_64b(); uint64_t ping_id = random_u64();
if (!ping_id) { if (!ping_id) {
++ping_id; ++ping_id;

View File

@ -110,15 +110,34 @@ static int32_t public_key_cmp(
const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] pk1, const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] pk1,
const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] pk2); const uint8_t[CRYPTO_PUBLIC_KEY_SIZE] pk2);
namespace random {
/**
* Return a random 16 bit integer.
*/
static uint16_t u16();
/** /**
* Return a random 32 bit integer. * Return a random 32 bit integer.
*/ */
static uint32_t random_int(); static uint32_t u32();
/** /**
* Return a random 64 bit integer. * Return a random 64 bit integer.
*/ */
static uint64_t random_64b(); static uint64_t u64();
/**
* Fill the given nonce with random bytes.
*/
static void nonce(uint8_t[CRYPTO_NONCE_SIZE] nonce);
/**
* Fill an array of bytes with random values.
*/
static void bytes(uint8_t[length] bytes);
}
/** /**
* Check if a Tox public key CRYPTO_PUBLIC_KEY_SIZE is valid or not. This * Check if a Tox public key CRYPTO_PUBLIC_KEY_SIZE is valid or not. This
@ -226,21 +245,11 @@ static void increment_nonce(uint8_t[CRYPTO_NONCE_SIZE] nonce);
*/ */
static void increment_nonce_number(uint8_t[CRYPTO_NONCE_SIZE] nonce, uint32_t host_order_num); static void increment_nonce_number(uint8_t[CRYPTO_NONCE_SIZE] nonce, uint32_t host_order_num);
/**
* Fill the given nonce with random bytes.
*/
static void random_nonce(uint8_t[CRYPTO_NONCE_SIZE] nonce);
/** /**
* Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes. * Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes.
*/ */
static void new_symmetric_key(uint8_t[CRYPTO_SYMMETRIC_KEY_SIZE] key); static void new_symmetric_key(uint8_t[CRYPTO_SYMMETRIC_KEY_SIZE] key);
/**
* Fill an array of bytes with random values.
*/
static void random_bytes(uint8_t[length] bytes);
%{ %{
#endif /* CRYPTO_CORE_H */ #endif /* CRYPTO_CORE_H */
%} %}

View File

@ -86,14 +86,21 @@ int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2)
return crypto_verify_32(pk1, pk2); return crypto_verify_32(pk1, pk2);
} }
uint32_t random_int(void) uint16_t random_u16(void)
{
uint16_t randnum;
randombytes((uint8_t *)&randnum, sizeof(randnum));
return randnum;
}
uint32_t random_u32(void)
{ {
uint32_t randnum; uint32_t randnum;
randombytes((uint8_t *)&randnum, sizeof(randnum)); randombytes((uint8_t *)&randnum, sizeof(randnum));
return randnum; return randnum;
} }
uint64_t random_64b(void) uint64_t random_u64(void)
{ {
uint64_t randnum; uint64_t randnum;
randombytes((uint8_t *)&randnum, sizeof(randnum)); randombytes((uint8_t *)&randnum, sizeof(randnum));

View File

@ -122,15 +122,30 @@ void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length);
*/ */
int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2); int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2);
/**
* Return a random 16 bit integer.
*/
uint16_t random_u16(void);
/** /**
* Return a random 32 bit integer. * Return a random 32 bit integer.
*/ */
uint32_t random_int(void); uint32_t random_u32(void);
/** /**
* Return a random 64 bit integer. * Return a random 64 bit integer.
*/ */
uint64_t random_64b(void); uint64_t random_u64(void);
/**
* Fill the given nonce with random bytes.
*/
void random_nonce(uint8_t *nonce);
/**
* Fill an array of bytes with random values.
*/
void random_bytes(uint8_t *bytes, size_t length);
/** /**
* Check if a Tox public key CRYPTO_PUBLIC_KEY_SIZE is valid or not. This * Check if a Tox public key CRYPTO_PUBLIC_KEY_SIZE is valid or not. This
@ -216,19 +231,9 @@ void increment_nonce(uint8_t *nonce);
*/ */
void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num); void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num);
/**
* Fill the given nonce with random bytes.
*/
void random_nonce(uint8_t *nonce);
/** /**
* Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes. * Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes.
*/ */
void new_symmetric_key(uint8_t *key); void new_symmetric_key(uint8_t *key);
/**
* Fill an array of bytes with random values.
*/
void random_bytes(uint8_t *bytes, size_t length);
#endif /* CRYPTO_CORE_H */ #endif /* CRYPTO_CORE_H */

View File

@ -1892,7 +1892,7 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const u
conn->rtt_time = DEFAULT_PING_CONNECTION; conn->rtt_time = DEFAULT_PING_CONNECTION;
memcpy(conn->dht_public_key, dht_public_key, CRYPTO_PUBLIC_KEY_SIZE); memcpy(conn->dht_public_key, dht_public_key, CRYPTO_PUBLIC_KEY_SIZE);
conn->cookie_request_number = random_64b(); conn->cookie_request_number = random_u64();
uint8_t cookie_request[COOKIE_REQUEST_LENGTH]; uint8_t cookie_request[COOKIE_REQUEST_LENGTH];
if (create_cookie_request(c, cookie_request, conn->dht_public_key, conn->cookie_request_number, if (create_cookie_request(c, cookie_request, conn->dht_public_key, conn->cookie_request_number,

View File

@ -143,7 +143,7 @@ uint64_t ping_array_add(Ping_Array *array, const uint8_t *data, uint32_t length)
array->entries[index].length = length; array->entries[index].length = length;
array->entries[index].time = unix_time(); array->entries[index].time = unix_time();
++array->last_added; ++array->last_added;
uint64_t ping_id = random_64b(); uint64_t ping_id = random_u64();
ping_id /= array->total_size; ping_id /= array->total_size;
ping_id *= array->total_size; ping_id *= array->total_size;
ping_id += index; ping_id += index;