Fabcoin Core  0.16.2
P2P Digital Currency
dmac.h
Go to the documentation of this file.
1 // dmac.h - written and placed in the public domain by Wei Dai
2 
6 
7 #ifndef CRYPTOPP_DMAC_H
8 #define CRYPTOPP_DMAC_H
9 
10 #include "cbcmac.h"
11 
13 
14 template <class T>
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 
38  typename T::Encryption m_f2;
39  unsigned int m_counter;
40 };
41 
47 template <class T>
48 class DMAC : public MessageAuthenticationCodeFinal<DMAC_Base<T> >
49 {
50 public:
52  DMAC() {}
53 
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>
81 {
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 
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:1111
uint8_t byte
Definition: Common.h:57
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
void resize(size_type newSize)
Change size and preserve contents.
Definition: secblock.h:705
#define T(i, x)
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:524
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:379
static std::string StaticAlgorithmName()
Definition: dmac.h:21
Classes for CBC MAC.
void TruncatedFinal(byte *mac, size_t size)
Computes the hash of the current message.
Definition: cbcmac.cpp:43
CBC_MAC< T > m_mac1
Definition: dmac.h:37
void ThrowIfInvalidTruncatedSize(size_t size) const
Validates a truncated digest size.
Definition: cryptlib.cpp:416
T::Encryption m_f2
Definition: dmac.h:38
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:512
Provides key lengths based on another class&#39;s key length.
Definition: seckey.h:222
#define CRYPTOPP_CONSTANT(x)
Definition: config.h:540
size_t m_subkeylength
Definition: dmac.h:35
#define CRYPTOPP_NO_VTABLE
Definition: config.h:369
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: cbcmac.cpp:16
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
uint8_t const size_t const size
Definition: sha3.h:20
uint8_t byte
Definition: Common.h:10
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: dmac.h:30
#define NAMESPACE_END
Definition: config.h:201
SecByteBlock m_subkeys
Definition: dmac.h:36
byte * GenerateSubKeys(const byte *key, size_t keylength)
Definition: dmac.h:95
CBC-MAC
Definition: cbcmac.h:40
unsigned int m_counter
Definition: dmac.h:39
Interface for retrieving values given their names.
Definition: cryptlib.h:279