Documentation

CslibFrontier.Crypto.Primitives.BlockCipher.CBCMAC

CBC-MAC Finite Uniform-Random-Function Bounds and Forgery Separations #

Formalizes the Cipher Block Chaining Message Authentication Code (CBC-MAC) over BitString n, its prefix-free smoothness, the information-theoretic uniform-random-function step abs_cbcUniformFunctionSuccess_sub_cbcIdealSuccess_le (#6046) toward prefix-free CBC-MAC PRF security ([BS23], Section 6.4.1, Theorem 6.3, pp. 226–229; [KL20], Section 4.4.2, pp. 123–127) bounding cbcMacFinite g for a uniform random function g : BitString n → BitString n rather than a keyed computational PRF, and explicit forgery counterexamples for six relaxations of basic CBC-MAC ([KL20], Section 4.4, pp. 120–127, and Chapter 4 Exercises, p. 140ff.; interior [KL20] numbering from the existing module notes, see manual/sources.md).

Main definitions #

Main results #

References #

Section 1: Basic CBC-MAC Chaining ([KL20], Section 4.4, Construction 4.9) #

def Cslib.Crypto.cbcMacBlocks {n : ℕ} (g : BitString n → BitString n) (iv : BitString n) (msg : List (BitString n)) :

CBC-MAC chaining ([KL20], Section 4.4, Construction 4.9): folds the CBC step state ↦ g (state + block) across a list of n-bit blocks starting from iv (which is 0 in standard basic CBC-MAC).

@[simp]
theorem Cslib.Crypto.cbcMacBlocks_nil {n : ℕ} (g : BitString n → BitString n) (iv : BitString n) :
cbcMacBlocks g iv [] = iv
@[simp]
theorem Cslib.Crypto.cbcMacBlocks_cons {n : ℕ} (g : BitString n → BitString n) (iv b : BitString n) (bs : List (BitString n)) :
cbcMacBlocks g iv (b :: bs) = cbcMacBlocks g (g (iv + b)) bs
@[simp]
theorem Cslib.Crypto.cbcMacBlocks_singleton {n : ℕ} (g : BitString n → BitString n) (iv b : BitString n) :
cbcMacBlocks g iv [b] = g (iv + b)
@[simp]
theorem Cslib.Crypto.cbcMacBlocks_append {n : ℕ} (g : BitString n → BitString n) (iv : BitString n) (xs ys : List (BitString n)) :
cbcMacBlocks g iv (xs ++ ys) = cbcMacBlocks g (cbcMacBlocks g iv xs) ys

Chaining identity for cbcMacBlocks: processing xs ++ ys from iv is identical to processing ys from the intermediate tag cbcMacBlocks g iv xs.

theorem Cslib.Crypto.getLast_cbcEncryptBlocks_eq_cbcMacBlocks {n : ℕ} (g : BitString n → BitString n) (iv b : BitString n) (bs : List (BitString n)) :
(cbcEncryptBlocks g iv (b :: bs)).getLast ⋯ = cbcMacBlocks g iv (b :: bs)

The final block of multi-block CBC encryption (cbcEncryptBlocks) on a nonempty message equals the cbcMacBlocks tag from the same initial state iv.

@[reducible, inline]
abbrev Cslib.Crypto.CBCInput (n ℓ : ℕ) :

Nonempty block vectors of length between 1 and ℓ blocks (Σ k : Fin ℓ, Fin (k + 1) → BitString n), matching the non-empty bounded-length domain of [KL20], Section 4.4.

@[instance_reducible]

Decidable equality on CBCInput n ℓ, transported from the sigma type. Instance search does not find it through the abbrev inside compound types such as List (CBCInput n ℓ × BitString n).

def Cslib.Crypto.cbcInputBlocks {n ℓ : ℕ} (x : CBCInput n ℓ) :

Convert a CBCInput n ℓ into its underlying nonempty list of n-bit blocks.

@[simp]
theorem Cslib.Crypto.length_cbcInputBlocks {n ℓ : ℕ} (x : CBCInput n ℓ) :
def Cslib.Crypto.cbcMacFinite {n ℓ : ℕ} (g : BitString n → BitString n) (x : CBCInput n ℓ) :

Basic CBC-MAC with zero initial state on a bounded nonempty block input CBCInput n ℓ.

Section 2: Prefix-Free CBC-MAC Smoothness ([KL20], Claim 4.13) #

def Cslib.Crypto.CBCPrefixFree {n ℓ : ℕ} (queries : List (CBCInput n ℓ)) :

A list of CBCInput n ℓ queries is prefix-free if no two distinct inputs have one block list as a prefix of the other ([KL20], Section 4.4, p. 124).

def Cslib.Crypto.CBCSmooth (n q ℓ : ℕ) (δ : ℝ) :

CBC smoothness ([KL20], Section 4.4, p. 125): CBCSmooth n q ℓ δ states that for every injective family of q prefix-free queries of at most ℓ blocks and every assignment of target tags tags : Fin q → BitString n, a uniform random function g : BitString n → BitString n satisfies ∀ i, cbcMacFinite g (inputs i) = tags i with probability at least (1 - δ) * 2^(-n * q).

theorem Cslib.Crypto.cbcSmooth_iff (n q ℓ : ℕ) (δ : ℝ) :
CBCSmooth n q ℓ δ ↔ ∀ (inputs : Fin q → CBCInput n ℓ), Function.Injective inputs → CBCPrefixFree (List.ofFn inputs) → ∀ (tags : Fin q → BitString n), (1 - δ) * 2 ^ (-↑(n * q)) ≤ ↑{g : BitString n → BitString n | ∀ (i : Fin q), cbcMacFinite g (inputs i) = tags i}.card / ↑(Fintype.card (BitString n → BitString n))

Counting characterization of CBCSmooth.

theorem Cslib.Crypto.cbcSmooth_of_prefixFree (n q ℓ : ℕ) :
CBCSmooth n q ℓ (↑q ^ 2 * ↑ℓ ^ 2 / (2 * 2 ^ n))

A sharpened form of [KL20], Section 4.4, Claim 4.13 (p. 125): random-function CBC-MAC is (q, ℓ, q^2 * ℓ^2 / (2 * 2^n))-smooth on prefix-free queries.

Section 3: Uniform-Random-Function Step Toward [KL20], Theorem 4.12 (#6046) #

@[reducible, inline]

The oracle interface of CBC-MAC distinguishers: a query is a bounded nonempty block input CBCInput n ℓ, answered by an n-bit tag.

def Cslib.Crypto.cbcQueryRun {n ℓ : ℕ} {α : Type} (oracle : CBCInput n ℓ → BitString n) (program : OracleComp (CBCOracleSpec n ℓ) α) :
α × List (CBCInput n ℓ)

Run an oracle program against a fixed oracle CBCInput n ℓ → BitString n, interpreting each call with OracleComp.interpret in the writer monad that logs the query. Returns the output together with the list of queried inputs, in order and with repetitions.

@[simp]
theorem Cslib.Crypto.cbcQueryRun_pure {n ℓ : ℕ} {α : Type} (oracle : CBCInput n ℓ → BitString n) (a : α) :
cbcQueryRun oracle (pure a) = (a, [])
theorem Cslib.Crypto.cbcQueryRun_liftBind {n ℓ : ℕ} {α : Type} (oracle : CBCInput n ℓ → BitString n) (x : CBCInput n ℓ) (next : BitString n → OracleComp (CBCOracleSpec n ℓ) α) :
cbcQueryRun oracle (PFunctor.FreeM.liftBind x next) = ((cbcQueryRun oracle (next (oracle x))).1, x :: (cbcQueryRun oracle (next (oracle x))).2)

One oracle call: the output and log of the continuation on the answer, after the query.

@[simp]
theorem Cslib.Crypto.cbcQueryRun_query_bind {n ℓ : ℕ} {α : Type} (oracle : CBCInput n ℓ → BitString n) (x : CBCInput n ℓ) (next : BitString n → OracleComp (CBCOracleSpec n ℓ) α) :
cbcQueryRun oracle (OracleComp.query x >>= next) = ((cbcQueryRun oracle (next (oracle x))).1, x :: (cbcQueryRun oracle (next (oracle x))).2)
theorem Cslib.Crypto.cbcQueryRun_fst {n ℓ : ℕ} {α : Type} (oracle : CBCInput n ℓ → BitString n) (program : OracleComp (CBCOracleSpec n ℓ) α) :
(cbcQueryRun oracle program).1 = OracleComp.interpret oracle program

The output of cbcQueryRun is the plain interpretation of the program with the oracle.

def Cslib.Crypto.CBCAdmissible {n ℓ : ℕ} (q : ℕ) (tree : OracleComp (CBCOracleSpec n ℓ) Bool) :

An oracle program is q-admissible if, run against every fixed oracle, its queries are prefix-free and it makes at most q of them. Quantifying over executions keeps paths that no consistent oracle produces (inconsistent answers to repeated queries) out of the restriction.

noncomputable def Cslib.Crypto.cbcUniformFunctionSuccess (n ℓ : ℕ) (D : PMF (OracleComp (CBCOracleSpec n ℓ) Bool)) :

Acceptance probability of a distribution D over adaptive oracle programs when interacting with cbcMacFinite g over a uniform random block function g : BitString n → BitString n (not a keyed computational PRF; #6046).

noncomputable def Cslib.Crypto.cbcIdealSuccess (n ℓ : ℕ) (D : PMF (OracleComp (CBCOracleSpec n ℓ) Bool)) :

Ideal-world acceptance probability of a distribution D over adaptive oracle programs when interacting with a uniform random function f : CBCInput n ℓ → BitString n.

theorem Cslib.Crypto.abs_cbcUniformFunctionSuccess_sub_cbcIdealSuccess_le (n q ℓ : ℕ) (D : PMF (OracleComp (CBCOracleSpec n ℓ) Bool)) (hD : ∀ (tree : OracleComp (CBCOracleSpec n ℓ) Bool), D tree ≠ 0 → CBCAdmissible q tree) :
|cbcUniformFunctionSuccess n ℓ D - cbcIdealSuccess n ℓ D| ≤ ↑q ^ 2 * ↑ℓ ^ 2 / (2 * 2 ^ n)

Information-theoretic uniform-random-function step (#6046) toward prefix-free CBC-MAC PRF security ([BS23], Section 6.4.1, Theorem 6.3, pp. 226–229; [KL20], Section 4.4.2, pp. 123–127), bounding cbcMacFinite g for a uniform random function g : BitString n → BitString n rather than a keyed computational PRF: for every block width n, query budget q, maximum block length ℓ, and distribution D over q-admissible adaptive oracle programs, the distinguishing advantage between cbcMacFinite g and a uniform random function f : CBCInput n ℓ → BitString n is at most q^2 * ℓ^2 / (2 * 2^n).

Section 4: Explicit CBC-MAC Forgery Attacks ([KL20], Exercises 4.13–4.16) #

structure Cslib.Crypto.ForgeryTrace (Message : Type u_1) (Tag : Type u_2) :
Type (max u_1 u_2)

The trace of a deterministic MAC forgery attack: the messages it queried, and the message and tag it outputs.

  • queries : List Message

    The messages submitted to the tagging oracle.

  • message : Message

    The forged message.

  • tag : Tag

    The tag claimed for message.

def Cslib.Crypto.ForgeryTrace.Valid {Message : Type u_1} {Tag : Type u_2} (trace : ForgeryTrace Message Tag) (mac : Message → Tag) :

A forgery trace is valid against the tagging function mac if its message was never queried and mac gives it the claimed tag.

Two-block-to-four-block length-extension attack ([KL20], Exercise 4.13(a), p. 142): queries [0, 0] to obtain tag t = g(g(0)), then forges [0, 0, t, 0] with the same tag t.

theorem Cslib.Crypto.cbcTwoToFourAttack_valid {n : ℕ} (g : BitString n → BitString n) :
have trace := cbcTwoToFourAttack (cbcMacBlocks g 0); trace.queries.length = 1 ∧ (∀ q ∈ trace.queries, q.length = 2) ∧ trace.message.length = 4 ∧ trace.Valid (cbcMacBlocks g 0)

cbcTwoToFourAttack succeeds with probability 1 against every round function g using a single 2-block query and forging a fresh 4-block message ([KL20], Exercise 4.13(a), p. 142).

One-and-two-block-to-three-block splicing attack ([KL20], Exercise 4.13(b), p. 142): queries [0] (getting t = g(0)) and [0, 0] (getting u = g(g(0))), then forges [0, t, 0] with tag u.

theorem Cslib.Crypto.cbcToThreeAttack_valid {n : ℕ} (g : BitString n → BitString n) :
have trace := cbcToThreeAttack (cbcMacBlocks g 0); trace.queries.length = 2 ∧ trace.message.length = 3 ∧ trace.Valid (cbcMacBlocks g 0)

cbcToThreeAttack succeeds with probability 1 against every round function g using two queries ([0] and [0, 0]) and forging the fresh 3-block message [0, g(0), 0] ([KL20], Exercise 4.13(b), p. 142).

Insecure two-block CBC-MAC variant that outputs both intermediate chaining values (g(m₁), g(g(m₁) + m₂)) ([KL20], Exercise 4.14(a), p. 142).

Leaked-intermediate-tags forgery attack ([KL20], Exercise 4.14(a), p. 142): queries (0, 0), (1^n, 0), and (1^n, 1^n), and uses the leaked first-block tags to forge a fresh 2-block message: (0, 1^n) with tag c when a.1 = b.1, or (0, a.1 + b.1) with tag (a.1, b.2) otherwise.

theorem Cslib.Crypto.cbcLeakedTagsAttack_valid {n : ℕ} (hn : 0 < n) (g : BitString n → BitString n) :

cbcLeakedTagsAttack makes 3 queries and outputs a valid fresh 2-block forgery against cbcAllTagsTwo g for every round function g whenever 0 < n ([KL20], Exercise 4.14(a), p. 142).

Insecure random-IV CBC-MAC variant that outputs (iv, cbcMacBlocks g iv msg) ([KL20], Exercise 4.14(b), p. 142).

Random-IV CBC-MAC forgery attack ([KL20], Exercise 4.14(b), p. 142): given (iv, t) for message [0], outputs message 1^n with modified IV iv + 1^n and tag t.

theorem Cslib.Crypto.cbcRandomIVAttack_valid {n : ℕ} (hn : 0 < n) (g : BitString n → BitString n) (iv : BitString n) :
match cbcRandomIVAttack (cbcRandomIVMac g iv [0]) with | (message, iv', tag) => message ≠ 0 ∧ cbcRandomIVMac g iv' [message] = (iv', tag)

cbcRandomIVAttack produces a fresh 1-block message 1^n ≠ 0 whose random-IV CBC-MAC tag (cbcRandomIVMac) validates for every iv and g whenever 0 < n ([KL20], Exercise 4.14(b), p. 142).

def Cslib.Crypto.lengthAppendedCBC {n : ℕ} (g : BitString n → BitString n) (encodeLength : ℕ → List (BitString n)) (msg : List (BitString n)) :

Insecure CBC-MAC variant that appends encodeLength (msg.length * n) at the end of msg ([KL20], Exercise 4.15, p. 142).

def Cslib.Crypto.cbcLengthSuffixAttack {n : ℕ} (encodeLength : ℕ → List (BitString n)) (oracle : List (BitString n) → BitString n) :

Append-length CBC-MAC forgery attack ([KL20], Exercise 4.15, p. 142): queries [0], [1^n], and [0] ++ encodeLength n ++ [t], and forges [1^n] ++ encodeLength n ++ [u].

theorem Cslib.Crypto.cbcLengthSuffixAttack_valid_any_encoding {n : ℕ} (hn : 0 < n) (encodeLength : ℕ → List (BitString n)) (g : BitString n → BitString n) :
have trace := cbcLengthSuffixAttack encodeLength (lengthAppendedCBC g encodeLength); trace.queries.length = 3 ∧ trace.Valid (lengthAppendedCBC g encodeLength)

cbcLengthSuffixAttack succeeds with 3 queries against lengthAppendedCBC for every encodeLength and g whenever 0 < n ([KL20], Exercise 4.15, p. 142).

theorem Cslib.Crypto.cbcLengthSuffixAttack_valid {n : ℕ} (hn : 4 ≤ n) (encodeBlock : ℕ → BitString n) (g : BitString n → BitString n) :
have encodeLength := fun (len : ℕ) => [encodeBlock len]; have trace := cbcLengthSuffixAttack encodeLength (lengthAppendedCBC g encodeLength); trace.queries.length = 3 ∧ (∀ q ∈ trace.queries, q.length * n < 2 ^ n) ∧ trace.message.length * n < 2 ^ n ∧ trace.Valid (lengthAppendedCBC g encodeLength)

For a single-block length encoding and 4 ≤ n, all queries and the forged message of cbcLengthSuffixAttack also satisfy the < 2^n bit-length bound ([KL20], Exercise 4.15, p. 142).

def Cslib.Crypto.zeroPadCBC {n : ℕ} (g : BitString n → BitString n) (ℓ : ℕ) (m : List Bool) :

Insecure zero-padded CBC-MAC for messages of length at most ℓ * 2^n bits: right-pad with false bits to length ℓ * 2^n (List.rightpad), then split into the (ℓ * 2^n) / n blocks of BitString.padBlocks (the floor block count; exact when n ∣ ℓ * 2^n) ([KL20], Exercise 4.16, p. 142).

Zero-padded CBC-MAC forgery attack ([KL20], Exercise 4.16, p. 142): queries [true] and outputs the distinct message [true, false] with the same tag.

For any L ≥ 2, right-padding [true] and [true, false] with false to length L produces the same bit string.

theorem Cslib.Crypto.zeroPadAttack_valid {n ℓ : ℕ} (hn : 0 < n) (hℓ : 0 < ℓ) (g : BitString n → BitString n) :
have trace := zeroPadAttack (zeroPadCBC g ℓ); trace.queries.length = 1 ∧ (∀ q ∈ trace.queries, q.length ≤ ℓ * 2 ^ n) ∧ trace.message.length ≤ ℓ * 2 ^ n ∧ trace.Valid (zeroPadCBC g ℓ)

zeroPadAttack succeeds with 1 query against zeroPadCBC for every 0 < n, 0 < ℓ, and g ([KL20], Exercise 4.16, p. 142).