~/bend-docscommunity

cast5.bend checks

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

CAST-128 (RFC 2144, The CAST-128 Encryption Algorithm, 1997), also called CAST5: a 64-bit block cipher with keys of 40 to 128 bits in 8-bit steps. This file also holds the S-boxes S1 .. S4 and round functions that CAST-256 (cast6.bend) shares.

The block is two big-endian halves L, R; each round is the Feistel round (L, R) -> (R, L xor f_i(R)) and the output is (R16, L16). Rounds 1, 4, 7, 10, 13, 16 use f type 1, rounds 2, 5, ... type 2, rounds 3, 6, ... type 3: type 1: I = (Km + D) <<< Kr, f = ((S1[Ia] xor S2[Ib]) - S3[Ic]) + S4[Id] type 2: I = (Km xor D) <<< Kr, f = ((S1[Ia] - S2[Ib]) + S3[Ic]) xor S4[Id] type 3: I = (Km - D) <<< Kr, f = ((S1[Ia] + S2[Ib]) xor S3[Ic]) - S4[Id] (Ia the most significant byte of I). Keys of up to 80 bits use 12 rounds, longer ones 16 (2.5); a shorter key is zero-padded to 128 bits. The key schedule (2.4) computes K1 .. K32 with S5 .. S8; round i uses Km_i = K_i and Kr_i = the low 5 bits of K_(16+i).

The rounds are legacy-ciphers.bend's Feistel rounds over f, followed by the exchange of the halves, so decryption (the exchange, then the rounds undone last to first: RFC 2144's decryption) undoes encryption for every key and block, whatever the S-boxes hold.

3 imports
import Base
import bend-osdev-bytes@0.1.0.0/bytes.bend as B
import ./legacy_ciphers.bend as L

Types

type LcCastTabs source · line 308 · raw

Data

S1 .. S4, the round functions' S-boxes (internal; CAST-256 uses them too)

type LcCastTy source · line 315 · raw

Data

The round function's type (internal)

type LcCast5Rk source · line 351 · raw

Data

A round key (internal)

type LcCastKTabs source · line 362 · raw

Data

S5 .. S8 (internal)

type LcCastHalf source · line 430 · raw

Data

sixteen subkeys from x, and the x after them

type LcCast5Key source · line 492 · raw

Data

A CAST-128 key: the round keys (opaque; built by LcCast5.key)

Definitions

def cast.s1.list source · line 27 · raw

List<&2, U32>

BEGIN GENERATED by gen/tables.py: tables S1 (RFC 2144, Appendix A)

def cast.s2.list source · line 62 · raw

List<&2, U32>

S2 (RFC 2144, Appendix A)

def cast.s3.list source · line 97 · raw

List<&2, U32>

S3 (RFC 2144, Appendix A)

def cast.s4.list source · line 132 · raw

List<&2, U32>

S4 (RFC 2144, Appendix A)

def cast.s5.list source · line 167 · raw

List<&2, U32>

S5 (RFC 2144, Appendix A)

def cast.s6.list source · line 202 · raw

List<&2, U32>

S6 (RFC 2144, Appendix A)

def cast.s7.list source · line 237 · raw

List<&2, U32>

S7 (RFC 2144, Appendix A)

def cast.s8.list source · line 272 · raw

List<&2, U32>

S8 (RFC 2144, Appendix A)

def cast.tables source · line 311 · raw

LcCastTabs

def cast.lookup source · line 320 · raw

@+t:LcCastTabs -> @+i:U32 -> @n:Nat -> U32

def cast.f source · line 338 · raw

@ty:LcCastTy -> @+t:LcCastTabs -> @d:U32 -> @km:U32 -> @+kr:U32 -> U32

f of type ty on data d, with masking key km and rotation kr (0 to 31)

def cast5.f source · line 354 · raw

@k:LcCast5Rk -> @d:U32 -> U32

def cast.ktables source · line 365 · raw

LcCastKTabs

def cast.ks source · line 369 · raw

