Crypto++  7.0
Free C++ class library of cryptographic schemes
dmac.h
Go to the documentation of this file.
1 // dmac.h - originally written and placed in the public domain by Wei Dai
2 
3 /// \file dmac.h
4 /// \brief Classes for DMAC message authentication code
5 
6 #ifndef CRYPTOPP_DMAC_H
7 #define CRYPTOPP_DMAC_H
8 
9 #include "cbcmac.h"
10 
11 NAMESPACE_BEGIN(CryptoPP)
12 
13 /// \brief DMAC message authentication code base class
14 /// \tparam T class derived from BlockCipherDocumentation
15 /// \since Crypto++ 3.1
16 template <class T>
17 class CRYPTOPP_NO_VTABLE DMAC_Base : public SameKeyLengthAs<T>, public MessageAuthenticationCode
18 {
19 public:
20  CRYPTOPP_CONSTANT(DIGESTSIZE=T::BLOCKSIZE)
21  static std::string StaticAlgorithmName() {return std::string("DMAC(") + T::StaticAlgorithmName() + ")";}
22 
23  virtual~DMAC_Base() {}
24  DMAC_Base() : m_subkeylength(0), m_counter(0) {}
25 
26  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
27  void Update(const byte *input, size_t length);
28  void TruncatedFinal(byte *mac, size_t size);
29  unsigned int DigestSize() const {return DIGESTSIZE;}
30 
31 private:
32  byte *GenerateSubKeys(const byte *key, size_t keylength);
33 
34  size_t m_subkeylength;
35  SecByteBlock m_subkeys;
36  CBC_MAC<T> m_mac1;
37  typename T::Encryption m_f2;
38  unsigned int m_counter;
39 };
40 
41 /// \brief DMAC message authentication code
42 /// \tparam T class derived from BlockCipherDocumentation
43 /// \sa <A HREF="https://eprint.iacr.org/1997/010">CBC MAC for Real-Time Data Sources (08.15.1997)</A>
44 /// by Erez Petrank and Charles Rackoff
45 /// \since Crypto++ 3.1
46 template <class T>
47 class DMAC : public MessageAuthenticationCodeFinal<DMAC_Base<T> >
48 {
49 public:
50  /// \brief Construct a DMAC
51  DMAC() {}
52 
53  /// \brief Construct a DMAC
54  /// \param key a byte array used to key the cipher
55  /// \param length the size of the byte array, in bytes
56  DMAC(const byte *key, size_t length=DMAC_Base<T>::DEFAULT_KEYLENGTH)
57  {this->SetKey(key, length);}
58 };
59 
60 template <class T>
61 void DMAC_Base<T>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
62 {
63  m_subkeylength = T::StaticGetValidKeyLength(T::BLOCKSIZE);
64  m_subkeys.resize(2*UnsignedMin((unsigned int)T::BLOCKSIZE, m_subkeylength));
65  m_mac1.SetKey(GenerateSubKeys(key, length), m_subkeylength, params);
66  m_f2.SetKey(m_subkeys+m_subkeys.size()/2, m_subkeylength, params);
67  m_counter = 0;
68  m_subkeys.resize(0);
69 }
70 
71 template <class T>
72 void DMAC_Base<T>::Update(const byte *input, size_t length)
73 {
74  m_mac1.Update(input, length);
75  m_counter = (unsigned int)((m_counter + length) % T::BLOCKSIZE);
76 }
77 
78 template <class T>
79 void DMAC_Base<T>::TruncatedFinal(byte *mac, size_t size)
80 {
81  ThrowIfInvalidTruncatedSize(size);
82 
83  byte pad[T::BLOCKSIZE];
84  byte padByte = byte(T::BLOCKSIZE-m_counter);
85  memset(pad, padByte, padByte);
86  m_mac1.Update(pad, padByte);
87  m_mac1.TruncatedFinal(mac, size);
88  m_f2.ProcessBlock(mac);
89 
90  m_counter = 0; // reset for next message
91 }
92 
93 template <class T>
94 byte *DMAC_Base<T>::GenerateSubKeys(const byte *key, size_t keylength)
95 {
96  typename T::Encryption cipher(key, keylength);
97  memset(m_subkeys, 0, m_subkeys.size());
98  cipher.ProcessBlock(m_subkeys);
99  m_subkeys[m_subkeys.size()/2 + T::BLOCKSIZE - 1] = 1;
100  cipher.ProcessBlock(m_subkeys+m_subkeys.size()/2);
101  return m_subkeys;
102 }
103 
104 NAMESPACE_END
105 
106 #endif
void TruncatedFinal(byte *mac, size_t size)
Computes the hash of the current message.
Definition: dmac.h:79
Interface for message authentication codes.
Definition: cryptlib.h:1245
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:64
DMAC message authentication code.
Definition: dmac.h:47
virtual void TruncatedFinal(byte *digest, size_t digestSize)=0
Computes the hash of the current message.
DMAC()
Construct a DMAC.
Definition: dmac.h:51
DMAC message authentication code base class.
Definition: dmac.h:17
Provides class member functions to key a message authentication code.
Definition: seckey.h:392
SecBlock<byte> typedef.
Definition: secblock.h:822
Classes for CBC MAC.
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: dmac.h:29
DMAC(const byte *key, size_t length=DMAC_Base< T >::DEFAULT_KEYLENGTH)
Construct a DMAC.
Definition: dmac.h:56
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: dmac.h:72
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
Definition: misc.h:546
Provides key lengths based on another class&#39;s key length.
Definition: seckey.h:240
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
Definition: dmac.h:61
Crypto++ library namespace.
CBC-MAC.
Definition: cbcmac.h:43
virtual void Update(const byte *input, size_t length)=0
Updates a hash with additional input.
Interface for retrieving values given their names.
Definition: cryptlib.h:290