sm4.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // sm4.h - written and placed in the public domain by Jeffrey Walton and Han Lulu
  2. /// \file sm4.h
  3. /// \brief Classes for the SM4 block cipher
  4. /// \details SM4 is a block cipher designed by Xiaoyun Wang, et al. The block cipher is part of the
  5. /// Chinese State Cryptography Administration portfolio. The cipher was formerly known as SMS4.
  6. /// \details SM4 encryption is accelerated on machines with AES-NI. Decryption is not accelerated because
  7. /// it is not profitable. Thanks to Markku-Juhani Olavi Saarinen for help and the code.
  8. /// \sa <A HREF="http://eprint.iacr.org/2008/329.pdf">SMS4 Encryption Algorithm for Wireless Networks</A>,
  9. /// <A HREF="http://github.com/guanzhi/GmSSL">Reference implementation using OpenSSL</A> and
  10. /// <A HREF="https://github.com/mjosaarinen/sm4ni">Markku-Juhani Olavi Saarinen GitHub</A>.
  11. /// \since Crypto++ 6.0
  12. #ifndef CRYPTOPP_SM4_H
  13. #define CRYPTOPP_SM4_H
  14. #include "config.h"
  15. #include "seckey.h"
  16. #include "secblock.h"
  17. #if (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86)
  18. # ifndef CRYPTOPP_DISABLE_SM4_SIMD
  19. # define CRYPTOPP_SM4_ADVANCED_PROCESS_BLOCKS 1
  20. # endif
  21. #endif
  22. NAMESPACE_BEGIN(CryptoPP)
  23. /// \brief SM4 block cipher information
  24. /// \since Crypto++ 6.0
  25. struct SM4_Info : public FixedBlockSize<16>, FixedKeyLength<16>
  26. {
  27. static const std::string StaticAlgorithmName()
  28. {
  29. return "SM4";
  30. }
  31. };
  32. /// \brief Classes for the SM4 block cipher
  33. /// \details SM4 is a block cipher designed by Xiaoyun Wang, et al. The block cipher is part of the
  34. /// Chinese State Cryptography Administration portfolio. The cipher was formerly known as SMS4.
  35. /// \sa <A HREF="http://eprint.iacr.org/2008/329.pdf">SMS4 Encryption Algorithm for Wireless Networks</A>
  36. /// \since Crypto++ 6.0
  37. class CRYPTOPP_NO_VTABLE SM4 : public SM4_Info, public BlockCipherDocumentation
  38. {
  39. public:
  40. /// \brief SM4 block cipher transformation functions
  41. /// \details Provides implementation common to encryption and decryption
  42. /// \since Crypto++ 6.0
  43. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<SM4_Info>
  44. {
  45. protected:
  46. void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
  47. SecBlock<word32, AllocatorWithCleanup<word32> > m_rkeys;
  48. mutable SecBlock<word32, AllocatorWithCleanup<word32> > m_wspace;
  49. };
  50. /// \brief Encryption transformation
  51. /// \details Enc provides implementation for encryption transformation. All key
  52. /// sizes are supported.
  53. /// \details SM4 encryption is accelerated on machines with AES-NI. Decryption is
  54. /// not accelerated because it is not profitable. Thanks to Markku-Juhani Olavi
  55. /// Saarinen.
  56. /// \since Crypto++ 6.0, AESNI encryption since Crypto++ 8.0
  57. class CRYPTOPP_NO_VTABLE Enc : public Base
  58. {
  59. public:
  60. std::string AlgorithmProvider() const;
  61. protected:
  62. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  63. #if CRYPTOPP_SM4_ADVANCED_PROCESS_BLOCKS
  64. size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
  65. #endif
  66. };
  67. /// \brief Decryption transformation
  68. /// \details Dec provides implementation for decryption transformation. All key
  69. /// sizes are supported.
  70. /// \details SM4 encryption is accelerated on machines with AES-NI. Decryption is
  71. /// not accelerated because it is not profitable. Thanks to Markku-Juhani Olavi
  72. /// Saarinen.
  73. /// \since Crypto++ 6.0
  74. class CRYPTOPP_NO_VTABLE Dec : public Base
  75. {
  76. protected:
  77. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  78. };
  79. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  80. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  81. };
  82. NAMESPACE_END
  83. #endif // CRYPTOPP_SM4_H