Documentation

CslibFrontier.Crypto.Primitives.BlockCipher.Modes

Symmetric block-cipher modes of operation, collision lemmas, and mode separations #

Block-cipher and pseudorandom-function modes of operation ([KL20], Sections 3.6.2–3.6.4) lift a fixed-width block function f : BitString n → BitString n (or permutation π : Equiv.Perm (BitString n)) to multi-block messages using an initialization vector iv : BitString n. Throughout this module, BitString n is equipped with Mathlib's coordinatewise XOR group structure (+ via BooleanRing Bool, where every element is its own inverse, BitString.add_self). Cipher Feedback (CFB) mode is included alongside CBC, OFB, and CTR as a standard self-synchronizing feedback mode (not covered in [KL20]).

Main definitions #

Main results #

References #

Multi-block modes of operation (CBC, OFB, CFB, CTR) #

Cipher Block Chaining (CBC) encryption ([KL20], Section 3.6.3, Figure 3.6): given block function f : BitString n → BitString n, chaining value iv : BitString n, and plaintext blocks ms : List (BitString n), emits c₁ = f (iv + m₁) and continues from c₁.

Cipher Block Chaining (CBC) decryption ([KL20], Section 3.6.3, Figure 3.6): given inverse block function fInv : BitString n → BitString n, chaining value iv : BitString n, and ciphertext blocks cs : List (BitString n), recovers m₁ = iv + fInv c₁ and continues from c₁.

@[simp]

CBC encryption of the empty block list is empty.

@[simp]
theorem Cslib.Crypto.cbcEncryptBlocks_cons {n : ℕ} (f : BitString n → BitString n) (iv m : BitString n) (ms : List (BitString n)) :
cbcEncryptBlocks f iv (m :: ms) = f (iv + m) :: cbcEncryptBlocks f (f (iv + m)) ms

CBC encryption unfolds by encrypting iv + m and chaining from f (iv + m).

@[simp]
theorem Cslib.Crypto.cbcDecryptBlocks_nil {n : ℕ} (fInv : BitString n → BitString n) (iv : BitString n) :

CBC decryption of the empty block list is empty.

@[simp]
theorem Cslib.Crypto.cbcDecryptBlocks_cons {n : ℕ} (fInv : BitString n → BitString n) (iv c : BitString n) (cs : List (BitString n)) :
cbcDecryptBlocks fInv iv (c :: cs) = (iv + fInv c) :: cbcDecryptBlocks fInv c cs

CBC decryption unfolds by recovering iv + fInv c and chaining from c.

@[simp]
theorem Cslib.Crypto.length_cbcEncryptBlocks {n : ℕ} (f : BitString n → BitString n) (iv : BitString n) (ms : List (BitString n)) :

CBC encryption preserves the number of blocks.

@[simp]
theorem Cslib.Crypto.length_cbcDecryptBlocks {n : ℕ} (fInv : BitString n → BitString n) (iv : BitString n) (cs : List (BitString n)) :

CBC decryption preserves the number of blocks.

theorem Cslib.Crypto.cbcDecryptBlocks_cbcEncryptBlocks {n : ℕ} {f fInv : BitString n → BitString n} (hInv : Function.LeftInverse fInv f) (iv : BitString n) (ms : List (BitString n)) :
cbcDecryptBlocks fInv iv (cbcEncryptBlocks f iv ms) = ms

CBC decryption inverts CBC encryption whenever fInv is a left inverse of f.

theorem Cslib.Crypto.cbcEncryptBlocks_cbcDecryptBlocks {n : ℕ} {f fInv : BitString n → BitString n} (hInv : Function.RightInverse fInv f) (iv : BitString n) (cs : List (BitString n)) :
cbcEncryptBlocks f iv (cbcDecryptBlocks fInv iv cs) = cs

CBC encryption inverts CBC decryption whenever fInv is a right inverse of f.

@[simp]

For a permutation π : Equiv.Perm (BitString n), CBC decryption with π.symm inverts CBC encryption with π.

def Cslib.Crypto.ofbChain {n : ℕ} (f : BitString n → BitString n) :
BitString n → ℕ → List (BitString n)

Output Feedback (OFB) keystream chain ([KL20], Section 3.6.3, Figure 3.8): k iterations of yᵢ = f yᵢ₋₁ starting from y₀ = iv.

def Cslib.Crypto.ofbEncryptBlocks {n : ℕ} (f : BitString n → BitString n) (iv : BitString n) (ms : List (BitString n)) :

Output Feedback (OFB) encryption ([KL20], Section 3.6.3, Figure 3.8): XORs each plaintext block with the corresponding block of ofbChain f iv ms.length.

