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.

56 lines
1.5 KiB

  1. // cbcmac.h - written and placed in the public domain by Wei Dai
  2. //! \file
  3. //! \headerfile cbcmac.h
  4. //! \brief Classes for CBC MAC
  5. #ifndef CRYPTOPP_CBCMAC_H
  6. #define CRYPTOPP_CBCMAC_H
  7. #include "seckey.h"
  8. #include "secblock.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. //! _
  11. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_MAC_Base : public MessageAuthenticationCode
  12. {
  13. public:
  14. CBC_MAC_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 const_cast<CBC_MAC_Base*>(this)->AccessCipher().BlockSize();}
  19. protected:
  20. virtual BlockCipher & AccessCipher() =0;
  21. private:
  22. void ProcessBuf();
  23. SecByteBlock m_reg;
  24. unsigned int m_counter;
  25. };
  26. //! <a href="http://www.weidai.com/scan-mirror/mac.html#CBC-MAC">CBC-MAC</a>
  27. /*! Compatible with FIPS 113. T should be a class derived from BlockCipherDocumentation.
  28. Secure only for fixed length messages. For variable length messages use CMAC or DMAC.
  29. */
  30. template <class T>
  31. class CBC_MAC : public MessageAuthenticationCodeImpl<CBC_MAC_Base, CBC_MAC<T> >, public SameKeyLengthAs<T>
  32. {
  33. public:
  34. CBC_MAC() {}
  35. CBC_MAC(const byte *key, size_t length=SameKeyLengthAs<T>::DEFAULT_KEYLENGTH)
  36. {this->SetKey(key, length);}
  37. static std::string StaticAlgorithmName() {return std::string("CBC-MAC(") + T::StaticAlgorithmName() + ")";}
  38. private:
  39. BlockCipher & AccessCipher() {return m_cipher;}
  40. typename T::Encryption m_cipher;
  41. };
  42. NAMESPACE_END
  43. #endif