Bitcoin Core  31.0.0
P2P Digital Currency
chacha20poly1305.h
Go to the documentation of this file.
1 // Copyright (c) 2023-present 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 <cstdint>
10 #include <span>
11 
12 #include <crypto/chacha20.h>
13 #include <crypto/poly1305.h>
14 
17 {
20 
21 public:
23  static constexpr unsigned KEYLEN = 32;
24 
26  static constexpr unsigned EXPANSION = Poly1305::TAGLEN;
27 
29  AEADChaCha20Poly1305(std::span<const std::byte> key) noexcept;
30 
32  void SetKey(std::span<const std::byte> key) noexcept;
33 
36 
41  void Encrypt(std::span<const std::byte> plain, std::span<const std::byte> aad, Nonce96 nonce, std::span<std::byte> cipher) noexcept
42  {
43  Encrypt(plain, {}, aad, nonce, cipher);
44  }
45 
50  void 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;
51 
56  bool Decrypt(std::span<const std::byte> cipher, std::span<const std::byte> aad, Nonce96 nonce, std::span<std::byte> plain) noexcept
57  {
58  return Decrypt(cipher, aad, nonce, plain, {});
59  }
60 
65  bool 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;
66 
72  void Keystream(Nonce96 nonce, std::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.
108  FSChaCha20Poly1305(const FSChaCha20Poly1305&) = delete;
110  FSChaCha20Poly1305& operator=(const FSChaCha20Poly1305&) = delete;
111  FSChaCha20Poly1305& operator=(FSChaCha20Poly1305&&) = delete;
112 
114  FSChaCha20Poly1305(std::span<const std::byte> key, uint32_t rekey_interval) noexcept :
115  m_aead(key), m_rekey_interval(rekey_interval) {}
116 
121  void Encrypt(std::span<const std::byte> plain, std::span<const std::byte> aad, std::span<std::byte> cipher) noexcept
122  {
123  Encrypt(plain, {}, aad, cipher);
124  }
125 
130  void 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;
131 
136  bool Decrypt(std::span<const std::byte> cipher, std::span<const std::byte> aad, std::span<std::byte> plain) noexcept
137  {
138  return Decrypt(cipher, aad, plain, {});
139  }
140 
145  bool 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 };
147 
148 #endif // BITCOIN_CRYPTO_CHACHA20POLY1305_H
The AEAD_CHACHA20_POLY1305 authenticated encryption algorithm from RFC8439 section 2...
uint64_t m_rekey_counter
The number of rekeys performed so far.
unsigned int nonce
Definition: miner_tests.cpp:82
void Keystream(Nonce96 nonce, std::span< std::byte > keystream) noexcept
Get a number of keystream bytes from the underlying stream cipher.
void Encrypt(std::span< const std::byte > plain, std::span< const std::byte > aad, std::span< std::byte > cipher) noexcept
Encrypt a message with a specified aad.
Forward-secure wrapper around AEADChaCha20Poly1305.
uint32_t m_packet_counter
The number of encryptions/decryptions since the last rekey.
Definition: common.h:29
AEADChaCha20Poly1305(std::span< const std::byte > key) noexcept
Initialize an AEAD instance with a specified 32-byte key.
ChaCha20Aligned::Nonce96 Nonce96
96-bit nonce type.
Definition: chacha20.h:99
AEADChaCha20Poly1305 m_aead
Internal AEAD.
void SetKey(std::span< const std::byte > key) noexcept
Switch to another 32-byte key.
const uint32_t m_rekey_interval
Every how many iterations this cipher rekeys.
static constexpr unsigned EXPANSION
Expansion when encrypting.
static constexpr unsigned KEYLEN
Expected size of key argument in constructor.
Unrestricted ChaCha20 cipher.
Definition: chacha20.h:75
void NextPacket() noexcept
Update counters (and if necessary, key) to transition to the next message.
void Encrypt(std::span< const std::byte > plain, std::span< const std::byte > aad, Nonce96 nonce, std::span< std::byte > cipher) noexcept
Encrypt a message with a specified 96-bit nonce and aad.
ChaCha20 m_chacha20
Internal stream cipher.
bool Decrypt(std::span< const std::byte > cipher, std::span< const std::byte > aad, std::span< std::byte > plain) noexcept
Decrypt a message with a specified aad.
ChaCha20::Nonce96 Nonce96
96-bit nonce type.
bool Decrypt(std::span< const std::byte > cipher, std::span< const std::byte > aad, Nonce96 nonce, std::span< std::byte > plain) noexcept
Decrypt a message with a specified 96-bit nonce and aad.
static constexpr auto EXPANSION
Expansion when encrypting.
static constexpr auto KEYLEN
Length of keys expected by the constructor.
static constexpr unsigned TAGLEN
Length of the output produced by Finalize().
Definition: poly1305.h:44