Crypto++
hmac.h
1 // hmac.h - written and placed in the public domain by Wei Dai
2 
3 #ifndef CRYPTOPP_HMAC_H
4 #define CRYPTOPP_HMAC_H
5 
6 #include "seckey.h"
7 #include "secblock.h"
8 
9 NAMESPACE_BEGIN(CryptoPP)
10 
11 //! _
12 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, INT_MAX>, public MessageAuthenticationCode
13 {
14 public:
15  HMAC_Base() : m_innerHashKeyed(false) {}
16  void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
17 
18  void Restart();
19  void Update(const byte *input, size_t length);
20  void TruncatedFinal(byte *mac, size_t size);
21  unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();}
22  unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();}
23 
24 protected:
25  virtual HashTransformation & AccessHash() =0;
26  byte * AccessIpad() {return m_buf;}
27  byte * AccessOpad() {return m_buf + AccessHash().BlockSize();}
28  byte * AccessInnerHash() {return m_buf + 2*AccessHash().BlockSize();}
29 
30 private:
31  void KeyInnerHash();
32 
33  SecByteBlock m_buf;
34  bool m_innerHashKeyed;
35 };
36 
37 //! <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a>
38 /*! HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text)) */
39 template <class T>
40 class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> >
41 {
42 public:
43  CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE)
44  CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE)
45 
46  HMAC() {}
47  HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
48  {this->SetKey(key, length);}
49 
50  static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
51  std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";}
52 
53 private:
54  HashTransformation & AccessHash() {return m_hash;}
55 
56  T m_hash;
57 };
58 
59 NAMESPACE_END
60 
61 #endif
interface for message authentication codes
Definition: cryptlib.h:617
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
set or reset the key of this object
_
Definition: hmac.h:12
unsigned int DigestSize() const
size of the hash/digest/MAC returned by Final()
Definition: hmac.h:22
virtual unsigned int BlockSize() const
block size of underlying compression function, or 0 if not block based
Definition: cryptlib.h:559
support query of variable key length, template parameters are default, min, max, multiple (default mu...
Definition: seckey.h:80
HMAC
Definition: hmac.h:40
interface for hash functions and data processing part of MACs
Definition: cryptlib.h:530
std::string AlgorithmName() const
returns name of this algorithm, not universally implemented yet
Definition: hmac.h:51
interface for retrieving values given their names
Definition: cryptlib.h:224
unsigned int OptimalBlockSize() const
input to Update() should have length a multiple of this for optimal speed
Definition: hmac.h:21