cbcmac.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // cbcmac.h - originally written and placed in the public domain by Wei Dai
  2. /// \file
  3. /// \brief Classes for CBC MAC
  4. /// \since Crypto++ 3.1
  5. #ifndef CRYPTOPP_CBCMAC_H
  6. #define CRYPTOPP_CBCMAC_H
  7. #include "seckey.h"
  8. #include "secblock.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. /// \brief CBC-MAC base class
  11. /// \since Crypto++ 3.1
  12. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_MAC_Base : public MessageAuthenticationCode
  13. {
  14. public:
  15. CBC_MAC_Base() : m_counter(0) {}
  16. void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
  17. void Update(const byte *input, size_t length);
  18. void TruncatedFinal(byte *mac, size_t size);
  19. unsigned int DigestSize() const {return const_cast<CBC_MAC_Base*>(this)->AccessCipher().BlockSize();}
  20. protected:
  21. virtual BlockCipher & AccessCipher() =0;
  22. private:
  23. void ProcessBuf();
  24. SecByteBlock m_reg;
  25. unsigned int m_counter;
  26. };
  27. /// \brief CBC-MAC
  28. /// \tparam T BlockCipherDocumentation derived class
  29. /// \details CBC-MAC is compatible with FIPS 113. The MAC is secure only for fixed
  30. /// length messages. For variable length messages use CMAC or DMAC.
  31. /// \sa <a href="http://www.weidai.com/scan-mirror/mac.html#CBC-MAC">CBC-MAC</a>
  32. /// \since Crypto++ 3.1
  33. template <class T>
  34. class CBC_MAC : public MessageAuthenticationCodeImpl<CBC_MAC_Base, CBC_MAC<T> >, public SameKeyLengthAs<T>
  35. {
  36. public:
  37. /// \brief Construct a CBC_MAC
  38. CBC_MAC() {}
  39. /// \brief Construct a CBC_MAC
  40. /// \param key a byte buffer used to key the cipher
  41. /// \param length the length of the byte buffer
  42. CBC_MAC(const byte *key, size_t length=SameKeyLengthAs<T>::DEFAULT_KEYLENGTH)
  43. {this->SetKey(key, length);}
  44. static std::string StaticAlgorithmName() {return std::string("CBC-MAC(") + T::StaticAlgorithmName() + ")";}
  45. private:
  46. BlockCipher & AccessCipher() {return m_cipher;}
  47. typename T::Encryption m_cipher;
  48. };
  49. NAMESPACE_END
  50. #endif