From 339dcd60707ea7b46f5c450569d32f852a1c0be7 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 13 Sep 2013 10:42:14 -0400 Subject: Nonce generation changes. Nonces don't need to be random, only different. also random_int now gives same quality random numbers for both NaCl and libsodium. --- toxcore/net_crypto.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'toxcore/net_crypto.c') diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index a182bb53..8163701e 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -145,14 +145,26 @@ static void increment_nonce(uint8_t *nonce) /* Fill the given nonce with random bytes. */ void random_nonce(uint8_t *nonce) { - uint32_t i, temp; + randombytes(nonce, crypto_box_NONCEBYTES); +} + + +static uint8_t base_nonce[crypto_box_NONCEBYTES]; +static uint8_t nonce_set = 0; - for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) { - temp = random_int(); - memcpy(nonce + 4 * i, &temp, 4); +/*Gives a nonce guaranteed to be different from previous ones.*/ +void new_nonce(uint8_t *nonce) +{ + if (nonce_set == 0) { + random_nonce(base_nonce); + nonce_set = 1; } + + increment_nonce(base_nonce); + memcpy(nonce, base_nonce, crypto_box_NONCEBYTES); } + /* return 0 if there is no received data in the buffer. * return -1 if the packet was discarded. * return length of received data if successful. @@ -237,7 +249,7 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t * uint8_t temp[MAX_DATA_SIZE]; memcpy(temp + 1, data, length); temp[0] = request_id; - random_nonce(nonce); + new_nonce(nonce); int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1, 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); @@ -336,7 +348,7 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; uint8_t nonce[crypto_box_NONCEBYTES]; - random_nonce(nonce); + new_nonce(nonce); memcpy(temp, secret_nonce, crypto_box_NONCEBYTES); memcpy(temp + crypto_box_NONCEBYTES, session_key, crypto_box_PUBLICKEYBYTES); -- cgit v1.2.3