@[reducible, inline]
abbrev Cslib.Crypto.ofbDecryptBlocks {n : ℕ} (f : BitString n → BitString n) (iv : BitString n) (cs : List (BitString n)) :

Output Feedback (OFB) decryption is identical to ofbEncryptBlocks.

@[simp]
theorem Cslib.Crypto.length_ofbChain {n : ℕ} (f : BitString n → BitString n) (iv : BitString n) (k : ℕ) :
(ofbChain f iv k).length = k

ofbChain f iv k has length k.

@[simp]
theorem Cslib.Crypto.length_ofbEncryptBlocks {n : ℕ} (f : BitString n → BitString n) (iv : BitString n) (ms : List (BitString n)) :

OFB encryption preserves the number of blocks.

@[simp]

OFB decryption inverts OFB encryption for every block function f, without requiring f to be invertible.

Structural equivalence between OFB and zero-block CBC: the k-block OFB keystream chain equals the CBC encryption of k all-zero blocks.

Cipher Feedback (CFB) encryption: emits c₁ = f iv + m₁ and feeds the ciphertext block c₁ into the next block-function call.

Cipher Feedback (CFB) decryption: recovers `m₁ = f iv

  • c₁and feedsc₁into the next block-function call, using only forward evaluations off`.
@[simp]

CFB encryption of the empty block list is empty.

@[simp]
theorem Cslib.Crypto.cfbEncryptBlocks_cons {n : ℕ} (f : BitString n → BitString n) (iv m : BitString n) (ms : List (BitString n)) :
cfbEncryptBlocks f iv (m :: ms) = (f iv + m) :: cfbEncryptBlocks f (f iv + m) ms

CFB encryption unfolds by emitting f iv + m and chaining from f iv + m.

@[simp]

CFB decryption of the empty block list is empty.

@[simp]
theorem Cslib.Crypto.cfbDecryptBlocks_cons {n : ℕ} (f : BitString n → BitString n) (iv c : BitString n) (cs : List (BitString n)) :
cfbDecryptBlocks f iv (c :: cs) = (f iv + c) :: cfbDecryptBlocks f c cs

CFB decryption unfolds by recovering f iv + c and chaining from c.

@[simp]
theorem Cslib.Crypto.length_cfbEncryptBlocks {n : ℕ} (f : BitString n → BitString n) (iv : BitString n) (ms : List (BitString n)) :

CFB encryption preserves the number of blocks.

@[simp]
theorem Cslib.Crypto.length_cfbDecryptBlocks {n : ℕ} (f : BitString n → BitString n) (iv : BitString n) (cs : List (BitString n)) :

CFB decryption preserves the number of blocks.

@[simp]

CFB decryption inverts CFB encryption for every block function f, without requiring f to be invertible.

@[simp]

CFB encryption inverts CFB decryption for every block function f.

def Cslib.Crypto.ctrMaskBlocks {n : ℕ} (f : BitString n → BitString n) (counter : BitString n → ℕ → BitString n) (iv : BitString n) (k : ℕ) :

Counter (CTR) mode keystream blocks ([KL20], Section 3.6.3, Figure 3.9): evaluates f (counter iv i) for i = 0, …, k - 1.

def Cslib.Crypto.ctrEncryptBlocks {n : ℕ} (f : BitString n → BitString n) (counter : BitString n → ℕ → BitString n) (iv : BitString n) (ms : List (BitString n)) :

Counter (CTR) mode encryption ([KL20], Section 3.6.3, Figure 3.9): XORs each plaintext block with the corresponding counter mask block.

@[reducible, inline]
abbrev Cslib.Crypto.ctrDecryptBlocks {n : ℕ} (f : BitString n → BitString n) (counter : BitString n → ℕ → BitString n) (iv : BitString n) (cs : List (BitString n)) :

Counter (CTR) mode decryption is identical to ctrEncryptBlocks.

@[simp]
theorem Cslib.Crypto.length_ctrMaskBlocks {n : ℕ} (f : BitString n → BitString n) (counter : BitString n → ℕ → BitString n) (iv : BitString n) (k : ℕ) :
(ctrMaskBlocks f counter iv k).length = k

ctrMaskBlocks f counter iv k has length k.

@[simp]
theorem Cslib.Crypto.length_ctrEncryptBlocks {n : ℕ} (f : BitString n → BitString n) (counter : BitString n → ℕ → BitString n) (iv : BitString n) (ms : List (BitString n)) :
(ctrEncryptBlocks f counter iv ms).length = ms.length

CTR encryption preserves the number of blocks.

