Crypto++
mdc.h
Go to the documentation of this file.
00001  // mdc.h - written and placed in the public domain by Wei Dai
00002 
00003 #ifndef CRYPTOPP_MDC_H
00004 #define CRYPTOPP_MDC_H
00005 
00006 /** \file
00007 */
00008 
00009 #include "seckey.h"
00010 #include "misc.h"
00011 
00012 NAMESPACE_BEGIN(CryptoPP)
00013 
00014 //! _
00015 template <class T>
00016 struct MDC_Info : public FixedBlockSize<T::DIGESTSIZE>, public FixedKeyLength<T::BLOCKSIZE>
00017 {
00018     static std::string StaticAlgorithmName() {return std::string("MDC/")+T::StaticAlgorithmName();}
00019 };
00020 
00021 //! <a href="http://www.weidai.com/scan-mirror/cs.html#MDC">MDC</a>
00022 /*! a construction by Peter Gutmann to turn an iterated hash function into a PRF */
00023 template <class T>
00024 class MDC : public MDC_Info<T>
00025 {
00026     class CRYPTOPP_NO_VTABLE Enc : public BlockCipherImpl<MDC_Info<T> >
00027     {
00028         typedef typename T::HashWordType HashWordType;
00029 
00030     public:
00031         void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
00032         {
00033             this->AssertValidKeyLength(length);
00034             memcpy_s(m_key, m_key.size(), userKey, this->KEYLENGTH);
00035             T::CorrectEndianess(Key(), Key(), this->KEYLENGTH);
00036         }
00037 
00038         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
00039         {
00040             T::CorrectEndianess(Buffer(), (HashWordType *)inBlock, this->BLOCKSIZE);
00041             T::Transform(Buffer(), Key());
00042             if (xorBlock)
00043             {
00044                 T::CorrectEndianess(Buffer(), Buffer(), this->BLOCKSIZE);
00045                 xorbuf(outBlock, xorBlock, m_buffer, this->BLOCKSIZE);
00046             }
00047             else
00048                 T::CorrectEndianess((HashWordType *)outBlock, Buffer(), this->BLOCKSIZE);
00049         }
00050 
00051         bool IsPermutation() const {return false;}
00052 
00053         unsigned int OptimalDataAlignment() const {return sizeof(HashWordType);}
00054 
00055     private:
00056         HashWordType *Key() {return (HashWordType *)m_key.data();}
00057         const HashWordType *Key() const {return (const HashWordType *)m_key.data();}
00058         HashWordType *Buffer() const {return (HashWordType *)m_buffer.data();}
00059 
00060         // VC60 workaround: bug triggered if using FixedSizeAllocatorWithCleanup
00061         FixedSizeSecBlock<byte, MDC_Info<T>::KEYLENGTH, AllocatorWithCleanup<byte> > m_key;
00062         mutable FixedSizeSecBlock<byte, MDC_Info<T>::BLOCKSIZE, AllocatorWithCleanup<byte> > m_buffer;
00063     };
00064 
00065 public:
00066     //! use BlockCipher interface
00067     typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
00068 };
00069 
00070 NAMESPACE_END
00071 
00072 #endif