scripted-diff: unify xor-vs-obfuscation nomenclature

Mechanical refactor of the low-level "xor" wording to signal the intent instead of the implementation used.
The renames are ordered by heaviest-hitting substitutions first, and were constructed such that after each replacement the code is still compilable.

-BEGIN VERIFY SCRIPT-
sed -i \
  -e 's/\bGetObfuscateKey\b/GetObfuscation/g' \
  -e 's/\bxor_key\b/obfuscation/g' \
  -e 's/\bxor_pat\b/obfuscation/g' \
  -e 's/\bm_xor_key\b/m_obfuscation/g' \
  -e 's/\bm_xor\b/m_obfuscation/g' \
  -e 's/\bobfuscate_key\b/m_obfuscation/g' \
  -e 's/\bOBFUSCATE_KEY_KEY\b/OBFUSCATION_KEY_KEY/g' \
  -e 's/\bSetXor(/SetObfuscation(/g' \
  -e 's/\bdata_xor\b/obfuscation/g' \
  -e 's/\bCreateObfuscateKey\b/CreateObfuscation/g' \
  -e 's/\bobfuscate key\b/obfuscation key/g' \
  $(git ls-files '*.cpp' '*.h')
-END VERIFY SCRIPT-
This commit is contained in:
Lőrinc
2025-04-25 23:18:48 +02:00
parent 972697976c
commit 0b8bec8aa6
9 changed files with 59 additions and 59 deletions

View File

