Botan  2.1.0
Crypto and TLS for C++11
cbc_mac.cpp
Go to the documentation of this file.
1 /*
2 * CBC-MAC
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/cbc_mac.h>
9 
10 namespace Botan {
11 
12 /*
13 * Update an CBC-MAC Calculation
14 */
15 void CBC_MAC::add_data(const uint8_t input[], size_t length)
16  {
17  size_t xored = std::min(output_length() - m_position, length);
18  xor_buf(&m_state[m_position], input, xored);
19  m_position += xored;
20 
21  if(m_position < output_length())
22  return;
23 
24  m_cipher->encrypt(m_state);
25  input += xored;
26  length -= xored;
27  while(length >= output_length())
28  {
29  xor_buf(m_state, input, output_length());
30  m_cipher->encrypt(m_state);
31  input += output_length();
32  length -= output_length();
33  }
34 
35  xor_buf(m_state, input, length);
36  m_position = length;
37  }
38 
39 /*
40 * Finalize an CBC-MAC Calculation
41 */
42 void CBC_MAC::final_result(uint8_t mac[])
43  {
44  if(m_position)
45  m_cipher->encrypt(m_state);
46 
47  copy_mem(mac, m_state.data(), m_state.size());
48  zeroise(m_state);
49  m_position = 0;
50  }
51 
52 /*
53 * CBC-MAC Key Schedule
54 */
55 void CBC_MAC::key_schedule(const uint8_t key[], size_t length)
56  {
57  m_cipher->set_key(key, length);
58  }
59 
60 /*
61 * Clear memory of sensitive data
62 */
64  {
65  m_cipher->clear();
66  zeroise(m_state);
67  m_position = 0;
68  }
69 
70 /*
71 * Return the name of this type
72 */
73 std::string CBC_MAC::name() const
74  {
75  return "CBC-MAC(" + m_cipher->name() + ")";
76  }
77 
78 /*
79 * Return a clone of this object
80 */
82  {
83  return new CBC_MAC(m_cipher->clone());
84  }
85 
86 /*
87 * CBC-MAC Constructor
88 */
90  m_cipher(cipher), m_state(cipher->block_size())
91  {
92  }
93 
94 }
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:115
std::string name() const override
Definition: cbc_mac.cpp:73
CBC_MAC(BlockCipher *cipher)
Definition: cbc_mac.cpp:89
MessageAuthenticationCode * clone() const override
Definition: cbc_mac.cpp:81
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
Definition: alg_id.cpp:13
void clear() override
Definition: cbc_mac.cpp:63
T min(T a, T b)
Definition: ct_utils.h:180
size_t output_length() const override
Definition: cbc_mac.h:24
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:211