Skip to content

Commit d767503

Browse files
committed
Merge bitcoin#33039: refactor,test: follow-ups to multi-byte block obfuscation
86e3a0a refactor: standardize obfuscation memory alignment (Lőrinc) 13f0034 refactor: write `Obfuscation` object when new key is generated in dbwrapper (Lőrinc) e5b1b7c refactor: rename `OBFUSCATION_KEY_KEY` (Lőrinc) 298bf95 refactor: simplify `Obfuscation::HexKey` (Lőrinc) 2dea045 test: make `obfuscation_serialize` more thorough (Lőrinc) a17d820 test: merge xor_roundtrip_random_chunks and xor_bytes_reference (Lőrinc) Pull request description: Follow up for bitcoin#31144 Applied the remaining comments in separate commits - except for the last one where I could group them. Please see the commit messages for more context. ACKs for top commit: achow101: ACK 86e3a0a ryanofsky: Code review ACK 86e3a0a, just tweaking key write assert as suggested hodlinator: ACK 86e3a0a Tree-SHA512: 967510a141fbb57bf9d088d92b554cf2fffc2f6aa0eab756cbae3230f53e9b04ceebcc6fea5f3383c01ad41985ecde5b5686c64a771ca9deae3497b9b88c1c8b
2 parents cf15d45 + 86e3a0a commit d767503

4 files changed

Lines changed: 40 additions & 60 deletions

File tree

src/dbwrapper.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,12 @@ CDBWrapper::CDBWrapper(const DBParams& params)
249249
LogInfo("Finished database compaction of %s", fs::PathToString(params.path));
250250
}
251251

