From e2fca8cac5ae5e6b570face2ec8ebbd5faf50a7e Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 22 Jun 2015 13:53:35 +0200 Subject: [PATCH] Add sodium_increment() --- ChangeLog | 2 ++ src/libsodium/include/sodium/utils.h | 3 +++ src/libsodium/sodium/utils.c | 13 +++++++++++++ test/default/sodium_utils.c | 23 +++++++++++++++++++++++ test/default/sodium_utils.exp | 5 +++++ 5 files changed, 46 insertions(+) diff --git a/ChangeLog b/ChangeLog index 502e3e1f..6f6a4734 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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 diff --git a/src/libsodium/include/sodium/utils.h b/src/libsodium/include/sodium/utils.h index 42f21bc2..4384262f 100644 --- a/src/libsodium/include/sodium/utils.h +++ b/src/libsodium/include/sodium/utils.h @@ -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); diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index ae0d2be8..b41c858c 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -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; + } +} diff --git a/test/default/sodium_utils.c b/test/default/sodium_utils.c index 89274deb..137aab0b 100644 --- a/test/default/sodium_utils.c +++ b/test/default/sodium_utils.c @@ -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; } diff --git a/test/default/sodium_utils.exp b/test/default/sodium_utils.exp index e087a911..661e6570 100644 --- a/test/default/sodium_utils.exp +++ b/test/default/sodium_utils.exp @@ -11,3 +11,8 @@ dt2: 2 dt3: 11 dt4: 11 dt5: 11 +010000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000 +010100000000000000000000000000000000000000000000 +020000000000000000000000000000000000000000000000 +0001ff000000000000000000000000000000000000000000