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