8#include <botan/internal/cmac.h>
10#include <botan/exceptn.h>
11#include <botan/internal/fmt.h>
12#include <botan/internal/poly_dbl.h>
13#include <botan/internal/stl_util.h>
20void CMAC::add_data(std::span<const uint8_t> input) {
23 const size_t initial_fill = std::min(m_buffer.size() - m_position, input.size());
24 copy_mem(m_buffer.data() + m_position, input.data(), initial_fill);
26 if(m_position + input.size() > bs) {
28 m_cipher->encrypt(m_state);
30 BufferSlicer in(input);
31 in.skip(bs - m_position);
32 while(in.remaining() > bs) {
33 xor_buf(m_state, in.take(bs), bs);
34 m_cipher->encrypt(m_state);
37 const auto remaining = in.take(in.remaining());
38 copy_mem(m_buffer.data(), remaining.data(), remaining.size());
39 m_position = remaining.size();
41 m_position += input.size();
48void CMAC::final_result(std::span<uint8_t> mac) {
49 xor_buf(m_state, m_buffer, m_position);
54 m_state[m_position] ^= 0x80;
58 m_cipher->encrypt(m_state);
68 return m_cipher->has_keying_material();
74void CMAC::key_schedule(std::span<const uint8_t> key) {
76 m_cipher->set_key(key);
77 m_cipher->encrypt(m_B);
98 return fmt(
"CMAC({})", m_cipher->name());
105 return std::make_unique<CMAC>(m_cipher->new_object());
111CMAC::CMAC(std::unique_ptr<BlockCipher> cipher) : m_cipher(std::move(cipher)), m_block_size(m_cipher->block_size()) {
113 throw Invalid_Argument(
fmt(
"CMAC cannot use the {} bit cipher {}", m_block_size * 8, m_cipher->name()));
size_t output_length() const override
CMAC(std::unique_ptr< BlockCipher > cipher)
std::unique_ptr< MessageAuthenticationCode > new_object() const override
bool has_keying_material() const override
std::string name() const override
void zeroise(std::vector< T, Alloc > &vec)
std::string fmt(std::string_view format, const T &... args)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
void poly_double_n(uint8_t out[], const uint8_t in[], size_t n)
bool poly_double_supported_size(size_t n)
constexpr void copy_mem(T *out, const T *in, size_t n)