# The AES encryption round function, AESRound(A, B) = MixColumns(ShiftRows( # SubBytes(A))) xor B (FIPS 197, sections 5.1.1 to 5.1.4; not the last # round), on 16-byte blocks: the building block of AEGIS (aegis128.bend). It # is the x86 instruction AESENC; RFC 10032 (the AEGIS family), Appendix A.1, # gives its test vector. # # A block is four big-endian 32-bit words, each one column of the AES state: # byte r of word c is the state's row r, column c (FIPS 197, 3.4: byte i of # the input is row i mod 4, column i / 4). The S-box is a table built once # (AdAes.sbox) and passed in, so a caller that runs many rounds (AEGIS keeps # it in its key) builds it once. import Base import bend-osdev-bytes@0.1.0.0/bytes.bend as B # A 16-byte block (open type) type AdBlk is Data: AdBlk{w0: U32, w1: U32, w2: U32, w3: U32} def AdBlk.zero() -> AdBlk: AdBlk{0, 0, 0, 0} def AdBlk.xor(a: AdBlk, b: AdBlk) -> AdBlk: match a b: case AdBlk{a0, a1, a2, a3} AdBlk{b0, b1, b2, b3}: AdBlk{U32.xor(a0, b0), U32.xor(a1, b1), U32.xor(a2, b2), U32.xor(a3, b3)} def AdBlk.and(a: AdBlk, b: AdBlk) -> AdBlk: match a b: case AdBlk{a0, a1, a2, a3} AdBlk{b0, b1, b2, b3}: AdBlk{U32.and(a0, b0), U32.and(a1, b1), U32.and(a2, b2), U32.and(a3, b3)} def ad.blk.at(xs: List<&2, U32>, i: Nat) -> U32: match xs: case Nil{}: 0 case Con{h, t}: match i: case 0n: h case 1n+p: ad.blk.at(t, p) def ad.blk.word(+xs: List<&2, U32>, +i: Nat) -> U32: U32.or(U32.or(U32.shln(B.byte(ad.blk.at(xs, i)), 24n), U32.shln(B.byte(ad.blk.at(xs, Nat.add(i, 1n))), 16n)), U32.or(U32.shln(B.byte(ad.blk.at(xs, Nat.add(i, 2n))), 8n), B.byte(ad.blk.at(xs, Nat.add(i, 3n))))) # the block of the first 16 bytes of xs (zeros past the end) def AdBlk.of_list(+xs: List<&2, U32>) -> AdBlk: AdBlk{ad.blk.word(xs, 0n), ad.blk.word(xs, 4n), ad.blk.word(xs, 8n), ad.blk.word(xs, 12n)} # a block's 16 bytes def AdBlk.to_list(b: AdBlk) -> List<&2, U32>: match b: case AdBlk{w0, w1, w2, w3}: B.append(B.be32(w0), B.append(B.be32(w1), B.append(B.be32(w2), B.be32(w3)))) def AdBlk.of_octets(+o: B.Octets) -> AdBlk: AdBlk.of_list(B.Octets.to_list(o)) def AdBlk.to_octets(b: AdBlk) -> B.Octets: B.Octets.of_list(AdBlk.to_list(b)) def AdBlk.hex(b: AdBlk) -> String: B.Octets.hex(AdBlk.to_octets(b)) # The S-box # BEGIN GENERATED by gen/tables.py: sbox def ad.aes.sbox.list() -> List<&2, U32>: [99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22] # END GENERATED: sbox # a table of 2^d words, read by the bits of an index (internal) type AdTab is Data: AdLeaf{v: U32} AdNode{l: AdTab, r: AdTab} def ad.tab.leaves(xs: List<&2, U32>) -> List<&2, AdTab>: match xs: case Nil{}: Nil{} case Con{h, t}: Con{AdLeaf{h}, ad.tab.leaves(t)} def ad.tab.pairs(ts: List<&2, AdTab>) -> List<&2, AdTab>: match ts: case Con{a, Con{b, t}}: Con{AdNode{a, b}, ad.tab.pairs(t)} case _: Nil{} def ad.tab.up(n: Nat, ts: List<&2, AdTab>) -> List<&2, AdTab>: match n: case 0n: ts case 1n+p: ad.tab.up(p, ad.tab.pairs(ts)) def ad.tab.root(ts: List<&2, AdTab>) -> AdTab: match ts: case Nil{}: AdLeaf{0} case Con{h, t}: h def ad.tab.pick(one: Bool, l: AdTab, r: AdTab) -> AdTab: match one: case True{}: r case False{}: l def ad.tab.get(d: Nat, t: AdTab, +i: U32) -> U32: match d: case 0n: match t: case AdLeaf{v}: v case AdNode{l, r}: 0 case 1n+(+p): match t: case AdLeaf{v}: v case AdNode{l, r}: ad.tab.get(p, ad.tab.pick(U32.is_eq(U32.and(1, U32.shrn(i, p)), 1), l, r), i) # The S-box as a table (opaque) type AdSbox is Data: AdSbox{t: AdTab} def AdAes.sbox() -> AdSbox: AdSbox{ad.tab.root(ad.tab.up(8n, ad.tab.leaves(ad.aes.sbox.list())))} def ad.aes.s(+t: AdTab, +w: U32, n: Nat) -> U32: ad.tab.get(8n, t, U32.and(255, U32.shrn(w, n))) # x times 2 in GF(2^8) (xtime, FIPS 197 4.2.1) def ad.aes.x2(+a: U32) -> U32: U32.xor(U32.and(255, U32.shl(a)), U32.mul(27, U32.shrn(a, 7n))) # MixColumns of one column a0 .. a3 (row 0 first), as a word def ad.aes.mix(+a0: U32, +a1: U32, +a2: U32, +a3: U32) -> U32: +b0 = U32.xor(U32.xor(ad.aes.x2(a0), U32.xor(ad.aes.x2(a1), a1)), U32.xor(a2, a3)) +b1 = U32.xor(U32.xor(a0, ad.aes.x2(a1)), U32.xor(U32.xor(ad.aes.x2(a2), a2), a3)) +b2 = U32.xor(U32.xor(a0, a1), U32.xor(ad.aes.x2(a2), U32.xor(ad.aes.x2(a3), a3))) +b3 = U32.xor(U32.xor(U32.xor(ad.aes.x2(a0), a0), a1), U32.xor(a2, ad.aes.x2(a3))) U32.or(U32.or(U32.shln(b0, 24n), U32.shln(b1, 16n)), U32.or(U32.shln(b2, 8n), b3)) # one column of the round: rows 0 .. 3 from columns c, c + 1, c + 2, c + 3 # (ShiftRows), each through the S-box (SubBytes), then MixColumns def ad.aes.col(+t: AdTab, +x0: U32, +x1: U32, +x2: U32, +x3: U32) -> U32: ad.aes.mix(ad.aes.s(t, x0, 24n), ad.aes.s(t, x1, 16n), ad.aes.s(t, x2, 8n), ad.aes.s(t, x3, 0n)) # AESRound(a, k) def AdAes.round(s: AdSbox, a: AdBlk, k: AdBlk) -> AdBlk: match s a k: case AdSbox{+t} AdBlk{+a0, +a1, +a2, +a3} AdBlk{k0, k1, k2, k3}: AdBlk{U32.xor(ad.aes.col(t, a0, a1, a2, a3), k0), U32.xor(ad.aes.col(t, a1, a2, a3, a0), k1), U32.xor(ad.aes.col(t, a2, a3, a0, a1), k2), U32.xor(ad.aes.col(t, a3, a0, a1, a2), k3)}