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.

144 lines
3.8 KiB

  1. #ifndef CRYPTOPP_PANAMA_H
  2. #define CRYPTOPP_PANAMA_H
  3. #include "strciphr.h"
  4. #include "iterhash.h"
  5. NAMESPACE_BEGIN(CryptoPP)
  6. /// base class, do not use directly
  7. template <class B>
  8. class CRYPTOPP_NO_VTABLE Panama
  9. {
  10. public:
  11. void Reset();
  12. void Iterate(size_t count, const word32 *p=NULL, word32 *z=NULL, const word32 *y=NULL);
  13. protected:
  14. typedef word32 Stage[8];
  15. CRYPTOPP_CONSTANT(STAGES = 32)
  16. FixedSizeAlignedSecBlock<word32, 20 + 8*32> m_state;
  17. };
  18. namespace Weak {
  19. /// <a href="http://www.weidai.com/scan-mirror/md.html#Panama">Panama Hash</a>
  20. template <class B = LittleEndian>
  21. class PanamaHash : protected Panama<B>, public AlgorithmImpl<IteratedHash<word32, NativeByteOrder, 32>, PanamaHash<B> >
  22. {
  23. public:
  24. CRYPTOPP_CONSTANT(DIGESTSIZE = 32)
  25. PanamaHash() {Panama<B>::Reset();}
  26. unsigned int DigestSize() const {return DIGESTSIZE;}
  27. void TruncatedFinal(byte *hash, size_t size);
  28. static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
  29. protected:
  30. void Init() {Panama<B>::Reset();}
  31. void HashEndianCorrectedBlock(const word32 *data) {this->Iterate(1, data);} // push
  32. size_t HashMultipleBlocks(const word32 *input, size_t length);
  33. word32* StateBuf() {return NULL;}
  34. };
  35. }
  36. //! MAC construction using a hermetic hash function
  37. template <class T_Hash, class T_Info = T_Hash>
  38. class HermeticHashFunctionMAC : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<MessageAuthenticationCode, VariableKeyLength<32, 0, INT_MAX> > >, T_Info>
  39. {
  40. public:
  41. void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
  42. {
  43. m_key.Assign(key, length);
  44. Restart();
  45. }
  46. void Restart()
  47. {
  48. m_hash.Restart();
  49. m_keyed = false;
  50. }
  51. void Update(const byte *input, size_t length)
  52. {
  53. if (!m_keyed)
  54. KeyHash();
  55. m_hash.Update(input, length);
  56. }
  57. void TruncatedFinal(byte *digest, size_t digestSize)
  58. {
  59. if (!m_keyed)
  60. KeyHash();
  61. m_hash.TruncatedFinal(digest, digestSize);
  62. m_keyed = false;
  63. }
  64. unsigned int DigestSize() const
  65. {return m_hash.DigestSize();}
  66. unsigned int BlockSize() const
  67. {return m_hash.BlockSize();}
  68. unsigned int OptimalBlockSize() const
  69. {return m_hash.OptimalBlockSize();}
  70. unsigned int OptimalDataAlignment() const
  71. {return m_hash.OptimalDataAlignment();}
  72. protected:
  73. void KeyHash()
  74. {
  75. m_hash.Update(m_key, m_key.size());
  76. m_keyed = true;
  77. }
  78. T_Hash m_hash;
  79. bool m_keyed;
  80. SecByteBlock m_key;
  81. };
  82. namespace Weak {
  83. /// Panama MAC
  84. template <class B = LittleEndian>
  85. class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
  86. {
  87. public:
  88. PanamaMAC() {}
  89. PanamaMAC(const byte *key, unsigned int length)
  90. {this->SetKey(key, length);}
  91. };
  92. }
  93. //! algorithm info
  94. template <class B>
  95. struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32>
  96. {
  97. static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
  98. };
  99. //! _
  100. template <class B>
  101. class PanamaCipherPolicy : public AdditiveCipherConcretePolicy<word32, 8>,
  102. public PanamaCipherInfo<B>,
  103. protected Panama<B>
  104. {
  105. protected:
  106. void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
  107. void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
  108. bool CipherIsRandomAccess() const {return false;}
  109. void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
  110. #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X64
  111. unsigned int GetAlignment() const;
  112. #endif
  113. FixedSizeSecBlock<word32, 8> m_key;
  114. };
  115. //! <a href="http://www.cryptolounge.org/wiki/PANAMA">Panama Stream Cipher</a>
  116. template <class B = LittleEndian>
  117. struct PanamaCipher : public PanamaCipherInfo<B>, public SymmetricCipherDocumentation
  118. {
  119. typedef SymmetricCipherFinal<ConcretePolicyHolder<PanamaCipherPolicy<B>, AdditiveCipherTemplate<> >, PanamaCipherInfo<B> > Encryption;
  120. typedef Encryption Decryption;
  121. };
  122. NAMESPACE_END
  123. #endif