~/bend-docscommunity

twofish.bend checks

raw source on the hub · import bend-osdev-legacy-ciphers@0.1.0.0/twofish.bend as Twofish

Twofish (B. Schneier, J. Kelsey, D. Whiting, D. Wagner, C. Hall, N. Ferguson, "Twofish: A 128-Bit Block Cipher", AES submission, 1998): a 128-bit block cipher with 128-, 192- or 256-bit keys, as a cipher for cipher-modes' modes (~LcTwofishKey, ~LcTwofish.encrypt, ~LcTwofish.decrypt).

Twofish reads the block as four little-endian words; 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 whitening XORs, the sixteen rounds (R0, R1, R2, R3) -> ((R2 xor F0) >>> 1, (R3 <<< 1) xor F1, R0, R1) with (F0, F1) = F(R0, R1, r), and the swap that undoes the last round's exchange of halves. Encryption runs the steps first to last; decryption undoes them last to first, the same function as the paper's decryption, and decrypt(encrypt(b)) == b is provable for every key and block: a round is undone whatever F is. The byte swaps and the 1-bit rotations a round undoes are computed on bits (legacy-ciphers.bend), so a proof computes through them; F (the key-dependent S-boxes, the MDS matrix, the PHT) and the key schedule use native word operations.

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 LcTfTabs source · line 72 · raw

Data

The tables, built once per key (internal): q0, q1 and the MDS columns

type LcTfOp source · line 180 · raw

Data

The steps of the cipher (internal)

type LcTwofishKey source · line 318 · raw

Data

A Twofish key: the steps of the cipher (opaque; built by LcTwofish.key)

Definitions

def tf.q0.t source · line 26 · raw

List<&2, U32>

def tf.q1.t source · line 32 · raw

List<&2, U32>

def tf.t source · line 39 · raw

@+ts:List<&2, U32> -> @i:Nat -> @+x:U32 -> U32

entry x of table i (0 to 3) of ts

def tf.ror4 source · line 43 · raw

@+x:U32 -> U32

a 4-bit value rotated right by one bit

def tf.q.half source · line 46 · raw

@+a:U32 -> @+b:U32 -> U32

def tf.q.go source · line 49 · raw

@+ts:List<&2, U32> -> @+a2:U32 -> @+b2:U32 -> U32

def tf.q.raw source · line 53 · raw

@+ts:List<&2, U32> -> @+x:U32 -> U32

q(x) from its tables

def tf.q0 source · line 65 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def tf.q1 source · line 68 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def tf.q source · line 75 · raw

@t:LcTfTabs -> @one:Bool -> @+x:U32 -> U32

def tf.xt source · line 86 · raw

@+a:U32 -> @+poly:U32 -> U32

def tf.gmul.go source · line 89 · raw

@n:Nat -> @+a:U32 -> @+b:U32 -> @+poly:U32 -> @p:U32 -> U32

def tf.gmul source · line 96 · raw

@+a:U32 -> @+b:U32 -> @+poly:U32 -> U32

def tf.mds.m source · line 103 · raw

List<&2, U32>

def tf.mds.col source · line 109 · raw

@+j:Nat -> @+x:U32 -> U32

def tf.mds0 source · line 113 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def tf.mds1 source · line 116 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def tf.mds2 source · line 119 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def tf.mds3 source · line 122 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def tf.tables source · line 125 · raw

LcTfTabs

def tf.mds source · line 128 · raw

@t:LcTfTabs -> @+y:U32 -> U32

def tf.stage source · line 136 · raw

@+t:LcTfTabs -> @p0:Bool -> @p1:Bool -> @p2:Bool -> @p3:Bool -> @+x:U32 -> @+l:U32 -> U32

byte i of x through q (q1 if p_i), then XOR byte i of l

def tf.h2 source · line 140 · raw

@+t:LcTfTabs -> @+x:U32 -> @+l0:U32 -> @+l1:U32 -> U32

the stages every key length shares: with L_1, with L_0, then q alone

def tf.h3 source · line 143 · raw

@+t:LcTfTabs -> @+x:U32 -> @+l0:U32 -> @+l1:U32 -> @+l2:U32 -> U32

def tf.h4 source · line 146 · raw

@+t:LcTfTabs -> @+x:U32 -> @+l0:U32 -> @+l1:U32 -> @+l2:U32 -> @+l3:U32 -> U32

def tf.h source · line 150 · raw

