Crypto++ 8.7
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/// \brief Enable CMAC and wide block ciphers
14/// \details CMAC is only defined for AES. The library can support wide
15/// block ciphers like Kaylna and Threefish since we know the polynomials.
16#ifndef CRYPTOPP_CMAC_WIDE_BLOCK_CIPHERS
17# define CRYPTOPP_CMAC_WIDE_BLOCK_CIPHERS 1
18#endif // CRYPTOPP_CMAC_WIDE_BLOCK_CIPHERS
19
20NAMESPACE_BEGIN(CryptoPP)
21
22/// \brief CMAC base implementation
23/// \since Crypto++ 5.6.0
24class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CMAC_Base : public MessageAuthenticationCode
25{
26public:
27
28 virtual ~CMAC_Base() {}
29 CMAC_Base() : m_counter(0) {}
30
31 void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
32 void Update(const byte *input, size_t length);
33 void TruncatedFinal(byte *mac, size_t size);
34 unsigned int DigestSize() const {return GetCipher().BlockSize();}
35 unsigned int OptimalBlockSize() const {return GetCipher().BlockSize();}
36 unsigned int OptimalDataAlignment() const {return GetCipher().OptimalDataAlignment();}
37 std::string AlgorithmProvider() const {return GetCipher().AlgorithmProvider();}
38
39protected:
40 friend class EAX_Base;
41
42 const BlockCipher & GetCipher() const {return const_cast<CMAC_Base*>(this)->AccessCipher();}
43 virtual BlockCipher & AccessCipher() =0;
44
45 void ProcessBuf();
46 SecByteBlock m_reg;
47 unsigned int m_counter;
48};
49
50/// \brief CMAC message authentication code
51/// \tparam T block cipher
52/// \details Template parameter T should be a class derived from BlockCipherDocumentation, for example AES, with a block size of 8, 16, or 32.
53/// \sa <a href="http://www.cryptolounge.org/wiki/CMAC">CMAC</a>
54/// \since Crypto++ 5.6.0
55template <class T>
56class CMAC : public MessageAuthenticationCodeImpl<CMAC_Base, CMAC<T> >, public SameKeyLengthAs<T>
57{
58public:
59 /// \brief Construct a CMAC
60 CMAC() {}
61 /// \brief Construct a CMAC
62 /// \param key the MAC key
63 /// \param length the key size, in bytes
64 CMAC(const byte *key, size_t length=SameKeyLengthAs<T>::DEFAULT_KEYLENGTH)
65 {this->SetKey(key, length);}
66
67 static std::string StaticAlgorithmName() {return std::string("CMAC(") + T::StaticAlgorithmName() + ")";}
68
69private:
70 BlockCipher & AccessCipher() {return m_cipher;}
71 typename T::Encryption m_cipher;
72};
73
74NAMESPACE_END
75
76#endif
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1283
CMAC base implementation.
Definition: cmac.h:25
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: cmac.h:37
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: cmac.h:36
void Update(const byte *input, size_t length)
Updates a hash with additional input.
void TruncatedFinal(byte *mac, size_t size)
Computes the hash of the current message.
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this hash.
Definition: cmac.h:35
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: cmac.h:34
CMAC message authentication code.
Definition: cmac.h:57
CMAC()
Construct a CMAC.
Definition: cmac.h:60
CMAC(const byte *key, size_t length=SameKeyLengthAs< T >::DEFAULT_KEYLENGTH)
Construct a CMAC.
Definition: cmac.h:64
EAX block cipher base implementation.
Definition: eax.h:19
Interface for message authentication codes.
Definition: cryptlib.h:1299
Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication code...
Definition: seckey.h:363
Interface for retrieving values given their names.
Definition: cryptlib.h:322
Provides key lengths based on another class's key length.
Definition: seckey.h:218
SecBlock<byte> typedef.
Definition: secblock.h:1226
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
Sets or reset the key of this object.
Crypto++ library namespace.
Classes and functions for secure memory allocations.
Classes and functions for implementing secret key algorithms.