seqiv.bend checks
raw source on the hub · import bend-osdev-aead@0.1.0.0/seqiv.bend as Seqiv
seqiv: an AEAD whose nonce is made from a sequence number, sent in front of the ciphertext (the Linux kernel's "seqiv(aead)", as IPsec ESP uses it with GCM and CCM). The sender keeps a counter (the packet's sequence number); the nonce is the counter, right-aligned in the nonce's length and XORed with a secret salt from the key material, as RFC 8446 (TLS 1.3), section 5.3, forms its per-record nonce, and as RFC 5116, section 3.2, recommends (a counter, so that no nonce repeats under one key). The nonce travels with the ciphertext, as ESP's explicit IV does, so the receiver needs no counter: IV = salt xor (0 .. 0 || seq), sealed: IV || AEAD.seal(K, IV, A || IV, P) opened: the first ivlen bytes are the IV, AEAD.open(K, IV, A || IV, the rest) The IV is part of the AEAD's associated data, so it is authenticated even under an AEAD that does not bind its nonce (Encrypt-then-MAC over A || C, authenc.bend's kernel layout), as ESP authenticates its IV (RFC 4303, section 3.3.2.1, with the IV part of the payload the ICV covers). The salt is the last ivlen bytes of the raw key (as RFC 4106's GCM in ESP takes its salt from the end of the key material); the kernel's seqiv draws its salt at random, so its IVs differ from these, but a receiver reads the IV from the packet, and the two interoperate.
Over any AEAD of the crypto package's AEAD interface (~KA, ~akey, ~seal, ~open) with an ivlen-byte nonce. Laws (LAWS.bend): open accepts what seal produced wherever the AEAD's open accepts what its seal produced, at the generated IV (seqiv_round_trip_if).
4 imports
import Base import bend-osdev-bytes@0.1.0.0/bytes.bend as B import bend-osdev-crypto@0.1.0.0/crypto.bend as Cr import bend-osdev-crypto@0.1.0.0/octets.bend as O
Types
type AdSeqivKey source · line 31 · raw
@-KA:Data -> Data
The key: the AEAD's key and the salt
AdSeqivKey@-KA:Data -> @ka:KA -> @salt:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> AdSeqivKey<KA>
Templates
template ad.sq.ka source · line 34 · raw
@-KA:Data -> @k:AdSeqivKey<KA> -> KA
template AdSeqiv.iv source · line 41 · raw
@-KA:Data -> @k:AdSeqivKey<KA> -> @+seq:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
The IV of sequence number seq (big-endian bytes, at most as long as the salt): the salt XOR seq, right-aligned
template ad.sq.key source · line 46 · raw
@-KA:Data -> @-akey:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, KA>) -> @+ivlen:Nat -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, AdSeqivKey<KA>>
template AdSeqiv.key source · line 55 · raw
@-KA:Data -> @-akey:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, KA>) -> @-ivlen:Nat -> @o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, AdSeqivKey<KA>>
The key from raw key material: the AEAD's key, then an ivlen-byte salt
template ad.sq.seal source · line 58 · raw
@-KA:Data -> @-seal:(@_:KA -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>) -> @+k:AdSeqivKey<KA> -> @+seq:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @aad:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @p:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
template AdSeqiv.seal source · line 63 · raw
@-KA:Data -> @-seal:(@_:KA -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>) -> @k:AdSeqivKey<KA> -> @seq:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @aad:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @p:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
p sealed under the sequence number seq: IV || the AEAD's ciphertext
template ad.sq.open.go source · line 66 · raw
@-KA:Data -> @-open:(@_:KA -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>) -> @+k:AdSeqivKey<KA> -> @aad:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @+n:Nat -> @s:Maybe<&2, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.OctetsSplit> -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
template ad.sq.open source · line 75 · raw
@-KA:Data -> @-open:(@_:KA -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>) -> @-ivlen:Nat -> @+k:AdSeqivKey<KA> -> @seq:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @aad:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @+c:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
IV || C opened (the IV the first ivlen bytes; the sequence number is not needed): the AEAD's open of C under that IV and A || IV, or CrTooShort
template AdSeqiv.open source · line 78 · raw
@-KA:Data -> @-open:(@_:KA -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>) -> @-ivlen:Nat -> @k:AdSeqivKey<KA> -> @seq:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @aad:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @c:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>