5 #if defined(HAVE_CONFIG_H) 22 assert(key.size() == KEYLEN);
27 assert(key.size() == KEYLEN);
28 m_chacha20.SetKey(key);
33 #ifndef HAVE_TIMINGSAFE_BCMP 34 #define HAVE_TIMINGSAFE_BCMP 36 int timingsafe_bcmp(
const unsigned char* b1,
const unsigned char* b2,
size_t n) noexcept
38 const unsigned char *p1 = b1, *p2 = b2;
50 static const std::byte PADDING[16] = {{}};
54 chacha20.Keystream(first_block);
61 const unsigned aad_padding_length = (16 - (aad.size() % 16)) % 16;
62 poly1305.Update(aad).Update(
Span{PADDING}.
first(aad_padding_length));
64 const unsigned cipher_padding_length = (16 - (cipher.size() % 16)) % 16;
65 poly1305.Update(cipher).Update(
Span{PADDING}.
first(cipher_padding_length));
70 poly1305.Update(length_desc);
73 poly1305.Finalize(tag);
80 assert(cipher.size() == plain1.size() + plain2.size() + EXPANSION);
83 m_chacha20.Seek(
nonce, 1);
84 m_chacha20.Crypt(plain1, cipher.first(plain1.size()));
85 m_chacha20.Crypt(plain2, cipher.subspan(plain1.size()).first(plain2.size()));
88 m_chacha20.Seek(
nonce, 0);
89 ComputeTag(m_chacha20, aad, cipher.first(cipher.size() - EXPANSION), cipher.last(EXPANSION));
94 assert(cipher.size() == plain1.size() + plain2.size() + EXPANSION);
97 m_chacha20.Seek(
nonce, 0);
98 std::byte expected_tag[EXPANSION];
99 ComputeTag(m_chacha20, aad, cipher.first(cipher.size() - EXPANSION), expected_tag);
100 if (timingsafe_bcmp(
UCharCast(expected_tag),
UCharCast(cipher.last(EXPANSION).data()), EXPANSION))
return false;
103 m_chacha20.Crypt(cipher.first(plain1.size()), plain1);
104 m_chacha20.Crypt(cipher.subspan(plain1.size()).first(plain2.size()), plain2);
111 m_chacha20.Seek(
nonce, 1);
112 m_chacha20.Keystream(keystream);
135 m_aead.Encrypt(plain1, plain2, aad, {m_packet_counter, m_rekey_counter}, cipher);
141 bool ret = m_aead.Decrypt(cipher, aad, {m_packet_counter, m_rekey_counter}, plain1, plain2);
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
uint64_t m_rekey_counter
The number of rekeys performed so far.
void Encrypt(Span< const std::byte > plain, Span< const std::byte > aad, Span< std::byte > cipher) noexcept
Encrypt a message with a specified aad.
uint32_t m_packet_counter
The number of encryptions/decryptions since the last rekey.
AEADChaCha20Poly1305(Span< const std::byte > key) noexcept
Initialize an AEAD instance with a specified 32-byte key.
static constexpr unsigned BLOCKLEN
Block size (inputs/outputs to Keystream / Crypt should be multiples of this).
bool Decrypt(Span< const std::byte > cipher, Span< const std::byte > aad, Nonce96 nonce, Span< std::byte > plain) noexcept
Decrypt a message with a specified 96-bit nonce and aad.
void Encrypt(Span< const std::byte > plain, Span< const std::byte > aad, Nonce96 nonce, Span< std::byte > cipher) noexcept
Encrypt a message with a specified 96-bit nonce and aad.
AEADChaCha20Poly1305 m_aead
Internal AEAD.
const uint32_t m_rekey_interval
Every how many iterations this cipher rekeys.
Unrestricted ChaCha20 cipher.
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
static constexpr unsigned KEYLEN
Length of the keys expected by the constructor.
void NextPacket() noexcept
Update counters (and if necessary, key) to transition to the next message.
void SetKey(Span< const std::byte > key) noexcept
Switch to another 32-byte key.
C++ wrapper with std::byte Span interface around poly1305_donna code.
void Keystream(Nonce96 nonce, Span< std::byte > keystream) noexcept
Get a number of keystream bytes from the underlying stream cipher.
ChaCha20::Nonce96 Nonce96
96-bit nonce type.
static constexpr auto KEYLEN
Length of keys expected by the constructor.
unsigned char * UCharCast(char *c)
static constexpr unsigned TAGLEN
Length of the output produced by Finalize().
static void WriteLE64(unsigned char *ptr, uint64_t x)
bool Decrypt(Span< const std::byte > cipher, Span< const std::byte > aad, Span< std::byte > plain) noexcept
Decrypt a message with a specified aad.