Added function to increment nonce by specified number.

Nonces now behave like big endian numbers.
This commit is contained in:
irungentoo 2014-04-18 20:36:52 -04:00
parent 47fb2e8067
commit 9f6b17de55
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
2 changed files with 31 additions and 4 deletions

View File

@ -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. */

View File

@ -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);