1 | // hmac.h - 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 | //! \class HMAC_Base |
---|
15 | //! \brief HMAC information |
---|
16 | //! \details HMAC_Base derives from VariableKeyLength and MessageAuthenticationCode |
---|
17 | class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, INT_MAX>, public MessageAuthenticationCode |
---|
18 | { |
---|
19 | public: |
---|
20 | //! \brief Construct a HMAC_Base |
---|
21 | HMAC_Base() : m_innerHashKeyed(false) {} |
---|
22 | void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs ¶ms); |
---|
23 | |
---|
24 | void Restart(); |
---|
25 | void Update(const byte *input, size_t length); |
---|
26 | void TruncatedFinal(byte *mac, size_t size); |
---|
27 | unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();} |
---|
28 | unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();} |
---|
29 | |
---|
30 | protected: |
---|
31 | virtual HashTransformation & AccessHash() =0; |
---|
32 | byte * AccessIpad() {return m_buf;} |
---|
33 | byte * AccessOpad() {return m_buf + AccessHash().BlockSize();} |
---|
34 | byte * AccessInnerHash() {return m_buf + 2*AccessHash().BlockSize();} |
---|
35 | |
---|
36 | private: |
---|
37 | void KeyInnerHash(); |
---|
38 | |
---|
39 | SecByteBlock m_buf; |
---|
40 | bool m_innerHashKeyed; |
---|
41 | }; |
---|
42 | |
---|
43 | //! \class HMAC |
---|
44 | //! \brief HMAC |
---|
45 | //! \tparam T HashTransformation derived class |
---|
46 | //! \details HMAC derives from MessageAuthenticationCodeImpl. It calculates the HMAC using |
---|
47 | //! <tt>HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text))</tt>. |
---|
48 | //! \sa <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a> |
---|
49 | template <class T> |
---|
50 | class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> > |
---|
51 | { |
---|
52 | public: |
---|
53 | CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE) |
---|
54 | CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE) |
---|
55 | |
---|
56 | //! \brief Construct a HMAC |
---|
57 | HMAC() {} |
---|
58 | //! \brief Construct a HMAC |
---|
59 | //! \param key the HMAC key |
---|
60 | //! \param length the size of the HMAC key |
---|
61 | HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH) |
---|
62 | {this->SetKey(key, length);} |
---|
63 | |
---|
64 | static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";} |
---|
65 | std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";} |
---|
66 | |
---|
67 | private: |
---|
68 | HashTransformation & AccessHash() {return m_hash;} |
---|
69 | |
---|
70 | T m_hash; |
---|
71 | }; |
---|
72 | |
---|
73 | NAMESPACE_END |
---|
74 | |
---|
75 | #endif |
---|