cast6.bend checks
raw source on the hub · import bend-osdev-legacy-ciphers@0.1.0.0/cast6.bend as Cast6
CAST-256 (RFC 2612, The CAST-256 Encryption Algorithm, 1999), also called CAST6: a 128-bit block cipher with keys of 128, 160, 192, 224 or 256 bits, as a cipher for cipher-modes' modes (~LcCast6Key, ~LcCast6.encrypt, ~LcCast6.decrypt).
The block is four big-endian words A, B, C, D (a CmBlock). The forward quad-round Q(i) is C ^= f1(D), B ^= f2(C), A ^= f3(B), D ^= f1(A) with key set i's masking and rotation keys, and QBAR(i) is the same four steps in reverse order; encryption is Q(0) .. Q(5), then QBAR(6) .. QBAR(11). f1, f2, f3 are CAST-128's round functions of types 1, 2, 3 over its S1 .. S4 (cast5.bend). The key schedule pads the key with zeros to eight words A .. H and runs 24 forward octaves W(i) on them, with the masks Tm (from 2^30 sqrt 2, stepping by 2^30 sqrt 3) and rotations Tr (from 19, stepping by 17 mod 32); after octaves 2i and 2i + 1, Kr(i) is the low 5 bits of A, C, E, G and Km(i) is H, F, D, B.
Each step XORs a function of one word into another, so it is undone by itself, and QBAR(i) undoes Q(i): decryption (the quad-rounds undone last to first, RFC 2612's decryption) undoes encryption for every key and block, whatever the S-boxes hold.
5 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 import ./cast5.bend as C5
Types
type LcCast6Ks source · line 29 · raw
Data
A quad-round's key set (internal)
LcCast6Ks@m0:U32 -> @m1:U32 -> @m2:U32 -> @m3:U32 -> @r0:U32 -> @r1:U32 -> @r2:U32 -> @r3:U32 -> @t:0x7d16ec09f99b830428c8e2852af315f6/cast5.LcCastTabs -> LcCast6Ks
type LcCast6Op source · line 69 · raw
Data
The steps of the cipher (internal): Q (fwd True) or QBAR with a key set
LcCast6Op@fwd:Bool -> @k:LcCast6Ks -> LcCast6Op
type LcCast6Kst source · line 119 · raw
Data
KAPPA (the eight words A .. H) and the next Tm and Tr
LcCast6Kst@k:List<&2, U32> -> @cm:U32 -> @cr:U32 -> LcCast6Kst
type LcCast6Key source · line 159 · raw
Data
A CAST-256 key: the steps of the cipher (opaque; built by LcCast6.key)
LcCast6Key@ops:List<&2, LcCast6Op> -> LcCast6Key
Definitions
def c6.sc source · line 33 · raw
@k:LcCast6Ks -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
the four steps of Q, each undone by itself
def c6.sb source · line 40 · raw
@k:LcCast6Ks -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def c6.sa source · line 47 · raw
@k:LcCast6Ks -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def c6.sd source · line 54 · raw
@k:LcCast6Ks -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def c6.q source · line 62 · raw
@+k:LcCast6Ks -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
Q and QBAR
def c6.qbar source · line 65 · raw
@+k:LcCast6Ks -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def c6.op source · line 72 · raw
@o:LcCast6Op -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def c6.unop source · line 81 · raw
@o:LcCast6Op -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def c6.run source · line 90 · raw
@os:List<&2, LcCast6Op> -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def c6.unrun source · line 97 · raw
@os:List<&2, LcCast6Op> -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def c6.set source · line 107 · raw
@xs:List<&2, U32> -> @i:Nat -> @v:U32 -> List<&2, U32>
word i of xs replaced by v
def c6.w source · line 123 · raw
@+t:0x7d16ec09f99b830428c8e2852af315f6/cast5.LcCastTabs -> @ty:0x7d16ec09f99b830428c8e2852af315f6/cast5.LcCastTy -> @+dst:Nat -> @src:Nat -> @st:LcCast6Kst -> LcCast6Kst
one step of an octave: word dst ^= f_ty(word src), with the next Tm, Tr
def c6.octave source · line 129 · raw
@+t:0x7d16ec09f99b830428c8e2852af315f6/cast5.LcCastTabs -> @st:LcCast6Kst -> LcCast6Kst
the forward octave W
def c6.ks source · line 140 · raw
@+t:0x7d16ec09f99b830428c8e2852af315f6/cast5.LcCastTabs -> @+k:List<&2, U32> -> LcCast6Ks
the key set read from KAPPA: Km = H, F, D, B and Kr = A, C, E, G (low 5 bits)
def c6.ks.of source · line 144 · raw
@+t:0x7d16ec09f99b830428c8e2852af315f6/cast5.LcCastTabs -> @st:LcCast6Kst -> LcCast6Ks
the key set read from KAPPA after an octave pair
def c6.ops source · line 150 · raw
@n:Nat -> @+i:Nat -> @+t:0x7d16ec09f99b830428c8e2852af315f6/cast5.LcCastTabs -> @st:LcCast6Kst -> List<&2, LcCast6Op>
the steps from quad-round i on: Q up to i = 5, then QBAR
def c6.words source · line 163 · raw
@xs:List<&2, U32> -> List<&2, U32>
the eight key words of a key padded to 32 bytes
def c6.key.pick source · line 170 · raw
@ok:Bool -> @+n:Nat -> @+xs:List<&2, U32> -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcCast6Key>
def LcCast6.key source · line 180 · raw
@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcCast6Key>
a key of 16, 20, 24, 28 or 32 bytes
def LcCast6.encrypt source · line 185 · raw
@k:LcCast6Key -> @b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def LcCast6.decrypt source · line 190 · raw
@k:LcCast6Key -> @b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock