Crypto++ 8.6 Free C++ class library of cryptographic schemes

hmac.h
Go to the documentation of this file.
1 // hmac.h - originally written and placed in the public domain by Wei Dai
2
3 /// \file hmac.h
4 /// \brief Classes for HMAC message authentication codes
5
6 #ifndef CRYPTOPP_HMAC_H
7 #define CRYPTOPP_HMAC_H
8
9 #include "seckey.h"
10 #include "secblock.h"
11
12 NAMESPACE_BEGIN(CryptoPP)
13
14 /// \brief HMAC information
15 /// \details HMAC_Base derives from VariableKeyLength and MessageAuthenticationCode
16 /// \since Crypto++ 2.1
17 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, INT_MAX>, public MessageAuthenticationCode
18 {
19 public:
20 virtual ~HMAC_Base() {}
21
22 /// \brief Construct a HMAC_Base
23 HMAC_Base() : m_innerHashKeyed(false) {}
24 void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
25
26 void Restart();
27 void Update(const byte *input, size_t length);
28 void TruncatedFinal(byte *mac, size_t size);
29 unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();}
30 unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();}
31
32 protected:
33 virtual HashTransformation & AccessHash() =0;
34 byte * AccessIpad() {return m_buf;}
35 byte * AccessOpad() {return m_buf + AccessHash().BlockSize();}
36 byte * AccessInnerHash() {return m_buf + 2*AccessHash().BlockSize();}
37
38 private:
39 void KeyInnerHash();
40
41 SecByteBlock m_buf;
42 bool m_innerHashKeyed;
43 };
44
45 /// \brief HMAC
46 /// \tparam T HashTransformation derived class
47 /// \details HMAC derives from MessageAuthenticationCodeImpl. It calculates the HMAC using
48 /// <tt>HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text))</tt>.
49 /// \sa <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a>
50 /// \since Crypto++ 2.1
51 template <class T>
52 class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> >
53 {
54 public:
55 CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE);
56 CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE);
57
58 virtual ~HMAC() {}
59
60 /// \brief Construct a HMAC
61 HMAC() {}
62 /// \brief Construct a HMAC
63 /// \param key the HMAC key
64 /// \param length the size of the HMAC key
65 HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
66 {this->SetKey(key, length);}
67
68 static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
69 std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";}
70 std::string AlgorithmProvider() const {return m_hash.AlgorithmProvider();}
71
72 private:
73 HashTransformation & AccessHash() {return m_hash;}
74
75 T m_hash;
76 };
77
78 NAMESPACE_END
79
80 #endif
HMAC_Base
HMAC information.
Definition: hmac.h:18
HMAC_Base::OptimalBlockSize
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this hash.
Definition: hmac.h:29
HMAC_Base::UncheckedSetKey
void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
HMAC_Base::TruncatedFinal
void TruncatedFinal(byte *mac, size_t size)
Computes the hash of the current message.
HMAC_Base::Restart
void Restart()
Restart the hash.
HMAC_Base::HMAC_Base
HMAC_Base()
Construct a HMAC_Base.
Definition: hmac.h:23
HMAC_Base::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: hmac.h:30
HMAC_Base::Update
void Update(const byte *input, size_t length)
Updates a hash with additional input.
HMAC
HMAC.
Definition: hmac.h:53
HMAC::HMAC
HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
Construct a HMAC.
Definition: hmac.h:65
HMAC::HMAC
HMAC()
Construct a HMAC.
Definition: hmac.h:61
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1118
HashTransformation::BlockSize
virtual unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: cryptlib.h:1170
MessageAuthenticationCode
Interface for message authentication codes.
Definition: cryptlib.h:1304
MessageAuthenticationCodeImpl
Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication code...
Definition: seckey.h:363
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:327
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:1226
VariableKeyLength
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:166
VariableKeyLength< 16, 0, INT_MAX >::DEFAULT_KEYLENGTH
static const int DEFAULT_KEYLENGTH
The default key length used by the algorithm provided as a constant.
Definition: seckey.h:184
CryptoPP
Crypto++ library namespace.
secblock.h
Classes and functions for secure memory allocations.
seckey.h
Classes and functions for implementing secret key algorithms.