Team Fortress 2 Source Code as on 22/4/2020
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.

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