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.

58 lines
1.7 KiB

  1. // cmac.h - written and placed in the public domain by Wei Dai
  2. //! \file
  3. //! \headerfile cmac.h
  4. //! \brief Classes for CMAC message authentication code
  5. #ifndef CRYPTOPP_CMAC_H
  6. #define CRYPTOPP_CMAC_H
  7. #include "seckey.h"
  8. #include "secblock.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. //! _
  11. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CMAC_Base : public MessageAuthenticationCode
  12. {
  13. public:
  14. CMAC_Base() : m_counter(0) {}
  15. void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
  16. void Update(const byte *input, size_t length);
  17. void TruncatedFinal(byte *mac, size_t size);
  18. unsigned int DigestSize() const {return GetCipher().BlockSize();}
  19. unsigned int OptimalBlockSize() const {return GetCipher().BlockSize();}
  20. unsigned int OptimalDataAlignment() const {return GetCipher().OptimalDataAlignment();}
  21. protected:
  22. friend class EAX_Base;
  23. const BlockCipher & GetCipher() const {return const_cast<CMAC_Base*>(this)->AccessCipher();}
  24. virtual BlockCipher & AccessCipher() =0;
  25. void ProcessBuf();
  26. SecByteBlock m_reg;
  27. unsigned int m_counter;
  28. };
  29. /// <a href="http://www.cryptolounge.org/wiki/CMAC">CMAC</a>
  30. /*! Template parameter T should be a class derived from BlockCipherDocumentation, for example AES, with a block size of 8, 16, or 32 */
  31. template <class T>
  32. class CMAC : public MessageAuthenticationCodeImpl<CMAC_Base, CMAC<T> >, public SameKeyLengthAs<T>
  33. {
  34. public:
  35. CMAC() {}
  36. CMAC(const byte *key, size_t length=SameKeyLengthAs<T>::DEFAULT_KEYLENGTH)
  37. {this->SetKey(key, length);}
  38. static std::string StaticAlgorithmName() {return std::string("CMAC(") + T::StaticAlgorithmName() + ")";}
  39. private:
  40. BlockCipher & AccessCipher() {return m_cipher;}
  41. typename T::Encryption m_cipher;
  42. };
  43. NAMESPACE_END
  44. #endif