14#ifndef OPENVPN_CRYPTO_CRYPTO_AEAD_H
15#define OPENVPN_CRYPTO_CRYPTO_AEAD_H
41template <
typename CRYPTO_API>
49 static_assert(4 + CRYPTO_API::CipherContextAEAD::IV_LEN ==
sizeof(
data),
50 "AEAD IV_LEN inconsistency");
68 constexpr size_t implicit_iv_len = 8;
69 if (sk.
size() < implicit_iv_len)
70 throw aead_error(
"insufficient key material for nonce tail");
74 std::memcpy(
data + implicit_iv_offset, sk.
data(), implicit_iv_len);
122 return pid_recv.
test_add(pid, now, stats_arg);
125 const unsigned char *
iv()
const
130 const unsigned char *
ad()
const
159 typename CRYPTO_API::CipherContextAEAD
impl;
167 typename CRYPTO_API::CipherContextAEAD
impl;
201 throw aead_error(
"encrypt work buffer too small");
206 unsigned char *auth_tag_tmp =
nullptr;
210 unsigned char *auth_tag =
e.
work.
prepend_alloc(CRYPTO_API::CipherContextAEAD::AUTH_TAG_LEN);
211 if (
e.
impl.requires_authtag_at_end())
213 auth_tag_tmp =
e.
work.
write_alloc(CRYPTO_API::CipherContextAEAD::AUTH_TAG_LEN);
222 std::memcpy(auth_tag, auth_tag_tmp, CRYPTO_API::CipherContextAEAD::AUTH_TAG_LEN);
224 e.
work.
inc_size(-CRYPTO_API::CipherContextAEAD::AUTH_TAG_LEN);
245 unsigned char *auth_tag =
nullptr;
247 auth_tag = buf.
read_alloc(CRYPTO_API::CipherContextAEAD::AUTH_TAG_LEN);
252 throw aead_error(
"decrypt work buffer too small");
254 if (auth_tag &&
e.
impl.requires_authtag_at_end())
256 unsigned char *auth_tag_end = buf.
write_alloc(CRYPTO_API::CipherContextAEAD::AUTH_TAG_LEN);
257 std::memcpy(auth_tag_end, auth_tag, CRYPTO_API::CipherContextAEAD::AUTH_TAG_LEN);
268 if (
e.
impl.requires_authtag_at_end())
298 clamp_to_default<unsigned int>(encrypt_key.size(), 0),
299 CRYPTO_API::CipherContextAEAD::ENCRYPT);
303 clamp_to_default<unsigned int>(decrypt_key.size(), 0),
304 CRYPTO_API::CipherContextAEAD::DECRYPT);
320 stats = recv_stats_arg;
355template <
typename CRYPTO_API>
391 return CRYPTO_API::CipherContextAEAD::AUTH_TAG_LEN;
CryptoContext(SSLLib::Ctx libctx_arg, CryptoDCSettingsData dc_settings_data, const Frame::Ptr &frame_arg, const SessionStats::Ptr &stats_arg)
size_t encap_overhead() const override
CryptoDCSettingsData crypto_info() override
CryptoDCSettingsData dc_settings
RCPtr< CryptoContext > Ptr
CryptoDCInstance::Ptr new_obj(const unsigned int key_id) override
size_t ad_len(const PacketIDDataSend &pid_send) const
void set_tail(const StaticKey &sk)
void prepend_ad(Buffer &buf, const PacketIDDataSend &pid_send) const
static constexpr std::size_t op32_size
const unsigned char * ad() const
static constexpr std::size_t data_offset_pkt_id
size_t ad_len(const PacketIDDataReceive &pid_recv) const
Nonce(const Nonce &ref, const PacketIDDataReceive &recv_pid, Buffer &buf, const unsigned char *op32)
bool verify_packet_id(PacketIDDataReceive &pid_recv, const PacketIDControl::time_t now, const SessionStats::Ptr &stats_arg)
const unsigned char * iv() const
Nonce(const Nonce &ref, PacketIDDataSend &pid_send, const unsigned char *op32)
void init_pid(const char *recv_name, const int recv_unit, const SessionStats::Ptr &recv_stats_arg) override
bool encrypt(BufferAllocated &buf, const unsigned char *op32) override
void init_cipher(StaticKey &&encrypt_key, StaticKey &&decrypt_key) override
unsigned int defined() const override
Error::Type decrypt(BufferAllocated &buf, const std::time_t now, const unsigned char *op32) override
void rekey(const typename Base::RekeyType type) override
void init_hmac(StaticKey &&encrypt_key, StaticKey &&decrypt_key) override
Crypto(SSLLib::Ctx libctx_arg, CryptoDCSettingsData dc_settings_data, const Frame::Ptr &frame_arg, const SessionStats::Ptr &stats_arg)
bool consider_compression(const CompressContext &comp_ctx) override
CryptoDCSettingsData dc_settings
void swap(BufferAllocatedType< T_ > &other)
Swaps the contents of this BufferAllocatedType object with another BufferAllocatedType object.
const T * c_data() const
Returns a const pointer to the start of the buffer.
T * prepend_alloc(const size_t size)
Allocate space for prepending data to the buffer.
void inc_size(const size_t delta)
Increment the size of the array (usually used in a similar context to set_size such as after mutable_...
size_t max_size() const
Return the maximum allowable size value in T objects given the current offset (without considering re...
T * write_alloc(const size_t size)
Allocate space for writing data to the buffer.
void prepend(const T *data, const size_t size)
Prepend data to the buffer.
size_t size() const
Returns the size of the buffer in T objects.
T * data()
Get a mutable pointer to the start of the array.
auto * read_alloc(const size_t size)
Allocate memory and read data from the buffer into the allocated memory.
void set_size(const size_t size)
After an external method, operating on the array as a mutable unsigned char buffer,...
void reset_size()
Resets the size of the buffer to zero.
void read(NCT *data, const size_t size)
Read data from the buffer into the specified memory location.
CryptoAlgs::KeyDerivation key_derivation
void set_digest(CryptoAlgs::Type digest)
CryptoAlgs::Type cipher() const
size_t prepare(const unsigned int context, Buffer &buf) const
PacketIDData read_next(Buffer &buf) const
void init(const char *name_arg, const int unit_arg, bool wide_arg)
std::size_t constexpr length() const
bool test_add(const PacketIDData &pin, const Time::base_type now, const SessionStats::Ptr &stats)
constexpr std::size_t length() const
bool wrap_warning() const
void write_next(Buffer &buf)
const unsigned char * data() const
#define OPENVPN_EXCEPTION(C)
bool defined(const Type type)
OutT clamp_to_default(InT inVal, OutT defVal)
Adjusts the input value to the default if the input value exceeds the range of the output type.
CRYPTO_API::CipherContextAEAD impl
PacketIDDataReceive pid_recv
CRYPTO_API::CipherContextAEAD impl
PacketIDDataSend pid_send
static constexpr std::size_t long_id_size