Crypto++  5.6.5
Free C++ class library of cryptographic schemes
cmac.h
Go to the documentation of this file.
1 // cmac.h - written and placed in the public domain by Wei Dai
2 
3 //! \file cmac.h
4 //! \brief Classes for CMAC message authentication code
5 //! \since Crypto++ 5.6.0
6 
7 #ifndef CRYPTOPP_CMAC_H
8 #define CRYPTOPP_CMAC_H
9 
10 #include "seckey.h"
11 #include "secblock.h"
12 
13 NAMESPACE_BEGIN(CryptoPP)
14 
15 //! \class CMAC_Base
16 //! \brief CMAC base implementation
17 //! \since Crypto++ 5.6.0
18 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CMAC_Base : public MessageAuthenticationCode
19 {
20 public:
21  CMAC_Base() : m_counter(0) {}
22 
23  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
24  void Update(const byte *input, size_t length);
25  void TruncatedFinal(byte *mac, size_t size);
26  unsigned int DigestSize() const {return GetCipher().BlockSize();}
27  unsigned int OptimalBlockSize() const {return GetCipher().BlockSize();}
28  unsigned int OptimalDataAlignment() const {return GetCipher().OptimalDataAlignment();}
29 
30 protected:
31  friend class EAX_Base;
32 
33  const BlockCipher & GetCipher() const {return const_cast<CMAC_Base*>(this)->AccessCipher();}
34  virtual BlockCipher & AccessCipher() =0;
35 
36  void ProcessBuf();
37  SecByteBlock m_reg;
38  unsigned int m_counter;
39 };
40 
41 //! \brief CMAC message authentication code
42 //! \tparam T block cipher
43 //! \details Template parameter T should be a class derived from BlockCipherDocumentation, for example AES, with a block size of 8, 16, or 32.
44 //! \sa <a href="http://www.cryptolounge.org/wiki/CMAC">CMAC</a>
45 //! \since Crypto++ 5.6.0
46 template <class T>
47 class CMAC : public MessageAuthenticationCodeImpl<CMAC_Base, CMAC<T> >, public SameKeyLengthAs<T>
48 {
49 public:
50  //! \brief Construct a CMAC
51  CMAC() {}
52  //! \brief Construct a CMAC
53  //! \param key the MAC key
54  //! \param length the key size, in bytes
55  CMAC(const byte *key, size_t length=SameKeyLengthAs<T>::DEFAULT_KEYLENGTH)
56  {this->SetKey(key, length);}
57 
58  static std::string StaticAlgorithmName() {return std::string("CMAC(") + T::StaticAlgorithmName() + ")";}
59 
60 private:
61  BlockCipher & AccessCipher() {return m_cipher;}
62  typename T::Encryption m_cipher;
63 };
64 
65 NAMESPACE_END
66 
67 #endif
Interface for message authentication codes.
Definition: cryptlib.h:1111
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
Sets or reset the key of this object.
Definition: cryptlib.cpp:97
EAX block cipher base implementation.
Definition: eax.h:19
SecBlock typedef.
Definition: secblock.h:731
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1095
CMAC message authentication code.
Definition: cmac.h:47
Classes and functions for secure memory allocations.
CMAC()
Construct a CMAC.
Definition: cmac.h:51
Classes and functions for implementing secret key algorithms.
Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication code...
Definition: seckey.h:370
Provides key lengths based on another class's key length.
Definition: seckey.h:222
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this hash.
Definition: cmac.h:27
CMAC(const byte *key, size_t length=SameKeyLengthAs< T >::DEFAULT_KEYLENGTH)
Construct a CMAC.
Definition: cmac.h:55
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: cmac.h:28
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: cmac.h:26
CMAC base implementation.
Definition: cmac.h:18
Crypto++ library namespace.
Interface for retrieving values given their names.
Definition: cryptlib.h:279