Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.1 KiB

  1. // mdc.h - written and placed in the public domain by Wei Dai
  2. #ifndef CRYPTOPP_MDC_H
  3. #define CRYPTOPP_MDC_H
  4. /** \file
  5. */
  6. #include "seckey.h"
  7. #include "misc.h"
  8. NAMESPACE_BEGIN(CryptoPP)
  9. //! _
  10. template <class T>
  11. struct MDC_Info : public FixedBlockSize<T::DIGESTSIZE>, public FixedKeyLength<T::BLOCKSIZE>
  12. {
  13. static std::string StaticAlgorithmName() {return std::string("MDC/")+T::StaticAlgorithmName();}
  14. };
  15. //! <a href="http://www.weidai.com/scan-mirror/cs.html#MDC">MDC</a>
  16. /*! a construction by Peter Gutmann to turn an iterated hash function into a PRF */
  17. template <class T>
  18. class MDC : public MDC_Info<T>
  19. {
  20. class CRYPTOPP_NO_VTABLE Enc : public BlockCipherImpl<MDC_Info<T> >
  21. {
  22. typedef typename T::HashWordType HashWordType;
  23. public:
  24. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
  25. {
  26. this->AssertValidKeyLength(length);
  27. memcpy_s(m_key, m_key.size(), userKey, this->KEYLENGTH);
  28. T::CorrectEndianess(Key(), Key(), this->KEYLENGTH);
  29. }
  30. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  31. {
  32. T::CorrectEndianess(Buffer(), (HashWordType *)inBlock, this->BLOCKSIZE);
  33. T::Transform(Buffer(), Key());
  34. if (xorBlock)
  35. {
  36. T::CorrectEndianess(Buffer(), Buffer(), this->BLOCKSIZE);
  37. xorbuf(outBlock, xorBlock, m_buffer, this->BLOCKSIZE);
  38. }
  39. else
  40. T::CorrectEndianess((HashWordType *)outBlock, Buffer(), this->BLOCKSIZE);
  41. }
  42. bool IsPermutation() const {return false;}
  43. unsigned int OptimalDataAlignment() const {return sizeof(HashWordType);}
  44. private:
  45. HashWordType *Key() {return (HashWordType *)m_key.data();}
  46. const HashWordType *Key() const {return (const HashWordType *)m_key.data();}
  47. HashWordType *Buffer() const {return (HashWordType *)m_buffer.data();}
  48. // VC60 workaround: bug triggered if using FixedSizeAllocatorWithCleanup
  49. FixedSizeSecBlock<byte, MDC_Info<T>::KEYLENGTH, AllocatorWithCleanup<byte> > m_key;
  50. mutable FixedSizeSecBlock<byte, MDC_Info<T>::BLOCKSIZE, AllocatorWithCleanup<byte> > m_buffer;
  51. };
  52. public:
  53. //! use BlockCipher interface
  54. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  55. };
  56. NAMESPACE_END
  57. #endif