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