~/bend-docscommunity

camellia.bend checks

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

Camellia (RFC 3713, A Description of the Camellia Encryption Algorithm, 2004): a 128-bit block cipher with 128-, 192- or 256-bit keys, as a cipher for cipher-modes' modes (~LcCamelliaKey, ~LcCamellia.encrypt, ~LcCamellia.decrypt).

The state is the block as a CmBlock: D1 is its first two words, D2 its last two. The key schedule turns the key into a list of steps, each one invertible: the whitening XORs, the Feistel rounds (D2 ^= F(D1, k), then D1 ^= F(D2, k)), the FL / FL^-1 layers, and the final swap of D1 and D2. Encryption runs the steps first to last; decryption undoes them last to first. That is the same function as RFC 3713's decryption with the subkeys reversed, and it makes decrypt(encrypt(b)) == b provable for every key and block: each step is undone whatever F 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 LcCamTabs source · line 67 · raw

Data

The four S-boxes as tables, built once per key (internal)

type LcCamOp source · line 123 · raw

Data

The steps of the cipher (internal)

type LcCamelliaKey source · line 290 · raw

Data

A Camellia key: the steps of the cipher (opaque; built by LcCamellia.key)

Definitions

def cam.sbox1.list source · line 20 · raw

List<&2, U32>

SBOX1 (RFC 3713, 2.4.4); SBOX2, SBOX3 and SBOX4 are derived from it

def cam.rotl8 source · line 39 · raw

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

a byte rotated left by n

def cam.s1.raw source · line 50 · raw

@+x:U32 -> U32

def cam.s1 source · line 53 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def cam.s2 source · line 57 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

SBOX2[x] = SBOX1[x] <<< 1, SBOX3[x] = SBOX1[x] <<< 7, SBOX4[x] = SBOX1[x <<< 1]

def cam.s3 source · line 60 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def cam.s4 source · line 63 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def cam.tables source · line 70 · raw

LcCamTabs

def cam.hi source · line 75 · raw

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

def cam.lo source · line 80 · raw

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

def cam.p source · line 85 · raw

@+t1:U32 -> @+t2:U32 -> @+t3:U32 -> @+t4:U32 -> @+t5:U32 -> @+t6:U32 -> @+t7:U32 -> @+t8:U32 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def cam.f source · line 96 · raw

@t:LcCamTabs -> @x:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> @k:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def cam.fl source · line 104 · raw

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

FL and FL^-1 (RFC 3713, 2.4.2)

def cam.flinv source · line 110 · raw

@y:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> @k:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def cam.white source · line 117 · raw

@a:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

D1 ^= a, D2 ^= b

def cam.op source · line 130 · raw

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

def cam.unop source · line 156 · raw

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

the step undone

def cam.run source · line 181 · raw

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

def cam.unrun source · line 188 · raw

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

def cam.xor128 source · line 197 · raw

@a:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

def cam.rotw source · line 201 · raw

@q:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

q rotated left by 32 bits, and by r bits (r = 0 to 31)

def cam.rotb source · line 206 · raw

@q:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+r:Nat -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

def cam.rotws source · line 212 · raw

@n:Nat -> @q:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

def cam.rol source · line 220 · raw

@q:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+n:Nat -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

q <<< n

def cam.top source · line 223 · raw

@q:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def cam.bot source · line 228 · raw

@q:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def cam.h source · line 234 · raw

@q:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+n:Nat -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

the high and the low 64 bits of q <<< n

def cam.l source · line 237 · raw

@q:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+n:Nat -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def cam.sigma source · line 240 · raw

@n:Nat -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def cam.mix source · line 256 · raw

@+t:LcCamTabs -> @q:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+i:Nat -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

D2 ^= F(D1, Sigma_i); D1 ^= F(D2, Sigma_(i+1)), on the 128-bit value D1:D2

def cam.ka source · line 260 · raw

@+t:LcCamTabs -> @+kl:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @kr:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

KA from KL and KR

def cam.kb source · line 264 · raw

@t:LcCamTabs -> @ka:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @kr:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

KB from KA and KR

def cam.ops128 source · line 268 · raw

@+t:LcCamTabs -> @+kl:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+ka:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> List<&2, LcCamOp>

the steps for a 128-bit key

def cam.ops256 source · line 278 · raw

@+t:LcCamTabs -> @+kl:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+kr:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+ka:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+kb:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> List<&2, LcCamOp>

the steps for a 192- or 256-bit key

def cam.w source · line 294 · raw

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

word i (big-endian) of the key bytes xs

def cam.key128 source · line 298 · raw

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

def cam.key256.go source · line 303 · raw

@+kl:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+kr:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> LcCamelliaKey

def cam.key192 source · line 309 · raw

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

a 192-bit key's KR is its last 64 bits, then their complement

def cam.key256 source · line 314 · raw

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

def cam.key.pick source · line 318 · raw

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

the key for 16, 24 or 32 bytes, if n says so

def LcCamellia.key source · line 332 · raw

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

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

def LcCamellia.key128 source · line 338 · raw

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

a key of exactly this size

def LcCamellia.key192 source · line 342 · raw

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

def LcCamellia.key256 source · line 346 · raw

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

def LcCamellia.encrypt source · line 350 · raw

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

def LcCamellia.decrypt source · line 355 · raw

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

Templates

template cam.gen source · line 43 · raw

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

the entries of a table for x = i, i + 1, .., i + n - 1