~/bend-docscommunity

des.bend checks

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

DES (FIPS 46-3, Data Encryption Standard, 1999) as a 64-bit block cipher.

The cipher is IP, sixteen Feistel rounds (L, R) -> (R, L xor f(K_i, R)), the swap of the two halves, and IP^-1. Decryption here undoes each step in reverse order: IP, the swap, the rounds undone last to first ((L', R') -> (R' xor f(K_i, L'), L')), IP^-1. That is the same function as FIPS 46-3's "the same algorithm with the key schedule reversed", written so that decrypt(encrypt(b)) == b is provable for every key and every block (LAWS.bend): a Feistel round is undone whatever its round function.

IP and IP^-1 move bits as lists of Bools, so a proof computes through them. The round function f and the key schedule use native word operations: f looks up eight tables that combine each S-box with the permutation P (SP), built from FIPS 46-3's S-boxes and P once per key (Bend 2.0.37 does not keep a computed table between calls, so the key holds it).

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

Types

type LcDesHalves source · line 138 · raw

Data

The two halves of the Feistel network (internal)

type LcDesSp source · line 220 · raw

Data

the eight SP tables, built once per key (internal)

type LcDesRk source · line 228 · raw

Data

a round key: a 48-bit subkey as its eight 6-bit groups, K's bits 1 to 6 first, and the SP tables (internal)

type LcDesKey source · line 329 · raw

Data

A DES key: its sixteen round keys (opaque; built by LcDes.key)

Definitions

def des.ip.table source · line 23 · raw

List<&2, Nat>

IP

def des.fp.table source · line 30 · raw

List<&2, Nat>

IP^-1

def des.p.table source · line 37 · raw

List<&2, Nat>

P

def des.pc1c.table source · line 42 · raw

List<&2, Nat>

PC-1, the bits of C_0

def des.pc1d.table source · line 47 · raw

List<&2, Nat>

PC-1, the bits of D_0

def des.pc2a.table source · line 52 · raw

List<&2, Nat>

PC-2, its first 24 entries

def des.pc2b.table source · line 57 · raw

List<&2, Nat>

PC-2, its last 24 entries

def des.shifts source · line 62 · raw

List<&2, Nat>

the left shifts of the key schedule

def des.s1 source · line 66 · raw

List<&2, U32>

S1: row r (0 to 3), column c (0 to 15) is entry 16r + c

def des.s2 source · line 73 · raw

List<&2, U32>

S2: row r (0 to 3), column c (0 to 15) is entry 16r + c

def des.s3 source · line 80 · raw

List<&2, U32>

S3: row r (0 to 3), column c (0 to 15) is entry 16r + c

def des.s4 source · line 87 · raw

List<&2, U32>

S4: row r (0 to 3), column c (0 to 15) is entry 16r + c

def des.s5 source · line 94 · raw

List<&2, U32>

S5: row r (0 to 3), column c (0 to 15) is entry 16r + c

def des.s6 source · line 101 · raw

List<&2, U32>

S6: row r (0 to 3), column c (0 to 15) is entry 16r + c

def des.s7 source · line 108 · raw

List<&2, U32>

S7: row r (0 to 3), column c (0 to 15) is entry 16r + c

def des.s8 source · line 115 · raw

List<&2, U32>

S8: row r (0 to 3), column c (0 to 15) is entry 16r + c

def des.bits source · line 124 · raw

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

the bits of a block, bit 1 (the top bit of hi) first

def des.pick source · line 130 · raw

@tab:List<&2, Nat> -> @+bs:List<&2, Bool> -> List<&2, Bool>

the bits a table picks: entry t is bit t of bs

def des.halves source · line 142 · raw

@bs:List<&2, Bool> -> LcDesHalves

64 bits as halves: the first 32 (most significant first) are l

def des.block source · line 146 · raw

@bs:List<&2, Bool> -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def des.hbits source · line 150 · raw

@h:LcDesHalves -> List<&2, Bool>

def des.ip source · line 155 · raw

@b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> LcDesHalves

def des.fp source · line 158 · raw

@h:LcDesHalves -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64

def des.swap source · line 161 · raw

@h:LcDesHalves -> LcDesHalves

def des.perm32 source · line 170 · raw

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

a permutation of a 32-bit word (P): entry t is bit t of x

def des.sbox source · line 178 · raw

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

the S-box entry for 6 bits b1 .. b6: row b1 b6, column b2 b3 b4 b5

def des.sp.go source · line 185 · raw

@n:Nat -> @+s:List<&2, U32> -> @+sh:Nat -> @+x:U32 -> List<&2, U32>

the SP table of S-box s, its output placed sh bits up (28 for S1 .. 0 for S8): entry x is P of S(x) in place

def des.sp source · line 192 · raw

@s:List<&2, U32> -> @sh:Nat -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def des.sp1 source · line 195 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def des.sp2 source · line 198 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def des.sp3 source · line 201 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def des.sp4 source · line 204 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def des.sp5 source · line 207 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def des.sp6 source · line 210 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def des.sp7 source · line 213 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def des.sp8 source · line 216 · raw

0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab

def des.tables source · line 223 · raw

LcDesSp

def des.e source · line 233 · raw

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

group i of E(R): bits 4i .. 4i + 5 of R (bit 0 is bit 32), the top six bits of R rotated left by n = 4i - 1 (mod 32)

def des.f source · line 236 · raw

@k:LcDesRk -> @+r:U32 -> U32

def des.step source · line 247 · raw

@k:LcDesRk -> @h:LcDesHalves -> LcDesHalves

def des.unstep source · line 253 · raw

@k:LcDesRk -> @h:LcDesHalves -> LcDesHalves

the round undone

def des.rounds source · line 259 · raw

@ks:List<&2, LcDesRk> -> @h:LcDesHalves -> LcDesHalves

the rounds of ks, first to last

def des.unrounds source · line 267 · raw

@ks:List<&2, LcDesRk> -> @h:LcDesHalves -> LcDesHalves

the rounds of ks undone, last to first

def des.kbit.pick source · line 276 · raw

@low:Bool -> @+hi:U32 -> @+lo:U32 -> @+t:Nat -> U32

def des.picks64 source · line 284 · raw

@tab:List<&2, Nat> -> @+hi:U32 -> @+lo:U32 -> @acc:U32 -> U32

the bits that tab picks from the 64-bit key hi:lo, appended to acc

def des.cdbit.pick source · line 291 · raw

@low:Bool -> @+c:U32 -> @+d:U32 -> @+t:Nat -> U32

def des.picks56 source · line 299 · raw

@tab:List<&2, Nat> -> @+c:U32 -> @+d:U32 -> @acc:U32 -> U32

the bits that tab picks from the 56 bits C:D (28 each), appended to acc

def des.rot28 source · line 306 · raw

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

def des.group source · line 309 · raw

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

def des.subkey source · line 313 · raw

@sp:LcDesSp -> @+c:U32 -> @+d:U32 -> LcDesRk

K_i = PC-2(C_i D_i), as its eight groups

def des.schedule source · line 319 · raw

@shifts:List<&2, Nat> -> @+sp:LcDesSp -> @+c:U32 -> @+d:U32 -> List<&2, LcDesRk>

K_1 .. K_16 from C_0, D_0

def des.key_of source · line 333 · raw

@+hi:U32 -> @+lo:U32 -> LcDesKey

the key whose 64 bits (parity bits included, and ignored) are hi:lo

def des.key.pick source · line 336 · raw

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

def LcDes.key source · line 346 · raw

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

an 8-byte key (the parity bits, the low bit of each byte, are not checked)

def LcDes.encrypt source · line 350 · raw

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

def LcDes.decrypt source · line 355 · raw

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