check.bend source
check.bend on the hub · documented module
# CAST-128: RFC 2144 B.1's three vectors, both ways (the ciphertext, then# the plaintext decrypted back), and CAST-128 through modes64.bend's CBC over# 16 KB both ways.import Baseimport bend-osdev-bytes@0.1.1.0/bytes.bend as Bimport bend-osdev-cipher-core@0.1.0.0/cipher_core.bend as Limport bend-osdev-cipher-core@0.1.0.0/modes64.bend as Ximport ./cast5.bend as C5def 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}: bdef 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}: kdef 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 cast5_key(k: B.Octets) -> C5.LcCast5Key: key(~C5.LcCast5Key, C5.LcCast5.key(k), C5.LcCast5Key{Nil{}})def cast5(k: B.Octets, b: B.Octets) -> String: both64(~C5.LcCast5Key, ~C5.LcCast5.encrypt, ~C5.LcCast5.decrypt, cast5_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 cast5_cbc(k: B.Octets, n: Nat) -> String: cbc64(~C5.LcCast5Key, ~C5.LcCast5.encrypt, ~C5.LcCast5.decrypt, cast5_key(k), n)# BEGIN GENERATED by gen/vectors.py: checkdef main() -> IO(Unit): do IO<Unit>: IO.print(cast5(B.Octets.of_list([1, 35, 69, 103, 18, 52, 86, 120, 35, 69, 103, 137, 52, 86, 120, 154]), B.Octets.of_list([1, 35, 69, 103, 137, 171, 205, 239]))) IO.print(cast5(B.Octets.of_list([1, 35, 69, 103, 18, 52, 86, 120, 35, 69]), B.Octets.of_list([1, 35, 69, 103, 137, 171, 205, 239]))) IO.print(cast5(B.Octets.of_list([1, 35, 69, 103, 18]), B.Octets.of_list([1, 35, 69, 103, 137, 171, 205, 239]))) IO.print(cast5_cbc(B.Octets.of_list([1, 35, 69, 103, 137, 171, 205, 239, 254, 220, 186, 152, 118, 84, 50, 16]), 16384n))#|238b4fe5847e44b2 0123456789abcdef#|eb6a711a2c02271b 0123456789abcdef#|7ac816d16e9b302e 0123456789abcdef#|1701cdf260449014 ok# END GENERATED: check