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