mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
optimization: Migrate fixed-size obfuscation end-to-end from std::vector<std::byte>
to uint64_t
Since `util::Xor` accepts `uint64_t` values, we're eliminating any repeated vector-to-uint64_t conversions going back to the loading/saving of these values (we're still serializing them as vectors, but converting as soon as possible to `uint64_t`). This is the reason the tests still generate vector values and convert to `uint64_t` later instead of generating it directly. We're also short-circuit `Xor` calls with 0 key values early to avoid unnecessary calculations (e.g. `MakeWritableByteSpan`) - even assuming that XOR is never called for 0. > cmake -B build -DBUILD_BENCH=ON -DCMAKE_BUILD_TYPE=Release \ && cmake --build build -j$(nproc) \ && build/bin/bench_bitcoin -filter='XorObfuscationBench' -min-time=10000 C++ compiler .......................... AppleClang 17.0.0.17000013 | ns/MiB | MiB/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 14,730.40 | 67,886.80 | 0.1% | 11.01 | `XorObfuscationBench` C++ compiler .......................... GNU 13.3.0 | ns/MiB | MiB/s | err% | ins/MiB | cyc/MiB | IPC | bra/MiB | miss% | total | benchmark |--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:---------- | 51,187.17 | 19,536.15 | 0.0% | 327,683.95 | 183,747.58 | 1.783 | 65,536.55 | 0.0% | 11.00 | `XorObfuscationBench` ---- A few other benchmarks that seem to have improved as well (tested with Clang only): Before: | ns/op | op/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 2,202,618.49 | 454.01 | 0.2% | 11.01 | `ReadBlockBench` | 734,444.92 | 1,361.57 | 0.3% | 10.66 | `ReadRawBlockBench` After: | ns/op | op/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 1,912,308.06 | 522.93 | 0.4% | 10.98 | `ReadBlockBench` | 49,092.93 | 20,369.53 | 0.2% | 10.99 | `ReadRawBlockBench`
This commit is contained in:
parent
e50732d25f
commit
c5e866b190
13 changed files with 209 additions and 125 deletions
|
@ -19,13 +19,12 @@ static void XorObfuscationBench(benchmark::Bench& bench)
|
|||
constexpr size_t bytes{10_MiB};
|
||||
auto test_data{rng.randbytes<std::byte>(bytes)};
|
||||
|
||||
std::vector key_bytes{rng.randbytes<std::byte>(8)};
|
||||
uint64_t key;
|
||||
std::memcpy(&key, key_bytes.data(), 8);
|
||||
const Obfuscation obfuscation{rng.rand64()};
|
||||
assert(obfuscation);
|
||||
|
||||
size_t offset{0};
|
||||
bench.batch(bytes / 1_MiB).unit("MiB").run([&] {
|
||||
util::Xor(test_data, key_bytes, offset++);
|
||||
obfuscation(test_data, offset++);
|
||||
ankerl::nanobench::doNotOptimizeAway(test_data);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -173,7 +173,7 @@ void CDBBatch::Clear()
|
|||
void CDBBatch::WriteImpl(std::span<const std::byte> key, DataStream& ssValue)
|
||||
{
|
||||
leveldb::Slice slKey(CharCast(key.data()), key.size());
|
||||
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
|
||||
dbwrapper_private::GetObfuscation(parent)(ssValue);
|
||||
leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
|
||||
m_impl_batch->batch.Put(slKey, slValue);
|
||||
}
|
||||
|
@ -215,6 +215,7 @@ struct LevelDBContext {
|
|||
CDBWrapper::CDBWrapper(const DBParams& params)
|
||||
: m_db_context{std::make_unique<LevelDBContext>()},
|
||||
m_name{fs::PathToString(params.path.stem())},
|
||||
m_obfuscation{0},
|
||||
m_path{params.path},
|
||||
m_is_memory{params.memory_only}
|
||||
{
|
||||
|
@ -252,21 +253,22 @@ CDBWrapper::CDBWrapper(const DBParams& params)
|
|||
}
|
||||
|
||||
{
|
||||
obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES, '\000'); // Needed for unobfuscated Read
|
||||
const bool key_missing{!Read(OBFUSCATE_KEY_KEY, obfuscate_key)};
|
||||
m_obfuscation = 0; // Needed for unobfuscated Read
|
||||
std::vector<unsigned char> obfuscate_key_vector(Obfuscation::SIZE_BYTES, '\000');
|
||||
const bool key_missing{!Read(OBFUSCATE_KEY_KEY, obfuscate_key_vector)};
|
||||
if (key_missing && params.obfuscate && IsEmpty()) {
|
||||
// Initialize non-degenerate obfuscation if it won't upset existing, non-obfuscated data.
|
||||
std::vector<uint8_t> new_key(OBFUSCATE_KEY_NUM_BYTES);
|
||||
std::vector<uint8_t> new_key(Obfuscation::SIZE_BYTES);
|
||||
GetRandBytes(new_key);
|
||||
|
||||
// Write `new_key` so we don't obfuscate the key with itself
|
||||
Write(OBFUSCATE_KEY_KEY, new_key);
|
||||
obfuscate_key = new_key;
|
||||
obfuscate_key_vector = new_key;
|
||||
|
||||
LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key));
|
||||
LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key_vector));
|
||||
}
|
||||
|
||||
LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key));
|
||||
LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key_vector));
|
||||
m_obfuscation = obfuscate_key_vector;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -317,9 +319,6 @@ size_t CDBWrapper::DynamicMemoryUsage() const
|
|||
// We must use a string constructor which specifies length so that we copy
|
||||
// past the null-terminator.
|
||||
const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14);
|
||||
|
||||
const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
|
||||
|
||||
std::optional<std::string> CDBWrapper::ReadImpl(std::span<const std::byte> key) const
|
||||
{
|
||||
leveldb::Slice slKey(CharCast(key.data()), key.size());
|
||||
|
@ -402,6 +401,5 @@ void CDBIterator::SeekToFirst() { m_impl_iter->iter->SeekToFirst(); }
|
|||
void CDBIterator::Next() { m_impl_iter->iter->Next(); }
|
||||
|
||||
namespace dbwrapper_private {
|
||||
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper& w) { return w.obfuscate_key; }
|
||||
|
||||
Obfuscation GetObfuscation(const CDBWrapper& w) { return w.m_obfuscation; }
|
||||
} // namespace dbwrapper_private
|
||||
|
|
|
@ -63,8 +63,7 @@ namespace dbwrapper_private {
|
|||
* Database obfuscation should be considered an implementation detail of the
|
||||
* specific database.
|
||||
*/
|
||||
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w);
|
||||
|
||||
Obfuscation GetObfuscation(const CDBWrapper&);
|
||||
}; // namespace dbwrapper_private
|
||||
|
||||
bool DestroyDB(const std::string& path_str);
|
||||
|
@ -166,7 +165,7 @@ public:
|
|||
template<typename V> bool GetValue(V& value) {
|
||||
try {
|
||||
DataStream ssValue{GetValueImpl()};
|
||||
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
|
||||
dbwrapper_private::GetObfuscation(parent)(ssValue);
|
||||
ssValue >> value;
|
||||
} catch (const std::exception&) {
|
||||
return false;
|
||||
|
@ -179,7 +178,7 @@ struct LevelDBContext;
|
|||
|
||||
class CDBWrapper
|
||||
{
|
||||
friend const std::vector<unsigned char>& dbwrapper_private::GetObfuscateKey(const CDBWrapper &w);
|
||||
friend Obfuscation dbwrapper_private::GetObfuscation(const CDBWrapper&);
|
||||
private:
|
||||
//! holds all leveldb-specific fields of this class
|
||||
std::unique_ptr<LevelDBContext> m_db_context;
|
||||
|
@ -188,14 +187,11 @@ private:
|
|||
std::string m_name;
|
||||
|
||||
//! a key used for optional XOR-obfuscation of the database
|
||||
std::vector<unsigned char> obfuscate_key;
|
||||
Obfuscation m_obfuscation;
|
||||
|
||||
//! 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;
|
||||
|
||||
//! path to filesystem storage
|
||||
const fs::path m_path;
|
||||
|
||||
|
@ -226,7 +222,7 @@ public:
|
|||
}
|
||||
try {
|
||||
DataStream ssValue{MakeByteSpan(*strValue)};
|
||||
ssValue.Xor(obfuscate_key);
|
||||
m_obfuscation(ssValue);
|
||||
ssValue >> value;
|
||||
} catch (const std::exception&) {
|
||||
return false;
|
||||
|
|
|
@ -777,13 +777,13 @@ void BlockManager::UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const
|
|||
|
||||
AutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
|
||||
{
|
||||
return AutoFile{m_block_file_seq.Open(pos, fReadOnly), m_xor_key};
|
||||
return AutoFile{m_block_file_seq.Open(pos, fReadOnly), m_obfuscation};
|
||||
}
|
||||
|
||||
/** Open an undo file (rev?????.dat) */
|
||||
AutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
|
||||
{
|
||||
return AutoFile{m_undo_file_seq.Open(pos, fReadOnly), m_xor_key};
|
||||
return AutoFile{m_undo_file_seq.Open(pos, fReadOnly), m_obfuscation};
|
||||
}
|
||||
|
||||
fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
|
||||
|
@ -1103,7 +1103,7 @@ FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
|
|||
return pos;
|
||||
}
|
||||
|
||||
static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
|
||||
static Obfuscation InitBlocksdirXorKey(const BlockManager::Options& opts)
|
||||
{
|
||||
// Bytes are serialized without length indicator, so this is also the exact
|
||||
// size of the XOR-key file.
|
||||
|
@ -1152,12 +1152,12 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
|
|||
};
|
||||
}
|
||||
LogInfo("Using obfuscation key for blocksdir *.dat files (%s): '%s'\n", fs::PathToString(opts.blocks_dir), HexStr(xor_key));
|
||||
return std::vector<std::byte>{xor_key.begin(), xor_key.end()};
|
||||
return Obfuscation{xor_key};
|
||||
}
|
||||
|
||||
BlockManager::BlockManager(const util::SignalInterrupt& interrupt, Options opts)
|
||||
: m_prune_mode{opts.prune_target > 0},
|
||||
m_xor_key{InitBlocksdirXorKey(opts)},
|
||||
m_obfuscation{InitBlocksdirXorKey(opts)},
|
||||
m_opts{std::move(opts)},
|
||||
m_block_file_seq{FlatFileSeq{m_opts.blocks_dir, "blk", m_opts.fast_prune ? 0x4000 /* 16kB */ : BLOCKFILE_CHUNK_SIZE}},
|
||||
m_undo_file_seq{FlatFileSeq{m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE}},
|
||||
|
|
|
@ -235,7 +235,7 @@ private:
|
|||
|
||||
const bool m_prune_mode;
|
||||
|
||||
const std::vector<std::byte> m_xor_key;
|
||||
const Obfuscation m_obfuscation;
|
||||
|
||||
/** Dirty block index entries. */
|
||||
std::set<CBlockIndex*> m_dirty_blockindex;
|
||||
|
|
|
@ -60,12 +60,11 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
|
|||
file >> version;
|
||||
|
||||
if (version == MEMPOOL_DUMP_VERSION_NO_XOR_KEY) {
|
||||
std::vector<std::byte> xor_key;
|
||||
file.SetXor(xor_key);
|
||||
file.SetObfuscation(0);
|
||||
} else if (version == MEMPOOL_DUMP_VERSION) {
|
||||
std::vector<std::byte> xor_key;
|
||||
file >> xor_key;
|
||||
file.SetXor(xor_key);
|
||||
Obfuscation obfuscation{0};
|
||||
file >> obfuscation;
|
||||
file.SetObfuscation(obfuscation);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
@ -181,13 +180,11 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
|
|||
file << version;
|
||||
|
||||
if (!pool.m_opts.persist_v1_dat) {
|
||||
std::vector<std::byte> xor_key(8);
|
||||
FastRandomContext{}.fillrand(xor_key);
|
||||
file << xor_key;
|
||||
file.SetXor(xor_key);
|
||||
const Obfuscation obfuscation{FastRandomContext{}.rand64()};
|
||||
file << obfuscation;
|
||||
file.SetObfuscation(obfuscation);
|
||||
} else {
|
||||
std::vector<std::byte> xor_key(8);
|
||||
file.SetXor(xor_key);
|
||||
file.SetObfuscation(0);
|
||||
}
|
||||
|
||||
uint64_t mempool_transactions_to_write(vinfo.size());
|
||||
|
|
85
src/obfuscation.h
Normal file
85
src/obfuscation.h
Normal file
|
@ -0,0 +1,85 @@
|
|||
// Copyright (c) 2009-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_OBFUSCATION_H
|
||||
#define BITCOIN_OBFUSCATION_H
|
||||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <random>
|
||||
#include <span.h>
|
||||
#include <util/check.h>
|
||||
#include <cstring>
|
||||
#include <climits>
|
||||
#include <serialize.h>
|
||||
|
||||
class Obfuscation
|
||||
{
|
||||
public:
|
||||
static constexpr size_t SIZE_BYTES{sizeof(uint64_t)};
|
||||
|
||||
private:
|
||||
std::array<uint64_t, SIZE_BYTES> rotations; // Cached key rotations
|
||||
void SetRotations(const uint64_t key)
|
||||
{
|
||||
for (size_t i{0}; i < SIZE_BYTES; ++i) {
|
||||
size_t key_rotation_bits{CHAR_BIT * i};
|
||||
if constexpr (std::endian::native == std::endian::big) key_rotation_bits *= -1;
|
||||
rotations[i] = std::rotr(key, key_rotation_bits);
|
||||
}
|
||||
}
|
||||
|
||||
static uint64_t ToUint64(const std::span<const std::byte, SIZE_BYTES> key_span)
|
||||
{
|
||||
uint64_t key{};
|
||||
std::memcpy(&key, key_span.data(), SIZE_BYTES);
|
||||
return key;
|
||||
}
|
||||
|
||||
static void Xor(std::span<std::byte> write, const uint64_t key, const size_t size)
|
||||
{
|
||||
assert(size <= write.size());
|
||||
uint64_t raw{};
|
||||
std::memcpy(&raw, write.data(), size);
|
||||
raw ^= key;
|
||||
std::memcpy(write.data(), &raw, size);
|
||||
}
|
||||
|
||||
public:
|
||||
Obfuscation(const uint64_t key) { SetRotations(key); }
|
||||
Obfuscation(const std::span<const std::byte, SIZE_BYTES> key_span) : Obfuscation(ToUint64(key_span)) {}
|
||||
Obfuscation(const std::vector<uint8_t>& key_vec) : Obfuscation(MakeByteSpan(key_vec).first<SIZE_BYTES>()) {}
|
||||
Obfuscation(const std::vector<std::byte>& key_vec) : Obfuscation(std::span(key_vec).first<SIZE_BYTES>()) {}
|
||||
|
||||
uint64_t Key() const { return rotations[0]; }
|
||||
operator bool() const { return Key() != 0; }
|
||||
void operator()(std::span<std::byte> write, const size_t key_offset_bytes = 0) const
|
||||
{
|
||||
if (!*this) return;
|
||||
const uint64_t rot_key{rotations[key_offset_bytes % SIZE_BYTES]}; // Continue obfuscation from where we left off
|
||||
for (; write.size() >= SIZE_BYTES; write = write.subspan(SIZE_BYTES)) { // Process multiple bytes at a time
|
||||
Xor(write, rot_key, SIZE_BYTES);
|
||||
}
|
||||
Xor(write, rot_key, write.size());
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
void Serialize(Stream& s) const
|
||||
{
|
||||
std::vector<std::byte> bytes(SIZE_BYTES);
|
||||
std::memcpy(bytes.data(), &rotations[0], SIZE_BYTES);
|
||||
s << bytes;
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
void Unserialize(Stream& s)
|
||||
{
|
||||
std::vector<std::byte> bytes(SIZE_BYTES);
|
||||
s >> bytes;
|
||||
SetRotations(ToUint64(MakeByteSpan(bytes).first<SIZE_BYTES>()));
|
||||
}
|
||||
};
|
||||
|
||||
#endif // BITCOIN_OBFUSCATION_H
|
|
@ -9,8 +9,7 @@
|
|||
|
||||
#include <array>
|
||||
|
||||
AutoFile::AutoFile(std::FILE* file, std::vector<std::byte> data_xor)
|
||||
: m_file{file}, m_xor{std::move(data_xor)}
|
||||
AutoFile::AutoFile(std::FILE* file, const Obfuscation& obfuscation) : m_file{file}, m_obfuscation{obfuscation}
|
||||
{
|
||||
if (!IsNull()) {
|
||||
auto pos{std::ftell(m_file)};
|
||||
|
@ -21,12 +20,12 @@ AutoFile::AutoFile(std::FILE* file, std::vector<std::byte> data_xor)
|
|||
std::size_t AutoFile::detail_fread(std::span<std::byte> dst)
|
||||
{
|
||||
if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
|
||||
size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);
|
||||
if (!m_xor.empty()) {
|
||||
if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::read: position unknown");
|
||||
util::Xor(dst.subspan(0, ret), m_xor, *m_position);
|
||||
const size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);
|
||||
if (m_obfuscation) {
|
||||
if (!m_position) throw std::ios_base::failure("AutoFile::read: position unknown");
|
||||
m_obfuscation(dst, *m_position);
|
||||
}
|
||||
if (m_position.has_value()) *m_position += ret;
|
||||
if (m_position) *m_position += ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -81,7 +80,7 @@ void AutoFile::ignore(size_t nSize)
|
|||
void AutoFile::write(std::span<const std::byte> src)
|
||||
{
|
||||
if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
|
||||
if (m_xor.empty()) {
|
||||
if (!m_obfuscation) {
|
||||
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
|
||||
throw std::ios_base::failure("AutoFile::write: write failed");
|
||||
}
|
||||
|
@ -100,9 +99,9 @@ void AutoFile::write(std::span<const std::byte> src)
|
|||
void AutoFile::write_buffer(std::span<std::byte> src)
|
||||
{
|
||||
if (!m_file) throw std::ios_base::failure("AutoFile::write_buffer: file handle is nullptr");
|
||||
if (m_xor.size()) {
|
||||
if (m_obfuscation) {
|
||||
if (!m_position) throw std::ios_base::failure("AutoFile::write_buffer: obfuscation position unknown");
|
||||
util::Xor(src, m_xor, *m_position); // obfuscate in-place
|
||||
m_obfuscation(src, *m_position); // obfuscate in-place
|
||||
}
|
||||
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
|
||||
throw std::ios_base::failure("AutoFile::write_buffer: write failed");
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#ifndef BITCOIN_STREAMS_H
|
||||
#define BITCOIN_STREAMS_H
|
||||
|
||||
#include <obfuscation.h>
|
||||
#include <serialize.h>
|
||||
#include <span.h>
|
||||
#include <support/allocators/zeroafterfree.h>
|
||||
|
@ -21,30 +22,8 @@
|
|||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace util {
|
||||
inline void Xor(std::span<std::byte> write, std::span<const std::byte> key, size_t key_offset = 0)
|
||||
{
|
||||
if (key.size() == 0) {
|
||||
return;
|
||||
}
|
||||
key_offset %= key.size();
|
||||
|
||||
for (size_t i = 0, j = key_offset; i != write.size(); i++) {
|
||||
write[i] ^= key[j++];
|
||||
|
||||
// This potentially acts on very many bytes of data, so it's
|
||||
// important that we calculate `j`, i.e. the `key` index in this
|
||||
// way instead of doing a %, which would effectively be a division
|
||||
// for each byte Xor'd -- much slower than need be.
|
||||
if (j == key.size())
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
} // namespace util
|
||||
|
||||
/* Minimal stream for overwriting and/or appending to an existing byte vector
|
||||
*
|
||||
* The referenced vector will grow as necessary
|
||||
|
@ -261,21 +240,16 @@ public:
|
|||
return (*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
DataStream& operator>>(T&& obj)
|
||||
{
|
||||
::Unserialize(*this, obj);
|
||||
return (*this);
|
||||
}
|
||||
|
||||
/**
|
||||
* XOR the contents of this stream with a certain key.
|
||||
*
|
||||
* @param[in] key The key used to XOR the data in this stream.
|
||||
*/
|
||||
void Xor(const std::vector<unsigned char>& key)
|
||||
void Obfuscate(const Obfuscation& obfuscation)
|
||||
{
|
||||
util::Xor(MakeWritableByteSpan(*this), MakeByteSpan(key));
|
||||
if (obfuscation) obfuscation(MakeWritableByteSpan(*this));
|
||||
}
|
||||
|
||||
/** Compute total memory usage of this object (own memory + any dynamic memory). */
|
||||
|
@ -392,11 +366,11 @@ class AutoFile
|
|||
{
|
||||
protected:
|
||||
std::FILE* m_file;
|
||||
std::vector<std::byte> m_xor;
|
||||
Obfuscation m_obfuscation;
|
||||
std::optional<int64_t> m_position;
|
||||
|
||||
public:
|
||||
explicit AutoFile(std::FILE* file, std::vector<std::byte> data_xor={});
|
||||
explicit AutoFile(std::FILE* file, const Obfuscation& obfuscation = 0);
|
||||
|
||||
~AutoFile() { fclose(); }
|
||||
|
||||
|
@ -428,7 +402,7 @@ public:
|
|||
bool IsNull() const { return m_file == nullptr; }
|
||||
|
||||
/** Continue with a different XOR key */
|
||||
void SetXor(std::vector<std::byte> data_xor) { m_xor = data_xor; }
|
||||
void SetObfuscation(const Obfuscation& obfuscation) { m_obfuscation = obfuscation; }
|
||||
|
||||
/** Implementation detail, only used internally. */
|
||||
std::size_t detail_fread(std::span<std::byte> dst);
|
||||
|
|
|
@ -14,16 +14,6 @@
|
|||
|
||||
using util::ToString;
|
||||
|
||||
// Test if a string consists entirely of null characters
|
||||
static bool is_null_key(const std::vector<unsigned char>& key) {
|
||||
bool isnull = true;
|
||||
|
||||
for (unsigned int i = 0; i < key.size(); i++)
|
||||
isnull &= (key[i] == '\x00');
|
||||
|
||||
return isnull;
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(dbwrapper)
|
||||
|
@ -37,7 +27,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
|
|||
uint256 res;
|
||||
|
||||
// Ensure that we're doing real obfuscation when obfuscate=true
|
||||
BOOST_CHECK(obfuscate != is_null_key(dbwrapper_private::GetObfuscateKey(dbw)));
|
||||
BOOST_CHECK(obfuscate == dbwrapper_private::GetObfuscation(dbw));
|
||||
|
||||
BOOST_CHECK(dbw.Write(key, in));
|
||||
BOOST_CHECK(dbw.Read(key, res));
|
||||
|
@ -57,7 +47,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
|
|||
bool res_bool;
|
||||
|
||||
// Ensure that we're doing real obfuscation when obfuscate=true
|
||||
BOOST_CHECK(obfuscate != is_null_key(dbwrapper_private::GetObfuscateKey(dbw)));
|
||||
BOOST_CHECK(obfuscate == dbwrapper_private::GetObfuscation(dbw));
|
||||
|
||||
//Simulate block raw data - "b + block hash"
|
||||
std::string key_block = "b" + m_rng.rand256().ToString();
|
||||
|
@ -232,7 +222,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
|
|||
BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());
|
||||
|
||||
BOOST_CHECK(!odbw.IsEmpty()); // There should be existing data
|
||||
BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); // The key should be an empty string
|
||||
BOOST_CHECK(!dbwrapper_private::GetObfuscation(odbw));
|
||||
|
||||
uint256 in2 = m_rng.rand256();
|
||||
uint256 res3;
|
||||
|
@ -269,7 +259,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
|
|||
// Check that the key/val we wrote with unobfuscated wrapper doesn't exist
|
||||
uint256 res2;
|
||||
BOOST_CHECK(!odbw.Read(key, res2));
|
||||
BOOST_CHECK(!is_null_key(dbwrapper_private::GetObfuscateKey(odbw)));
|
||||
BOOST_CHECK(dbwrapper_private::GetObfuscation(odbw));
|
||||
|
||||
uint256 in2 = m_rng.rand256();
|
||||
uint256 res3;
|
||||
|
|
|
@ -20,7 +20,7 @@ FUZZ_TARGET(autofile)
|
|||
FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
|
||||
AutoFile auto_file{
|
||||
fuzzed_file_provider.open(),
|
||||
ConsumeRandomLengthByteVector<std::byte>(fuzzed_data_provider),
|
||||
fuzzed_data_provider.ConsumeIntegral<uint64_t>()
|
||||
};
|
||||
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
|
||||
{
|
||||
|
|
|
@ -22,7 +22,7 @@ FUZZ_TARGET(buffered_file)
|
|||
std::optional<BufferedFile> opt_buffered_file;
|
||||
AutoFile fuzzed_file{
|
||||
fuzzed_file_provider.open(),
|
||||
ConsumeRandomLengthByteVector<std::byte>(fuzzed_data_provider),
|
||||
fuzzed_data_provider.ConsumeIntegral<uint64_t>()
|
||||
};
|
||||
try {
|
||||
auto n_buf_size = fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096);
|
||||
|
|
|
@ -20,10 +20,10 @@ BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup)
|
|||
// Test that obfuscation can be properly reversed even with random chunk sizes.
|
||||
BOOST_AUTO_TEST_CASE(xor_roundtrip_random_chunks)
|
||||
{
|
||||
auto apply_random_xor_chunks{[&](std::span<std::byte> write, const std::span<std::byte> key) {
|
||||
auto apply_random_xor_chunks{[&](std::span<std::byte> write, const Obfuscation& obfuscation) {
|
||||
for (size_t offset{0}; offset < write.size();) {
|
||||
const size_t chunk_size{1 + m_rng.randrange(write.size() - offset)};
|
||||
util::Xor(write.subspan(offset, chunk_size), key, offset);
|
||||
obfuscation(write.subspan(offset, chunk_size), offset);
|
||||
offset += chunk_size;
|
||||
}
|
||||
}};
|
||||
|
@ -33,16 +33,15 @@ BOOST_AUTO_TEST_CASE(xor_roundtrip_random_chunks)
|
|||
const std::vector original{m_rng.randbytes<std::byte>(write_size)};
|
||||
std::vector roundtrip{original};
|
||||
|
||||
std::vector key_bytes{m_rng.randbytes<std::byte>(sizeof(uint64_t))};
|
||||
uint64_t xor_key;
|
||||
std::memcpy(&xor_key, key_bytes.data(), sizeof(xor_key));
|
||||
apply_random_xor_chunks(roundtrip, key_bytes);
|
||||
const auto key_bytes{m_rng.randbytes<std::byte>(Obfuscation::SIZE_BYTES)};
|
||||
const Obfuscation obfuscation{key_bytes};
|
||||
apply_random_xor_chunks(roundtrip, obfuscation);
|
||||
|
||||
// Verify intermediate state is different from original (unless key is zero)
|
||||
const bool all_zero = (xor_key == 0) || (HexStr(key_bytes).find_first_not_of('0') >= write_size * 2);
|
||||
const bool all_zero = !obfuscation || (HexStr(key_bytes).find_first_not_of('0') >= write_size * 2);
|
||||
BOOST_CHECK_EQUAL(original != roundtrip, !all_zero);
|
||||
|
||||
apply_random_xor_chunks(roundtrip, key_bytes);
|
||||
apply_random_xor_chunks(roundtrip, obfuscation);
|
||||
BOOST_CHECK(original == roundtrip);
|
||||
}
|
||||
}
|
||||
|
@ -61,20 +60,69 @@ BOOST_AUTO_TEST_CASE(xor_bytes_reference)
|
|||
const size_t write_size{1 + m_rng.randrange(100U)};
|
||||
const size_t key_offset{m_rng.randrange(3 * 8U)}; // Should wrap around
|
||||
|
||||
std::vector key_bytes{m_rng.randbytes<std::byte>(sizeof(uint64_t))};
|
||||
uint64_t xor_key;
|
||||
std::memcpy(&xor_key, key_bytes.data(), sizeof(xor_key));
|
||||
|
||||
const auto key_bytes{m_rng.randbytes<std::byte>(Obfuscation::SIZE_BYTES)};
|
||||
const Obfuscation obfuscation{key_bytes};
|
||||
std::vector expected{m_rng.randbytes<std::byte>(write_size)};
|
||||
std::vector actual{expected};
|
||||
|
||||
expected_xor(expected, key_bytes, key_offset);
|
||||
util::Xor(actual, key_bytes, key_offset);
|
||||
obfuscation(actual, key_offset);
|
||||
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(obfuscation_constructors)
|
||||
{
|
||||
constexpr uint64_t test_key = 0x0123456789ABCDEF;
|
||||
|
||||
// Direct uint64_t constructor
|
||||
const Obfuscation obf1{test_key};
|
||||
BOOST_CHECK_EQUAL(obf1.Key(), test_key);
|
||||
|
||||
// std::span constructor
|
||||
std::array<std::byte, Obfuscation::SIZE_BYTES> key_bytes{};
|
||||
std::memcpy(key_bytes.data(), &test_key, Obfuscation::SIZE_BYTES);
|
||||
const Obfuscation obf2{std::span{key_bytes}};
|
||||
BOOST_CHECK_EQUAL(obf2.Key(), test_key);
|
||||
|
||||
// std::vector<uint8_t> constructor
|
||||
std::vector<uint8_t> uint8_key(Obfuscation::SIZE_BYTES);
|
||||
std::memcpy(uint8_key.data(), &test_key, uint8_key.size());
|
||||
const Obfuscation obf4{uint8_key};
|
||||
BOOST_CHECK_EQUAL(obf4.Key(), test_key);
|
||||
|
||||
// std::vector<std::byte> constructor
|
||||
std::vector<std::byte> byte_vector_key(Obfuscation::SIZE_BYTES);
|
||||
std::memcpy(byte_vector_key.data(), &test_key, byte_vector_key.size());
|
||||
const Obfuscation obf5{byte_vector_key};
|
||||
BOOST_CHECK_EQUAL(obf5.Key(), test_key);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(obfuscation_serialize)
|
||||
{
|
||||
const Obfuscation original{0xDEADBEEF};
|
||||
|
||||
// Serialize
|
||||
DataStream ds;
|
||||
ds << original;
|
||||
|
||||
BOOST_CHECK_EQUAL(ds.size(), 1 + Obfuscation::SIZE_BYTES); // serialized as a vector
|
||||
|
||||
// Deserialize
|
||||
Obfuscation recovered{0};
|
||||
ds >> recovered;
|
||||
|
||||
BOOST_CHECK_EQUAL(recovered.Key(), original.Key());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(obfuscation_empty)
|
||||
{
|
||||
const Obfuscation null_obf{0};
|
||||
BOOST_CHECK(!null_obf);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(xor_file)
|
||||
{
|
||||
fs::path xor_path{m_args.GetDataDirBase() / "test_xor.bin"};
|
||||
|
@ -99,7 +147,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
|
|||
#else
|
||||
const char* mode = "wbx";
|
||||
#endif
|
||||
AutoFile xor_file{raw_file(mode), key_bytes};
|
||||
AutoFile xor_file{raw_file(mode), xor_key};
|
||||
xor_file << test1 << test2;
|
||||
}
|
||||
{
|
||||
|
@ -112,7 +160,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
|
|||
BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
|
||||
}
|
||||
{
|
||||
AutoFile xor_file{raw_file("rb"), key_bytes};
|
||||
AutoFile xor_file{raw_file("rb"), xor_key};
|
||||
std::vector<std::byte> read1, read2;
|
||||
xor_file >> read1 >> read2;
|
||||
BOOST_CHECK_EQUAL(HexStr(read1), HexStr(test1));
|
||||
|
@ -121,7 +169,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
|
|||
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
|
||||
}
|
||||
{
|
||||
AutoFile xor_file{raw_file("rb"), key_bytes};
|
||||
AutoFile xor_file{raw_file("rb"), xor_key};
|
||||
std::vector<std::byte> read2;
|
||||
// Check that ignore works
|
||||
xor_file.ignore(4);
|
||||
|
@ -289,7 +337,7 @@ BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
|
|||
// Degenerate case
|
||||
{
|
||||
DataStream ds{in};
|
||||
ds.Xor("0000000000000000"_hex_v_u8);
|
||||
Obfuscation{0}(ds);
|
||||
BOOST_CHECK_EQUAL(""s, ds.str());
|
||||
}
|
||||
|
||||
|
@ -297,12 +345,10 @@ BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
|
|||
in.push_back(std::byte{0xf0});
|
||||
|
||||
{
|
||||
const auto key_bytes{"ffffffffffffffff"_hex_v};
|
||||
uint64_t xor_key;
|
||||
std::memcpy(&xor_key, key_bytes.data(), sizeof(xor_key));
|
||||
const Obfuscation obfuscation{"ffffffffffffffff"_hex_v};
|
||||
|
||||
DataStream ds{in};
|
||||
ds.Xor("ffffffffffffffff"_hex_v_u8);
|
||||
obfuscation(ds);
|
||||
BOOST_CHECK_EQUAL("\xf0\x0f"s, ds.str());
|
||||
}
|
||||
|
||||
|
@ -311,10 +357,10 @@ BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
|
|||
in.push_back(std::byte{0x0f});
|
||||
|
||||
{
|
||||
const auto key_bytes{"ff0fff0fff0fff0f"_hex_v};
|
||||
const Obfuscation obfuscation{"ff0fff0fff0fff0f"_hex_v};
|
||||
|
||||
DataStream ds{in};
|
||||
ds.Xor("ff0fff0fff0fff0f"_hex_v_u8);
|
||||
obfuscation(ds);
|
||||
BOOST_CHECK_EQUAL("\x0f\x00"s, ds.str());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue