~/bend-docscommunity

ccm.bend checks

raw source on the hub · import bend-osdev-cipher-modes@0.1.0.0/ccm.bend as Ccm

CCM, counter with CBC-MAC (NIST SP 800-38C; RFC 3610), authenticated encryption over any 128-bit block cipher (only its encryption is used; see cipher-modes.bend for the template arguments).

Parameters: the tag length M (4, 6, 8, 10, 12, 14 or 16 bytes) and the nonce length N (7 to 13 bytes), which fixes the length field L = 15 - N. The message must be shorter than 2^(8L) bytes. B_0 = flags || nonce || l(m) flags = 64*Adata + 8*((M-2)/2) + (L-1) then l(a) encoded and a (if a is not empty), zero-padded to a block, and m, zero-padded: their CBC-MAC (IV 0) is the tag T (its first M bytes). A_i = (L-1) || nonce || i, S_i = E(A_i): ciphertext = m xor S_1 S_2 ..., then T xor the first M bytes of S_0. CCM needs the message length before it starts (it is in B_0), so it has no streaming interface.

3 imports
import Base
import bend-osdev-bytes@0.1.0.0/bytes.bend as B
import ./cipher_modes.bend as M

Definitions

def ccm.be source · line 20 · raw

@n:Nat -> @+v:U32 -> List<&2, U32>

the low n bytes of v, most significant first (zeros above the 32 bits of v)

def ccm.pad source · line 24 · raw

@+xs:List<&2, U32> -> List<&2, U32>

xs zero-padded to a whole number of blocks

def ccm.alen.pick source · line 28 · raw

@short:Bool -> @+v:U32 -> List<&2, U32>

l(a): two bytes below 65280 (2^16 - 2^8), else 0xFF 0xFE and four bytes

def ccm.aad.pick source · line 35 · raw

@empty:Bool -> @+a:List<&2, U32> -> List<&2, U32>

def ccm.ai source · line 63 · raw

@a0:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @i:U32 -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

A_i = A_0 with counter i (i < 2^(8L), so it stays in the counter field)

def ccm.a0 source · line 81 · raw

@+l:Nat -> @+nonce:List<&2, U32> -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

A_0: (L-1) || nonce || zeros

def ccm.tag_ok source · line 84 · raw

@+m:Nat -> Bool

def ccm.len_ok.three source · line 89 · raw

@three:Bool -> @+n:Nat -> Bool

the message must be shorter than 2^(8L) bytes: a bound only for L = 2 or 3 (with L >= 4 every list is shorter)

def ccm.len_ok.two source · line 96 · raw

@two:Bool -> @+l:Nat -> @+n:Nat -> Bool

def ccm.len_ok source · line 103 · raw

@+l:Nat -> @+n:Nat -> Bool

def ccm.checks.len source · line 106 · raw

@ok:Bool -> @+n:Nat -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, Unit>

def ccm.checks.nonce source · line 113 · raw

@ok:Bool -> @+nl:Nat -> @+n:Nat -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, Unit>

def ccm.checks.tag source · line 120 · raw

@ok:Bool -> @+m:Nat -> @+nl:Nat -> @+n:Nat -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, Unit>

def ccm.checks source · line 128 · raw

@+m:Nat -> @+nl:Nat -> @+n:Nat -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, Unit>

the tag length, the nonce length and the message length n, in that order

def ccm.open source · line 144 · raw

@ok:Bool -> @p:List<&2, U32> -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

def ccm.pad.fast source · line 179 · raw

@+xs:List<&2, U32> -> List<&2, U32>

def ccm.aad.pick.fast source · line 182 · raw

@empty:Bool -> @+a:List<&2, U32> -> List<&2, U32>

Templates

template ccm.cbcmac source · line 44 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @bs:List<&2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock> -> @x:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

CBC-MAC of whole blocks, from x

template ccm.mac source · line 52 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @+l:Nat -> @+nonce:List<&2, U32> -> @+a:List<&2, U32> -> @+p:List<&2, U32> -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

the CBC-MAC of B_0, the encoded a and the padded p

