hmac.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // hmac.h - originally written and placed in the public domain by Wei Dai
  2. /// \file hmac.h
  3. /// \brief Classes for HMAC message authentication codes
  4. #ifndef CRYPTOPP_HMAC_H
  5. #define CRYPTOPP_HMAC_H
  6. #include "seckey.h"
  7. #include "secblock.h"
  8. NAMESPACE_BEGIN(CryptoPP)
  9. /// \brief HMAC information
  10. /// \details HMAC_Base derives from VariableKeyLength and MessageAuthenticationCode
  11. /// \since Crypto++ 2.1
  12. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, INT_MAX>, public MessageAuthenticationCode
  13. {
  14. public:
  15. virtual ~HMAC_Base() {}
  16. /// \brief Construct a HMAC_Base
  17. HMAC_Base() : m_innerHashKeyed(false) {}
  18. void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
  19. void Restart();
  20. void Update(const byte *input, size_t length);
  21. void TruncatedFinal(byte *mac, size_t size);
  22. unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();}
  23. unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();}
  24. protected:
  25. virtual HashTransformation & AccessHash() =0;
  26. byte * AccessIpad() {return m_buf;}
  27. byte * AccessOpad() {return m_buf + AccessHash().BlockSize();}
  28. byte * AccessInnerHash() {return m_buf + 2*AccessHash().BlockSize();}
  29. private:
  30. void KeyInnerHash();
  31. SecByteBlock m_buf;
  32. bool m_innerHashKeyed;
  33. };
  34. /// \brief HMAC
  35. /// \tparam T HashTransformation derived class
  36. /// \details HMAC derives from MessageAuthenticationCodeImpl. It calculates the HMAC using
  37. /// <tt>HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text))</tt>.
  38. /// \sa <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a>
  39. /// \since Crypto++ 2.1
  40. template <class T>
  41. class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> >
  42. {
  43. public:
  44. CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE);
  45. CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE);
  46. virtual ~HMAC() {}
  47. /// \brief Construct a HMAC
  48. HMAC() {}
  49. /// \brief Construct a HMAC
  50. /// \param key the HMAC key
  51. /// \param length the size of the HMAC key
  52. HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
  53. {this->SetKey(key, length);}
  54. static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
  55. std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";}
  56. std::string AlgorithmProvider() const {return m_hash.AlgorithmProvider();}
  57. private:
  58. HashTransformation & AccessHash() {return m_hash;}
  59. T m_hash;
  60. };
  61. NAMESPACE_END
  62. #endif