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

randombit / botan / 13274522654

11 Feb 2025 11:26PM UTC coverage: 91.645% (-0.007%) from 91.652%
13274522654

push

github

web-flow
Merge pull request #4647 from randombit/jack/internal-assert-and-mem-ops

Avoid using mem_ops.h or assert.h in public headers

94854 of 103501 relevant lines covered (91.65%)

11334975.77 hits per line

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

98.31
/src/lib/mac/cmac/cmac.cpp
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
void CMAC::add_data(std::span<const uint8_t> input) {
1,034,162✔
22
   const size_t bs = output_length();
1,034,162✔
23

24
   const size_t initial_fill = std::min(m_buffer.size() - m_position, input.size());
1,034,162✔
25
   copy_mem(m_buffer.data() + m_position, input.data(), initial_fill);
1,034,162✔
26

27
   if(m_position + input.size() > bs) {
1,034,162✔
28
      xor_buf(m_state, m_buffer, bs);
14,977✔
29
      m_cipher->encrypt(m_state);
14,977✔
30

31
      BufferSlicer in(input);
13,759✔
32
      in.skip(bs - m_position);
13,759✔
33
      while(in.remaining() > bs) {
29,827✔
34
         xor_buf(m_state, in.take(bs), bs);
16,068✔
35
         m_cipher->encrypt(m_state);
45,895✔
36
      }
37

38
      const auto remaining = in.take(in.remaining());
13,759✔
39
      copy_mem(m_buffer.data(), remaining.data(), remaining.size());
13,759✔
40
      m_position = remaining.size();
13,759✔
41
   } else {
42
      m_position += input.size();
1,019,185✔
43
   }
44
}
1,032,944✔
45

46
/*
47
* Finalize an CMAC Calculation
48
*/
49
void CMAC::final_result(std::span<uint8_t> mac) {
892,222✔
50
   xor_buf(m_state, m_buffer, m_position);
892,222✔
51

52
   if(m_position == output_length()) {
892,222✔
53
      xor_buf(m_state, m_B, output_length());
878,750✔
54
   } else {
55
      m_state[m_position] ^= 0x80;
13,472✔
56
      xor_buf(m_state, m_P, output_length());
13,472✔
57
   }
58

59
   m_cipher->encrypt(m_state);
892,222✔
60

61
   copy_mem(mac.data(), m_state.data(), output_length());
890,216✔
62

63
   zeroise(m_state);
890,216✔
64
   zeroise(m_buffer);
890,216✔
65
   m_position = 0;
890,216✔
66
}
890,216✔
67

68
bool CMAC::has_keying_material() const {
1,140✔
69
   return m_cipher->has_keying_material();
1,140✔
70
}
71

72
/*
73
* CMAC Key Schedule
74
*/
75
void CMAC::key_schedule(std::span<const uint8_t> key) {
2,622✔
76
   clear();
2,622✔
77
   m_cipher->set_key(key);
2,622✔
78
   m_cipher->encrypt(m_B);
2,622✔
79
   poly_double_n(m_B.data(), m_B.size());
2,622✔
80
   poly_double_n(m_P.data(), m_B.data(), m_P.size());
2,622✔
81
}
2,622✔
82

83
/*
84
* Clear memory of sensitive data
85
*/
86
void CMAC::clear() {
3,726✔
87
   m_cipher->clear();
3,726✔
88
   zeroise(m_state);
3,726✔
89
   zeroise(m_buffer);
3,726✔
90
   zeroise(m_B);
3,726✔
91
   zeroise(m_P);
3,726✔
92
   m_position = 0;
3,726✔
93
}
3,726✔
94

95
/*
96
* Return the name of this type
97
*/
98
std::string CMAC::name() const {
1,222✔
99
   return fmt("CMAC({})", m_cipher->name());
1,222✔
100
}
101

102
/*
103
* Return a new_object of this object
104
*/
105
std::unique_ptr<MessageAuthenticationCode> CMAC::new_object() const {
439✔
106
   return std::make_unique<CMAC>(m_cipher->new_object());
439✔
107
}
108

109
/*
110
* CMAC Constructor
111
*/
112
CMAC::CMAC(std::unique_ptr<BlockCipher> cipher) : m_cipher(std::move(cipher)), m_block_size(m_cipher->block_size()) {
3,366✔
113
   if(poly_double_supported_size(m_block_size) == false) {
3,366✔
114
      throw Invalid_Argument(fmt("CMAC cannot use the {} bit cipher {}", m_block_size * 8, m_cipher->name()));
×
115
   }
116

117
   m_state.resize(output_length());
3,366✔
118
   m_buffer.resize(output_length());
3,366✔
119
   m_B.resize(output_length());
3,366✔
120
   m_P.resize(output_length());
3,366✔
121
   m_position = 0;
3,366✔
122
}
3,366✔
123

124
}  // 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