serpent.bend checks
raw source on the hub · import bend-osdev-legacy-ciphers@0.1.0.0/serpent.bend as Serpent
Serpent (R. Anderson, E. Biham, L. Knudsen, "Serpent: A Proposal for the Advanced Encryption Standard", AES submission, 1998), in its bitslice description: a 128-bit block cipher with keys of up to 256 bits, as a cipher for cipher-modes' modes (~LcSerpentKey, ~LcSerpent.encrypt, ~LcSerpent.decrypt).
Serpent reads the block as four little-endian words X0..X3 (byte 0 is the low byte of X0, as in the NESSIE vectors and the kernel); a CmBlock holds big-endian words, so the cipher swaps each word's bytes on the way in and out. The key schedule turns the key into a list of steps, each one invertible: the byte swap, the 33 round-key XORs, the 32 S-box layers and the 31 linear transformations. Encryption runs the steps first to last; decryption undoes them last to first (the inverse S-boxes and the inverse linear transformation), as the paper's decryption does, and decrypt(encrypt(b)) == b is provable for every key and block.
The S-box layer works on bits: bit j of X0..X3 is a 4-bit value (X0 its low bit) that the S-box maps, so a proof can undo it one bit position at a time. The rotations of the linear transformation are computed on bits too; the key schedule's arithmetic is native.
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 LcSpBox source · line 28 · raw
Data
LcSp0LcSpBox
LcSp1LcSpBox
LcSp2LcSpBox
LcSp3LcSpBox
LcSp4LcSpBox
LcSp5LcSpBox
LcSp6LcSpBox
LcSp7LcSpBox
type LcSpNib source · line 89 · raw
Data
Four bits: bit j of X0, X1, X2, X3 (X0's is the low bit of the S-box input)
LcSpNib@a:Bool -> @b:Bool -> @c:Bool -> @d:Bool -> LcSpNib
type LcSpQ source · line 123 · raw
@-m:Nat -> Data
Four m-bit words
LcSpQ@-m:Nat -> @a:Word(m) -> @b:Word(m) -> @c:Word(m) -> @d:Word(m) -> LcSpQ<m>
type LcSpOp source · line 238 · raw
Data
The steps of the cipher (internal)
LcSpBswapLcSpOp
LcSpKey@k:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> LcSpOp
LcSpS@b:LcSpBox -> LcSpOp
LcSpLtLcSpOp
type LcSerpentKey source · line 358 · raw
Data
A Serpent key: the steps of the cipher (opaque; built by LcSerpent.key)
LcSerpentKey@ops:List<&2, LcSpOp> -> LcSerpentKey
Definitions
def sp.table source · line 38 · raw
@b:LcSpBox -> List<&2, U32>
def sp.itable source · line 58 · raw
@b:LcSpBox -> List<&2, U32>
the inverse S-boxes
def sp.pick source · line 77 · raw
@inv:Bool -> @b:LcSpBox -> List<&2, U32>
def sp.sbox source · line 85 · raw
@b:LcSpBox -> @inv:Bool -> @v:U32 -> U32
the S-box (inv False) or its inverse (inv True) at v
def sp.na source · line 92 · raw
@n:LcSpNib -> Bool
def sp.nb source · line 97 · raw
@n:LcSpNib -> Bool
def sp.nc source · line 102 · raw
@n:LcSpNib -> Bool
def sp.nd source · line 107 · raw
@n:LcSpNib -> Bool
def sp.bit source · line 112 · raw
@+v:U32 -> @i:Nat -> Bool
def sp.nib source · line 115 · raw
@b:LcSpBox -> @inv:Bool -> @n:LcSpNib -> LcSpNib
def sp.qa source · line 126 · raw
@-m:Nat -> @q:LcSpQ<m> -> Word(m)
def sp.qb source · line 131 · raw
@-m:Nat -> @q:LcSpQ<m> -> Word(m)
def sp.qc source · line 136 · raw
@-m:Nat -> @q:LcSpQ<m> -> Word(m)
def sp.qd source · line 141 · raw
@-m:Nat -> @q:LcSpQ<m> -> Word(m)
def sp.layer source · line 147 · raw
@+b:LcSpBox -> @+inv:Bool -> @m:Nat -> @q:LcSpQ<m> -> LcSpQ<m>
the S-box (or its inverse) at every bit position of four m-bit words
def sp.sop source · line 159 · raw
@+b:LcSpBox -> @+inv:Bool -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
the S-box layer (or its inverse) on the state X0..X3
def sp.lt1 source · line 167 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.lt2 source · line 172 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.lt3 source · line 178 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
X1 = X1 xor X0 xor X2 (its own inverse)
def sp.lt4 source · line 184 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
X3 = X3 xor X2 xor (X0 << 3) (its own inverse)
def sp.lt5 source · line 189 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.lt6 source · line 195 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
X0 = X0 xor X1 xor X3 (its own inverse)
def sp.lt7 source · line 201 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
X2 = X2 xor X3 xor (X1 << 7) (its own inverse)
def sp.lt8 source · line 206 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.ilt1 source · line 211 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.ilt2 source · line 216 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.ilt5 source · line 221 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.ilt8 source · line 226 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.lt source · line 231 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.ilt source · line 234 · raw
@s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.op source · line 244 · raw
@o:LcSpOp -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.unop source · line 258 · raw
@o:LcSpOp -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
the step undone
def sp.run source · line 271 · raw
@os:List<&2, LcSpOp> -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.unrun source · line 278 · raw
@os:List<&2, LcSpOp> -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def sp.box.go source · line 289 · raw
@n:Nat -> LcSpBox
S-box n (n = 0 to 7)
def sp.box source · line 311 · raw
@n:Nat -> LcSpBox
the prekeys w_i = (w_(i-8) xor w_(i-5) xor w_(i-3) xor w_(i-1) xor phi xor i) <<< 11, for n more words from i; win holds w_(i-8) .. w_(i-1) S-box n mod 8
def sp.prekeys source · line 314 · raw
@n:Nat -> @+i:U32 -> @win:List<&2, U32> -> List<&2, U32>
def sp.round_keys source · line 328 · raw
@n:Nat -> @+i:Nat -> @ws:List<&2, U32> -> List<&2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock>
round key K_i = S_((3 - i) mod 8)(w_(4i) .. w_(4i+3)), as an XOR step, for n more round keys from i
def sp.rounds source · line 341 · raw
@ks:List<&2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock> -> @+i:Nat -> List<&2, LcSpOp>
the steps of the 32 rounds from round i: K_i, S_(i mod 8), then LT (rounds 0 to 30) or K_32 (round 31)
def sp.append source · line 350 · raw
@xs:List<&2, LcSpOp> -> @ys:List<&2, LcSpOp> -> List<&2, LcSpOp>
def sp.pad.pick source · line 361 · raw
@short:Bool -> @+xs:List<&2, U32> -> @+n:Nat -> List<&2, U32>
def sp.pad source · line 369 · raw
@+xs:List<&2, U32> -> @+n:Nat -> List<&2, U32>
the key bytes padded to 256 bits: a 1 bit after the key, then zeros
def sp.le source · line 372 · raw
@+xs:List<&2, U32> -> @+i:Nat -> U32
def sp.key source · line 376 · raw
@+xs:List<&2, U32> -> LcSerpentKey
def sp.key.pick source · line 381 · raw
@ok:Bool -> @+xs:List<&2, U32> -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcSerpentKey>
def LcSerpent.key source · line 392 · raw
@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcSerpentKey>
a key of 0 to 32 bytes (16, 24 and 32 are the usual sizes; a shorter key is padded as the specification says)
def LcSerpent.encrypt source · line 396 · raw
@k:LcSerpentKey -> @b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def LcSerpent.decrypt source · line 401 · raw
@k:LcSerpentKey -> @b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock