blowfish.bend checks
raw source on the hub · import bend-osdev-legacy-ciphers@0.1.0.0/blowfish.bend as Blowfish
Blowfish (B. Schneier, "Description of a New Variable-Length Key, 64-Bit Block Cipher (Blowfish)", Fast Software Encryption 1993): a 64-bit block cipher with a key of 32 to 448 bits.
The block is two big-endian 32-bit halves xL, xR. Each of the sixteen rounds is xL = xL xor P_i, xR = F(xL) xor xR, then the halves exchanged; after the last round the exchange is undone and xR = xR xor P17, xL = xL xor P18. F(x) = ((S1[a] + S2[b]) xor S3[c]) + S4[d], a .. d the bytes of x, most significant first. The key schedule starts from the digits of pi (P1 .. P18, then the four S-boxes), XORs the key into the P-array, and replaces P1 .. P18 and then every S-box entry, two at a time, with successive encryptions of the zero block.
The key holds the cipher as a list of steps: sixteen rounds (L, R) -> (R xor F(L xor P_i), L xor P_i) and the final (L, R) -> (R xor P18, L xor P17). Decryption undoes them last to first (the specification's decryption, P in reverse order), so decrypt(encrypt(b)) == b is provable 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 LcBfTabs source · line 162 · raw
Data
The four S-boxes (internal)
LcBfTabs@s1:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @s2:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @s3:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @s4:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> LcBfTabs
type LcBfOp source · line 172 · raw
Data
The steps of the cipher (internal)
LcBfRound@p:U32 -> @t:LcBfTabs -> LcBfOp
LcBfFinal@p17:U32 -> @p18:U32 -> LcBfOp
type LcBfSt source · line 296 · raw
Data
The state of the key schedule: the P-array, the S-boxes, and the last block encrypted
LcBfSt@ps:List<&2, U32> -> @t:LcBfTabs -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> LcBfSt
type LcBlowfishKey source · line 336 · raw
Data
A Blowfish key: the steps of the cipher (opaque; built by LcBlowfish.key)
LcBlowfishKey@ops:List<&2, LcBfOp> -> LcBlowfishKey
Definitions
def bf.pi.list source · line 27 · raw
List<&2, U32>
BEGIN GENERATED by gen/tables.py: tables the first 1042 words of the fractional part of pi: P1 .. P18, then S1, S2, S3, S4 (256 words each)
def bf.f source · line 166 · raw
@t:LcBfTabs -> @+x:U32 -> U32
F
def bf.op source · line 176 · raw
@o:LcBfOp -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64
def bf.unop source · line 188 · raw
@o:LcBfOp -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64
def bf.run source · line 199 · raw
@os:List<&2, LcBfOp> -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64
def bf.unrun source · line 206 · raw
@os:List<&2, LcBfOp> -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64
def bf.ops source · line 216 · raw
@n:Nat -> @ps:List<&2, U32> -> @+t:LcBfTabs -> List<&2, LcBfOp>
the steps from a P-array of 18 words (n = 16 rounds, then the final)
def bf.slice source · line 232 · raw
@xs:List<&2, U32> -> @i:Nat -> @n:Nat -> List<&2, U32>
n words of xs from offset i
def bf.cycle source · line 237 · raw
@n:Nat -> @ys:List<&2, U32> -> @+xs:List<&2, U32> -> List<&2, U32>
the key bytes repeated cyclically: n bytes, from the rest of the key ys (the whole key xs when ys runs out)
def bf.pkey source · line 253 · raw
@ps:List<&2, U32> -> @ks:List<&2, U32> -> List<&2, U32>
P xored with the key bytes as big-endian words
def bf.set_p source · line 261 · raw
@i:Nat -> @ps:List<&2, U32> -> @+h:U32 -> @+l:U32 -> List<&2, U32>
P entries i and i + 1 replaced by h and l
def bf.set_s source · line 277 · raw
@t:LcBfTabs -> @k:Nat -> @+j:U32 -> @v:U32 -> LcBfTabs
entry j of the S-box k (0 to 3) set to v
def bf.fill_p.go source · line 299 · raw
@n:Nat -> @+i:Nat -> @+ps:List<&2, U32> -> @+t:LcBfTabs -> @c:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> LcBfSt
def bf.fill_p source · line 312 · raw
@s:LcBfSt -> LcBfSt
P1 .. P18 replaced, two at a time, by successive encryptions (nine)
def bf.fill_s.go source · line 317 · raw
@n:Nat -> @+k:Nat -> @+j:U32 -> @+ps:List<&2, U32> -> @+t:LcBfTabs -> @c:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> LcBfSt
def bf.fill_s source · line 330 · raw
@+k:Nat -> @s:LcBfSt -> LcBfSt
the S-box k's 256 entries replaced, two at a time (128 encryptions)
def bf.tables source · line 340 · raw
@+pi:List<&2, U32> -> LcBfTabs
the S-boxes before the key: pi's words 18 to 1041
def bf.key.of source · line 343 · raw
@s:LcBfSt -> LcBlowfishKey
def bf.key source · line 348 · raw
@+xs:List<&2, U32> -> LcBlowfishKey
def bf.key.pick source · line 354 · raw
@ok:Bool -> @+xs:List<&2, U32> -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcBlowfishKey>
def LcBlowfish.key source · line 364 · raw
@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcBlowfishKey>
a key of 4 to 56 bytes (32 to 448 bits, as the specification says)
def LcBlowfish.key_any source · line 371 · raw
@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcBlowfishKey>
a key of 1 to 72 bytes: the key schedule takes up to 72 (18 words of P), and some test sets use keys shorter than 4 bytes
def LcBlowfish.encrypt source · line 376 · raw
@k:LcBlowfishKey -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64
def LcBlowfish.decrypt source · line 381 · raw
@k:LcBlowfishKey -> @b:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64