@[simp]
theorem Cslib.Crypto.ctrDecryptBlocks_ctrEncryptBlocks {n : ℕ} (f : BitString n → BitString n) (counter : BitString n → ℕ → BitString n) (iv : BitString n) (ms : List (BitString n)) :
ctrDecryptBlocks f counter iv (ctrEncryptBlocks f counter iv ms) = ms

CTR decryption inverts CTR encryption for every block function f and counter schedule counter, without requiring f to be invertible.

Random-function keystream uniformity and lazy-sampling chain collisions #

theorem Cslib.Crypto.advantage_xorEvalPoints_eq_zero {n : ℕ} {ι : Type u_1} [Finite ι] {pts : ι → BitString n} (hpts : Function.Injective pts) (m₀ m₁ : ι → BitString n) (D : (ι → BitString n) → Game) :
Game.advantage ((PMF.map (fun (g : BitString n → BitString n) (i : ι) => g (pts i) + m₀ i) (Probability.PMF.uniformOfFintype (BitString n → BitString n))).bind D) ((PMF.map (fun (g : BitString n → BitString n) (i : ι) => g (pts i) + m₁ i) (Probability.PMF.uniformOfFintype (BitString n → BitString n))).bind D) = 0

Zero distinguishing advantage under distinct evaluation points: for any injective point schedule pts : ι → BitString n, any two multi-block messages m₀, m₁ : ι → BitString n, and any distinguisher D : (ι → BitString n) → Game, the distinguishing advantage between masking m₀ and masking m₁ at pts under a uniform random function is 0.

Good (collision-free) step invariant for lazy-sampled chains (used in the style of the random-IV chaining analysis in [KL20], Section 3.6.3): given a list ks : List (BitString n) of already-queried block-function inputs, current chaining block c : BitString n, remaining message blocks bs : List (BitString n) (arbitrary for CBC, or all-zero for OFB), and fresh uniform block draws draws : List (BitString n), asserts that every step input c + b is fresh (not in ks or any earlier step of the chain).

noncomputable def Cslib.Crypto.uniformBlocks {n : ℕ} (k : ℕ) :

Distribution of k independent uniform n-bit blocks as a list, used to lazily sample an idealized chain of length k: the independent draws PMF.iid read off by List.ofFn.

@[simp]

No uniform blocks: the empty list.

k + 1 uniform blocks: a uniform head followed by k independent uniform blocks.

Reading k independent uniform n-bit blocks as a function on Fin k and converting via List.ofFn equals uniformBlocks k.

Every list in the support of uniformBlocks k has length k.

Every (iv, us) pair in the support of a uniform IV paired with uniformBlocks k has us.length = k.

theorem Cslib.Crypto.map_zipWith_uniformBlocks {n : ℕ} (ms : List (BitString n)) :
PMF.map (fun (us : List (BitString n)) => List.zipWith (fun (x1 x2 : BitString n) => x1 + x2) us ms) (uniformBlocks ms.length) = uniformBlocks ms.length

XORing a fixed block sequence ms into uniformBlocks ms.length preserves the uniform distribution uniformBlocks ms.length.

theorem Cslib.Crypto.map_iv_uniformBlocks_zipWith {n : ℕ} (ms : List (BitString n)) :
PMF.map (fun (p : BitString n × List (BitString n)) => (p.1, List.zipWith (fun (x1 x2 : BitString n) => x1 + x2) p.2 ms)) ((Probability.PMF.uniformOfFintype (BitString n)).bind fun (iv : BitString n) => PMF.map (fun (x : List (BitString n)) => (iv, x)) (uniformBlocks ms.length)) = (Probability.PMF.uniformOfFintype (BitString n)).bind fun (iv : BitString n) => PMF.map (fun (x : List (BitString n)) => (iv, x)) (uniformBlocks ms.length)

XORing a fixed block sequence ms into the uniform block draws paired with an independent uniform IV leaves the joint (iv, blocks) distribution invariant.

Lazy-sampled multi-block chain collision lemma: when the initial chaining block iv and the bs.length step outputs are drawn independently and uniformly from BitString n, the outer measure of the bad event ¬goodChain ks iv bs draws (some step input cᵢ₋₁ + bᵢ collides with ks or an earlier step input in the same chain) is at most (bs.length * ks.length + bs.length.choose 2) / 2 ^ n: the j-th step input meets one of the ks.length + j earlier inputs with probability at most (ks.length + j) / 2 ^ n. This bounds the bad-event probability in lazy random-function sampling in the style of [KL20], Section 3.6.3.

IV-reuse, predictable-IV, and elementary encryption-mode counterexamples #

