cipher_modes.bend checks
raw source on the hub · import bend-osdev-cipher-modes@0.1.0.0/cipher_modes.bend as Cipher_modes
bend-osdev-cipher-modes: block-cipher modes of operation over any 128-bit block cipher. This file holds what every mode shares: 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.0.0/bytes.bend as B import bend-osdev-word64@0.1.0.0/word64.bend as W
Types
type CmBlock source · line 21 · raw
Data
CmBlock@w0:U32 -> @w1:U32 -> @w2:U32 -> @w3:U32 -> CmBlock
type CmError source · line 25 · 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 135 · 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 40 · raw
CmBlock
def CmBlock.xor source · line 43 · raw
@a:CmBlock -> @b:CmBlock -> CmBlock
def cm.byte source · line 53 · raw
@x:U32 -> U32
a byte (the low 8 bits of x), as bend-osdev-bytes masks it
def cm.bits8 source · line 56 · raw
@b:U32 -> Word(8n)
def cm.of8 source · line 61 · raw
@s:Word(8n) -> U32
def cm.word source · line 65 · 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 69 · raw
@w:U32 -> U32
the bytes of w, most significant first
def cm.b1 source · line 74 · raw
@w:U32 -> U32
def cm.b2 source · line 79 · raw
@w:U32 -> U32
def cm.b3 source · line 84 · raw
@w:U32 -> U32
def cm.unword source · line 90 · 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 93 · raw
@b:CmBlock -> U32
def cm.w1 source · line 98 · raw
@b:CmBlock -> U32
def cm.w2 source · line 103 · raw
@b:CmBlock -> U32
def cm.w3 source · line 108 · raw
@b:CmBlock -> U32
def cm.bytes_onto source · line 114 · 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 117 · raw
@b:CmBlock -> List<&2, U32>
def cm.block16 source · line 120 · 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 123 · raw
@xs:List<&2, U32> -> CmBlock
def cm.block_of source · line 131 · 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 138 · raw
@b:CmBlock -> @s:CmSplit -> CmSplit
def cm.split source · line 143 · raw
@+xs:List<&2, U32> -> CmSplit
def cm.split.blocks source · line 150 · raw
@s:CmSplit -> List<&2, CmBlock>
def cm.split.tail source · line 155 · raw
@s:CmSplit -> List<&2, U32>
def cm.join_onto source · line 161 · 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 168 · raw
@bs:List<&2, CmBlock> -> List<&2, U32>
def cm.xor_bytes source · line 172 · 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 188 · 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 195 · raw
@bs:List<&2, CmBlock> -> @n:Nat -> Nat
def cm.blocks.len source · line 203 · raw
@bs:List<&2, CmBlock> -> Nat
the number of blocks (List.length)
def cm.blocks.append source · line 207 · raw
@xs:List<&2, CmBlock> -> @ys:List<&2, CmBlock> -> List<&2, CmBlock>
xs, then ys (List.append)
def cm.split.go source · line 211 · 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 218 · raw
@+xs:List<&2, U32> -> CmSplit
def cm.join.go source · line 222 · 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 230 · raw
@bs:List<&2, CmBlock> -> @rest:List<&2, U32> -> List<&2, U32>
cm.join_onto
def cm.xor_bytes.go source · line 233 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>
def cm.xor_bytes.fast source · line 241 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> List<&2, U32>
cm.xor_bytes
def cm.diff source · line 244 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> @acc:U32 -> U32
def cm.ct_eq source · line 254 · 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 258 · 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 263 · raw
@ok:Bool -> @e:CmError -> Result<&2, &2, CmError, Unit>
def cm.need_block.pick source · line 271 · 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 278 · raw
@+xs:List<&2, U32> -> @e:CmError -> Result<&2, &2, CmError, CmBlock>
def CmBlock.to_octets source · line 283 · raw
@b:CmBlock -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
def CmBlock.of_octets.pick source · line 286 · raw
@ok:Bool -> @xs:List<&2, U32> -> Maybe<&2, CmBlock>
def CmBlock.of_octets source · line 294 · raw
@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, CmBlock>
a 16-byte string as a block; None for any other length
def CmBlock.hex source · line 298 · raw
@b:CmBlock -> String