TPLSM4Context

Data types, cryptography

Description

TPLSM4Context stores the expanded key schedule of the SM4 block cipher. RoundKeys[0] is the first encryption round key and RoundKeys[31] the last; decryption walks the same array in reverse.

Fill the record with PLSM4InitKey before encrypting or decrypting. The key expansion applies the standard FK system parameters and CK constants and keeps only the 32 derived round keys. The block routines take the context as a const parameter and do not modify it, so one context can serve any number of blocks.

Syntax

Delphi

TPLSM4Context = Record
  RoundKeys: Array [0.. 31] Of UInt32;
End;

Fields

RoundKeys32 round keys in encryption order

Remarks

A key whose length is not 16 bytes leaves the record zeroed; the block routines detect an all-zero round-key set and return an empty string.

The all-zero check is a guard for an unused context: a correctly derived SM4 schedule cannot have all 32 round keys equal to zero.

The context does not retain the original key, so changing the key requires another PLSM4InitKey call.

PLSM4EncryptBlock walks RoundKeys forward and PLSM4DecryptBlock walks it backward; both require a 16-byte block and return an empty string otherwise.

The record is 128 bytes and has no managed fields, so it can be copied and stored without lifetime management.

See also

PLSM4InitKey, PLSM4EncryptBlock, PLSM4DecryptBlock, SM4EncryptECB, SM4EncryptCBC