hmac.cpp
// hmac.cpp - originally written and placed in the public domain by Wei Dai
#include "pch.h"
#ifndef CRYPTOPP_IMPORTS
#include "hmac.h"
NAMESPACE_BEGIN(CryptoPP)
void HMAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
{
AssertValidKeyLength(keylength);
Restart();
HashTransformation &hash = AccessHash();
unsigned int blockSize = hash.BlockSize();
if (!blockSize)
throw InvalidArgument("HMAC: can only be used with a block-based hash function");
m_buf.resize(2*AccessHash().BlockSize() + AccessHash().DigestSize());
if (keylength <= blockSize)
{
// hmac.cpp:26:9: runtime error: null pointer passed as argument 2
if (AccessIpad() && userKey && keylength)
std::memcpy(AccessIpad(), userKey, keylength);
}
else
{
AccessHash().CalculateDigest(AccessIpad(), userKey, keylength);
keylength = hash.DigestSize();
}
CRYPTOPP_ASSERT(keylength <= blockSize);
std::memset(AccessIpad()+keylength, 0, blockSize-keylength);
for (unsigned int i=0; i<blockSize; i++)
{
AccessOpad()[i] = AccessIpad()[i] ^ 0x5c;
AccessIpad()[i] ^= 0x36;
}
}
void HMAC_Base::KeyInnerHash()
{
CRYPTOPP_ASSERT(!m_innerHashKeyed);
HashTransformation &hash = AccessHash();
hash.Update(AccessIpad(), hash.BlockSize());
m_innerHashKeyed = true;
}
void HMAC_Base::Restart()
{
if (m_innerHashKeyed)
{
AccessHash().Restart();
m_innerHashKeyed = false;
}
}
void HMAC_Base::Update(const byte *input, size_t length)
{
if (!m_innerHashKeyed)
KeyInnerHash();
AccessHash().Update(input, length);
}
void HMAC_Base::TruncatedFinal(byte *mac, size_t size)
{
ThrowIfInvalidTruncatedSize(size);
HashTransformation &hash = AccessHash();
if (!m_innerHashKeyed)
KeyInnerHash();
hash.Final(AccessInnerHash());
hash.Update(AccessOpad(), hash.BlockSize());
hash.Update(AccessInnerHash(), hash.DigestSize());
hash.TruncatedFinal(mac, size);
m_innerHashKeyed = false;
}
NAMESPACE_END
#endif
HMAC_Base::UncheckedSetKey
void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
HMAC_Base::TruncatedFinal
void TruncatedFinal(byte *mac, size_t size)
Computes the hash of the current message.
HMAC_Base::Restart
void Restart()
Restart the hash.
HMAC_Base::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition hmac.h:30
HMAC_Base::Update
void Update(const byte *input, size_t length)
Updates a hash with additional input.
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition cryptlib.h:1118
HashTransformation::BlockSize
virtual unsigned int BlockSize() const
Provides the block size of the compression function.
Definition cryptlib.h:1170
HashTransformation::TruncatedFinal
virtual void TruncatedFinal(byte *digest, size_t digestSize)=0
Computes the hash of the current message.
HashTransformation::Restart
virtual void Restart()
Restart the hash.
Definition cryptlib.h:1152
HashTransformation::DigestSize
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
HashTransformation::Final
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition cryptlib.h:1147
HashTransformation::Update
virtual void Update(const byte *input, size_t length)=0
Updates a hash with additional input.
HashTransformation::CalculateDigest
virtual void CalculateDigest(byte *digest, const byte *input, size_t length)
Updates the hash with additional input and computes the hash of the current message.
Definition cryptlib.h:1193
InvalidArgument
An invalid argument was detected.
Definition cryptlib.h:208
NameValuePairs
Interface for retrieving values given their names.
Definition cryptlib.h:327
SecBlock::resize
void resize(size_type newSize)
Change size and preserve contents.
Definition secblock.h:1198
hmac.h
Classes for HMAC message authentication codes.
CryptoPP
Crypto++ library namespace.
pch.h
Precompiled header file.
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition trap.h:68