~/bend-docscommunity

cmac.bend checks

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

CMAC (NIST SP 800-38B; RFC 4493 for AES-CMAC), a MAC over any 128-bit block cipher (only its encryption is used; see cipher-modes.bend for the template arguments). L = E(0^128), K1 = dbl(L), K2 = dbl(K1) dbl(X) = X << 1, xor 0x87 into the last byte when X's top bit was set the message in 16-byte blocks M_1 .. M_n (n >= 1); the last is xored with K1 if it is whole, else padded with 0x80 0x00 .. and xored with K2; the MAC is the last block of their CBC encryption (IV 0). A message may arrive in pieces: start, update (any number of times) and finish give the MAC of the pieces joined.

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

Types

type CmacState source · line 38 · raw

Data

The state: the chaining value x, and the bytes not yet processed (at most 16, newest first) and their count. A full buffer is kept until the next byte arrives, since the last block is treated differently.

Definitions

def Cmac.dbl source · line 17 · raw

@b:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

def Cmac.start source · line 41 · raw

CmacState

Templates

template cmac.k1 source · line 22 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

template cmac.k2 source · line 25 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

template Cmac.subkey1 source · line 29 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

the subkeys K1 and K2

template Cmac.subkey2 source · line 32 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock

template cmac.push source · line 44 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @full:Bool -> @+k:K -> @x:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> @buf:List<&2, U32> -> @+n:Nat -> @b:U32 -> CmacState

template cmac.step source · line 51 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @s:CmacState -> @b:U32 -> CmacState

template cmac.feed source · line 56 · raw

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

template Cmac.update source · line 64 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @s:CmacState -> @m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> CmacState

the state after the bytes of m

template cmac.last source · line 68 · raw

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

the last block: whole, xor K1; partial (or empty), padded, xor K2

template Cmac.finish source · line 76 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @s:CmacState -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets

the 16-byte MAC

template Cmac.mac source · line 82 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets

The MAC of m in one call

template Cmac.verify source · line 86 · raw

@-K:Data -> @-enc:(@_:K -> @_:0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock -> 0xf4b936e0e6b2608080d2f02bb3ce9abe/cipher_modes.CmBlock) -> @+k:K -> @m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @tag:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Bool

whether tag is m's 16-byte MAC, compared with no early exit