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 #
cbcEncryptBlocks,cbcDecryptBlocks: Cipher Block Chaining (CBC) encryption and decryption ([KL20], Section 3.6.3, Figure 3.6).ofbChain,ofbEncryptBlocks,ofbDecryptBlocks: Output Feedback (OFB) keystream chain and encryption/decryption ([KL20], Section 3.6.3, Figure 3.8).cfbEncryptBlocks,cfbDecryptBlocks: Cipher Feedback (CFB) encryption and decryption.ctrMaskBlocks,ctrEncryptBlocks,ctrDecryptBlocks: Counter (CTR) mode mask generation and encryption/decryption ([KL20], Section 3.6.3, Figure 3.9).goodChain,uniformBlocks: collision-free step invariant and lazy-sampling block draws for multi-block feedback chains.streamIVReuseTest,streamIVReuseRealGame,streamIVReuseIdealGame: same-IV keystream cancellation distinguisher ([KL20], Section 3.6.4).overlappingStreamBlocks,overlappingStreamTest,overlappingStreamRealGame,overlappingStreamIdealGame: overlapping counter/stream window distinguisher ([KL20], Exercises 3.24 and 3.32).cbcPredictableIVQuery,cbcPredictableIVGame: predictable-IV chosen-plaintext attack on CBC mode ([KL20], Section 3.6.3 and Exercise 3.33).cfbSameIVRecover,xorFunctionCipher,xorFunctionCipherRecover,additiveModeCipher,additiveModeDistinguisher: message-recovery and distinguishing attacks on reused-IV CFB, linear XOR encryption ([KL20], Exercise 3.21), and additive-IV permutation mode over any commutative group ([KL20], Exercise 3.25).flipCipherBlockBit,dropCipherBlock,cbcBlockDecrypt,cfbBlockDecrypt,maskBlockDecrypt,ofbBlockMask,ctrBlockMask: indexed block-stream decryption and transmission-error operators ([KL20], Section 3.6.3, Exercises 3.29 and 3.30).
Main results #
cbcDecryptBlocks_cbcEncryptBlocks,ofbDecryptBlocks_ofbEncryptBlocks,cfbDecryptBlocks_cfbEncryptBlocks,ctrDecryptBlocks_ctrEncryptBlocks: round-trip inversion identities, plusofbChain_eq_cbcEncryptBlocks_replicate_zero.advantage_xorEvalPoints_eq_zero: masking with a uniform random function on any fixed (non-adaptive) injective point schedulepts : ι → BitString n(such as non-overlapping CTR counters) has zero distinguishing advantage, from the bit-string uniformity lemmaBitString.map_xorEvalPoints_uniformOfFintype.toOuterMeasure_badChain_le: lazy-sampling chain collision bound(bs.length * ks.length + bs.length.choose 2) / 2 ^ n, built from the per-step boundBitString.toOuterMeasure_addRight_mem_list_le, used as a proof ingredient when analyzing random-IV chaining ([KL20], Section 3.6.3).ofbEncryptBlocks_zipWith_add_same_iv,ctrEncryptBlocks_zipWith_add_same_iv,advantage_streamIVReuseGame_eq,ctrEncryptBlocks_overlapping_nonce,advantage_overlappingStreamGame_eq,advantage_cbcPredictableIVGame_eq_one,cfbSameIVRecover_cfbEncryptBlocks,xorFunctionCipherRecover_correct,additiveModeDistinguisher_spec: mode counterexamples and IV/nonce misuse separations.maskBlockDecrypt_ofbBlockMask_ofbEncryptBlocks,maskBlockDecrypt_ctrBlockMask_ctrEncryptBlocks: the indexed OFB / CTR masks decrypt the list-mode ciphertexts.maskBlockDecrypt_flipBit,cbcBlockDecrypt_flipBit,cfbBlockDecrypt_flipBit,cbcBlockDecrypt_dropBlock,cfbBlockDecrypt_dropBlock,maskBlockDecrypt_dropBlock: single-bit flip and dropped-block error-propagation laws.
References #
- J. Katz, Y. Lindell, Introduction to Modern Cryptography, 3rd ed., Sections 3.6.2–3.6.4 and Exercises 3.21, 3.24, 3.25, 3.29, 3.30, 3.32, 3.33.
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₁.
CBC decryption inverts CBC encryption whenever fInv is a left inverse of f.
CBC encryption inverts CBC decryption whenever fInv is a right inverse of f.
For a permutation π : Equiv.Perm (BitString n), CBC decryption with π.symm inverts CBC
encryption with π.
ctrMaskBlocks f counter iv k has length k.
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 #
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).
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.
XORing a fixed block sequence ms into uniformBlocks ms.length preserves the uniform
distribution 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 #
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.
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.
streamIVReuseTest accepts any pair of 1-block OFB ciphertexts encrypted under the same
iv.
streamIVReuseTest accepts any pair of 1-block CTR ciphertexts encrypted under the same
iv.
Ideal two-ciphertext game: the two ciphertext blocks are independent uniform n-bit blocks.
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)⁻¹.
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).
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.
overlappingStreamTest always accepts overlappingStreamBlocks f step iv.
Ideal experiment for Exercises 3.24 and 3.32: four independent uniform n-bit blocks.
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)⁻¹.
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₀.
XORing iv₁ with cbcPredictableIVQuery iv₁ iv₂ m₀ produces the target CBC input iv₂ + m₀.
Encrypting cbcPredictableIVQuery iv₁ iv₂ m₀ under iv₁ in CBC mode produces the same
1-block ciphertext as encrypting m₀ under iv₂.
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.
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.
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.
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.
cfbSameIVRecover recovers the target first plaintext block mStar from a known-plaintext
CFB encryption under the same iv.
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.
xorFunctionCipherRecover recovers any message m from a single encryption of 0 under any
randomness r₁, r₂.
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₂))).
Zero-query distinguisher for additiveModeCipher on M₀ = (0, 0) vs M₁ = (0, -δ): tests
whether the two ciphertext blocks are equal (c₁ = c₂).
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) #
Indexed OFB block mask: ofbBlockMask f iv j = f^[j] iv.
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.
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.
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.
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.
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.
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.
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 blockj > bto the shifted plaintext blockcbcBlockDecrypt fInv 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).
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).