sm4ekey

SVE2 SM4 Key Update

SM4EKEY <Zd>.S, <Zn>.S, <Zm>.S

SM4 key-schedule step on each 128-bit segment of a scalable vector (SVE2).

Pseudocode Operation

for each 128-bit segment s
  key = Zn[s]                      // words k0 (oldest, bits 31:0) to k3
  for r = 0 to 3
    t = key.k1 EOR key.k2 EOR key.k3 EOR Zm[s].word[r]
    t = SM4_SBOX_EACH_BYTE(t)
    t = t EOR ROL(t, 13) EOR ROL(t, 23) EOR key.k0
    key = (t : key.k3 : key.k2 : key.k1)   // drop k0, new word on top
  Zd[s] = key

Example

SM4EKEY Z0.S, Z1.S, Z2.S

Encoding

Binary Layout
01000101
31:24
0
23
0
22
1
21
Zm
20:16
11110
15:11
0
10
Zn
9:5
Zd
4:0
 
Format SVE2 Crypto
Opcode 0x4520F000
Extension SVE2

Operands

  • Zd
    Destination scalable vector register
  • Zn
    First source: the current key, four 32-bit words per 128-bit segment
  • Zm
    Second source: the round constants, four 32-bit words per 128-bit segment

Related

Other forms of sm4ekey

More in SVE2

Reference

Instruction Forms

Encoding Instruction ISA Bit pattern
0xCE60C800 SM4EKEY <Vd>.4S, <Vn>.4S, <Vm>.4S A64 11001110011 | Rm | 1 | 1 | 00 | 10 | Rn | Rd
0x4520F000 SM4EKEY <Zd>.S, <Zn>.S, <Zm>.S A64 01000101 | 0 | 0 | 1 | Zm | 11110 | 0 | Zn | Zd

Description

The SM4EKEY instruction reads four rounds of 32-bit input key values from each 128-bit segment of the first source vector, along with four rounds of 32-bit constants from the corresponding 128-bit segment of the second source vector. The four rounds of output key values are derived in accordance with the SM4 standard, and placed in the corresponding segments of the destination vector. This instruction is unpredicated. ID_AA64ZFR0_EL1.SM4 indicates whether this instruction is implemented. This instruction is illegal when executed in Streaming SVE mode, unless FEAT_SME_FA64 is implemented and enabled.

Operation

CheckNonStreamingSVEEnabled();
constant integer VL = CurrentVL;
constant integer segments = VL DIV 128;
bits(VL) operand1 = Z[n, VL];
bits(VL) operand2 = Z[m, VL];
bits(VL) result;

for s = 0 to segments-1
    bits(128) source = Elem[operand2, s, 128];
    bits(32) intval;
    bits(8) sboxout;
    bits(32) const;
    bits(128) roundresult = Elem[operand1, s, 128];

    for index = 0 to 3
        const = Elem[source, index, 32];
        intval = roundresult<127:96> EOR roundresult<95:64> EOR roundresult<63:32> EOR const;
        for i = 0 to 3
            Elem[intval, i, 8] = Sbox(Elem[intval, i, 8]);

        intval = intval EOR ROL(intval, 13) EOR ROL(intval, 23);
        intval = intval EOR roundresult<31:0>;

        roundresult<31:0> = roundresult<63:32>;
        roundresult<63:32> = roundresult<95:64>;
        roundresult<95:64> = roundresult<127:96>;
        roundresult<127:96> = intval;

    Elem[result, s, 128] = roundresult;

Z[d, VL] = result;