speck.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // speck.h - written and placed in the public domain by Jeffrey Walton
  2. /// \file speck.h
  3. /// \brief Classes for the Speck block cipher
  4. /// \details Speck is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
  5. /// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
  6. /// \sa <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SPECK Families of
  7. /// Lightweight Block Ciphers</A>, <A HREF="http://iadgov.github.io/simon-speck/">
  8. /// The Simon and Speck GitHub</A> and <A HREF="https://www.cryptopp.com/wiki/SPECK">
  9. /// SPECK</A> on the Crypto++ wiki.
  10. /// \since Crypto++ 6.0
  11. #ifndef CRYPTOPP_SPECK_H
  12. #define CRYPTOPP_SPECK_H
  13. #include "config.h"
  14. #include "seckey.h"
  15. #include "secblock.h"
  16. #if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86 || \
  17. CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8 || \
  18. CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64
  19. # ifndef CRYPTOPP_DISABLE_SPECK_SIMD
  20. # define CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS 1
  21. # endif
  22. #endif
  23. // Yet another SunStudio/SunCC workaround. Failed self tests
  24. // in SSE code paths on i386 for SunStudio 12.3 and below.
  25. #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120)
  26. # undef CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS
  27. #endif
  28. NAMESPACE_BEGIN(CryptoPP)
  29. /// \brief SPECK block cipher information
  30. /// \tparam L block size of the cipher, in bytes
  31. /// \tparam D default key length, in bytes
  32. /// \tparam N minimum key length, in bytes
  33. /// \tparam M maximum key length, in bytes
  34. /// \since Crypto++ 6.0
  35. template <unsigned int L, unsigned int D, unsigned int N, unsigned int M>
  36. struct SPECK_Info : public FixedBlockSize<L>, VariableKeyLength<D, N, M>
  37. {
  38. /// \brief The algorithm name
  39. /// \return the algorithm name
  40. /// \details StaticAlgorithmName returns the algorithm's name as a static
  41. /// member function.
  42. static const std::string StaticAlgorithmName()
  43. {
  44. // Format is Cipher-Blocksize(Keylength)
  45. return "SPECK-" + IntToString(L*8);
  46. }
  47. };
  48. /// \brief SPECK block cipher base class
  49. /// \tparam W the word type
  50. /// \details User code should use SPECK64 or SPECK128
  51. /// \sa SPECK64, SPECK128, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a>
  52. /// \since Crypto++ 6.0
  53. template <class W>
  54. struct SPECK_Base
  55. {
  56. virtual ~SPECK_Base() {}
  57. SPECK_Base() : m_kwords(0), m_rounds(0) {}
  58. typedef SecBlock<W, AllocatorWithCleanup<W, true> > AlignedSecBlock;
  59. mutable AlignedSecBlock m_wspace; // workspace
  60. AlignedSecBlock m_rkeys; // round keys
  61. unsigned int m_kwords; // number of key words
  62. unsigned int m_rounds; // number of rounds
  63. };
  64. /// \brief SPECK 64-bit block cipher
  65. /// \details Speck is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
  66. /// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
  67. /// \details SPECK64 provides 64-bit block size. The valid key sizes are 96-bit and 128-bit.
  68. /// \sa SPECK64, SPECK128, <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SPECK
  69. /// Families of Lightweight Block Ciphers</A>, <A HREF="http://iadgov.github.io/simon-speck/">
  70. /// The Simon and Speck GitHub</A>, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a> on the
  71. /// Crypto++ wiki
  72. /// \since Crypto++ 6.0
  73. class CRYPTOPP_NO_VTABLE SPECK64 : public SPECK_Info<8, 12, 12, 16>, public BlockCipherDocumentation
  74. {
  75. public:
  76. /// \brief SPECK64 block cipher base implementation
  77. /// \details Provides implementation common to encryption and decryption
  78. /// \since Crypto++ 6.0
  79. class CRYPTOPP_NO_VTABLE Base : protected SPECK_Base<word32>, public BlockCipherImpl<SPECK_Info<8, 12, 12, 16> >
  80. {
  81. public:
  82. /// \brief The algorithm name
  83. /// \return the algorithm name
  84. /// \details AlgorithmName returns the algorithm's name as a
  85. /// member function.
  86. std::string AlgorithmName() const {
  87. return StaticAlgorithmName() + (m_kwords == 0 ? "" :
  88. "(" + IntToString(m_kwords*sizeof(word32)*8) + ")");
  89. }
  90. std::string AlgorithmProvider() const;
  91. /// \brief Provides input and output data alignment for optimal performance.
  92. /// \return the input data alignment that provides optimal performance
  93. /// \sa GetAlignment() and OptimalBlockSize()
  94. unsigned int OptimalDataAlignment() const;
  95. protected:
  96. void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
  97. };
  98. /// \brief SPECK64 encryption transformation
  99. /// \details Enc provides the encryption transformation.
  100. /// All key sizes are supported.
  101. /// \since Crypto++ 6.0
  102. class CRYPTOPP_NO_VTABLE Enc : public Base
  103. {
  104. public:
  105. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  106. };
  107. /// \brief SPECK64 decryption transformation
  108. /// \details Dec provides the decryption transformation.
  109. /// All key sizes are supported.
  110. /// \since Crypto++ 6.0
  111. class CRYPTOPP_NO_VTABLE Dec : public Base
  112. {
  113. public:
  114. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  115. };
  116. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  117. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  118. };
  119. /// \brief SPECK 128-bit block cipher
  120. /// \details Speck is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
  121. /// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
  122. /// \details SPECK128 provides 128-bit block size. The valid key sizes are 128-bit, 192-bit and 256-bit.
  123. /// \sa SPECK64, SPECK128, <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SPECK
  124. /// Families of Lightweight Block Ciphers</A>, <A HREF="http://iadgov.github.io/simon-speck/">
  125. /// The Simon and Speck GitHub</A>, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a> on the
  126. /// Crypto++ wiki
  127. /// \since Crypto++ 6.0
  128. class CRYPTOPP_NO_VTABLE SPECK128 : public SPECK_Info<16, 16, 16, 32>, public BlockCipherDocumentation
  129. {
  130. public:
  131. /// \brief SPECK128 block cipher base implementation
  132. /// \details Provides implementation common to encryption and decryption
  133. /// \since Crypto++ 6.0
  134. class CRYPTOPP_NO_VTABLE Base : protected SPECK_Base<word64>, public BlockCipherImpl<SPECK_Info<16, 16, 16, 32> >
  135. {
  136. public:
  137. /// \brief The algorithm name
  138. /// \return the algorithm name
  139. /// \details AlgorithmName returns the algorithm's name as a
  140. /// member function.
  141. std::string AlgorithmName() const {
  142. return StaticAlgorithmName() + (m_kwords == 0 ? "" :
  143. "(" + IntToString(m_kwords*sizeof(word64)*8) + ")");
  144. }
  145. std::string AlgorithmProvider() const;
  146. /// \brief Provides input and output data alignment for optimal performance.
  147. /// \return the input data alignment that provides optimal performance
  148. /// \sa GetAlignment() and OptimalBlockSize()
  149. unsigned int OptimalDataAlignment() const;
  150. protected:
  151. void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
  152. };
  153. /// \brief SPECK128 encryption transformation
  154. /// \details Enc provides the encryption transformation.
  155. /// All key sizes are supported.
  156. /// \since Crypto++ 6.0
  157. class CRYPTOPP_NO_VTABLE Enc : public Base
  158. {
  159. public:
  160. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  161. #if CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS
  162. size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
  163. #endif
  164. };
  165. /// \brief SPECK128 decryption transformation
  166. /// \details Dec provides the decryption transformation.
  167. /// All key sizes are supported.
  168. /// \since Crypto++ 6.0
  169. class CRYPTOPP_NO_VTABLE Dec : public Base
  170. {
  171. public:
  172. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  173. #if CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS
  174. size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
  175. #endif
  176. };
  177. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  178. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  179. };
  180. NAMESPACE_END
  181. #endif // CRYPTOPP_SPECK_H