Bitcoin ABC 0.32.4
P2P Digital Currency
chacha20poly1305.h
Go to the documentation of this file.
1// Copyright (c) 2023 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_CRYPTO_CHACHA20POLY1305_H
6#define BITCOIN_CRYPTO_CHACHA20POLY1305_H
7
8#include <crypto/chacha20.h>
9#include <crypto/poly1305.h>
10#include <span.h>
11
12#include <cstddef>
13#include <cstdint>
14#include <cstdlib>
15
23
24public:
26 static constexpr unsigned KEYLEN = 32;
27
29 static constexpr unsigned EXPANSION = Poly1305::TAGLEN;
30
33
35 void SetKey(Span<const std::byte> key) noexcept;
36
39
46 Nonce96 nonce, Span<std::byte> cipher) noexcept {
47 Encrypt(plain, {}, aad, nonce, cipher);
48 }
49
58 Span<std::byte> cipher) noexcept;
59
67 Nonce96 nonce, Span<std::byte> plain) noexcept {
68 return Decrypt(cipher, aad, nonce, plain, {});
69 }
70
78 Nonce96 nonce, Span<std::byte> plain1,
79 Span<std::byte> plain2) noexcept;
80
87 void Keystream(Nonce96 nonce, Span<std::byte> keystream) noexcept;
88};
89
100private:
103
105 const uint32_t m_rekey_interval;
106
108 uint32_t m_packet_counter{0};
109
111 uint64_t m_rekey_counter{0};
112
117 void NextPacket() noexcept;
118
119public:
121 static constexpr auto KEYLEN = AEADChaCha20Poly1305::KEYLEN;
122
124 static constexpr auto EXPANSION = AEADChaCha20Poly1305::EXPANSION;
125
126 // No copy or move to protect the secret.
129 FSChaCha20Poly1305 &operator=(const FSChaCha20Poly1305 &) = delete;
130 FSChaCha20Poly1305 &operator=(FSChaCha20Poly1305 &&) = delete;
131
136 FSChaCha20Poly1305(Span<const std::byte> key,
137 uint32_t rekey_interval) noexcept
138 : m_aead(key), m_rekey_interval(rekey_interval) {}
139
146 Span<std::byte> cipher) noexcept {
147 Encrypt(plain, {}, aad, cipher);
148 }
149
157 Span<const std::byte> aad, Span<std::byte> cipher) noexcept;
158
165 Span<std::byte> plain) noexcept {
166 return Decrypt(cipher, aad, plain, {});
167 }
168
176 Span<std::byte> plain1, Span<std::byte> plain2) noexcept;
177};
178
179#endif // BITCOIN_CRYPTO_CHACHA20POLY1305_H
The AEAD_CHACHA20_POLY1305 authenticated encryption algorithm from RFC8439 section 2....
AEADChaCha20Poly1305(Span< const std::byte > key) noexcept
Initialize an AEAD instance with a specified 32-byte key.
ChaCha20::Nonce96 Nonce96
96-bit nonce type.
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.
static constexpr unsigned EXPANSION
Expansion when encrypting.
void SetKey(Span< const std::byte > key) noexcept
Switch to another 32-byte key.
static constexpr unsigned KEYLEN
Expected size of key argument in constructor.
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 Keystream(Nonce96 nonce, Span< std::byte > keystream) noexcept
Get a number of keystream bytes from the underlying stream cipher.
ChaCha20 m_chacha20
Internal stream cipher.
Unrestricted ChaCha20 cipher.
Definition: chacha20.h:89
ChaCha20Aligned::Nonce96 Nonce96
96-bit nonce type.
Definition: chacha20.h:112
Forward-secure wrapper around AEADChaCha20Poly1305.
void NextPacket() noexcept
Update counters (and if necessary, key) to transition to the next message.
const uint32_t m_rekey_interval
Every how many iterations this cipher rekeys.
bool Decrypt(Span< const std::byte > cipher, Span< const std::byte > aad, Span< std::byte > plain) noexcept
Decrypt a message with a specified aad.
uint32_t m_packet_counter
The number of encryptions/decryptions since the last rekey.
AEADChaCha20Poly1305 m_aead
Internal AEAD.
static constexpr auto KEYLEN
Length of keys expected by the constructor.
uint64_t m_rekey_counter
The number of rekeys performed so far.
static constexpr auto EXPANSION
Expansion when encrypting.
void Encrypt(Span< const std::byte > plain, Span< const std::byte > aad, Span< std::byte > cipher) noexcept
Encrypt a message with a specified aad.
static constexpr unsigned TAGLEN
Length of the output produced by Finalize().
Definition: poly1305.h:43
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:94
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259