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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

98.25
/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,547✔
20
   const size_t bs = output_length();
1,033,547✔
21

22
   buffer_insert(m_buffer, m_position, input, length);
1,033,547✔
23
   if(m_position + length > bs) {
1,033,547✔
24
      xor_buf(m_state, m_buffer, bs);
14,807✔
25
      m_cipher->encrypt(m_state);
14,807✔
26
      input += (bs - m_position);
13,589✔
27
      length -= (bs - m_position);
13,589✔
28
      while(length > bs) {
29,294✔
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,589✔
35
      m_position = 0;
13,589✔
36
   }
37
   m_position += length;
1,032,329✔
38
}
1,032,329✔
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,727✔
48
   } else {
49
      m_state[m_position] ^= 0x80;
13,409✔
50
      xor_buf(m_state, m_P, output_length());
13,409✔
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 { return m_cipher->has_keying_material(); }
1,140✔
63

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

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

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

92
/*
93
* Return a new_object of this object
94
*/
95
std::unique_ptr<MessageAuthenticationCode> CMAC::new_object() const {
430✔
96
   return std::make_unique<CMAC>(m_cipher->new_object());
430✔
97
}
98

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

107
   m_state.resize(output_length());
3,344✔
108
   m_buffer.resize(output_length());
3,344✔
109
   m_B.resize(output_length());
3,344✔
110
   m_P.resize(output_length());
3,344✔
111
   m_position = 0;
3,344✔
112
}
3,344✔
113

114
}
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