- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# hmac.h

    00001 // hmac.h - written and placed in the public domain by Wei Dai
    00002
    00003 #ifndef CRYPTOPP_HMAC_H
    00004 #define CRYPTOPP_HMAC_H
    00005
    00006 #include "seckey.h"
    00007 #include "secblock.h"
    00008
    00009 NAMESPACE_BEGIN(CryptoPP)
    00010
    00011 //! _
    00012 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, UINT_MAX>, public MessageAuthenticationCode
    00013 {
    00014 public:
    00015         HMAC_Base() : m_innerHashKeyed(false) {}
    00016         void UncheckedSetKey(const byte *userKey, unsigned int keylength);
    00017
    00018         void Restart();
    00019         void Update(const byte *input, size_t length);
    00020         void TruncatedFinal(byte *mac, size_t size);
    00021         unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();}
    00022         unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();}
    00023
    00024 protected:
    00025         virtual HashTransformation & AccessHash() =0;
    00026         virtual byte * AccessIpad() =0;
    00027         virtual byte * AccessOpad() =0;
    00028         virtual byte * AccessInnerHash() =0;
    00029
    00030 private:
    00031         void KeyInnerHash();
    00032
    00033         enum {IPAD=0x36, OPAD=0x5c};
    00034
    00035         bool m_innerHashKeyed;
    00036 };
    00037
    00038 //! <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a>
    00039 /*! HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text)) */
    00040 template <class T>
    00041 class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> >
    00042 {
    00043 public:
    00044         enum {DIGESTSIZE=T::DIGESTSIZE, BLOCKSIZE=T::BLOCKSIZE};
    00045
    00046         HMAC() {}
    00047         HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
    00048                 {this->SetKey(key, length);}
    00049
    00050         static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
    00051         std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";}
    00052
    00053 private:
    00054         HashTransformation & AccessHash() {return m_hash;}
    00055         byte * AccessIpad() {return m_ipad;}
    00056         byte * AccessOpad() {return m_opad;}
    00057         byte * AccessInnerHash() {return m_innerHash;}
    00058
    00059         FixedSizeSecBlock<byte, BLOCKSIZE> m_ipad, m_opad;
    00060         FixedSizeSecBlock<byte, DIGESTSIZE> m_innerHash;
    00061         T m_hash;
    00062 };
    00063
    00064 NAMESPACE_END
    00065
    00066 #endif