Crypto++  5.6.3
Free C++ class library of cryptographic schemes
cbcmac.h
Go to the documentation of this file.
1 // cbcmac.h - written and placed in the public domain by Wei Dai
2 
3 //! \file
4 //! \headerfile cbcmac.h
5 //! \brief Classes for CBC MAC
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 //! _
16 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_MAC_Base : public MessageAuthenticationCode
17 {
18 public:
19  CBC_MAC_Base() : m_counter(0) {}
20 
21  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
22  void Update(const byte *input, size_t length);
23  void TruncatedFinal(byte *mac, size_t size);
24  unsigned int DigestSize() const {return const_cast<CBC_MAC_Base*>(this)->AccessCipher().BlockSize();}
25 
26 protected:
27  virtual BlockCipher & AccessCipher() =0;
28 
29 private:
30  void ProcessBuf();
31  SecByteBlock m_reg;
32  unsigned int m_counter;
33 };
34 
35 //! <a href="http://www.weidai.com/scan-mirror/mac.html#CBC-MAC">CBC-MAC</a>
36 /*! Compatible with FIPS 113. T should be a class derived from BlockCipherDocumentation.
37  Secure only for fixed length messages. For variable length messages use CMAC or DMAC.
38 */
39 template <class T>
40 class CBC_MAC : public MessageAuthenticationCodeImpl<CBC_MAC_Base, CBC_MAC<T> >, public SameKeyLengthAs<T>
41 {
42 public:
43  CBC_MAC() {}
44  CBC_MAC(const byte *key, size_t length=SameKeyLengthAs<T>::DEFAULT_KEYLENGTH)
45  {this->SetKey(key, length);}
46 
47  static std::string StaticAlgorithmName() {return std::string("CBC-MAC(") + T::StaticAlgorithmName() + ")";}
48 
49 private:
50  BlockCipher & AccessCipher() {return m_cipher;}
51  typename T::Encryption m_cipher;
52 };
53 
54 NAMESPACE_END
55 
56 #endif
Interface for message authentication codes.
Definition: cryptlib.h:1107
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:100
SecBlock typedef.
Definition: secblock.h:734
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: cbcmac.h:24
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1091
Classes and functions for secure memory allocations.
Classes and functions for implementing secret key algorithms.
Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication code...
Definition: seckey.h:358
virtual unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: cryptlib.h:975
Provides key lengths based on another class's key length.
Definition: seckey.h:219
Crypto++ library namespace.
CBC-MAC
Definition: cbcmac.h:40
Interface for retrieving values given their names.
Definition: cryptlib.h:277