@+t:LcCastKTabs -> @j:Nat -> @+a:List<&2, U32> -> @+i:Nat -> U32

S_j[byte i of a], j = 5 to 8

def cast.w source · line 383 · raw

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

the big-endian word of bytes i .. i + 3 of a

def cast.bytes source · line 387 · raw

@+w:U32 -> @rest:List<&2, U32> -> List<&2, U32>

a word's four bytes, most significant first, in front of rest

def cast.mix source · line 393 · raw

@+t:LcCastKTabs -> @+w:U32 -> @+cur:List<&2, U32> -> @ia:Nat -> @ib:Nat -> @ic:Nat -> @id:Nat -> @+src:List<&2, U32> -> @j:Nat -> @ie:Nat -> U32

a word of z (or x) from a word of x (or z): w ^ S5[a] ^ S6[b] ^ S7[c] ^ S8[d] ^ Sj[e], bytes a .. d of the half being built so far (cur) and e of the other (src)

def cast.z source · line 398 · raw

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

z from x (the four lines z0z1z2z3 = ... of 2.4.1); the bytes of z built so far are the cur argument of the later words

def cast.x source · line 405 · raw

@+t:LcCastKTabs -> @+z:List<&2, U32> -> List<&2, U32>

x from z (the four lines x0x1x2x3 = ...)

def cast.sub source · line 412 · raw

@+t:LcCastKTabs -> @+v:List<&2, U32> -> @ia:Nat -> @ib:Nat -> @ic:Nat -> @id:Nat -> @j:Nat -> @ie:Nat -> U32

a subkey: S5[a] ^ S6[b] ^ S7[c] ^ S8[d] ^ Sj[e] on the bytes of v

def cast.set1 source · line 417 · raw

@+t:LcCastKTabs -> @+z:List<&2, U32> -> List<&2, U32>

the four subkey sets of 2.4.1 (K1 .. K4, K5 .. K8, K9 .. K12, K13 .. K16; then again for K17 .. K32)

def cast.set2 source · line 420 · raw

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

def cast.set3 source · line 423 · raw

@+t:LcCastKTabs -> @+z:List<&2, U32> -> List<&2, U32>

def cast.set4 source · line 426 · raw

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

def cast.half source · line 433 · raw

@+t:LcCastKTabs -> @+x:List<&2, U32> -> LcCastHalf

def cast.subkeys.rest source · line 440 · raw

@+t:LcCastKTabs -> @h:LcCastHalf -> List<&2, U32>

def cast.subkeys.of source · line 445 · raw

@+t:LcCastKTabs -> @h:LcCastHalf -> List<&2, U32>

def cast.subkeys source · line 451 · raw

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

K1 .. K32 of a 16-byte (padded) key

def cast5.ty.of source · line 455 · raw

@m:Nat -> LcCastTy

def cast5.ty source · line 465 · raw

@i:Nat -> LcCastTy

the round type of round i (0-based): 1, 2, 3, 1, 2, 3, ...

def cast5.rounds.pick source · line 468 · raw

@short:Bool -> Nat

def cast5.rounds source · line 476 · raw

@n:Nat -> Nat

the number of rounds for a key of n bytes: 12 up to 80 bits, else 16

def cast5.rks source · line 480 · raw

@n:Nat -> @+i:Nat -> @+t:LcCastTabs -> @kms:List<&2, U32> -> @krs:List<&2, U32> -> List<&2, LcCast5Rk>

the round keys of n rounds from round i, Km from kms and Kr from krs

def cast5.key source · line 495 · raw

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

def cast5.key.pick source · line 499 · raw

@ok:Bool -> @+n:Nat -> @+xs:List<&2, U32> -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcCast5Key>

def LcCast5.key source · line 509 · raw

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

a key of 5 to 16 bytes (40 to 128 bits); up to 10 bytes, 12 rounds

def LcCast5.encrypt source · line 514 · raw

@k:LcCast5Key -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def LcCast5.decrypt source · line 519 · raw

@k:LcCast5Key -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64