252-
assert(!m_obfuscation); // 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);
252+
if (!Read(OBFUSCATION_KEY, m_obfuscation) && params.obfuscate && IsEmpty()) {
253+
// Generate and write the new obfuscation key.
254+
const Obfuscation obfuscation{FastRandomContext{}.randbytes<Obfuscation::KEY_SIZE>()};
255+
assert(!m_obfuscation); // Make sure the key is written without obfuscation.
256+
Write(OBFUSCATION_KEY, obfuscation);
257+
m_obfuscation = obfuscation;
257258
LogInfo("Wrote new obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
258259
}
259260
LogInfo("Using obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());

src/dbwrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class CDBWrapper
189189
Obfuscation m_obfuscation;
190190

191191
//! obfuscation key storage key, null-prefixed to avoid collisions
192-
inline static const std::string OBFUSCATION_KEY_KEY{"\000obfuscate_key", 14}; // explicit size to avoid truncation at leading \0
192+
inline static const std::string OBFUSCATION_KEY{"\000obfuscate_key", 14}; // explicit size to avoid truncation at leading \0
193193

194194
//! path to filesystem storage
195195
const fs::path m_path;

src/test/streams_tests.cpp

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ using namespace util::hex_literals;
1818

1919
BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup)
2020

21-
// Test that obfuscation can be properly reverted even with random chunk sizes.
22-
BOOST_AUTO_TEST_CASE(xor_roundtrip_random_chunks)
21+
// Check optimized obfuscation with random offsets and sizes to ensure proper
22+
// handling of key wrapping. Also verify it roundtrips.
23+
BOOST_AUTO_TEST_CASE(xor_random_chunks)
2324
{
2425
auto apply_random_xor_chunks{[&](std::span<std::byte> target, const Obfuscation& obfuscation) {
2526
for (size_t offset{0}; offset < target.size();) {
@@ -37,41 +38,14 @@ BOOST_AUTO_TEST_CASE(xor_roundtrip_random_chunks)
3738
const auto key_bytes{m_rng.randbool() ? m_rng.randbytes<Obfuscation::KEY_SIZE>() : std::array<std::byte, Obfuscation::KEY_SIZE>{}};
3839
const Obfuscation obfuscation{key_bytes};
3940
apply_random_xor_chunks(roundtrip, obfuscation);
40-
41-
const bool key_all_zeros{std::ranges::all_of(
42-
std::span{key_bytes}.first(std::min(write_size, Obfuscation::KEY_SIZE)), [](auto b) { return b == std::byte{0}; })};
43-
BOOST_CHECK(key_all_zeros ? original == roundtrip : original != roundtrip);
44-
45-
apply_random_xor_chunks(roundtrip, obfuscation);
46-
BOOST_CHECK(original == roundtrip);
47-
}
48-
}
49-
50-
// Compares optimized obfuscation against a trivial, byte-by-byte reference implementation
51-
// with random offsets to ensure proper handling of key wrapping.
52-
BOOST_AUTO_TEST_CASE(xor_bytes_reference)
53-
{
54-
auto expected_xor{[](std::span<std::byte> target, std::span<const std::byte, Obfuscation::KEY_SIZE> obfuscation, size_t key_offset) {
55-
for (auto& b : target) {
56-
b ^= obfuscation[key_offset++ % obfuscation.size()];
41+
BOOST_CHECK_EQUAL(roundtrip.size(), original.size());
42+
for (size_t i{0}; i < original.size(); ++i) {
43+
BOOST_CHECK_EQUAL(roundtrip[i], original[i] ^ key_bytes[i % Obfuscation::KEY_SIZE]);
5744
}
58-
}};
59-
60-
for (size_t test{0}; test < 100; ++test) {
61-
const size_t write_size{1 + m_rng.randrange(100U)};
62-
const size_t key_offset{m_rng.randrange(3 * Obfuscation::KEY_SIZE)}; // Make sure the key can wrap around
63-
const size_t write_offset{std::min(write_size, m_rng.randrange(Obfuscation::KEY_SIZE * 2))}; // Write unaligned data
6445

65-
const auto key_bytes{m_rng.randbool() ? m_rng.randbytes<Obfuscation::KEY_SIZE>() : std::array<std::byte, Obfuscation::KEY_SIZE>{}};
66-
const Obfuscation obfuscation{key_bytes};
67-
std::vector expected{m_rng.randbytes<std::byte>(write_size)};
68-
std::vector actual{expected};
69-
70-
expected_xor(std::span{expected}.subspan(write_offset), key_bytes, key_offset);
71-
obfuscation(std::span{actual}.subspan(write_offset), key_offset);
72-
73-
BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end());
74-
}
46+
apply_random_xor_chunks(roundtrip, obfuscation);
47+
BOOST_CHECK_EQUAL_COLLECTIONS(roundtrip.begin(), roundtrip.end(), original.begin(), original.end());
48+
}
7549
}
7650

7751
BOOST_AUTO_TEST_CASE(obfuscation_hexkey)
@@ -84,19 +58,24 @@ BOOST_AUTO_TEST_CASE(obfuscation_hexkey)
8458

8559
BOOST_AUTO_TEST_CASE(obfuscation_serialize)
8660
{
87-
const Obfuscation original{m_rng.randbytes<Obfuscation::KEY_SIZE>()};
88-
89-
// Serialization
90-
DataStream ds;
91-
ds << original;
92-
93-
BOOST_CHECK_EQUAL(ds.size(), 1 + Obfuscation::KEY_SIZE); // serialized as a vector
94-
95-
// Deserialization
96-
Obfuscation recovered{};
97-
ds >> recovered;
98-
99-
BOOST_CHECK_EQUAL(recovered.HexKey(), original.HexKey());
61+
Obfuscation obfuscation{};
62+
BOOST_CHECK(!obfuscation);
63+
64+
// Test loading a key.
65+
std::vector key_in{m_rng.randbytes<std::byte>(Obfuscation::KEY_SIZE)};
66+
DataStream ds_in;
67+
ds_in << key_in;
68+
BOOST_CHECK_EQUAL(ds_in.size(), 1 + Obfuscation::KEY_SIZE); // serialized as a vector
69+
ds_in >> obfuscation;
70+
71+
// Test saving the key.
72+
std::vector<std::byte> key_out;
73+
DataStream ds_out;
74+
ds_out << obfuscation;
75+
ds_out >> key_out;
76+
77+
// Make sure saved key is the same.
78+
BOOST_CHECK_EQUAL_COLLECTIONS(key_in.begin(), key_in.end(), key_out.begin(), key_out.end());
10079
}
10180

10281
BOOST_AUTO_TEST_CASE(obfuscation_empty)

src/util/obfuscation.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,21 @@ class Obfuscation
3636

3737
KeyType rot_key{m_rotations[key_offset % KEY_SIZE]}; // Continue obfuscation from where we left off
3838
if (target.size() > KEY_SIZE) {
39-
// Obfuscate until 64-bit alignment boundary
40-
if (const auto misalign{std::bit_cast<uintptr_t>(target.data()) % KEY_SIZE}) {
41-
const size_t alignment{std::min(KEY_SIZE - misalign, target.size())};
39+
// Obfuscate until KEY_SIZE alignment boundary
40+
if (const auto misalign{reinterpret_cast<uintptr_t>(target.data()) % KEY_SIZE}) {
41+
const size_t alignment{KEY_SIZE - misalign};
4242
XorWord(target.first(alignment), rot_key);
4343

4444
target = {std::assume_aligned<KEY_SIZE>(target.data() + alignment), target.size() - alignment};
4545
rot_key = m_rotations[(key_offset + alignment) % KEY_SIZE];
4646
}
47-
// Aligned obfuscation in 64-byte chunks
47+
// Aligned obfuscation in 8*KEY_SIZE chunks
4848
for (constexpr auto unroll{8}; target.size() >= KEY_SIZE * unroll; target = target.subspan(KEY_SIZE * unroll)) {
4949
for (size_t i{0}; i < unroll; ++i) {
5050
XorWord(target.subspan(i * KEY_SIZE, KEY_SIZE), rot_key);
5151
}
5252
}
53-
// Aligned obfuscation in 64-bit chunks
53+
// Aligned obfuscation in KEY_SIZE chunks
5454
for (; target.size() >= KEY_SIZE; target = target.subspan(KEY_SIZE)) {
5555
XorWord(target.first<KEY_SIZE>(), rot_key);
5656
}
@@ -78,7 +78,7 @@ class Obfuscation
7878

7979
std::string HexKey() const
8080
{
81-
return HexStr(std::bit_cast<std::array<uint8_t, KEY_SIZE>>(m_rotations[0]));
81+
return HexStr(std::as_bytes(std::span{&m_rotations[0], 1}));
8282
}
8383

8484
private:

0 commit comments

Comments
 (0)