theorem Cslib.Crypto.ofbEncryptBlocks_zipWith_add_same_iv {n : ℕ} (f : BitString n → BitString n) (iv : BitString n) {ms₀ ms₁ : List (BitString n)} (hlen : ms₁.length = ms₀.length) :
List.zipWith (fun (x1 x2 : BitString n) => x1 + x2) (ofbEncryptBlocks f iv ms₀) (ofbEncryptBlocks f iv ms₁) = List.zipWith (fun (x1 x2 : BitString n) => x1 + x2) ms₀ ms₁

OFB IV-reuse keystream cancellation ([KL20], Section 3.6.4): encrypting two equal-length block sequences ms₀, ms₁ under the same iv in OFB mode cancels the keystream, leaking List.zipWith (· + ·) ms₀ ms₁ for every block function f.

theorem Cslib.Crypto.ctrEncryptBlocks_zipWith_add_same_iv {n : ℕ} (f : BitString n → BitString n) (counter : BitString n → ℕ → BitString n) (iv : BitString n) {ms₀ ms₁ : List (BitString n)} (hlen : ms₁.length = ms₀.length) :
List.zipWith (fun (x1 x2 : BitString n) => x1 + x2) (ctrEncryptBlocks f counter iv ms₀) (ctrEncryptBlocks f counter iv ms₁) = List.zipWith (fun (x1 x2 : BitString n) => x1 + x2) ms₀ ms₁

CTR IV-reuse keystream cancellation ([KL20], Section 3.6.4): encrypting two equal-length block sequences ms₀, ms₁ under the same iv in CTR mode cancels the keystream, leaking List.zipWith (· + ·) ms₀ ms₁ for every block function f and counter schedule counter.

Two-ciphertext same-IV distinguisher for stream / OFB / CTR modes ([KL20], Section 3.6.4): checks whether c₀ + c₁ = m₀ + m₁.

theorem Cslib.Crypto.streamIVReuseTest_ofbEncryptBlocks {n : ℕ} (f : BitString n → BitString n) (iv m₀ m₁ : BitString n) :
streamIVReuseTest m₀ m₁ ((ofbEncryptBlocks f iv [m₀]).getD 0 0, (ofbEncryptBlocks f iv [m₁]).getD 0 0) = true

streamIVReuseTest accepts any pair of 1-block OFB ciphertexts encrypted under the same iv.

theorem Cslib.Crypto.streamIVReuseTest_ctrEncryptBlocks {n : ℕ} (f : BitString n → BitString n) (counter : BitString n → ℕ → BitString n) (iv m₀ m₁ : BitString n) :
streamIVReuseTest m₀ m₁ ((ctrEncryptBlocks f counter iv [m₀]).getD 0 0, (ctrEncryptBlocks f counter iv [m₁]).getD 0 0) = true

streamIVReuseTest accepts any pair of 1-block CTR ciphertexts encrypted under the same iv.

noncomputable def Cslib.Crypto.streamIVReuseRealGame {n : ℕ} (padDist : PMF (BitString n)) (m₀ m₁ : BitString n) :

Real same-IV stream-encryption game on two 1-block messages m₀, m₁: both blocks are masked by the same keystream block pad.

noncomputable def Cslib.Crypto.streamIVReuseIdealGame {n : ℕ} (m₀ m₁ : BitString n) :

Ideal two-ciphertext game: the two ciphertext blocks are independent uniform n-bit blocks.

@[simp]
theorem Cslib.Crypto.streamIVReuseRealGame_eq_pure_true {n : ℕ} (padDist : PMF (BitString n)) (m₀ m₁ : BitString n) :

In the same-IV stream game, the keystream pad cancels and streamIVReuseTest always outputs true.

On two independent uniform n-bit ciphertext blocks, streamIVReuseTest accepts with exact probability (2 ^ n)⁻¹.

theorem Cslib.Crypto.advantage_streamIVReuseGame_eq {n : ℕ} (padDist : PMF (BitString n)) (m₀ m₁ : BitString n) :
(streamIVReuseRealGame padDist m₀ m₁).advantage (streamIVReuseIdealGame m₀ m₁) = 1 - (2 ^ n)⁻¹

Exact advantage of the same-IV distinguisher ([KL20], Section 3.6.4): reusing an IV on two messages gives distinguishing advantage 1 - (2 ^ n)⁻¹ against uniform ciphertexts for every pad distribution padDist.

Overlapping stream / nonce-CTR keystream blocks ([KL20], Exercises 3.24 and 3.32): generating two 2-block keystreams from distinct nonces iv and step iv produces ((f (step iv), f (step (step iv))), (f (step (step iv)), f (step (step (step iv))))), whose inner blocks always coincide because the counter windows overlap at step (step iv).

