6 #include <chainparams.h> 35 if (!init_key.IsValid())
return;
37 auto init_ent = provider.ConsumeBytes<std::byte>(32);
41 if (!resp_key.
IsValid())
return;
43 auto resp_ent = provider.ConsumeBytes<std::byte>(32);
74 unsigned mode = provider.ConsumeIntegral<uint8_t>();
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);
79 unsigned aad_length = provider.ConsumeIntegralInRange<
unsigned>(0, (1 << aad_length_bits) - 1);
80 unsigned length_bits = 2 * ((mode >> 5) & 7);
81 unsigned length = provider.ConsumeIntegralInRange<
unsigned>(0, (1 << length_bits) - 1);
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);
97 unsigned damage_bit = provider.ConsumeIntegralInRange<
unsigned>(0,
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);
std::vector< B > randbytes(size_t len) noexcept
Generate random bytes.
Span< const std::byte > GetSendGarbageTerminator() const noexcept
Get the Garbage Terminator to send.
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
RAII class initializing and deinitializing global state for elliptic curve support.
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
void Initialize(const EllSwiftPubKey &their_pubkey, bool initiator, bool self_decrypt=false) noexcept
Initialize when the other side's public key is received.
The BIP324 packet cipher, encapsulating its key derivation, stream cipher, and AEAD.
Span< const std::byte > GetSessionID() const noexcept
Get the Session ID.
const EllSwiftPubKey & GetOurPubKey() const noexcept
Retrieve our public key.
FUZZ_TARGET(bip324_cipher_roundtrip,.init=Initialize)
An encapsulated private key.
A Span is an object that can refer to a contiguous sequence of objects.
Span< const std::byte > GetReceiveGarbageTerminator() const noexcept
Get the expected Garbage Terminator to receive.
static constexpr unsigned EXPANSION
static constexpr unsigned LENGTH_LEN
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
CKey ConsumePrivateKey(FuzzedDataProvider &fuzzed_data_provider, std::optional< bool > compressed) noexcept
bool IsValid() const
Check whether this private key is valid.