Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.9 KiB

  1. // hmac.h - written and placed in the public domain by Wei Dai
  2. //! \file
  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. //! _
  10. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, INT_MAX>, public MessageAuthenticationCode
  11. {
  12. public:
  13. HMAC_Base() : m_innerHashKeyed(false) {}
  14. void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
  15. void Restart();
  16. void Update(const byte *input, size_t length);
  17. void TruncatedFinal(byte *mac, size_t size);
  18. unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();}
  19. unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();}
  20. protected:
  21. virtual HashTransformation & AccessHash() =0;
  22. byte * AccessIpad() {return m_buf;}
  23. byte * AccessOpad() {return m_buf + AccessHash().BlockSize();}
  24. byte * AccessInnerHash() {return m_buf + 2*AccessHash().BlockSize();}
  25. private:
  26. void KeyInnerHash();
  27. SecByteBlock m_buf;
  28. bool m_innerHashKeyed;
  29. };
  30. //! <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a>
  31. /*! HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text)) */
  32. template <class T>
  33. class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> >
  34. {
  35. public:
  36. CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE)
  37. CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE)
  38. HMAC() {}
  39. HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
  40. {this->SetKey(key, length);}
  41. static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
  42. std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";}
  43. private:
  44. HashTransformation & AccessHash() {return m_hash;}
  45. T m_hash;
  46. };
  47. NAMESPACE_END
  48. #endif