Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:04

0001 // mdc.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file mdc.h

0004 /// \brief Classes for the MDC message digest

0005 
0006 #ifndef CRYPTOPP_MDC_H
0007 #define CRYPTOPP_MDC_H
0008 
0009 #include "seckey.h"
0010 #include "secblock.h"
0011 #include "misc.h"
0012 
0013 // GCC cast warning

0014 #define HashWordPtr(x) ((HashWordType*)(void*)(x))
0015 #define ConstHashWordPtr(x) ((const HashWordType*)(const void*)(x))
0016 
0017 NAMESPACE_BEGIN(CryptoPP)
0018 
0019 /// \tparam B BlockCipher derived class

0020 /// \brief MDC_Info cipher information

0021 template <class B>
0022 struct MDC_Info : public FixedBlockSize<B::DIGESTSIZE>, public FixedKeyLength<B::BLOCKSIZE>
0023 {
0024     static std::string StaticAlgorithmName() {return std::string("MDC/")+B::StaticAlgorithmName();}
0025 };
0026 
0027 /// \brief MDC cipher

0028 /// \tparam H HashTransformation derived class

0029 /// \details MDC() is a construction by Peter Gutmann to turn an iterated hash function into a PRF

0030 /// \sa <a href="http://www.cryptopp.com/wiki/MDC">MDC</a>

0031 template <class H>
0032 class MDC : public MDC_Info<H>
0033 {
0034     /// \brief MDC cipher encryption operation

0035     class CRYPTOPP_NO_VTABLE Enc : public BlockCipherImpl<MDC_Info<H> >
0036     {
0037         typedef typename H::HashWordType HashWordType;
0038 
0039     public:
0040         void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
0041         {
0042             CRYPTOPP_UNUSED(params);
0043             this->AssertValidKeyLength(length);
0044             ConditionalByteReverse(BIG_ENDIAN_ORDER, Key(), ConstHashWordPtr(userKey), this->KEYLENGTH);
0045         }
0046 
0047         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
0048         {
0049             ConditionalByteReverse(BIG_ENDIAN_ORDER, Buffer(), ConstHashWordPtr(inBlock), this->BLOCKSIZE);
0050             H::Transform(Buffer(), Key());
0051 
0052             if (xorBlock)
0053             {
0054                 ConditionalByteReverse(BIG_ENDIAN_ORDER, Buffer(), Buffer(), this->BLOCKSIZE);
0055                 xorbuf(outBlock, xorBlock, m_buffer, this->BLOCKSIZE);
0056             }
0057             else
0058             {
0059                 ConditionalByteReverse(BIG_ENDIAN_ORDER, HashWordPtr(outBlock), Buffer(), this->BLOCKSIZE);
0060             }
0061         }
0062 
0063         bool IsPermutation() const {return false;}
0064 
0065         unsigned int OptimalDataAlignment() const {return sizeof(HashWordType);}
0066 
0067     private:
0068         HashWordType *Key() {return HashWordPtr(m_key.data());}
0069         const HashWordType *Key() const {return ConstHashWordPtr(m_key.data());}
0070         HashWordType *Buffer() const {return HashWordPtr(m_buffer.data());}
0071 
0072         // VC60 workaround: bug triggered if using FixedSizeAllocatorWithCleanup

0073         FixedSizeSecBlock<byte, MDC_Info<H>::KEYLENGTH, AllocatorWithCleanup<byte> > m_key;
0074         mutable FixedSizeSecBlock<byte, MDC_Info<H>::BLOCKSIZE, AllocatorWithCleanup<byte> > m_buffer;
0075     };
0076 
0077 public:
0078     // use BlockCipher interface

0079     typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
0080 };
0081 
0082 NAMESPACE_END
0083 
0084 #endif