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/hmac | |
parent | 9efa3be92442afb3d0b69890a36c7f122df18eda (diff) |
Move lib into src
Diffstat (limited to 'src/lib/mac/hmac')
-rw-r--r-- | src/lib/mac/hmac/hmac.cpp | 97 | ||||
-rw-r--r-- | src/lib/mac/hmac/hmac.h | 53 | ||||
-rw-r--r-- | src/lib/mac/hmac/info.txt | 5 |
3 files changed, 155 insertions, 0 deletions
diff --git a/src/lib/mac/hmac/hmac.cpp b/src/lib/mac/hmac/hmac.cpp new file mode 100644 index 000000000..9e9a643db --- /dev/null +++ b/src/lib/mac/hmac/hmac.cpp @@ -0,0 +1,97 @@ +/* +* HMAC +* (C) 1999-2007 Jack Lloyd +* 2007 Yves Jerschow +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/hmac.h> +#include <botan/internal/xor_buf.h> + +namespace Botan { + +/* +* Update a HMAC Calculation +*/ +void HMAC::add_data(const byte input[], size_t length) + { + hash->update(input, length); + } + +/* +* Finalize a HMAC Calculation +*/ +void HMAC::final_result(byte mac[]) + { + hash->final(mac); + hash->update(o_key); + hash->update(mac, output_length()); + hash->final(mac); + hash->update(i_key); + } + +/* +* HMAC Key Schedule +*/ +void HMAC::key_schedule(const byte key[], size_t length) + { + hash->clear(); + + i_key.resize(hash->hash_block_size()); + o_key.resize(hash->hash_block_size()); + + std::fill(i_key.begin(), i_key.end(), 0x36); + std::fill(o_key.begin(), o_key.end(), 0x5C); + + if(length > hash->hash_block_size()) + { + secure_vector<byte> hmac_key = hash->process(key, length); + xor_buf(i_key, hmac_key, hmac_key.size()); + xor_buf(o_key, hmac_key, hmac_key.size()); + } + else + { + xor_buf(i_key, key, length); + xor_buf(o_key, key, length); + } + + hash->update(i_key); + } + +/* +* Clear memory of sensitive data +*/ +void HMAC::clear() + { + hash->clear(); + zap(i_key); + zap(o_key); + } + +/* +* Return the name of this type +*/ +std::string HMAC::name() const + { + return "HMAC(" + hash->name() + ")"; + } + +/* +* Return a clone of this object +*/ +MessageAuthenticationCode* HMAC::clone() const + { + return new HMAC(hash->clone()); + } + +/* +* HMAC Constructor +*/ +HMAC::HMAC(HashFunction* hash_in) : hash(hash_in) + { + if(hash->hash_block_size() == 0) + throw Invalid_Argument("HMAC cannot be used with " + hash->name()); + } + +} diff --git a/src/lib/mac/hmac/hmac.h b/src/lib/mac/hmac/hmac.h new file mode 100644 index 000000000..39a084874 --- /dev/null +++ b/src/lib/mac/hmac/hmac.h @@ -0,0 +1,53 @@ +/* +* HMAC +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_HMAC_H__ +#define BOTAN_HMAC_H__ + +#include <botan/mac.h> +#include <botan/hash.h> + +namespace Botan { + +/** +* HMAC +*/ +class BOTAN_DLL HMAC : public MessageAuthenticationCode + { + public: + void clear(); + std::string name() const; + MessageAuthenticationCode* clone() const; + + size_t output_length() const { return hash->output_length(); } + + Key_Length_Specification key_spec() const + { + return Key_Length_Specification(0, 512); + } + + /** + * @param hash the hash to use for HMACing + */ + HMAC(HashFunction* hash); + + HMAC(const HMAC&) = delete; + HMAC& operator=(const HMAC&) = delete; + + ~HMAC() { delete hash; } + private: + void add_data(const byte[], size_t); + void final_result(byte[]); + void key_schedule(const byte[], size_t); + + HashFunction* hash; + secure_vector<byte> i_key, o_key; + }; + +} + +#endif diff --git a/src/lib/mac/hmac/info.txt b/src/lib/mac/hmac/info.txt new file mode 100644 index 000000000..7bc8b27f8 --- /dev/null +++ b/src/lib/mac/hmac/info.txt @@ -0,0 +1,5 @@ +define HMAC 20131128 + +<requires> +hash +</requires> |