~/bend-docscommunity

aes.bend checks

raw source on the hub · import bend-osdev-aes@0.1.0.0/aes.bend as Aes

AES-128, AES-192 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): it undoes bend-collections' cipher round by round, with that cipher's own round keys (its Schedule), the last one first. 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; InvMixColumns' products by {09}, {0b}, {0d} and {0e} are sums of doublings (xtime), likewise branch-free.

Both directions use the round keys with each byte masked to its low 8 bits (aes.sched), which changes nothing for a key made by CmAes.key (its bytes are below 256) and lets LAWS.bend state the round trip for every value of CmAesKey.

5 imports
import Base
import bend-osdev-bytes@0.1.1.0/bytes.bend as B
import bend-osdev-mode-core@0.1.0.0/mode_core.bend as M
import bend-collections@1.0.0.0/src/crypto/aes/aes.bend as A
import bend-collections@1.0.0.0/src/crypto/aes/types.bend as T

Types

type CmAesKey source · line 199 · raw

Data

An expanded AES key: bend-collections' schedule (the number of rounds and the round keys), for encryption and decryption alike. Internal; built by CmAes.key.

Definitions

def aes.xt source · line 26 · raw

@+x:U32 -> U32

def aes.gmul.go source · line 29 · raw

@n:Nat -> @+a:U32 -> @+b:U32 -> @p:U32 -> U32

def aes.gmul source · line 36 · raw

@+a:U32 -> @+b:U32 -> U32

def aes.inv.go source · line 40 · 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 49 · raw

@+x:U32 -> U32

the multiplicative inverse, x^254 (0 for 0)

def aes.rotl source · line 53 · raw

@+b:U32 -> @+n:Nat -> U32

def aes.isbox source · line 57 · raw

@x:U32 -> U32

the inverse S-box: the inverse affine map, then the inverse

def aes.mask_w source · line 65 · raw

@w:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.Quad -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.Quad

each byte's low 8 bits

def aes.mask source · line 70 · raw

@s:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State

def aes.mask_keys source · line 75 · raw

@ks:List<&2, 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State> -> List<&2, 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State>

def aes.sched source · line 83 · raw

@s:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/aes.Schedule -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/aes.Schedule

a schedule with every round key byte masked

def aes.isub_w source · line 89 · raw

@w:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.Quad -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.Quad

InvSubBytes: the inverse S-box on every byte

def aes.inv_sub source · line 94 · raw

@s:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State

def aes.inv_shift source · line 100 · raw

@s:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State

InvShiftRows: row r moves r columns to the right

def aes.c9 source · line 108 · raw

@+b:U32 -> U32

InvMixColumns: each column times {0e, 0b, 0d, 09}, rotated per row a byte times {09}, {0b}, {0d}, {0e}: x^3 + 1, x^3 + x + 1, x^3 + x^2 + 1, x^3 + x^2 + x, as sums of doublings

def aes.c11 source · line 111 · raw

@+b:U32 -> U32

def aes.c13 source · line 115 · raw

@+b:U32 -> U32

def aes.c14 source · line 119 · raw

@+b:U32 -> U32

def aes.m9 source · line 125 · raw

@a:U32 -> U32

the same products of a word's low byte

def aes.m11 source · line 128 · raw

@a:U32 -> U32

def aes.m13 source · line 131 · raw

@a:U32 -> U32

def aes.m14 source · line 134 · raw

@a:U32 -> U32

def aes.imix0 source · line 137 · raw

@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> U32

def aes.imix_w source · line 140 · raw

@w:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.Quad -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.Quad

def aes.inv_mix source · line 145 · raw

@s:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State

def aes.inv_round source · line 152 · raw

@c:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State -> @k:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State

A round undone (round key k): AddRoundKey, InvMixColumns, InvShiftRows, InvSubBytes; the last round has no MixColumns to undo

def aes.inv_last source · line 155 · raw

@c:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State -> @k:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State

def aes.inv_rounds source · line 160 · raw

@n:Nat -> @ks:List<&2, 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State> -> @c:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State

bend-collections' rounds (n full rounds, then the last, a round key each) undone, the last round key first

def aes.inv_cipher source · line 171 · raw

@+nr:Nat -> @ks:List<&2, 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State> -> @c:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State

bend-collections' cipher (AddRoundKey with the first round key, then the rounds) undone

def aes.state16 source · line 179 · raw

@xs:List<&2, U32> -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State

a block's 16 bytes as a state, and back

def aes.state_in source · line 186 · raw

@b:0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmBlock -> 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State

def aes.out source · line 189 · raw

@s:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/types.State -> 0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmBlock

def aes.key.go source · line 202 · raw

@m:Maybe<&2, 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/aes.Schedule> -> @+xs:List<&2, U32> -> Result<&2, &2, 0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmError, CmAesKey>

def aes.key.pick source · line 209 · raw

@ok:Bool -> @+xs:List<&2, U32> -> Result<&2, &2, 0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmError, CmAesKey>

def CmAes.key source · line 217 · raw

@o:0xe0217e1ab38629fddf738452c1f2a82e/bytes.Octets -> Result<&2, &2, 0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmError, CmAesKey>

a 16-byte (AES-128) or 32-byte (AES-256) key

def CmAes.key128 source · line 222 · raw

@o:0xe0217e1ab38629fddf738452c1f2a82e/bytes.Octets -> Result<&2, &2, 0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmError, CmAesKey>

def CmAes.key192 source · line 226 · raw

@o:0xe0217e1ab38629fddf738452c1f2a82e/bytes.Octets -> Result<&2, &2, 0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmError, CmAesKey>

def CmAes.key256 source · line 230 · raw

@o:0xe0217e1ab38629fddf738452c1f2a82e/bytes.Octets -> Result<&2, &2, 0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmError, CmAesKey>

def aes.of_maybe source · line 234 · raw

@m:Maybe<&2, List<&2, U32>> -> 0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmBlock

def CmAes.encrypt source · line 242 · raw

@k:CmAesKey -> @b:0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmBlock -> 0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmBlock

one block, encrypted (bend-collections' encrypt_block)

def CmAes.prepare source · line 250 · raw

@k:CmAesKey -> CmAesKey

the key as it is: decryption needs nothing beyond the schedule (kept so that callers written for an earlier version, which computed decryption round keys here, still build)

def aes.decrypt_with source · line 253 · raw

@s:0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/aes/aes.Schedule -> @b:0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmBlock -> 0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmBlock

def CmAes.decrypt source · line 259 · raw

@k:CmAesKey -> @b:0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmBlock -> 0x999adc34209a88c2495b3a5407a5ad25/mode_core.CmBlock

one block, decrypted (the inverse cipher above)