9#include <botan/internal/siv.h>
11#include <botan/block_cipher.h>
12#include <botan/internal/cmac.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/ctr.h>
15#include <botan/internal/poly_dbl.h>
20 m_name(cipher->
name() +
"/SIV"),
21 m_bs(cipher->block_size()),
22 m_ctr(std::make_unique<
CTR_BE>(cipher->new_object(), 8)),
23 m_mac(std::make_unique<
CMAC>(std::move(cipher))) {
66 return m_mac->key_spec().multiple(2);
70 return m_ctr->has_keying_material() && m_mac->has_keying_material();
73void SIV_Mode::key_schedule(std::span<const uint8_t> key) {
74 const size_t keylen = key.size() / 2;
75 m_mac->set_key(key.first(keylen));
76 m_ctr->set_key(key.last(keylen));
90 if(n >= m_ad_macs.size()) {
91 m_ad_macs.resize(n + 1);
94 m_ad_macs[n] = m_mac->process(ad);
97void SIV_Mode::start_msg(
const uint8_t nonce[],
size_t nonce_len) {
103 m_nonce = m_mac->process(nonce, nonce_len);
111size_t SIV_Mode::process_msg(uint8_t buf[],
size_t sz) {
113 m_msg_buf.insert(m_msg_buf.end(), buf, buf + sz);
118 const std::vector<uint8_t> zeros(
block_size());
122 for(
size_t i = 0; i != m_ad_macs.size(); ++i) {
127 if(!m_nonce.empty()) {
134 xor_buf(V.data(), text, text_len);
136 return m_mac->process(V);
143 return m_mac->final();
156 buffer.insert(buffer.begin() + offset,
msg_buf().begin(),
msg_buf().end());
161 buffer.insert(buffer.begin() + offset, V.begin(), V.end());
163 if(buffer.size() != offset + V.size()) {
165 ctr().
cipher1(&buffer[offset + V.size()], buffer.size() - offset - V.size());
173 buffer.insert(buffer.begin() + offset,
msg_buf().begin(),
msg_buf().end());
177 const size_t sz = buffer.size() - offset;
183 if(buffer.size() != offset + V.size()) {
186 ctr().
cipher(buffer.data() + offset + V.size(), buffer.data() + offset, buffer.size() - offset - V.size());
192 throw Invalid_Authentication_Tag(
"SIV tag check failed");
195 buffer.resize(buffer.size() -
tag_size());
#define BOTAN_ARG_CHECK(expr, msg)
size_t block_size() const
SIV_Mode(std::unique_ptr< BlockCipher > cipher)
void set_ctr_iv(secure_vector< uint8_t > V)
secure_vector< uint8_t > S2V(const uint8_t text[], size_t text_len)
void clear() override final
void reset() override final
bool has_keying_material() const override final
size_t update_granularity() const override final
std::string name() const override final
size_t ideal_granularity() const override final
secure_vector< uint8_t > & msg_buf()
size_t tag_size() const override final
Key_Length_Specification key_spec() const override final
bool valid_nonce_length(size_t) const override final
size_t maximum_associated_data_inputs() const override final
bool requires_entire_message() const override final
void set_associated_data_n(size_t n, std::span< const uint8_t > ad) override final
void cipher1(uint8_t buf[], size_t len)
void set_iv(const uint8_t iv[], size_t iv_len)
void cipher(const uint8_t in[], uint8_t out[], size_t len)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
std::vector< T, secure_allocator< T > > secure_vector
void poly_double_n(uint8_t out[], const uint8_t in[], size_t n)