Inside Bitcoin
Opening the selected file…
Inside Bitcoin
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) 2023-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_CRYPTO_CHACHA20POLY1305_H6#define BITCOIN_CRYPTO_CHACHA20POLY1305_H8#include <cstddef>9#include <cstdint>10#include <span>12#include <crypto/chacha20.h>13#include <crypto/poly1305.h>15/** The AEAD_CHACHA20_POLY1305 authenticated encryption algorithm from RFC8439 section 2.8. */16class AEADChaCha20Poly130517{18/** Internal stream cipher. */19ChaCha20 m_chacha20;21public:22/** Expected size of key argument in constructor. */23static constexpr unsigned KEYLEN = 32;25/** Expansion when encrypting. */26static constexpr unsigned EXPANSION = Poly1305::TAGLEN;28/** Initialize an AEAD instance with a specified 32-byte key. */29AEADChaCha20Poly1305(std::span<const std::byte> key) noexcept;31/** Switch to another 32-byte key. */32void SetKey(std::span<const std::byte> key) noexcept;34/** 96-bit nonce type. */35using Nonce96 = ChaCha20::Nonce96;37/** Encrypt a message with a specified 96-bit nonce and aad.38*39* Requires cipher.size() = plain.size() + EXPANSION.40*/41void Encrypt(std::span<const std::byte> plain, std::span<const std::byte> aad, Nonce96 nonce, std::span<std::byte> cipher) noexcept42{43Encrypt(plain, {}, aad, nonce, cipher);44}46/** Encrypt a message (given split into plain1 + plain2) with a specified 96-bit nonce and aad.47*48* Requires cipher.size() = plain1.size() + plain2.size() + EXPANSION.49*/50void Encrypt(std::span<const std::byte> plain1, std::span<const std::byte> plain2, std::span<const std::byte> aad, Nonce96 nonce, std::span<std::byte> cipher) noexcept;52/** Decrypt a message with a specified 96-bit nonce and aad. Returns true if valid.53*54* Requires cipher.size() = plain.size() + EXPANSION.55*/56bool Decrypt(std::span<const std::byte> cipher, std::span<const std::byte> aad, Nonce96 nonce, std::span<std::byte> plain) noexcept57{58return Decrypt(cipher, aad, nonce, plain, {});59}61/** Decrypt a message with a specified 96-bit nonce and aad and split the result. Returns true if valid.62*63* Requires cipher.size() = plain1.size() + plain2.size() + EXPANSION.64*/65bool Decrypt(std::span<const std::byte> cipher, std::span<const std::byte> aad, Nonce96 nonce, std::span<std::byte> plain1, std::span<std::byte> plain2) noexcept;67/** Get a number of keystream bytes from the underlying stream cipher.68*69* This is equivalent to Encrypt() with plain set to that many zero bytes, and dropping the70* last EXPANSION bytes off the result.71*/72void Keystream(Nonce96 nonce, std::span<std::byte> keystream) noexcept;73};75/** Forward-secure wrapper around AEADChaCha20Poly1305.76*77* This implements an AEAD which automatically increments the nonce on every encryption or78* decryption, and cycles keys after a predetermined number of encryptions or decryptions.79*80* See BIP324 for details.81*/82class FSChaCha20Poly130583{84private:85/** Internal AEAD. */86AEADChaCha20Poly1305 m_aead;88/** Every how many iterations this cipher rekeys. */89const uint32_t m_rekey_interval;91/** The number of encryptions/decryptions since the last rekey. */92uint32_t m_packet_counter{0};94/** The number of rekeys performed so far. */95uint64_t m_rekey_counter{0};97/** Update counters (and if necessary, key) to transition to the next message. */98void NextPacket() noexcept;100public:101/** Length of keys expected by the constructor. */102static constexpr auto KEYLEN = AEADChaCha20Poly1305::KEYLEN;104/** Expansion when encrypting. */105static constexpr auto EXPANSION = AEADChaCha20Poly1305::EXPANSION;107// No copy or move to protect the secret.108FSChaCha20Poly1305(const FSChaCha20Poly1305&) = delete;109FSChaCha20Poly1305(FSChaCha20Poly1305&&) = delete;110FSChaCha20Poly1305& operator=(const FSChaCha20Poly1305&) = delete;111FSChaCha20Poly1305& operator=(FSChaCha20Poly1305&&) = delete;113/** Construct an FSChaCha20Poly1305 cipher that rekeys every rekey_interval operations. */114FSChaCha20Poly1305(std::span<const std::byte> key, uint32_t rekey_interval) noexcept :115m_aead(key), m_rekey_interval(rekey_interval) {}117/** Encrypt a message with a specified aad.118*119* Requires cipher.size() = plain.size() + EXPANSION.120*/121void Encrypt(std::span<const std::byte> plain, std::span<const std::byte> aad, std::span<std::byte> cipher) noexcept122{123Encrypt(plain, {}, aad, cipher);124}126/** Encrypt a message (given split into plain1 + plain2) with a specified aad.127*128* Requires cipher.size() = plain.size() + EXPANSION.129*/130void Encrypt(std::span<const std::byte> plain1, std::span<const std::byte> plain2, std::span<const std::byte> aad, std::span<std::byte> cipher) noexcept;132/** Decrypt a message with a specified aad. Returns true if valid.133*134* Requires cipher.size() = plain.size() + EXPANSION.135*/136bool Decrypt(std::span<const std::byte> cipher, std::span<const std::byte> aad, std::span<std::byte> plain) noexcept137{138return Decrypt(cipher, aad, plain, {});139}141/** Decrypt a message with a specified aad and split the result. Returns true if valid.142*143* Requires cipher.size() = plain1.size() + plain2.size() + EXPANSION.144*/145bool Decrypt(std::span<const std::byte> cipher, std::span<const std::byte> aad, std::span<std::byte> plain1, std::span<std::byte> plain2) noexcept;146};148#endif // BITCOIN_CRYPTO_CHACHA20POLY1305_H