Documentation

CslibFrontier.Crypto.Computational.Encryption.CTRMode

CPA Security of Counter (CTR) Mode #

Counter mode ([KL20], Section 3.6.3, Figure 3.9) encrypts a message of k blocks under a block function f, a counter schedule counter : BitString n → ℕ → BitString n, and a uniform initialization vector iv by XORing the keystream f (counter iv 0), f (counter iv 1), … with the plaintext blocks. [KL20] (Section 3.6.3, Theorem 3.33, p. 93) proves that CTR mode is CPA-secure whenever f is a pseudorandom function.

The security notion is the left-or-right multiple-encryption experiment of [KL20], Definition 3.22, using the shared block-mode query/cost/ideal-experiment interface (BlockModeQuery, BlockModeAdversary, blockModeSelect, blockModeBlocks, blockModeCost, blockModeIdealExperiment) from RandomFunctionHybrid.lean.

The proof works for any counter schedule counter : BitString n → ℕ → BitString n satisfying within-query distinctness (∀ iv i j, i < ℓ → j < ℓ → i ≠ j → counter iv i ≠ counter iv j) and per-offset injectivity in the IV (∀ i < ℓ, Function.Injective (fun iv => counter iv i)), and is instantiated on the canonical modular counter schedule ctrModCounter when ℓ ≤ 2 ^ n:

  1. The CTR experiment is the real PRF experiment for a reduction encrypting with its oracle (ctrCpaGame_eq_realExperiment), making at most K oracle calls (isQueryBounded_ctrReduction).
  2. Against a lazily sampled uniform random function, the reduction's view differs from fresh uniform ciphertexts only when some counter iv i hits an earlier queried input. Within-query distinctness rules out intra-query collisions, and per-offset injectivity bounds inter-query collisions by K ^ 2 / 2 ^ n (advantage_idealExperiment_ctrReduction_le).
  3. Combining the ideal hybrids with the two PRF advantages yields the concrete bound (advantage_ctrCpaGame_le, and advantage_ctrCpaGame_ctrModCounter_le for ctrModCounter).

The PRF experiments are the concrete ones of Cslib.Crypto.PRF ([BS23], Chapter 4). The reductions here are typed ProbOracleComp programs without an efficiency certificate. The uniform statement, with a certified word-level reduction and the asymptotic theorem PseudorandomFunction.indcpa_ctrWordEncrypt, is in CslibFrontier/Crypto/Computational/Encryption/CTRMode/Uniform.lean, which reuses advantage_idealExperiment_ctrReduction_le.

Main definitions #

Main results #

References #

Section 1: The CTR Left-or-Right Experiment #

noncomputable def Cslib.Crypto.ctrOracle {n : ℕ} (counter : BitString n → ℕ → BitString n) (ℓ : ℕ) (f : BitString n → BitString n) (b : Bool) (q : BlockModeQuery n) :

The CTR left-or-right oracle for counter schedule counter, block function f, and challenge bit b ([KL20], Section 3.6.3, Figure 3.9).

noncomputable def Cslib.Crypto.ctrCpaGame {n : ℕ} {Key : Type} (counter : BitString n → ℕ → BitString n) [Fintype Key] [Nonempty Key] (F : Key → BitString n → BitString n) (ℓ : ℕ) (b : Bool) (adversary : BlockModeAdversary n) :

The CTR left-or-right experiment for keyed block function F and challenge bit b ([KL20], Definition 3.22).

Section 2: The Reduction to a Pseudorandom Function #

def Cslib.Crypto.ctrMaskQueriesFrom {n : ℕ} (counter : BitString n → ℕ → BitString n) (iv : BitString n) :
ℕ → ℕ → ProbOracleComp (fun (x : BitString n) => BitString n) (List (BitString n))

Query the function oracle on counter iv i, counter iv (i + 1), …, counter iv (i + k - 1).

noncomputable def Cslib.Crypto.ctrQueryProgram {n : ℕ} (counter : BitString n → ℕ → BitString n) (ℓ : ℕ) (b : Bool) (q : BlockModeQuery n) :

One CTR encryption computed with a function oracle: sample a uniform IV, evaluate the keystream masks via oracle queries starting at offset 0, and XOR with the selected plaintext.

noncomputable def Cslib.Crypto.ctrReduction {n : ℕ} (counter : BitString n → ℕ → BitString n) (ℓ : ℕ) (b : Bool) (adversary : BlockModeAdversary n) :

