dmac.h
Go to the documentation of this file.
// dmac.h - originally written and placed in the public domain by Wei Dai
/// \file dmac.h
/// \brief Classes for DMAC message authentication code
#ifndef CRYPTOPP_DMAC_H
#define CRYPTOPP_DMAC_H
#include "cbcmac.h"
NAMESPACE_BEGIN(CryptoPP)
/// \brief DMAC message authentication code base class
/// \tparam T class derived from BlockCipherDocumentation
/// \since Crypto++ 3.1
template <class T>
class CRYPTOPP_NO_VTABLE DMAC_Base : public SameKeyLengthAs<T>, public MessageAuthenticationCode
{
public:
CRYPTOPP_CONSTANT(DIGESTSIZE=T::BLOCKSIZE);
static std::string StaticAlgorithmName() {return std::string("DMAC(") + T::StaticAlgorithmName() + ")";}
virtual~DMAC_Base() {}
DMAC_Base() : m_subkeylength(0), m_counter(0) {}
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
void Update(const byte *input, size_t length);
void TruncatedFinal(byte *mac, size_t size);
unsigned int DigestSize() const {return DIGESTSIZE;}
std::string AlgorithmProvider() const;
private:
byte *GenerateSubKeys(const byte *key, size_t keylength);
size_t m_subkeylength;
SecByteBlock m_subkeys;
CBC_MAC<T> m_mac1;
typename T::Encryption m_f2;
unsigned int m_counter;
};
template <class T>
std::string DMAC_Base<T>::AlgorithmProvider() const
{
return m_f2.AlgorithmProvider();
}
/// \brief DMAC message authentication code
/// \tparam T class derived from BlockCipherDocumentation
/// \sa <A HREF="https://eprint.iacr.org/1997/010">CBC MAC for Real-Time Data Sources (08.15.1997)</A>
/// by Erez Petrank and Charles Rackoff
/// \since Crypto++ 3.1
template <class T>
class DMAC : public MessageAuthenticationCodeFinal<DMAC_Base<T> >
{
public:
/// \brief Construct a DMAC
DMAC() {}
/// \brief Construct a DMAC
/// \param key a byte array used to key the cipher
/// \param length the size of the byte array, in bytes
DMAC(const byte *key, size_t length=DMAC_Base<T>::DEFAULT_KEYLENGTH)
{this->SetKey(key, length);}
};
template <class T>
void DMAC_Base<T>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
{
m_subkeylength = T::StaticGetValidKeyLength(T::BLOCKSIZE);
m_subkeys.resize(2*UnsignedMin((unsigned int)T::BLOCKSIZE, m_subkeylength));
m_mac1.SetKey(GenerateSubKeys(key, length), m_subkeylength, params);
m_f2.SetKey(m_subkeys+m_subkeys.size()/2, m_subkeylength, params);
m_counter = 0;
m_subkeys.resize(0);
}
template <class T>
void DMAC_Base<T>::Update(const byte *input, size_t length)
{
m_mac1.Update(input, length);
m_counter = (unsigned int)((m_counter + length) % T::BLOCKSIZE);
}
template <class T>
void DMAC_Base<T>::TruncatedFinal(byte *mac, size_t size)
{
ThrowIfInvalidTruncatedSize(size);
byte pad[T::BLOCKSIZE];
byte padByte = byte(T::BLOCKSIZE-m_counter);
memset(pad, padByte, padByte);
m_mac1.Update(pad, padByte);
m_mac1.TruncatedFinal(mac, size);
m_f2.ProcessBlock(mac);
m_counter = 0; // reset for next message
}
template <class T>
byte *DMAC_Base<T>::GenerateSubKeys(const byte *key, size_t keylength)
{
typename T::Encryption cipher(key, keylength);
memset(m_subkeys, 0, m_subkeys.size());
cipher.ProcessBlock(m_subkeys);
m_subkeys[m_subkeys.size()/2 + T::BLOCKSIZE - 1] = 1;
cipher.ProcessBlock(m_subkeys+m_subkeys.size()/2);
return m_subkeys;
}
NAMESPACE_END
#endif
cbcmac.h
Classes for CBC MAC.
Algorithm::AlgorithmProvider
virtual std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: cryptlib.h:636
CBC_MAC
CBC-MAC.
Definition: cbcmac.h:44
DMAC_Base
DMAC message authentication code base class.
Definition: dmac.h:18
DMAC_Base::UncheckedSetKey
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
Definition: dmac.h:69
DMAC_Base::AlgorithmProvider
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: dmac.h:44
DMAC_Base::Update
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: dmac.h:80
DMAC_Base::TruncatedFinal
void TruncatedFinal(byte *mac, size_t size)
Computes the hash of the current message.
Definition: dmac.h:87
DMAC_Base::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: dmac.h:29
DMAC
DMAC message authentication code.
Definition: dmac.h:56
DMAC::DMAC
DMAC()
Construct a DMAC.
Definition: dmac.h:59
DMAC::DMAC
DMAC(const byte *key, size_t length=DMAC_Base< T >::DEFAULT_KEYLENGTH)
Construct a DMAC.
Definition: dmac.h:64
HashTransformation::TruncatedFinal
virtual void TruncatedFinal(byte *digest, size_t digestSize)=0
Computes the hash of the current message.
HashTransformation::Update
virtual void Update(const byte *input, size_t length)=0
Updates a hash with additional input.
MessageAuthenticationCodeFinal
Provides class member functions to key a message authentication code.
Definition: seckey.h:371
MessageAuthenticationCode
Interface for message authentication codes.
Definition: cryptlib.h:1299
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:322
SameKeyLengthAs
Provides key lengths based on another class's key length.
Definition: seckey.h:218
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:1226
byte
unsigned char byte
8-bit unsigned datatype
Definition: config_int.h:56
UnsignedMin
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be negative and incorrectly promoted.
Definition: misc.h:694
CryptoPP
Crypto++ library namespace.