~/bend-docscommunity

legacy_ciphers.bend checks

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

bend-osdev-legacy-ciphers: the older block and stream ciphers a kernel's crypto layer still carries (DES, 3DES, SM4, Camellia, Serpent, Twofish, ARIA, CAST-256, Blowfish, CAST-128, TEA, XTEA, FCrypt, RC4), one file per cipher. This file holds what they share: the package's error type, the 64-bit block, word helpers, tables, and Feistel rounds over any round function.

The 128-bit block ciphers (sm4.bend, camellia.bend, serpent.bend, twofish.bend, aria.bend, cast6.bend) work on cipher-modes' CmBlock, so they plug into every mode there as the template arguments ~K, ~enc, ~dec. The 64-bit ones (des.bend, tdes.bend, blowfish.bend, cast5.bend, tea.bend, xtea.bend, fcrypt.bend) work on LcBlock64 below, and the 64-bit modes in modes64.bend.

3 imports
import Base
import bend-osdev-bytes@0.1.0.0/bytes.bend as B
import bend-osdev-word64@0.1.0.0/word64.bend as W

Types

type LcError source · line 19 · raw

Data

Every failure the package reports

type LcBlock64 source · line 28 · raw

Data

A 64-bit block: two 32-bit words, big-endian (hi holds bytes 0 to 3, byte 0 in its top 8 bits). Every value of the type is a block, so "decryption inverts encryption" can be stated for every block. The type is open: a cipher may build and match blocks directly.

type LcTab source · line 244 · raw

Data

Definitions

def LcBlock64.zero source · line 31 · raw

LcBlock64

def LcBlock64.xor source · line 34 · raw

@a:LcBlock64 -> @b:LcBlock64 -> LcBlock64

def lc.rotl source · line 42 · raw

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

def lc.byte source · line 46 · raw

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

byte n of x, counting from the least significant (n = 0 to 3)

def lc.be source · line 50 · raw

@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> U32

the word with bytes a (most significant), b, c, d

def lc.le source · line 54 · raw

@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> U32

the word with bytes a (least significant), b, c, d

def lc.bit source · line 58 · raw

@x:U32 -> @i:Nat -> U32

bit i of x (0 the least significant), as 0 or 1

def lc.at source · line 62 · raw

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

element n of a list of words (0 past the end)

def lc.wrotl source · line 79 · raw

@+m:Nat -> @+n:Nat -> @+w:Word(Nat.add(m, n)) -> Word(Nat.add(n, m))

the (m + n)-bit word w rotated left by n

def lc.rotl1.bits source · line 82 · raw

@x:U32 -> U32

def lc.rotl3.bits source · line 87 · raw

@x:U32 -> U32

def lc.rotl5.bits source · line 92 · raw

@x:U32 -> U32

def lc.rotl7.bits source · line 97 · raw

@x:U32 -> U32

def lc.rotl10.bits source · line 102 · raw

@x:U32 -> U32

def lc.rotl13.bits source · line 107 · raw

@x:U32 -> U32

def lc.rotl19.bits source · line 112 · raw

@x:U32 -> U32

def lc.rotl22.bits source · line 117 · raw

@x:U32 -> U32

def lc.rotl25.bits source · line 122 · raw

@x:U32 -> U32

def lc.rotl27.bits source · line 127 · raw

@x:U32 -> U32

def lc.rotl29.bits source · line 132 · raw

@x:U32 -> U32

def lc.rotl31.bits source · line 137 · raw

@x:U32 -> U32

def lc.bits8 source · line 143 · raw

@b:U32 -> Word(8n)

the low 8 bits of a word, as bits, and the byte with these bits

def lc.of8 source · line 148 · raw

@s:Word(8n) -> U32

def lc.be.bits source · line 152 · raw

@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> U32

the word with bytes a (most significant), b, c, d, by bits

def lc.b0.bits source · line 156 · raw

@w:U32 -> U32

the bytes of w, by bits: b0 the most significant

def lc.b1.bits source · line 161 · raw

@w:U32 -> U32

def lc.b2.bits source · line 166 · raw

@w:U32 -> U32

