diff --git a/src/bench/xor.cpp b/src/bench/xor.cpp index fc9dc5d1721..f3d6145c2b2 100644 --- a/src/bench/xor.cpp +++ b/src/bench/xor.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -14,7 +15,7 @@ static void Xor(benchmark::Bench& bench) { FastRandomContext frc{/*fDeterministic=*/true}; auto data{frc.randbytes(1024)}; - auto key{frc.randbytes(31)}; + auto key{frc.randbytes(Obfuscation::KEY_SIZE)}; bench.batch(data.size()).unit("byte").run([&] { util::Xor(data, key); diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index 1c35e11863b..19b1adafca5 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -249,7 +250,7 @@ CDBWrapper::CDBWrapper(const DBParams& params) } // The base-case obfuscation key, which is a noop. - obfuscate_key = std::vector(OBFUSCATE_KEY_NUM_BYTES, '\000'); + obfuscate_key = std::vector(Obfuscation::KEY_SIZE, '\000'); bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key); @@ -316,15 +317,13 @@ size_t CDBWrapper::DynamicMemoryUsage() const // past the null-terminator. const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14); -const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8; - /** * Returns a string (consisting of 8 random bytes) suitable for use as an * obfuscating XOR key. */ std::vector CDBWrapper::CreateObfuscateKey() const { - std::vector ret(OBFUSCATE_KEY_NUM_BYTES); + std::vector ret(Obfuscation::KEY_SIZE); GetRandBytes(ret); return ret; } diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 789b5be8fc7..64d428ce5de 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -193,9 +193,6 @@ private: //! the key under which the obfuscation key is stored static const std::string OBFUSCATE_KEY_KEY; - //! the length of the obfuscate key in number of bytes - static const unsigned int OBFUSCATE_KEY_NUM_BYTES; - std::vector CreateObfuscateKey() const; //! path to filesystem storage diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index ba205a9c693..e224c5985d8 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -1123,7 +1124,7 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts) { // Bytes are serialized without length indicator, so this is also the exact // size of the XOR-key file. - std::array xor_key{}; + std::array xor_key{}; // Consider this to be the first run if the blocksdir contains only hidden // files (those which start with a .). Checking for a fully-empty dir would diff --git a/src/node/mempool_persist.cpp b/src/node/mempool_persist.cpp index ff47172c274..eeb690b0877 100644 --- a/src/node/mempool_persist.cpp +++ b/src/node/mempool_persist.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -179,7 +180,7 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock const uint64_t version{pool.m_opts.persist_v1_dat ? MEMPOOL_DUMP_VERSION_NO_XOR_KEY : MEMPOOL_DUMP_VERSION}; file << version; - std::vector xor_key(8); + std::vector xor_key(Obfuscation::KEY_SIZE); if (!pool.m_opts.persist_v1_dat) { FastRandomContext{}.fillrand(xor_key); file << xor_key; diff --git a/src/test/fuzz/autofile.cpp b/src/test/fuzz/autofile.cpp index 8d17624da66..2cebba227f6 100644 --- a/src/test/fuzz/autofile.cpp +++ b/src/test/fuzz/autofile.cpp @@ -4,9 +4,10 @@ #include #include -#include #include +#include #include +#include #include #include @@ -18,9 +19,10 @@ FUZZ_TARGET(autofile) { FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider}; + const auto key_bytes{ConsumeFixedLengthByteVector(fuzzed_data_provider, Obfuscation::KEY_SIZE)}; AutoFile auto_file{ fuzzed_file_provider.open(), - ConsumeRandomLengthByteVector(fuzzed_data_provider), + key_bytes, }; LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100) { diff --git a/src/test/fuzz/buffered_file.cpp b/src/test/fuzz/buffered_file.cpp index a6a042a25cb..c61910e55a7 100644 --- a/src/test/fuzz/buffered_file.cpp +++ b/src/test/fuzz/buffered_file.cpp @@ -4,9 +4,10 @@ #include #include -#include #include +#include #include +#include #include #include @@ -20,9 +21,10 @@ FUZZ_TARGET(buffered_file) FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider}; std::optional opt_buffered_file; + const auto key_bytes{ConsumeFixedLengthByteVector(fuzzed_data_provider, Obfuscation::KEY_SIZE)}; AutoFile fuzzed_file{ fuzzed_file_provider.open(), - ConsumeRandomLengthByteVector(fuzzed_data_provider), + key_bytes, }; try { auto n_buf_size = fuzzed_data_provider.ConsumeIntegralInRange(0, 4096); diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp index 9e8281d26a7..fbba654285a 100644 --- a/src/test/streams_tests.cpp +++ b/src/test/streams_tests.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -563,7 +564,7 @@ BOOST_AUTO_TEST_CASE(buffered_reader_matches_autofile_random_content) const FlatFilePos pos{0, 0}; const FlatFileSeq test_file{m_args.GetDataDirBase(), "buffered_file_test_random", node::BLOCKFILE_CHUNK_SIZE}; - const std::vector obfuscation{m_rng.randbytes(8)}; + const std::vector obfuscation{m_rng.randbytes(Obfuscation::KEY_SIZE)}; // Write out the file with random content { @@ -618,7 +619,7 @@ BOOST_AUTO_TEST_CASE(buffered_writer_matches_autofile_random_content) const FlatFileSeq test_buffered{m_args.GetDataDirBase(), "buffered_write_test", node::BLOCKFILE_CHUNK_SIZE}; const FlatFileSeq test_direct{m_args.GetDataDirBase(), "direct_write_test", node::BLOCKFILE_CHUNK_SIZE}; - const std::vector obfuscation{m_rng.randbytes(8)}; + const std::vector obfuscation{m_rng.randbytes(Obfuscation::KEY_SIZE)}; { DataBuffer test_data{m_rng.randbytes(file_size)}; diff --git a/src/util/obfuscation.h b/src/util/obfuscation.h new file mode 100644 index 00000000000..628dacfc9d5 --- /dev/null +++ b/src/util/obfuscation.h @@ -0,0 +1,16 @@ +// Copyright (c) 2025-present The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_UTIL_OBFUSCATION_H +#define BITCOIN_UTIL_OBFUSCATION_H + +#include + +class Obfuscation +{ +public: + static constexpr size_t KEY_SIZE{sizeof(uint64_t)}; +}; + +#endif // BITCOIN_UTIL_OBFUSCATION_H