Counter Strike : Global Offensive Source Code
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.

62 lines
1.9 KiB

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