Add sodium_increment()

This commit is contained in:
Frank Denis 2015-06-22 13:53:35 +02:00
parent 5f74196b0f
commit e2fca8cac5
5 changed files with 46 additions and 0 deletions

View File

@ -8,6 +8,8 @@ crypto_aead_chacha20poly1305_ietf_npubbytes(),
crypto_aead_chacha20poly1305_ietf_encrypt() and
crypto_aead_chacha20poly1305_ietf_decrypt().
- Sodium can now be used in Windows Store apps.
- The sodium_increment() helper function has been added, to increment
an arbitrary long number (such as a nonce).
* Version 1.0.3
- In addition to sodium_bin2hex(), sodium_hex2bin() is now a

View File

@ -95,6 +95,9 @@ int sodium_mprotect_readonly(void *ptr);
SODIUM_EXPORT
int sodium_mprotect_readwrite(void *ptr);
SODIUM_EXPORT
void sodium_increment(unsigned char *n, const size_t nlen);
/* -------- */
int _sodium_alloc_init(void);

View File

@ -510,3 +510,16 @@ sodium_mprotect_readwrite(void *ptr)
{
return _sodium_mprotect(ptr, _mprotect_readwrite);
}
void
sodium_increment(unsigned char *n, const size_t nlen)
{
size_t i;
unsigned int c = 1U;
for (i = (size_t) 0U; i < nlen; i++) {
c += n[i];
n[i] = (unsigned char) c;
c >>= 8;
}
}

View File

@ -8,6 +8,8 @@ int main(void)
unsigned char buf2[1000];
char buf3[33];
unsigned char buf4[4];
unsigned char nonce[24];
char nonce_hex[49];
const char *hex;
const char *hex_end;
size_t bin_len;
@ -60,5 +62,26 @@ int main(void)
}
printf("dt5: %ld\n", (long) (hex_end - hex));
memset(nonce, 0, sizeof nonce);
sodium_increment(nonce, sizeof nonce);
printf("%s\n", sodium_bin2hex(nonce_hex, sizeof nonce_hex,
nonce, sizeof nonce));
memset(nonce, 255, sizeof nonce);
sodium_increment(nonce, sizeof nonce);
printf("%s\n", sodium_bin2hex(nonce_hex, sizeof nonce_hex,
nonce, sizeof nonce));
nonce[1] = 1U;
sodium_increment(nonce, sizeof nonce);
printf("%s\n", sodium_bin2hex(nonce_hex, sizeof nonce_hex,
nonce, sizeof nonce));
nonce[1] = 0U;
sodium_increment(nonce, sizeof nonce);
printf("%s\n", sodium_bin2hex(nonce_hex, sizeof nonce_hex,
nonce, sizeof nonce));
nonce[0] = 255U;
nonce[2] = 255U;
sodium_increment(nonce, sizeof nonce);
printf("%s\n", sodium_bin2hex(nonce_hex, sizeof nonce_hex,
nonce, sizeof nonce));
return 0;
}

View File

@ -11,3 +11,8 @@ dt2: 2
dt3: 11
dt4: 11
dt5: 11
010000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000
010100000000000000000000000000000000000000000000
020000000000000000000000000000000000000000000000
0001ff000000000000000000000000000000000000000000