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