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