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