theorem Cslib.Crypto.ctrEncryptBlocks_overlapping_nonce {n : ℕ} (f step : BitString n → BitString n) (iv m₀ m₁ m₀' m₁' : BitString n) :
have ctr := fun (v : BitString n) (i : ℕ) => step^[i + 1] v; have c₁ := ctrEncryptBlocks f ctr iv [m₀, m₁]; have c₂ := ctrEncryptBlocks f ctr (step iv) [m₀', m₁']; ((c₁.getD 0 0 + m₀, c₁.getD 1 0 + m₁), c₂.getD 0 0 + m₀', c₂.getD 1 0 + m₁') = overlappingStreamBlocks f step iv

Overlapping-counter nonce-CTR keystream recovery ([KL20], Exercise 3.32): for the step-based counter schedule ctr v i = (step^[i + 1]) v, encrypting two 2-block plaintexts [m₀, m₁] and [m₀', m₁'] under nonces iv and step iv and XORing out the known plaintexts yields overlappingStreamBlocks f step iv.

Distinguisher for Exercises 3.24 and 3.32: checks whether the second block of the first output equals the first block of the second output.

noncomputable def Cslib.Crypto.overlappingStreamRealGame {n : ℕ} (f step : BitString n → BitString n) (ivDist : PMF (BitString n)) :

Real experiment for Exercises 3.24 and 3.32 over any initial nonce/IV distribution ivDist.

Ideal experiment for Exercises 3.24 and 3.32: four independent uniform n-bit blocks.

@[simp]

In the real overlapping-stream experiment, overlappingStreamTest accepts with probability 1.

On four independent uniform n-bit blocks, overlappingStreamTest accepts with exact probability (2 ^ n)⁻¹.

Exact advantage for overlapping counter/stream nonces ([KL20], Exercises 3.24 and 3.32): overlappingStreamTest distinguishes overlappingStreamBlocks (under distinct nonces iv and step iv) from four uniform n-bit blocks with exact advantage 1 - (2 ^ n)⁻¹.

def Cslib.Crypto.cbcPredictableIVQuery {n : ℕ} (iv₁ iv₂ m₀ : BitString n) :

Predictable-IV CBC CPA query ([KL20], Section 3.6.3 and Exercise 3.33): if the first encryption query uses iv₁ and the challenge encryption uses a predictable iv₂, querying cbcPredictableIVQuery iv₁ iv₂ m₀ = iv₁ + iv₂ + m₀ forces the first query's CBC block input to equal the challenge's CBC block input on m₀.

@[simp]
theorem Cslib.Crypto.add_cbcPredictableIVQuery {n : ℕ} (iv₁ iv₂ m₀ : BitString n) :
iv₁ + cbcPredictableIVQuery iv₁ iv₂ m₀ = iv₂ + m₀

XORing iv₁ with cbcPredictableIVQuery iv₁ iv₂ m₀ produces the target CBC input iv₂ + m₀.

theorem Cslib.Crypto.cbcEncryptBlocks_cbcPredictableIVQuery {n : ℕ} (f : BitString n → BitString n) (iv₁ iv₂ m₀ : BitString n) :
cbcEncryptBlocks f iv₁ [cbcPredictableIVQuery iv₁ iv₂ m₀] = cbcEncryptBlocks f iv₂ [m₀]

Encrypting cbcPredictableIVQuery iv₁ iv₂ m₀ under iv₁ in CBC mode produces the same 1-block ciphertext as encrypting m₀ under iv₂.

theorem Cslib.Crypto.cbcPredictableIV_eq_iff {n : ℕ} {f : BitString n → BitString n} (hf : Function.Injective f) (iv₁ iv₂ m₀ m₁ : BitString n) (hne : m₀ ≠ m₁) (b : Bool) :
cbcEncryptBlocks f iv₁ [cbcPredictableIVQuery iv₁ iv₂ m₀] = cbcEncryptBlocks f iv₂ [if b = true then m₁ else m₀] ↔ b = false

For any injective block function f, the predictable-IV CBC query matches the challenge ciphertext on if b then m₁ else m₀ (m₀ ≠ m₁) if and only if b = false.

noncomputable def Cslib.Crypto.cbcPredictableIVGame {n : ℕ} (f : BitString n → BitString n) (iv₁ iv₂ m₀ m₁ : BitString n) (b : Bool) :

Predictable-IV CBC CPA game for challenge bit b: the adversary queries cbcPredictableIVQuery iv₁ iv₂ m₀ under iv₁, receives challenge ciphertext under iv₂, and tests whether the two ciphertext blocks are equal.

theorem Cslib.Crypto.advantage_cbcPredictableIVGame_eq_one {n : ℕ} {f : BitString n → BitString n} (hf : Function.Injective f) (iv₁ iv₂ m₀ m₁ : BitString n) (hne : m₀ ≠ m₁) :
(cbcPredictableIVGame f iv₁ iv₂ m₀ m₁ false).advantage (cbcPredictableIVGame f iv₁ iv₂ m₀ m₁ true) = 1

