00001
00002
00003
00004
00005
00006 #ifndef CRYPTOPP_HMAC_H
00007 #define CRYPTOPP_HMAC_H
00008
00009 #include "seckey.h"
00010 #include "secblock.h"
00011
00012 NAMESPACE_BEGIN(CryptoPP)
00013
00014
00015 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, INT_MAX>, public MessageAuthenticationCode
00016 {
00017 public:
00018 HMAC_Base() : m_innerHashKeyed(false) {}
00019 void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs ¶ms);
00020
00021 void Restart();
00022 void Update(const byte *input, size_t length);
00023 void TruncatedFinal(byte *mac, size_t size);
00024 unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();}
00025 unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();}
00026
00027 protected:
00028 virtual HashTransformation & AccessHash() =0;
00029 byte * AccessIpad() {return m_buf;}
00030 byte * AccessOpad() {return m_buf + AccessHash().BlockSize();}
00031 byte * AccessInnerHash() {return m_buf + 2*AccessHash().BlockSize();}
00032
00033 private:
00034 void KeyInnerHash();
00035
00036 SecByteBlock m_buf;
00037 bool m_innerHashKeyed;
00038 };
00039
00040
00041
00042 template <class T>
00043 class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> >
00044 {
00045 public:
00046 CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE)
00047 CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE)
00048
00049 HMAC() {}
00050 HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
00051 {this->SetKey(key, length);}
00052
00053 static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
00054 std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";}
00055
00056 private:
00057 HashTransformation & AccessHash() {return m_hash;}
00058
00059 T m_hash;
00060 };
00061
00062 NAMESPACE_END
00063
00064 #endif