lea.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // lea.h - written and placed in the public domain by Kim Sung Hee and Jeffrey Walton
  2. // Based on "LEA: A 128-Bit Block Cipher for Fast Encryption on Common
  3. // Processors" by Deukjo Hong, Jung-Keun Lee, Dong-Chan Kim, Daesung Kwon,
  4. // Kwon Ho Ryu, and Dong-Geon Lee.
  5. /// \file lea.h
  6. /// \brief Classes for the LEA block cipher
  7. /// \since Crypto++ 8.0
  8. #ifndef CRYPTOPP_LEA_H
  9. #define CRYPTOPP_LEA_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 || CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8)
  15. # ifndef CRYPTOPP_DISABLE_LEA_SIMD
  16. # define CRYPTOPP_LEA_ADVANCED_PROCESS_BLOCKS 1
  17. # endif
  18. #endif
  19. // Yet another SunStudio/SunCC workaround. Failed self tests
  20. // in SSE code paths on i386 for SunStudio 12.3 and below.
  21. #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120)
  22. # undef CRYPTOPP_LEA_ADVANCED_PROCESS_BLOCKS
  23. #endif
  24. NAMESPACE_BEGIN(CryptoPP)
  25. /// \brief LEA block cipher information
  26. /// \since Crypto++ 8.0
  27. struct LEA_Info : public FixedBlockSize<16>, public VariableKeyLength<16,16,32,8>
  28. {
  29. /// \brief The algorithm name
  30. /// \return the algorithm name
  31. /// \details StaticAlgorithmName returns the algorithm's name as a static
  32. /// member function.
  33. static const std::string StaticAlgorithmName()
  34. {
  35. // Format is Cipher-Blocksize
  36. return "LEA-128";
  37. }
  38. };
  39. /// \brief LEA 128-bit block cipher
  40. /// \details LEA provides 128-bit block size. The valid key size is 128-bits, 192-bits and 256-bits.
  41. /// \note Crypto++ provides a byte oriented implementation
  42. /// \sa <a href="http://www.cryptopp.com/wiki/LEA">LEA</a>,
  43. /// <a href="https://seed.kisa.or.kr/html/egovframework/iwt/ds/ko/ref/LEA%20A%20128-Bit%20Block%20Cipher%20for%20Fast%20Encryption%20on%20Common%20Processors-English.pdf">
  44. /// LEA: A 128-Bit Block Cipher for Fast Encryption on Common Processors</a>
  45. /// \since Crypto++ 8.0
  46. class CRYPTOPP_NO_VTABLE LEA : public LEA_Info, public BlockCipherDocumentation
  47. {
  48. public:
  49. /// \brief LEA block cipher transformation functions
  50. /// \details Provides implementation common to encryption and decryption
  51. /// \since Crypto++ 8.0
  52. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<LEA_Info>
  53. {
  54. protected:
  55. void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
  56. std::string AlgorithmProvider() const;
  57. SecBlock<word32> m_rkey;
  58. mutable SecBlock<word32> m_temp;
  59. unsigned int m_rounds;
  60. };
  61. /// \brief Encryption transformation
  62. /// \details Enc provides implementation for encryption transformation. All key and block
  63. /// sizes are supported.
  64. /// \since Crypto++ 8.0
  65. class CRYPTOPP_NO_VTABLE Enc : public Base
  66. {
  67. public:
  68. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  69. #if CRYPTOPP_LEA_ADVANCED_PROCESS_BLOCKS
  70. size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
  71. #endif
  72. };
  73. /// \brief Decryption transformation
  74. /// \details Dec provides implementation for decryption transformation. All key and block
  75. /// sizes are supported.
  76. /// \since Crypto++ 8.0
  77. class CRYPTOPP_NO_VTABLE Dec : public Base
  78. {
  79. public:
  80. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  81. #if CRYPTOPP_LEA_ADVANCED_PROCESS_BLOCKS
  82. size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
  83. #endif
  84. };
  85. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  86. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  87. };
  88. typedef LEA::Encryption LEAEncryption;
  89. typedef LEA::Decryption LEADecryption;
  90. NAMESPACE_END
  91. #endif // CRYPTOPP_LEA_H