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
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 //! _
15 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, INT_MAX>, public MessageAuthenticationCode
16 {
17 public:
18  HMAC_Base() : m_innerHashKeyed(false) {}
19  void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
20 
21  void Restart();
22  void Update(const byte *input, size_t length);
23  void TruncatedFinal(byte *mac, size_t size);
24  unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();}
25  unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();}
26 
27 protected:
28  virtual HashTransformation & AccessHash() =0;
29  byte * AccessIpad() {return m_buf;}
30  byte * AccessOpad() {return m_buf + AccessHash().BlockSize();}
31  byte * AccessInnerHash() {return m_buf + 2*AccessHash().BlockSize();}
32 
33 private:
34  void KeyInnerHash();
35 
36  SecByteBlock m_buf;
37  bool m_innerHashKeyed;
38 };
39 
40 //! <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a>
41 /*! HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text)) */
42 template <class T>
43 class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> >
44 {
45 public:
46  CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE)
47  CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE)
48 
49  HMAC() {}
50  HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
51  {this->SetKey(key, length);}
52 
53  static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
54  std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";}
55 
56 private:
57  HashTransformation & AccessHash() {return m_hash;}
58 
59  T m_hash;
60 };
61 
62 NAMESPACE_END
63 
64 #endif
Interface for message authentication codes.
Definition: cryptlib.h:1027
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:100
SecByteBlock is a SecBlock<byte> typedef.
Definition: secblock.h:719
Classes and functions for secure memory allocations.
_
Definition: hmac.h:15
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: hmac.h:25
Classes and functions for implementing secret key algorithms.
static const int DEFAULT_KEYLENGTH
The default key length used by the cipher provided as a constant.
Definition: seckey.h:171
Provides class member functions to access MessageAuthenticationCode constants.
Definition: seckey.h:347
virtual unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: cryptlib.h:907
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:152
HMAC
Definition: hmac.h:43
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:858
Crypto++ library namespace.
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: hmac.h:54
Interface for retrieving values given their names.
Definition: cryptlib.h:261
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this hash.
Definition: hmac.h:24