Botan  1.10.9
x919_mac.cpp
Go to the documentation of this file.
1 /*
2 * ANSI X9.19 MAC
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/x919_mac.h>
9 #include <botan/internal/xor_buf.h>
10 #include <algorithm>
11 
12 namespace Botan {
13 
14 /*
15 * Update an ANSI X9.19 MAC Calculation
16 */
17 void ANSI_X919_MAC::add_data(const byte input[], size_t length)
18  {
19  size_t xored = std::min(8 - position, length);
20  xor_buf(&state[position], input, xored);
21  position += xored;
22 
23  if(position < 8) return;
24 
25  e->encrypt(state);
26  input += xored;
27  length -= xored;
28  while(length >= 8)
29  {
30  xor_buf(state, input, 8);
31  e->encrypt(state);
32  input += 8;
33  length -= 8;
34  }
35 
36  xor_buf(state, input, length);
37  position = length;
38  }
39 
40 /*
41 * Finalize an ANSI X9.19 MAC Calculation
42 */
43 void ANSI_X919_MAC::final_result(byte mac[])
44  {
45  if(position)
46  e->encrypt(state);
47  d->decrypt(state, mac);
48  e->encrypt(mac);
49  zeroise(state);
50  position = 0;
51  }
52 
53 /*
54 * ANSI X9.19 MAC Key Schedule
55 */
56 void ANSI_X919_MAC::key_schedule(const byte key[], size_t length)
57  {
58  e->set_key(key, 8);
59  if(length == 8) d->set_key(key, 8);
60  else d->set_key(key + 8, 8);
61  }
62 
63 /*
64 * Clear memory of sensitive data
65 */
67  {
68  e->clear();
69  d->clear();
70  zeroise(state);
71  position = 0;
72  }
73 
74 std::string ANSI_X919_MAC::name() const
75  {
76  return "X9.19-MAC";
77  }
78 
80  {
81  return new ANSI_X919_MAC(e->clone());
82  }
83 
84 /*
85 * ANSI X9.19 MAC Constructor
86 */
88  e(e_in), d(e->clone()), state(e->block_size()), position(0)
89  {
90  if(e->name() != "DES")
91  throw Invalid_Argument("ANSI X9.19 MAC only supports DES");
92  }
93 
94 /*
95 * ANSI X9.19 MAC Destructor
96 le*/
98  {
99  delete e;
100  delete d;
101  }
102 
103 }
virtual void clear()=0
void decrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:57
size_t block_size
Definition: ossl_md.cpp:41
std::string name() const
Definition: x919_mac.cpp:74
virtual BlockCipher * clone() const =0
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
unsigned char byte
Definition: types.h:22
RC4_KEY state
Definition: ossl_arc4.cpp:39
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
MessageAuthenticationCode * mac
Definition: fpe_fe1.cpp:94
ANSI_X919_MAC(BlockCipher *cipher)
Definition: x919_mac.cpp:87
virtual std::string name() const =0
void encrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:47
MessageAuthenticationCode * clone() const
Definition: x919_mac.cpp:79
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415