Predictable-IV CBC CPA break ([KL20], Section 3.6.3 and Exercise 3.33): for any injective block function f and distinct messages m₀ ≠ m₁, the 1-query predictable-IV adversary distinguishes b = false from b = true with advantage 1.

def Cslib.Crypto.cfbSameIVRecover {n : ℕ} (m₁ c₁ cStar : BitString n) :

Same-IV CFB plaintext recovery: if two CFB encryptions reuse the same iv, given known message block m₁, its first ciphertext block c₁ = f iv + m₁, and a target first ciphertext block cStar = f iv + mStar, XORing c₁ + m₁ + cStar recovers mStar exactly for every block function f.

@[simp]
theorem Cslib.Crypto.cfbSameIVRecover_cfbEncryptBlocks {n : ℕ} (f : BitString n → BitString n) (iv m₁ mStar : BitString n) :
cfbSameIVRecover m₁ (f iv + m₁) (f iv + mStar) = mStar

cfbSameIVRecover recovers the target first plaintext block mStar from a known-plaintext CFB encryption under the same iv.

Linear XOR-keyed encryption ([KL20], Exercise 3.21): encrypts m under key k and randomness r as (r, k + r + m).

One-query message recovery for xorFunctionCipher: from a single encryption (r₁, c₁) of 0 and a target ciphertext (r₂, c₂), computes r₁ + c₁ + r₂ + c₂ = m.

@[simp]

xorFunctionCipherRecover recovers any message m from a single encryption of 0 under any randomness r₁, r₂.

def Cslib.Crypto.additiveModeCipher {B : Type u_1} {α : Type u_2} [Add B] (π : B → α) (δ r m₁ m₂ : B) :
B × α × α

Additive-IV two-block permutation mode ([KL20], Exercise 3.25): over any block type B with addition + (such as ZMod (2 ^ n) for modular addition mod 2^n, or BitString n for XOR), given a block function π : B → α and step offset δ : B (e.g. 1 : ZMod (2 ^ n)), encrypts (m₁, m₂) with random r : B as (r, (π (r + m₁), π (r + δ + m₂))).

def Cslib.Crypto.additiveModeDistinguisher {B : Type u_1} {α : Type u_2} [DecidableEq α] :
B × α × α → Bool

Zero-query distinguisher for additiveModeCipher on M₀ = (0, 0) vs M₁ = (0, -δ): tests whether the two ciphertext blocks are equal (c₁ = c₂).

theorem Cslib.Crypto.additiveModeDistinguisher_spec {B : Type u_1} {α : Type u_2} [AddCommGroup B] [DecidableEq α] {π : B → α} (hπ : Function.Injective π) {δ : B} (hδ : δ ≠ 0) (r : B) :

For any injective π : B → α over [AddCommGroup B] and nonzero step δ ≠ 0, additiveModeDistinguisher rejects every encryption of (0, 0) and accepts every encryption of (0, -δ) ([KL20], Exercise 3.25).

Transmission error-propagation laws and separations (Exercises 3.29 and 3.30) #

def Cslib.Crypto.flipCipherBlockBit {n : ℕ} (c : ℕ → BitString n) (b : ℕ) (i : Fin n) :

Flip bit i : Fin n of ciphertext block b : ℕ in an indexed block stream c : ℕ → BitString n.

def Cslib.Crypto.dropCipherBlock {n : ℕ} (c : ℕ → BitString n) (b : ℕ) :

Drop ciphertext block b : ℕ from an indexed block stream c : ℕ → BitString n, shifting all blocks at index ≥ b left by one position.

def Cslib.Crypto.cbcBlockDecrypt {n : ℕ} (fInv : BitString n → BitString n) (c : ℕ → BitString n) (j : ℕ) :

Indexed-stream CBC decryption: block 0 is the IV (c 0 = iv), and plaintext block j ≥ 1 is recovered as c (j - 1) + fInv (c j).

def Cslib.Crypto.cfbBlockDecrypt {n : ℕ} (f : BitString n → BitString n) (c : ℕ → BitString n) (j : ℕ) :

Indexed-stream CFB decryption: block 0 is the IV (c 0 = iv), and plaintext block j ≥ 1 is recovered as f (c (j - 1)) + c j.

def Cslib.Crypto.maskBlockDecrypt {n : ℕ} (mask : BitString n → ℕ → BitString n) (iv : BitString n) (c : ℕ → BitString n) (j : ℕ) :

