check.bend source
check.bend on the hub · documented module
# ARIA: RFC 5794's three vectors, both ways (the ciphertext, then the# plaintext decrypted back), and ARIA-128 through cbc's CBC over# 32 KB both ways.import Baseimport bend-osdev-bytes@0.1.1.0/bytes.bend as Bimport bend-osdev-mode-core@0.1.0.0/mode_core.bend as Mimport bend-osdev-cbc@0.1.0.0/cbc.bend as CBimport bend-osdev-cipher-core@0.1.0.0/cipher_core.bend as Limport ./aria.bend as Adef b128.of(m: Maybe<&2, M.CmBlock>) -> M.CmBlock: match m: case None{}: M.CmBlock.zero() case Some{b}: bdef b128(o: B.Octets) -> M.CmBlock: b128.of(M.CmBlock.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 both(~K: Data, ~enc: K -> M.CmBlock -> M.CmBlock, ~dec: K -> M.CmBlock -> M.CmBlock, +k: K, b: M.CmBlock) -> String: +c = enc(k, b) M.CmBlock.hex(c) ++ " " ++ M.CmBlock.hex(dec(k, c))def aria_key(k: B.Octets) -> A.LcAriaKey: key(~A.LcAriaKey, A.LcAria.key(k), A.LcAriaKey{Nil{}})def aria(k: B.Octets, b: B.Octets) -> String: both(~A.LcAriaKey, ~A.LcAria.encrypt, ~A.LcAria.decrypt, aria_key(k), b128(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 iv16() -> B.Octets: B.Octets.of_list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])def last16(r: Result<&2, &2, M.CmError, B.Octets>) -> String: match r: case Fail{e}: "fail" case Done{+o}: B.Octets.hex(B.Octets.drop(Nat.sub(B.Octets.len(o), 16n), o))def verdict(b: Bool) -> String: match b: case True{}: "ok" case False{}: "differs"def same(r: Result<&2, &2, M.CmError, B.Octets>, +m: B.Octets) -> String: match r: case Fail{e}: "fail" case Done{o}: verdict(B.Octets.is_eq(o, m))def cbc128(~K: Data, ~enc: K -> M.CmBlock -> M.CmBlock, ~dec: K -> M.CmBlock -> M.CmBlock, +k: K, n: Nat) -> String: +m = big(n) +c = CB.Cbc.encrypt(~K, ~enc, k, iv16(), m) last16(c) ++ " " ++ same(Result.bind(&2, &2, M.CmError, B.Octets, B.Octets, c, x => CB.Cbc.decrypt(~K, ~dec, k, iv16(), x)), m)def aria_cbc(k: B.Octets, n: Nat) -> String: cbc128(~A.LcAriaKey, ~A.LcAria.encrypt, ~A.LcAria.decrypt, aria_key(k), n)# BEGIN GENERATED by gen/vectors.py: checkdef main() -> IO(Unit): do IO<Unit>: IO.print(aria(B.Octets.of_list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]), B.Octets.of_list([0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255]))) IO.print(aria(B.Octets.of_list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]), B.Octets.of_list([0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255]))) IO.print(aria(B.Octets.of_list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]), B.Octets.of_list([0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255]))) IO.print(aria_cbc(B.Octets.of_list([1, 35, 69, 103, 137, 171, 205, 239, 254, 220, 186, 152, 118, 84, 50, 16]), 32768n))#|d718fbd6ab644c739da95f3be6451778 00112233445566778899aabbccddeeff#|26449c1805dbe7aa25a468ce263a9e79 00112233445566778899aabbccddeeff#|f92bd7c79fb72e2f2b8f80c1972d24fc 00112233445566778899aabbccddeeff#|78d7ea7c6c947995b6180eeac7c72206 ok# END GENERATED: check