9#include <botan/internal/chacha20poly1305.h>
11#include <botan/exceptn.h>
12#include <botan/internal/ct_utils.h>
13#include <botan/internal/loadstor.h>
25 return (n == 8 || n == 12 || n == 24);
49 return m_chacha->has_keying_material();
52void ChaCha20Poly1305_Mode::key_schedule(std::span<const uint8_t> key) {
57 BOTAN_ARG_CHECK(idx == 0,
"ChaCha20Poly1305: cannot handle non-zero index in set_associated_data_n");
59 throw Invalid_State(
"Cannot set AD for ChaCha20Poly1305 while processing a message");
61 m_ad.assign(ad.begin(), ad.end());
65 uint8_t len8[8] = {0};
66 store_le(
static_cast<uint64_t
>(len), len8);
70void ChaCha20Poly1305_Mode::start_msg(
const uint8_t nonce[],
size_t nonce_len) {
80 uint8_t first_block[64];
81 m_chacha->write_keystream(first_block,
sizeof(first_block));
90 if(
m_ad.size() % 16 != 0) {
91 const uint8_t zeros[16] = {0};
99size_t ChaCha20Poly1305_Encryption::process_msg(uint8_t buf[],
size_t sz) {
110 const uint8_t zeros[16] = {0};
117 buffer.resize(buffer.size() +
tag_size());
123size_t ChaCha20Poly1305_Decryption::process_msg(uint8_t buf[],
size_t sz) {
132 const size_t sz = buffer.size() - offset;
133 uint8_t* buf = buffer.data() + offset;
137 const size_t remaining = sz -
tag_size();
147 const uint8_t zeros[16] = {0};
158 const uint8_t* included_tag = &buf[remaining];
164 throw Invalid_Authentication_Tag(
"ChaCha20Poly1305 tag check failed");
166 buffer.resize(offset + remaining);
#define BOTAN_ARG_CHECK(expr, msg)
static std::unique_ptr< AEAD_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
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
void update(T &buffer, size_t offset=0)
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