Botan  2.7.0
Crypto and TLS for C++11
x919_mac.cpp
Go to the documentation of this file.
1 /*
2 * ANSI X9.19 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/x919_mac.h>
9 
10 namespace Botan {
11 
12 /*
13 * Update an ANSI X9.19 MAC Calculation
14 */
15 void ANSI_X919_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(8 - m_position, length);
20  xor_buf(&m_state[m_position], input, xored);
21  m_position += xored;
22 
23  if(m_position < 8) return;
24 
25  m_des1->encrypt(m_state);
26  input += xored;
27  length -= xored;
28  while(length >= 8)
29  {
30  xor_buf(m_state, input, 8);
31  m_des1->encrypt(m_state);
32  input += 8;
33  length -= 8;
34  }
35 
36  xor_buf(m_state, input, length);
37  m_position = length;
38  }
39 
40 /*
41 * Finalize an ANSI X9.19 MAC Calculation
42 */
43 void ANSI_X919_MAC::final_result(uint8_t mac[])
44  {
45  if(m_position)
46  m_des1->encrypt(m_state);
47  m_des2->decrypt(m_state.data(), mac);
48  m_des1->encrypt(mac);
49  zeroise(m_state);
50  m_position = 0;
51  }
52 
53 /*
54 * ANSI X9.19 MAC Key Schedule
55 */
56 void ANSI_X919_MAC::key_schedule(const uint8_t key[], size_t length)
57  {
58  m_state.resize(8);
59 
60  m_des1->set_key(key, 8);
61 
62  if(length == 16)
63  key += 8;
64 
65  m_des2->set_key(key, 8);
66  }
67 
68 /*
69 * Clear memory of sensitive data
70 */
72  {
73  m_des1->clear();
74  m_des2->clear();
75  zap(m_state);
76  m_position = 0;
77  }
78 
79 std::string ANSI_X919_MAC::name() const
80  {
81  return "X9.19-MAC";
82  }
83 
85  {
86  return new ANSI_X919_MAC;
87  }
88 
89 /*
90 * ANSI X9.19 MAC Constructor
91 */
93  m_des1(BlockCipher::create("DES")),
94  m_des2(m_des1->clone()),
95  m_position(0)
96  {
97  }
98 
99 }
void verify_key_set(bool cond) const
Definition: sym_algo.h:89
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:193
void clear() override
Definition: x919_mac.cpp:71
std::string name() const override
Definition: x919_mac.cpp:79
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:174
MessageAuthenticationCode * clone() const override
Definition: x919_mac.cpp:84
Definition: alg_id.cpp:13
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183