File indexing completed on 2025-01-18 09:55:03
0001
0002
0003
0004
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
0015
0016
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
0023 HMAC_Base() : m_innerHashKeyed(false) {}
0024 void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs ¶ms);
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
0046
0047
0048
0049
0050
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
0061 HMAC() {}
0062
0063
0064
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