# Blowfish: six of Schneier's vectors (keys of 8, 1, 3 and 24 bytes), both # ways (the ciphertext, then the plaintext decrypted back), and Blowfish # through modes64.bend's CBC over 16 KB both ways. import Base import bend-osdev-bytes@0.1.1.0/bytes.bend as B import bend-osdev-cipher-core@0.1.0.0/cipher_core.bend as L import bend-osdev-cipher-core@0.1.0.0/modes64.bend as X import ./blowfish.bend as BF def err(e: L.LcError) -> String: match e: case L.LcBadKeyLength{n}: "bad key " ++ Nat.show(n) case L.LcBadIvLength{n}: "bad iv " ++ Nat.show(n) case L.LcNotBlockMultiple{n}: "not whole blocks " ++ Nat.show(n) def b64.of(m: Maybe<&2, L.LcBlock64>) -> L.LcBlock64: match m: case None{}: L.LcBlock64.zero() case Some{b}: b def b64(o: B.Octets) -> L.LcBlock64: b64.of(L.LcBlock64.of_octets(o)) def key(~K: Data, r: Result<&2, &2, L.LcError, K>, d: K) -> K: match r: case Fail{e}: d case Done{k}: k def both64(~K: Data, ~enc: K -> L.LcBlock64 -> L.LcBlock64, ~dec: K -> L.LcBlock64 -> L.LcBlock64, +k: K, b: L.LcBlock64) -> String: +c = enc(k, b) L.LcBlock64.hex(c) ++ " " ++ L.LcBlock64.hex(dec(k, c)) def blowfish_key(k: B.Octets) -> BF.LcBlowfishKey: key(~BF.LcBlowfishKey, BF.LcBlowfish.key_any(k), BF.LcBlowfishKey{Nil{}}) def blowfish(k: B.Octets, b: B.Octets) -> String: both64(~BF.LcBlowfishKey, ~BF.LcBlowfish.encrypt, ~BF.LcBlowfish.decrypt, blowfish_key(k), b64(b)) # n bytes, byte i = 7i + 3 (mod 256) def big.go(n: Nat, +i: U32, acc: List<&2, U32>) -> List<&2, U32>: match n: case 0n: B.rev(acc) case 1n+p: big.go(p, U32.add(i, 1), Con{U32.and(255, U32.add(U32.mul(i, 7), 3)), acc}) def big(n: Nat) -> B.Octets: B.Octets.of_list(big.go(n, 0, Nil{})) def verdict(b: Bool) -> String: match b: case True{}: "ok" case False{}: "differs" def last8(r: Result<&2, &2, L.LcError, B.Octets>) -> String: match r: case Fail{e}: err(e) case Done{+o}: B.Octets.hex(B.Octets.drop(Nat.sub(B.Octets.len(o), 8n), o)) def same64(r: Result<&2, &2, L.LcError, B.Octets>, +m: B.Octets) -> String: match r: case Fail{e}: err(e) case Done{o}: verdict(B.Octets.is_eq(o, m)) def cbc64(~K: Data, ~enc: K -> L.LcBlock64 -> L.LcBlock64, ~dec: K -> L.LcBlock64 -> L.LcBlock64, +k: K, n: Nat) -> String: +m = big(n) +c = X.Lc64.cbc_encrypt(~K, ~enc, k, B.Octets.zeros(8n), m) last8(c) ++ " " ++ same64(Result.bind(&2, &2, L.LcError, B.Octets, B.Octets, c, x => X.Lc64.cbc_decrypt(~K, ~dec, k, B.Octets.zeros(8n), x)), m) def blowfish_cbc(k: B.Octets, n: Nat) -> String: cbc64(~BF.LcBlowfishKey, ~BF.LcBlowfish.encrypt, ~BF.LcBlowfish.decrypt, blowfish_key(k), n) # BEGIN GENERATED by gen/vectors.py: check def main() -> IO(Unit): do IO: IO.print(blowfish(B.Octets.of_list([0, 0, 0, 0, 0, 0, 0, 0]), B.Octets.of_list([0, 0, 0, 0, 0, 0, 0, 0]))) IO.print(blowfish(B.Octets.of_list([255, 255, 255, 255, 255, 255, 255, 255]), B.Octets.of_list([255, 255, 255, 255, 255, 255, 255, 255]))) IO.print(blowfish(B.Octets.of_list([254, 220, 186, 152, 118, 84, 50, 16]), B.Octets.of_list([1, 35, 69, 103, 137, 171, 205, 239]))) IO.print(blowfish(B.Octets.of_list([240]), B.Octets.of_list([254, 220, 186, 152, 118, 84, 50, 16]))) IO.print(blowfish(B.Octets.of_list([240, 225, 210]), B.Octets.of_list([254, 220, 186, 152, 118, 84, 50, 16]))) IO.print(blowfish(B.Octets.of_list([240, 225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 0, 17, 34, 51, 68, 85, 102, 119]), B.Octets.of_list([254, 220, 186, 152, 118, 84, 50, 16]))) IO.print(blowfish_cbc(B.Octets.of_list([1, 35, 69, 103, 137, 171, 205, 239]), 16384n)) #|4ef997456198dd78 0000000000000000 #|51866fd5b85ecb8a ffffffffffffffff #|0aceab0fc6a0a28d 0123456789abcdef #|f9ad597c49db005e fedcba9876543210 #|e9c2b70a1bc65cf3 fedcba9876543210 #|05044b62fa52d080 fedcba9876543210 #|d9e0b4045974d5f0 ok # END GENERATED: check