Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

dmac.h

00001 #ifndef CRYPTOPP_DMAC_H
00002 #define CRYPTOPP_DMAC_H
00003 
00004 #include "cbcmac.h"
00005 
00006 NAMESPACE_BEGIN(CryptoPP)
00007 
00008 template <class T>
00009 class DMAC_Base : public SameKeyLengthAs<T>, public MessageAuthenticationCode
00010 {
00011 public:
00012         static std::string StaticAlgorithmName() {return std::string("DMAC(") + T::StaticAlgorithmName() + ")";}
00013 
00014         enum {DIGESTSIZE=T::BLOCKSIZE};
00015 
00016         DMAC_Base() {}
00017 
00018         void CheckedSetKey(void *, Empty empty, const byte *key, unsigned int length, const NameValuePairs &params);
00019         void Update(const byte *input, unsigned int length);
00020         void TruncatedFinal(byte *mac, unsigned int size);
00021         unsigned int DigestSize() const {return DIGESTSIZE;}
00022 
00023 private:
00024         byte *GenerateSubKeys(const byte *key, unsigned int keylength);
00025 
00026         unsigned int m_subkeylength;
00027         SecByteBlock m_subkeys;
00028         CBC_MAC<T> m_mac1;
00029         typename T::Encryption m_f2;
00030         unsigned int m_counter;
00031 };
00032 
00033 //! DMAC
00034 /*! Based on "CBC MAC for Real-Time Data Sources" by Erez Petrank
00035         and Charles Rackoff. T should be BlockTransformation class.
00036 */
00037 template <class T>
00038 class DMAC : public MessageAuthenticationCodeTemplate<DMAC_Base<T> >
00039 {
00040 public:
00041         DMAC() {}
00042         DMAC(const byte *key, unsigned int length=DMAC_Base<T>::DEFAULT_KEYLENGTH)
00043                 {SetKey(key, length);}
00044 };
00045 
00046 template <class T>
00047 void DMAC_Base<T>::CheckedSetKey(void *, Empty empty, const byte *key, unsigned int length, const NameValuePairs &params)
00048 {
00049         m_subkeylength = T::StaticGetValidKeyLength(T::BLOCKSIZE);
00050         m_subkeys.resize(2*STDMAX((unsigned int)T::BLOCKSIZE, m_subkeylength));
00051         m_mac1.SetKey(GenerateSubKeys(key, length), m_subkeylength, params);
00052         m_f2.SetKey(m_subkeys+m_subkeys.size()/2, m_subkeylength, params);
00053         m_counter = 0;
00054         m_subkeys.resize(0);
00055 }
00056 
00057 template <class T>
00058 void DMAC_Base<T>::Update(const byte *input, unsigned int length)
00059 {
00060         m_mac1.Update(input, length);
00061         m_counter = (m_counter + length) % T::BLOCKSIZE;
00062 }
00063 
00064 template <class T>
00065 void DMAC_Base<T>::TruncatedFinal(byte *mac, unsigned int size)
00066 {
00067         ThrowIfInvalidTruncatedSize(size);
00068 
00069         byte pad[T::BLOCKSIZE];
00070         byte padByte = byte(T::BLOCKSIZE-m_counter);
00071         memset(pad, padByte, padByte);
00072         m_mac1.Update(pad, padByte);
00073         m_mac1.TruncatedFinal(mac, size);
00074         m_f2.ProcessBlock(mac);
00075 }
00076 
00077 template <class T>
00078 byte *DMAC_Base<T>::GenerateSubKeys(const byte *key, unsigned int keylength)
00079 {
00080         typename T::Encryption cipher(key, keylength);
00081         memset(m_subkeys, 0, m_subkeys.size());
00082         cipher.ProcessBlock(m_subkeys);
00083         m_subkeys[m_subkeys.size()/2 + T::BLOCKSIZE - 1] = 1;
00084         cipher.ProcessBlock(m_subkeys+m_subkeys.size()/2);
00085         return m_subkeys;
00086 }
00087 
00088 NAMESPACE_END
00089 
00090 #endif

Generated on Mon Apr 19 18:12:29 2004 for Crypto++ by doxygen 1.3.6-20040222