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