Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:03

0001 // hmac.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file hmac.h

0004 /// \brief Classes for HMAC message authentication codes

0005 
0006 #ifndef CRYPTOPP_HMAC_H
0007 #define CRYPTOPP_HMAC_H
0008 
0009 #include "seckey.h"
0010 #include "secblock.h"
0011 
0012 NAMESPACE_BEGIN(CryptoPP)
0013 
0014 /// \brief HMAC information

0015 /// \details HMAC_Base derives from VariableKeyLength and MessageAuthenticationCode

0016 /// \since Crypto++ 2.1

0017 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, INT_MAX>, public MessageAuthenticationCode
0018 {
0019 public:
0020     virtual ~HMAC_Base() {}
0021 
0022     /// \brief Construct a HMAC_Base

0023     HMAC_Base() : m_innerHashKeyed(false) {}
0024     void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
0025 
0026     void Restart();
0027     void Update(const byte *input, size_t length);
0028     void TruncatedFinal(byte *mac, size_t size);
0029     unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();}
0030     unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();}
0031 
0032 protected:
0033     virtual HashTransformation & AccessHash() =0;
0034     byte * AccessIpad() {return m_buf;}
0035     byte * AccessOpad() {return m_buf + AccessHash().BlockSize();}
0036     byte * AccessInnerHash() {return m_buf + 2*AccessHash().BlockSize();}
0037 
0038 private:
0039     void KeyInnerHash();
0040 
0041     SecByteBlock m_buf;
0042     bool m_innerHashKeyed;
0043 };
0044 
0045 /// \brief HMAC

0046 /// \tparam T HashTransformation derived class

0047 /// \details HMAC derives from MessageAuthenticationCodeImpl. It calculates the HMAC using

0048 ///   <tt>HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text))</tt>.

0049 /// \sa <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a>

0050 /// \since Crypto++ 2.1

0051 template <class T>
0052 class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> >
0053 {
0054 public:
0055     CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE);
0056     CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE);
0057 
0058     virtual ~HMAC() {}
0059 
0060     /// \brief Construct a HMAC

0061     HMAC() {}
0062     /// \brief Construct a HMAC

0063     /// \param key the HMAC key

0064     /// \param length the size of the HMAC key

0065     HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
0066         {this->SetKey(key, length);}
0067 
0068     static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
0069     std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";}
0070     std::string AlgorithmProvider() const {return m_hash.AlgorithmProvider();}
0071 
0072 private:
0073     HashTransformation & AccessHash() {return m_hash;}
0074 
0075     T m_hash;
0076 };
0077 
0078 NAMESPACE_END
0079 
0080 #endif