Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:54

0001 // cbcmac.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file

0004 /// \brief Classes for CBC MAC

0005 /// \since Crypto++ 3.1

0006 
0007 #ifndef CRYPTOPP_CBCMAC_H
0008 #define CRYPTOPP_CBCMAC_H
0009 
0010 #include "seckey.h"
0011 #include "secblock.h"
0012 
0013 NAMESPACE_BEGIN(CryptoPP)
0014 
0015 /// \brief CBC-MAC base class

0016 /// \since Crypto++ 3.1

0017 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_MAC_Base : public MessageAuthenticationCode
0018 {
0019 public:
0020     CBC_MAC_Base() : m_counter(0) {}
0021 
0022     void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
0023     void Update(const byte *input, size_t length);
0024     void TruncatedFinal(byte *mac, size_t size);
0025     unsigned int DigestSize() const {return const_cast<CBC_MAC_Base*>(this)->AccessCipher().BlockSize();}
0026 
0027 protected:
0028     virtual BlockCipher & AccessCipher() =0;
0029 
0030 private:
0031     void ProcessBuf();
0032     SecByteBlock m_reg;
0033     unsigned int m_counter;
0034 };
0035 
0036 /// \brief CBC-MAC

0037 /// \tparam T BlockCipherDocumentation derived class

0038 /// \details CBC-MAC is compatible with FIPS 113. The MAC is secure only for fixed

0039 ///   length messages. For variable length messages use CMAC or DMAC.

0040 /// \sa <a href="http://www.weidai.com/scan-mirror/mac.html#CBC-MAC">CBC-MAC</a>

0041 /// \since Crypto++ 3.1

0042 template <class T>
0043 class CBC_MAC : public MessageAuthenticationCodeImpl<CBC_MAC_Base, CBC_MAC<T> >, public SameKeyLengthAs<T>
0044 {
0045 public:
0046     /// \brief Construct a CBC_MAC

0047     CBC_MAC() {}
0048     /// \brief Construct a CBC_MAC

0049     /// \param key a byte buffer used to key the cipher

0050     /// \param length the length of the byte buffer

0051     CBC_MAC(const byte *key, size_t length=SameKeyLengthAs<T>::DEFAULT_KEYLENGTH)
0052         {this->SetKey(key, length);}
0053 
0054     static std::string StaticAlgorithmName() {return std::string("CBC-MAC(") + T::StaticAlgorithmName() + ")";}
0055 
0056 private:
0057     BlockCipher & AccessCipher() {return m_cipher;}
0058     typename T::Encryption m_cipher;
0059 };
0060 
0061 NAMESPACE_END
0062 
0063 #endif