diff options
author | lloyd <[email protected]> | 2014-01-10 03:41:59 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-01-10 03:41:59 +0000 |
commit | 6894dca64c04936d07048c0e8cbf7e25858548c3 (patch) | |
tree | 5d572bfde9fe667dab14e3f04b5285a85d8acd95 /src/lib/mac/cbc_mac | |
parent | 9efa3be92442afb3d0b69890a36c7f122df18eda (diff) |
Move lib into src
Diffstat (limited to 'src/lib/mac/cbc_mac')
-rw-r--r-- | src/lib/mac/cbc_mac/cbc_mac.cpp | 105 | ||||
-rw-r--r-- | src/lib/mac/cbc_mac/cbc_mac.h | 49 | ||||
-rw-r--r-- | src/lib/mac/cbc_mac/info.txt | 5 |
3 files changed, 159 insertions, 0 deletions
diff --git a/src/lib/mac/cbc_mac/cbc_mac.cpp b/src/lib/mac/cbc_mac/cbc_mac.cpp new file mode 100644 index 000000000..118570e72 --- /dev/null +++ b/src/lib/mac/cbc_mac/cbc_mac.cpp @@ -0,0 +1,105 @@ +/* +* CBC-MAC +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cbc_mac.h> +#include <botan/internal/xor_buf.h> +#include <algorithm> + +namespace Botan { + +/* +* Update an CBC-MAC Calculation +*/ +void CBC_MAC::add_data(const byte input[], size_t length) + { + size_t xored = std::min(output_length() - position, length); + xor_buf(&state[position], input, xored); + position += xored; + + if(position < output_length()) + return; + + e->encrypt(state); + input += xored; + length -= xored; + while(length >= output_length()) + { + xor_buf(state, input, output_length()); + e->encrypt(state); + input += output_length(); + length -= output_length(); + } + + xor_buf(state, input, length); + position = length; + } + +/* +* Finalize an CBC-MAC Calculation +*/ +void CBC_MAC::final_result(byte mac[]) + { + if(position) + e->encrypt(state); + + copy_mem(mac, &state[0], state.size()); + zeroise(state); + position = 0; + } + +/* +* CBC-MAC Key Schedule +*/ +void CBC_MAC::key_schedule(const byte key[], size_t length) + { + e->set_key(key, length); + } + +/* +* Clear memory of sensitive data +*/ +void CBC_MAC::clear() + { + e->clear(); + zeroise(state); + position = 0; + } + +/* +* Return the name of this type +*/ +std::string CBC_MAC::name() const + { + return "CBC-MAC(" + e->name() + ")"; + } + +/* +* Return a clone of this object +*/ +MessageAuthenticationCode* CBC_MAC::clone() const + { + return new CBC_MAC(e->clone()); + } + +/* +* CBC-MAC Constructor +*/ +CBC_MAC::CBC_MAC(BlockCipher* e_in) : + e(e_in), state(e->block_size()) + { + position = 0; + } + +/* +* CBC-MAC Destructor +*/ +CBC_MAC::~CBC_MAC() + { + delete e; + } + +} diff --git a/src/lib/mac/cbc_mac/cbc_mac.h b/src/lib/mac/cbc_mac/cbc_mac.h new file mode 100644 index 000000000..be25718d9 --- /dev/null +++ b/src/lib/mac/cbc_mac/cbc_mac.h @@ -0,0 +1,49 @@ +/* +* CBC-MAC +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CBC_MAC_H__ +#define BOTAN_CBC_MAC_H__ + +#include <botan/mac.h> +#include <botan/block_cipher.h> + +namespace Botan { + +/** +* CBC-MAC +*/ +class BOTAN_DLL CBC_MAC : public MessageAuthenticationCode + { + public: + std::string name() const; + MessageAuthenticationCode* clone() const; + size_t output_length() const { return e->block_size(); } + void clear(); + + Key_Length_Specification key_spec() const + { + return e->key_spec(); + } + + /** + * @param cipher the underlying block cipher to use + */ + CBC_MAC(BlockCipher* cipher); + ~CBC_MAC(); + private: + void add_data(const byte[], size_t); + void final_result(byte[]); + void key_schedule(const byte[], size_t); + + BlockCipher* e; + secure_vector<byte> state; + size_t position; + }; + +} + +#endif diff --git a/src/lib/mac/cbc_mac/info.txt b/src/lib/mac/cbc_mac/info.txt new file mode 100644 index 000000000..cb017e2a1 --- /dev/null +++ b/src/lib/mac/cbc_mac/info.txt @@ -0,0 +1,5 @@ +define CBC_MAC 20131128 + +<requires> +block +</requires> |