hmac.h
// hmac.h - written and placed in the public domain by Wei Dai
#ifndef CRYPTOPP_HMAC_H
#define CRYPTOPP_HMAC_H
#include "seckey.h"
#include "secblock.h"
NAMESPACE_BEGIN(CryptoPP)
//! _
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, INT_MAX>, public MessageAuthenticationCode
{
public:
HMAC_Base() : m_innerHashKeyed(false) {}
void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
void Restart();
void Update(const byte *input, size_t length);
void TruncatedFinal(byte *mac, size_t size);
unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();}
unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();}
protected:
virtual HashTransformation & AccessHash() =0;
byte * AccessIpad() {return m_buf;}
byte * AccessOpad() {return m_buf + AccessHash().BlockSize();}
byte * AccessInnerHash() {return m_buf + 2*AccessHash().BlockSize();}
private:
void KeyInnerHash();
SecByteBlock m_buf;
bool m_innerHashKeyed;
};
//! <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a>
/*! HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text)) */
template <class T>
class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> >
{
public:
CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE)
CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE)
HMAC() {}
HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
{this->SetKey(key, length);}
static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";}
private:
HashTransformation & AccessHash() {return m_hash;}
T m_hash;
};
NAMESPACE_END
#endif
MessageAuthenticationCode
interface for message authentication codes
Definition: cryptlib.h:617
SimpleKeyingInterface::SetKey
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
set or reset the key of this object
SecBlock< byte >
HMAC_Base
_
Definition: hmac.h:12
HMAC_Base::DigestSize
unsigned int DigestSize() const
size of the hash/digest/MAC returned by Final()
Definition: hmac.h:22
MessageAuthenticationCodeImpl
_
Definition: seckey.h:167
HashTransformation::BlockSize
virtual unsigned int BlockSize() const
block size of underlying compression function, or 0 if not block based
Definition: cryptlib.h:559
VariableKeyLength
support query of variable key length, template parameters are default, min, max, multiple (default mu...
Definition: seckey.h:80
HMAC
HMAC
Definition: hmac.h:40
HashTransformation
interface for hash functions and data processing part of MACs
Definition: cryptlib.h:530
HMAC::AlgorithmName
std::string AlgorithmName() const
returns name of this algorithm, not universally implemented yet
Definition: hmac.h:51
NameValuePairs
interface for retrieving values given their names
Definition: cryptlib.h:224
HMAC_Base::OptimalBlockSize
unsigned int OptimalBlockSize() const
input to Update() should have length a multiple of this for optimal speed
Definition: hmac.h:21