diff options
author | Pieter Wuille <pieter@wuille.net> | 2023-07-18 13:52:52 -0400 |
---|---|---|
committer | Pieter Wuille <pieter@wuille.net> | 2023-08-17 15:31:27 -0400 |
commit | 7d1cd932342e74421ae927800eeada14f504b944 (patch) | |
tree | 5dda099aae936cf061c04bfa20731fd2e5ea4d90 /src/random.cpp | |
parent | 44c11769a83b90ca6b8af086d6fa69ff7ac1c3ae (diff) | |
download | bitcoin-7d1cd932342e74421ae927800eeada14f504b944.tar.xz |
crypto: require key on ChaCha20 initialization
Diffstat (limited to 'src/random.cpp')
-rw-r--r-- | src/random.cpp | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/src/random.cpp b/src/random.cpp index 3fdb857b2d..51b8b3ad9d 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -6,6 +6,7 @@ #include <random.h> #include <compat/cpuid.h> +#include <crypto/chacha20.h> #include <crypto/sha256.h> #include <crypto/sha512.h> #include <logging.h> @@ -606,10 +607,7 @@ void FastRandomContext::fillrand(Span<std::byte> output) rng.Keystream(output); } -FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), bitbuf_size(0) -{ - rng.SetKey(MakeByteSpan(seed)); -} +FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), rng(MakeByteSpan(seed)), bitbuf_size(0) {} bool Random_SanityCheck() { @@ -657,13 +655,13 @@ bool Random_SanityCheck() return true; } -FastRandomContext::FastRandomContext(bool fDeterministic) noexcept : requires_seed(!fDeterministic), bitbuf_size(0) +static constexpr std::array<std::byte, ChaCha20::KEYLEN> ZERO_KEY{}; + +FastRandomContext::FastRandomContext(bool fDeterministic) noexcept : requires_seed(!fDeterministic), rng(ZERO_KEY), bitbuf_size(0) { - if (!fDeterministic) { - return; - } - static constexpr std::array<std::byte, ChaCha20::KEYLEN> ZERO{}; - rng.SetKey(ZERO); + // Note that despite always initializing with ZERO_KEY, requires_seed is set to true if not + // fDeterministic. That means the rng will be reinitialized with a secure random key upon first + // use. } FastRandomContext& FastRandomContext::operator=(FastRandomContext&& from) noexcept |