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(std::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(std::span{ciphertext}.subspan(initiator.
LENGTH_LEN), aad, dec_ignore, decrypt);
126 assert(ignore == dec_ignore);
127 assert(decrypt == contents);
void Initialize(const EllSwiftPubKey &their_pubkey, bool initiator, bool self_decrypt=false) noexcept
Initialize when the other side's public key is received.
RAII class initializing and deinitializing global state for elliptic curve support.