~/bend-docscommunity

tea.bend checks

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

TEA (D. Wheeler, R. Needham, "TEA, a Tiny Encryption Algorithm", Fast Software Encryption 1994): a 64-bit block cipher with a 128-bit key. This file also holds the steps TEA and XTEA (xtea.bend) share.

The block is two 32-bit words y, z and the key four words k0 .. k3. Each cycle adds delta (0x9E3779B9) to sum, then y += ((z << 4) + k0) xor (z + sum) xor ((z >> 5) + k1) z += ((y << 4) + k2) xor (y + sum) xor ((y >> 5) + k3) The key schedule turns the key into a list of such steps (each adds a function of one half to the other); decryption undoes them last to first, each by subtracting what it added: the same function as the paper's decode routine, and decrypt(encrypt(b)) == b is provable for every key and block (an addition is undone by a subtraction, for every word).

The paper fixes 32-bit words and leaves their byte order open ("same endian coding or decoding"). The order is a parameter: little-endian words (the Linux kernel's "tea") are LcTea.key, big-endian ones LcTea.key_be; the number of cycles (32 recommended) is a parameter of LcTea.key_with. A little-endian key reverses the bytes of each block word on the way in and out (a step undone by itself).

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

Types

type LcTeaOp source · line 26 · raw

Data

The steps (internal), on the block as LcBlock64{y, z}

type LcTeaKey source · line 141 · raw

Data

A TEA key: the steps of the cipher (opaque; built by LcTea.key)

Definitions

def tea.g source · line 34 · raw

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

TEA's function of one half: ((x << 4) + a) xor (x + s) xor ((x >> 5) + b)

def xtea.g source · line 38 · raw

@+x:U32 -> @c:U32 -> U32

XTEA's: (((x << 4) xor (x >> 5)) + x) xor c

def tea.op source · line 41 · raw

@o:LcTeaOp -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def tea.unop source · line 65 · raw

@o:LcTeaOp -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

the step undone

def tea.run source · line 88 · raw

@os:List<&2, LcTeaOp> -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def tea.unrun source · line 95 · raw

@os:List<&2, LcTeaOp> -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def tea.delta source · line 104 · raw

U32

def tea.word source · line 108 · raw

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

the four key words, little-endian (le True) or big-endian

def tea.append source · line 115 · raw

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

def tea.order source · line 124 · raw

@le:Bool -> @os:List<&2, LcTeaOp> -> List<&2, LcTeaOp>

the steps in the chosen byte order: little-endian words are byte-swapped on the way in and out

def tea.cycles source · line 132 · raw

@n:Nat -> @+s:U32 -> @+k0:U32 -> @+k1:U32 -> @+k2:U32 -> @+k3:U32 -> List<&2, LcTeaOp>

TEA's steps for n more cycles, sum the value before the next

def tea.key.pick source · line 144 · raw

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

def LcTea.key_with source · line 155 · raw

@le:Bool -> @n:Nat -> @o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcTeaKey>

a 16-byte key, words in the order le picks (True: little-endian), and n cycles (32 recommended)

def LcTea.key source · line 161 · raw

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

the named default: little-endian words (the Linux kernel's "tea"), 32 cycles

def LcTea.key_be source · line 165 · raw

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

big-endian words, 32 cycles

def LcTea.encrypt source · line 168 · raw

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

def LcTea.decrypt source · line 173 · raw

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