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