Data processing method, device, equipment and medium based on SIMD instructions
Through the data processing method based on SIMD instructions, the Sbox and T transformation of the SM4 algorithm are improved, and the problem of slow computing speed of the SM4 algorithm is solved, data processing performance is improved, dependence on dedicated encryption cards is reduced, and costs are reduced.
Patent Information
- Application Number
- CN202011470449.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-16
- Publication Date
- 2024-11-22
- Estimated Expiration
- 2040-12-16
AI Technical Summary
The existing SM4 algorithms are slow to operate when applied, resulting in poor data processing performance. They need to build PCI-E dedicated encryption card in the device to accelerate, increasing costs.
The data processing method based on SIMD instructions is adopted to block the data and keys and encrypt or decrypt the operations using SIMD instructions. By improving the Sbox and T transformation processes, cyclic shift operations are avoided and computing efficiency is improved.
It significantly improves the speed of encryption and decryption operations, improves data processing performance, reduces dependence on PCI-E encryption cards, and reduces costs.
Smart Images

Figure CN112543096B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a data processing method, device, equipment and medium based on SIMD instructions. Background Art
[0002] Data encryption is the basic guarantee of data security. Even if the data is stolen, the data security can be guaranteed without leaking the key. Symmetric encryption algorithm is one of the most efficient encryption methods. Commonly used symmetric encryption algorithms include AES (Advanced Encryption Standard) algorithm and SM4 algorithm. SM4 algorithm is a block cipher algorithm with a block length of 128 bits (i.e. 128 bits) and a key length of 128 bits, i.e. plaintext, key, and ciphertext are all 128 bits. The encryption and decryption keys are the same, and encryption and decryption are achieved through 32 cycles of nonlinear iterative round functions. Among them, it includes nonlinear transformation Sbox and linear transformation composed of cyclic shift and XOR. The encryption and decryption processes of SM4 algorithm have the same structure, except that the round keys are used in reverse order, where the decryption round key is the reverse order of the encryption round key. The specific process is: divide the 128-bit key into 4 groups of 32 bits each, and generate 32 groups of 32-bit round keys according to the key expansion algorithm; and divide the input 128-bit plaintext (that is, the data to be encrypted) or ciphertext (that is, the data to be decrypted) into 4 groups of 32 bits each, and then perform a circular operation to obtain the corresponding ciphertext or plaintext.
[0003] However, in the prior art, the SM4 algorithm has a slow computing speed when applied, resulting in poor data processing performance in many scenarios where the SM4 algorithm is required. This results in the need to build a dedicated PCI-E encryption card into the device for acceleration, and the introduction of the encryption card greatly increases the cost. Summary of the invention
[0004] The embodiments of the present invention provide a data processing method, device, equipment and medium based on SIMD instructions to solve the defects of low operation speed and poor performance in the prior art.
[0005] In a first aspect, an embodiment of the present invention provides a data processing method based on SIMD instructions, comprising:
[0006] Based on SIMD instructions, 2 N The data to be processed are grouped into a first number of first data blocks, and the keys of a second number of rounds are grouped into a second number of second data blocks; the data to be processed include data to be encrypted or data to be decrypted;
[0007] An encryption operation is performed based on the first number of first data blocks and the second number of second data blocks to obtain a ciphertext data block; or a decryption operation is performed based on the first number of first data blocks and the second number of second data blocks to obtain a plaintext data block.
[0008] In a second aspect, an embodiment of the present invention provides a data processing device based on SIMD instructions, comprising:
[0009] The first processing module is used to convert 2 N The data to be processed are grouped into a first number of first data blocks, and the keys of a second number of rounds are grouped into a second number of second data blocks; the data to be processed include data to be encrypted or data to be decrypted;
[0010] The second processing module is used to perform encryption operation according to the first number of first data blocks and the second number of second data blocks to obtain ciphertext data blocks; or to perform decryption operation according to the first number of first data blocks and the second number of second data blocks to obtain plaintext data blocks.
[0011] In a third aspect, an embodiment of the present invention provides an electronic device, including: a memory, a transceiver, and at least one processor;
[0012] The processor, the memory and the transceiver are interconnected via a circuit;
[0013] The memory stores computer-executable instructions; the transceiver is used to receive data to be processed;
[0014] The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor performs the method described in the first aspect and various possible designs of the first aspect.
[0015] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer execution instructions. When a processor executes the computer execution instructions, the method described in the first aspect and various possible designs of the first aspect are implemented.
[0016] The data processing method, device, equipment and medium based on SIMD instructions provided in the embodiments of the present invention implement 2 N The encryption operation or decryption operation of the group of data to be processed effectively improves the speed of encryption and decryption operations, thereby improving data processing performance. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0018] Figure 1 A flow chart of a data processing method based on SIMD instructions provided by an embodiment of the present invention;
[0019] Figure 2 An exemplary flow chart of a data processing method based on SIMD instructions provided by an embodiment of the present invention;
[0020] Figure 3 Another exemplary flow chart of a data processing method based on SIMD instructions provided by an embodiment of the present invention;
[0021] Figure 4 An exemplary schematic diagram of a synthetic substitution process provided by one embodiment of the present invention;
[0022] Figure 5 Another exemplary schematic diagram of a synthetic substitution process provided by one embodiment of the present invention;
[0023] Figure 6 A schematic diagram of key improvement provided by an embodiment of the present invention;
[0024] Figure 7 A schematic diagram of the encryption operation structure principle provided by an embodiment of the present invention;
[0025] Figure 8 A schematic diagram of a table lookup operation principle provided by an embodiment of the present invention;
[0026] Fig. 9 A schematic diagram of the encryption process principle of the CBC mode provided in one embodiment of the present invention;
[0027] Fig.10 A schematic diagram of the CBC mode operation principle when the data to be encrypted is less than 16 groups according to an embodiment of the present invention;
[0028] Fig.11 A schematic diagram of the structure of a data processing device based on SIMD instructions provided by an embodiment of the present invention;
[0029] Fig.12 A schematic diagram of the structure of a data processing device based on SIMD instructions provided by another embodiment of the present invention;
[0030] Fig.13A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention.
[0031] The above drawings have shown clear embodiments of the present invention, which will be described in more detail below. These drawings and text descriptions are not intended to limit the scope of the present invention in any way, but to illustrate the concept of the present invention for those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION
[0032] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0033] First, the terms involved in the present invention are explained:
[0034] SIMD instruction: SIMD stands for Single Instruction Multiple Data, which is a set of instructions that can copy multiple operands and pack them into large registers. Take the addition instruction as an example. After the single instruction single data (SISD) processor CPU decodes the addition instruction, the execution unit first accesses the memory to obtain the first operand; then accesses the memory again to obtain the second operand; and then the sum operation can be performed. In a SIMD CPU, after the instruction is decoded, several execution units access the memory at the same time to obtain all operands at one time for calculation.
[0035] SM4 algorithm: is a block key algorithm. SM4.0 (formerly SMS4.0) is a block cipher standard adopted by the government of the People's Republic of China and was issued by the State Cryptography Administration on March 21, 2012. The relevant standard is "GM / T 0002-2012 "SM4 Block Cipher Algorithm" (formerly SMS4 Block Cipher Algorithm).
[0036] CBC mode: Cipher Block Chaining mode, ciphertext group chaining mode, in which ciphertext groups are connected to each other like a chain. In CBC mode, the plaintext is first XORed with the previous ciphertext and then encrypted. When decrypting, it is first decrypted and then XORed with the previous ciphertext.
[0037] In addition, the terms "first", "second", etc. are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. In the description of the following embodiments, the meaning of "multiple" is two or more, unless otherwise clearly and specifically defined.
[0038] The following specific embodiments may be combined with each other, and the same or similar concepts or processes may not be described in detail in some embodiments. The embodiments of the present invention will be described below in conjunction with the accompanying drawings.
[0039] An embodiment of the present invention provides a data processing method based on SIMD instructions, which is used in financial business scenarios such as financial IC cards, online banking, online securities, mobile payments, and electronic insurance policies, and can also be applied to any other scenarios that require data security encryption and decryption according to actual needs. The execution subject of this embodiment is a data processing device based on SIMD instructions, which can be set in an electronic device, which can be a server or other implementable computer device.
[0040] like Figure 1 FIG. 1 is a flow chart of a data processing method based on SIMD instructions provided in this embodiment, and the method includes:
[0041] Step 101, based on SIMD instructions, convert 2 N The data to be processed are grouped into a first number of first data blocks, and the keys of a second number of rounds are grouped into a second number of second data blocks; the data to be processed include data to be encrypted or data to be decrypted, and N is a preset positive integer.
[0042] Specifically, SIMD instructions can use registers with more bits, such as 128-bit registers, 256-bit registers, 512-bit registers, etc., when each instruction is executed, so that one instruction can process more data, for example, during the encryption or decryption process, multiple groups of 128-bit data to be processed can be operated at the same time. In practical applications, the data to be processed is not limited to 128 bits, for example, it can also be 64 bits or 256 bits.
[0043] The data to be processed can be received in real time or stored after being received and obtained from the storage area during operation. When the data to be processed is received or obtained in actual applications and needs to be encrypted or decrypted, the 2 NThe data to be processed are grouped into a first number of first data blocks, and the keys of the second number of rounds are formed into a second number of second data blocks. The first number and the second number can be set according to actual needs. For example, for 128 bits of data to be processed, if each group is split according to 32 bits, the first number is 4; if each group is split according to 64 bits, the first number is 2; for the second number, for example, 32 rounds of calculations are performed, the second number is 32, and 16 rounds of calculations are performed, the second number is 16. This embodiment is not limited.
[0044] A data block is a structure for storing data, such as a 128-bit data block, a 256-bit data block, or a 512-bit data block. The data block can be in memory or in a register. In subsequent operations, the data block can be used in registers of the corresponding number of bits. For example, based on SIMD instructions, 2 N The 128-bit data to be processed are grouped into four data blocks R[0], R[1], R[2], and R[3] and imported into four registers.
[0045] Similarly, the second number of rounds of keys can be formed into a second number of second data blocks based on SIMD instructions for the second number of rounds of encryption or decryption operations. For example, 32 rounds of keys can be formed into 32 data blocks based on SIMD instructions, where the kth round of keys forms the second data block RK[k]. RK[k] stores 2 N Group keys, each group is used for the operation of the corresponding group of data in the kth round. The number of bits of each group key in each round of keys is the same as the number of bits of each group in the first data block, for example 2 N =16 groups of 128-bit data to be processed form 4 first data blocks, each first data block stores 16 groups of 32-bit values, that is, each group of 128-bit data to be processed is split into 4 32-bit values, and 16 groups of 32-bit values on the same bit form a first data block, for example, 16 groups of 31:0 bits form a first data block.
[0046] Optionally, the second number of round keys may be obtained in advance by expanding a known encryption key, such as expanding a known 128-bit key to obtain a 32-bit round key, and any existing expansion method may be used. For the expansion of keys of other digits, the principle is similar to that of expanding a 128-bit key to obtain a 32-bit round key.
[0047] Step 102: Perform encryption operation based on the first number of first data blocks and the second number of second data blocks to obtain ciphertext data blocks; or perform decryption operation based on the first number of first data blocks and the second number of second data blocks to obtain plaintext data blocks.
[0048] Specifically, the data to be processed can be data to be encrypted or data to be decrypted. If it is data to be encrypted, an encryption operation is performed, and if it is data to be decrypted, a decryption operation is performed. The encryption operation and decryption operation processes are the same, and the keys are the same, but during the operation process, the order of using the second number of rounds of keys is reversed.
[0049] In practical applications, for data with different bits to be processed, when SIMD instructions use registers with a certain bit number, different numbers of first data blocks can be obtained according to different ways of splitting the data to be processed, and encryption or decryption operations of different numbers of groups of data to be processed can be implemented.
[0050] Exemplarily, when a SIMD instruction uses a 512-bit register, if each group of data to be processed is split into 32 bits, that is, each group of data to be processed is split into multiple 32-bit values, then 16 groups of 128 bits (split into 4 32 bits) of data to be processed can be operated on, that is, 4 first data blocks are formed, and 16 groups of 64 bits (split into 2 32 bits) of data to be processed can be operated on, that is, 2 first data blocks are formed; similarly, if each group of data to be processed is split into 16 bits, that is, each group of data to be processed is split into multiple 16-bit values, then 32 groups of data to be processed can be operated on, and the specific number of register bits and the way to split the data to be processed can be set according to actual needs.
[0051] The encryption and decryption operation principle based on SIMD instructions is similar to the existing 128-bit encryption and decryption operation process. The difference is that the SIMD instructions can be used to simultaneously realize the encryption and decryption operations of multiple groups of data, and for different splitting methods, its transportation principle can be similar to the existing principle of splitting by 32 bits. For example, when splitting by 16, the difference from the 32-bit splitting is that the number of first data blocks formed is different, and different rounds of operations can also be set, such as 16 rounds. For each round of operation, the data blocks involved in the XOR operation and T transformation are different, but the overall operation process can be consistent with the existing one. Accordingly, in order to realize the simultaneous processing of multiple groups of data based on SIMD instructions, it is necessary to improve the Sbox used in the existing operation process, and based on the improved Sbox, improve the T transformation in the operation process, so that the operation process can be implemented based on SIMD instructions. For each splitting method, the Sbox can be improved according to actual needs, and the T transformation in the operation process can be improved based on the improved Sbox.
[0052] Exemplarily, for splitting by 32 bits, the Sbox can be improved, the circular shift operation in the existing T transformation can be removed, and the T transformation result can be obtained directly through table lookup operation, and the table lookup operation can be set to 8-bit table lookup operation and 16-bit table lookup operation according to actual needs. The specific improvement method can be set according to actual needs, such as obtaining an improved Sbox that conforms to the 8-bit table lookup operation by circular shifting the existing Sbox, and obtaining an Sbox that conforms to the 16-bit table lookup operation according to the existing Sbox through certain rules. The splitting by 16 bits is the same as that by 32 bits, and will not be repeated here.
[0053] The data processing method based on SIMD instructions provided in this embodiment implements 2 N The encryption operation or decryption operation of the group of data to be processed effectively improves the speed of encryption and decryption operations, thereby improving data processing performance.
[0054] Another embodiment of the present invention further supplements the method provided by the above embodiment.
[0055] As an implementable approach, Figure 2 FIG. 1 is a flow chart of an exemplary method for processing data based on SIMD instructions provided in this embodiment. The method includes:
[0056] Step 1011, based on SIMD instruction, convert 2 N The 128-bit data to be processed are formed into four first data blocks R[0], R[1], R[2], and R[3], and the k-th round key is formed into a second data block RK[k].
[0057] The data to be processed includes data to be encrypted or data to be decrypted, k=0, 1, ..., 31. RK[k] includes 2 N A 32-bit key.
[0058] Specifically, for 128 bits of data to be processed, when encryption or decryption is required, 2 N The 128-bit data to be processed form a first number of four first data blocks R[0], R[1], R[2], and R[3]. R[0], R[1], R[2], and R[3] store 2 N Each group of 128 bits is divided into 32 bits each, and is divided into four 32-bit numbers. The number of bits can be represented as 0:31, 32:63, 64:95, and 96:127, respectively. R[0] stores 2 N 32-bit value of group 0:31, R[1] stores 2 NA data block is a structure for storing data, such as a 128-bit data block, a 256-bit data block, or a 512-bit data block. A data block can be in memory or in a register. In subsequent operations, the data block can be used in a register of the corresponding number of bits. For example, a 2 N The 128-bit data to be processed are grouped into four data blocks R[0], R[1], R[2], and R[3] and imported into four registers.
[0059] Similarly, based on the SIMD instruction, the second number of rounds of keys, which is 32 rounds, can be formed into 32 second data blocks, wherein the kth round of keys forms the second data block RK[k]. RK[k] stores 2 N A group of 32-bit keys, each group is used for the operation of the corresponding group of data in the kth round.
[0060] For example, in the i-th round of operation, R[i+1], R[i+2], R[i+3] need to be XORed with the key RK[i], and R[i+1], R[i+2], and R[i+3] are all 2 N A 32-bit value, RK[i] is also 2 N If a group of 32-bit values is formed, the XOR operation is performed on the 32-bit values of the corresponding group respectively.
[0061] Optionally, the 32-bit round key can be obtained by pre-expanding the 128-bit key to obtain a 32-bit round key, and then further improved to 2 N A 32-bit round key is formed to be suitable for encryption and decryption operations based on SIMD instructions.
[0062] The 32-bit round key can be obtained by expanding the 128-bit key, and any existing expansion method can be used, wherein the 128-bit key is a known encryption key. For example, the 32-bit round key can be generated according to a preset system parameter FK and a preset fixed parameter CK.
[0063] For example, the known encryption key is MK = (MK0, MK1, MK2, MK3), that is, the encryption key is divided into 4 32-bit values, the system parameter FK = (FK0, FK1, FK2, FK3), also 4 32-bit values, and the fixed parameter CK = (CK0, CK1, ..., CK 31 ), which is 32 32-bit values. i It is a 32-bit round key, which is generated by the encryption key as follows:
[0064]
[0065] For i = 0, 1, ..., 31, we have:
[0066]
[0067] in, represents an XOR operation, and T′ represents a synthetic substitution, which is a prior art and will not be described in detail.
[0068] After obtaining the 32-bit round key, the 32-bit round key can be further improved to obtain the key RK of the embodiment of the present invention, where RK=(RK[0], RK[1], . . . , RK
[31] ).
[0069] For example, the key rk of each round can be i Copy 2 N Servings, 2 per round N A 32-bit key, RK[i], consists of 2 N rk i , or any other feasible method can be used to obtain 2 N The 32-bit key is not limited in this embodiment.
[0070] Taking the 512-bit register in the CPU as an example, 2 N =16, that is, RK[i] includes 16 rk i .
[0071] Step 1021, perform 32 rounds of encryption operations based on R[0], R[1], R[2], R[3] and RK[k] to obtain a ciphertext data block; or, perform 32 rounds of decryption operations based on R[0], R[1], R[2], R[3] and RK[k] to obtain a plaintext data block; k=0, 1, ..., 31.
[0072] The k-th round key of the decryption operation process is the 31-k-th round key of the encryption operation process.
[0073] Specifically, the data to be processed can be data to be encrypted or data to be decrypted. If it is data to be encrypted, an encryption operation is performed, and if it is data to be decrypted, a decryption operation is performed. The encryption operation and decryption operation processes are the same, and the keys are the same, but during the operation process, the order of using the 32-round keys is reversed.
[0074] In specific operations, if it is an encryption operation, 32 rounds of encryption operations can be performed based on R[0], R[1], R[2], R[3] and RK[k] to obtain a ciphertext data block; if it is a decryption operation, 32 rounds of decryption operations can be performed based on R[0], R[1], R[2], R[3] and RK[k] to obtain a plaintext data block.
[0075] The encryption and decryption operation principle based on SIMD instructions is similar to the existing 128-bit encryption and decryption operation process. The difference is that SIMD instructions can be used to simultaneously implement encryption and decryption operations for multiple sets of data. Accordingly, in order to be able to simultaneously process multiple sets of data based on SIMD instructions, it is necessary to improve the Sbox used in the existing operation process. Based on the improved Sbox, the T transformation in the operation process is improved so that the operation process can be implemented based on SIMD instructions.
[0076] Optionally, in practical applications, encryption and decryption can adopt CBC mode. When adopting CBC mode, the parameter IV (Initialization Vector) is required to participate in the operation process. When performing encryption operation, R[0], R[1], R[2], and R[3] need to be XORed with parameter IV first, and then 32 rounds of operation are performed. When performing decryption operation, the result obtained by 32 rounds of operation is XORed with parameter IV to obtain the corresponding plaintext. It can be understood that in order to be applicable to the simultaneous processing of multiple groups of data based on SIMD instructions, parameter IV is also obtained through pre-improvement. In the existing operation process, the first group of 128-bit data is XORed with the original 128-bit parameter IV′, and then 32 rounds of operation are performed. In the present invention, the original parameter IV′ is improved to 2 N The 128-bit value is used for the XOR operation with each group of data to be encrypted during the operation, or for the XOR operation with each group of 32 rounds of operation results.
[0077] Optionally, in CBC mode, the first 2 N The operation of the group of data to be processed adopts the preset initial parameter IV, and the ciphertext of the first batch is used as the parameter IV of the next batch.
[0078] It should be noted that the improved CBC mode forms a chain in a certain way from the existing standard SM4 CBC mode in terms of parameter IV. In the existing standard SM4 CBC mode, taking the encryption process as an example, the encryption result (i.e., ciphertext) of the previous 128-bit plaintext is used as parameter IV' of the next 128-bit plaintext encryption process, so that the previous and next data form a chain. In the improved CBC mode of the embodiment of the present invention, taking the 512-bit register as an example, the previous batch (16 groups) )The ciphertext of the 128-bit plaintext is used as the parameter IV of the next batch (16 groups) of 128-bit plaintext encryption process. Taking the first batch (including groups 0 to 15) and the second batch (including groups 16 to 31) as examples, the ciphertext of the 0th group of plaintext in the first batch will be used as the parameter IV of the 16th group in the second batch, the ciphertext of the 1st group of plaintext in the first batch will be used as the parameter IV of the 17th group in the second batch, and so on. The ciphertext of the 15th group of plaintext in the first batch will be used as the parameter IV of the 31st group in the second batch.
[0079] Optionally, for the initial parameter IV, the original initial parameter IV′ may be copied 2 N Servings, 2 N The 128-bit value of the group is used as the initial parameter IV of the present invention. When the first batch of data to be processed is not used for operation, the parameter IV used is the ciphertext of the previous batch, which is also 2 N The 128-bit value is grouped to achieve CBC mode operation based on SIMD instructions.
[0080] Optionally, for the case of other splitting methods and other rounds of operation, the specific principle is similar to the above-mentioned 32-bit splitting. Take 128-bit encrypted data and a 512-bit register as an example. For example, splitting by 16 bits and 16 rounds of operation form 8 data blocks, such as R1[0] to R1[7]. Each data block stores 32 groups of 16-bit values. Then, 16 rounds of encryption operation can be performed according to R1[0] to R1[7] and the key of 16 rounds. The details will not be repeated here.
[0081] like Figure 3 As shown, it is another exemplary flow chart of the data processing method based on SIMD instructions provided in this embodiment.
[0082] As an implementable manner, based on the above embodiment, optionally, 32 rounds of encryption operations are performed according to R[0], R[1], R[2], R[3] and RK[k] to obtain a ciphertext data block, including:
[0083] Step 2011, for the i-th round of calculation, determine R[i+4] based on R[i], R[i+1], R[i+2], R[i+3] and RK[i].
[0084] Where i = 0, 1, ..., 31, R[i] is 32×2 N Block of bits.
[0085] Step 2012, use R
[32] ~R
[35] obtained after 32 rounds of calculation as the ciphertext data block.
[0086] Specifically, in each round of operation, a new data block R[i+4] is generated, which together with R[i+1], R[i+2], and R[i+3] serves as the basis for the next round of operation. The principle is similar to that of each round of operation in the prior art, except that the operation process is implemented based on SIMD instructions, which can realize the operation of multiple sets of data at the same time.
[0087] Exemplarily, taking encryption operation as an example, in the i=0th round, R[4] is determined based on R[0], R[1], R[2], R[3] and key RK[0]. In the i=1th round, R[5] is determined based on R[1], R[2], R[3], R[4] and key RK[1]. And so on. In the i=31st round, R
[35] is determined based on R
[31] , R
[32] , R
[33] , R
[34] and key RK
[31] . R
[32] , R
[33] , R
[34] and R
[35] are used as the ciphertext data blocks corresponding to the first data blocks R[0], R[1], R[2] and R[3].
[0088] In one embodiment, optionally, 32 rounds of decryption operations are performed according to R[0], R[1], R[2], R[3] and RK[k] to obtain a plaintext data block, including:
[0089] For the i-th round of operation, according to R[i], R[i+1], R[i+2], R[i+3] and RK[i], R[i+4] is determined, i=0, 1, ..., 31, R[i] is 32×2 N bits of data block; R
[32] ~R
[35] obtained after 32 rounds of calculations are used as the plaintext data block.
[0090] Among them, for the i-th round of operations, the specific operation of determining R[i+4] is the same or similar to the encryption operation based on R[i], R[i+1], R[i+2], R[i+3] and RK[i], and will not be repeated here.
[0091] In one embodiment, optionally, during the encryption or decryption process, for the i-th round of operation, R[i], R[i+1], R[i+2], R[i+3] and RK[i] are used to determine R[i+4], including:
[0092] For the i-th round of operations, determine the synthetic permutation T of the i-th round based on R[i+1], R[i+2], R[i+3] and RK[i] i Input X i ; X i Input the synthetic permutation T of round i i , get the synthetic permutation T of round i i The output Y i ; R[i] and Y i Perform an XOR operation to obtain R[i+4].
[0093] Specifically, X i For 2 N The data block formed by the group of 32-bit values, the synthetic permutation T of the i-th round i is a transformation process that transforms the input X iEach group of 32-bit values is transformed into another group of 2 N A data block formed by a group of 32-bit values, that is, the output Y i , R[i] and Y i Perform an XOR operation to obtain R[i+4]. That is:
[0094]
[0095]
[0096] in, Represents the exclusive-or operation.
[0097] For example, taking a 512-bit register as an example, the table lookup operation can use the SIMD instruction _mm512_i32gather_epi32 (_m512i vindex, void const* base_addr, int scale), and the XOR operation can use the SIMD instruction _mm512_xor_si512 (_m512ia, _m512ib). For SIMD instructions, registers of different bits have different specific SIMD instructions for implementing similar operations, which can be set according to actual needs.
[0098] It should be noted that for encryption and decryption operations, for the i-th round of operations, the specific operation of R[i+4] is determined to be consistent based on R[i], R[i+1], R[i+2], R[i+3] and RK[i]. Therefore, the i-th round of operations may refer to the i-th round of encryption operations or the i-th round of decryption operations.
[0099] Optionally, for the case of other splitting modes and other rounds of operation, the specific principle is similar to the above-mentioned splitting by 32 bits. Taking 128 bits of encrypted data and 512 bits of register as an example, for example, splitting by 16 bits and performing 16 rounds of operation, 8 data blocks are formed, such as R1[0] to R1[7]. Each data block stores 32 groups of 16-bit values. Then, 16 rounds of encryption operation can be performed according to R1[0] to R1[7] and the key of 16 rounds. For the j-th round of operation, R1[j+8] can be determined according to R1[j] to R1[j+7] and the key RK1[j], where j=0, 1, ..., 15. R1
[16] to R1
[23] obtained by 16 rounds of operation are used as plaintext data blocks or ciphertext data blocks.
[0100] In one embodiment, if Figure 4 As shown in the figure, it is an exemplary schematic diagram of the synthetic substitution provided in this embodiment, wherein X i Input the synthetic permutation T of round i i , get the synthetic permutation T of round ii The output Y i ,include:
[0101] Step 2021, for X i For each group of 32-bit values in, a table lookup operation is performed according to the high 16 bits, the low 16 bits and the first improved Sbox of the group of 32-bit values to obtain a first table lookup result corresponding to the group of high 16 bits and a second table lookup result corresponding to the group of low 16 bits.
[0102] Step 2022, perform an XOR operation on the first table lookup result corresponding to the upper 16 bits of each group and the second table lookup result corresponding to the lower 16 bits of each group to obtain the synthetic permutation T of the i-th round. i The output Y i .
[0103] Specifically, the first improved Sbox can be obtained in advance for table lookup operation. The first improved Sbox is used to look up the table for the 16-bit value, that is, X i Each group of 32-bit values in is split into two 16-bit values, an upper 16-bit value and a lower 16-bit value. Table lookup operations are performed on the two 16-bit values to obtain a first table lookup result corresponding to the upper 16 bits and a second table lookup result corresponding to the lower 16 bits.
[0104] Based on a SIMD table lookup instruction, X can be realized i Middle 2 N The table lookup operation of the 16-bit value can greatly improve the operation speed.
[0105] In one implementation, the first improved Sbox may be obtained by improving the existing original Sbox, and may specifically include:
[0106] Generate Sbox1 and Sbox2 according to the original Sbox; Sbox1 and Sbox2 both include 256×256 32-bit values; Sbox1 and Sbox2 are used as the first improved Sbox.
[0107] Among them, Sbox1 is used for table lookup operation of the lower 16 bits, and Sbox2 is used for table lookup operation of the upper 16 bits.
[0108] Specifically, the first improved Sbox may include two Sboxes, Sbox1 and Sbox2, which are used for table lookup operations of lower 16 bits and upper 16 bits, respectively.
[0109] In one embodiment, generating Sbox1 and Sbox2 according to the original Sbox includes:
[0110] According to the original Sbox, use the following formula 2 to generate Sbox1:
[0111] Sbox1=[Sbox1[0],…,Sbox1[256×256-1]]
[0112]
[0113] According to the original Sbox, the following formula 3 is used to generate Sbox2:
[0114] Sbox2=[Sbox2[0],…,Sbox2[256×256-1]]
[0115]
[0116] Among them, j = [0, 256*256], <<< means circular left shift, represents XOR operation, % represents remainder, / represents division, Sbox[i] represents the i-th 32-bit value in the original Sbox, and Sbox includes 256 32-bit values.
[0117] In another embodiment, Figure 5 As shown, another exemplary schematic diagram of the synthetic substitution provided in this embodiment is shown, where X i Input the synthetic permutation T of round i i , get the synthetic permutation T of round i i The output Y i ,include:
[0118] Step 2031, for X i For each group of 32-bit values in i [s] decomposed into 4 8-bit values X i [s][0], X i [s][1], X i [s][2], X i [s][3], and perform a table lookup operation based on the 4 8-bit values and the second improved Sbox to obtain the third table lookup result y corresponding to each 8-bit in the group i [s][0]、y i [s][1], y i [s][2], y i [s][3].
[0119] Where s = 0, 1, ..., 2 N -1,X i [s] represents X i The sth group of 32-bit values in .
[0120] Step 2032, perform an XOR operation on the third table lookup results corresponding to each 8-bit of each group to obtain the synthetic permutation T of the i-th round i The output Yi .
[0121] Right now:
[0122] Y i =[Y i [0],Y i [1], ..., Y i [2 N -1]]
[0123]
[0124] or,
[0125]
[0126] Among them, [y i [0][0],y i [1][0], ..., y i [2 N -1][0]] means 2 N The third table lookup result corresponding to group 31:24 bits, [y i [0][1],y i [1][1], ..., y i [2 N -1][1]] means 2 N The third table lookup result corresponding to group 23:16 bits, and so on.
[0127] Specifically, another improvement can be made to the original Sbox to obtain a second improved Sbox for 8-bit table lookup operation. i Each group of 32-bit values in is split into four 8-bit values, and table lookup operations are performed separately. The third table lookup results corresponding to each 8-bit of each group are XORed to obtain the synthetic permutation T of the i-th round. i The output Y i Based on a SIMD XOR instruction, 2 N XOR operation of the group. That is, 2 N The third table lookup results corresponding to each 8 bits of the group are XOR-operated in parallel.
[0128] Optionally, the second improved Sbox can be obtained by cyclically shifting the 256 32-bit values in the original Sbox to obtain four Sboxes, such as Sbox1, Sbox2, Sbox3 and Sbox4, which are used for table lookup operations of four 8-bit values respectively. i Input X i The 7∶0 bit value is used for table lookup operation, Sbox2 is used for T i Input X iThe 15:8 bit value is used for table lookup operation, Sbox3 is used for T i Input X i The 23:16-bit value of the table is used for lookup operation; Sbox4 is used for T i Input X i The 31:24 bit value is used for table lookup.
[0129] Based on a SIMD table lookup instruction, X can be realized i Middle 2 N The table lookup operation of the group 8-bit value greatly improves the operation speed.
[0130] In one embodiment, in the case of X i For each group of 32-bit values in i [s] decomposed into 4 8-bit values X i [s][0], X i [s][1], X i [s][2], X i [s][3], and perform a table lookup operation based on the 4 8-bit values and the second improved Sbox to obtain the third table lookup result y corresponding to each 8-bit in the group i [s][0]、y i [s][1], y i [s][2], y i [s][3] Before, the method further includes:
[0131] Generate Sbox1, Sbox2, Sbox3 and Sbox4 according to the original Sbox; Sbox1, Sbox2, Sbox3 and Sbox4 each include 256 32-bit values; Sbox1, Sbox2, Sbox3 and Sbox4 are used as the second improved Sbox; among them, Sbox1 is used for X i [s][3] table lookup operation; Sbox2 is used for X i [s][2] table lookup; Sbox3 is used for X i [s][1] table lookup; Sbox4 is used for X i Table lookup of [s][0].
[0132] Optionally, Sbox1, Sbox2, Sbox3 and Sbox4 are generated according to the original Sbox, including:
[0133] Shift the 256 32-bit values of the original Sbox left by 8 bits respectively to obtain Sbox1; shift the 256 32-bit values of the original Sbox left by 16 bits respectively to obtain Sbox2; shift the 256 32-bit values of the original Sbox left by 24 bits respectively to obtain Sbox3; use the original Sbox as Sbox4.
[0134] As another practicable manner, based on the above embodiment, optionally, CBC mode is used for encryption or decryption; for the i-th round of operation, R[i], R[i+1], R[i+2], R[i+3] and RK[i] are used to determine R[i+4], including:
[0135] 2 N The 128-bit data to be processed is the jth batch of data to be encrypted. For the 0th round of encryption operation, R[4] is determined according to the parameters IV, R[0], R[1], R[2], R[3] and RK[0]. For the i≠0th round of encryption operation, R[i+4] is determined according to R[i], R[i+1], R[i+2], R[i+3] and RK[i]. When j=1, the parameter IV is the preset 2 N 128-bit value; when j>1, parameter IV is the ciphertext of the j-1th batch of data to be encrypted; or
[0136] 2 N The 128-bit data to be processed is the jth batch of data to be decrypted. For the i-th round of decryption operation, R[i], R[i+1], R[i+2], R[i+3] and RK[i] are used to determine R[i+4]. When j=1, the parameter IV is the preset 2. N 128-bit value; when j>1, parameter IV is the first data block formed by the j-1th batch of data to be decrypted; accordingly, R
[32] ~R
[35] obtained after 32 rounds of calculation are used as plaintext data blocks, including:
[0137] Perform an XOR operation on R
[32] ~R
[35] obtained after 32 rounds of decryption operations and parameter IV to obtain a plaintext data block.
[0138] Specifically, when the CBC mode is used for encryption and decryption, the parameter IV needs to be involved in the operation process. When performing encryption operations, R[0], R[1], R[2], and R[3] need to be XORed with the parameter IV first, and then 32 rounds of operations are performed. When performing decryption operations, the results obtained from the 32 rounds of operations are XORed with the parameter IV to obtain the corresponding plaintext. The process of the 32 rounds of operations is consistent with the above process and will not be repeated here. It can be understood that in order to be applicable to the simultaneous processing of multiple groups of data based on SIMD instructions, the parameter IV is also obtained through pre-improvement. In the existing operation process, the first group of 128-bit data is XORed with the original 128-bit parameter IV′, and then 32 rounds of operations are performed. In the present invention, the original parameter IV′ is improved to 2 N The 128-bit value is used for the XOR operation with each group of data to be encrypted during the operation, or for the XOR operation with each group of 32 rounds of operation results.
[0139] Optionally, in CBC mode, the first 2 N The operation of the group of data to be processed adopts the preset initial parameter IV, and the ciphertext of the first batch is used as the parameter IV of the next batch.
[0140] Optionally, for the initial parameter IV, the original initial parameter IV′ may be copied 2 N Servings, 2 N The 128-bit value of the group is used as the initial parameter IV of the present invention. When the first batch of data to be processed is not used for operation, the parameter IV used is the ciphertext of the previous batch, which is also 2 N The 128-bit value is grouped to achieve CBC mode operation based on SIMD instructions.
[0141] In one embodiment, if 2 N The 128-bit data to be processed is the jth batch of data to be encrypted. For the 0th round of encryption operation, according to the parameters IV, R[0], R[1], R[2], R[3] and RK[0], R[4] is determined, including:
[0142] If 2 N The 128-bit group of data to be processed is the jth batch of data to be encrypted. For the 0th round of encryption operation, IV is XORed with R[0], R[1], R[2], and R[3] to obtain R′[0], R′[1], R′[2], and R′[3]. Based on R′[0], R′[1], R′[2], R′[3], and RK[0], R[4] is determined.
[0143] In an exemplary implementation, taking a 512-bit register as an example, the encryption process based on SIMD instructions is described in detail:
[0144] Since the register is 512 bits, R[i] can store 2 N = 16 groups of 32-bit values, that is, each time 16 128-bit values are processed simultaneously.
[0145] 1. Preparation
[0146] 1. Key Improvement
[0147] In order to facilitate loading the key into the 512-bit register, the key may be expanded from the original 128 bits to 128*16=2048 bits, that is, for each round of keys, it is expanded from 32 bits to 16 32 bits.
[0148] For example, Figure 6 , which is a schematic diagram of the key improvement provided by this embodiment. Among them, SM4[0]~SM4
[31] are 32 rounds of 32-bit keys obtained by expanding the known 128-bit key, namely rk0~rk31 , expand each round key into 16 32-bit keys RK[k], k = 0, 1, ..., 31. RK[k] stores 16 SM4[k].
[0149] 2. Sbox Improvements
[0150] In the prior art, in the T transformation (i.e., synthetic permutation) process, each 32-bit input needs to be looked up four times based on the original Sbox and a circular shift is performed. In the embodiment of the present invention, the Sbox is improved, and in the synthetic permutation T i Shift operations are avoided during the process, thereby reducing operations and further improving the calculation speed.
[0151] The embodiment of the present invention can perform two improvement schemes on the original Sbox:
[0152] (1) Generate an Sbox for each 8 bits
[0153] Shift the 256 32-bit values of the original Sbox left 8 bits respectively to obtain Sbox1, which is used for table lookup operation on the 7:0 bit values; shift the 256 32-bit values of the original Sbox left 16 bits respectively to obtain Sbox2, which is used for table lookup operation on the 15:8 bit values; shift the 256 32-bit values of the original Sbox left 24 bits respectively to obtain Sbox3, which is used for table lookup operation on the 23:16 bit values; copy the 256 32-bit values of the original Sbox as Sbox4, which is used for table lookup operation on the 31:24 bit values.
[0154] (2) Generate an Sbox for each 16 bits
[0155]
[0156] Sbox1=[Sbox1[0],…,Sbox1[256×256-1]]
[0157]
[0158] Sbox2=[Sbox2[0],…,Sbox2[256×256-1]]
[0159] Among them, Sbox1 is used for the lower 16-bit table lookup operation, Sbox2 is used for the upper 16-bit table lookup operation, j = [0, 256 * 256], <<< means circular left shift, represents XOR operation, % represents remainder, / represents division, Sbox[i] represents the i-th 32-bit value in the original Sbox, and Sbox includes 256 32-bit values.
[0160] The improved Sbox can be stored in memory according to the corresponding data blocks.
[0161] There are two improvement schemes, which can be selected according to actual needs. Scheme (1) occupies less memory, and the probability of cache miss will be reduced. Each synthesis replacement requires 4 table lookups, which occupies more CPU resources. Scheme (2) occupies more memory, and the probability of cache miss will be increased. Each synthesis replacement only requires 2 table lookups, which occupies less CPU resources. As the secondary cache becomes larger and larger, in most CPUs, scheme (2) can be faster and have higher performance. In some specific CPUs, such as CPUs with smaller secondary caches, scheme (1) may be faster and have higher performance. In actual applications, you can choose according to the specific CPU scenario.
[0162] Exemplarily, the 256 32-bit values included in the original Sbox are as follows:
[0163] 0x8ED55B5B,0xD0924242,0x4DEAA7A7,0x06FDFBFB,0xFCCF3333,0x65E28787,0xC93DF4F4,0x6BB5DEDE,0x4E165858,0x6EB4DADA,0x44145050,0xCAC10B0B,0x8828A0A0,0x17F8EFEF,0x9C2CB0B0,0x11051414,0x872BACAC,0xFB669D9D,0xF2986A6A,0xAE77D9D9,0x822AA8A8,0x46BCFAFA,0x14041010,0xCFC00F0F,0x02A8AAAA,0x54451111,0x5F134C4C,0xBE269898,0x6D482525,0x9E841A1A,0x1E061818,0xFD9B6666,0xEC9E7272,0x4A430909,0x10514141,0x24F7D3D3,0xD5934646,0x53ECBFBF,0xF89A6262,0x927BE9E9,0xFF33CCCC,0x04555151,0x270B2C2C,0x4F420D0D,0x59EEB7B7,0xF3CC3F3F,0x1CAEB2B2,0xEA638989,0x74E79393,0x7FB1CECE,0x6C1C7070,0x0DABA6A6,0xEDCA2727,0x28082020,0x48EBA3A3,0xC1975656,0x80820202,0xA3DC7F7F,0xC4965252,0x12F9EBEB,0xA174D5D5,0xB38D3E3E,0xC33FFCFC,0x3EA49A9A,0x5B461D1D,0x1B071C1C,0x3BA59E9E,0x0CFFF3F3,0x3FF0CFCF,0xBF72CDCD,0x4B175C5C,0x52B8EAEA,0x8F810E0E,0x3D586565,0xCC3CF0F0,0x7D196464,0x7EE59B9B,0x91871616,0x734E3D3D,0x08AAA2A2,0xC869A1A1,0xC76AADAD,0x85830606,0x7AB0CACA,0xB570C5C5,0xF4659191,0xB2D96B6B,0xA7892E2E,0x18FBE3E3,0x47E8AFAF,0x330F3C3C,0x674A2D2D,0xB071C1C1,0x0E575959,0xE99F7676,0xE135D4D4,0x661E7878,0xB4249090,0x360E3838,0x265F7979,0xEF628D8D,0x38596161,0x95D24747,0x2AA08A8A,0xB1259494,0xAA228888,0x8C7DF1F1,0xD73BECEC,0x05010404,0xA5218484,0x9879E1E1,0x9B851E1E,0x84D75353,0x00000000,0x5E471919,0x0B565D5D,0xE39D7E7E,0x9FD04F4F,0xBB279C9C,0x1A534949,0x7C4D3131,0xEE36D8D8,0x0A020808,0x7BE49F9F,0x20A28282,0xD4C71313,0xE8CB2323,0xE69C7A7A,0x42E9ABAB,0x43BDFEFE,0xA2882A2A,0x9AD14B4B,0x40410101,0xDBC41F1F,0xD838E0E0,0x61B7D6D6,0x2FA18E8E,0x2BF4DFDF,0x3AF1CBCB,0xF6CD3B3B,0x1DFAE7E7,0xE5608585,0x41155454,0x25A38686,0x60E38383,0x16ACBABA,0x295C7575,0x34A69292,0xF7996E6E,0xE434D0D0,0x721A6868,0x01545555,0x19AFB6B6,0xDF914E4E,0xFA32C8C8,0xF030C0C0,0x21F6D7D7,0xBC8E3232,0x75B3C6C6,0x6FE08F8F,0x691D7474,0x2EF5DBDB,0x6AE18B8B,0x962EB8B8,0x8A800A0A,0xFE679999,0xE2C92B2B,0xE0618181,0xC0C30303,0x8D29A4A4,0xAF238C8C,0x07A9AEAE,0x390D3434,0x1F524D4D,0x764F3939,0xD36EBDBD,0x81D65757,0xB7D86F6F,0xEB37DCDC,0x51441515,0xA6DD7B7B,0x09FEF7F7,0xB68C3A3A,0x932FBCBC,0x0F030C0C,0x03FCFFFF,0xC26BA9A9,0xBA73C9C9,0xD96CB5B5,0xDC6DB1B1,0x375A6D6D,0x 15504545,0xB98F3636,0x771B6C6C,0x13ADBEBE,0xDA904A4A,0x57B9EEEE,0xA9DE7777,0x4CBEF2F2,0x837EFDFD,0x551 14444,0xBDDA6767,0x2C5D7171,0x45400505,0x631F7C7C,0x50104040,0x325B6969,0xB8DB6363,0x220A2828,0xC5C207 07,0xF531C4C4,0xA88A2222,0x31A79696,0xF9CE3737,0x977AEDED,0x49BFF6F6,0x992DB4B4,0xA475D1D1,0x90D34343, 0x5A124848,0x58BAE2E2,0x71E69797,0x64B6D2D2,0x70B2C2C2,0xAD8B2626,0xCD68A5A5,0xCB955E5E,0x624B2929,0x 3C0C3030,0xCE945A5A,0xAB76DDDD,0x867FF9F9,0xF1649595,0x5DBBE6E6,0x35F2C7C7,0x2D092424,0xD1C61717,0xD66 FB9B9,0xDEC51B1B,0x94861212,0x78186060,0x30F3C3C3,0x897CF5F5,0x5CEFB3B3,0xD23AE8E8,0xACDF7373,0x794C35 35,0xA0208080,0x9D78E5E5,0x56EDBBBB,0x235E7D7D,0xC63EF8F8,0x8BD45F5F,0xE7C82F2F,0xDD39E4E4,0x68492121,
[0164] Accordingly, the T transformation in the operation process also needs to be improved according to the improved Sbox. After the improvement, the synthetic permutation of each round is T i .
[0165] 2. Encryption Process
[0166] 1. Read the data (plaintext (data to be encrypted), key) into a 512-bit register
[0167] During the encryption operation, the 512-bit key can be imported into the register using the _mm512_loadu_si512 instruction, as follows:
[0168] If the improved key pointer is (int*)key, then key[0] represents the first SM4[0], key
[16] represents the first SM4[1], and the register to store the key is RKEY. Then the key for round i=0 is:
[0169] RKEY[0]=_mm512_loadu_si512((const void*)&key[0])
[0170] Import the key for round i=1 as:
[0171] RKEY[1]=_mm512_loadu_si512((const void*)&key
[16] )
[0172] By analogy, the key for round i=31 is:
[0173] RKEY
[31] =_mm512_loadu_si512((const void*)&key
[496] )
[0174] That is, the k-th round key forms the second data block RK[k] and is stored in the register RKEY[k], where k=0, 1, ..., 31.
[0175] The timing of importing the key into the register during the operation of each round can be set according to actual needs. For example, the key can be imported into the register in advance, or it can be imported into the register in real time when it needs to participate in the operation.
[0176] For encryption operations, the plaintext also needs to be imported into a 512-bit register. Since the plaintext needs to be stored in four registers, namely R[0], R[1], R[2], and R[3], and R[0] stores the first 32 bits of 16 groups of 128 bits, R[1] needs to store the 32nd to 63rd bits of 16 groups of 128 bits, and so on, R[0] to R[3] need to use table lookup instructions, such as _mm512_i32gather_epi32, as follows:
[0177] (1) The first pointer of the plaintext string is Data, so a temporary register needs to be generated first, RTemp = _mm512_setr_epi32 (0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60).
[0178] (2) R[0] = _mm512_i32gather_epi32(RTemp, (int*)Data, 4), which implements storing the first 32 bits of R[0] in 16 groups of 128 bits.
[0179] Among them, the function of the instruction R[0]=_mm512_i32gather_epi32(RTemp, (int*)Data, 4) is as follows:
[0180] R[0][31:0]=*(int*)((char*)Data+RTemp[31:0]*4);
[0181] R[0][63:32]=*(int*)((char*)Data+RTemp[63:32]*4);
[0182] R[0][95:64]=*(int*)((char*)Data+RTemp[95:64]*4);
[0183] …
[0184] R[0][511:480]=*(int*)((char*)Data+RTemp[255:224]*4).
[0185] It can be understood that for the decryption process, the ciphertext and the key are read into the register to perform the decryption operation.
[0186] 2. Encryption
[0187] During the encryption operation, the SIMD instructions involved include XOR instructions, shift instructions, AND operation instructions, and table lookup instructions. Taking the improved Sbox of the 16-bit table lookup in the above solution (2) as an example, the encryption operation is explained. Figure 7 As shown in FIG. 1 , it is a schematic diagram of the encryption operation structure principle provided in this embodiment. The timing process based on SIMD instructions is as follows:
[0188] (1) Taking the i=0th round operation as an example, three XOR operations are performed to determine the input X0 of the synthetic permutation T0 of the 0th round according to R[1], R[2], R[3] and RK[0] (RK[0] is stored in register RKEY[0] during the operation):
[0189] a)X0=_mm512_xor_si512(R[1], R[2])
[0190] b)X0=_mm512_xor_si512(X0, R[3])
[0191] c)X0=_mm512_xor_si512(X0,RKEY[0])
[0192] (2) Set up a temporary 512-bit register for storage and operation to obtain the high 16-bit value and the low 16-bit value and perform table lookup operation:
[0193] RMask_mm512_setr_epi32 (0xFFFF, OxFFFF, OxFFFF, OxFFFF, OxFFFF, OxFFFF, OxFFFF, OxFFFF, OxFFFF, OxFFFF, OxFFFF, OxFFFF, OxFFFF, OxFFFF, OxFFFF, OxFFFF).
[0194] (3) Take out the high 16 bits and the low 16 bits
[0195] a) Take the lower 16 bits:
[0196] RLOW=_mm512_and_si512(X0, RMask)
[0197] b) Take the high 16 bits:
[0198] RHIGH=mm512_srli_epi32(R,16)
[0199] That is, all 32-bit values are shifted right 16 bits to get the upper 16 bits.
[0200] (4) Look up the table for the upper 16 bits and the lower 16 bits respectively, and then perform an XOR operation
[0201] a) Low 16 bits lookup table:
[0202] RLOW1=_mm512_i32gather_epi32(RLOW, (int*)Sbox1, 4)
[0203] b) High 16 bits lookup table:
[0204] RHIGH1=_mm512_i32gather_epi32(RHIGH, (int*)Sbox2, 4)
[0205] like Figure 8 FIG. 1 is a schematic diagram of the table lookup operation principle provided by this embodiment. Here, only a group of 32-bit values in X0 is taken as an example. In practical applications, one table lookup operation can implement 16 groups of table lookups in parallel.
[0206] It can be understood that the specific table lookup operation of the improved solution for 8-bit table lookup is similar to the 16-bit table lookup process, and will not be described in detail here.
[0207] c) XOR operation:
[0208] Y0=_mm512_xor_si512(RLOW1,RHIGH1)
[0209] (5) Perform an XOR operation on Y0 and R[0] to obtain R[4]:
[0210] R[4]=_mm512_xor_si512(Y0, R[0])
[0211] (6) Use R[1] to R[4] for the next round of calculations, and so on.
[0212] The decryption process is the same as the encryption process, the only difference is that the order of using the round keys is reversed, which will not be described here one by one.
[0213] For the CBC mode, still taking the 512-bit register as an example, the existing CBC is a 128-bit row operation, and the first group of 128-bit encryption results are used as the next group of 128-bit parameters IV'. After the embodiment of the present invention is optimized based on SIMD instructions, each encryption operation is 16 groups of 128 bits simultaneously operated, which is equivalent to a 2048-bit ciphertext output at a time. Therefore, a batch of 16 groups of 128-bit plaintext encryption results output 2048 bits of ciphertext as the next batch of encryption parameters IV, such as Fig. 9 The figure is a schematic diagram of the encryption process principle of the CBC mode provided in this embodiment. Among them, the original IV is the 128-bit original initial parameter IV', the first batch of 16 groups of data is the first batch of 16 groups of plaintext, Fig. 9 It can be seen that the ciphertext of the previous batch (16 groups) of 128-bit plaintext is used as the parameter IV of the encryption process of the next batch (16 groups) of 128-bit plaintext, with the first batch (including groups 0 to 15, corresponding to Fig. 9 The first batch of 128-bit plaintext [1] to
[16] ) and the second batch (including groups 16 to 31, corresponding to Fig. 9 For example, the ciphertext of the 0th group of plaintext in the first batch will be used as the parameter IV of the 16th group in the second batch, the ciphertext of the 1st group of plaintext in the first batch will be used as the parameter IV of the 17th group in the second batch, and so on. The ciphertext of the 15th group of plaintext in the first batch will be used as the parameter IV of the 31st group in the second batch.
[0214] In CBC mode, the encryption process requires the parameter IV to be XORed with the plaintext as the final data to be encrypted for subsequent encryption process. The subsequent SIMD instruction encryption process is consistent with the aforementioned encryption process and will not be described in detail here.
[0215] Optionally, when the plaintext is less than 16 groups of 128 bits at the end, you can switch to the existing CBC mode for encryption, that is, encrypt 128 bits of plaintext each time. Fig.10 As shown, it is a schematic diagram of the CBC mode operation principle provided by this embodiment when the number of data to be encrypted is less than 16 groups.
[0216] In CBC mode, the difference between the decryption operation and the encryption operation process is that the parameter IV is XORed with R
[32] ~R
[35] obtained after 32 rounds of operation to obtain R
[32] ′~R
[35] ′ as the final plaintext. The other processes are not described in detail.
[0217] The embodiment of the present invention conducts a comparative test on the computing speed of the encryption operation based on SIMD instructions provided by the present invention and the existing non-optimized encryption operation (using the professional encryption instructions provided by the CPU for computing, such as AES encryption instructions). For example, the CPU used for the test verification is Intel(R) Xeon(R) Silver 4114 CPU@2.20GHz, and the time consumption of encrypting 1GB with a single logical core is compared. The results are shown in Table 1 below:
[0218] Table 1
[0219] Encryption Algorithm No optimization AVX(128bit) AVX2 (256bit) AVX512 (512bit) Time consumed (seconds) 12.017 8.367 5.21 4.056
[0220] Among them, AVX means the use of the AVX instruction set in SIMD. The AVX instruction set is a new instruction set under the Sandy Bridge and Larrabee architectures. It is a single instruction multiple data stream that expands the previous 128 bits to 256 bits. AVX2 means the AVX2 instruction set in SIMD. The AVX2 instruction set is extended on the basis of AVX, and the supported integer SIMD data width is extended from 128 bits to 256 bits. Although Sandy Bridge has increased the supported SIMD data width to 256 bits, it only adds support for 256-bit floating-point SIMD, and the width of the integer SIMD data remains at 128 bits; the data element types supported by broadcast and permutation instructions, the support of shift operations for variable shift numbers of each data element, and stride memory access support are enhanced. AVX512 means the AVX512 instruction set, which is a further extension of AVX2. The width of the register is increased from 256 bits to 512 bits, and 16 groups of 32-bit data can be processed in parallel. These instruction sets are all existing technologies and will not be described one by one here. As shown in Table 1, the encryption operation based on SIMD instructions of 512-bit registers takes the shortest time and has the fastest speed. After optimization, it is 2.96 times that before optimization, which is equivalent to about 85.2MB per second before optimization. After optimization, the encryption operation based on SIMD instructions of 128-bit registers is 122.4MB per second, 256-bit registers is 196.5MB, and 512-bit registers is 252.5MB.
[0221] Therefore, accelerating the existing SM4 algorithm or the CBC mode of the SM4 algorithm based on SIMD instructions can effectively improve the computing speed and solve the problem of low encryption and decryption speed of the existing algorithm. After improving the computing performance, in actual application scenarios, there is no need to install a PCI-E encryption card, thereby saving costs.
[0222] Optionally, for registers of other bit numbers, splitting in other splitting methods, and other rounds of operations, the specific operation principle is similar to the above-mentioned 32-bit splitting, and can be set according to actual needs, which will not be repeated here.
[0223] It should be noted that the various practicable methods in this embodiment may be implemented separately, or may be combined in any combination without conflict to implement the present invention without limitation.
[0224] The data processing method based on SIMD instructions provided in this embodiment improves Sbox and T transformations, thereby avoiding circular shift operations during the calculation process and further improving the calculation speed.
[0225] Yet another embodiment of the present invention provides a data processing device based on SIMD instructions, which is used to execute the method of the above embodiment.
[0226] like Fig.11 FIG. 3 is a schematic diagram of a data processing device based on SIMD instructions provided in this embodiment. The device 30 includes: a first processing module 31 and a second processing module 32 .
[0227] The first processing module is used to convert 2 N The data to be processed are grouped into a first number of first data blocks, and the keys of the second number of rounds are grouped into a second number of second data blocks; the data to be processed include data to be encrypted or data to be decrypted, and N is a preset positive integer; the second processing module is used to perform encryption operation according to the first number of first data blocks and the second number of second data blocks to obtain ciphertext data blocks; or, perform decryption operation according to the first number of first data blocks and the second number of second data blocks to obtain plaintext data blocks.
[0228] Specifically, the first processing module can obtain the data to be processed and the key from other modules (such as the receiving module and the storage module), and convert the 2 N The data to be processed are grouped into a first number of first data blocks, and the keys of a second number of rounds are grouped into a second number of second data blocks, which are stored in a register or a memory. During operation, the second processing module can perform a second number of rounds of encryption operation according to the first data block and the second data block in the register to obtain a ciphertext data block; or, perform a second number of rounds of decryption operation according to the first data block and the second data block to obtain a plaintext data block; or, according to the demand, the data can be read from the memory to the register for operation.
[0229] Optionally, the encryption operation and the decryption operation may be performed in different modules, for example, the device may further include a third processing module, the second processing module may be used for the encryption operation, and the third processing module may be used for the decryption operation. Specific settings may be made according to actual needs.
[0230] In practical applications, the device may only perform encryption operations, may only perform decryption operations, or may have both encryption and decryption operations, and the specific settings may be based on actual needs.
[0231] Regarding the device in this embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and can achieve the same technical effect, so it will not be elaborated here.
[0232] Another embodiment of the present invention further supplements the device provided in the above embodiment.
[0233] In one implementation, based on the above embodiment, optionally, the first processing module is specifically configured to convert 2N The 128-bit data to be processed is formed into four first data blocks R[0], R[1], R[2], and R[3], and the k-th round key is formed into a second data block RK[k]; the data to be processed includes data to be encrypted or data to be decrypted, N is a preset positive integer, and RK[k] includes 2 N A 32-bit key is formed in the group; a second processing module is specifically used to perform 32 rounds of encryption operations according to R[0], R[1], R[2], R[3] and RK[k] to obtain a ciphertext data block; or, to perform 32 rounds of decryption operations according to R[0], R[1], R[2], R[3] and RK[k] to obtain a plaintext data block; k=0, 1, ..., 31; wherein the kth round key of the decryption operation process is the 31-kth round key of the encryption operation process.
[0234] Specifically, the first processing module can convert 2 N The 128-bit data to be processed is formed into four first data blocks R[0], R[1], R[2], and R[3], and the k-th round key is formed into a second data block RK[k], which is stored in a register or memory. During operation, the second processing module can perform 32 rounds of encryption operations based on R[0], R[1], R[2], R[3] and RK[k] in the register to obtain a ciphertext data block; or, perform 32 rounds of decryption operations based on R[0], R[1], R[2], R[3] and RK[k] to obtain a plaintext data block; or, the data can be read from the memory to the register for operation as required.
[0235] As an implementable manner, based on the above embodiment, optionally, the second processing module is specifically used to: in the encryption or decryption process, for the i-th round of operation, according to R[i], R[i+1], R[i+2], R[i+3] and RK[i], determine R[i+4], i=0, 1, ..., 31, R[i] is 32×2 N bits data block; in the encryption process, R
[32] ~R
[35] obtained after 32 rounds of operations are used as ciphertext data blocks; or, in the decryption process, R
[32] ~R
[35] obtained after 32 rounds of operations are used as plaintext data blocks.
[0236] Specifically, the encryption operation or decryption operation for the i-th round of operation, based on R[i], R[i+1], R[i+2], R[i+3] and RK[i], determines the specific operation of R[i+4] to be consistent, and will not be repeated here. For encryption operation, R
[32] ~R
[35] obtained after 32 rounds of operation are used as the ciphertext data block corresponding to the data to be encrypted. For decryption operation, R
[32] ~R
[35] obtained after 32 rounds of operation are used as the plaintext data block corresponding to the data to be decrypted.
[0237] Optionally, the second processing module is specifically configured to: for the i-th round of operation, determine the synthetic permutation T of the i-th round according to R[i+1], R[i+2], R[i+3] and RK[i] i Input X i ; X i Input the synthetic permutation T of round i i , get the synthetic permutation T of round i i The output Y i ; R[i] and Y i Perform an XOR operation to obtain R[i+4].
[0238] Optionally, the second processing module can also be divided into multiple submodules according to actual functions. For example, the second processing module can include: an input determination submodule, a synthesis permutation submodule, an output submodule and an XOR submodule. Among them, the input determination submodule is used to determine the synthesis permutation T of the i-th round according to R[i+1], R[i+2], R[i+3] and RK[i]. i Input X i , and sent to the synthesis permutation submodule, the synthesis permutation submodule will input X i Transform to Y i , and sent to the output submodule, the output submodule sends Yi to the XOR submodule, the XOR submodule sends Y i Perform an XOR operation with R[i] to obtain R[i+4], which is sent to the input determination submodule to enter the next round of calculation.
[0239] Optionally, the second processing module (or synthesis substitution submodule) is specifically used to: i For each group of 32-bit values in, a table lookup operation is performed according to the high 16 bits, low 16 bits and the first improved Sbox of the group of 32-bit values to obtain the first table lookup result corresponding to the group of high 16 bits and the second table lookup result corresponding to the group of low 16 bits; the first table lookup result corresponding to each group of high 16 bits and the second table lookup result corresponding to each group of low 16 bits are XORed to obtain the synthetic permutation T of the i-th round i The output Y i .
[0240] Optionally, each submodule in the second processing module may be further subdivided according to actual functions, and may be specifically configured according to actual needs.
[0241] Alternatively, if Fig.12As shown, it is a structural schematic diagram of the data processing device based on SIMD instructions provided in this embodiment. The device 30 can also include a preprocessing module 33, which is used to generate Sbox1 and Sbox2 according to the original Sbox; Sbox1 and Sbox2 both include 256×256 32-bit values; Sbox1 and Sbox2 are used as the first improved Sbox; wherein Sbox1 is used for table lookup operations of the lower 16 bits, and Sbox2 is used for table lookup operations of the upper 16 bits.
[0242] Specifically, the preprocessing module may obtain the original Sbox from the original Sbox storage area, and generate and store Sbox1 and Sbox2 according to the original Sbox for subsequent table lookup operations.
[0243] Optionally, the preprocessing module is specifically used to: generate Sbox1 according to the original Sbox using the following formula 2:
[0244] Sbox1=[Sbox1[0],…,Sbox1[256×256-1]]
[0245]
[0246] According to the original Sbox, the following formula 3 is used to generate Sbox2:
[0247] Sbox2=[Sbox2[0],…,Sbox2[256×256-1]]
[0248]
[0249] Among them, j = [0, 256*256], <<< means circular left shift, represents XOR operation, % represents remainder, / represents division, Sbox represents the original Sbox, and Sbox includes 256 32-bit values.
[0250] As another practicable manner, based on the above embodiment, optionally, the second processing module is specifically configured to: i For each group of 32-bit values in i [s] decomposed into 4 8-bit values X i [s][0], X i [s][1], X i [s][2], X i [s][3], and perform a table lookup operation based on the 4 8-bit values and the second improved Sbox to obtain the third table lookup result y corresponding to each 8-bit in the group i [s][0]、y i [s][1], y i [s][2], yi [s][3]; s = 0, 1, ..., 2 N -1, Xi[s] represents X i The sth group of 32-bit values in the sth group; XOR the third table lookup results corresponding to each 8-bit of each group to obtain the synthetic permutation T of the i-th round i The output Y i .Right now:
[0251] Y i =[Y i [0],Y i [1], ..., Y i [2 N -1]]
[0252]
[0253] or,
[0254]
[0255] Among them, [y i [0][0],y i [1][0], ..., y i [2 N -1][0]] means 2 N The third table lookup result corresponding to group 31:24 bits, [y i [0][1],y i [1][1], ..., y i [2 N -1][1]] means 2 N The third table lookup result corresponding to group 23:16 bits, and so on.
[0256] Optionally, the preprocessing module can also be used to: generate Sbox1, Sbox2, Sbox3 and Sbox4 according to the original Sbox; Sbox1, Sbox2, Sbox3 and Sbox4 each include 256 32-bit values; use Sbox1, Sbox2, Sbox3 and Sbox4 as the second improved Sbox; wherein Sbox1 is used for X i [s][3] table lookup operation; Sbox2 is used for X i [s][2] table lookup; Sbox3 is used for X i [s][1] table lookup; Sbox4 is used for X i Table lookup of [s][0].
[0257] Optionally, the preprocessing module is specifically used to circularly shift the 256 32-bit values of the original Sbox by 8 bits to the left to obtain Sbox1; circularly shift the 256 32-bit values of the original Sbox by 16 bits to the left to obtain Sbox2; circularly shift the 256 32-bit values of the original Sbox by 24 bits to the left to obtain Sbox3; and use the original Sbox as Sbox4.
[0258] As another practicable manner, based on the above embodiment, optionally, encryption or decryption may be performed in CBC mode; the second processing module is specifically used for:
[0259] 2 N The 128-bit data to be processed is the jth batch of data to be encrypted. For the 0th round of encryption operation, R[4] is determined based on the parameters IV, R[0], R[1], R[2], R[3] and RK[0]. For the i≠0th round of encryption operation, R[i+4] is determined based on R[i], R[i+1], R[i+2], R[i+3] and RK[i]. When j=1, the parameter IV is the preset 2 N 128-bit value; when j>1, parameter IV is the ciphertext of the j-1th batch of data to be encrypted; or, 2 N The 128-bit data to be processed is the jth batch of data to be decrypted. For the i-th round of decryption operation, R[i], R[i+1], R[i+2], R[i+3] and RK[i] are used to determine R[i+4]. When j=1, parameter IV is the preset 2. N a 128-bit value; when j>1, parameter IV is the first data block formed by the j-1th batch of data to be decrypted; accordingly, R
[32] ~R
[35] obtained after 32 rounds of operation are used as the plaintext data block, including: performing an XOR operation on R
[32] ~R
[35] obtained after 32 rounds of decryption operation and parameter IV to obtain the plaintext data block.
[0260] Optionally, the second processing module is specifically configured to: N The 128-bit group of data to be processed is the jth batch of data to be encrypted. For the 0th round of encryption operation, IV is XORed with R[0], R[1], R[2], and R[3] to obtain R′[0], R′[1], R′[2], and R′[3]. Based on R′[0], R′[1], R′[2], R′[3], and RK[0], R[4] is determined.
[0261] It should be noted that the various practicable methods in this embodiment may be implemented separately, or may be combined in any combination without conflict to implement the present invention without limitation.
[0262] Regarding the device in this embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and can achieve the same technical effect, so it will not be elaborated here.
[0263] Another embodiment of the present invention provides an electronic device for executing the method provided in the above embodiment. The electronic device may be a server or other implementable computer device.
[0264] like Fig.13 FIG. 5 is a schematic diagram of the structure of an electronic device provided in this embodiment. The electronic device 50 includes: a memory 51 , a transceiver 52 and at least one processor 53 .
[0265] Among them, the processor, memory and transceiver are interconnected through circuits; the memory stores computer execution instructions; the transceiver is used to receive data to be processed; at least one processor executes the computer execution instructions stored in the memory, so that at least one processor executes the method provided in any of the above embodiments.
[0266] Specifically, when the transceiver of the electronic device receives the data to be processed, the data to be processed can be stored or cached. When encryption or decryption is required, the processor reads and executes the computer execution instructions stored in the memory to implement the method provided by any of the above embodiments, and can store the obtained results in the memory or cache them in the internal memory, and can also send the processed results to other devices. The specific settings can be based on actual needs.
[0267] Optionally, in actual applications, the electronic device may only have the encryption function, and the decryption function is implemented by other devices. Similarly, the electronic device may only have the decryption function, and the encryption function is implemented by other devices, or may have both the encryption function and the decryption function. The specific settings can be made according to actual needs.
[0268] The electronic device provided by the embodiment of the present invention can be applied to financial business scenarios such as financial IC cards, online banking, online securities, mobile payments, and electronic insurance policies, and can also be applied to any other scenarios that require data security encryption and decryption according to actual needs. Taking mobile payment as an example, the electronic device can be a server that interacts with the user's mobile terminal for business. During the mobile payment process, the transmission data is encrypted and decrypted. After receiving the data to be processed sent by the user's mobile terminal, it is encrypted or decrypted based on the method provided in the above embodiment to obtain the corresponding plaintext or ciphertext for subsequent business processing.
[0269] It should be noted that the electronic device of this embodiment can implement the method provided by any of the above embodiments and can achieve the same technical effect, which will not be described in detail here.
[0270] Yet another embodiment of the present invention provides a computer-readable storage medium, in which computer-executable instructions are stored. When a processor executes the computer-executable instructions, the method provided in any of the above embodiments is implemented.
[0271] It should be noted that the computer-readable storage medium of this embodiment can implement the method provided by any of the above embodiments and can achieve the same technical effect, which will not be repeated here.
[0272] In the several embodiments provided by the present invention, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0273] Those skilled in the art will readily appreciate other embodiments of the present invention after considering the specification and practicing the invention disclosed herein. The present invention is intended to cover any variations, uses or adaptations of the present invention that follow the general principles of the present invention and include common knowledge or customary techniques in the art that are not disclosed by the present invention. The description and examples are to be considered exemplary only, and the true scope and spirit of the present invention is indicated by the following claims.
[0274] It should be understood that the present invention is not limited to the exact construction that has been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present invention is limited only by the appended claims.
Claims
1. A data processing method based on SIMD instructions, characterized in that: include: Based on SIMD instructions, 2 N The 128-bit data to be processed is formed into four first data blocks R[0], R[1], R[2], and R[3], and the k-th round key is formed into a second data block RK[k]; the data to be processed includes data to be encrypted or data to be decrypted, and RK[k] includes 2 N A 32-bit key, N is a preset positive integer; k = 0, 1, ..., 31; For the i-th round of operations, determine the synthetic permutation T of the i-th round based on R[i+1], R[i+2], R[i+3] and RK[i] i Input X i ; i = 0, 1, ..., 31; X i Input the synthetic permutation T of round i i , get the synthetic permutation T of round i i The output Y i ; R[i] and Y i Perform XOR operation to obtain R[i+4]; R[i] is 32×2 N bits of data block; during encryption operation, R[32]~R[35] obtained after 32 rounds of operation are used as ciphertext data blocks; or, during decryption operation, R[32]~R[35] obtained after 32 rounds of operation are used as plaintext data blocks; The k-th round key of the decryption operation process is the 31-k-th round key of the encryption operation process.
2. The method according to claim 1, characterized in that The X i Input the synthetic permutation T of round i i , get the synthetic permutation T of round i i The output Y i ,include: For X i For each group of 32-bit values in, perform a table lookup operation according to the upper 16 bits, lower 16 bits and the first improved Sbox of the group of 32-bit values to obtain a first table lookup result corresponding to the upper 16 bits of the group and a second table lookup result corresponding to the lower 16 bits of the group; Perform an XOR operation on the first table lookup result corresponding to the high 16 bits of each group and the second table lookup result corresponding to the low 16 bits of each group to obtain the synthetic permutation T of the i-th round. i The output Y i .
3. The method according to claim 2, characterized in that In targeting X i Before performing a table lookup operation according to the upper 16 bits, the lower 16 bits and the first improved Sbox of each group of 32-bit values in the 32-bit value group to obtain a first table lookup result corresponding to the upper 16 bits of the group and a second table lookup result corresponding to the lower 16 bits of the group, the method further comprises: Generate Sbox1 and Sbox2 based on the original Sbox; Sbox1 and Sbox2 both include 256×256 32-bit values; Take Sbox1 and Sbox2 as the first improved Sbox; Among them, Sbox1 is used for table lookup operation of the lower 16 bits, and Sbox2 is used for table lookup operation of the upper 16 bits.
4. The method according to claim 3, characterized in that The generating of Sbox1 and Sbox2 according to the original Sbox includes: According to the original Sbox, the following formula 2 is used to generate Sbox1: Sbox1=[Sbox1[0],…,Sbox1[256×256-1]] Sbox1[j]=(Sbox[j%256]<<<8)⊕(Sbox[j / 256]<<<16) Formula 2 According to the original Sbox, Sbox2 is generated using the following formula 3: Sbox2=[Sbox2[0],…,Sbox2[256×256-1]] Sbox2[j]=(Sbox[j%256]<<<24)⊕Sbox[j / 256] Formula 3 Wherein, j=[0,256*256], <<< indicates circular left shift, ⊕ indicates XOR operation, % indicates remainder, / indicates division, Sbox indicates original Sbox, and Sbox includes 256 32-bit values.
5. The method according to claim 1, characterized in that The X i Input the synthetic permutation T of round i i , get the synthetic permutation T of round i i The output Y i , include: For X i For each group of 32-bit values in i [s] decomposed into 4 8-bit values X i [s][0], X i [s][1], X i [s][2], X i [s][3], and perform a table lookup operation based on the 4 8-bit values and the second improved Sbox to obtain the third table lookup result y corresponding to each 8-bit in the group i [s][0]、y i [s][1], y i [s][2], y i [s][3]; s = 0, 1, ..., 2 N -1,X i [s] represents X i The sth group of 32-bit values in ; Perform an XOR operation on the third table lookup results corresponding to each 8-bit of each group to obtain the synthetic permutation T of the i-th round i The output Y i .
6. The method according to claim 5, characterized in that In targeting X i For each group of 32-bit values in i [s] decomposed into 4 8-bit values X i [s][0], X i [s][1], X i [s][2], X i [s][3], and perform a table lookup operation based on the 4 8-bit values and the second improved Sbox to obtain the third table lookup result y corresponding to each 8-bit in the group i [s][0]、y i [s][1], y i [s][2], y i [s][3] before, the method further comprises: Generate Sbox1, Sbox2, Sbox3 and Sbox4 based on the original Sbox; Sbox1, Sbox2, Sbox3 and Sbox4 each include 256 32-bit values; Sbox1, Sbox2, Sbox3 and Sbox4 are used as the second improved Sbox; Among them, Sbox1 is used for X i [s][3] table lookup; Sbox2 is used for X i [s][2] table lookup; Sbox3 is used for X i [s][1] table lookup; Sbox4 is used for X i Table lookup of [s][0].
7. The method according to claim 6, characterized in that The generating of Sbox1, Sbox2, Sbox3 and Sbox4 according to the original Sbox comprises: Shift the 256 32-bit values of the original Sbox cyclically left by 8 bits to obtain Sbox1; Circularly shift the 256 32-bit values of the original Sbox by 16 bits respectively to the left to obtain Sbox2; Shift the 256 32-bit values of the original Sbox 24 bits left respectively to obtain Sbox3; Set the original Sbox as Sbox4.
8. The method according to claim 1, characterized in that Use CBC mode for encryption or decryption; During the encryption or decryption process, for the i-th round of operation, R[i], R[i+1], R[i+2], R[i+3] and RK[i] are used to determine R[i+4], including: 2 N The 128-bit data to be processed is the jth batch of data to be encrypted. For the 0th round of encryption operation, R[4] is determined based on the parameters IV, R[0], R[1], R[2], R[3] and RK[0]. For the i≠0th round of encryption operation, R[i+4] is determined based on R[i], R[i+1], R[i+2], R[i+3] and RK[i]. When j=1, the parameter IV is the preset 2 N 128-bit value; when j>1, parameter IV is the ciphertext of the j-1th batch of data to be encrypted; or 2 N The 128-bit data to be processed is the jth batch of data to be decrypted. For the i-th round of decryption operation, R[i], R[i+1], R[i+2], R[i+3] and RK[i] are used to determine R[i+4]. When j=1, parameter IV is the preset 2. N a 128-bit value; when j>1, parameter IV is the first data block formed by the j-1th batch of data to be decrypted; R[32]~R[35] obtained after 32 rounds of operation are used as the plaintext data block, specifically: R[32]~R[35] obtained after 32 rounds of decryption operation are XOR-ed with parameter IV to obtain the plaintext data block.
9. The method according to claim 8, characterized in that If the 2 N The 128-bit data to be processed is the jth batch of data to be encrypted. For the 0th round of encryption operation, according to the parameters IV, R[0], R[1], R[2], R[3] and RK[0], R[4] is determined, including: If the 2 N The 128-bit data to be processed is the jth batch of data to be encrypted. For the 0th round of encryption operation, perform an XOR operation on IV and R[0], R[1], R[2], and R[3] to obtain R′[0], R′[1], R′[2], and R′[3]. Determine R[4] based on R′[0], R′[1], R′[2], R′[3] and RK[0].
10. A data processing device based on SIMD instructions, characterized in that: include: The first processing module is used to convert 2 N The 128-bit data to be processed is formed into four first data blocks R[0], R[1], R[2], and R[3], and the k-th round key is formed into a second data block RK[k]; the data to be processed includes data to be encrypted or data to be decrypted, and RK[k] includes 2 N A 32-bit key, N is a preset positive integer; k = 0, 1, ..., 31; The second processing module is used to determine the synthetic permutation T of the i-th round of operation according to R[i+1], R[i+2], R[i+3] and RK[i]. i Input X i ; i = 0, 1, ..., 31; X i Input the synthetic permutation T of round i i , get the synthetic permutation T of round i i The output Y i ; R[i] and Y i Perform XOR operation to obtain R[i+4]; R[i] is 32×2 N bits of data block; during encryption operation, R[32]~R[35] obtained after 32 rounds of operation are used as ciphertext data blocks; or, during decryption operation, R[32]~R[35] obtained after 32 rounds of operation are used as plaintext data blocks; The k-th round key of the decryption operation process is the 31-k-th round key of the encryption operation process.
11. The device according to claim 10, characterized in that The second processing module is specifically used for: For X i For each group of 32-bit values in, perform a table lookup operation according to the upper 16 bits, lower 16 bits and the first improved Sbox of the group of 32-bit values to obtain a first table lookup result corresponding to the upper 16 bits of the group and a second table lookup result corresponding to the lower 16 bits of the group; Perform an XOR operation on the first table lookup result corresponding to the high 16 bits of each group and the second table lookup result corresponding to the low 16 bits of each group to obtain the synthetic permutation T of the i-th round. i The output Y i .
12. The device according to claim 11, characterized in that Also includes: Preprocessing module; The preprocessing module is used to: Generate Sbox1 and Sbox2 based on the original Sbox; Sbox1 and Sbox2 both include 256×256 32-bit values; Take Sbox1 and Sbox2 as the first improved Sbox; Among them, Sbox1 is used for table lookup operation of the lower 16 bits, and Sbox2 is used for table lookup operation of the upper 16 bits.
13. The device according to claim 10, characterized in that The second processing module is specifically used for: For X i For each group of 32-bit values in i [s] decomposed into 4 8-bit values X i [s][0], X i [s][1], X i [s][2], X i [s][3], and perform a table lookup operation based on the 4 8-bit values and the second improved Sbox to obtain the third table lookup result y corresponding to each 8-bit in the group i [s][0]、y i [s][1], y i [s][2], y i [s][3]; s = 0, 1, ..., 2 N -1,X i [s] represents X i The sth group of 32-bit values in ; Perform an XOR operation on the third table lookup results corresponding to each 8-bit of each group to obtain the synthetic permutation T of the i-th round i The output Y i .
14. The device according to claim 13, characterized in that Also includes: Preprocessing module; The preprocessing module is used to: Generate Sbox1, Sbox2, Sbox3 and Sbox4 based on the original Sbox; Sbox1, Sbox2, Sbox3 and Sbox4 each include 256 32-bit values; Sbox1, Sbox2, Sbox3 and Sbox4 are used as the second improved Sbox; Among them, Sbox1 is used for X i [s][3] table lookup operation; Sbox2 is used for X i [s][2] table lookup; Sbox3 is used for X i [s][1] table lookup; Sbox4 is used for X i Table lookup of [s][0].
15. An electronic device, characterized in that: include: A memory, a transceiver, and at least one processor; The processor, the memory and the transceiver are interconnected via a circuit; The memory stores computer-executable instructions; The transceiver is used to receive data to be processed; The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor performs the method according to any one of claims 1 to 9.
16. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and when the processor executes the computer-executable instructions, the method according to any one of claims 1 to 9 is implemented.
Citation Information
Patent Citations
Improved SM4 parallel encryption and decryption communication method based on GPU (Graphics Processing Unit)
CN105490802A
Method for optimizing SM4 encryption and decryption in parallel operation mode, and processor
CN109600217A
Rapid software implementation method of national cipher SM4
CN110166223A
Software optimization implementation method of SM4 encryption algorithm
CN110197076A
Method for encrypting and decrypting multiple messages in parallel by adopting grouping symmetric key algorithm
CN110880967A
