34 if (!init_key.
IsValid())
return;
40 if (!resp_key.
IsValid())
return;
74 bool ignore = mode & 1;
75 bool from_init = mode & 2;
76 bool damage = mode & 4;
77 unsigned aad_length_bits = 4 * ((mode >> 3) & 3);
79 unsigned length_bits = 2 * ((mode >> 5) & 7);
82 auto aad = rng.
randbytes<std::byte>(aad_length);
83 auto contents = rng.
randbytes<std::byte>(length);
86 auto& sender{from_init ? initiator : responder};
87 auto& receiver{from_init ? responder : initiator};
90 std::vector<std::byte> ciphertext(length + initiator.
EXPANSION);
91 sender.Encrypt(contents, aad, ignore, ciphertext);
97 (ciphertext.size() + aad.size()) * 8U - 1U);
98 unsigned damage_pos = damage_bit >> 3;
99 std::byte damage_val{(uint8_t)(1U << (damage_bit & 7))};
100 if (damage_pos >= ciphertext.size()) {
101 aad[damage_pos - ciphertext.size()] ^= damage_val;
103 ciphertext[damage_pos] ^= damage_val;
108 uint32_t dec_length = receiver.DecryptLength(
Span{ciphertext}.
first(initiator.
LENGTH_LEN));
110 assert(dec_length == length);
113 if (dec_length > 16384 + length)
break;
115 ciphertext.resize(dec_length + initiator.
EXPANSION);
119 std::vector<std::byte> decrypt(dec_length);
120 bool dec_ignore{
false};
121 bool ok = receiver.Decrypt(
Span{ciphertext}.
subspan(initiator.
LENGTH_LEN), aad, dec_ignore, decrypt);
125 assert(ignore == dec_ignore);
126 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.