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.

136 lines
3.6 KiB

  1. // tea.h - written and placed in the public domain by Wei Dai
  2. //! \file tea.h
  3. //! \brief Classes for the TEA, BTEA and XTEA block ciphers
  4. #ifndef CRYPTOPP_TEA_H
  5. #define CRYPTOPP_TEA_H
  6. #include "seckey.h"
  7. #include "secblock.h"
  8. #include "misc.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. //! _
  11. struct TEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public VariableRounds<32>
  12. {
  13. static const char *StaticAlgorithmName() {return "TEA";}
  14. };
  15. /// <a href="http://www.weidai.com/scan-mirror/cs.html#TEA">TEA</a>
  16. class TEA : public TEA_Info, public BlockCipherDocumentation
  17. {
  18. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<TEA_Info>
  19. {
  20. public:
  21. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
  22. protected:
  23. FixedSizeSecBlock<word32, 4> m_k;
  24. word32 m_limit;
  25. };
  26. class CRYPTOPP_NO_VTABLE Enc : public Base
  27. {
  28. public:
  29. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  30. };
  31. class CRYPTOPP_NO_VTABLE Dec : public Base
  32. {
  33. public:
  34. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  35. };
  36. public:
  37. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  38. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  39. };
  40. typedef TEA::Encryption TEAEncryption;
  41. typedef TEA::Decryption TEADecryption;
  42. //! _
  43. struct XTEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public VariableRounds<32>
  44. {
  45. static const char *StaticAlgorithmName() {return "XTEA";}
  46. };
  47. /// <a href="http://www.weidai.com/scan-mirror/cs.html#TEA">XTEA</a>
  48. class XTEA : public XTEA_Info, public BlockCipherDocumentation
  49. {
  50. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<XTEA_Info>
  51. {
  52. public:
  53. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
  54. protected:
  55. FixedSizeSecBlock<word32, 4> m_k;
  56. word32 m_limit;
  57. };
  58. class CRYPTOPP_NO_VTABLE Enc : public Base
  59. {
  60. public:
  61. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  62. };
  63. class CRYPTOPP_NO_VTABLE Dec : public Base
  64. {
  65. public:
  66. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  67. };
  68. public:
  69. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  70. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  71. };
  72. //! _
  73. struct BTEA_Info : public FixedKeyLength<16>
  74. {
  75. static const char *StaticAlgorithmName() {return "BTEA";}
  76. };
  77. //! <a href="http://www.weidai.com/scan-mirror/cs.html#TEA">corrected Block TEA</a> (as described in "xxtea").
  78. /*! This class hasn't been tested yet. */
  79. class BTEA : public BTEA_Info, public BlockCipherDocumentation
  80. {
  81. class CRYPTOPP_NO_VTABLE Base : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BlockCipher, BTEA_Info>, BTEA_Info>, public BTEA_Info
  82. {
  83. public:
  84. void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
  85. {
  86. CRYPTOPP_UNUSED(length), CRYPTOPP_UNUSED(params);
  87. m_blockSize = params.GetIntValueWithDefault("BlockSize", 60*4);
  88. GetUserKey(BIG_ENDIAN_ORDER, m_k.begin(), 4, key, KEYLENGTH);
  89. }
  90. unsigned int BlockSize() const {return m_blockSize;}
  91. protected:
  92. FixedSizeSecBlock<word32, 4> m_k;
  93. unsigned int m_blockSize;
  94. };
  95. class CRYPTOPP_NO_VTABLE Enc : public Base
  96. {
  97. public:
  98. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  99. };
  100. class CRYPTOPP_NO_VTABLE Dec : public Base
  101. {
  102. public:
  103. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  104. };
  105. public:
  106. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  107. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  108. };
  109. NAMESPACE_END
  110. #endif