Coverage Report

Created: 2026-03-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/src/lib/mac/cmac/cmac.cpp
Line
Count
Source
1
/*
2
* CMAC
3
* (C) 1999-2007,2014 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/internal/cmac.h>
9
10
#include <botan/exceptn.h>
11
#include <botan/mem_ops.h>
12
#include <botan/internal/fmt.h>
13
#include <botan/internal/poly_dbl.h>
14
#include <botan/internal/stl_util.h>
15
16
namespace Botan {
17
18
/*
19
* Update an CMAC Calculation
20
*/
21
0
void CMAC::add_data(std::span<const uint8_t> input) {
22
0
   const size_t bs = output_length();
23
24
0
   const size_t initial_fill = std::min(m_buffer.size() - m_position, input.size());
25
0
   copy_mem(m_buffer.data() + m_position, input.data(), initial_fill);
26
27
0
   if(m_position + input.size() > bs) {
28
0
      xor_buf(m_state, m_buffer, bs);
29
0
      m_cipher->encrypt(m_state);
30
31
0
      BufferSlicer in(input);
32
0
      in.skip(bs - m_position);
33
0
      while(in.remaining() > bs) {
34
0
         xor_buf(m_state, in.take(bs), bs);
35
0
         m_cipher->encrypt(m_state);
36
0
      }
37
38
0
      const auto remaining = in.take(in.remaining());
39
0
      copy_mem(m_buffer.data(), remaining.data(), remaining.size());
40
0
      m_position = remaining.size();
41
0
   } else {
42
0
      m_position += input.size();
43
0
   }
44
0
}
45
46
/*
47
* Finalize an CMAC Calculation
48
*/
49
0
void CMAC::final_result(std::span<uint8_t> mac) {
50
0
   xor_buf(m_state, m_buffer, m_position);
51
52
0
   if(m_position == output_length()) {
53
0
      xor_buf(m_state, m_B, output_length());
54
0
   } else {
55
0
      m_state[m_position] ^= 0x80;
56
0
      xor_buf(m_state, m_P, output_length());
57
0
   }
58
59
0
   m_cipher->encrypt(m_state);
60
61
0
   copy_mem(mac.data(), m_state.data(), output_length());
62
63
0
   zeroise(m_state);
64
0
   zeroise(m_buffer);
65
0
   m_position = 0;
66
0
}
67
68
0
bool CMAC::has_keying_material() const {
69
0
   return m_cipher->has_keying_material();
70
0
}
71
72
/*
73
* CMAC Key Schedule
74
*/
75
0
void CMAC::key_schedule(std::span<const uint8_t> key) {
76
0
   clear();
77
0
   m_cipher->set_key(key);
78
0
   m_cipher->encrypt(m_B);
79
0
   poly_double_n(m_B.data(), m_B.size());
80
0
   poly_double_n(m_P.data(), m_B.data(), m_P.size());
81
0
}
82
83
/*
84
* Clear memory of sensitive data
85
*/
86
0
void CMAC::clear() {
87
0
   m_cipher->clear();
88
0
   zeroise(m_state);
89
0
   zeroise(m_buffer);
90
0
   zeroise(m_B);
91
0
   zeroise(m_P);
92
0
   m_position = 0;
93
0
}
94
95
/*
96
* Return the name of this type
97
*/
98
0
std::string CMAC::name() const {
99
0
   return fmt("CMAC({})", m_cipher->name());
100
0
}
101
102
/*
103
* Return a new_object of this object
104
*/
105
0
std::unique_ptr<MessageAuthenticationCode> CMAC::new_object() const {
106
0
   return std::make_unique<CMAC>(m_cipher->new_object());
107
0
}
108
109
/*
110
* CMAC Constructor
111
*/
112
0
CMAC::CMAC(std::unique_ptr<BlockCipher> cipher) : m_cipher(std::move(cipher)), m_block_size(m_cipher->block_size()) {
113
0
   if(!poly_double_supported_size(m_block_size)) {
114
0
      throw Invalid_Argument(fmt("CMAC cannot use the {} bit cipher {}", m_block_size * 8, m_cipher->name()));
115
0
   }
116
117
0
   m_state.resize(output_length());
118
0
   m_buffer.resize(output_length());
119
0
   m_B.resize(output_length());
120
0
   m_P.resize(output_length());
121
0
   m_position = 0;
122
0
}
123
124
}  // namespace Botan