tea.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // tea.h - originally 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. /// \brief TEA block cipher information
  11. struct TEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public VariableRounds<32>
  12. {
  13. /// \brief The algorithm name
  14. /// \return the algorithm name
  15. /// \details StaticAlgorithmName returns the algorithm's name as a static
  16. /// member function.
  17. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "TEA";}
  18. };
  19. /// \brief TEA block cipher
  20. /// \sa <a href="http://www.cryptopp.com/wiki/TEA">TEA</a>
  21. class TEA : public TEA_Info, public BlockCipherDocumentation
  22. {
  23. /// \brief TEA block cipher default operation
  24. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<TEA_Info>
  25. {
  26. public:
  27. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
  28. protected:
  29. FixedSizeSecBlock<word32, 4> m_k;
  30. word32 m_limit;
  31. };
  32. /// \brief TEA block cipher encryption operation
  33. class CRYPTOPP_NO_VTABLE Enc : public Base
  34. {
  35. public:
  36. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  37. };
  38. /// \brief TEA block cipher decryption operation
  39. class CRYPTOPP_NO_VTABLE Dec : public Base
  40. {
  41. public:
  42. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  43. };
  44. public:
  45. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  46. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  47. };
  48. typedef TEA::Encryption TEAEncryption;
  49. typedef TEA::Decryption TEADecryption;
  50. /// \brief XTEA block cipher information
  51. struct XTEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public VariableRounds<32>
  52. {
  53. /// \brief The algorithm name
  54. /// \return the algorithm name
  55. /// \details StaticAlgorithmName returns the algorithm's name as a static
  56. /// member function.
  57. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "XTEA";}
  58. };
  59. /// \brief XTEA block cipher
  60. /// \sa <a href="http://www.cryptopp.com/wiki/TEA">XTEA</a>
  61. class XTEA : public XTEA_Info, public BlockCipherDocumentation
  62. {
  63. /// \brief XTEA block cipher default operation
  64. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<XTEA_Info>
  65. {
  66. public:
  67. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
  68. protected:
  69. FixedSizeSecBlock<word32, 4> m_k;
  70. word32 m_limit;
  71. };
  72. /// \brief XTEA block cipher encryption operation
  73. class CRYPTOPP_NO_VTABLE Enc : public Base
  74. {
  75. public:
  76. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  77. };
  78. /// \brief XTEA block cipher decryption operation
  79. class CRYPTOPP_NO_VTABLE Dec : public Base
  80. {
  81. public:
  82. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  83. };
  84. public:
  85. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  86. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  87. };
  88. /// \brief BTEA block cipher information
  89. struct BTEA_Info : public FixedKeyLength<16>
  90. {
  91. /// \brief The algorithm name
  92. /// \return the algorithm name
  93. /// \details StaticAlgorithmName returns the algorithm's name as a static
  94. /// member function.
  95. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "BTEA";}
  96. };
  97. /// \brief BTEA block cipher
  98. /// \details Corrected Block TEA as described in "xxtea". This class hasn't been tested yet.
  99. /// \sa <A HREF="http://www.movable-type.co.uk/scripts/xxtea.pdf">Correction to xtea</A> and
  100. /// <a href="http://www.cryptopp.com/wiki/TEA">Corrected Block TEA</a>.
  101. class BTEA : public BTEA_Info, public BlockCipherDocumentation
  102. {
  103. /// \brief BTEA block cipher default operation
  104. class CRYPTOPP_NO_VTABLE Base : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BlockCipher, BTEA_Info>, BTEA_Info>
  105. {
  106. public:
  107. void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
  108. {
  109. CRYPTOPP_UNUSED(length), CRYPTOPP_UNUSED(params);
  110. m_blockSize = params.GetIntValueWithDefault("BlockSize", 60*4);
  111. GetUserKey(BIG_ENDIAN_ORDER, m_k.begin(), 4, key, KEYLENGTH);
  112. }
  113. unsigned int BlockSize() const {return m_blockSize;}
  114. protected:
  115. FixedSizeSecBlock<word32, 4> m_k;
  116. unsigned int m_blockSize;
  117. };
  118. /// \brief BTEA block cipher encryption operation
  119. class CRYPTOPP_NO_VTABLE Enc : public Base
  120. {
  121. public:
  122. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  123. };
  124. /// \brief BTEA block cipher decryption operation
  125. class CRYPTOPP_NO_VTABLE Dec : public Base
  126. {
  127. public:
  128. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  129. };
  130. public:
  131. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  132. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  133. };
  134. NAMESPACE_END
  135. #endif