template ccm.tag source · line 59 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @+l:Nat -> @+nonce:List<&2, U32> -> @+a:List<&2, U32> -> @+p:List<&2, U32> -> List<&2, U32>

the tag: the first m bytes of the MAC

template ccm.ctrb source · line 67 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @bs:List<&2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock> -> @+a0:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+i:U32 -> List<&2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock>

CTR on whole blocks: b_j xor S_(i+j)

template ccm.xcrypt source · line 75 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+a0:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmSplit -> List<&2, U32>

CTR from counter 1: the whole blocks, then the tail xor the next S_i

template ccm.ebody source · line 132 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @+nv:List<&2, U32> -> @+av:List<&2, U32> -> @+pv:List<&2, U32> -> List<&2, U32>

the ciphertext of pv, then the tag

template ccm.e2 source · line 137 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @+nv:List<&2, U32> -> @+av:List<&2, U32> -> @+pv:List<&2, U32> -> @r:Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, Unit> -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template ccm.dbody source · line 152 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @+nv:List<&2, U32> -> @+av:List<&2, U32> -> @+cv:List<&2, U32> -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

the plaintext, if the tag (compared with no early exit) matches

template ccm.d3 source · line 159 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @+nv:List<&2, U32> -> @+av:List<&2, U32> -> @+cv:List<&2, U32> -> @r:Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, Unit> -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template ccm.d2 source · line 166 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @ok:Bool -> @+k:K -> @+m:Nat -> @+nv:List<&2, U32> -> @+av:List<&2, U32> -> @+cv:List<&2, U32> -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template ccm.mac.fast source · line 190 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @+l:Nat -> @+nonce:List<&2, U32> -> @+a:List<&2, U32> -> @+p:List<&2, U32> -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

template ccm.tag.fast source · line 196 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @+l:Nat -> @+nonce:List<&2, U32> -> @+a:List<&2, U32> -> @+p:List<&2, U32> -> List<&2, U32>

template ccm.ctrb.go source · line 200 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @bs:List<&2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock> -> @+a0:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+i:U32 -> @acc:List<&2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock> -> List<&2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock>

the blocks so far kept reversed in acc

template ccm.ctrb.fast source · line 207 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @bs:List<&2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock> -> @+a0:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @+i:U32 -> List<&2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock>

template ccm.xcrypt.fast source · line 210 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+a0:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @s:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmSplit -> List<&2, U32>

template ccm.ebody.fast source · line 215 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @+nv:List<&2, U32> -> @+av:List<&2, U32> -> @+pv:List<&2, U32> -> List<&2, U32>

template ccm.e2.fast source · line 220 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @+nv:List<&2, U32> -> @+av:List<&2, U32> -> @+pv:List<&2, U32> -> @r:Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, Unit> -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template ccm.dbody.fast source · line 227 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @+nv:List<&2, U32> -> @+av:List<&2, U32> -> @+cv:List<&2, U32> -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template ccm.d3.fast source · line 234 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @+nv:List<&2, U32> -> @+av:List<&2, U32> -> @+cv:List<&2, U32> -> @r:Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, Unit> -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template ccm.d2.fast source · line 241 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @ok:Bool -> @+k:K -> @+m:Nat -> @+nv:List<&2, U32> -> @+av:List<&2, U32> -> @+cv:List<&2, U32> -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template Ccm.encrypt.ref source · line 250 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @nonce:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @aad:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @p:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

Ccm.encrypt and Ccm.decrypt through the structural forms (the laws' proofs reason about these; PROOF.bend proves them equal to the public defs)

template Ccm.decrypt.ref source · line 255 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @nonce:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @aad:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @c:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template Ccm.encrypt source · line 260 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @nonce:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @aad:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @p:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

p with associated data aad under the nonce: the ciphertext, then the m-byte tag

template Ccm.decrypt source · line 268 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @+m:Nat -> @nonce:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @aad:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @c:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

the plaintext of c (the ciphertext, then the m-byte tag), or CmTagMismatch when the tag does not match; no plaintext is released from a message that fails