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