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