Crypto++ 7.0 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 # define CRYPTOPP_SM4_ADVANCED_PROCESS_BLOCKS 1
23 #endif
24
25 NAMESPACE_BEGIN(CryptoPP)
26
27 /// \brief SM4 block cipher information
28 /// \since Crypto++ 6.0
29 struct SM4_Info : public FixedBlockSize<16>, FixedKeyLength<16>
30 {
31 static const std::string StaticAlgorithmName()
32 {
33 return "SM4";
34 }
35 };
36
37 /// \brief Classes for the SM4 block cipher
38 /// \details SM4 is a block cipher designed by Xiaoyun Wang, et al. The block cipher is part of the
39 /// Chinese State Cryptography Administration portfolio. The cipher was formely known as SMS4.
40 /// \sa <A HREF="http://eprint.iacr.org/2008/329.pdf">SMS4 Encryption Algorithm for Wireless Networks</A>
41 /// \since Crypto++ 6.0
42 class CRYPTOPP_NO_VTABLE SM4 : public SM4_Info, public BlockCipherDocumentation
43 {
44 public:
45 /// \brief SM4 block cipher transformation functions
46 /// \details Provides implementation common to encryption and decryption
47 /// \since Crypto++ 6.0
48 class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<SM4_Info>
49 {
50 protected:
51 void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
52
53 SecBlock<word32, AllocatorWithCleanup<word32> > m_rkeys;
54 mutable SecBlock<word32, AllocatorWithCleanup<word32> > m_wspace;
55 };
56
57 /// \brief Encryption transformation
58 /// \details Enc provides implementation for encryption transformation. All key
59 /// sizes are supported.
60 /// \details SM4 encryption is accelerated on machines with AES-NI. Decryption is
61 /// not acclerated because it is not profitable. Thanks to Markku-Juhani Olavi
62 /// Saarinen.
63 /// \since Crypto++ 6.0, AESNI encryption since Crypto++ 7.1
64 class CRYPTOPP_NO_VTABLE Enc : public Base
65 {
66 public:
67 std::string AlgorithmProvider() const;
68 protected:
69 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
70 #if CRYPTOPP_SM4_ADVANCED_PROCESS_BLOCKS
71 size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
72 #endif
73 };
74
75 /// \brief Encryption transformation
76 /// \details Dec provides implementation for decryption transformation. All key
77 /// sizes are supported.
78 /// \details SM4 encryption is accelerated on machines with AES-NI. Decryption is
79 /// not acclerated because it is not profitable. Thanks to Markku-Juhani Olavi
80 /// Saarinen.
81 /// \since Crypto++ 6.0
82 class CRYPTOPP_NO_VTABLE Dec : public Base
83 {
84 protected:
85 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
86 };
87
88 typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
89 typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
90 };
91
92 NAMESPACE_END
93
94 #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:682
config.h
Library configuration file.
SM4::Enc
Encryption transformation.
Definition: sm4.h:64
BlockCipher
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1248
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:42
SM4::Base
SM4 block cipher transformation functions.
Definition: sm4.h:48
SM4_Info
SM4 block cipher information.
Definition: sm4.h:29
BlockCipherImpl
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:305
CryptoPP
Crypto++ library namespace.
SM4::Dec
Encryption transformation.
Definition: sm4.h:82
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:293