Crypto++ 8.2 Free C++ class library of cryptographic schemes

sm4.h
Go to the documentation of this file.
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 formely known as SMS4.
7 /// \details SM4 encryption is accelerated on machines with AES-NI. Decryption is not acclerated 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 {
35 return "SM4";
36 }
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 formely 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 &params);
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 acclerated 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 acclerated 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
FixedKeyLength
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:124
BlockCipherDocumentation
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher...
Definition: seckey.h:398
SecBlock
Secure memory block with allocator and cleanup.
Definition: secblock.h:709
config.h
Library configuration file.
SM4::Enc
Encryption transformation.
Definition: sm4.h:66
BlockCipher
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1278
secblock.h
Classes and functions for secure memory allocations.
FixedBlockSize
Inherited by algorithms with fixed block size.
Definition: seckey.h:40
seckey.h
Classes and functions for implementing secret key algorithms.
BlockCipherFinal< ENCRYPTION, Enc >
SM4
Classes for the SM4 block cipher.
Definition: sm4.h:44
SM4::Base
SM4 block cipher transformation functions.
Definition: sm4.h:50
SM4_Info
SM4 block cipher information.
Definition: sm4.h:31
BlockCipherImpl
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:305
CryptoPP
Crypto++ library namespace.
SM4::Dec
Decryption transformation.
Definition: sm4.h:84
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:321