diff options
author | lloyd <[email protected]> | 2008-09-28 19:29:24 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-28 19:29:24 +0000 |
commit | 9bcfe627321ddc81691b835dffaa6324ac4684a4 (patch) | |
tree | fe5e8ae9813b853549558b59833022e87e83981b /src/mac/hmac | |
parent | 9822a701516396b7de4e41339faecd48ff8dc8ff (diff) |
Move all modules into src/ directory
Diffstat (limited to 'src/mac/hmac')
-rw-r--r-- | src/mac/hmac/hmac.cpp | 97 | ||||
-rw-r--r-- | src/mac/hmac/hmac.h | 34 | ||||
-rw-r--r-- | src/mac/hmac/modinfo.txt | 10 |
3 files changed, 141 insertions, 0 deletions
diff --git a/src/mac/hmac/hmac.cpp b/src/mac/hmac/hmac.cpp new file mode 100644 index 000000000..b8c76e8f6 --- /dev/null +++ b/src/mac/hmac/hmac.cpp @@ -0,0 +1,97 @@ +/************************************************* +* HMAC Source File * +* (C) 1999-2007 Jack Lloyd * +* 2007 Yves Jerschow * +*************************************************/ + +#include <botan/hmac.h> +#include <botan/lookup.h> +#include <botan/xor_buf.h> + +namespace Botan { + +/************************************************* +* Update a HMAC Calculation * +*************************************************/ +void HMAC::add_data(const byte input[], u32bit 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(const byte key[], u32bit length) + { + hash->clear(); + std::fill(i_key.begin(), i_key.end(), 0x36); + std::fill(o_key.begin(), o_key.end(), 0x5C); + + if(length > hash->HASH_BLOCK_SIZE) + { + SecureVector<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() throw() + { + hash->clear(); + i_key.clear(); + o_key.clear(); + } + +/************************************************* +* 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->name()); + } + +/************************************************* +* HMAC Constructor * +*************************************************/ +HMAC::HMAC(const std::string& hash_name) : + MessageAuthenticationCode(output_length_of(hash_name), + 1, 2*block_size_of(hash_name)), + hash(get_hash(hash_name)) + { + if(hash->HASH_BLOCK_SIZE == 0) + throw Invalid_Argument("HMAC cannot be used with " + hash->name()); + i_key.create(hash->HASH_BLOCK_SIZE); + o_key.create(hash->HASH_BLOCK_SIZE); + } + +} diff --git a/src/mac/hmac/hmac.h b/src/mac/hmac/hmac.h new file mode 100644 index 000000000..62529cf13 --- /dev/null +++ b/src/mac/hmac/hmac.h @@ -0,0 +1,34 @@ +/************************************************* +* HMAC Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_HMAC_H__ +#define BOTAN_HMAC_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* HMAC * +*************************************************/ +class BOTAN_DLL HMAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const; + MessageAuthenticationCode* clone() const; + HMAC(const std::string&); + ~HMAC() { delete hash; } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key(const byte[], u32bit); + HashFunction* hash; + SecureVector<byte> i_key, o_key; + }; + +} + +#endif diff --git a/src/mac/hmac/modinfo.txt b/src/mac/hmac/modinfo.txt new file mode 100644 index 000000000..534d2e036 --- /dev/null +++ b/src/mac/hmac/modinfo.txt @@ -0,0 +1,10 @@ +realname "HMAC" + +define HMAC + +load_on auto + +<add> +hmac.cpp +hmac.h +</add> |