Botan 3.4.0
Crypto and TLS for C&
kmac.cpp
Go to the documentation of this file.
1/*
2* KMAC
3* (C) 2023 Jack Lloyd
4* (C) 2023 Falko Strenzke
5* (C) 2023 René Meusel - Rohde & Schwarz Cybersecurity
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#include <botan/internal/kmac.h>
11
12#include <botan/internal/cshake_xof.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/keccak_helpers.h>
15
16namespace Botan {
17
18KMAC::KMAC(std::unique_ptr<cSHAKE_XOF> cshake, size_t output_bit_length) :
19 m_output_bit_length(output_bit_length), m_message_started(false), m_cshake(std::move(cshake)) {
20 BOTAN_ARG_CHECK(m_output_bit_length % 8 == 0, "KMAC output length must be full bytes");
21 BOTAN_ARG_CHECK(m_output_bit_length > 0, "KMAC output length must be at least one byte");
22 BOTAN_ASSERT_NONNULL(m_cshake);
23}
24
25KMAC::~KMAC() = default;
26
28 zap(m_encoded_key);
29 m_message_started = false;
30 m_cshake->clear();
31}
32
33size_t KMAC::output_length() const {
34 return m_output_bit_length / 8;
35}
36
38 // KMAC supports key lengths from zero up to 2²⁰⁴⁰ (2^(2040)) bits:
39 // https://nvlpubs.nist.gov/nistpubs/specialpublications/nist.sp.800-185.pdf#page=28
40 //
41 // However, we restrict the key length to 64 bytes in order to avoid allocation of overly
42 // large memory stretches when client code works with the maximal key length.
43 return Key_Length_Specification(0, 64);
44}
45
47 return !m_encoded_key.empty();
48}
49
50std::string KMAC::provider() const {
51 return m_cshake->provider();
52}
53
54void KMAC::start_msg(std::span<const uint8_t> nonce) {
56 m_cshake->start(nonce);
57 m_cshake->update(m_encoded_key);
58 m_message_started = true;
59}
60
61void KMAC::add_data(std::span<const uint8_t> data) {
62 assert_key_material_set(!m_encoded_key.empty());
63 if(!m_message_started) {
64 start();
65 }
66 m_cshake->update(data);
67}
68
69void KMAC::final_result(std::span<uint8_t> output) {
71 std::array<uint8_t, keccak_max_int_encoding_size()> encoded_output_length_buffer;
72 m_cshake->update(keccak_int_right_encode(encoded_output_length_buffer, m_output_bit_length));
73 m_cshake->output(output.first(output_length()));
74 m_cshake->clear();
75 m_message_started = false;
76}
77
78void KMAC::key_schedule(std::span<const uint8_t> key) {
79 clear();
80 keccak_absorb_padded_strings_encoding(m_encoded_key, m_cshake->block_size(), key);
81}
82
83// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
84
85KMAC128::KMAC128(size_t output_bit_length) : KMAC(std::make_unique<cSHAKE_128_XOF>("KMAC"), output_bit_length) {}
86
87std::string KMAC128::name() const {
88 return fmt("KMAC-128({})", output_length() * 8);
89}
90
91std::unique_ptr<MessageAuthenticationCode> KMAC128::new_object() const {
92 return std::make_unique<KMAC128>(output_length() * 8);
93}
94
95KMAC256::KMAC256(size_t output_bit_length) : KMAC(std::make_unique<cSHAKE_256_XOF>("KMAC"), output_bit_length) {}
96
97std::string KMAC256::name() const {
98 return fmt("KMAC-256({})", output_length() * 8);
99}
100
101std::unique_ptr<MessageAuthenticationCode> KMAC256::new_object() const {
102 return std::make_unique<KMAC256>(output_length() * 8);
103}
104
105} // namespace Botan
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:86
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
std::string name() const override
Definition kmac.cpp:87
std::unique_ptr< MessageAuthenticationCode > new_object() const override
Definition kmac.cpp:91
KMAC128(size_t output_bit_length)
Definition kmac.cpp:85
std::string name() const override
Definition kmac.cpp:97
KMAC256(size_t output_bit_length)
Definition kmac.cpp:95
std::unique_ptr< MessageAuthenticationCode > new_object() const override
Definition kmac.cpp:101
KMAC(std::unique_ptr< cSHAKE_XOF > cshake, size_t output_bit_length)
Definition kmac.cpp:18
virtual ~KMAC()
bool has_keying_material() const final
Definition kmac.cpp:46
void clear() final
Definition kmac.cpp:27
size_t output_length() const final
Definition kmac.cpp:33
Key_Length_Specification key_spec() const final
Definition kmac.cpp:37
std::string provider() const final
Definition kmac.cpp:50
void assert_key_material_set() const
Definition sym_algo.h:139
void zap(std::vector< T, Alloc > &vec)
Definition secmem.h:117
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
size_t keccak_absorb_padded_strings_encoding(T &sink, size_t padding_mod, Ts... byte_strings)
std::span< const uint8_t > keccak_int_right_encode(std::span< uint8_t > out, size_t x)
constexpr size_t keccak_max_int_encoding_size()