Indexed-stream mask decryption (covering OFB and CTR modes): plaintext block j ≥ 1 is mask iv j + c j.

def Cslib.Crypto.ofbBlockMask {n : ℕ} (f : BitString n → BitString n) (iv : BitString n) (j : ℕ) :

Indexed OFB block mask: ofbBlockMask f iv j = f^[j] iv.

def Cslib.Crypto.ctrBlockMask {n : ℕ} (f : BitString n → BitString n) (counter : BitString n → ℕ → BitString n) (iv : BitString n) (j : ℕ) :

Indexed CTR block mask: ctrBlockMask f counter iv j = f (counter iv (j - 1)), so that indexed block j ≥ 1 uses the same counter value as list block j - 1 of ctrMaskBlocks.

theorem Cslib.Crypto.maskBlockDecrypt_ofbBlockMask_ofbEncryptBlocks {n : ℕ} (f : BitString n → BitString n) (iv : BitString n) (ms : List (BitString n)) (c : ℕ → BitString n) (hc : ∀ (j : ℕ) (hj : j < ms.length), c (j + 1) = (ofbEncryptBlocks f iv ms)[j]) (j : ℕ) (hj : j < ms.length) :
maskBlockDecrypt (ofbBlockMask f) iv c (j + 1) = ms[j]

Indexed OFB decryption agrees with list OFB encryption: if the indexed stream c carries the list ciphertext ofbEncryptBlocks f iv ms at positions 1, …, ms.length, then indexed block j + 1 decrypts to plaintext block j.

theorem Cslib.Crypto.maskBlockDecrypt_ctrBlockMask_ctrEncryptBlocks {n : ℕ} (f : BitString n → BitString n) (counter : BitString n → ℕ → BitString n) (iv : BitString n) (ms : List (BitString n)) (c : ℕ → BitString n) (hc : ∀ (j : ℕ) (hj : j < ms.length), c (j + 1) = (ctrEncryptBlocks f counter iv ms)[j]) (j : ℕ) (hj : j < ms.length) :
maskBlockDecrypt (ctrBlockMask f counter) iv c (j + 1) = ms[j]

Indexed CTR decryption agrees with list CTR encryption: if the indexed stream c carries the list ciphertext ctrEncryptBlocks f counter iv ms at positions 1, …, ms.length, then indexed block j + 1 decrypts to plaintext block j, for every counter schedule counter.

theorem Cslib.Crypto.maskBlockDecrypt_flipBit {n : ℕ} (mask : BitString n → ℕ → BitString n) (iv : BitString n) (c : ℕ → BitString n) (b : ℕ) (i : Fin n) :
maskBlockDecrypt mask iv (flipCipherBlockBit c b i) b i = !maskBlockDecrypt mask iv c b i ∧ (∀ (k : Fin n), k ≠ i → maskBlockDecrypt mask iv (flipCipherBlockBit c b i) b k = maskBlockDecrypt mask iv c b k) ∧ ∀ (j : ℕ), j ≠ b → maskBlockDecrypt mask iv (flipCipherBlockBit c b i) j = maskBlockDecrypt mask iv c j

OFB / CTR single-bit flip law ([KL20], Exercise 3.29): flipping bit i of ciphertext block b flips only bit i of recovered plaintext block b and leaves every other block j ≠ b and every other bit k ≠ i unchanged.

theorem Cslib.Crypto.maskBlockDecrypt_changeIV {n : ℕ} (mask : BitString n → ℕ → BitString n) (iv iv' : BitString n) (c : ℕ → BitString n) (j : ℕ) :
maskBlockDecrypt mask iv' c j = maskBlockDecrypt mask iv c j + (mask iv' j + mask iv j)

OFB / CTR IV modification law ([KL20], Exercise 3.29): replacing iv by iv' shifts every recovered plaintext block j by mask iv' j + mask iv j.

theorem Cslib.Crypto.cbcBlockDecrypt_flipBit {n : ℕ} (fInv : BitString n → BitString n) (c : ℕ → BitString n) (b : ℕ) (hb : 1 ≤ b) (i : Fin n) :
cbcBlockDecrypt fInv (flipCipherBlockBit c b i) b = c (b - 1) + fInv (flipCipherBlockBit c b i b) ∧ cbcBlockDecrypt fInv (flipCipherBlockBit c b i) (b + 1) i = !cbcBlockDecrypt fInv c (b + 1) i ∧ (∀ (k : Fin n), k ≠ i → cbcBlockDecrypt fInv (flipCipherBlockBit c b i) (b + 1) k = cbcBlockDecrypt fInv c (b + 1) k) ∧ ∀ (j : ℕ), 1 ≤ j → j ≠ b → j ≠ b + 1 → cbcBlockDecrypt fInv (flipCipherBlockBit c b i) j = cbcBlockDecrypt fInv c j

