Coverage Report

Created: 2026-06-01 18:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/util/obfuscation.h
Line
Count
Source
1
// Copyright (c) 2025-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#ifndef BITCOIN_UTIL_OBFUSCATION_H
6
#define BITCOIN_UTIL_OBFUSCATION_H
7
8
#include <crypto/hex_base.h>
9
#include <span.h>
10
#include <tinyformat.h>
11
#include <util/strencodings.h>
12
13
#include <array>
14
#include <bit>
15
#include <climits>
16
#include <cstdint>
17
#include <ios>
18
#include <memory>
19
20
class Obfuscation
21
{
22
public:
23
    using KeyType = uint64_t;
24
    static constexpr size_t KEY_SIZE{sizeof(KeyType)};
25
26
1.22k
    Obfuscation() { SetRotations(0); }
27
    explicit Obfuscation(std::span<const std::byte, KEY_SIZE> key_bytes)
28
305
    {
29
305
        SetRotations(ToKey(key_bytes));
30
305
    }
31
32
12.1M
    operator bool() const { return m_rotations[0] != 0; }
33
34
    void operator()(std::span<std::byte> target, size_t key_offset = 0) const
35
4.27k
    {
36
4.27k
        if (!*this) return;
  Branch (36:13): [True: 1.83k, False: 2.44k]
37
38
2.44k
        KeyType rot_key{m_rotations[key_offset % KEY_SIZE]}; // Continue obfuscation from where we left off
39
2.44k
        if (target.size() > KEY_SIZE) {
  Branch (39:13): [True: 1.52k, False: 915]
40
            // Obfuscate until KEY_SIZE alignment boundary
41
1.52k
            if (const auto misalign{reinterpret_cast<uintptr_t>(target.data()) % KEY_SIZE}) {
  Branch (41:28): [True: 0, False: 1.52k]
42
0
                const size_t alignment{KEY_SIZE - misalign};
43
0
                XorWord(target.first(alignment), rot_key);
44
45
0
                target = {std::assume_aligned<KEY_SIZE>(target.data() + alignment), target.size() - alignment};
46
0
                rot_key = m_rotations[(key_offset + alignment) % KEY_SIZE];
47
0
            }
48
            // Aligned obfuscation in 8*KEY_SIZE chunks
49
2.13k
            for (constexpr auto unroll{8}; target.size() >= KEY_SIZE * unroll; target = target.subspan(KEY_SIZE * unroll)) {
  Branch (49:44): [True: 610, False: 1.52k]
50
5.49k
                for (size_t i{0}; i < unroll; ++i) {
  Branch (50:35): [True: 4.88k, False: 610]
51
4.88k
                    XorWord(target.subspan(i * KEY_SIZE, KEY_SIZE), rot_key);
52
4.88k
                }
53
610
            }
54
            // Aligned obfuscation in KEY_SIZE chunks
55
5.18k
            for (; target.size() >= KEY_SIZE; target = target.subspan(KEY_SIZE)) {
  Branch (55:20): [True: 3.66k, False: 1.52k]
56
3.66k
                XorWord(target.first<KEY_SIZE>(), rot_key);
57
3.66k
            }
58
1.52k
        }
59
2.44k
        XorWord(target, rot_key);
60
2.44k
    }
61
62
    template <typename Stream>
63
    void Serialize(Stream& s) const
64
305
    {
65
        // Use vector serialization for convenient compact size prefix.
66
305
        std::vector<std::byte> bytes{KEY_SIZE};
67
305
        std::memcpy(bytes.data(), &m_rotations[0], KEY_SIZE);
68
305
        s << bytes;
69
305
    }
Unexecuted instantiation: void Obfuscation::Serialize<DataStream>(DataStream&) const
void Obfuscation::Serialize<AutoFile>(AutoFile&) const
Line
Count
Source
64
305
    {
65
        // Use vector serialization for convenient compact size prefix.
66
305
        std::vector<std::byte> bytes{KEY_SIZE};
67
305
        std::memcpy(bytes.data(), &m_rotations[0], KEY_SIZE);
68
305
        s << bytes;
69
305
    }
70
71
    template <typename Stream>
72
    void Unserialize(Stream& s)
73
0
    {
74
0
        std::vector<std::byte> bytes{KEY_SIZE};
75
0
        s >> bytes;
76
0
        if (bytes.size() != KEY_SIZE) throw std::ios_base::failure(strprintf("Obfuscation key size should be exactly %s bytes long", KEY_SIZE));
  Branch (76:13): [True: 0, False: 0]
  Branch (76:13): [True: 0, False: 0]
77
0
        SetRotations(ToKey(std::span<std::byte, KEY_SIZE>(bytes)));
78
0
    }
Unexecuted instantiation: void Obfuscation::Unserialize<SpanReader>(SpanReader&)
Unexecuted instantiation: void Obfuscation::Unserialize<AutoFile>(AutoFile&)
79
80
    std::string HexKey() const
81
0
    {
82
0
        return HexStr(std::as_bytes(std::span{&m_rotations[0], 1}));
83
0
    }
84
85
private:
86
    // Cached key rotations for different offsets.
87
    std::array<KeyType, KEY_SIZE> m_rotations;
88
89
    void SetRotations(KeyType key)
90
1.52k
    {
91
13.7k
        for (size_t i{0}; i < KEY_SIZE; ++i) {
  Branch (91:27): [True: 12.2k, False: 1.52k]
92
12.2k
            int key_rotation_bits{int(CHAR_BIT * i)};
93
            if constexpr (std::endian::native == std::endian::big) key_rotation_bits *= -1;
94
12.2k
            m_rotations[i] = std::rotr(key, key_rotation_bits);
95
12.2k
        }
96
1.52k
    }
97
98
    static KeyType ToKey(std::span<const std::byte, KEY_SIZE> key_span)
99
305
    {
100
305
        KeyType key{};
101
305
        std::memcpy(&key, key_span.data(), KEY_SIZE);
102
305
        return key;
103
305
    }
104
105
    static void XorWord(std::span<std::byte> target, KeyType key)
106
10.9k
    {
107
10.9k
        assert(target.size() <= KEY_SIZE);
  Branch (107:9): [True: 10.9k, False: 0]
108
10.9k
        if (target.empty()) return;
  Branch (108:13): [True: 915, False: 10.0k]
109
10.0k
        KeyType raw{};
110
10.0k
        std::memcpy(&raw, target.data(), target.size());
111
10.0k
        raw ^= key;
112
10.0k
        std::memcpy(target.data(), &raw, target.size());
113
10.0k
    }
114
};
115
116
#endif // BITCOIN_UTIL_OBFUSCATION_H