Bitcoin Core  27.99.0
P2P Digital Currency
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
5 #ifndef BITCOIN_BIP324_H
6 #define BITCOIN_BIP324_H
8 #include <array>
9 #include <cstddef>
10 #include <optional>
12 #include <crypto/chacha20.h>
14 #include <key.h>
15 #include <pubkey.h>
16 #include <span.h>
20 {
21 public:
22  static constexpr unsigned SESSION_ID_LEN{32};
23  static constexpr unsigned GARBAGE_TERMINATOR_LEN{16};
24  static constexpr unsigned REKEY_INTERVAL{224};
25  static constexpr unsigned LENGTH_LEN{3};
26  static constexpr unsigned HEADER_LEN{1};
28  static constexpr std::byte IGNORE_BIT{0x80};
30 private:
31  std::optional<FSChaCha20> m_send_l_cipher;
32  std::optional<FSChaCha20> m_recv_l_cipher;
33  std::optional<FSChaCha20Poly1305> m_send_p_cipher;
34  std::optional<FSChaCha20Poly1305> m_recv_p_cipher;
39  std::array<std::byte, SESSION_ID_LEN> m_session_id;
40  std::array<std::byte, GARBAGE_TERMINATOR_LEN> m_send_garbage_terminator;
41  std::array<std::byte, GARBAGE_TERMINATOR_LEN> m_recv_garbage_terminator;
43 public:
45  BIP324Cipher() = delete;
48  BIP324Cipher(const CKey& key, Span<const std::byte> ent32) noexcept;
51  BIP324Cipher(const CKey& key, const EllSwiftPubKey& pubkey) noexcept;
54  const EllSwiftPubKey& GetOurPubKey() const noexcept { return m_our_pubkey; }
62  void Initialize(const EllSwiftPubKey& their_pubkey, bool initiator, bool self_decrypt = false) noexcept;
65  explicit operator bool() const noexcept { return m_send_l_cipher.has_value(); }
71  void Encrypt(Span<const std::byte> contents, Span<const std::byte> aad, bool ignore, Span<std::byte> output) noexcept;
77  unsigned DecryptLength(Span<const std::byte> input) noexcept;
84  bool Decrypt(Span<const std::byte> input, Span<const std::byte> aad, bool& ignore, Span<std::byte> contents) noexcept;
87  Span<const std::byte> GetSessionID() const noexcept { return m_session_id; }
94 };
96 #endif // BITCOIN_BIP324_H
The BIP324 packet cipher, encapsulating its key derivation, stream cipher, and AEAD.
Definition: bip324.h:20
Span< const std::byte > GetSendGarbageTerminator() const noexcept
Get the Garbage Terminator to send.
Definition: bip324.h:90
static constexpr unsigned REKEY_INTERVAL
Definition: bip324.h:24
Span< const std::byte > GetSessionID() const noexcept
Get the Session ID.
Definition: bip324.h:87
static constexpr unsigned GARBAGE_TERMINATOR_LEN
Definition: bip324.h:23
static constexpr unsigned HEADER_LEN
Definition: bip324.h:26
unsigned DecryptLength(Span< const std::byte > input) noexcept
Decrypt the length of a packet.
Definition: bip324.cpp:89
const EllSwiftPubKey & GetOurPubKey() const noexcept
Retrieve our public key.
Definition: bip324.h:54
EllSwiftPubKey m_our_pubkey
Definition: bip324.h:37
static constexpr std::byte IGNORE_BIT
Definition: bip324.h:28
No default constructor; keys must be provided to create a BIP324Cipher.
bool Decrypt(Span< const std::byte > input, Span< const std::byte > aad, bool &ignore, Span< std::byte > contents) noexcept
Decrypt a packet.
Definition: bip324.cpp:100
std::optional< FSChaCha20Poly1305 > m_recv_p_cipher
Definition: bip324.h:34
CKey m_key
Definition: bip324.h:36
std::array< std::byte, GARBAGE_TERMINATOR_LEN > m_recv_garbage_terminator
Definition: bip324.h:41
std::array< std::byte, SESSION_ID_LEN > m_session_id
Definition: bip324.h:39
std::array< std::byte, GARBAGE_TERMINATOR_LEN > m_send_garbage_terminator
Definition: bip324.h:40
void Encrypt(Span< const std::byte > contents, Span< const std::byte > aad, bool ignore, Span< std::byte > output) noexcept
Encrypt a packet.
Definition: bip324.cpp:73
std::optional< FSChaCha20 > m_recv_l_cipher
Definition: bip324.h:32
static constexpr unsigned LENGTH_LEN
Definition: bip324.h:25
static constexpr unsigned EXPANSION
Definition: bip324.h:27
void Initialize(const EllSwiftPubKey &their_pubkey, bool initiator, bool self_decrypt=false) noexcept
Initialize when the other side's public key is received.
Definition: bip324.cpp:34
std::optional< FSChaCha20Poly1305 > m_send_p_cipher
Definition: bip324.h:33
static constexpr unsigned SESSION_ID_LEN
Definition: bip324.h:22
Span< const std::byte > GetReceiveGarbageTerminator() const noexcept
Get the expected Garbage Terminator to receive.
Definition: bip324.h:93
std::optional< FSChaCha20 > m_send_l_cipher
Definition: bip324.h:31
An encapsulated private key.
Definition: key.h:33
static constexpr auto EXPANSION
Expansion when encrypting.
An ElligatorSwift-encoded public key.
Definition: pubkey.h:310