mode_core.bend checks
raw source on the hub · import bend-osdev-mode-core@0.1.0.0/mode_core.bend as Mode_core
bend-osdev-mode-core: what the block-cipher modes of operation over any 128-bit block cipher share (bend-osdev-cbc, -xts, -ccm, -cmac, -pkcs7, and the ciphers that plug into them). This file holds it: the 128-bit block, the package's error type, and the byte-level helpers the modes are built from.
The block cipher is a parameter. Every mode takes it as template arguments: ~K: Data the cipher's key (an expanded key schedule) ~enc: K -> CmBlock -> CmBlock encryption of one block ~dec: K -> CmBlock -> CmBlock decryption of one block (CBC, XTS only) and the key itself as +k: K. The modes never look inside K, so they run on any cipher with 128-bit blocks; aes.bend supplies AES-128 and AES-256.
A CmBlock is four 32-bit words, big-endian: w0 holds bytes 0 to 3 of the block, byte 0 in its top 8 bits. Every value of the type is a block (there is no out-of-range byte), so a cipher's "decryption inverts encryption" can be stated for every block. The type is open: a cipher adapter may build and match blocks directly.
3 imports
import Base import bend-osdev-bytes@0.1.1.0/bytes.bend as B import bend-osdev-word64@0.1.1.0/word64.bend as W
Types
type CmBlock source · line 22 · raw
Data
CmBlock@w0:U32 -> @w1:U32 -> @w2:U32 -> @w3:U32 -> CmBlock
type CmError source · line 26 · raw
Data
Every failure a mode can report
CmBadKeyLength@got:Nat -> CmError
CmBadIvLength@got:Nat -> CmError
CmBadTweakLength@got:Nat -> CmError
CmBadNonceLength@got:Nat -> CmError
CmBadTagLength@got:Nat -> CmError
CmBadBlockSize@got:Nat -> CmError
CmNotBlockMultiple@len:Nat -> CmError
CmTooShort@len:Nat -> CmError
CmTooLong@len:Nat -> CmError
CmBadPaddingCmError
CmTagMismatchCmError
type CmSplit source · line 146 · raw
Data
A byte string as its whole blocks and the 0 to 15 bytes after them
CmSplit@blocks:List<&2, CmBlock> -> @tail:List<&2, U32> -> CmSplit
Definitions
def CmBlock.zero source · line 41 · raw
CmBlock
def CmBlock.xor source · line 44 · raw
@a:CmBlock -> @b:CmBlock -> CmBlock
def cm.byte source · line 64 · raw
@x:U32 -> U32
a byte (the low 8 bits of x), as bend-osdev-bytes masks it
def cm.bits8 source · line 67 · raw
@b:U32 -> Word(8n)
def cm.of8 source · line 72 · raw
@s:Word(8n) -> U32
def cm.word source · line 76 · raw
@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> U32
the word with bytes a, b, c, d, most significant first
def cm.b0 source · line 80 · raw
@w:U32 -> U32
the bytes of w, most significant first
def cm.b1 source · line 85 · raw
@w:U32 -> U32
def cm.b2 source · line 90 · raw
@w:U32 -> U32
def cm.b3 source · line 95 · raw
@w:U32 -> U32
def cm.unword source · line 101 · raw
@+w:U32 -> @rest:List<&2, U32> -> List<&2, U32>
the four bytes of w, most significant first, in front of rest
def cm.w0 source · line 104 · raw
@b:CmBlock -> U32
def cm.w1 source · line 109 · raw
@b:CmBlock -> U32
def cm.w2 source · line 114 · raw
@b:CmBlock -> U32
def cm.w3 source · line 119 · raw
@b:CmBlock -> U32
def cm.bytes_onto source · line 125 · raw
@+b:CmBlock -> @rest:List<&2, U32> -> List<&2, U32>
the 16 bytes of a block in front of rest (always 16 cells, whatever b is)
def cm.bytes_of source · line 128 · raw
@b:CmBlock -> List<&2, U32>
def cm.block16 source · line 131 · raw
@a0:U32 -> @a1:U32 -> @a2:U32 -> @a3:U32 -> @a4:U32 -> @a5:U32 -> @a6:U32 -> @a7:U32 -> @a8:U32 -> @a9:U32 -> @a10:U32 -> @a11:U32 -> @a12:U32 -> @a13:U32 -> @a14:U32 -> @a15:U32 -> CmBlock
def cm.block_of.full source · line 134 · raw
@xs:List<&2, U32> -> CmBlock
def cm.block_of source · line 142 · raw
@xs:List<&2, U32> -> CmBlock
the block of the first 16 bytes of xs, zero-filled if xs is shorter
def cm.split.cons source · line 149 · raw
@b:CmBlock -> @s:CmSplit -> CmSplit
def cm.split source · line 154 · raw
@+xs:List<&2, U32> -> CmSplit
def cm.split.blocks source · line 161 · raw
@s:CmSplit -> List<&2, CmBlock>
def cm.split.tail source · line 166 · raw
@s:CmSplit -> List<&2, U32>
def cm.join_onto source · line 172 · raw
@bs:List<&2, CmBlock> -> @rest:List<&2, U32> -> List<&2, U32>
the bytes of the blocks bs, in front of rest
def cm.join source · line 179 · raw
@bs:List<&2, CmBlock> -> List<&2, U32>
def cm.xor_bytes source · line 183 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> List<&2, U32>
byte-wise XOR, as long as the shorter list
def cm.blocks.rev_onto source · line 199 · raw
@bs:List<&2, CmBlock> -> @acc:List<&2, CmBlock> -> List<&2, CmBlock>
the blocks of bs in reverse order, in front of acc
def cm.blocks.len.go source · line 206 · raw
@bs:List<&2, CmBlock> -> @n:Nat -> Nat
def cm.blocks.len source · line 214 · raw
@bs:List<&2, CmBlock> -> Nat
the number of blocks (List.length)
def cm.blocks.append source · line 218 · raw
@xs:List<&2, CmBlock> -> @ys:List<&2, CmBlock> -> List<&2, CmBlock>
xs, then ys (List.append)
def cm.split.go source · line 222 · raw
@+xs:List<&2, U32> -> @acc:List<&2, CmBlock> -> CmSplit
the blocks split so far are kept reversed in acc (cm.split)
def cm.split.fast source · line 229 · raw
@+xs:List<&2, U32> -> CmSplit
def cm.join.go source · line 233 · raw
@rs:List<&2, CmBlock> -> @acc:List<&2, U32> -> List<&2, U32>
the bytes of the blocks rs (last first) in front of acc
def cm.join_onto.fast source · line 241 · raw
@bs:List<&2, CmBlock> -> @rest:List<&2, U32> -> List<&2, U32>
cm.join_onto
def cm.xor_bytes.go source · line 244 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>
def cm.xor_bytes.fast source · line 252 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> List<&2, U32>
cm.xor_bytes
def cm.diff source · line 255 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> @acc:U32 -> U32
def cm.ct_eq source · line 265 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> Bool
equality of two byte lists with no early exit of the same length (a tag check)
def cm.mask source · line 269 · raw
@b:U32 -> U32
0 for a 0 bit, all ones for a 1 bit (the low bit of b)
def cm.check source · line 274 · raw
@ok:Bool -> @e:CmError -> Result<&2, &2, CmError, Unit>
def cm.need_block.pick source · line 282 · raw
@ok:Bool -> @xs:List<&2, U32> -> @e:CmError -> Result<&2, &2, CmError, CmBlock>
a 16-byte value as a block, or the error e
def cm.need_block source · line 289 · raw
@+xs:List<&2, U32> -> @e:CmError -> Result<&2, &2, CmError, CmBlock>
def CmBlock.to_octets source · line 294 · raw
@b:CmBlock -> 0xe0217e1ab38629fddf738452c1f2a82e/bytes.Octets
def CmBlock.of_octets.pick source · line 297 · raw
@ok:Bool -> @xs:List<&2, U32> -> Maybe<&2, CmBlock>
def CmBlock.of_octets source · line 305 · raw
@o:0xe0217e1ab38629fddf738452c1f2a82e/bytes.Octets -> Maybe<&2, CmBlock>
a 16-byte string as a block; None for any other length
def CmBlock.hex source · line 309 · raw
@b:CmBlock -> String
Templates
template CmBlock.dec_enc source · line 52 · raw
@-K:Data -> @-enc:(@_:K -> @_:CmBlock -> CmBlock) -> @-dec:(@_:K -> @_:CmBlock -> CmBlock) -> @+k:K -> @xs:List<&2, CmBlock> -> List<&2, CmBlock>
Each block of xs encrypted under k and decrypted again: xs itself for every list exactly where decryption undoes encryption at k, which is how the round-trip laws (LAWS.bend) take that property of the cipher