Botan  2.6.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  verify_key_set(m_state.empty() == false);
18 
19  size_t xored = std::min(output_length() - m_position, length);
20  xor_buf(&m_state[m_position], input, xored);
21  m_position += xored;
22 
23  if(m_position < output_length())
24  return;
25 
26  m_cipher->encrypt(m_state);
27  input += xored;
28  length -= xored;
29  while(length >= output_length())
30  {
31  xor_buf(m_state, input, output_length());
32  m_cipher->encrypt(m_state);
33  input += output_length();
34  length -= output_length();
35  }
36 
37  xor_buf(m_state, input, length);
38  m_position = length;
39  }
40 
41 /*
42 * Finalize an CBC-MAC Calculation
43 */
44 void CBC_MAC::final_result(uint8_t mac[])
45  {
46  verify_key_set(m_state.empty() == false);
47 
48  if(m_position)
49  m_cipher->encrypt(m_state);
50 
51  copy_mem(mac, m_state.data(), m_state.size());
52  zeroise(m_state);
53  m_position = 0;
54  }
55 
56 /*
57 * CBC-MAC Key Schedule
58 */
59 void CBC_MAC::key_schedule(const uint8_t key[], size_t length)
60  {
61  m_state.resize(m_cipher->block_size());
62  m_cipher->set_key(key, length);
63  }
64 
65 /*
66 * Clear memory of sensitive data
67 */
69  {
70  m_cipher->clear();
71  zap(m_state);
72  m_position = 0;
73  }
74 
75 /*
76 * Return the name of this type
77 */
78 std::string CBC_MAC::name() const
79  {
80  return "CBC-MAC(" + m_cipher->name() + ")";
81  }
82 
83 /*
84 * Return a clone of this object
85 */
87  {
88  return new CBC_MAC(m_cipher->clone());
89  }
90 
91 /*
92 * CBC-MAC Constructor
93 */
95  m_cipher(cipher)
96  {
97  }
98 
99 }
void verify_key_set(bool cond) const
Definition: sym_algo.h:95
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:193
std::string name() const override
Definition: cbc_mac.cpp:78
CBC_MAC(BlockCipher *cipher)
Definition: cbc_mac.cpp:94
MessageAuthenticationCode * clone() const override
Definition: cbc_mac.cpp:86
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:174
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:108
Definition: alg_id.cpp:13
void clear() override
Definition: cbc_mac.cpp:68
size_t output_length() const override
Definition: cbc_mac.h:24
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183