~/bend-docscommunity

vmac.bend checks

raw source on the hub · import bend-osdev-crypto@0.1.0.0/vmac.bend as Vmac

VMAC over any block cipher of at least 128 bits (the block-cipher interface, block.bend), as T. Krovetz and W. Dai, "VMAC: Message Authentication Code using Universal Hashing" (draft-krovetz-vmac-01, 2007) specifies it with AES: a 64- or 128-bit tag (VMAC-64, VMAC-128) of a message under a key and a nonce of 1 to 15 bytes.

KDF(K, index, n): the first n bytes of E(K, index || 0), E(K, index || 1), .. (the index a byte, the counter the block's other bytes, big-endian) VHASH, one 64-bit word per 64 bits of tag (iter = 0, 1): L1: NH over each 128-byte piece of the message (zero-padded to 16 bytes): the sum of (m_i + k_i)(m_(i+1) + k_(i+1)) mod 2^126 over the piece's 64-bit words m (little-endian) and the NH key's k (big-endian; KDF index 128, offset by 16 bytes per iter) L2: y = 1, then y = y k + a mod 2^127 - 1 for each NH value a (the poly key k from KDF index 192 with each 32-bit field's top 3 bits cleared; y = k for an empty message), then plus (the message's bit length mod 1024) 2^64 L3: ((m1 + k1)(m2 + k2)) mod 2^64 - 257, m1 and m2 y's quotient and remainder by 2^64 - 2^32, k1 and k2 the iter-th pair of 64-bit words of KDF index 224 both below 2^64 - 257 PDF: E(K, the nonce left-padded with zeros to a block) (for a 64-bit tag, with the nonce's last bit cleared, and the half that bit picks) the tag: each 64-bit word of the PDF plus the VHASH word, mod 2^64

As a MAC (mac.bend), with the nonce as the message's first nlen bytes (CrVmac.mac): ~CrVmacKey<K>, ~CrVmac.key(..), ~CrVmac.mac(..), ~CrVmac.verify(..); verify is the interface's named default, so the MAC laws hold for VMAC over every cipher (LAWS.bend: vmac_accepts).

6 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
import ./crypto.bend as Cr
import ./mac.bend as Mc
import ./octets.bend as O

Types

type CrU128 source · line 37 · raw

Data

128-bit numbers (internal)

type CrVmQr source · line 287 · raw

Data

q (the quotient's two low base-2^32 digits) and r, for (x2, x1, x0) divided by 2^32 - 1, one digit at a time

type CrVmIter source · line 312 · raw

Data

one VHASH word's keys: the NH key words, the poly key, the L3 pair

type CrVmacKey source · line 358 · raw

@-K:Data -> Data

A VMAC key: the cipher's key schedule and each VHASH word's keys (one for a 64-bit tag, two for a 128-bit tag; opaque)

Definitions

def cr.vm.carry source · line 40 · raw

@c:Bool -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

def cr.vm.u source · line 47 · raw

@+lo:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> CrU128

def cr.vm.add source · line 51 · raw

@a:CrU128 -> @b:CrU128 -> CrU128

a + b mod 2^128

def cr.vm.w source · line 57 · raw

@+a:U32 -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

def cr.vm.mul source · line 61 · raw

@+a:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @+b:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> CrU128

the 128-bit product of two 64-bit words

def cr.vm.at.go source · line 76 · raw

@xs:List<&2, U32> -> @i:Nat -> U32

def cr.vm.at source · line 87 · raw

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

def cr.vm.u32be source · line 90 · raw

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

def cr.vm.u32le source · line 93 · raw

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

def cr.vm.w64be source · line 97 · raw

@+xs:List<&2, U32> -> @+i:Nat -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

the big-endian and the little-endian 64-bit word at byte i

def cr.vm.w64le source · line 100 · raw

@+xs:List<&2, U32> -> @+i:Nat -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

def cr.vm.word source · line 103 · raw

@be:Bool -> @+xs:List<&2, U32> -> @+i:Nat -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

def cr.vm.words source · line 111 · raw

@n:Nat -> @+be:Bool -> @+xs:List<&2, U32> -> @+i:Nat -> List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64>

n big-endian (be True) or little-endian 64-bit words of xs from byte i

def cr.vm.bytes64 source · line 118 · raw

@+w:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> List<&2, U32>

def cr.vm.nh.go source · line 147 · raw

@ms:List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64> -> @ks:List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64> -> @y:CrU128 -> CrU128

def cr.vm.mod126 source · line 158 · raw

@x:CrU128 -> CrU128

def cr.vm.nh source · line 164 · raw

@+ks:List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64> -> @+p:List<&2, U32> -> CrU128

the NH value of a piece of 1 to 128 bytes (zero-padded to 16 bytes)

def cr.vm.m63 source · line 169 · raw

0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

def cr.vm.fold.pick source · line 172 · raw

@top:Bool -> @+h:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @+l:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> CrU128

def cr.vm.fold source · line 180 · raw

@x:CrU128 -> CrU128

x below 2^128 to a value congruent mod p127 below 2^127 + 1

def cr.vm.norm.pick source · line 185 · raw

@p:Bool -> @+x:CrU128 -> CrU128

def cr.vm.norm source · line 193 · raw

@+x:CrU128 -> CrU128

x (below 2^127 + 1) fully reduced mod p127

def cr.vm.reduce source · line 198 · raw

@+x:CrU128 -> CrU128

def cr.vm.lt source · line 201 · raw

@a:CrU128 -> @b:CrU128 -> Bool

def cr.vm.mm3 source · line 208 · raw

@+x3:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @+x2:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @+x1:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @+x0:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> CrU128

the 256-bit number with 64-bit words x3, x2, x1, x0 reduced mod p127: (x >> 127) + (x mod 2^127)

def cr.vm.mm2 source · line 211 · raw

@b:CrU128 -> @+x1:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @+x0:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> CrU128

def cr.vm.mm1 source · line 216 · raw

@a:CrU128 -> @+x0:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @+mh:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @midc:Bool -> @+p3:CrU128 -> CrU128

def cr.vm.mm0 source · line 221 · raw

@p0:CrU128 -> @mid:CrU128 -> @midc:Bool -> @+p3:CrU128 -> CrU128

def cr.vm.mulmod.go source · line 227 · raw

@p0:CrU128 -> @+p1:CrU128 -> @p2:CrU128 -> @+p3:CrU128 -> CrU128

the 256-bit p3 2^128 + (p1 + p2) 2^64 + p0, reduced mod p127

def cr.vm.mulmod source · line 232 · raw

@a:CrU128 -> @b:CrU128 -> CrU128

a * b mod p127, for a, b below 2^127

def cr.vm.poly.go source · line 239 · raw

@+nk:List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64> -> @+k:CrU128 -> @ps:List<&2, List<&2, U32>> -> @+y:CrU128 -> CrU128

L2 over the NH values of the pieces ps, from y; an empty message is the poly key itself

def cr.vm.poly source · line 246 · raw

@+nk:List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64> -> @+k:CrU128 -> @ps:List<&2, List<&2, U32>> -> CrU128

def cr.vm.p64 source · line 256 · raw

0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

def cr.vm.f64 source · line 260 · raw

@x:CrU128 -> CrU128

x below 2^128, folded: hi 2^64 + lo = hi 257 + lo (mod p64)

def cr.vm.sub64.pick source · line 265 · raw

@ge:Bool -> @+x:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

def cr.vm.last64 source · line 272 · raw

@x:CrU128 -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

def cr.vm.mod64 source · line 279 · raw

@+x:CrU128 -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

x below 2^130 (as hi, lo) mod p64

def cr.vm.digit source · line 282 · raw

@+r:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @+d:U32 -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

def cr.vm.qr source · line 290 · raw

@+x2:U32 -> @+x1:U32 -> @+x0:U32 -> CrVmQr

def cr.vm.l3.go source · line 298 · raw

@qr:CrVmQr -> @+lo:U32 -> @+k1:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @+k2:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

def cr.vm.l3 source · line 304 · raw

@y:CrU128 -> @+k1:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @+k2:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64

L3 of y (below p127) under k1, k2

def cr.vm.l3keys.pick source · line 315 · raw

@ok:Bool -> @+a:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @+b:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @rest:List<&2, CrU128> -> List<&2, CrU128>

def cr.vm.l3keys source · line 323 · raw

@xs:List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64> -> List<&2, CrU128>

the L3 pairs of 64-bit words of the KDF bytes xs both below p64, in order

def cr.vm.nth.pair source · line 330 · raw

@ps:List<&2, CrU128> -> @i:Nat -> CrU128

def cr.vm.u128.of source · line 341 · raw

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

def cr.vm.pmask source · line 345 · raw

@x:CrU128 -> CrU128

the poly key mask: each 32-bit field's top 3 bits cleared

def cr.vm.iter.of source · line 351 · raw

@+nh:List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64> -> @+poly:CrU128 -> @kp:CrU128 -> CrVmIter

def cr.vm.vhash source · line 380 · raw

@its:List<&2, CrVmIter> -> @+ps:List<&2, List<&2, U32>> -> @+lenterm:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64>

VHASH: one word per iter

def cr.vm.sum source · line 390 · raw

@ps:List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64> -> @hs:List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64> -> List<&2, U32>

the tag words: pad word + hash word, mod 2^64

def cr.vm.len source · line 397 · raw

@its:List<&2, CrVmIter> -> Nat

def cr.vm.pdf.last source · line 404 · raw

@one:Bool -> @+b:U32 -> U32

def cr.vm.pdf.pick source · line 411 · raw

@one:Bool -> @odd:Bool -> @+t:List<&2, U32> -> @+words:Nat -> List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64>

def cr.vm.or_empty source · line 450 · raw

@r:Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets> -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets

Templates

template cr.vm.encb source · line 123 · raw

@-K:Data -> @-Blk:Data -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+k:K -> @x:Maybe<&2, Blk> -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> List<&2, U32>

template cr.vm.e source · line 130 · raw

@-K:Data -> @-Blk:Data -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+k:K -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> List<&2, U32>

template cr.vm.kdf.go source · line 133 · raw

@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @blocks:Nat -> @+k:K -> @+index:U32 -> @+c:U32 -> List<&2, U32>

template cr.vm.kdf source · line 142 · raw

@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+k:K -> @+index:U32 -> @+n:Nat -> List<&2, U32>

the first n bytes of the KDF for index

template cr.vm.iters source · line 361 · raw

@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @n:Nat -> @+k:K -> @+i:Nat -> @+l3:List<&2, CrU128> -> List<&2, CrVmIter>

template cr.vm.key.of source · line 370 · raw

@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+words:Nat -> @+k:K -> CrVmacKey<K>

template CrVmac.key source · line 376 · raw

@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-key:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, K>) -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @-words:Nat -> @o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, CrVmacKey<K>>

The key for tags of words 64-bit words (1: VMAC-64, 2: VMAC-128) from a raw key the cipher takes (KDF index 224's first 16 pairs give the L3 keys)

template cr.vm.pdf source · line 423 · raw

@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+k:K -> @+words:Nat -> @+nonce:List<&2, U32> -> List<&2, 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64>

the PDF's words for a nonce of 1 to 15 bytes

template cr.vm.tag.go source · line 431 · raw

@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @kk:CrVmacKey<K> -> @+nonce:List<&2, U32> -> @+m:List<&2, U32> -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets

template cr.vm.tag.pick source · line 437 · raw

@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @ok:Bool -> @kk:CrVmacKey<K> -> @+nonce:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template CrVmac.tag source · line 446 · raw

@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @kk:CrVmacKey<K> -> @+nonce:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

The tag of m under a nonce of 1 to bsize - 1 bytes (CrBadNonceLength otherwise): 8 bytes for a VMAC-64 key, 16 for a VMAC-128 key

template cr.vm.mac source · line 459 · raw

@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @-nlen:Nat -> @kk:CrVmacKey<K> -> @+m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets

As a MAC: the nonce is the message's first nlen bytes (1 to bsize - 1), the data the rest; a message too short for its nonce has the empty tag

template CrVmac.mac source · line 462 · raw

@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @-nlen:Nat -> @kk:CrVmacKey<K> -> @m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets

template CrVmac.verify source · line 466 · raw

@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @-nlen:Nat -> @kk:CrVmacKey<K> -> @m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @t:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Bool

whether t is m's tag (every byte compared; the MAC interface's default)