cham.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // cham.h - written and placed in the public domain by Kim Sung Hee and Jeffrey Walton
  2. // Based on "CHAM: A Family of Lightweight Block Ciphers for
  3. // Resource-Constrained Devices" by Bonwook Koo, Dongyoung Roh,
  4. // Hyeonjin Kim, Younghoon Jung, Dong-Geon Lee, and Daesung Kwon
  5. /// \file cham.h
  6. /// \brief Classes for the CHAM block cipher
  7. /// \since Crypto++ 8.0
  8. #ifndef CRYPTOPP_CHAM_H
  9. #define CRYPTOPP_CHAM_H
  10. #include "config.h"
  11. #include "seckey.h"
  12. #include "secblock.h"
  13. #include "algparam.h"
  14. #if (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86)
  15. # define CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS 1
  16. #endif
  17. // Yet another SunStudio/SunCC workaround. Failed self tests
  18. // in SSE code paths on i386 for SunStudio 12.3 and below.
  19. #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120)
  20. # undef CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
  21. #endif
  22. NAMESPACE_BEGIN(CryptoPP)
  23. /// \brief CHAM block cipher information
  24. /// \since Crypto++ 8.0
  25. struct CHAM64_Info : public FixedBlockSize<8>, public FixedKeyLength<16>
  26. {
  27. /// \brief The algorithm name
  28. /// \return the algorithm name
  29. /// \details StaticAlgorithmName returns the algorithm's name as a static
  30. /// member function.
  31. static const std::string StaticAlgorithmName()
  32. {
  33. // Format is Cipher-Blocksize
  34. return "CHAM-64";
  35. }
  36. };
  37. /// \brief CHAM block cipher information
  38. /// \since Crypto++ 8.0
  39. struct CHAM128_Info : public FixedBlockSize<16>, public VariableKeyLength<16,16,32,16>
  40. {
  41. /// \brief The algorithm name
  42. /// \return the algorithm name
  43. /// \details StaticAlgorithmName returns the algorithm's name as a static
  44. /// member function.
  45. static const std::string StaticAlgorithmName()
  46. {
  47. // Format is Cipher-Blocksize
  48. return "CHAM-128";
  49. }
  50. };
  51. /// \brief CHAM 64-bit block cipher
  52. /// \details CHAM64 provides 64-bit block size. The valid key size is 128-bit.
  53. /// \note Crypto++ provides a byte oriented implementation
  54. /// \sa CHAM128, <a href="http://www.cryptopp.com/wiki/CHAM">CHAM</a>,
  55. /// <a href="https://pdfs.semanticscholar.org/2f57/61b5c2614cffd58a09cc83c375a2b32a2ed3.pdf">
  56. /// CHAM: A Family of Lightweight Block Ciphers for Resource-Constrained Devices</a>
  57. /// \since Crypto++ 8.0
  58. class CRYPTOPP_NO_VTABLE CHAM64 : public CHAM64_Info, public BlockCipherDocumentation
  59. {
  60. public:
  61. /// \brief CHAM block cipher transformation functions
  62. /// \details Provides implementation common to encryption and decryption
  63. /// \since Crypto++ 8.0
  64. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<CHAM64_Info>
  65. {
  66. protected:
  67. void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
  68. SecBlock<word16> m_rk;
  69. mutable FixedSizeSecBlock<word16, 4> m_x;
  70. unsigned int m_kw;
  71. };
  72. /// \brief Encryption transformation
  73. /// \details Enc provides implementation for encryption transformation. All key and block
  74. /// sizes are supported.
  75. /// \since Crypto++ 8.0
  76. class CRYPTOPP_NO_VTABLE Enc : public Base
  77. {
  78. public:
  79. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  80. };
  81. /// \brief Decryption transformation
  82. /// \details Dec provides implementation for decryption transformation. All key and block
  83. /// sizes are supported.
  84. /// \since Crypto++ 8.0
  85. class CRYPTOPP_NO_VTABLE Dec : public Base
  86. {
  87. public:
  88. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  89. };
  90. /// \brief CHAM64 encryption
  91. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  92. /// \brief CHAM64 decryption
  93. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  94. };
  95. /// \brief CHAM64 encryption
  96. typedef CHAM64::Encryption CHAM64Encryption;
  97. /// \brief CHAM64 decryption
  98. typedef CHAM64::Decryption CHAM64Decryption;
  99. /// \brief CHAM 128-bit block cipher
  100. /// \details CHAM128 provides 128-bit block size. The valid key size is 128-bit and 256-bit.
  101. /// \note Crypto++ provides a byte oriented implementation
  102. /// \sa CHAM64, <a href="http://www.cryptopp.com/wiki/CHAM">CHAM</a>,
  103. /// <a href="https://pdfs.semanticscholar.org/2f57/61b5c2614cffd58a09cc83c375a2b32a2ed3.pdf">
  104. /// CHAM: A Family of Lightweight Block Ciphers for Resource-Constrained Devices</a>
  105. /// \since Crypto++ 8.0
  106. class CRYPTOPP_NO_VTABLE CHAM128 : public CHAM128_Info, public BlockCipherDocumentation
  107. {
  108. public:
  109. /// \brief CHAM block cipher transformation functions
  110. /// \details Provides implementation common to encryption and decryption
  111. /// \since Crypto++ 8.0
  112. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<CHAM128_Info>
  113. {
  114. protected:
  115. void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
  116. std::string AlgorithmProvider() const;
  117. SecBlock<word32> m_rk;
  118. mutable FixedSizeSecBlock<word32, 4> m_x;
  119. unsigned int m_kw;
  120. };
  121. /// \brief Encryption transformation
  122. /// \details Enc provides implementation for encryption transformation. All key and block
  123. /// sizes are supported.
  124. /// \since Crypto++ 8.0
  125. class CRYPTOPP_NO_VTABLE Enc : public Base
  126. {
  127. public:
  128. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  129. #if CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
  130. size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
  131. #endif
  132. };
  133. /// \brief Decryption transformation
  134. /// \details Dec provides implementation for decryption transformation. All key and block
  135. /// sizes are supported.
  136. /// \since Crypto++ 8.0
  137. class CRYPTOPP_NO_VTABLE Dec : public Base
  138. {
  139. public:
  140. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  141. #if CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
  142. size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
  143. #endif
  144. };
  145. /// \brief CHAM128 encryption
  146. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  147. /// \brief CHAM128 decryption
  148. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  149. };
  150. /// \brief CHAM128 encryption
  151. typedef CHAM128::Encryption CHAM128Encryption;
  152. /// \brief CHAM128 decryption
  153. typedef CHAM128::Decryption CHAM128Decryption;
  154. NAMESPACE_END
  155. #endif // CRYPTOPP_CHAM_H