inst_aes.bend
1# AES as a block cipher: cipher-modes' AES-128 and AES-256 (FIPS 197;
2# encryption from bend-collections, decryption in cipher-modes), with its
3# key schedule's decryption round keys computed once (CmAes.prepare). The
4# instance:
5# ~X.CmAesKey, ~M.CmBlock, ~16n, ~CrAes.key, ~X.CmAes.encrypt,
6# ~X.CmAes.decrypt, ~CrBlk128.of_octets, ~CrBlk128.to_octets, ~CrBlk128.xor
7# and, for a mode that only encrypts (CTR, CMAC, CCM), the same with
8# ~CrAes.key_enc in place of ~CrAes.key, which skips the decryption round
9# keys.
10# This is the only file of the package that imports cipher-modes' aes.bend,
11# and so bend-collections.
12import Base
13import bend-osdev-bytes@0.1.0.0/bytes.bend as B
14import bend-osdev-cipher-modes@0.1.0.0/cipher_modes.bend as M
15import bend-osdev-cipher-modes@0.1.0.0/aes.bend as X
16import bend-osdev-crypto@0.1.0.0/crypto.bend as Cr
17import bend-osdev-crypto@0.1.0.0/inst_cm.bend as CM
18
19# a 16- or 32-byte key (AES-128 or AES-256), with its decryption round keys;
20# CrBadKeyLength for any other length
21def CrAes.key(o: B.Octets) -> Result<&2, &2, Cr.CrError, X.CmAesKey>:
22 CM.CrCm.lift(~X.CmAesKey, Result.map(&2, &2, M.CmError, X.CmAesKey, X.CmAesKey, X.CmAes.prepare, X.CmAes.key(o)))
23
24# the same without the decryption round keys, for uses that only encrypt
25# (CTR, CMAC, CCM): a key made this way still decrypts, deriving those round
26# keys again for every block
27def CrAes.key_enc(o: B.Octets) -> Result<&2, &2, Cr.CrError, X.CmAesKey>:
28 CM.CrCm.lift(~X.CmAesKey, X.CmAes.key(o))