ext-cryptopp/dmac.h

94 lines
2.5 KiB
C
Raw Normal View History

2002-10-04 17:31:41 +00:00
#ifndef CRYPTOPP_DMAC_H
#define CRYPTOPP_DMAC_H
#include "cbcmac.h"
NAMESPACE_BEGIN(CryptoPP)
//! _
2002-10-04 17:31:41 +00:00
template <class T>
2003-05-16 00:53:53 +00:00
class CRYPTOPP_NO_VTABLE DMAC_Base : public SameKeyLengthAs<T>, public MessageAuthenticationCode
2002-10-04 17:31:41 +00:00
{
public:
static std::string StaticAlgorithmName() {return std::string("DMAC(") + T::StaticAlgorithmName() + ")";}
2006-12-14 11:41:39 +00:00
CRYPTOPP_CONSTANT(DIGESTSIZE=T::BLOCKSIZE)
2002-10-04 17:31:41 +00:00
DMAC_Base() {}
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
2005-07-12 04:23:32 +00:00
void Update(const byte *input, size_t length);
void TruncatedFinal(byte *mac, size_t size);
2002-10-04 17:31:41 +00:00
unsigned int DigestSize() const {return DIGESTSIZE;}
private:
2005-07-12 04:23:32 +00:00
byte *GenerateSubKeys(const byte *key, size_t keylength);
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
size_t m_subkeylength;
2002-10-04 17:31:41 +00:00
SecByteBlock m_subkeys;
CBC_MAC<T> m_mac1;
typename T::Encryption m_f2;
unsigned int m_counter;
};
//! DMAC
/*! Based on "CBC MAC for Real-Time Data Sources" by Erez Petrank
and Charles Rackoff. T should be a class derived from BlockCipherDocumentation.
2002-10-04 17:31:41 +00:00
*/
template <class T>
class DMAC : public MessageAuthenticationCodeFinal<DMAC_Base<T> >
2002-10-04 17:31:41 +00:00
{
public:
DMAC() {}
2005-07-12 04:23:32 +00:00
DMAC(const byte *key, size_t length=DMAC_Base<T>::DEFAULT_KEYLENGTH)
2004-06-19 08:28:09 +00:00
{this->SetKey(key, length);}
2002-10-04 17:31:41 +00:00
};
template <class T>
void DMAC_Base<T>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
2002-10-04 17:31:41 +00:00
{
m_subkeylength = T::StaticGetValidKeyLength(T::BLOCKSIZE);
2005-07-12 04:23:32 +00:00
m_subkeys.resize(2*UnsignedMin((unsigned int)T::BLOCKSIZE, m_subkeylength));
2002-10-04 17:31:41 +00:00
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>
2005-07-12 04:23:32 +00:00
void DMAC_Base<T>::Update(const byte *input, size_t length)
2002-10-04 17:31:41 +00:00
{
m_mac1.Update(input, length);
2005-07-12 04:23:32 +00:00
m_counter = (unsigned int)((m_counter + length) % T::BLOCKSIZE);
2002-10-04 17:31:41 +00:00
}
template <class T>
2005-07-12 04:23:32 +00:00
void DMAC_Base<T>::TruncatedFinal(byte *mac, size_t size)
2002-10-04 17:31:41 +00:00
{
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);
2007-10-23 17:27:49 +00:00
m_counter = 0; // reset for next message
2002-10-04 17:31:41 +00:00
}
template <class T>
2005-07-12 04:23:32 +00:00
byte *DMAC_Base<T>::GenerateSubKeys(const byte *key, size_t keylength)
2002-10-04 17:31:41 +00:00
{
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