Disclosure of Invention
The embodiment of the invention discloses a method, a device, equipment and a medium for SM4 encryption and key expansion, which are used for optimizing an SM4 encryption algorithm, improving the calculation efficiency and realizing the rapid encryption of mass data.
In a first aspect, an embodiment of the present invention provides an SM4 encryption method, including:
carrying out 32 times of iterative computation on 128-bit data to be encrypted by using a pre-generated round key, wherein the iterative computation comprises the following steps:
i is 0, 1, …, 31, where RK
iFor the purpose of the pre-generated round key,
the calculation method of T is as follows: if the T input is a, then,
then
And taking the iteration result of 32 iteration calculations as the encrypted data.
According to the SM4 encryption method provided by the embodiment of the invention, the calculation of the synthesis and replacement T in the SM4 encryption algorithm is decomposed by constructing the T1 and the T2, and compared with the linear transformation in the existing SM4 encryption algorithm which needs 4 times of table lookup, 4 times of circulation left shift and 4 times of XOR operation, the linear transformation in the SM4 encryption algorithm is simplified into the method which only needs two times of table lookup and one time of XOR operation, so that the SM4 encryption algorithm is simplified, the calculation efficiency is improved, and the rapid encryption of mass data can be realized.
In a possible implementation manner, in the method provided in the embodiment of the present invention, if the key is encrypted
Then the round key RK
iThe generation method comprises the following steps:
i=0,1,…,31,FK=(FK
0,FK
1,FK
2,FK
3) And CK ═ CK (CK)
0,CK
1,…,CK
31) All are fixed constants, and the calculation method of T' is as follows: if the T' input is a,
then
In one possible embodiment, the method provided by the present example includes T1[ a1] ═ L1[ BigSbox (a1) ], T2[ a2] ═ L2[ BigSbox (a2) ], where T1 is obtained by combining BigSbox calculation with linear transformation L1, T2 is obtained by combining BigSbox calculation with linear transformation L2, and BigSbox, L1, and L2 are all table lookup calculations.
In one possible implementation, embodiments of the present invention provide a method wherein, if the input to the BigSbox is a1,
then BigSbox (a1) ═ B1,
in a second aspect, an embodiment of the present invention provides an SM4 key expansion method, including:
if the key is encrypted
Then the round key RK
iThe generation method comprises the following steps:
i=0,1,…,31,FK=(FK
0,FK
1,FK
2,FK
3) And CK ═ CK (CK)
0,CK
1,…,CK
31) All are fixed constants, and the calculation method of T' is as follows: if the T' input is a,
then
According to the SM4 key expansion method provided by the embodiment of the invention, the calculation of synthesizing and replacing T ' in the SM4 key expansion algorithm is decomposed by constructing T '1 and T '2, and compared with the linear transformation in the existing SM4 key expansion algorithm which needs 4 times of table lookup, 4 times of circulation left shift and 4 times of XOR operation, the linear transformation in the SM4 key expansion algorithm is simplified into the linear transformation which only needs two times of table lookup and one time of XOR operation, so that the SM4 key expansion algorithm is simplified, the calculation efficiency is improved, and the rapid encryption of mass data can be realized.
In one possible embodiment, the method provided by the embodiments of the present invention includes that T '1[ a1] ═ L'1[ BigSbox (a1) ], T '2[ a2] ═ L'2[ BigSbox (a2) ], where T '1 is obtained by combining BigSbox calculation and linear transformation L'1, T '2 is obtained by combining BigSbox calculation and linear transformation L'2, and BigSbox, L '1, and L'2 are all table lookup calculations.
In one possible implementation, embodiments of the present invention provide a method wherein, if the input to the BigSbox is a1,
then BigSbox (a1) ═ B1,
in a third aspect, an embodiment of the present invention provides an SM4 encryption apparatus, including:
a calculating unit, configured to perform 32 iterative computations on 128-bit data to be encrypted by using a pre-generated round key, where the iterative computations are:
i is 0, 1, …, 31, where RK
iFor the purpose of the pre-generated round key,
the calculation method of T is as follows: if the T input is a, then,
then
And the processing unit is used for taking the iteration result of the 32 times of iterative computation as the encrypted data.
In a possible implementation manner, in the apparatus provided in this embodiment of the present invention, if the key is encrypted
The calculation unit generates the round key RK in the following way
i:
i=0,1,…,31,FK=(FK
0,FK
1,FK
2,FK
3) And CK ═ CK (CK)
0,CK
1,…,CK
31) All are fixed constants, and the calculation method of T' is as follows: if the T' input is a,
then
In one possible implementation, in the apparatus provided by the embodiment of the present invention, T1[ a1] ═ L1[ BigSbox (a1) ], T2[ a2] ═ L2[ BigSbox (a2) ], where T1 is obtained by combining BigSbox calculation and linear transformation L1, T2 is obtained by combining BigSbox calculation and linear transformation L2 calculation, and BigSbox, L1, and L2 are all table lookup calculations.
In one possible implementation, in the apparatus provided in the embodiments of the present invention, if the input of BigSbox is a1,
then BigSbox (a1) ═ B1,
in a fourth aspect, an embodiment of the present invention provides an SM4 key expansion apparatus, including:
a processing unit for encrypting the key
Under the condition of (1), a round key RK is generated by adopting the following method
i:
i=0,1,…,31,FK=(FK
0,FK
1,FK
2,FK
3) And CK ═ CK (CK)
0,CK
1,…,CK
31) All are fixed constants, and the calculation method of T' is as follows: if the T' input is a,
then
In one possible implementation, in the apparatus provided in this embodiment of the present invention, T '1[ a1] ═ L'1[ BigSbox (a1) ], T '2[ a2] ═ L'2[ BigSbox (a2) ], where T '1 is obtained by combining BigSbox calculation and linear transformation L'1, T '2 is obtained by combining BigSbox calculation and linear transformation L'2, and BigSbox, L '1, and L'2 are all table lookup calculations.
In one possible implementation, in the apparatus provided in the embodiments of the present invention, if the input of BigSbox is a1,
then BigSbox (a1) ═ B1,
in a fifth aspect, an embodiment of the present application provides an SM4 password optimization apparatus, including: the apparatus may include at least one processor, at least one memory, and computer program instructions stored in the memory, which when executed by the processor, implement the method provided by the first or second aspect of an embodiment of the present application.
In a sixth aspect, embodiments of the present application provide a computer-readable storage medium, on which computer program instructions are stored, which, when executed by a processor, implement the method provided by the first aspect or the second aspect of the embodiments of the present application.
In specific implementation, the derivation process of the calculation method of T' is as follows:
in the T ' substitution, T ' (·) L ' (τ (·)), first, for τ, the input is assumed to be
Output is as
Then (b)
0,b
1,b
2,b
3)=τA=(Sbox(a
0),Sbox(a
1),Sbox(a
2),Sbox(a
3))。
Constructing a 16-bit input and 16-bit output substitution table BigSbox, wherein the substitution table BigSbox is constructed as follows: let the input of BigSbox be a1 ═ a0,a1) The output is B1 ═ B0,b1) Wherein a is0,a1,b0,b1All are 8-bit bytes, BigSbox [ a ]0,a1]=(b0,b1) In which b is0=Sbox[a0],b1=Sbox[a1]For the same reason, the input is a2 ═ a2,a3) The output is B2 ═ B2,b3) Then BigSbox [ a ]2,a3]=(b2,b3)。
In summary, B ═ (B1, B2) ═ B (B) can be obtained0,b1,b2,b3)=τA=(Big BigSbox[a0,a1],Big BigSbox[a2,a3]),Tau A calculation requires 2 BigSbox tables.
In the T 'permutation, the linear transformation L' has the following properties: suppose that
Then
Suppose that
Then
According to the nature of the linear transformation
Two sizes of the structure are 2
16When the linear transformation tables L '1 and L '2 of 65536, both of which have 16 bits input and 32 bits output, are used, L '1[ B1 []=L'(B1*65536);L'2[B2]=L'(B2)。
Combining the L '1 and BigSbox tables to obtain a T'1 table, and combining the L '2 and BigSbox tables to obtain a T'2 table, wherein the specific structure is as follows: suppose that
Then T'1[ a ]]=L'1[BigSbox[a]];T'2[a]=L'2[BigSbox[a]]I.e. T' is calculated as: input is as
Then
Based on the same inventive concept, the embodiment of the invention also provides an SM4 encryption device.
As shown in fig. 2, an SM4 encryption apparatus provided in an embodiment of the present invention includes:
a calculating
unit 201, configured to perform 32 iterative computations on 128 bits of data to be encrypted by using a pre-generated round key, where the iterative computations are:
i is 0, 1, …, 31, where RK
iFor the purpose of the pre-generated round key,
the calculation method of T is as follows: if the T input is a, then,
then
And the processing unit 202 is used for taking an iteration result of the 32 iteration calculations as the encrypted data.
In a possible implementation manner, in the apparatus provided in this embodiment of the present invention, if the key is encrypted
The calculation unit generates the round key RK in the following way
i:
i=0,1,…,31,FK=(FK
0,FK
1,FK
2,FK
3) And CK ═ CK (CK)
0,CK
1,…,CK
31) All are fixed constants, and the calculation method of T' is as follows: if the T' input is a,
then
In one possible implementation, in the apparatus provided by the embodiment of the present invention, T1[ a1] ═ L1[ BigSbox (a1) ], T2[ a2] ═ L2[ BigSbox (a2) ], where T1 is obtained by combining BigSbox calculation and linear transformation L1, T2 is obtained by combining BigSbox calculation and linear transformation L2 calculation, and BigSbox, L1, and L2 are all table lookup calculations.
In one possible implementation, in the apparatus provided in the embodiments of the present invention, if the input of BigSbox is a1,
then BigSbox (a1) ═ B1,
an embodiment of the present invention further provides an SM4 key expansion apparatus, including:
a processing unit for encrypting the key
Under the condition of (1), a round key RK is generated by adopting the following method
i:
i=0,1,…,31,FK=(FK
0,FK
1,FK
2,FK
3) And CK ═ CK (CK)
0,CK
1,…,CK
31) Are all fixed constantThe method for calculating T' comprises the following steps: if the T' input is a,
then
In one possible implementation, in the apparatus provided in this embodiment of the present invention, T '1[ a1] ═ L'1[ BigSbox (a1) ], T '2[ a2] ═ L'2[ BigSbox (a2) ], where T '1 is obtained by combining BigSbox calculation and linear transformation L'1, T '2 is obtained by combining BigSbox calculation and linear transformation L'2, and BigSbox, L '1, and L'2 are all table lookup calculations.
In one possible implementation, in the apparatus provided in the embodiments of the present invention, if the input of BigSbox is a1,
then BigSbox (a1) ═ B1,
in addition, the SM4 encryption and key expansion methods and apparatuses of the embodiments of the present application described in connection with fig. 1-2 can be implemented by an SM4 password optimization device. Fig. 3 shows a hardware structure diagram of an SM4 password optimization device provided in an embodiment of the present application.
The SM4 password optimization device may include a processor 301 and a memory 302 having stored computer program instructions.
In particular, the processor 301 may include a Central Processing Unit (CPU), or an Application Specific Integrated Circuit (ASIC), or may be configured as one or more Integrated circuits implementing embodiments of the present invention.
Memory 302 may include mass storage for data or instructions. By way of example, and not limitation, memory 302 may include a Hard Disk Drive (HDD), floppy Disk Drive, flash memory, optical Disk, magneto-optical Disk, tape, or Universal Serial Bus (USB) Drive or a combination of two or more of these. Memory 302 may include removable or non-removable (or fixed) media, where appropriate. The memory 302 may be internal or external to the data processing apparatus, where appropriate. In a particular embodiment, the memory 302 is a non-volatile solid-state memory. In a particular embodiment, the memory 302 includes Read Only Memory (ROM). Where appropriate, the ROM may be mask-programmed ROM, Programmable ROM (PROM), Erasable PROM (EPROM), Electrically Erasable PROM (EEPROM), electrically rewritable ROM (EAROM), or flash memory or a combination of two or more of these.
The processor 301 implements any one of the SM4 encryption methods or the SM4 key expansion method in the above embodiments by reading and executing computer program instructions stored in the memory 302.
In one example, the SM4 password optimization device may also include a communication interface 303 and a bus 310. As shown in fig. 3, the processor 301, the memory 302, and the communication interface 303 are connected via a bus 310 to complete communication therebetween.
The communication interface 303 is mainly used for implementing communication between modules, apparatuses, units and/or devices in the embodiment of the present invention.
The bus 310 includes hardware, software, or both that couple the components of the SM4 cryptographic optimization device to one another. By way of example, and not limitation, a bus may include an Accelerated Graphics Port (AGP) or other graphics bus, an Enhanced Industry Standard Architecture (EISA) bus, a Front Side Bus (FSB), a Hypertransport (HT) interconnect, an Industry Standard Architecture (ISA) bus, an infiniband interconnect, a Low Pin Count (LPC) bus, a memory bus, a Micro Channel Architecture (MCA) bus, a Peripheral Component Interconnect (PCI) bus, a PCI-Express (PCI-X) bus, a Serial Advanced Technology Attachment (SATA) bus, a video electronics standards association local (VLB) bus, or other suitable bus or a combination of two or more of these. Bus 310 may include one or more buses, where appropriate. Although specific buses have been described and shown in the embodiments of the invention, any suitable buses or interconnects are contemplated by the invention.
The SM4 password optimization device may perform the SM4 encryption method or the SM4 key expansion method in the embodiment of the present invention, thereby implementing the SM4 encryption method or the SM4 key expansion method described in conjunction with fig. 1.
In addition, in combination with the SM4 password optimization method in the foregoing embodiments, embodiments of the present invention may provide a computer-readable storage medium to implement. The computer readable storage medium having stored thereon computer program instructions; the computer program instructions, when executed by a processor, implement any one of the SM4 encryption methods or the SM4 key expansion method of the above embodiments.
As will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present application is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the application. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
While the preferred embodiments of the present application have been described, additional variations and modifications in those embodiments may occur to those skilled in the art once they learn of the basic inventive concepts. Therefore, it is intended that the appended claims be interpreted as including preferred embodiments and all alterations and modifications as fall within the scope of the application.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present invention without departing from the spirit and scope of the invention. Thus, if such modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include such modifications and variations.