Documentation

CslibFrontier.Crypto.Primitives.BlockCipher.LengthPrependedCBCMAC

Length-Prepended CBC-MAC over a Uniform Random Function #

This module proves the information-theoretic uniform-random-function step (#6046) toward length-prepended CBC-MAC PRF security ([BS23], Section 6.4.1, Theorem 6.3, pp. 226–229, and Sections 6.6–6.6.1, Theorem 6.8, pp. 235–236; [KL20], Section 4.4, pp. 120–127), bounding length-prepended CBC-MAC over a uniform random function g : BitString n → BitString n rather than a keyed computational PRF. Basic CBC-MAC is secure only for prefix-free message sets; prepending the message block count encoded as an n-bit block makes variable-length block-aligned messages prefix-free ([BS23], Section 6.6.1, pp. 235–236; [KL20], Section 4.4.1–4.4.2 and Chapter 4 Exercises, p. 140ff., interior [KL20] numbering from the existing module notes, see manual/sources.md).

Here block-aligned messages of 0 to L blocks prepend an n-bit block encoding the number of blocks. Every adaptive distinguisher that asks such messages is translated into a distinguisher against basic random-function CBC-MAC on inputs of at most L + 1 blocks whose queries are prefix-free whenever the length encoding is injective. The uniform-random-function step abs_cbcUniformFunctionSuccess_sub_cbcIdealSuccess_le (#6046) then gives the variable-length bound q ^ 2 * (L + 1) ^ 2 / (2 * 2 ^ n).

Main definitions #

Main results #

References #

Section 1: The Length-Prepended Encoding Is Prefix-Free #

@[reducible, inline]

Block-aligned messages of at most L blocks: a block count k ≤ L and k blocks of n bits. The empty message is included.

@[instance_reducible]

Decidable equality on LengthPrependedInput n L, transported from the sigma type.

The list of blocks of a block-aligned message.

A block-aligned message is determined by its list of blocks.

def Cslib.Crypto.prependLength {n L : ℕ} (encodeLength : Fin (L + 1) → BitString n) (x : LengthPrependedInput n L) :
CBCInput n (L + 1)

Prepend the encoded block count encodeLength x.1 to a block-aligned message x, giving a nonempty CBC-MAC input of x.1 + 1 ≤ L + 1 blocks.

@[simp]
theorem Cslib.Crypto.cbcInputBlocks_prependLength {n L : ℕ} (encodeLength : Fin (L + 1) → BitString n) (x : LengthPrependedInput n L) :
cbcInputBlocks (prependLength encodeLength x) = encodeLength x.fst :: lengthPrependedBlocks x
def Cslib.Crypto.lengthPrependedCbcMac {n L : ℕ} (g : BitString n → BitString n) (encodeLength : Fin (L + 1) → BitString n) (x : LengthPrependedInput n L) :

Length-prepended CBC-MAC ([KL20], Section 4.4.1, p. 122, Figure 4.2): basic CBC-MAC of the encoded block count followed by the message blocks.

theorem Cslib.Crypto.lengthPrependedCbcMac_eq {n L : ℕ} (g : BitString n → BitString n) (encodeLength : Fin (L + 1) → BitString n) (x : LengthPrependedInput n L) :
lengthPrependedCbcMac g encodeLength x = cbcMacBlocks g (g (encodeLength x.fst)) (lengthPrependedBlocks x)

Length-prepended CBC-MAC chains the message blocks starting from g (encodeLength x.1).

theorem Cslib.Crypto.prependLength_injective {n L : ℕ} (encodeLength : Fin (L + 1) → BitString n) :

Prepending the block count is injective, for any length encoding.

theorem Cslib.Crypto.not_isPrefix_prependLength {n L : ℕ} {encodeLength : Fin (L + 1) → BitString n} (he : Function.Injective encodeLength) {x y : LengthPrependedInput n L} (hxy : x ≠ y) :

Length prepending is prefix-free ([KL20], Exercise 4.18, p. 143): with an injective length encoding, no length-prepended input is a prefix of a different one.

theorem Cslib.Crypto.isPrefixFreeEncoding_prependLength {n L : ℕ} {encodeLength : Fin (L + 1) → BitString n} (he : Function.Injective encodeLength) :

Method 1: length prepending is a prefix-free encoding ([BS23], Section 6.6.1, p. 235): restates not_isPrefix_prependLength as PRF.IsPrefixFreeEncoding.

theorem Cslib.Crypto.cbcPrefixFree_of_forall_mem_range {n L : ℕ} {encodeLength : Fin (L + 1) → BitString n} (he : Function.Injective encodeLength) {queries : List (CBCInput n (L + 1))} (hq : ∀ z ∈ queries, z ∈ Set.range (prependLength encodeLength)) :

Every list of length-prepended inputs is prefix-free, for an injective length encoding.

def Cslib.Crypto.blockCountBlock {n L : ℕ} (h : L < 2 ^ n) (k : Fin (L + 1)) :

The block count k ≤ L written as the n-bit block with binary value k. [KL20] prepend the bit length |m|; for block-aligned messages the block count carries the same information, and any injective encoding works. This one is injective when L < 2 ^ n.

Section 2: Adaptive Variable-Length Distinguishers #

@[reducible, inline]

The oracle interface of variable-length distinguishers: a query is a block-aligned message of at most L blocks, answered by an n-bit tag.

def Cslib.Crypto.prependLengthQueries {n L : ℕ} {α : Type} (encodeLength : Fin (L + 1) → BitString n) (program : OracleComp (LengthPrependedSpec n L) α) :

Translate a variable-length distinguisher into a CBC-MAC distinguisher that asks the length-prepended version of each query.

@[simp]
theorem Cslib.Crypto.prependLengthQueries_pure {n L : ℕ} {α : Type} (encodeLength : Fin (L + 1) → BitString n) (a : α) :
prependLengthQueries encodeLength (pure a) = pure a
@[simp]
theorem Cslib.Crypto.prependLengthQueries_query_bind {n L : ℕ} {α : Type} (encodeLength : Fin (L + 1) → BitString n) (x : LengthPrependedInput n L) (next : BitString n → OracleComp (LengthPrependedSpec n L) α) :
prependLengthQueries encodeLength (OracleComp.query x >>= next) = do let y ← OracleComp.query (prependLength encodeLength x) prependLengthQueries encodeLength (next y)
theorem Cslib.Crypto.interpret_prependLengthQueries {n L : ℕ} {α : Type} {m : Type → Type u_1} [Monad m] [LawfulMonad m] (encodeLength : Fin (L + 1) → BitString n) (oracle : CBCInput n (L + 1) → m (BitString n)) (program : OracleComp (LengthPrependedSpec n L) α) :
OracleComp.interpret oracle (prependLengthQueries encodeLength program) = OracleComp.interpret (fun (x : LengthPrependedInput n L) => oracle (prependLength encodeLength x)) program

Answering the translated program with a CBC-MAC oracle is answering the original program with the oracle precomposed with prependLength.

theorem Cslib.Crypto.cbcQueryRun_prependLengthQueries {n L q : ℕ} (encodeLength : Fin (L + 1) → BitString n) {program : OracleComp (LengthPrependedSpec n L) Bool} (hq : program.IsQueryBounded q) (oracle : CBCInput n (L + 1) → BitString n) :
(∀ z ∈ (cbcQueryRun oracle (prependLengthQueries encodeLength program)).2, z ∈ Set.range (prependLength encodeLength)) ∧ (cbcQueryRun oracle (prependLengthQueries encodeLength program)).2.length ≤ q

Every query that the translated program asks is length-prepended, and a program that asks at most q queries leads to at most q CBC-MAC queries.

noncomputable def Cslib.Crypto.lengthPrependedUniformFunctionSuccess {n L : ℕ} (encodeLength : Fin (L + 1) → BitString n) (D : PMF (OracleComp (LengthPrependedSpec n L) Bool)) :

Acceptance probability of a distribution D over variable-length distinguishers interacting with lengthPrependedCbcMac g encodeLength over a uniform random block function g : BitString n → BitString n (not a keyed computational PRF; #6046).

Ideal-world acceptance probability of a distribution D over variable-length distinguishers interacting with a uniform random function f : LengthPrependedInput n L → BitString n.

The translated distinguisher has the same uniform-function acceptance probability.

The translated distinguisher has the same ideal-world acceptance probability: a uniform random function on CBC-MAC inputs, precomposed with the injective map prependLength, is a uniform random function on block-aligned messages.

theorem Cslib.Crypto.abs_lengthPrependedUniformFunctionSuccess_sub_lengthPrependedIdealSuccess_le (n q L : ℕ) {encodeLength : Fin (L + 1) → BitString n} (he : Function.Injective encodeLength) (D : PMF (OracleComp (LengthPrependedSpec n L) Bool)) (hD : ∀ (tree : OracleComp (LengthPrependedSpec n L) Bool), D tree ≠ 0 → tree.IsQueryBounded q) :
|lengthPrependedUniformFunctionSuccess encodeLength D - lengthPrependedIdealSuccess n L D| ≤ ↑q ^ 2 * (↑L + 1) ^ 2 / (2 * 2 ^ n)

Information-theoretic uniform-random-function step (#6046) toward length-prepended CBC-MAC PRF security ([BS23], Section 6.4.1, Theorem 6.3, pp. 226–229, and Sections 6.6–6.6.1, Theorem 6.8, pp. 235–236; [KL20], Section 4.4, pp. 120–127), bounding length-prepended CBC-MAC over a uniform random function g : BitString n → BitString n rather than a keyed computational PRF: for an injective length encoding and every distribution D over distinguishers that ask at most q block-aligned messages of at most L blocks, length-prepended CBC-MAC with a uniform random function is within q ^ 2 * (L + 1) ^ 2 / (2 * 2 ^ n) of a uniform random function on such messages.