Bitcoin Core  26.99.0
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 <cstddef>
9 #include <stdint.h>
10 
11 #include <crypto/chacha20.h>
12 #include <crypto/poly1305.h>
13 #include <span.h>
14 
17 {
20 
21 public:
23  static constexpr unsigned KEYLEN = 32;
24 
26  static constexpr unsigned EXPANSION = Poly1305::TAGLEN;
27 
30 
32  void SetKey(Span<const std::byte> key) noexcept;
33 
36 
42  {
43  Encrypt(plain, {}, aad, nonce, cipher);
44  }
45 
51 
57  {
58  return Decrypt(cipher, aad, nonce, plain, {});
59  }
60 
66 
72  void Keystream(Nonce96 nonce, Span<std::byte> keystream) noexcept;
73 };
74 
83 {
84 private:
87 
89  const uint32_t m_rekey_interval;
90 
92  uint32_t m_packet_counter{0};
93 
95  uint64_t m_rekey_counter{0};
96 
98  void NextPacket() noexcept;
99 
100 public:
102  static constexpr auto KEYLEN = AEADChaCha20Poly1305::KEYLEN;
103 
105  static constexpr auto EXPANSION = AEADChaCha20Poly1305::EXPANSION;
106 
107  // No copy or move to protect the secret.
110  FSChaCha20Poly1305& operator=(const FSChaCha20Poly1305&) = delete;
111  FSChaCha20Poly1305& operator=(FSChaCha20Poly1305&&) = delete;
112 
114  FSChaCha20Poly1305(Span<const std::byte> key, uint32_t rekey_interval) noexcept :
115  m_aead(key), m_rekey_interval(rekey_interval) {}
116 
122  {
123  Encrypt(plain, {}, aad, cipher);
124  }
125 
131 
137  {
138  return Decrypt(cipher, aad, plain, {});
139  }
140 
145  bool Decrypt(Span<const std::byte> cipher, Span<const std::byte> aad, Span<std::byte> plain1, Span<std::byte> plain2) noexcept;
146 };
147 
148 #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:78
ChaCha20Aligned::Nonce96 Nonce96
96-bit nonce type.
Definition: chacha20.h:101
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
unsigned int nonce
Definition: miner_tests.cpp:71