From eb5ff7270ecda54a72883ff3e61eba8893d01971 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sun, 19 Feb 2017 18:55:32 +0100 Subject: [PATCH] Use the IETF ChaCha20 version for randombytes_buf_deterministic() It doesn't make any difference except by limiting the maximum length to 256 Gb. But the code for the IETF version has a higher probability to already be used by something else than the original version. Enforcing a 256 Gb limit can also prevent surprises from happening in other implementations. --- src/libsodium/randombytes/randombytes.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libsodium/randombytes/randombytes.c b/src/libsodium/randombytes/randombytes.c index 56612c67..65c45c58 100644 --- a/src/libsodium/randombytes/randombytes.c +++ b/src/libsodium/randombytes/randombytes.c @@ -5,6 +5,7 @@ #include #include #include +#include #ifdef __EMSCRIPTEN__ # include @@ -169,10 +170,14 @@ void randombytes_buf_deterministic(void * const buf, const size_t size, const unsigned char seed[randombytes_SEEDBYTES]) { - static const unsigned char zero[crypto_stream_chacha20_NONCEBYTES]; + static const unsigned char zero[crypto_stream_chacha20_ietf_NONCEBYTES]; - COMPILER_ASSERT(randombytes_SEEDBYTES == crypto_stream_chacha20_KEYBYTES); - crypto_stream_chacha20((unsigned char *) buf, size, zero, seed); + COMPILER_ASSERT(randombytes_SEEDBYTES == crypto_stream_chacha20_ietf_KEYBYTES); + if (size > 0x4000000000ULL) { + abort(); + } + crypto_stream_chacha20_ietf((unsigned char *) buf, (unsigned long long) size, + zero, seed); } size_t