From 9f6b17de55a31cf7aeb8370345849c30683f0c0c Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 18 Apr 2014 20:36:52 -0400 Subject: Added function to increment nonce by specified number. Nonces now behave like big endian numbers. --- toxcore/net_crypto.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'toxcore/net_crypto.c') 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. */ -- cgit v1.2.3