Crypto++ 8.6 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
13 NAMESPACE_BEGIN(CryptoPP)
14
15 /// \brief CBC-MAC base class
16 /// \since Crypto++ 3.1
17 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_MAC_Base : public MessageAuthenticationCode
18 {
19 public:
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
27 protected:
28 virtual BlockCipher & AccessCipher() =0;
29
30 private:
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
42 template <class T>
43 class CBC_MAC : public MessageAuthenticationCodeImpl<CBC_MAC_Base, CBC_MAC<T> >, public SameKeyLengthAs<T>
44 {
45 public:
46 /// \brief Construct a CBC_MAC
47 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
56 private:
57 BlockCipher & AccessCipher() {return m_cipher;}
58 typename T::Encryption m_cipher;
59 };
60
61 NAMESPACE_END
62
63 #endif
SimpleKeyingInterface::SetKey
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
Sets or reset the key of this object.
secblock.h
Classes and functions for secure memory allocations.
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:1226
CBC_MAC_Base
CBC-MAC base class.
Definition: cbcmac.h:17
SameKeyLengthAs
Provides key lengths based on another class's key length.
Definition: seckey.h:217
HashTransformation::BlockSize
virtual unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: cryptlib.h:1165
seckey.h
Classes and functions for implementing secret key algorithms.
CBC_MAC_Base::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: cbcmac.h:25
MessageAuthenticationCodeImpl
Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication code...
Definition: seckey.h:362
CryptoPP
Crypto++ library namespace.
CBC_MAC::CBC_MAC
CBC_MAC(const byte *key, size_t length=SameKeyLengthAs< T >::DEFAULT_KEYLENGTH)
Construct a CBC_MAC.
Definition: cbcmac.h:51
BlockCipher
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1282
CBC_MAC::CBC_MAC
CBC_MAC()
Construct a CBC_MAC.
Definition: cbcmac.h:47
MessageAuthenticationCode
Interface for message authentication codes.
Definition: cryptlib.h:1298
CBC_MAC
CBC-MAC.
Definition: cbcmac.h:43
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:321