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