diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 17d2e8ff..ae3a69a1 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -163,16 +163,40 @@ void increment_nonce(uint8_t *nonce) { uint32_t i; - for (i = 0; i < crypto_box_NONCEBYTES; ++i) { - ++nonce[i]; + for (i = crypto_box_NONCEBYTES; i != 0; --i) { + ++nonce[i - 1]; - if (nonce[i] != 0) + if (nonce[i - 1] != 0) break; } } +/* increment the given nonce by num */ +void increment_nonce_number(uint8_t *nonce, uint32_t num) +{ + uint32_t num1, num2; + memcpy(&num1, nonce + (crypto_box_NONCEBYTES - sizeof(num1)), sizeof(num1)); + num1 = ntohl(num1); + num2 = num + num1; + + if (num2 < num1) { + uint32_t i; + + for (i = crypto_box_NONCEBYTES - sizeof(num1); i != 0; --i) { + ++nonce[i - 1]; + + if (nonce[i - 1] != 0) + break; + } + } + + num2 = htonl(num2); + memcpy(nonce + (crypto_box_NONCEBYTES - sizeof(num2)), &num2, sizeof(num2)); +} #if crypto_box_NONCEBYTES != crypto_secretbox_NONCEBYTES -/*if they no longer equal each other, this function must be split into two.*/ +/*if they no longer equal each other, this function and the previous ones + *must be split into two. + */ #error random_nonce(): crypto_box_NONCEBYTES must equal crypto_secretbox_NONCEBYTES. #endif /* Fill the given nonce with random bytes. */ diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index da776527..3c70c4d3 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -135,6 +135,9 @@ int decrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *encrypt /* Increment the given nonce by 1. */ void increment_nonce(uint8_t *nonce); +/* increment the given nonce by num */ +void increment_nonce_number(uint8_t *nonce, uint32_t num); + /* Fill the given nonce with random bytes. */ void random_nonce(uint8_t *nonce);