Back to home page

EIC code displayed by LXR

 
 

    


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

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

0002 
0003 /// \file cmac.h

0004 /// \brief Classes for CMAC message authentication code

0005 /// \since Crypto++ 5.6.0

0006 
0007 #ifndef CRYPTOPP_CMAC_H
0008 #define CRYPTOPP_CMAC_H
0009 
0010 #include "seckey.h"
0011 #include "secblock.h"
0012 
0013 /// \brief Enable CMAC and wide block ciphers

0014 /// \details CMAC is only defined for AES. The library can support wide

0015 ///  block ciphers like Kaylna and Threefish since we know the polynomials.

0016 #ifndef CRYPTOPP_CMAC_WIDE_BLOCK_CIPHERS
0017 # define CRYPTOPP_CMAC_WIDE_BLOCK_CIPHERS 1
0018 #endif  // CRYPTOPP_CMAC_WIDE_BLOCK_CIPHERS

0019 
0020 NAMESPACE_BEGIN(CryptoPP)
0021 
0022 /// \brief CMAC base implementation

0023 /// \since Crypto++ 5.6.0

0024 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CMAC_Base : public MessageAuthenticationCode
0025 {
0026 public:
0027 
0028     virtual ~CMAC_Base() {}
0029     CMAC_Base() : m_counter(0) {}
0030 
0031     void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
0032     void Update(const byte *input, size_t length);
0033     void TruncatedFinal(byte *mac, size_t size);
0034     unsigned int DigestSize() const {return GetCipher().BlockSize();}
0035     unsigned int OptimalBlockSize() const {return GetCipher().BlockSize();}
0036     unsigned int OptimalDataAlignment() const {return GetCipher().OptimalDataAlignment();}
0037     std::string AlgorithmProvider() const {return GetCipher().AlgorithmProvider();}
0038 
0039 protected:
0040     friend class EAX_Base;
0041 
0042     const BlockCipher & GetCipher() const {return const_cast<CMAC_Base*>(this)->AccessCipher();}
0043     virtual BlockCipher & AccessCipher() =0;
0044 
0045     void ProcessBuf();
0046     SecByteBlock m_reg;
0047     unsigned int m_counter;
0048 };
0049 
0050 /// \brief CMAC message authentication code

0051 /// \tparam T block cipher

0052 /// \details Template parameter T should be a class derived from BlockCipherDocumentation, for example AES, with a block size of 8, 16, or 32.

0053 /// \sa <a href="http://www.cryptolounge.org/wiki/CMAC">CMAC</a>

0054 /// \since Crypto++ 5.6.0

0055 template <class T>
0056 class CMAC : public MessageAuthenticationCodeImpl<CMAC_Base, CMAC<T> >, public SameKeyLengthAs<T>
0057 {
0058 public:
0059     /// \brief Construct a CMAC

0060     CMAC() {}
0061     /// \brief Construct a CMAC

0062     /// \param key the MAC key

0063     /// \param length the key size, in bytes

0064     CMAC(const byte *key, size_t length=SameKeyLengthAs<T>::DEFAULT_KEYLENGTH)
0065         {this->SetKey(key, length);}
0066 
0067     static std::string StaticAlgorithmName() {return std::string("CMAC(") + T::StaticAlgorithmName() + ")";}
0068 
0069 private:
0070     BlockCipher & AccessCipher() {return m_cipher;}
0071     typename T::Encryption m_cipher;
0072 };
0073 
0074 NAMESPACE_END
0075 
0076 #endif