1#ifndef CRYPTOPP_CMAC_H 2#define CRYPTOPP_CMAC_H 3 4#include "seckey.h" 5#include "secblock.h" 6 7NAMESPACE_BEGIN(CryptoPP) 8 9//! _ 10class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CMAC_Base : public MessageAuthenticationCode 11{ 12public: 13 CMAC_Base() {} 14 15 void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms); 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 22protected: 23 friend class EAX_Base; 24 25 const BlockCipher & GetCipher() const {return const_cast<CMAC_Base*>(this)->AccessCipher();} 26 virtual BlockCipher & AccessCipher() =0; 27 28 void ProcessBuf(); 29 SecByteBlock m_reg; 30 unsigned int m_counter; 31}; 32 33/// <a href="http://www.cryptolounge.org/wiki/CMAC">CMAC</a> 34/*! Template parameter T should be a class derived from BlockCipherDocumentation, for example AES, with a block size of 8, 16, or 32 */ 35template <class T> 36class CMAC : public MessageAuthenticationCodeImpl<CMAC_Base, CMAC<T> >, public SameKeyLengthAs<T> 37{ 38public: 39 CMAC() {} 40 CMAC(const byte *key, size_t length=SameKeyLengthAs<T>::DEFAULT_KEYLENGTH) 41 {this->SetKey(key, length);} 42 43 static std::string StaticAlgorithmName() {return std::string("CMAC(") + T::StaticAlgorithmName() + ")";} 44 45private: 46 BlockCipher & AccessCipher() {return m_cipher;} 47 typename T::Encryption m_cipher; 48}; 49 50NAMESPACE_END 51 52#endif 53