Coverage Report

Created: 2026-09-02 14:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/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
23.3k
    Obfuscation() { SetRotations(0); }
27
    explicit Obfuscation(std::span<const std::byte, KEY_SIZE> key_bytes)
28
2.89k
    {
29
2.89k
        SetRotations(ToKey(key_bytes));
30
2.89k
    }
31
32
68.4M
    operator bool() const { return m_rotations[0] != 0; }
33
34
    void operator()(std::span<std::byte> target, size_t key_offset = 0) const
35
1.73M
    {
36
1.73M
        if (!*this) return;
37
38
1.34M
        KeyType rot_key{m_rotations[key_offset % KEY_SIZE]}; // Continue obfuscation from where we left off
39
1.34M
        if (target.size() > KEY_SIZE) {
40
            // Obfuscate until KEY_SIZE alignment boundary
41
976k
            if (const auto misalign{reinterpret_cast<uintptr_t>(target.data()) % KEY_SIZE}) {
42
106
                const size_t alignment{KEY_SIZE - misalign};
43
106
                XorWord(target.first(alignment), rot_key);
44
45
106
                target = {std::assume_aligned<KEY_SIZE>(target.data() + alignment), target.size() - alignment};
46
106
                rot_key = m_rotations[(key_offset + alignment) % KEY_SIZE];
47
106
            }
48
            // Aligned obfuscation in 8*KEY_SIZE chunks
49
98.3M
            for (constexpr auto unroll{8}; target.size() >= KEY_SIZE * unroll; target = target.subspan(KEY_SIZE * unroll)) {
50
874M
                for (size_t i{0}; i < unroll; ++i) {
51
777M
                    XorWord(target.subspan(i * KEY_SIZE, KEY_SIZE), rot_key);
52
777M
                }
53
97.3M
            }
54
            // Aligned obfuscation in KEY_SIZE chunks
55
4.14M
            for (; target.size() >= KEY_SIZE; target = target.subspan(KEY_SIZE)) {
56
3.16M
                XorWord(target.first<KEY_SIZE>(), rot_key);
57
3.16M
            }
58
976k
        }
59
1.34M
        XorWord(target, rot_key);
60
1.34M
    }
61
62
    template <typename Stream>
63
    void Serialize(Stream& s) const
64
1.51k
    {
65
        // Use vector serialization for convenient compact size prefix.
66
1.51k
        std::vector<std::byte> bytes{KEY_SIZE};
67
1.51k
        std::memcpy(bytes.data(), &m_rotations[0], KEY_SIZE);
68
1.51k
        s << bytes;
69
1.51k
    }
void Obfuscation::Serialize<DataStream>(DataStream&) const
Line
Count
Source
64
539
    {
65
        // Use vector serialization for convenient compact size prefix.
66
539
        std::vector<std::byte> bytes{KEY_SIZE};
67
539
        std::memcpy(bytes.data(), &m_rotations[0], KEY_SIZE);
68
539
        s << bytes;
69
539
    }
void Obfuscation::Serialize<AutoFile>(AutoFile&) const
Line
Count
Source
64
974
    {
65
        // Use vector serialization for convenient compact size prefix.
66
974
        std::vector<std::byte> bytes{KEY_SIZE};
67
974
        std::memcpy(bytes.data(), &m_rotations[0], KEY_SIZE);
68
974
        s << bytes;
69
974
    }
70
71
    template <typename Stream>
72
    void Unserialize(Stream& s)
73
1.36k
    {
74
1.36k
        std::vector<std::byte> bytes{KEY_SIZE};
75
1.36k
        s >> bytes;
76
1.36k
        if (bytes.size() != KEY_SIZE) throw std::ios_base::failure(strprintf("Obfuscation key size should be exactly %s bytes long", KEY_SIZE));
77
1.36k
        SetRotations(ToKey(std::span<std::byte, KEY_SIZE>(bytes)));
78
1.36k
    }
void Obfuscation::Unserialize<DataStream>(DataStream&)
Line
Count
Source
73
1
    {
74
1
        std::vector<std::byte> bytes{KEY_SIZE};
75
1
        s >> bytes;
76
1
        if (bytes.size() != KEY_SIZE) throw std::ios_base::failure(strprintf("Obfuscation key size should be exactly %s bytes long", KEY_SIZE));
77
1
        SetRotations(ToKey(std::span<std::byte, KEY_SIZE>(bytes)));
78
1
    }
void Obfuscation::Unserialize<SpanReader>(SpanReader&)
Line
Count
Source
73
892
    {
74
892
        std::vector<std::byte> bytes{KEY_SIZE};
75
892
        s >> bytes;
76
892
        if (bytes.size() != KEY_SIZE) throw std::ios_base::failure(strprintf("Obfuscation key size should be exactly %s bytes long", KEY_SIZE));
77
892
        SetRotations(ToKey(std::span<std::byte, KEY_SIZE>(bytes)));
78
892
    }
void Obfuscation::Unserialize<AutoFile>(AutoFile&)
Line
Count
Source
73
474
    {
74
474
        std::vector<std::byte> bytes{KEY_SIZE};
75
474
        s >> bytes;
76
474
        if (bytes.size() != KEY_SIZE) throw std::ios_base::failure(strprintf("Obfuscation key size should be exactly %s bytes long", KEY_SIZE));
77
474
        SetRotations(ToKey(std::span<std::byte, KEY_SIZE>(bytes)));
78
474
    }
79
80
    std::string HexKey() const
81
3.45k
    {
82
3.45k
        return HexStr(std::as_bytes(std::span{&m_rotations[0], 1}));
83
3.45k
    }
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
27.5k
    {
91
248k
        for (size_t i{0}; i < KEY_SIZE; ++i) {
92
220k
            int key_rotation_bits{int(CHAR_BIT * i)};
93
            if constexpr (std::endian::native == std::endian::big) key_rotation_bits *= -1;
94
220k
            m_rotations[i] = std::rotr(key, key_rotation_bits);
95
220k
        }
96
27.5k
    }
97
98
    static KeyType ToKey(std::span<const std::byte, KEY_SIZE> key_span)
99
4.26k
    {
100
4.26k
        KeyType key{};
101
4.26k
        std::memcpy(&key, key_span.data(), KEY_SIZE);
102
4.26k
        return key;
103
4.26k
    }
104
105
    static void XorWord(std::span<std::byte> target, KeyType key)
106
781M
    {
107
781M
        assert(target.size() <= KEY_SIZE);
108
781M
        if (target.empty()) return;
109
781M
        KeyType raw{};
110
781M
        std::memcpy(&raw, target.data(), target.size());
111
781M
        raw ^= key;
112
781M
        std::memcpy(target.data(), &raw, target.size());
113
781M
    }
114
};
115
116
#endif // BITCOIN_UTIL_OBFUSCATION_H