Skip to content

Commit 6bbf2d9

Browse files
committed
refactor: prepare DBWrapper for obfuscation key change
Since `FastRandomContext` delegates to `GetRandBytes` anyway, we can simplify new key generation to a Write/Read combo, unifying the flow of enabling obfuscation via `Read`. The comments were also adjusted to clarify that the `m_obfuscation` field affects the behavior of `Read` and `Write` methods. These changes are meant to simplify the diffs for the riskier optimization commits later.
1 parent 0b8bec8 commit 6bbf2d9

File tree

2 files changed

+9
-37
lines changed

2 files changed

+9
-37
lines changed

src/dbwrapper.cpp

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -249,24 +249,15 @@ CDBWrapper::CDBWrapper(const DBParams& params)
249249
LogPrintf("Finished database compaction of %s\n", fs::PathToString(params.path));
250250
}
251251

252-
// The base-case obfuscation key, which is a noop.
253-
m_obfuscation = std::vector<unsigned char>(Obfuscation::KEY_SIZE, '\000');
254-
255-
bool key_exists = Read(OBFUSCATION_KEY_KEY, m_obfuscation);
256-
257-
if (!key_exists && params.obfuscate && IsEmpty()) {
258-
// Initialize non-degenerate obfuscation if it won't upset
259-
// existing, non-obfuscated data.
260-
std::vector<unsigned char> new_key = CreateObfuscation();
261-
262-
// Write `new_key` so we don't obfuscate the key with itself
263-
Write(OBFUSCATION_KEY_KEY, new_key);
264-
m_obfuscation = new_key;
265-
266-
LogPrintf("Wrote new obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(m_obfuscation));
252+
m_obfuscation = std::vector<uint8_t>(Obfuscation::KEY_SIZE, '\000'); // Needed for unobfuscated Read()/Write() below
253+
if (!Read(OBFUSCATION_KEY_KEY, m_obfuscation) && params.obfuscate && IsEmpty()) {
254+
// Generate, write and read back the new obfuscation key, making sure we don't obfuscate the key itself
255+
Write(OBFUSCATION_KEY_KEY, FastRandomContext{}.randbytes(Obfuscation::KEY_SIZE));
256+
Read(OBFUSCATION_KEY_KEY, m_obfuscation);
257+
LogInfo("Wrote new obfuscation key for %s: %s", fs::PathToString(params.path), HexStr(m_obfuscation));
267258
}
259+
LogInfo("Using obfuscation key for %s: %s", fs::PathToString(params.path), HexStr(m_obfuscation));
268260

269-
LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(m_obfuscation));
270261
}
271262

272263
CDBWrapper::~CDBWrapper()
@@ -311,23 +302,6 @@ size_t CDBWrapper::DynamicMemoryUsage() const
311302
return parsed.value();
312303
}
313304

314-
// Prefixed with null character to avoid collisions with other keys
315-
//
316-
// We must use a string constructor which specifies length so that we copy
317-
// past the null-terminator.
318-
const std::string CDBWrapper::OBFUSCATION_KEY_KEY("\000obfuscate_key", 14);
319-
320-
/**
321-
* Returns a string (consisting of 8 random bytes) suitable for use as an
322-
* obfuscating XOR key.
323-
*/
324-
std::vector<unsigned char> CDBWrapper::CreateObfuscation() const
325-
{
326-
std::vector<uint8_t> ret(Obfuscation::KEY_SIZE);
327-
GetRandBytes(ret);
328-
return ret;
329-
}
330-
331305
std::optional<std::string> CDBWrapper::ReadImpl(std::span<const std::byte> key) const
332306
{
333307
leveldb::Slice slKey(CharCast(key.data()), key.size());

src/dbwrapper.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,8 @@ class CDBWrapper
190190
//! a key used for optional XOR-obfuscation of the database
191191
std::vector<unsigned char> m_obfuscation;
192192

193-
//! the key under which the obfuscation key is stored
194-
static const std::string OBFUSCATION_KEY_KEY;
195-
196-
std::vector<unsigned char> CreateObfuscation() const;
193+
//! obfuscation key storage key, null-prefixed to avoid collisions
194+
inline static const std::string OBFUSCATION_KEY_KEY{"\000obfuscate_key", 14}; // explicit size to avoid truncation at leading \0
197195

198196
//! path to filesystem storage
199197
const fs::path m_path;

0 commit comments

Comments
 (0)