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

randombit / botan / 6144099563

11 Sep 2023 08:48AM UTC coverage: 91.693% (-0.02%) from 91.711%
6144099563

push

github

web-flow
Merge pull request #3681 from randombit/span/bufcomp

std::span for Buffered_Computation

78567 of 85685 relevant lines covered (91.69%)

8524467.68 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
#include <botan/internal/stl_util.h>
14

15
namespace Botan {
16

17
/*
18
* Update an CMAC Calculation
19
*/
20
void CMAC::add_data(std::span<const uint8_t> input) {
1,033,531✔
21
   const size_t bs = output_length();
1,033,531✔
22

23
   buffer_insert(m_buffer, m_position, input.data(), input.size());
24
   if(m_position + input.size() > bs) {
1,033,531✔
25
      xor_buf(m_state, m_buffer, bs);
14,796✔
26
      m_cipher->encrypt(m_state);
14,796✔
27

28
      BufferSlicer in(input);
13,578✔
29
      in.skip(bs - m_position);
13,578✔
30
      while(in.remaining() > bs) {
29,284✔
31
         xor_buf(m_state, in.take(bs), bs);
15,706✔
32
         m_cipher->encrypt(m_state);
44,990✔
33
      }
34

35
      const auto remaining = in.take(in.remaining());
13,578✔
36
      copy_mem(m_buffer.data(), remaining.data(), remaining.size());
13,578✔
37
      m_position = remaining.size();
13,578✔
38
   } else {
39
      m_position += input.size();
1,018,735✔
40
   }
41
}
1,032,313✔
42

43
/*
44
* Finalize an CMAC Calculation
45
*/
46
void CMAC::final_result(std::span<uint8_t> mac) {
892,136✔
47
   xor_buf(m_state, m_buffer, m_position);
892,136✔
48

49
   if(m_position == output_length()) {
892,136✔
50
      xor_buf(m_state, m_B, output_length());
878,740✔
51
   } else {
52
      m_state[m_position] ^= 0x80;
13,396✔
53
      xor_buf(m_state, m_P, output_length());
13,396✔
54
   }
55

56
   m_cipher->encrypt(m_state);
892,136✔
57

58
   copy_mem(mac.data(), m_state.data(), output_length());
890,130✔
59

60
   zeroise(m_state);
890,130✔
61
   zeroise(m_buffer);
890,130✔
62
   m_position = 0;
890,130✔
63
}
890,130✔
64

65
bool CMAC::has_keying_material() const {
1,140✔
66
   return m_cipher->has_keying_material();
1,140✔
67
}
68

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

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

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

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

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

114
   m_state.resize(output_length());
3,344✔
115
   m_buffer.resize(output_length());
3,344✔
116
   m_B.resize(output_length());
3,344✔
117
   m_P.resize(output_length());
3,344✔
118
   m_position = 0;
3,344✔
119
}
3,344✔
120

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