28 m_our_pubkey = m_key.EllSwiftCreate(ent32);
32 m_key(key), m_our_pubkey(pubkey) {}
38 std::string salt = std::string{
"bitcoin_v2_shared_secret"} + std::string(std::begin(message_header), std::end(message_header));
41 ECDHSecret ecdh_secret = m_key.ComputeBIP324ECDHSecret(their_pubkey, m_our_pubkey, initiator);
44 bool side = (initiator != self_decrypt);
46 std::array<std::byte, 32> hkdf_32_okm;
48 (side ? m_send_l_cipher : m_recv_l_cipher).emplace(hkdf_32_okm, REKEY_INTERVAL);
50 (side ? m_send_p_cipher : m_recv_p_cipher).emplace(hkdf_32_okm, REKEY_INTERVAL);
52 (side ? m_recv_l_cipher : m_send_l_cipher).emplace(hkdf_32_okm, REKEY_INTERVAL);
54 (side ? m_recv_p_cipher : m_send_p_cipher).emplace(hkdf_32_okm, REKEY_INTERVAL);
58 std::copy(std::begin(hkdf_32_okm), std::begin(hkdf_32_okm) + GARBAGE_TERMINATOR_LEN,
59 (initiator ? m_send_garbage_terminator : m_recv_garbage_terminator).begin());
60 std::copy(std::end(hkdf_32_okm) - GARBAGE_TERMINATOR_LEN, std::end(hkdf_32_okm),
61 (initiator ? m_recv_garbage_terminator : m_send_garbage_terminator).begin());
75 assert(output.size() == contents.size() + EXPANSION);
78 std::byte len[LENGTH_LEN];
79 len[0] = std::byte{(uint8_t)(contents.size() & 0xFF)};
80 len[1] = std::byte{(uint8_t)((contents.size() >> 8) & 0xFF)};
81 len[2] = std::byte{(uint8_t)((contents.size() >> 16) & 0xFF)};
82 m_send_l_cipher->Crypt(len, output.first(LENGTH_LEN));
85 std::byte header[HEADER_LEN] = {ignore ? IGNORE_BIT : std::byte{0}};
86 m_send_p_cipher->Encrypt(header, contents, aad, output.subspan(LENGTH_LEN));
91 assert(input.size() == LENGTH_LEN);
93 std::byte buf[LENGTH_LEN];
95 m_recv_l_cipher->Crypt(input, buf);
97 return uint32_t(buf[0]) + (uint32_t(buf[1]) << 8) + (uint32_t(buf[2]) << 16);
102 assert(input.size() + LENGTH_LEN == contents.size() + EXPANSION);
104 std::byte header[HEADER_LEN];
105 if (!m_recv_p_cipher->Decrypt(input, aad, header, contents))
return false;
107 ignore = (header[0] & IGNORE_BIT) == IGNORE_BIT;
const CChainParams & Params()
Return the currently selected parameters.
unsigned DecryptLength(Span< const std::byte > input) noexcept
Decrypt the length of a packet.
BIP324Cipher()=delete
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.
void Encrypt(Span< const std::byte > contents, Span< const std::byte > aad, bool ignore, Span< std::byte > output) noexcept
Encrypt a packet.
void Initialize(const EllSwiftPubKey &their_pubkey, bool initiator, bool self_decrypt=false) noexcept
Initialize when the other side's public key is received.
const MessageStartChars & MessageStart() const
A rfc5869 HKDF implementation with HMAC_SHA256 and fixed key output length of 32 bytes (L=32)
void Expand32(const std::string &info, unsigned char hash[OUTPUT_SIZE])
An encapsulated private key.
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
std::array< std::byte, ECDH_SECRET_SIZE > ECDHSecret
unsigned char * UCharCast(char *c)
An ElligatorSwift-encoded public key.