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

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 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/internal/fmt.h>
12
#include <botan/internal/poly_dbl.h>
13

14
namespace Botan {
15

16
/*
17
* Update an CMAC Calculation
18
*/
19
void CMAC::add_data(const uint8_t input[], size_t length) {
1,033,535✔
20
   const size_t bs = output_length();
1,033,535✔
21

22
   buffer_insert(m_buffer, m_position, input, length);
1,033,535✔
23
   if(m_position + length > bs) {
1,033,535✔
24
      xor_buf(m_state, m_buffer, bs);
14,794✔
25
      m_cipher->encrypt(m_state);
14,794✔
26
      input += (bs - m_position);
13,576✔
27
      length -= (bs - m_position);
13,576✔
28
      while(length > bs) {
29,281✔
29
         xor_buf(m_state, input, bs);
15,705✔
30
         m_cipher->encrypt(m_state);
15,705✔
31
         input += bs;
15,705✔
32
         length -= bs;
15,705✔
33
      }
34
      copy_mem(m_buffer.data(), input, length);
13,576✔
35
      m_position = 0;
13,576✔
36
   }
37
   m_position += length;
1,032,317✔
38
}
1,032,317✔
39

40
/*
41
* Finalize an CMAC Calculation
42
*/
43
void CMAC::final_result(uint8_t mac[]) {
892,136✔
44
   xor_buf(m_state, m_buffer, m_position);
892,136✔
45

46
   if(m_position == output_length()) {
892,136✔
47
      xor_buf(m_state, m_B, output_length());
878,739✔
48
   } else {
49
      m_state[m_position] ^= 0x80;
13,397✔
50
      xor_buf(m_state, m_P, output_length());
13,397✔
51
   }
52

53
   m_cipher->encrypt(m_state);
892,136✔
54

55
   copy_mem(mac, m_state.data(), output_length());
890,130✔
56

57
   zeroise(m_state);
890,130✔
58
   zeroise(m_buffer);
890,130✔
59
   m_position = 0;
890,130✔
60
}
890,130✔
61

62
bool CMAC::has_keying_material() const {
1,140✔
63
   return m_cipher->has_keying_material();
1,140✔
64
}
65

66
/*
67
* CMAC Key Schedule
68
*/
69
void CMAC::key_schedule(const uint8_t key[], size_t length) {
2,571✔
70
   clear();
2,571✔
71
   m_cipher->set_key(key, length);
2,571✔
72
   m_cipher->encrypt(m_B);
2,571✔
73
   poly_double_n(m_B.data(), m_B.size());
2,571✔
74
   poly_double_n(m_P.data(), m_B.data(), m_P.size());
2,571✔
75
}
2,571✔
76

77
/*
78
* Clear memory of sensitive data
79
*/
80
void CMAC::clear() {
3,675✔
81
   m_cipher->clear();
3,675✔
82
   zeroise(m_state);
3,675✔
83
   zeroise(m_buffer);
3,675✔
84
   zeroise(m_B);
3,675✔
85
   zeroise(m_P);
3,675✔
86
   m_position = 0;
3,675✔
87
}
3,675✔
88

89
/*
90
* Return the name of this type
91
*/
92
std::string CMAC::name() const {
1,195✔
93
   return fmt("CMAC({})", m_cipher->name());
1,195✔
94
}
95

96
/*
97
* Return a new_object of this object
98
*/
99
std::unique_ptr<MessageAuthenticationCode> CMAC::new_object() const {
430✔
100
   return std::make_unique<CMAC>(m_cipher->new_object());
430✔
101
}
102

103
/*
104
* CMAC Constructor
105
*/
106
CMAC::CMAC(std::unique_ptr<BlockCipher> cipher) : m_cipher(std::move(cipher)), m_block_size(m_cipher->block_size()) {
3,344✔
107
   if(poly_double_supported_size(m_block_size) == false) {
3,344✔
108
      throw Invalid_Argument(fmt("CMAC cannot use the {} bit cipher {}", m_block_size * 8, m_cipher->name()));
×
109
   }
110

111
   m_state.resize(output_length());
3,344✔
112
   m_buffer.resize(output_length());
3,344✔
113
   m_B.resize(output_length());
3,344✔
114
   m_P.resize(output_length());
3,344✔
115
   m_position = 0;
3,344✔
116
}
3,344✔
117

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