Crypto++ 8.7
Free C++ class library of cryptographic schemes
cbcmac.h
Go to the documentation of this file.
1// cbcmac.h - originally written and placed in the public domain by Wei Dai
2
3/// \file
4/// \brief Classes for CBC MAC
5/// \since Crypto++ 3.1
6
7#ifndef CRYPTOPP_CBCMAC_H
8#define CRYPTOPP_CBCMAC_H
9
10#include "seckey.h"
11#include "secblock.h"
12
13NAMESPACE_BEGIN(CryptoPP)
14
15/// \brief CBC-MAC base class
16/// \since Crypto++ 3.1
17class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_MAC_Base : public MessageAuthenticationCode
18{
19public:
20 CBC_MAC_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 const_cast<CBC_MAC_Base*>(this)->AccessCipher().BlockSize();}
26
27protected:
28 virtual BlockCipher & AccessCipher() =0;
29
30private:
31 void ProcessBuf();
32 SecByteBlock m_reg;
33 unsigned int m_counter;
34};
35
36/// \brief CBC-MAC
37/// \tparam T BlockCipherDocumentation derived class
38/// \details CBC-MAC is compatible with FIPS 113. The MAC is secure only for fixed
39/// length messages. For variable length messages use CMAC or DMAC.
40/// \sa <a href="http://www.weidai.com/scan-mirror/mac.html#CBC-MAC">CBC-MAC</a>
41/// \since Crypto++ 3.1
42template <class T>
43class CBC_MAC : public MessageAuthenticationCodeImpl<CBC_MAC_Base, CBC_MAC<T> >, public SameKeyLengthAs<T>
44{
45public:
46 /// \brief Construct a CBC_MAC
48 /// \brief Construct a CBC_MAC
49 /// \param key a byte buffer used to key the cipher
50 /// \param length the length of the byte buffer
51 CBC_MAC(const byte *key, size_t length=SameKeyLengthAs<T>::DEFAULT_KEYLENGTH)
52 {this->SetKey(key, length);}
53
54 static std::string StaticAlgorithmName() {return std::string("CBC-MAC(") + T::StaticAlgorithmName() + ")";}
55
56private:
57 BlockCipher & AccessCipher() {return m_cipher;}
58 typename T::Encryption m_cipher;
59};
60
61NAMESPACE_END
62
63#endif
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1283
CBC-MAC base class.
Definition: cbcmac.h:18
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: cbcmac.h:25
void TruncatedFinal(byte *mac, size_t size)
Computes the hash of the current message.
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
void Update(const byte *input, size_t length)
Updates a hash with additional input.
CBC-MAC.
Definition: cbcmac.h:44
CBC_MAC()
Construct a CBC_MAC.
Definition: cbcmac.h:47
CBC_MAC(const byte *key, size_t length=SameKeyLengthAs< T >::DEFAULT_KEYLENGTH)
Construct a CBC_MAC.
Definition: cbcmac.h:51
virtual unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: cryptlib.h:1165
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.