@@ -174,7 +174,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));
ssValue.Xor(dbwrapper_private::GetObfuscation(parent));
leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
m_impl_batch->batch.Put(slKey, slValue);
}
@@ -250,23 +250,23 @@ CDBWrapper::CDBWrapper(const DBParams& params)
}
// The base-case obfuscation key, which is a noop.
obfuscate_key = std::vector<unsigned char>(Obfuscation::KEY_SIZE, '\000');
m_obfuscation = std::vector<unsigned char>(Obfuscation::KEY_SIZE, '\000');
bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
bool key_exists = Read(OBFUSCATION_KEY_KEY, m_obfuscation);
if (!key_exists && params.obfuscate && IsEmpty()) {
// Initialize non-degenerate obfuscation if it won't upset
// existing, non-obfuscated data.
std::vector<unsigned char> new_key = CreateObfuscateKey();
std::vector<unsigned char> new_key = CreateObfuscation();
// Write `new_key` so we don't obfuscate the key with itself
Write(OBFUSCATE_KEY_KEY, new_key);
obfuscate_key = new_key;
Write(OBFUSCATION_KEY_KEY, new_key);
m_obfuscation = new_key;
LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key));
LogPrintf("Wrote new obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(m_obfuscation));
}
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(m_obfuscation));
}
CDBWrapper::~CDBWrapper()
@@ -315,13 +315,13 @@ 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 std::string CDBWrapper::OBFUSCATION_KEY_KEY("\000obfuscate_key", 14);
/**
* Returns a string (consisting of 8 random bytes) suitable for use as an
* obfuscating XOR key.
*/
std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
std::vector<unsigned char> CDBWrapper::CreateObfuscation() const
{
std::vector<uint8_t> ret(Obfuscation::KEY_SIZE);
GetRandBytes(ret);
@@ -411,9 +411,9 @@ void CDBIterator::Next() { m_impl_iter->iter->Next(); }
namespace dbwrapper_private {
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
const std::vector<unsigned char>& GetObfuscation(const CDBWrapper &w)
{
return w.obfuscate_key;
return w.m_obfuscation;
}
} // namespace dbwrapper_private

View File

@@ -63,7 +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);
const std::vector<unsigned char>& GetObfuscation(const CDBWrapper &w);
}; // namespace dbwrapper_private
@@ -166,7 +166,7 @@ public:
template<typename V> bool GetValue(V& value) {
try {
DataStream ssValue{GetValueImpl()};
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
ssValue.Xor(dbwrapper_private::GetObfuscation(parent));
ssValue >> value;
} catch (const std::exception&) {
return false;
@@ -179,7 +179,7 @@ struct LevelDBContext;
class CDBWrapper
{
friend const std::vector<unsigned char>& dbwrapper_private::GetObfuscateKey(const CDBWrapper &w);
friend const std::vector<unsigned char>& dbwrapper_private::GetObfuscation(const CDBWrapper &w);
private:
//! holds all leveldb-specific fields of this class
std::unique_ptr<LevelDBContext> m_db_context;
@@ -188,12 +188,12 @@ private:
std::string m_name;
//! a key used for optional XOR-obfuscation of the database
std::vector<unsigned char> obfuscate_key;
std::vector<unsigned char> m_obfuscation;
//! the key under which the obfuscation key is stored
static const std::string OBFUSCATE_KEY_KEY;
static const std::string OBFUSCATION_KEY_KEY;
std::vector<unsigned char> CreateObfuscateKey() const;
std::vector<unsigned char> CreateObfuscation() const;
//! path to filesystem storage
const fs::path m_path;
@@ -225,7 +225,7 @@ public:
}
try {
DataStream ssValue{MakeByteSpan(*strValue)};
ssValue.Xor(obfuscate_key);
ssValue.Xor(m_obfuscation);
ssValue >> value;
} catch (const std::exception&) {
return false;

View File

@@ -780,13 +780,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
@@ -1124,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<std::byte, Obfuscation::KEY_SIZE> xor_key{};
std::array<std::byte, Obfuscation::KEY_SIZE> obfuscation{};
// 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
@@ -1141,14 +1141,14 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
if (opts.use_xor && first_run) {
// Only use random fresh key when the boolean option is set and on the
// very first start of the program.
FastRandomContext{}.fillrand(xor_key);
FastRandomContext{}.fillrand(obfuscation);
}
const fs::path xor_key_path{opts.blocks_dir / "xor.dat"};
if (fs::exists(xor_key_path)) {
// A pre-existing xor key file has priority.
AutoFile xor_key_file{fsbridge::fopen(xor_key_path, "rb")};
xor_key_file >> xor_key;
xor_key_file >> obfuscation;
} else {
// Create initial or missing xor key file
AutoFile xor_key_file{fsbridge::fopen(xor_key_path,
@@ -1158,7 +1158,7 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
"wbx"
#endif
)};
xor_key_file << xor_key;
xor_key_file << obfuscation;
if (xor_key_file.fclose() != 0) {
throw std::runtime_error{strprintf("Error closing XOR key file %s: %s",
fs::PathToString(xor_key_path),
@@ -1166,20 +1166,20 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
}
}
// If the user disabled the key, it must be zero.
if (!opts.use_xor && xor_key != decltype(xor_key){}) {
if (!opts.use_xor && obfuscation != decltype(obfuscation){}) {
throw std::runtime_error{
strprintf("The blocksdir XOR-key can not be disabled when a random key was already stored! "
"Stored key: '%s', stored path: '%s'.",
HexStr(xor_key), fs::PathToString(xor_key_path)),
HexStr(obfuscation), fs::PathToString(xor_key_path)),
};
}
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()};
LogInfo("Using obfuscation key for blocksdir *.dat files (%s): '%s'\n", fs::PathToString(opts.blocks_dir), HexStr(obfuscation));
return std::vector<std::byte>{obfuscation.begin(), obfuscation.end()};
}
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}},

View File

@@ -235,7 +235,7 @@ private:
const bool m_prune_mode;
const std::vector<std::byte> m_xor_key;
const std::vector<std::byte> m_obfuscation;
/** Dirty block index entries. */
std::set<CBlockIndex*> m_dirty_blockindex;

View File

@@ -60,15 +60,15 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
try {
uint64_t version;
file >> version;
std::vector<std::byte> xor_key;
std::vector<std::byte> obfuscation;
if (version == MEMPOOL_DUMP_VERSION_NO_XOR_KEY) {
// Leave XOR-key empty
} else if (version == MEMPOOL_DUMP_VERSION) {
file >> xor_key;
file >> obfuscation;
} else {
return false;
}
file.SetXor(xor_key);
file.SetObfuscation(obfuscation);
uint64_t total_txns_to_load;
file >> total_txns_to_load;
uint64_t txns_tried = 0;
@@ -180,12 +180,12 @@ 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<std::byte> xor_key(Obfuscation::KEY_SIZE);
std::vector<std::byte> obfuscation(Obfuscation::KEY_SIZE);
if (!pool.m_opts.persist_v1_dat) {
FastRandomContext{}.fillrand(xor_key);
file << xor_key;
FastRandomContext{}.fillrand(obfuscation);
file << obfuscation;
}
file.SetXor(xor_key);
file.SetObfuscation(obfuscation);
uint64_t mempool_transactions_to_write(vinfo.size());
file << mempool_transactions_to_write;

View File

@@ -9,8 +9,8 @@
#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, std::vector<std::byte> obfuscation)
: m_file{file}, m_obfuscation{std::move(obfuscation)}
{
if (!IsNull()) {
auto pos{std::ftell(m_file)};
@@ -22,9 +22,9 @@ 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_obfuscation.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);
util::Xor(dst.subspan(0, ret), m_obfuscation, *m_position);
}
if (m_position.has_value()) *m_position += ret;
return ret;
@@ -81,7 +81,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.empty()) {
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
throw std::ios_base::failure("AutoFile::write: write failed");
}
@@ -101,9 +101,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.size()) {
if (!m_position) throw std::ios_base::failure("AutoFile::write_buffer: obfuscation position unknown");
util::Xor(src, m_xor, *m_position); // obfuscate in-place
util::Xor(src, m_obfuscation, *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");

View File

@@ -402,12 +402,12 @@ class AutoFile
{
protected:
std::FILE* m_file;
std::vector<std::byte> m_xor;
std::vector<std::byte> m_obfuscation;
std::optional<int64_t> m_position;
bool m_was_written{false};
public:
explicit AutoFile(std::FILE* file, std::vector<std::byte> data_xor={});
explicit AutoFile(std::FILE* file, std::vector<std::byte> obfuscation={});
~AutoFile()
{
@@ -455,7 +455,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(std::vector<std::byte> obfuscation) { m_obfuscation = obfuscation; }
/** Implementation detail, only used internally. */
std::size_t detail_fread(std::span<std::byte> dst);

View File

@@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
BOOST_CHECK_EQUAL(obfuscate, !dbw.IsEmpty());
// Ensure that we're doing real obfuscation when obfuscate=true
obfuscation_key = dbwrapper_private::GetObfuscateKey(dbw);
obfuscation_key = dbwrapper_private::GetObfuscation(dbw);
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(obfuscation_key));
for (uint8_t k{0}; k < 10; ++k) {
@@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
// Verify that the obfuscation key is never obfuscated
{
CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = false}};
BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscateKey(dbw));
BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscation(dbw));
}
// Read back the values
@@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = obfuscate}};
// Ensure obfuscation is read back correctly
BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscateKey(dbw));
BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscation(dbw));
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(obfuscation_key));
// Verify all written values
@@ -89,7 +89,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
bool res_bool;
// Ensure that we're doing real obfuscation when obfuscate=true
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(dbwrapper_private::GetObfuscateKey(dbw)));
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(dbwrapper_private::GetObfuscation(dbw)));
//Simulate block raw data - "b + block hash"
std::string key_block = "b" + m_rng.rand256().ToString();
@@ -264,7 +264,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());
BOOST_CHECK(!odbw.IsEmpty());
BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); // The key should be an empty string
BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscation(odbw))); // The key should be an empty string
uint256 in2 = m_rng.rand256();
uint256 res3;
@@ -301,7 +301,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(!is_null_key(dbwrapper_private::GetObfuscation(odbw)));
uint256 in2 = m_rng.rand256();
uint256 res3;

