Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2025-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_UTIL_OBFUSCATION_H6#define BITCOIN_UTIL_OBFUSCATION_H8#include <crypto/hex_base.h>9#include <span.h>10#include <tinyformat.h>11#include <util/strencodings.h>13#include <array>14#include <bit>15#include <climits>16#include <cstdint>17#include <ios>18#include <memory>20class Obfuscation21{22public:23using KeyType = uint64_t;24static constexpr size_t KEY_SIZE{sizeof(KeyType)};26Obfuscation() { SetRotations(0); }27explicit Obfuscation(std::span<const std::byte, KEY_SIZE> key_bytes)28{29SetRotations(ToKey(key_bytes));30}32operator bool() const { return m_rotations[0] != 0; }34void operator()(std::span<std::byte> target, size_t key_offset = 0) const35{36if (!*this) return;38KeyType rot_key{m_rotations[key_offset % KEY_SIZE]}; // Continue obfuscation from where we left off39if (target.size() > KEY_SIZE) {40// Obfuscate until KEY_SIZE alignment boundary41if (const auto misalign{reinterpret_cast<uintptr_t>(target.data()) % KEY_SIZE}) {42const size_t alignment{KEY_SIZE - misalign};43XorWord(target.first(alignment), rot_key);45target = {std::assume_aligned<KEY_SIZE>(target.data() + alignment), target.size() - alignment};46rot_key = m_rotations[(key_offset + alignment) % KEY_SIZE];47}48// Aligned obfuscation in 8*KEY_SIZE chunks49for (constexpr auto unroll{8}; target.size() >= KEY_SIZE * unroll; target = target.subspan(KEY_SIZE * unroll)) {50for (size_t i{0}; i < unroll; ++i) {51XorWord(target.subspan(i * KEY_SIZE, KEY_SIZE), rot_key);52}53}54// Aligned obfuscation in KEY_SIZE chunks55for (; target.size() >= KEY_SIZE; target = target.subspan(KEY_SIZE)) {56XorWord(target.first<KEY_SIZE>(), rot_key);57}58}59XorWord(target, rot_key);60}62template <typename Stream>63void Serialize(Stream& s) const64{65// Use vector serialization for convenient compact size prefix.66std::vector<std::byte> bytes{KEY_SIZE};67std::memcpy(bytes.data(), &m_rotations[0], KEY_SIZE);68s << bytes;69}71template <typename Stream>72void Unserialize(Stream& s)73{74std::vector<std::byte> bytes{KEY_SIZE};75s >> bytes;76if (bytes.size() != KEY_SIZE) throw std::ios_base::failure(strprintf("Obfuscation key size should be exactly %s bytes long", KEY_SIZE));77SetRotations(ToKey(std::span<std::byte, KEY_SIZE>(bytes)));78}80std::string HexKey() const81{82return HexStr(std::as_bytes(std::span{&m_rotations[0], 1}));83}85private:86// Cached key rotations for different offsets.87std::array<KeyType, KEY_SIZE> m_rotations;89void SetRotations(KeyType key)90{91for (size_t i{0}; i < KEY_SIZE; ++i) {92int key_rotation_bits{int(CHAR_BIT * i)};93if constexpr (std::endian::native == std::endian::big) key_rotation_bits *= -1;94m_rotations[i] = std::rotr(key, key_rotation_bits);95}96}98static KeyType ToKey(std::span<const std::byte, KEY_SIZE> key_span)99{100KeyType key{};101std::memcpy(&key, key_span.data(), KEY_SIZE);102return key;103}105static void XorWord(std::span<std::byte> target, KeyType key)106{107assert(target.size() <= KEY_SIZE);108if (target.empty()) return;109KeyType raw{};110std::memcpy(&raw, target.data(), target.size());111raw ^= key;112std::memcpy(target.data(), &raw, target.size());113}114};116#endif // BITCOIN_UTIL_OBFUSCATION_H