Crypto++
dmac.h
1 #ifndef CRYPTOPP_DMAC_H
2 #define CRYPTOPP_DMAC_H
3 
4 #include "cbcmac.h"
5 
6 NAMESPACE_BEGIN(CryptoPP)
7 
8 //! _
9 template <class T>
10 class CRYPTOPP_NO_VTABLE DMAC_Base : public SameKeyLengthAs<T>, public MessageAuthenticationCode
11 {
12 public:
13  static std::string StaticAlgorithmName() {return std::string("DMAC(") + T::StaticAlgorithmName() + ")";}
14 
15  CRYPTOPP_CONSTANT(DIGESTSIZE=T::BLOCKSIZE)
16 
17  DMAC_Base() {}
18 
19  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
20  void Update(const byte *input, size_t length);
21  void TruncatedFinal(byte *mac, size_t size);
22  unsigned int DigestSize() const {return DIGESTSIZE;}
23 
24 private:
25  byte *GenerateSubKeys(const byte *key, size_t keylength);
26 
27  size_t m_subkeylength;
28  SecByteBlock m_subkeys;
29  CBC_MAC<T> m_mac1;
30  typename T::Encryption m_f2;
31  unsigned int m_counter;
32 };
33 
34 //! DMAC
35 /*! Based on "CBC MAC for Real-Time Data Sources" by Erez Petrank
36  and Charles Rackoff. T should be a class derived from BlockCipherDocumentation.
37 */
38 template <class T>
39 class DMAC : public MessageAuthenticationCodeFinal<DMAC_Base<T> >
40 {
41 public:
42  DMAC() {}
43  DMAC(const byte *key, size_t length=DMAC_Base<T>::DEFAULT_KEYLENGTH)
44  {this->SetKey(key, length);}
45 };
46 
47 template <class T>
48 void DMAC_Base<T>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
49 {
50  m_subkeylength = T::StaticGetValidKeyLength(T::BLOCKSIZE);
51  m_subkeys.resize(2*UnsignedMin((unsigned int)T::BLOCKSIZE, m_subkeylength));
52  m_mac1.SetKey(GenerateSubKeys(key, length), m_subkeylength, params);
53  m_f2.SetKey(m_subkeys+m_subkeys.size()/2, m_subkeylength, params);
54  m_counter = 0;
55  m_subkeys.resize(0);
56 }
57 
58 template <class T>
59 void DMAC_Base<T>::Update(const byte *input, size_t length)
60 {
61  m_mac1.Update(input, length);
62  m_counter = (unsigned int)((m_counter + length) % T::BLOCKSIZE);
63 }
64 
65 template <class T>
66 void DMAC_Base<T>::TruncatedFinal(byte *mac, size_t size)
67 {
68  ThrowIfInvalidTruncatedSize(size);
69 
70  byte pad[T::BLOCKSIZE];
71  byte padByte = byte(T::BLOCKSIZE-m_counter);
72  memset(pad, padByte, padByte);
73  m_mac1.Update(pad, padByte);
74  m_mac1.TruncatedFinal(mac, size);
75  m_f2.ProcessBlock(mac);
76 
77  m_counter = 0; // reset for next message
78 }
79 
80 template <class T>
81 byte *DMAC_Base<T>::GenerateSubKeys(const byte *key, size_t keylength)
82 {
83  typename T::Encryption cipher(key, keylength);
84  memset(m_subkeys, 0, m_subkeys.size());
85  cipher.ProcessBlock(m_subkeys);
86  m_subkeys[m_subkeys.size()/2 + T::BLOCKSIZE - 1] = 1;
87  cipher.ProcessBlock(m_subkeys+m_subkeys.size()/2);
88  return m_subkeys;
89 }
90 
91 NAMESPACE_END
92 
93 #endif
void TruncatedFinal(byte *mac, size_t size)
truncated version of Final()
Definition: dmac.h:66
interface for message authentication codes
Definition: cryptlib.h:617
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
set or reset the key of this object
DMAC.
Definition: dmac.h:39
_
Definition: dmac.h:10
void Update(const byte *input, size_t length)
process more input
Definition: dmac.h:59
support query of key length that's the same as another class
Definition: seckey.h:114
unsigned int DigestSize() const
size of the hash/digest/MAC returned by Final()
Definition: dmac.h:22
CBC-MAC
Definition: cbcmac.h:34
interface for retrieving values given their names
Definition: cryptlib.h:224