6#include <chainparams.h>
35 if (!init_key.
IsValid())
return;
41 if (!resp_key.
IsValid())
return;
75 bool ignore = mode & 1;
76 bool from_init = mode & 2;
77 bool damage = mode & 4;
78 unsigned aad_length_bits = 4 * ((mode >> 3) & 3);
80 unsigned length_bits = 2 * ((mode >> 5) & 7);
83 auto aad = rng.
randbytes<std::byte>(aad_length);
84 auto contents = rng.
randbytes<std::byte>(length);
87 auto& sender{from_init ? initiator : responder};
88 auto& receiver{from_init ? responder : initiator};
91 std::vector<std::byte> ciphertext(length + initiator.
EXPANSION);
92 sender.Encrypt(contents, aad, ignore, ciphertext);
98 (ciphertext.size() + aad.size()) * 8U - 1U);
99 unsigned damage_pos = damage_bit >> 3;
100 std::byte damage_val{(uint8_t)(1U << (damage_bit & 7))};
101 if (damage_pos >= ciphertext.size()) {
102 aad[damage_pos - ciphertext.size()] ^= damage_val;
104 ciphertext[damage_pos] ^= damage_val;
109 uint32_t dec_length = receiver.DecryptLength(
Span{ciphertext}.
first(initiator.
LENGTH_LEN));
111 assert(dec_length == length);
114 if (dec_length > 16384 + length)
break;
116 ciphertext.resize(dec_length + initiator.
EXPANSION);
120 std::vector<std::byte> decrypt(dec_length);
121 bool dec_ignore{
false};
122 bool ok = receiver.Decrypt(
Span{ciphertext}.
subspan(initiator.
LENGTH_LEN), aad, dec_ignore, decrypt);
126 assert(ignore == dec_ignore);
127 assert(decrypt == contents);
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
The BIP324 packet cipher, encapsulating its key derivation, stream cipher, and AEAD.
Span< const std::byte > GetReceiveGarbageTerminator() const noexcept
Get the expected Garbage Terminator to receive.
Span< const std::byte > GetSendGarbageTerminator() const noexcept
Get the Garbage Terminator to send.
const EllSwiftPubKey & GetOurPubKey() const noexcept
Retrieve our public key.
Span< const std::byte > GetSessionID() const noexcept
Get the Session ID.
static constexpr unsigned LENGTH_LEN
static constexpr unsigned EXPANSION
void Initialize(const EllSwiftPubKey &their_pubkey, bool initiator, bool self_decrypt=false) noexcept
Initialize when the other side's public key is received.
An encapsulated private key.
bool IsValid() const
Check whether this private key is valid.
RAII class initializing and deinitializing global state for elliptic curve support.
std::vector< T > ConsumeBytes(size_t num_bytes)
T ConsumeIntegralInRange(T min, T max)
std::vector< B > randbytes(size_t len) noexcept
Generate random bytes.
A Span is an object that can refer to a contiguous sequence of objects.
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
FUZZ_TARGET(bip324_cipher_roundtrip,.init=Initialize)
CKey ConsumePrivateKey(FuzzedDataProvider &fuzzed_data_provider, std::optional< bool > compressed) noexcept