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