Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:09

0001 // sm4.h - written and placed in the public domain by Jeffrey Walton and Han Lulu

0002 
0003 /// \file sm4.h

0004 /// \brief Classes for the SM4 block cipher

0005 /// \details SM4 is a block cipher designed by Xiaoyun Wang, et al. The block cipher is part of the

0006 ///   Chinese State Cryptography Administration portfolio. The cipher was formerly known as SMS4.

0007 /// \details SM4 encryption is accelerated on machines with AES-NI. Decryption is not accelerated because

0008 ///   it is not profitable. Thanks to Markku-Juhani Olavi Saarinen for help and the code.

0009 /// \sa <A HREF="http://eprint.iacr.org/2008/329.pdf">SMS4 Encryption Algorithm for Wireless Networks</A>,

0010 ///   <A HREF="http://github.com/guanzhi/GmSSL">Reference implementation using OpenSSL</A> and

0011 ///   <A HREF="https://github.com/mjosaarinen/sm4ni">Markku-Juhani Olavi Saarinen GitHub</A>.

0012 /// \since Crypto++ 6.0

0013 
0014 #ifndef CRYPTOPP_SM4_H
0015 #define CRYPTOPP_SM4_H
0016 
0017 #include "config.h"
0018 #include "seckey.h"
0019 #include "secblock.h"
0020 
0021 #if (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86)
0022 # ifndef CRYPTOPP_DISABLE_SM4_SIMD
0023 #  define CRYPTOPP_SM4_ADVANCED_PROCESS_BLOCKS 1
0024 # endif
0025 #endif
0026 
0027 NAMESPACE_BEGIN(CryptoPP)
0028 
0029 /// \brief SM4 block cipher information

0030 /// \since Crypto++ 6.0

0031 struct SM4_Info : public FixedBlockSize<16>, FixedKeyLength<16>
0032 {
0033     static const std::string StaticAlgorithmName()
0034     {
0035         return "SM4";
0036     }
0037 };
0038 
0039 /// \brief Classes for the SM4 block cipher

0040 /// \details SM4 is a block cipher designed by Xiaoyun Wang, et al. The block cipher is part of the

0041 ///   Chinese State Cryptography Administration portfolio. The cipher was formerly known as SMS4.

0042 /// \sa <A HREF="http://eprint.iacr.org/2008/329.pdf">SMS4 Encryption Algorithm for Wireless Networks</A>

0043 /// \since Crypto++ 6.0

0044 class CRYPTOPP_NO_VTABLE SM4 : public SM4_Info, public BlockCipherDocumentation
0045 {
0046 public:
0047     /// \brief SM4 block cipher transformation functions

0048     /// \details Provides implementation common to encryption and decryption

0049     /// \since Crypto++ 6.0

0050     class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<SM4_Info>
0051     {
0052     protected:
0053         void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
0054 
0055         SecBlock<word32, AllocatorWithCleanup<word32> > m_rkeys;
0056         mutable SecBlock<word32, AllocatorWithCleanup<word32> > m_wspace;
0057     };
0058 
0059     /// \brief Encryption transformation

0060     /// \details Enc provides implementation for encryption transformation. All key

0061     ///   sizes are supported.

0062     /// \details SM4 encryption is accelerated on machines with AES-NI. Decryption is

0063     ///   not accelerated because it is not profitable. Thanks to Markku-Juhani Olavi

0064     ///   Saarinen.

0065     /// \since Crypto++ 6.0, AESNI encryption since Crypto++ 8.0

0066     class CRYPTOPP_NO_VTABLE Enc : public Base
0067     {
0068     public:
0069         std::string AlgorithmProvider() const;
0070     protected:
0071         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0072 #if CRYPTOPP_SM4_ADVANCED_PROCESS_BLOCKS
0073         size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
0074 #endif
0075     };
0076 
0077     /// \brief Decryption transformation

0078     /// \details Dec provides implementation for decryption transformation. All key

0079     ///   sizes are supported.

0080     /// \details SM4 encryption is accelerated on machines with AES-NI. Decryption is

0081     ///   not accelerated because it is not profitable. Thanks to Markku-Juhani Olavi

0082     ///   Saarinen.

0083     /// \since Crypto++ 6.0

0084     class CRYPTOPP_NO_VTABLE Dec : public Base
0085     {
0086     protected:
0087         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0088     };
0089 
0090     typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
0091     typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
0092 };
0093 
0094 NAMESPACE_END
0095 
0096 #endif // CRYPTOPP_SM4_H