9#include <botan/internal/chacha20poly1305.h>
11#include <botan/internal/ct_utils.h>
12#include <botan/internal/loadstor.h>
24 return (n == 8 || n == 12 || n == 24);
48 return m_chacha->has_keying_material();
51void ChaCha20Poly1305_Mode::key_schedule(std::span<const uint8_t> key) {
56 BOTAN_ARG_CHECK(idx == 0,
"ChaCha20Poly1305: cannot handle non-zero index in set_associated_data_n");
58 throw Invalid_State(
"Cannot set AD for ChaCha20Poly1305 while processing a message");
60 m_ad.assign(ad.begin(), ad.end());
64 uint8_t len8[8] = {0};
65 store_le(
static_cast<uint64_t
>(len), len8);
69void ChaCha20Poly1305_Mode::start_msg(
const uint8_t nonce[],
size_t nonce_len) {
79 uint8_t first_block[64];
80 m_chacha->write_keystream(first_block,
sizeof(first_block));
89 if(
m_ad.size() % 16) {
90 const uint8_t zeros[16] = {0};
98size_t ChaCha20Poly1305_Encryption::process_msg(uint8_t buf[],
size_t sz) {
109 const uint8_t zeros[16] = {0};
116 buffer.resize(buffer.size() +
tag_size());
122size_t ChaCha20Poly1305_Decryption::process_msg(uint8_t buf[],
size_t sz) {
131 const size_t sz = buffer.size() - offset;
132 uint8_t* buf = buffer.data() + offset;
136 const size_t remaining = sz -
tag_size();
146 const uint8_t zeros[16] = {0};
157 const uint8_t* included_tag = &buf[remaining];
163 throw Invalid_Authentication_Tag(
"ChaCha20Poly1305 tag check failed");
165 buffer.resize(offset + remaining);
#define BOTAN_ARG_CHECK(expr, msg)
bool has_keying_material() const final
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) final
bool valid_nonce_length(size_t n) const override
secure_vector< uint8_t > m_ad
size_t ideal_granularity() const override
std::string name() const override
std::unique_ptr< StreamCipher > m_chacha
size_t update_granularity() const override
bool cfrg_version() const
size_t tag_size() const override
void update_len(size_t len)
std::unique_ptr< MessageAuthenticationCode > m_poly1305
int(* update)(CTX *, const void *, CC_LONG len)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
void secure_scrub_memory(void *ptr, size_t n)
constexpr auto store_le(ParamTs &&... params)
std::vector< T, secure_allocator< T > > secure_vector