The PRF distinguisher built from a CTR left-or-right adversary ([KL20], proof of Theorem 3.33).

theorem Cslib.Crypto.run_ctrMaskQueriesFrom {n : ℕ} (f : BitString n → BitString n) (counter : BitString n → ℕ → BitString n) (iv : BitString n) (i k : ℕ) :
((ctrMaskQueriesFrom counter iv i k).run fun (y : BitString n) => pure (f y)) = pure (List.map (fun (j : ℕ) => f (counter iv j)) (List.range' i k))

Answering ctrMaskQueriesFrom counter iv i k with a fixed function f evaluates (List.range' i k).map (fun j => f (counter iv j)).

theorem Cslib.Crypto.run_ctrMaskQueriesFrom_zero {n : ℕ} (f : BitString n → BitString n) (counter : BitString n → ℕ → BitString n) (iv : BitString n) (k : ℕ) :
((ctrMaskQueriesFrom counter iv 0 k).run fun (y : BitString n) => pure (f y)) = pure (ctrMaskBlocks f counter iv k)

Answering ctrMaskQueriesFrom counter iv 0 k with f computes ctrMaskBlocks f counter iv k.

theorem Cslib.Crypto.ctrCpaGame_eq_realExperiment {n : ℕ} {Key : Type} (counter : BitString n → ℕ → BitString n) [Fintype Key] [Nonempty Key] (F : Key → BitString n → BitString n) (ℓ : ℕ) (b : Bool) (adversary : BlockModeAdversary n) :
ctrCpaGame counter F ℓ b adversary = (PRF.realExperiment F (ctrReduction counter ℓ b adversary)).eval

The CTR experiment for keyed function F equals the real PRF experiment for F and ctrReduction counter ℓ b adversary.

theorem Cslib.Crypto.isQueryBounded_ctrMaskQueriesFrom {n : ℕ} (counter : BitString n → ℕ → BitString n) (iv : BitString n) (i k : ℕ) :

ctrMaskQueriesFrom counter iv i k makes at most k oracle queries.

theorem Cslib.Crypto.isQueryBounded_ctrReduction {n : ℕ} (counter : BitString n → ℕ → BitString n) (ℓ : ℕ) (b : Bool) {adversary : BlockModeAdversary n} {K : ℕ} (h : OracleComp.IsCostBounded (blockModeCost ℓ) adversary K) :
ProbOracleComp.IsQueryBounded (ctrReduction counter ℓ b adversary) K

If adversary asks at most K blocks in total, ctrReduction counter ℓ b adversary makes at most K oracle calls.

Section 3: Inter-Query Collision Bound for Fresh CTR Sampling #

Section 4: Security of CTR Mode and Modular Counter Instantiation #

theorem Cslib.Crypto.advantage_idealExperiment_ctrReduction_le {n : ℕ} (counter : BitString n → ℕ → BitString n) (ℓ : ℕ) (b : Bool) {adversary : BlockModeAdversary n} {K : ℕ} (h : OracleComp.IsCostBounded (blockModeCost ℓ) adversary K) (hintra : ∀ (iv : BitString n) (i j : ℕ), i < ℓ → j < ℓ → i ≠ j → counter iv i ≠ counter iv j) (hiv : ∀ i < ℓ, Function.Injective fun (iv : BitString n) => counter iv i) :
↑(Game.advantage (PRF.idealExperiment (ctrReduction counter ℓ b adversary)).eval (blockModeIdealExperiment ℓ adversary).eval) ≤ ↑K ^ 2 / 2 ^ n

The CTR reduction against a uniform random function. If the queries of adversary ask at most K blocks in total, and counter has no intra-query collisions below ℓ and is injective in iv at every offset below ℓ, then the reduction's output is within K ^ 2 / 2 ^ n of blockModeIdealExperiment for each challenge bit b.

theorem Cslib.Crypto.advantage_ctrCpaGame_randomFunction_le {n : ℕ} (counter : BitString n → ℕ → BitString n) (ℓ : ℕ) {adversary : BlockModeAdversary n} {K : ℕ} (h : OracleComp.IsCostBounded (blockModeCost ℓ) adversary K) (hintra : ∀ (iv : BitString n) (i j : ℕ), i < ℓ → j < ℓ → i ≠ j → counter iv i ≠ counter iv j) (hiv : ∀ i < ℓ, Function.Injective fun (iv : BitString n) => counter iv i) :
↑((ctrCpaGame counter (fun (f : BitString n → BitString n) => f) ℓ false adversary).advantage (ctrCpaGame counter (fun (f : BitString n → BitString n) => f) ℓ true adversary)) ≤ 2 * (↑K ^ 2 / 2 ^ n)

CTR mode with a uniform random function ([KL20], Section 3.6.3): when adversary asks at most K blocks in total and at most ℓ blocks per query, its left-or-right advantage is at most 2 * K ^ 2 / 2 ^ n.

theorem Cslib.Crypto.advantage_ctrCpaGame_le {n : ℕ} {Key : Type} (counter : BitString n → ℕ → BitString n) [Fintype Key] [Nonempty Key] (F : Key → BitString n → BitString n) (ℓ : ℕ) {adversary : BlockModeAdversary n} {K : ℕ} (h : OracleComp.IsCostBounded (blockModeCost ℓ) adversary K) (hintra : ∀ (iv : BitString n) (i j : ℕ), i < ℓ → j < ℓ → i ≠ j → counter iv i ≠ counter iv j) (hiv : ∀ i < ℓ, Function.Injective fun (iv : BitString n) => counter iv i) :
↑((ctrCpaGame counter F ℓ false adversary).advantage (ctrCpaGame counter F ℓ true adversary)) ≤ ↑(PRF.advantage F (ctrReduction counter ℓ false adversary)) + ↑(PRF.advantage F (ctrReduction counter ℓ true adversary)) + 2 * (↑K ^ 2 / 2 ^ n)

CTR mode is CPA-secure for a pseudorandom function ([KL20], Section 3.6.3, Theorem 3.33, p. 93): if adversary asks at most K blocks in total and at most ℓ blocks per query, its left-or-right advantage against CTR mode with keyed function F is at most the PRF advantages of the two reductions ctrReduction counter ℓ false adversary and ctrReduction counter ℓ true adversary (each making at most K queries by isQueryBounded_ctrReduction) plus 2 * K ^ 2 / 2 ^ n.

Canonical modular-addition counter schedule iv + i mod 2 ^ n ([KL20], Figure 3.9), via BitString.equivFin.

For every offset i, ctrModCounter · i is injective on the initialization vector iv.

theorem Cslib.Crypto.ctrModCounter_injOn {n ℓ : ℕ} (hℓ : ℓ ≤ 2 ^ n) (iv : BitString n) {i j : ℕ} (hi : i < ℓ) (hj : j < ℓ) (hij : i ≠ j) :

Whenever ℓ ≤ 2 ^ n, distinct offsets below ℓ yield distinct ctrModCounter iv i blocks.

theorem Cslib.Crypto.advantage_ctrCpaGame_ctrModCounter_randomFunction_le {n ℓ : ℕ} (hℓ : ℓ ≤ 2 ^ n) {adversary : BlockModeAdversary n} {K : ℕ} (h : OracleComp.IsCostBounded (blockModeCost ℓ) adversary K) :
↑((ctrCpaGame ctrModCounter (fun (f : BitString n → BitString n) => f) ℓ false adversary).advantage (ctrCpaGame ctrModCounter (fun (f : BitString n → BitString n) => f) ℓ true adversary)) ≤ 2 * (↑K ^ 2 / 2 ^ n)

Modular CTR mode with a uniform random function when ℓ ≤ 2 ^ n.

theorem Cslib.Crypto.advantage_ctrCpaGame_ctrModCounter_le {n : ℕ} {Key : Type} [Fintype Key] [Nonempty Key] (F : Key → BitString n → BitString n) {ℓ : ℕ} (hℓ : ℓ ≤ 2 ^ n) {adversary : BlockModeAdversary n} {K : ℕ} (h : OracleComp.IsCostBounded (blockModeCost ℓ) adversary K) :
↑((ctrCpaGame ctrModCounter F ℓ false adversary).advantage (ctrCpaGame ctrModCounter F ℓ true adversary)) ≤ ↑(PRF.advantage F (ctrReduction ctrModCounter ℓ false adversary)) + ↑(PRF.advantage F (ctrReduction ctrModCounter ℓ true adversary)) + 2 * (↑K ^ 2 / 2 ^ n)

Modular CTR mode is CPA-secure for any pseudorandom function F whenever ℓ ≤ 2 ^ n ([KL20], Section 3.6.3, Theorem 3.33).