5#ifndef BITCOIN_CRYPTO_CHACHA20_H
6#define BITCOIN_CRYPTO_CHACHA20_H
30 static constexpr unsigned KEYLEN{32};
79 std::array<std::byte, ChaCha20Aligned::BLOCKLEN>
m_buffer;
96 void SetKey(std::span<const std::byte> key)
noexcept;
112 void Crypt(std::span<const std::byte> in_bytes, std::span<std::byte> out_bytes)
noexcept;
115 void Keystream(std::span<std::byte> out)
noexcept;
151 FSChaCha20(std::span<const std::byte> key, uint32_t rekey_interval)
noexcept;
154 void Crypt(std::span<const std::byte> input, std::span<std::byte> output)
noexcept;
ChaCha20 cipher that only operates on multiples of 64 bytes.
void Keystream(std::span< std::byte > out) noexcept
outputs the keystream into out, whose length must be a multiple of BLOCKLEN.
void Crypt(std::span< const std::byte > input, std::span< std::byte > output) noexcept
en/deciphers the message <input> and write the result into <output>
ChaCha20Aligned() noexcept=delete
For safety, disallow initialization without key.
std::pair< uint32_t, uint64_t > Nonce96
Type for 96-bit nonces used by the Set function below.
void Seek(Nonce96 nonce, uint32_t block_counter) noexcept
Set the 96-bit nonce and 32-bit block counter.
static constexpr unsigned BLOCKLEN
Block size (inputs/outputs to Keystream / Crypt should be multiples of this).
static constexpr unsigned KEYLEN
Expected key length in constructor and SetKey.
void SetKey(std::span< const std::byte > key) noexcept
Set 32-byte key, and seek to nonce 0 and block position 0.
Unrestricted ChaCha20 cipher.
ChaCha20() noexcept=delete
For safety, disallow initialization without key.
static constexpr unsigned KEYLEN
Expected key length in constructor and SetKey.
std::array< std::byte, ChaCha20Aligned::BLOCKLEN > m_buffer
void Seek(Nonce96 nonce, uint32_t block_counter) noexcept
Set the 96-bit nonce and 32-bit block counter.
ChaCha20Aligned m_aligned
ChaCha20Aligned::Nonce96 Nonce96
96-bit nonce type.
ChaCha20 m_chacha20
Internal stream cipher.
FSChaCha20 & operator=(const FSChaCha20 &)=delete
uint32_t m_chunk_counter
The number of encryptions/decryptions since the last rekey.
FSChaCha20(FSChaCha20 &&)=delete
static constexpr unsigned KEYLEN
Length of keys expected by the constructor.
const uint32_t m_rekey_interval
The number of encryptions/decryptions before a rekey happens.
FSChaCha20 & operator=(FSChaCha20 &&)=delete
FSChaCha20(const FSChaCha20 &)=delete
void Crypt(std::span< const std::byte > input, std::span< std::byte > output) noexcept
Encrypt or decrypt a chunk.
uint64_t m_rekey_counter
The number of rekey operations that have happened.