2017-01-27 12:05:45 +00:00
|
|
|
// dmac.h - originally written and placed in the public domain by Wei Dai
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2018-01-22 14:53:49 +00:00
|
|
|
/// \file dmac.h
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Classes for DMAC message authentication code
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#ifndef CRYPTOPP_DMAC_H
|
|
|
|
#define CRYPTOPP_DMAC_H
|
|
|
|
|
|
|
|
#include "cbcmac.h"
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief DMAC message authentication code base class
|
|
|
|
/// \tparam T class derived from BlockCipherDocumentation
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T>
|
|
|
|
class CRYPTOPP_NO_VTABLE DMAC_Base : public SameKeyLengthAs<T>, public MessageAuthenticationCode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CRYPTOPP_CONSTANT(DIGESTSIZE=T::BLOCKSIZE)
|
2018-01-22 14:53:49 +00:00
|
|
|
static std::string StaticAlgorithmName() {return std::string("DMAC(") + T::StaticAlgorithmName() + ")";}
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2018-01-22 14:53:49 +00:00
|
|
|
virtual~DMAC_Base() {}
|
2015-11-18 20:32:28 +00:00
|
|
|
DMAC_Base() : m_subkeylength(0), m_counter(0) {}
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms);
|
|
|
|
void Update(const byte *input, size_t length);
|
|
|
|
void TruncatedFinal(byte *mac, size_t size);
|
|
|
|
unsigned int DigestSize() const {return DIGESTSIZE;}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \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
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T>
|
|
|
|
class DMAC : public MessageAuthenticationCodeFinal<DMAC_Base<T> >
|
|
|
|
{
|
|
|
|
public:
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Construct a DMAC
|
2015-11-05 06:59:46 +00:00
|
|
|
DMAC() {}
|
2016-10-26 03:43:40 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Construct a DMAC
|
|
|
|
/// \param key a byte array used to key the cipher
|
|
|
|
/// \param length the size of the byte array, in bytes
|
2015-11-05 06:59:46 +00:00
|
|
|
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 ¶ms)
|
|
|
|
{
|
|
|
|
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
|