sm4.bend checks
raw source on the hub · import bend-osdev-legacy-ciphers@0.1.0.0/sm4.bend as Sm4
SM4 (GB/T 32907-2016, Information security technology - SM4 block cipher algorithm; in English, draft-ribose-cfrg-sm4 and RFC 8998, section 2.1): a 128-bit block cipher with a 128-bit key, as a cipher for cipher-modes' modes (~LcSm4Key, ~LcSm4.encrypt, ~LcSm4.decrypt).
The state is four 32-bit words X0..X3, big-endian from the block, which is exactly a CmBlock. Each of the 32 rounds is (X0, X1, X2, X3) -> (X1, X2, X3, X0 xor T(X1 xor X2 xor X3 xor rk_i)) and the output is the last four words in reverse order. Decryption undoes each step in reverse order, which is the same function as the specification's "the round keys in reverse order", and makes decrypt(encrypt(b)) == b provable for every key and block: a round is undone whatever T is.
4 imports
import Base import bend-osdev-bytes@0.1.0.0/bytes.bend as B import bend-osdev-cipher-modes@0.1.0.0/cipher_modes.bend as M import ./legacy_ciphers.bend as L
Types
type LcSm4Key source · line 113 · raw
Data
An SM4 key: the S-box table (built once per key) and the 32 round keys (opaque; built by LcSm4.key)
LcSm4Key@tab:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @rks:List<&2, U32> -> LcSm4Key
Definitions
def sm4.sbox.list source · line 20 · raw
List<&2, U32>
The S-box (GB/T 32907-2016, 6.2: row i, column j is entry 16i + j)
def sm4.sbox source · line 38 · raw
0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab
def sm4.s source · line 41 · raw
@+tab:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @+a:U32 -> @n:Nat -> U32
def sm4.tau source · line 45 · raw
@+tab:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @+a:U32 -> U32
tau: the S-box on each byte of a word
def sm4.t source · line 49 · raw
@+tab:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @x:U32 -> U32
T = L(tau(x)), L(B) = B xor B<<<2 xor B<<<10 xor B<<<18 xor B<<<24
def sm4.t2 source · line 54 · raw
@+tab:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @x:U32 -> U32
T' of the key schedule: L'(B) = B xor B<<<13 xor B<<<23
def sm4.step source · line 60 · raw
@+tab:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @rk:U32 -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sm4.unstep source · line 66 · raw
@+tab:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @rk:U32 -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
the round undone
def sm4.rounds source · line 71 · raw
@+tab:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @rks:List<&2, U32> -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sm4.unrounds source · line 78 · raw
@+tab:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @rks:List<&2, U32> -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sm4.rev source · line 86 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
the reverse transformation R: (A0, A1, A2, A3) -> (A3, A2, A1, A0)
def sm4.fk source · line 94 · raw
@n:Nat -> U32
FK and CK_i (ck_(i,j) = (4i + j) * 7 mod 256)
def sm4.ck source · line 97 · raw
@+i:U32 -> U32
def sm4.schedule source · line 103 · raw
@n:Nat -> @+tab:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @+i:U32 -> @k0:U32 -> @+k1:U32 -> @+k2:U32 -> @+k3:U32 -> List<&2, U32>
rk_i = K_(i+4) = K_i xor T'(K_(i+1) xor K_(i+2) xor K_(i+3) xor CK_i), for n more rounds from round i, the window K_i .. K_(i+3) in k0 .. k3
def sm4.mk source · line 116 · raw
@+xs:List<&2, U32> -> @+i:Nat -> U32
def sm4.key.pick source · line 120 · raw
@ok:Bool -> @+xs:List<&2, U32> -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcSm4Key>
def LcSm4.key source · line 131 · raw
@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcSm4Key>
a 16-byte key
def LcSm4.encrypt source · line 135 · raw
@k:LcSm4Key -> @b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def LcSm4.decrypt source · line 140 · raw
@k:LcSm4Key -> @b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock