00001
00002
00003
00004
00005
00006
00007 #ifndef CRYPTOPP_DMAC_H
00008 #define CRYPTOPP_DMAC_H
00009
00010 #include "cbcmac.h"
00011
00012 NAMESPACE_BEGIN(CryptoPP)
00013
00014
00015 template <class T>
00016 class CRYPTOPP_NO_VTABLE DMAC_Base : public SameKeyLengthAs<T>, public MessageAuthenticationCode
00017 {
00018 public:
00019 static std::string StaticAlgorithmName() {return std::string("DMAC(") + T::StaticAlgorithmName() + ")";}
00020
00021 CRYPTOPP_CONSTANT(DIGESTSIZE=T::BLOCKSIZE)
00022
00023 DMAC_Base() : m_subkeylength(0), m_counter(0) {}
00024
00025 void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms);
00026 void Update(const byte *input, size_t length);
00027 void TruncatedFinal(byte *mac, size_t size);
00028 unsigned int DigestSize() const {return DIGESTSIZE;}
00029
00030 private:
00031 byte *GenerateSubKeys(const byte *key, size_t keylength);
00032
00033 size_t m_subkeylength;
00034 SecByteBlock m_subkeys;
00035 CBC_MAC<T> m_mac1;
00036 typename T::Encryption m_f2;
00037 unsigned int m_counter;
00038 };
00039
00040
00041
00042
00043
00044 template <class T>
00045 class DMAC : public MessageAuthenticationCodeFinal<DMAC_Base<T> >
00046 {
00047 public:
00048 DMAC() {}
00049 DMAC(const byte *key, size_t length=DMAC_Base<T>::DEFAULT_KEYLENGTH)
00050 {this->SetKey(key, length);}
00051 };
00052
00053 template <class T>
00054 void DMAC_Base<T>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
00055 {
00056 m_subkeylength = T::StaticGetValidKeyLength(T::BLOCKSIZE);
00057 m_subkeys.resize(2*UnsignedMin((unsigned int)T::BLOCKSIZE, m_subkeylength));
00058 m_mac1.SetKey(GenerateSubKeys(key, length), m_subkeylength, params);
00059 m_f2.SetKey(m_subkeys+m_subkeys.size()/2, m_subkeylength, params);
00060 m_counter = 0;
00061 m_subkeys.resize(0);
00062 }
00063
00064 template <class T>
00065 void DMAC_Base<T>::Update(const byte *input, size_t length)
00066 {
00067 m_mac1.Update(input, length);
00068 m_counter = (unsigned int)((m_counter + length) % T::BLOCKSIZE);
00069 }
00070
00071 template <class T>
00072 void DMAC_Base<T>::TruncatedFinal(byte *mac, size_t size)
00073 {
00074 ThrowIfInvalidTruncatedSize(size);
00075
00076 byte pad[T::BLOCKSIZE];
00077 byte padByte = byte(T::BLOCKSIZE-m_counter);
00078 memset(pad, padByte, padByte);
00079 m_mac1.Update(pad, padByte);
00080 m_mac1.TruncatedFinal(mac, size);
00081 m_f2.ProcessBlock(mac);
00082
00083 m_counter = 0;
00084 }
00085
00086 template <class T>
00087 byte *DMAC_Base<T>::GenerateSubKeys(const byte *key, size_t keylength)
00088 {
00089 typename T::Encryption cipher(key, keylength);
00090 memset(m_subkeys, 0, m_subkeys.size());
00091 cipher.ProcessBlock(m_subkeys);
00092 m_subkeys[m_subkeys.size()/2 + T::BLOCKSIZE - 1] = 1;
00093 cipher.ProcessBlock(m_subkeys+m_subkeys.size()/2);
00094 return m_subkeys;
00095 }
00096
00097 NAMESPACE_END
00098
00099 #endif