aes.bend checks
raw source on the hub · import bend-osdev-cipher-modes@0.1.0.0/aes.bend as Aes
AES-128 and AES-256 (FIPS 197) as block ciphers for the modes: the named default ciphers, used as ~CmAesKey, ~CmAes.encrypt, ~CmAes.decrypt Encryption is bend-collections' AES (bend-collections@1.0.0.0, src/crypto/aes/aes.bend: expand_key and encrypt_block). That library has no decryption, so this file adds FIPS 197's inverse cipher (section 5.3) and the key expansion it needs (section 5.2), written here from the specification. The inverse S-box is computed, not looked up: the inverse affine map, then the inverse in GF(2^8) as x^254, with masks instead of branches, so no branch or table index depends on a key or data byte.
4 imports
import Base import bend-osdev-bytes@0.1.0.0/bytes.bend as B import ./cipher_modes.bend as M import bend-collections@1.0.0.0/src/crypto/aes/aes.bend as A
Types
type CmAesState source · line 60 · raw
Data
CmAesState@s0:U32 -> @s1:U32 -> @s2:U32 -> @s3:U32 -> @s4:U32 -> @s5:U32 -> @s6:U32 -> @s7:U32 -> @s8:U32 -> @s9:U32 -> @s10:U32 -> @s11:U32 -> @s12:U32 -> @s13:U32 -> @s14:U32 -> @s15:U32 -> CmAesState
type CmAesKey source · line 229 · raw
Data
An expanded AES key: bend-collections' schedule for encryption, the key's bytes, and the round keys for decryption, last first, once CmAes.prepare has computed them (Nil until then: CmAes.decrypt then derives them for each block). Internal; built by CmAes.key.
CmAesKey@sched:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/aes.Schedule -> @raw:List<&2, U32> -> @dkeys:List<&2, CmAesState> -> CmAesKey
Definitions
def aes.xt source · line 18 · raw
@+x:U32 -> U32
def aes.gmul.go source · line 21 · raw
@n:Nat -> @+a:U32 -> @+b:U32 -> @p:U32 -> U32
def aes.gmul source · line 28 · raw
@+a:U32 -> @+b:U32 -> U32
def aes.inv.go source · line 32 · raw
@n:Nat -> @a:U32 -> @+x:U32 -> U32
a := a^2 * x, n times: from a = x, six times gives x^127
def aes.inv source · line 41 · raw
@+x:U32 -> U32
the multiplicative inverse, x^254 (0 for 0)
def aes.rotl source · line 45 · raw
@+b:U32 -> @+n:Nat -> U32
def aes.sbox source · line 49 · raw
@+x:U32 -> U32
the S-box: the inverse, then the affine map
def aes.isbox source · line 54 · raw
@x:U32 -> U32
the inverse S-box: the inverse affine map, then the inverse
def aes.byte source · line 63 · raw
@+w:U32 -> @n:Nat -> U32
def aes.state_of source · line 66 · raw
@b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> CmAesState
def aes.block_of source · line 71 · raw
@s:CmAesState -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def aes.add source · line 76 · raw
@s:CmAesState -> @k:CmAesState -> CmAesState
def aes.inv_shift_sub source · line 83 · raw
@s:CmAesState -> CmAesState
InvShiftRows: row r moves r columns to the right InvSubBytes: the inverse S-box on every byte (both in one pass)
def aes.m9 source · line 89 · raw
@+a:U32 -> U32
multiples of a byte by 9, 11, 13 and 14
def aes.m11 source · line 92 · raw
@+a:U32 -> U32
def aes.m13 source · line 96 · raw
@+a:U32 -> U32
def aes.m14 source · line 100 · raw
@+a:U32 -> U32
def aes.mix0 source · line 105 · raw
@+a:U32 -> @+b:U32 -> @+c:U32 -> @+d:U32 -> U32
def aes.inv_mix source · line 109 · raw
@s:CmAesState -> CmAesState
InvMixColumns: each column times {0e, 0b, 0d, 09}, rotated per row
def aes.rounds source · line 117 · raw
@rest:List<&2, CmAesState> -> @k:CmAesState -> @s:CmAesState -> CmAesState
The inverse cipher over the round keys, last round key first: AddRoundKey(Nr), then Nr - 1 rounds of InvShiftRows, InvSubBytes, AddRoundKey, InvMixColumns, then InvShiftRows, InvSubBytes, AddRoundKey(0)
def aes.decrypt source · line 124 · raw
@ks:List<&2, CmAesState> -> @s:CmAesState -> CmAesState
def aes.sub_word source · line 135 · raw
@+w:U32 -> U32
def aes.rot_word source · line 138 · raw
@+w:U32 -> U32
def aes.rcon.go source · line 141 · raw
@n:Nat -> @+x:U32 -> U32
def aes.rcon source · line 149 · raw
@j:Nat -> U32
x^(j - 1) in GF(2^8), the round constant of word j * Nk
def aes.temp.big source · line 157 · raw
@big:Bool -> @+w:U32 -> U32
Nk > 6 and i mod Nk = 4: SubWord alone
def aes.temp source · line 165 · raw
@first:Bool -> @+i:Nat -> @+nk:Nat -> @+w:U32 -> U32
temp from w[i - 1], for word i
def aes.head source · line 172 · raw
@xs:List<&2, U32> -> U32
def aes.tail source · line 179 · raw
@xs:List<&2, U32> -> List<&2, U32>
def aes.last source · line 186 · raw
@xs:List<&2, U32> -> @d:U32 -> U32
def aes.expand.go source · line 195 · raw
@n:Nat -> @+i:Nat -> @+nk:Nat -> @+win:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>
n more words from word i; win holds the last Nk words, oldest first, and acc every word so far, newest first
def aes.words source · line 204 · raw
@xs:List<&2, U32> -> List<&2, U32>
the key's bytes as big-endian words
def aes.round_keys source · line 211 · raw
@ws:List<&2, U32> -> List<&2, CmAesState>
def aes.expand source · line 219 · raw
@+nk:Nat -> @xs:List<&2, U32> -> List<&2, U32>
the 4 (Nr + 1) words of the schedule, Nr = Nk + 6
def aes.key.go source · line 232 · raw
@m:Maybe<&2, 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/aes.Schedule> -> @+xs:List<&2, U32> -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, CmAesKey>
def aes.key.pick source · line 239 · raw
@ok:Bool -> @+xs:List<&2, U32> -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, CmAesKey>
def aes.dkeys source · line 247 · raw
@dk:List<&2, CmAesState> -> @+raw:List<&2, U32> -> List<&2, CmAesState>
the decryption round keys: the ones computed already, or from the key bytes
def CmAes.key source · line 255 · raw
@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, CmAesKey>
a 16-byte (AES-128) or 32-byte (AES-256) key
def CmAes.key128 source · line 260 · raw
@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, CmAesKey>
def CmAes.key256 source · line 264 · raw
@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, CmAesKey>
def aes.of_maybe source · line 268 · raw
@m:Maybe<&2, List<&2, U32>> -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
def CmAes.encrypt source · line 276 · raw
@k:CmAesKey -> @b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
one block, encrypted (bend-collections' encrypt_block)
def CmAes.prepare source · line 282 · raw
@k:CmAesKey -> CmAesKey
the key with its decryption round keys computed, for decrypting many blocks
def CmAes.decrypt source · line 288 · raw
@k:CmAesKey -> @b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock
one block, decrypted (the inverse cipher above)