threefish.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // threefish.h - written and placed in the public domain by Jeffrey Walton
  2. // Based on public domain code by Keru Kuro. Kuro's code is
  3. // available at http://cppcrypto.sourceforge.net/.
  4. /// \file Threefish.h
  5. /// \brief Classes for the Threefish block cipher
  6. /// \since Crypto++ 6.0
  7. #ifndef CRYPTOPP_THREEFISH_H
  8. #define CRYPTOPP_THREEFISH_H
  9. #include "config.h"
  10. #include "seckey.h"
  11. #include "secblock.h"
  12. #include "algparam.h"
  13. #include "argnames.h"
  14. #include "stdcpp.h"
  15. NAMESPACE_BEGIN(CryptoPP)
  16. /// \brief Threefish block cipher information
  17. /// \tparam BS block size of the cipher, in bytes
  18. /// \since Crypto++ 6.0
  19. template <unsigned int BS>
  20. struct Threefish_Info : public FixedBlockSize<BS>, FixedKeyLength<BS>
  21. {
  22. static const std::string StaticAlgorithmName()
  23. {
  24. // Format is Cipher-Blocksize(Keylength)
  25. return "Threefish-" + IntToString(BS*8) + "(" + IntToString(BS*8) + ")";
  26. }
  27. };
  28. /// \brief Threefish block cipher base class
  29. /// \tparam BS block size of the cipher, in bytes
  30. /// \details User code should use Threefish256, Threefish512, Threefish1024
  31. /// \sa Threefish256, Threefish512, Threefish1024, <a href="http://www.cryptopp.com/wiki/Threefish">Threefish</a>
  32. /// \since Crypto++ 6.0
  33. template <unsigned int BS>
  34. struct CRYPTOPP_NO_VTABLE Threefish_Base
  35. {
  36. virtual ~Threefish_Base() {}
  37. void SetTweak(const NameValuePairs &params)
  38. {
  39. m_tweak.New(3);
  40. ConstByteArrayParameter t;
  41. if (params.GetValue(Name::Tweak(), t))
  42. {
  43. // Tweak size is fixed at 16 for Threefish
  44. CRYPTOPP_ASSERT(t.size() == 16);
  45. GetUserKey(LITTLE_ENDIAN_ORDER, m_tweak.begin(), 2, t.begin(), 16);
  46. m_tweak[2] = m_tweak[0] ^ m_tweak[1];
  47. }
  48. else
  49. {
  50. std::memset(m_tweak.begin(), 0x00, 24);
  51. }
  52. }
  53. typedef SecBlock<word64, AllocatorWithCleanup<word64, true> > AlignedSecBlock64;
  54. mutable AlignedSecBlock64 m_wspace; // workspace
  55. AlignedSecBlock64 m_rkey; // keys
  56. AlignedSecBlock64 m_tweak;
  57. };
  58. /// \brief Threefish 256-bit block cipher
  59. /// \details Threefish256 provides 256-bit block size. The valid key size is 256-bit.
  60. /// \note Crypto++ provides a byte oriented implementation
  61. /// \sa Threefish512, Threefish1024, <a href="http://www.cryptopp.com/wiki/Threefish">Threefish</a>
  62. /// \since Crypto++ 6.0
  63. class CRYPTOPP_NO_VTABLE Threefish256 : public Threefish_Info<32>, public BlockCipherDocumentation
  64. {
  65. public:
  66. /// \brief Threefish block cipher transformation functions
  67. /// \details Provides implementation common to encryption and decryption
  68. /// \since Crypto++ 6.0
  69. class CRYPTOPP_NO_VTABLE Base : public Threefish_Base<32>, public BlockCipherImpl<Threefish_Info<32> >
  70. {
  71. protected:
  72. void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
  73. };
  74. /// \brief Encryption transformation
  75. /// \details Enc provides implementation for encryption transformation. All key and block
  76. /// sizes are supported.
  77. /// \since Crypto++ 6.0
  78. class CRYPTOPP_NO_VTABLE Enc : public Base
  79. {
  80. protected:
  81. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  82. };
  83. /// \brief Decryption transformation
  84. /// \details Dec provides implementation for decryption transformation. All key and block
  85. /// sizes are supported.
  86. /// \since Crypto++ 6.0
  87. class CRYPTOPP_NO_VTABLE Dec : public Base
  88. {
  89. protected:
  90. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  91. };
  92. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  93. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  94. };
  95. typedef Threefish256::Encryption Threefish256Encryption;
  96. typedef Threefish256::Decryption Threefish256Decryption;
  97. /// \brief Threefish 512-bit block cipher
  98. /// \details Threefish512 provides 512-bit block size. The valid key size is 512-bit.
  99. /// \note Crypto++ provides a byte oriented implementation
  100. /// \sa Threefish256, Threefish1024, <a href="http://www.cryptopp.com/wiki/Threefish">Threefish</a>
  101. /// \since Crypto++ 6.0
  102. class CRYPTOPP_NO_VTABLE Threefish512 : public Threefish_Info<64>, public BlockCipherDocumentation
  103. {
  104. public:
  105. /// \brief Threefish block cipher transformation functions
  106. /// \details Provides implementation common to encryption and decryption
  107. /// \since Crypto++ 6.0
  108. class CRYPTOPP_NO_VTABLE Base : public Threefish_Base<64>, public BlockCipherImpl<Threefish_Info<64> >
  109. {
  110. protected:
  111. void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
  112. };
  113. /// \brief Encryption transformation
  114. /// \details Enc provides implementation for encryption transformation. All key and block
  115. /// sizes are supported.
  116. /// \since Crypto++ 6.0
  117. class CRYPTOPP_NO_VTABLE Enc : public Base
  118. {
  119. protected:
  120. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  121. };
  122. /// \brief Decryption transformation
  123. /// \details Dec provides implementation for decryption transformation. All key and block
  124. /// sizes are supported.
  125. /// \since Crypto++ 6.0
  126. class CRYPTOPP_NO_VTABLE Dec : public Base
  127. {
  128. protected:
  129. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  130. };
  131. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  132. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  133. };
  134. typedef Threefish512::Encryption Threefish512Encryption;
  135. typedef Threefish512::Decryption Threefish512Decryption;
  136. /// \brief Threefish 1024-bit block cipher
  137. /// \details Threefish1024 provides 1024-bit block size. The valid key size is 1024-bit.
  138. /// \note Crypto++ provides a byte oriented implementation
  139. /// \sa Threefish256, Threefish512, <a href="http://www.cryptopp.com/wiki/Threefish">Threefish</a>
  140. /// \since Crypto++ 6.0
  141. class CRYPTOPP_NO_VTABLE Threefish1024 : public Threefish_Info<128>, public BlockCipherDocumentation
  142. {
  143. public:
  144. /// \brief Threefish block cipher transformation functions
  145. /// \details Provides implementation common to encryption and decryption
  146. /// \since Crypto++ 6.0
  147. class CRYPTOPP_NO_VTABLE Base : public Threefish_Base<128>, public BlockCipherImpl<Threefish_Info<128> >
  148. {
  149. protected:
  150. void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
  151. };
  152. /// \brief Encryption transformation
  153. /// \details Enc provides implementation for encryption transformation. All key and block
  154. /// sizes are supported.
  155. /// \since Crypto++ 6.0
  156. class CRYPTOPP_NO_VTABLE Enc : public Base
  157. {
  158. protected:
  159. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  160. };
  161. /// \brief Encryption transformation
  162. /// \details Dec provides implementation for decryption transformation. All key and block
  163. /// sizes are supported.
  164. /// \since Crypto++ 6.0
  165. class CRYPTOPP_NO_VTABLE Dec : public Base
  166. {
  167. protected:
  168. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  169. };
  170. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  171. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  172. };
  173. typedef Threefish1024::Encryption Threefish1024Encryption;
  174. typedef Threefish1024::Decryption Threefish1024Decryption;
  175. NAMESPACE_END
  176. #endif // CRYPTOPP_THREEFISH_H