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.

86 lines
1.8 KiB

  1. // hmac.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_IMPORTS
  4. #include "hmac.h"
  5. NAMESPACE_BEGIN(CryptoPP)
  6. void HMAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
  7. {
  8. AssertValidKeyLength(keylength);
  9. Restart();
  10. HashTransformation &hash = AccessHash();
  11. unsigned int blockSize = hash.BlockSize();
  12. if (!blockSize)
  13. throw InvalidArgument("HMAC: can only be used with a block-based hash function");
  14. m_buf.resize(2*AccessHash().BlockSize() + AccessHash().DigestSize());
  15. if (keylength <= blockSize)
  16. memcpy(AccessIpad(), userKey, keylength);
  17. else
  18. {
  19. AccessHash().CalculateDigest(AccessIpad(), userKey, keylength);
  20. keylength = hash.DigestSize();
  21. }
  22. assert(keylength <= blockSize);
  23. memset(AccessIpad()+keylength, 0, blockSize-keylength);
  24. for (unsigned int i=0; i<blockSize; i++)
  25. {
  26. AccessOpad()[i] = AccessIpad()[i] ^ 0x5c;
  27. AccessIpad()[i] ^= 0x36;
  28. }
  29. }
  30. void HMAC_Base::KeyInnerHash()
  31. {
  32. assert(!m_innerHashKeyed);
  33. HashTransformation &hash = AccessHash();
  34. hash.Update(AccessIpad(), hash.BlockSize());
  35. m_innerHashKeyed = true;
  36. }
  37. void HMAC_Base::Restart()
  38. {
  39. if (m_innerHashKeyed)
  40. {
  41. AccessHash().Restart();
  42. m_innerHashKeyed = false;
  43. }
  44. }
  45. void HMAC_Base::Update(const byte *input, size_t length)
  46. {
  47. if (!m_innerHashKeyed)
  48. KeyInnerHash();
  49. AccessHash().Update(input, length);
  50. }
  51. void HMAC_Base::TruncatedFinal(byte *mac, size_t size)
  52. {
  53. ThrowIfInvalidTruncatedSize(size);
  54. HashTransformation &hash = AccessHash();
  55. if (!m_innerHashKeyed)
  56. KeyInnerHash();
  57. hash.Final(AccessInnerHash());
  58. hash.Update(AccessOpad(), hash.BlockSize());
  59. hash.Update(AccessInnerHash(), hash.DigestSize());
  60. hash.TruncatedFinal(mac, size);
  61. m_innerHashKeyed = false;
  62. }
  63. NAMESPACE_END
  64. #endif