Improved SM4 encryption method and system with side channel attack resistance attribute
By introducing variable length random number matrix and S-box intermediate matrix in the SM4 encryption process, the round key is generated, and the security problem of the SM4 encryption algorithm is solved, and the anti-side channel attack capability and data security are improved.
Patent Information
- Application Number
- CN202510368247.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-26
- Publication Date
- 2025-08-15
AI Technical Summary
The existing SM4 encryption algorithm is low in security and is susceptible to time-side channel and power-side channel attacks.
During the encryption process, variable-length random numbers are generated to form a random number matrix, and a round key is generated together with the intermediate matrix in the S box. The round key addition operation is performed on the SM4 standard encrypted data to improve the ability to resist side channel attacks.
The ciphertext results change after each encryption are achieved, which improves the security of data ciphertext and resists attacks on time-side channels and power-side channels, while the encryption performance has not been reduced.
Smart Images

Figure CN120498644A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to an improved SM4 encryption and decryption method and system, belonging to the technical field of data security. Background Art
[0002] The SM4 encryption algorithm is a symmetric encryption algorithm with a 16-byte key length. Data is encrypted in 16-byte blocks, primarily used to encrypt large amounts of data. SM4 utilizes a 4-branch unbalanced Feistel structure, an SP round function, and both a 128-bit block and key length. Both the encryption and key expansion algorithms utilize a 32-round nonlinear iterative structure. The decryption and encryption processes are similar, except that the round keys are used in the opposite order. The S-box has a fixed 8-bit input and 8-bit output. SM4's main operations include XOR, synthetic permutation, nonlinear permutation, reverse transformation, circular shift, and S-box transformation. Its security strength is comparable to that of AES, and the SM4 algorithm has been widely used in China.
[0003] Block cipher technology represented by SM4 has many advantages such as fast encryption and decryption speed, simple implementation, high efficiency and stability. It plays an important role in the field of data security, but also has some hidden dangers and challenges.
[0004] On the one hand, SM4 encryption is a symmetric encryption algorithm. For fixed data and keys, the output of each encryption is the same. As processor performance improves, there are certain security risks. On the other hand, in the standard SM4 encryption algorithm, the calculation time and power of each step are basically fixed. For data and keys of fixed length, the total encryption time and power consumption of each encryption are very close, which makes it vulnerable to side channel attacks such as time side channel attacks and power consumption side channel attacks.
[0005] In view of the above problems, this application proposes a solution. Summary of the Invention
[0006] The purpose of this application is to provide an improved SM4 encryption method to solve the problem that the existing SM4 encryption algorithm has low security and is susceptible to time side channel and power consumption side channel attacks.
[0007] The technical problem that this application needs to solve is: how to provide an SM4 encryption method that has anti-side channel attack properties and can improve data security.
[0008] The purpose of this application can be achieved through the following technical solutions:
[0009] Based on the standard SM4 encryption method, variable-length random numbers are generated during the encryption process to form a random number matrix. The data in the S-box is used to form an intermediate matrix, which is used together with the random number matrix to generate round keys. Finally, the round key addition operation is performed on the SM4 standard encrypted data.
[0010] The specific technical solution of the improved SM4 encryption method with side-channel attack resistance includes:
[0011] 1. Initialization operation, performed once before SM4 encryption, includes the following 4 operations:
[0012] (1) Set the random number length n, the value of n is not greater than 16;
[0013] (2) Set the maximum length of the generated random number to k×n, where k is an integer and is set to a larger value;
[0014] (3) Optionally, set the strategy for selecting the 4th-order intermediate matrix from the S-box; if not set, the first 4 rows and 4 columns in the S-box are selected by default to form the 4th-order intermediate matrix;
[0015] (4) Optionally, set a combination strategy for the SM4 ciphertext and the random number output with the ciphertext. The random number output with the ciphertext can be set to be placed after the SM4 reinforced ciphertext, or it can be set to be inserted into the SM4 reinforced ciphertext. If not set, the default strategy is 16 bytes of SM4 reinforced ciphertext + n bytes of random number output with the ciphertext.
[0016] 2. Generate a random number of variable length. According to the random number length n set during initialization, select a random value m (n<m<k×n, k>1) to generate a random number with an actual length of m.
[0017] 3. Form a 4th-order initial random number matrix. If the random number length m is not less than 16, directly select the first 16 bytes of random numbers to form a 4th-order initial random number matrix R init If the random number length m is less than 16, the m random numbers are filled from front to back to form 16 bytes, and a 4-order initial random number matrix R is formed. init .
[0018] 4. Generate the final random number matrix of order 4, including 2 steps:
[0019] (1) According to the selection strategy during initialization, 4 rows and 4 columns of data are selected from the 16-order S-box matrix to form a 4-order intermediate matrix S mid ;
[0020] (2) The 4th order initial random number matrix R init and the 4th-order intermediate matrix S midXOR by element position to get the final random number matrix of order 4 R final As a reinforcement wheel key.
[0021] 5. From the 4th order final random number matrix R final In the sequence from top to bottom and from left to right, n elements are selected to form the random number output along with the ciphertext.
[0022] 6. The 4th-order ciphertext matrix C that completes the last round of standard SM4 encryption operation std And the 4th order final random number matrix R final XOR by element position to get SM4 reinforced ciphertext
[0023] 7. Based on the initialization strategy, the SM4 reinforced ciphertext and the random number output with the ciphertext are combined to finally obtain the SM4 output ciphertext; wherein the combination method can be the reinforced ciphertext concatenated with the random number output with the ciphertext C1C2C3C4C5C6C7C8C9C 10 C 11 C 12 C 13 C 14 C 15 C 16 R1…R n , you can also convert n-byte random number elements R1…R n Insert into the reinforced ciphertext C1C2C3C4C5C6C7C8C9C respectively 10 C 11 C 12 C 13 C 14 C 15 C 16 The specified position in .
[0024] 8. Perform a restore operation on the random number output along with the ciphertext for n bytes to obtain the final random number matrix of order 4, which includes the following three steps:
[0025] (1) Construct a 4th-order matrix R whose elements are initially all 0 init , output random numbers with the ciphertext in bytes, and fill them into R from top to bottom and from left to right. init In the example, we get the 4th order temporary random number matrix R tmp ;
[0026] (2) The 4th order temporary random number matrix R tmp and the 4th-order intermediate matrix S mid XOR by element position to get a 4th-order intermediate random number matrix
[0027] (3) From top to bottom and from left to right, from the 4th order intermediate random number matrix R mid Extract the first n elements and add these n elements to the remaining 16-n positions in a cyclic insertion manner. After the cyclic insertion is completed, the final random number matrix R of order 4 is obtained. final .
[0028] Restoring the SM4 hardened ciphertext to obtain the SM4 standard ciphertext involves the following two steps:
[0029] (1) Insert the 16-byte SM4-enhanced ciphertext into a 4-order matrix from top to bottom and from left to right to form a 4-order SM4-enhanced ciphertext matrix C. safe ;
[0030] (2) The 4th-order SM4 reinforced ciphertext matrix C safe And the 4th order final random number matrix R final XOR by element position to get the 4th order SM4 standard ciphertext
[0031] The advantages of this application include the following two aspects:
[0032] 1. Introduce the random number matrix R init and the S-box intermediate matrix S mid , R init and S mid Get the reinforcement round key R by XORing the element position final , using R final By performing rounds of key addition operations on standard SM4 ciphertext data, it is possible to use the same encryption key to perform SM4 encryption on the same data. The ciphertext result will change each time it is encrypted, which has a similar effect to RSA public key encryption, improving the security of the data ciphertext itself. At the same time, the encryption performance is not significantly reduced, and it is much faster than SM2 public key encryption.
[0033] 2. When generating random numbers, a variable-length random number mechanism is introduced to make the time and power consumed by each SM4 encryption have a certain degree of variability, improving the ability to resist time side-channel attacks and power consumption side-channel attacks. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are merely embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without any creative work.
[0035] Figure 1Schematic diagram of the improved SM4 encryption algorithm implemented in this application;
[0036] Figure 2 Schematic diagram of the decryption process of the improved SM4 encryption algorithm implemented in this application. DETAILED DESCRIPTION
[0037] The technical solutions of the present invention will be clearly and completely described below with reference to the embodiments. Obviously, the embodiments described are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making any creative efforts shall fall within the scope of protection of the present invention.
[0038] like Figure 1 As shown, the specific process of the improved SM4 encryption includes the following steps:
[0039] Step P1: Perform an initialization operation before using SM4 encryption, including setting the random number length n, where n is not greater than 16; setting the maximum random number length to be generated to k×n, where k is an integer; setting the strategy for selecting a 4th-order intermediate matrix from the S-box. If not set, the first 4 rows and 4 columns in the S-box are selected by default to form a 4th-order intermediate matrix; setting the combination strategy of the SM4 ciphertext and the random number output with the ciphertext. If not set, the default strategy is 16 bytes of SM4 reinforced ciphertext + n bytes of random number output with the ciphertext;
[0040] Step P2: Execute the encryption operation in the standard SM4 algorithm to obtain the 4th-order standard SM4 ciphertext matrix C std , the output result of step P2 is used as input for step P3;
[0041] Step P3, generate a random number of variable length, select a random value m (n <m<k×n,k> 1) Generate a random number of actual length m and use it as input for step P4;
[0042] Step P4, forming a 4th order initial random number matrix. If the random number length m is not less than 16, directly select the first 16 bytes of random numbers to form a 4th order initial random number matrix R init If the random number length m is less than 16, the m random numbers are filled from front to back to form 16 bytes, and a 4-order initial random number matrix R is formed. init and use it as input for step P5;
[0043] Step P5: First, according to the selection strategy during initialization, select 4 rows and 4 columns of data from the 16-order S-box matrix to form a 4-order intermediate matrix S mid and use it as input for step P6;
[0044] Step P6, generate the 4th order final random number matrix. First, according to the selection strategy during initialization, select 4 rows and 4 columns of data from the 16th order S-box matrix to form the 4th order intermediate matrix S mid ; Then the 4th order initial random number matrix R init and the 4th-order intermediate matrix S mid XOR by element position to get the final random number matrix of order 4 R final As a reinforcement round key, it is used as input for steps P7 and P8;
[0045] Step P7, from the 4th order final random number matrix R final In the sequence from top to bottom and from left to right, select n elements to form a random number output along with the ciphertext, and use it as input for step P9;
[0046] Step P8: The 4th-order ciphertext matrix C generated in step P2 is std and the 4th-order final random number matrix R generated in step P6 final XOR by element position to get SM4 reinforced ciphertext and use it as input for step P9;
[0047] In step P9, based on the strategy set by initialization, the random number output along with the ciphertext generated in step P7 and the SM4 reinforced ciphertext generated in step P8 are combined to obtain the final SM4 ciphertext for output.
[0048] At this point, all steps of the improved SM4 encryption are completed; before encryption, the plaintext is grouped into 16-byte units, and then encrypted in units of groups; after encryption, the output ciphertext length of each group is 16+n, where n is the length of the random number output with the ciphertext.
[0049] like Figure 2 As shown, the specific process of the improved SM4 decryption includes the following steps:
[0050] Step S1: Decompose the ciphertext into groups, each group has a size of 16+n, and parse the groups according to the rules set in the initialization;
[0051] Step S2: Obtain 16-byte SM4 reinforced ciphertext and n-byte random number output along with the ciphertext through parsing;
[0052] Step S3: construct a 4th-order matrix R whose elements are initially all 0 init , output random numbers with the ciphertext in bytes, and fill them into R from top to bottom and from left to right. init In the example, we get the 4th order temporary random number matrix R tmp ;
[0053] Step S4, according to the selection strategy during initialization, select 4 rows and 4 columns of data from the 16-order S-box matrix to form a 4-order intermediate matrix S mid ;
[0054] Step S5: The 4th order temporary random number matrix R tmp and the 4th-order intermediate matrix S mid XOR by element position to get a 4th-order intermediate random number matrix
[0055] Step S6, in order from top to bottom and from left to right, from the 4th order intermediate random number matrix R mid Extract the first n elements and add these n elements to the remaining 16-n positions in a cyclic insertion manner. After the cyclic insertion is completed, the final random number matrix R of order 4 is obtained. final ;
[0056] Step S7: convert the 16-byte SM4 reinforced ciphertext into a 4th-order ciphertext matrix and compare it with the 4th-order final random number matrix R final Perform XOR on the element positions to obtain a 4th-order standard SM4 ciphertext matrix; the output result of step S7 is used as input for step S8;
[0057] In step S8, the output result of step S7 is used as the input ciphertext, and the standard SM4 decryption operation process is executed. After the execution is completed, the plaintext is obtained.
[0058] At this point, all steps of the improved SM4 decryption are completed; the improved SM4 decrypts in groups. Before decryption, the ciphertext length of each group is 16+n, where n is the length of the random number output with the ciphertext; after decryption, the plaintext is obtained in groups of 16 bytes.
[0059] The embodiments disclosed above are merely intended to illustrate the present application. These embodiments are selected and described in detail in this specification to better explain the principles and practical applications of the present invention, thereby enabling those skilled in the art to better understand and utilize the present invention. The present invention is limited only by the claims and their full scope and equivalents.
Claims
1. An improved SM4 encryption and decryption method with side-channel attack resistance. include: Initialization operation, generating random numbers of variable length to form an initial random number matrix; Generate the final random number matrix and determine the random number output along with the ciphertext; Generate SM4 reinforced ciphertext to form SM4 output ciphertext; Perform a restore operation on the random numbers output along with the ciphertext to obtain the final random number matrix; Perform a restore operation on the SM4 reinforced ciphertext to obtain the SM4 standard ciphertext.
2. The improved SM4 encryption method with side-channel attack resistance according to claim 1, characterized in that: Initialization operation generates random numbers of variable length to form an initial random number matrix, including: Set the random number length n, the value of n is not greater than 16; Set the maximum random number length to be k×n, where k is an integer greater than 1; Set the strategy for selecting a 4-order intermediate matrix from the S-box. By default, the first 4 rows and 4 columns in the S-box are selected to form the intermediate matrix. Set the strategy for combining the SM4 ciphertext and the random number output with the ciphertext. You can place the random number output with the ciphertext after the SM4 reinforced ciphertext or insert it into the SM4 reinforced ciphertext. The default strategy is 16 bytes of SM4 reinforced ciphertext + n bytes of random number output with the ciphertext. According to the random number length n set during initialization, a random number with an actual length of m (n<m<k×n, k>1) is generated; If the random number length m is not less than 16, directly select the first 16 bytes of random numbers to form a 4-order initial random number matrix R init If the random number length m is less than 16, the m random numbers are filled from front to back to form 16 bytes, and a 4-order initial random number matrix R is formed. init .
3. The improved SM4 encryption method with side-channel attack resistance according to claim 2, characterized in that: Generate the final random number matrix and determine the random number output along with the ciphertext, including: According to the selection strategy during initialization, 4 rows and 4 columns of data are selected from the 16-order S-box matrix to form a 4-order intermediate matrix S mid ; The initial random number matrix R init and the intermediate matrix S mid XOR by element position to get the final random number matrix From the final random number matrix R final In the sequence from top to bottom and from left to right, n elements are selected to form the random number output along with the ciphertext.
4. The improved SM4 encryption method with side-channel attack resistance according to claim 3, characterized in that: Generate SM4 reinforced ciphertext and form SM4 output ciphertext, including: The 4th-order ciphertext matrix C that will complete the last round of SM4 encryption operation std And the final random number matrix R final XOR by element position to get SM4 reinforced ciphertext Based on the initialization policy, the SM4 reinforced ciphertext and the random number output with the ciphertext are combined to obtain the SM4 output ciphertext.
5. The improved SM4 encryption method with side-channel attack resistance according to claim 4, characterized in that: Perform a restore operation on the random numbers output with the ciphertext to obtain the final random number matrix, including: First construct a 4th-order matrix R whose elements are initially all 0 init Then fill the n random numbers output with the ciphertext into R from top to bottom and from left to right. init In the example above, we get the temporary random number matrix R. tmp ; The temporary random number matrix R tmp With the intermediate matrix S mid XOR by element position to get the intermediate random number matrix In order from top to bottom and from left to right, from the middle random number matrix R mid Extract the first n elements and add these n elements to the remaining 16-n positions in a circular insertion manner. After the circular insertion is completed, the final random number matrix R is obtained. final .
6. The improved SM4 encryption method with side-channel attack resistance according to claim 5, characterized in that: Perform a restore operation on the SM4 hardened ciphertext to obtain the SM4 standard ciphertext, including: Insert the SM4 reinforced ciphertext into the 4-order matrix from top to bottom and from left to right to form the SM4 reinforced ciphertext matrix C safe ; The SM4 reinforced ciphertext matrix C safe And the final random number matrix R final XOR by element position to get the SM4 standard ciphertext C std =C safe ⊕R final .
7. An improved SM4 encryption and decryption system with side-channel attack resistance, characterized in that: The invention comprises a processor and a memory storing computer program instructions, wherein when the computer program instructions are executed by the processor, the improved SM4 encryption and decryption method with side-channel attack resistance as claimed in any one of claims 1 to 6 is executed.