Crypto++  6.0
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 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, INT_MAX>, public MessageAuthenticationCode
17 {
18 public:
19  /// \brief Construct a HMAC_Base
20  HMAC_Base() : m_innerHashKeyed(false) {}
21  void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
22 
23  void Restart();
24  void Update(const byte *input, size_t length);
25  void TruncatedFinal(byte *mac, size_t size);
26  unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();}
27  unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();}
28 
29 protected:
30  virtual HashTransformation & AccessHash() =0;
31  byte * AccessIpad() {return m_buf;}
32  byte * AccessOpad() {return m_buf + AccessHash().BlockSize();}
33  byte * AccessInnerHash() {return m_buf + 2*AccessHash().BlockSize();}
34 
35 private:
36  void KeyInnerHash();
37 
38  SecByteBlock m_buf;
39  bool m_innerHashKeyed;
40 };
41 
42 /// \brief HMAC
43 /// \tparam T HashTransformation derived class
44 /// \details HMAC derives from MessageAuthenticationCodeImpl. It calculates the HMAC using
45 /// <tt>HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text))</tt>.
46 /// \sa <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a>
47 template <class T>
48 class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> >
49 {
50 public:
51  CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE)
52  CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE)
53 
54  /// \brief Construct a HMAC
55  HMAC() {}
56  /// \brief Construct a HMAC
57  /// \param key the HMAC key
58  /// \param length the size of the HMAC key
59  HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
60  {this->SetKey(key, length);}
61 
62  static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
63  std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";}
64 
65 private:
66  HashTransformation & AccessHash() {return m_hash;}
67 
68  T m_hash;
69 };
70 
71 NAMESPACE_END
72 
73 #endif
virtual unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: cryptlib.h:1116
Interface for message authentication codes.
Definition: cryptlib.h:1244
HMAC_Base()
Construct a HMAC_Base.
Definition: hmac.h:20
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:64
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: hmac.h:27
SecBlock<byte> typedef.
Definition: secblock.h:822
Classes and functions for secure memory allocations.
HMAC information.
Definition: hmac.h:16
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:206
HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
Construct a HMAC.
Definition: hmac.h:59
Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication code...
Definition: seckey.h:383
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: hmac.h:63
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this hash.
Definition: hmac.h:26
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:187
HMAC.
Definition: hmac.h:48
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1065
Crypto++ library namespace.
Interface for retrieving values given their names.
Definition: cryptlib.h:291