View File

@@ -79,11 +79,11 @@ BOOST_AUTO_TEST_CASE(xor_file)
auto raw_file{[&](const auto& mode) { return fsbridge::fopen(xor_path, mode); }};
const std::vector<uint8_t> test1{1, 2, 3};
const std::vector<uint8_t> test2{4, 5};
const auto xor_pat{"ff00ff00ff00ff00"_hex_v};
const auto obfuscation{"ff00ff00ff00ff00"_hex_v};
{
// Check errors for missing file
AutoFile xor_file{raw_file("rb"), xor_pat};
AutoFile xor_file{raw_file("rb"), obfuscation};
BOOST_CHECK_EXCEPTION(xor_file << std::byte{}, std::ios_base::failure, HasReason{"AutoFile::write: file handle is nullptr"});
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: file handle is nullptr"});
BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: file handle is nullptr"});
@@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
#else
const char* mode = "wbx";
#endif
AutoFile xor_file{raw_file(mode), xor_pat};
AutoFile xor_file{raw_file(mode), obfuscation};
xor_file << test1 << test2;
BOOST_REQUIRE_EQUAL(xor_file.fclose(), 0);
}
@@ -109,7 +109,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"), xor_pat};
AutoFile xor_file{raw_file("rb"), obfuscation};
std::vector<std::byte> read1, read2;
xor_file >> read1 >> read2;
BOOST_CHECK_EQUAL(HexStr(read1), HexStr(test1));
@@ -118,7 +118,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"), xor_pat};
AutoFile xor_file{raw_file("rb"), obfuscation};
std::vector<std::byte> read2;
// Check that ignore works
xor_file.ignore(4);