Botan: src/mac/cbc_mac/cbc_mac.cpp Source File

Botan 1.10.16

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 */
65 void CBC_MAC::clear()
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 */
83 MessageAuthenticationCode* CBC_MAC::clone() const
84 {
85 return new CBC_MAC(e->clone());
86 }
87
88 /*
89 * CBC-MAC Constructor
90 */
91 CBC_MAC::CBC_MAC(BlockCipher* e_in) :
92 e(e_in), state(e->block_size())
93 {
94 position = 0;
95 }
96
97 /*
98 * CBC-MAC Destructor
99 */
100 CBC_MAC::~CBC_MAC()
101 {
102 delete e;
103 }
104
105 }
Botan::Algorithm::clear
virtual void clear()=0
Botan::CBC_MAC::output_length
size_t output_length() const
Definition: cbc_mac.h:24
Botan::CBC_MAC::CBC_MAC
CBC_MAC(BlockCipher *cipher)
Definition: cbc_mac.cpp:91
Botan::BlockCipher::clone
virtual BlockCipher * clone() const =0
Botan::MessageAuthenticationCode
Definition: mac.h:20
Botan::CBC_MAC::name
std::string name() const
Definition: cbc_mac.cpp:75
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::BlockCipher::encrypt
void encrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:47
Botan::SymmetricAlgorithm::set_key
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
Botan
Definition: algo_base.h:14
Botan::Algorithm::name
virtual std::string name() const =0
Botan::CBC_MAC::~CBC_MAC
~CBC_MAC()
Definition: cbc_mac.cpp:100
Botan::CBC_MAC::clear
void clear()
Definition: cbc_mac.cpp:65
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::CBC_MAC::clone
MessageAuthenticationCode * clone() const
Definition: cbc_mac.cpp:83
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:127
Botan::xor_buf
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
Botan::BlockCipher
Definition: block_cipher.h:18