Crypto++  5.6.3
Free C++ class library of cryptographic schemes
hmac.h
Go to the documentation of this file.
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 &params);
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
Interface for message authentication codes.
Definition: cryptlib.h:1107
HMAC_Base()
Construct a HMAC_Base.
Definition: hmac.h:21
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:101
SecBlock typedef.
Definition: secblock.h:728
Classes and functions for secure memory allocations.
HMAC information.
Definition: hmac.h:17
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: hmac.h:28
Classes and functions for implementing secret key algorithms.
static const int DEFAULT_KEYLENGTH
The default key length used by the algorithm provided as a constant.
Definition: seckey.h:176
HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
Construct a HMAC.
Definition: hmac.h:61
Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication code...
Definition: seckey.h:358
virtual unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: cryptlib.h:975
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:157
HMAC.
Definition: hmac.h:50
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:922
Crypto++ library namespace.
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: hmac.h:65
Interface for retrieving values given their names.
Definition: cryptlib.h:277
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this hash.
Definition: hmac.h:27