Crypto++ 8.1 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 CBC_MAC() {}
47 CBC_MAC(const byte *key, size_t length=SameKeyLengthAs<T>::DEFAULT_KEYLENGTH)
48 {this->SetKey(key, length);}
49
50 static std::string StaticAlgorithmName() {return std::string("CBC-MAC(") + T::StaticAlgorithmName() + ")";}
51
52 private:
53 BlockCipher & AccessCipher() {return m_cipher;}
54 typename T::Encryption m_cipher;
55 };
56
57 NAMESPACE_END
58
59 #endif
HashTransformation::BlockSize
virtual unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: cryptlib.h:1137
MessageAuthenticationCode
Interface for message authentication codes.
Definition: cryptlib.h:1266
SimpleKeyingInterface::SetKey
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:58
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:1058
CBC_MAC_Base
CBC-MAC base class.
Definition: cbcmac.h:17
BlockCipher
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1250
secblock.h
Classes and functions for secure memory allocations.
seckey.h
Classes and functions for implementing secret key algorithms.
MessageAuthenticationCodeImpl
Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication code...
Definition: seckey.h:362
SameKeyLengthAs
Provides key lengths based on another class&#39;s key length.
Definition: seckey.h:217
CBC_MAC_Base::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: cbcmac.h:25
CryptoPP
Crypto++ library namespace.
CBC_MAC
CBC-MAC.
Definition: cbcmac.h:43
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:293