CBC single-bit flip law ([KL20], Exercise 3.29): flipping bit i of ciphertext block b ≥ 1 replaces block b with c (b - 1) + fInv (flipCipherBlockBit c b i b), flips bit i of block b + 1 while keeping all other bits k ≠ i of block b + 1 unchanged, and leaves every block j ∉ {b, b + 1} (j ≥ 1) completely unchanged.

theorem Cslib.Crypto.cfbBlockDecrypt_flipBit {n : ℕ} (f : BitString n → BitString n) (c : ℕ → BitString n) (b : ℕ) (hb : 1 ≤ b) (i : Fin n) :
cfbBlockDecrypt f (flipCipherBlockBit c b i) b i = !cfbBlockDecrypt f c b i ∧ (∀ (k : Fin n), k ≠ i → cfbBlockDecrypt f (flipCipherBlockBit c b i) b k = cfbBlockDecrypt f c b k) ∧ cfbBlockDecrypt f (flipCipherBlockBit c b i) (b + 1) = f (flipCipherBlockBit c b i b) + c (b + 1) ∧ ∀ (j : ℕ), 1 ≤ j → j ≠ b → j ≠ b + 1 → cfbBlockDecrypt f (flipCipherBlockBit c b i) j = cfbBlockDecrypt f c j

CFB single-bit flip law: flipping bit i of ciphertext block b ≥ 1 flips only bit i of plaintext block b, replaces block b + 1 with f (flipCipherBlockBit c b i b) + c (b + 1), and leaves every block j ∉ {b, b + 1} (j ≥ 1) completely unchanged.

theorem Cslib.Crypto.cbcBlockDecrypt_dropBlock {n : ℕ} (fInv : BitString n → BitString n) (c : ℕ → BitString n) (b : ℕ) (hb : 1 ≤ b) :
(∀ (j : ℕ), 1 ≤ j → j < b → cbcBlockDecrypt fInv (dropCipherBlock c b) j = cbcBlockDecrypt fInv c j) ∧ cbcBlockDecrypt fInv (dropCipherBlock c b) b = c (b - 1) + fInv (c (b + 1)) ∧ ∀ (j : ℕ), b < j → cbcBlockDecrypt fInv (dropCipherBlock c b) j = cbcBlockDecrypt fInv c (j + 1)

CBC dropped-block resynchronization ([KL20], Exercise 3.30): dropping ciphertext block b ≥ 1 leaves blocks 1 ≤ j < b unchanged, corrupts block b to `c (b - 1)

  • fInv (c (b + 1)), and resynchronizes every subsequent block j > bto the shifted plaintext blockcbcBlockDecrypt fInv c (j + 1)`.
theorem Cslib.Crypto.cfbBlockDecrypt_dropBlock {n : ℕ} (f : BitString n → BitString n) (c : ℕ → BitString n) (b : ℕ) (hb : 1 ≤ b) :
(∀ (j : ℕ), 1 ≤ j → j < b → cfbBlockDecrypt f (dropCipherBlock c b) j = cfbBlockDecrypt f c j) ∧ cfbBlockDecrypt f (dropCipherBlock c b) b = f (c (b - 1)) + c (b + 1) ∧ ∀ (j : ℕ), b < j → cfbBlockDecrypt f (dropCipherBlock c b) j = cfbBlockDecrypt f c (j + 1)

CFB dropped-block resynchronization: dropping ciphertext block b ≥ 1 leaves blocks 1 ≤ j < b unchanged, corrupts block b to f (c (b - 1)) + c (b + 1), and resynchronizes every subsequent block j > b to the shifted plaintext block cfbBlockDecrypt f c (j + 1).

theorem Cslib.Crypto.maskBlockDecrypt_dropBlock {n : ℕ} (mask : BitString n → ℕ → BitString n) (iv : BitString n) (c : ℕ → BitString n) (b : ℕ) :
(∀ (j : ℕ), 1 ≤ j → j < b → maskBlockDecrypt mask iv (dropCipherBlock c b) j = maskBlockDecrypt mask iv c j) ∧ ∀ (j : ℕ), b ≤ j → maskBlockDecrypt mask iv (dropCipherBlock c b) j = maskBlockDecrypt mask iv c (j + 1) + (mask iv j + mask iv (j + 1))

OFB / CTR dropped-block permanent desynchronization ([KL20], Exercise 3.30): dropping ciphertext block b ≥ 1 leaves blocks 1 ≤ j < b unchanged, but shifts every block j ≥ b by the keystream misalignment mask iv j + mask iv (j + 1).