• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 24436095446

15 Apr 2026 01:50AM UTC coverage: 89.442% (-0.002%) from 89.444%
24436095446

push

github

web-flow
Merge pull request #5527 from randombit/jack/acvp-tests

Add script for running ACVP test vectors

105983 of 118494 relevant lines covered (89.44%)

11580585.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.21
/src/lib/mac/kmac/kmac.cpp
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

16
namespace Botan {
17

18
KMAC::KMAC(std::unique_ptr<cSHAKE_XOF> cshake, size_t output_bit_length) :
27✔
19
      m_output_bit_length(output_bit_length), m_message_started(false), m_cshake(std::move(cshake)) {
27✔
20
   BOTAN_ARG_CHECK(m_output_bit_length % 8 == 0, "KMAC output length must be full bytes");
27✔
21
   BOTAN_ARG_CHECK(m_output_bit_length > 0, "KMAC output length must be at least one byte");
27✔
22
   BOTAN_ASSERT_NONNULL(m_cshake);
27✔
23
}
27✔
24

25
KMAC::~KMAC() = default;
54✔
26

27
void KMAC::clear() {
75✔
28
   zap(m_encoded_key);
75✔
29
   m_message_started = false;
75✔
30
   m_cshake->clear();
75✔
31
}
75✔
32

33
size_t KMAC::output_length() const {
183✔
34
   return m_output_bit_length / 8;
60✔
35
}
36

37
Key_Length_Specification KMAC::key_spec() const {
54✔
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 192 bytes in order to avoid allocation of overly
42
   // large memory stretches when client code works with the maximal key length. We chose a
43
   // boundary that contains the length of the default_salt of the one-step KDM with KMAC128
44
   // of 164 bytes. (see NIST SP 800-56C Rev. 2, Section 4.1, Implementation-Dependent Parameters 3.).
45
   return Key_Length_Specification(0, 192);
54✔
46
}
47

48
bool KMAC::has_keying_material() const {
156✔
49
   return !m_encoded_key.empty();
156✔
50
}
51

52
std::string KMAC::provider() const {
6✔
53
   return m_cshake->provider();
6✔
54
}
55

56
void KMAC::start_msg(std::span<const uint8_t> nonce) {
72✔
57
   assert_key_material_set();
72✔
58
   m_cshake->start(nonce);
72✔
59
   m_cshake->update(m_encoded_key);
72✔
60
   m_message_started = true;
72✔
61
}
72✔
62

63
void KMAC::add_data(std::span<const uint8_t> data) {
126✔
64
   assert_key_material_set(!m_encoded_key.empty());
126✔
65
   if(!m_message_started) {
114✔
66
      start();
3✔
67
   }
68
   m_cshake->update(data);
114✔
69
}
114✔
70

71
void KMAC::final_result(std::span<uint8_t> output) {
66✔
72
   assert_key_material_set();
66✔
73
   if(!m_message_started) {
60✔
74
      start();
×
75
   }
76
   std::array<uint8_t, keccak_max_int_encoding_size()> encoded_output_length_buffer{};
60✔
77
   m_cshake->update(keccak_int_right_encode(encoded_output_length_buffer, m_output_bit_length));
60✔
78
   m_cshake->output(output.first(output_length()));
60✔
79
   m_cshake->clear();
60✔
80
   m_message_started = false;
60✔
81
}
60✔
82

83
void KMAC::key_schedule(std::span<const uint8_t> key) {
54✔
84
   clear();
54✔
85
   keccak_absorb_padded_strings_encoding(m_encoded_key, m_cshake->block_size(), key);
54✔
86
}
54✔
87

88
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
89

90
KMAC128::KMAC128(size_t output_bit_length) : KMAC(std::make_unique<cSHAKE_128_XOF>("KMAC"), output_bit_length) {}
36✔
91

92
std::string KMAC128::name() const {
18✔
93
   return fmt("KMAC-128({})", output_length() * 8);
18✔
94
}
95

96
std::unique_ptr<MessageAuthenticationCode> KMAC128::new_object() const {
3✔
97
   return std::make_unique<KMAC128>(output_length() * 8);
3✔
98
}
99

100
KMAC256::KMAC256(size_t output_bit_length) : KMAC(std::make_unique<cSHAKE_256_XOF>("KMAC"), output_bit_length) {}
18✔
101

102
std::string KMAC256::name() const {
18✔
103
   return fmt("KMAC-256({})", output_length() * 8);
18✔
104
}
105

106
std::unique_ptr<MessageAuthenticationCode> KMAC256::new_object() const {
3✔
107
   return std::make_unique<KMAC256>(output_length() * 8);
3✔
108
}
109

110
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc