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

# 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(CipherDir direction, const byte *userKey, unsigned int length)
    00032                 {
    00033                         assert(direction == ENCRYPTION);
    00034                         this->AssertValidKeyLength(length);
    00035                         memcpy(Key(), userKey, this->KEYLENGTH);
    00036                         T::CorrectEndianess(Key(), Key(), this->KEYLENGTH);
    00037                 }
    00038
    00039                 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
    00040                 {
    00041                         T::CorrectEndianess(Buffer(), (HashWordType *)inBlock, this->BLOCKSIZE);
    00042                         T::Transform(Buffer(), Key());
    00043                         if (xorBlock)
    00044                         {
    00045                                 T::CorrectEndianess(Buffer(), Buffer(), this->BLOCKSIZE);
    00046                                 xorbuf(outBlock, xorBlock, m_buffer, this->BLOCKSIZE);
    00047                         }
    00048                         else
    00049                                 T::CorrectEndianess((HashWordType *)outBlock, Buffer(), this->BLOCKSIZE);
    00050                 }
    00051
    00052                 bool IsPermutation() const {return false;}
    00053
    00054                 unsigned int GetAlignment() const {return sizeof(HashWordType);}
    00055
    00056         private:
    00057                 HashWordType *Key() {return (HashWordType *)m_key.data();}
    00058                 const HashWordType *Key() const {return (const HashWordType *)m_key.data();}
    00059                 HashWordType *Buffer() const {return (HashWordType *)m_buffer.data();}
    00060
    00061                 // VC60 workaround: bug triggered if using FixedSizeAllocatorWithCleanup
    00062                 FixedSizeSecBlock<byte, MDC_Info<T>::KEYLENGTH, AllocatorWithCleanup<byte> > m_key;
    00063                 mutable FixedSizeSecBlock<byte, MDC_Info<T>::BLOCKSIZE, AllocatorWithCleanup<byte> > m_buffer;
    00064         };
    00065
    00066 public:
    00067         //! use BlockCipher interface
    00068         typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
    00069 };
    00070
    00071 NAMESPACE_END
    00072
    00073 #endif