@+t:LcTfTabs -> @+x:U32 -> @l:List<&2, U32> -> U32

h(X, L) for L = L_0 .. L_(k-1), k = 2, 3 or 4

def tf.f source · line 164 · raw

@+t:LcTfTabs -> @+s:List<&2, U32> -> @r0:U32 -> @r1:U32 -> @ka:U32 -> @kb:U32 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def tf.f0 source · line 169 · raw

@x:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> U32

def tf.f1 source · line 174 · raw

@x:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> U32

def tf.op source · line 186 · raw

@o:LcTfOp -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

def tf.unop source · line 205 · raw

@o:LcTfOp -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

the step undone

def tf.run source · line 223 · raw

@os:List<&2, LcTfOp> -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

def tf.unrun source · line 230 · raw

@os:List<&2, LcTfOp> -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

def tf.words source · line 240 · raw

@n:Nat -> @+xs:List<&2, U32> -> @+i:Nat -> List<&2, U32>

the little-endian words of the key bytes xs, every other one from word i

def tf.rs source · line 249 · raw

List<&2, U32>

The RS matrix (section 4.3), over GF(2^8) modulo x^8 + x^6 + x^3 + x^2 + 1

def tf.rs.row source · line 256 · raw

@n:Nat -> @+xs:List<&2, U32> -> @+r:Nat -> @+j:Nat -> @+c:Nat -> @acc:U32 -> U32

row r of RS times the 8 key bytes from byte j

def tf.s_word source · line 264 · raw

@+xs:List<&2, U32> -> @+i:Nat -> U32

S_i: RS times key bytes 8i .. 8i + 7

def tf.svec source · line 269 · raw

@n:Nat -> @+xs:List<&2, U32> -> @+i:Nat -> @acc:List<&2, U32> -> List<&2, U32>

S = (S_(k-1), .., S_0), the k words of the key-dependent S-boxes

def tf.kpair source · line 277 · raw

@+t:LcTfTabs -> @+me:List<&2, U32> -> @+mo:List<&2, U32> -> @+i:U32 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

K_(2i) and K_(2i+1) as an LcBlock64

def tf.rounds source · line 283 · raw

@ks:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> @+t:LcTfTabs -> @+s:List<&2, U32> -> List<&2, LcTfOp>

the rounds' steps from round r, with the pairs K_(2r+8), K_(2r+9) in ks

def tf.kpairs source · line 290 · raw

@n:Nat -> @+t:LcTfTabs -> @+me:List<&2, U32> -> @+mo:List<&2, U32> -> @+i:U32 -> List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

def tf.white.le source · line 297 · raw

@a:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> LcTfOp

def tf.append source · line 302 · raw

@xs:List<&2, LcTfOp> -> @ys:List<&2, LcTfOp> -> List<&2, LcTfOp>

def tf.ops source · line 310 · raw

@ks:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> @+t:LcTfTabs -> @+s:List<&2, U32> -> List<&2, LcTfOp>

the steps from the subkey pairs (K_0, K_1) .. (K_38, K_39)

def tf.key source · line 322 · raw

@+k:Nat -> @+xs:List<&2, U32> -> LcTwofishKey

the key from k = 2, 3 or 4 64-bit words of key bytes xs

def tf.key.pick source · line 326 · raw

@n16:Bool -> @n24:Bool -> @n32:Bool -> @+xs:List<&2, U32> -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcTwofishKey>

def LcTwofish.key source · line 340 · raw

@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcTwofishKey>

a 16-, 24- or 32-byte key (Twofish-128, -192, -256, by its length)

def tf.pad.pick source · line 347 · raw

@n16:Bool -> @n24:Bool -> @n32:Bool -> @+n:Nat -> @+xs:List<&2, U32> -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcTwofishKey>

the key padded with zero bytes to 16, 24 or 32 bytes (section 4.3: keys shorter than 256 bits are padded with zeros to the next defined length)

def LcTwofish.key_padded source · line 359 · raw

@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcTwofishKey>

a key of 1 to 32 bytes, zero-padded as the specification says

def LcTwofish.encrypt source · line 364 · raw

@k:LcTwofishKey -> @b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

def LcTwofish.decrypt source · line 369 · raw

@k:LcTwofishKey -> @b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

Templates

template tf.gen source · line 58 · raw

@-f:(@_:U32 -> U32) -> @n:Nat -> @+i:U32 -> List<&2, U32>