def lc.b3.bits source · line 171 · raw

@w:U32 -> U32

def lc.bswap.bits source · line 177 · raw

@+w:U32 -> U32

the word with its bytes in reverse order (big-endian to little-endian)

def lc.msb.go source · line 181 · raw

@n:Nat -> @w:Word(n) -> @acc:List<&2, Bool> -> List<&2, Bool>

the bits of a word, most significant first, in front of acc

def lc.msb source · line 190 · raw

@x:U32 -> @acc:List<&2, Bool> -> List<&2, Bool>

def lc.rev_bits source · line 196 · raw

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

bs reversed, in front of acc

def lc.drop_bits source · line 204 · raw

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

all but the first n of bs

def lc.word_lsb source · line 217 · raw

@n:Nat -> @bs:List<&2, Bool> -> Word(n)

the n-bit word whose bits, least significant first, are the first n of bs (False past the end)

def lc.nth source · line 229 · raw

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

the element i of bs (False past the end)

def lc.tab.leaves source · line 248 · raw

@xs:List<&2, U32> -> List<&2, LcTab>

def lc.tab.pairs source · line 255 · raw

@ts:List<&2, LcTab> -> List<&2, LcTab>

def lc.tab.up source · line 262 · raw

@n:Nat -> @ts:List<&2, LcTab> -> List<&2, LcTab>

def lc.tab.root source · line 269 · raw

@ts:List<&2, LcTab> -> LcTab

def lc.tab source · line 277 · raw

@d:Nat -> @xs:List<&2, U32> -> LcTab

the table of the 2^d words xs (entry i is element i)

def lc.tab.pick source · line 280 · raw

@one:Bool -> @l:LcTab -> @r:LcTab -> LcTab

def lc.tab.get source · line 288 · raw

@d:Nat -> @t:LcTab -> @+i:U32 -> U32

entry i of a table of depth d (i below 2^d)

def lc.tab.path source · line 304 · raw

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

the bits of i (below 2^d) that pick its entry, most significant first

def lc.tab.set.go source · line 311 · raw

@t:LcTab -> @path:List<&2, Bool> -> @v:U32 -> LcTab

def lc.tab.set source · line 327 · raw

@d:Nat -> @t:LcTab -> @+i:U32 -> @v:U32 -> LcTab

entry i of a table of depth d set to v (i below 2^d)

def lc.swap source · line 360 · raw

@b:LcBlock64 -> LcBlock64

the two halves exchanged

def lc.block_bytes source · line 368 · raw

@b:LcBlock64 -> @rest:List<&2, U32> -> List<&2, U32>

the 8 bytes of a block in front of rest

def lc.block8 source · line 373 · raw

@a0:U32 -> @a1:U32 -> @a2:U32 -> @a3:U32 -> @a4:U32 -> @a5:U32 -> @a6:U32 -> @a7:U32 -> LcBlock64

def LcBlock64.to_octets source · line 376 · raw

@b:LcBlock64 -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets

def lc.block_of source · line 379 · raw

@xs:List<&2, U32> -> Maybe<&2, LcBlock64>

def LcBlock64.of_octets source · line 387 · raw

@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, LcBlock64>

an 8-byte string as a block (big-endian words); None for any other length

def LcBlock64.hex source · line 390 · raw

@b:LcBlock64 -> String

Templates

template lc.fe.step source · line 335 · raw

@-RK:Data -> @-f:(@_:RK -> @_:U32 -> U32) -> @k:RK -> @b:LcBlock64 -> LcBlock64

template lc.fe.unstep source · line 340 · raw

@-RK:Data -> @-f:(@_:RK -> @_:U32 -> U32) -> @k:RK -> @b:LcBlock64 -> LcBlock64

template lc.fe.run source · line 345 · raw

@-RK:Data -> @-f:(@_:RK -> @_:U32 -> U32) -> @ks:List<&2, RK> -> @b:LcBlock64 -> LcBlock64

template lc.fe.unrun source · line 352 · raw

@-RK:Data -> @-f:(@_:RK -> @_:U32 -> U32) -> @ks:List<&2, RK> -> @b:LcBlock64 -> LcBlock64