Crypto++  5.6.5
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
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 /// \brief DMAC message authentication code base class
15 /// \tparam T class derived from BlockCipherDocumentation
16 template <class T>
17 class CRYPTOPP_NO_VTABLE DMAC_Base : public SameKeyLengthAs<T>, public MessageAuthenticationCode
18 {
19 public:
20  static std::string StaticAlgorithmName() {return std::string("DMAC(") + T::StaticAlgorithmName() + ")";}
21 
22  CRYPTOPP_CONSTANT(DIGESTSIZE=T::BLOCKSIZE)
23 
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 template <class T>
46 class DMAC : public MessageAuthenticationCodeFinal<DMAC_Base<T> >
47 {
48 public:
49  /// \brief Construct a DMAC
50  DMAC() {}
51 
52  /// \brief Construct a DMAC
53  /// \param key a byte array used to key the cipher
54  /// \param length the size of the byte array, in bytes
55  DMAC(const byte *key, size_t length=DMAC_Base<T>::DEFAULT_KEYLENGTH)
56  {this->SetKey(key, length);}
57 };
58 
59 template <class T>
60 void DMAC_Base<T>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
61 {
62  m_subkeylength = T::StaticGetValidKeyLength(T::BLOCKSIZE);
63  m_subkeys.resize(2*UnsignedMin((unsigned int)T::BLOCKSIZE, m_subkeylength));
64  m_mac1.SetKey(GenerateSubKeys(key, length), m_subkeylength, params);
65  m_f2.SetKey(m_subkeys+m_subkeys.size()/2, m_subkeylength, params);
66  m_counter = 0;
67  m_subkeys.resize(0);
68 }
69 
70 template <class T>
71 void DMAC_Base<T>::Update(const byte *input, size_t length)
72 {
73  m_mac1.Update(input, length);
74  m_counter = (unsigned int)((m_counter + length) % T::BLOCKSIZE);
75 }
76 
77 template <class T>
78 void DMAC_Base<T>::TruncatedFinal(byte *mac, size_t size)
79 {
80  ThrowIfInvalidTruncatedSize(size);
81 
82  byte pad[T::BLOCKSIZE];
83  byte padByte = byte(T::BLOCKSIZE-m_counter);
84  memset(pad, padByte, padByte);
85  m_mac1.Update(pad, padByte);
86  m_mac1.TruncatedFinal(mac, size);
87  m_f2.ProcessBlock(mac);
88 
89  m_counter = 0; // reset for next message
90 }
91 
92 template <class T>
93 byte *DMAC_Base<T>::GenerateSubKeys(const byte *key, size_t keylength)
94 {
95  typename T::Encryption cipher(key, keylength);
96  memset(m_subkeys, 0, m_subkeys.size());
97  cipher.ProcessBlock(m_subkeys);
98  m_subkeys[m_subkeys.size()/2 + T::BLOCKSIZE - 1] = 1;
99  cipher.ProcessBlock(m_subkeys+m_subkeys.size()/2);
100  return m_subkeys;
101 }
102 
103 NAMESPACE_END
104 
105 #endif
void TruncatedFinal(byte *mac, size_t size)
Computes the hash of the current message.
Definition: dmac.h:78
Interface for message authentication codes.
Definition: cryptlib.h:1244
DMAC message authentication code.
Definition: dmac.h:46
virtual void TruncatedFinal(byte *digest, size_t digestSize)=0
Computes the hash of the current message.
DMAC()
Construct a DMAC.
Definition: dmac.h:50
DMAC message authentication code base class.
Definition: dmac.h:17
Provides class member functions to key a message authentication code.
Definition: seckey.h:391
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:55
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: dmac.h:71
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
Definition: misc.h:542
Provides key lengths based on another class&#39;s key length.
Definition: seckey.h:239
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
Definition: dmac.h:60
Crypto++ library namespace.
CBC-MAC.
Definition: cbcmac.h:40
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:291