Documentation

CslibFrontier.Crypto.Computational.Encryption.NonceCTR

Nonce-based counter mode and Theorem 5.6 #

Nonce-based counter mode ([BS23], Section 5.5.2, printed pp. 203–204) recasts the randomized counter-mode cipher of Section 5.4.2 (ctrWordKeystream in CslibFrontier.Crypto.Computational.Encryption.CTRMode.Uniform) to encrypt a sequence of n-bit blocks m[0], …, m[v - 1] under an n-bit PRF key k and a unique nonce N with zero ciphertext expansion. Both variants below share the deterministic constructor nonceStreamCipher family n toIV and differ only in the nonce-to-initial-counter map toIV:

  1. Construction, correctness, and polynomial-time algorithms (nonceCTRCounter, nonceStreamCipher, nonceCTRCipher, nonceCTRValid, nonceCTRValidNonce): nonceStreamCipher family n toIV XORs messages with ctrWordKeystream from toIV nonce and satisfies NonceCipher.Correct unconditionally (correct_nonceStreamCipher). Both encryption and decryption of nonceCTRCipher are computable in deterministic polynomial time whenever family and nonceLen are (computableInPolyTime_encrypt_nonceCTRCipher, computableInPolyTime_decrypt_nonceCTRCipher).
  2. Exact reduction and CPA security (nonceCTRToPRF, isOraclePPT_nonceCTRToPRF, advantage_cpaExperiment_nonceCTRCipher_eq_two_mul, PseudorandomFunction.indcpa_nonceCTR) ([BS23], Theorem 5.6, printed pp. 203–204, and inequality (5.32), printed p. 204): [BS23] leaves the proof of Theorem 5.6 as an exercise (p. 204), noting that it adapts the proof of Theorem 5.3 (p. 203) with B an elementary wrapper around A (p. 204). Here nonceCTRToPRF nonceLen A samples a uniform hidden bit b, simulates the stateful CPA word challenger for A via ctrKeystreamQueries against its external function oracle, and outputs whether ok && guess matches b. Against a uniform random function, distinct nonces and blocks ≤ 2 ^ (n - nonceLen n) make every counter query fresh, so ok && guess is independent of b and the distinguisher outputs true with probability 1 / 2. Because Attack Game 5.3's two-world CPA advantage (2 · nCPAadv* by (5.30), printed p. 202) agrees with Game.advantage, this yields the exact identity $$\mathrm{Adv}_{\mathrm{nCPA}}(A, \mathcal{E}) = 2 \cdot \mathrm{Adv}_{\mathrm{PRF}}(B_A, F),$$ whose .le direction is (5.32) (printed p. 204), and hence NonceCipher.INDCPA for every uniform PseudorandomFunction.
  3. Counter-overlap separation for unscaled nonces (unscaledNonceCTRCipher, unscaledNonceCTRAdversary, isOraclePPT_unscaledNonceCTRAdversary, advantage_cpaExperiment_unscaledNonceCTRAdversary, not_indcpa_unscaledNonceCTRCipher): instantiating the overlapping-interval failure noted in [BS23], Section 5.5.2 (printed p. 203), our concrete 2-query uniform strict IsOraclePPT distinguisher unscaledNonceCTRAdversary queries nonces 0 and 1 on 2-block messages (under blockPairValid with no block cap) to achieve CPA advantage 1 for every n ≥ 1, refuting NonceCipher.INDCPA for unscaledNonceCTRCipher.

Main definitions #

Main results #

References #

Nonce-based counter mode #

def Cslib.Crypto.nonceCTRCounter (n s : ℕ) (nonce : List Bool) (j : ℕ) :

The j-th counter input of nonce-based counter mode at block width n and nonce width s ([BS23], Section 5.5.2, printed p. 203): the (n - s)-bit little-endian offset j followed by nonce, representing lsbValue nonce * 2 ^ (n - s) + j when j < 2 ^ (n - s).

@[simp]
theorem Cslib.Crypto.length_nonceCTRCounter {n s : ℕ} (hs : s ≤ n) {nonce : List Bool} (hnonce : nonce.length = s) (j : ℕ) :
(nonceCTRCounter n s nonce j).length = n

When s ≤ n and nonce has s bits, nonceCTRCounter n s nonce j has n bits.

theorem Cslib.Crypto.lsbValue_nonceCTRCounter {n s : ℕ} (nonce : List Bool) {j : ℕ} (hj : j < 2 ^ (n - s)) :

Little-endian numeric value of nonceCTRCounter n s nonce j for j < 2 ^ (n - s), matching x = N · ℓ + j with ℓ = 2 ^ (n - s) ([BS23], Section 5.5.2, printed p. 203).

theorem Cslib.Crypto.ctrWordCounter_nonceCTRCounter_zero {n s : ℕ} (hs : s ≤ n) {nonce : List Bool} (hnonce : nonce.length = s) {j : ℕ} (hj : j < 2 ^ (n - s)) :
ctrWordCounter n (nonceCTRCounter n s nonce 0) j = nonceCTRCounter n s nonce j

For an s-bit nonce (s ≤ n) and block offset j < 2 ^ (n - s), advancing ctrWordCounter n from the scaled initial counter nonceCTRCounter n s nonce 0 (x = N · ℓ) produces nonceCTRCounter n s nonce j (N · ℓ + j).

Deterministic nonce-based counter-mode stream cipher at block width n under nonce-to-initial-counter map toIV: encrypt (or decrypt) a word of length len by XORing it with (len + n - 1) / n blocks of ctrWordKeystream family n k (toIV nonce), truncated or zero-padded to len bits.

theorem Cslib.Crypto.correct_nonceStreamCipher (family : List Bool → List Bool → List Bool) (n : ℕ) (toIV : List Bool → List Bool) :
(nonceStreamCipher family n toIV).Correct

Decryption inverts encryption for nonceStreamCipher on all keys, messages, and nonces.

Deterministic nonce-based counter-mode cipher ([BS23], Section 5.5.2, printed p. 203) at block width n and nonce width s: nonceStreamCipher under the scaled nonce translation nonce ↦ nonceCTRCounter n s nonce 0 (x := N · ℓ with ℓ = 2 ^ (n - s)).

def Cslib.Crypto.nonceCTRValid (nonceLen : ℕ → ℕ) (n : ℕ) (m₀ m₁ : List Bool) :

Admissible challenge-pair predicate for nonce-based counter mode with nonce-length schedule nonceLen ([BS23], Section 5.5.2, printed p. 203): equal-length messages of whole n-bit blocks (blockPairValid n m₀ m₁) with block count m₀.length / n ≤ 2 ^ (n - nonceLen n).

def Cslib.Crypto.nonceCTRValidNonce (nonceLen : ℕ → ℕ) (n : ℕ) (nonce : List Bool) :

Valid nonce predicate for nonce-based counter mode: nonce has nonceLen n bits.

Efficiency of the honest algorithms #

The PRF reduction for Theorem 5.6 #

noncomputable def Cslib.Crypto.nonceCTRToPRF (nonceLen : ℕ → ℕ) (adversary : OracleDistinguisher) :

Single-distinguisher PRF wrapper for nonce-based counter mode, adapting the bit-guessing reduction of [BS23], Theorem 5.3 (printed pp. 191–192) to Theorem 5.6 (whose proof [BS23], Section 5.5.2, printed pp. 203–204 leaves as an exercise, describing B as an elementary wrapper around A): sample a uniform hidden bit b, simulate the stateful CPA word challenger for adversary via ctrKeystreamQueries against the external function oracle, and output whether ok && guess equals b.

Efficiency of the Theorem 5.6 reduction. When nonceLen is computable in polynomial time in unary and adversary is uniform strict oracle PPT, nonceCTRToPRF nonceLen adversary is uniform strict oracle PPT.

Real and ideal PRF worlds and Theorem 5.6 #

theorem Cslib.Crypto.advantage_cpaExperiment_nonceCTRCipher_eq_two_mul {family : List Bool → List Bool → List Bool} (hlen : ∀ (key query : List Bool), query.length = key.length → (family key query).length = key.length) {nonceLen : ℕ → ℕ} {n : ℕ} (hs : nonceLen n ≤ n) (adversary : OracleDistinguisher) :
Game.advantage ((nonceCTRCipher family n (nonceLen n)).cpaExperiment (BitString.sampleList n) (nonceCTRValid nonceLen n) (nonceCTRValidNonce nonceLen n) (NonceCipher.unitCPACodec.adapt (adversary n [])) false).eval ((nonceCTRCipher family n (nonceLen n)).cpaExperiment (BitString.sampleList n) (nonceCTRValid nonceLen n) (nonceCTRValidNonce nonceLen n) (NonceCipher.unitCPACodec.adapt (adversary n [])) true).eval = 2 * Game.advantage (prfRealGame id family (nonceCTRToPRF nonceLen adversary) n).eval (prfIdealGame id id (nonceCTRToPRF nonceLen adversary) n).eval

Exact CPA-to-PRF advantage identity for nonce-based counter mode (strengthening inequality (5.32) of [BS23], Theorem 5.6, printed p. 204, to an equality): for any length-preserving family, parameter n with nonceLen n ≤ n, and distinguisher adversary, the two-world CPA advantage of unitCPACodec.adapt (adversary n []) against nonceCTRCipher family n (nonceLen n) equals twice the PRF advantage of nonceCTRToPRF nonceLen adversary. Applying .le gives (5.32) directly (Attack Game 5.3's two-world advantage, printed p. 202, equals Game.advantage and is twice the bit-guessing advantage (5.30)).

theorem Cslib.Crypto.PseudorandomFunction.indcpa_nonceCTR {family : List Bool → List Bool → List Bool} (hprf : PseudorandomFunction family) {nonceLen : ℕ → ℕ} (hnonceLen : Turing.MultiTapeTM.ComputableInPolyTime nonceLen Turing.MultiTapeTM.encUnary Turing.MultiTapeTM.encUnary) (hle : ∀ (n : ℕ), nonceLen n ≤ n) :
NonceCipher.INDCPA (fun (n : ℕ) => nonceCTRCipher family n (nonceLen n)) BitString.sampleList (nonceCTRValid nonceLen) (nonceCTRValidNonce nonceLen)

Nonce-based counter mode is CPA-secure for a pseudorandom function ([BS23], Section 5.5.2, Theorem 5.6, printed pp. 203–204): in the standard model against uniform strict oracle-PPT adversaries, if family is a PseudorandomFunction (n-bit keys and n-bit inputs to n-bit outputs) and nonceLen is computable in polynomial time in unary with nonceLen n ≤ n, then nonceCTRCipher family n (nonceLen n) satisfies NonceCipher.INDCPA on whole-block message pairs of at most ℓ = 2 ^ (n - nonceLen n) blocks under nonceLen n-bit nonces (the power-of-two case ℓ ∣ 2 ^ n noted on printed p. 203).

Counter-overlap attack on unscaled nonce-based counter mode #

The unscaled first attempt at nonce-based counter mode ([BS23], Section 5.5.2, printed p. 203): nonceStreamCipher under the identity nonce translation nonce ↦ nonce (x := N), using the n-bit nonce directly as the initial counter of ctrWordKeystream without multiplying by the maximum block count ℓ.

Concrete two-query counter-overlap CPA distinguisher against unscaledNonceCTRCipher, instantiating the overlapping-interval observation of [BS23], Section 5.5.2, printed p. 203:

  1. Query (0^(2n), 0^(2n)) under nonce lowBits n 0 to observe F(k, 1) as the second n-bit ciphertext block.
  2. Query (0^(2n), 1^n ++ 0^n) under distinct nonce lowBits n 1, whose first counter block is also lowBits n 1, and check whether the first n-bit ciphertext block equals the second block of reply 1.

At every parameter n ≥ 1, unscaledNonceCTRAdversary achieves CPA advantage 1 against unscaledNonceCTRCipher family n for any length-preserving family on whole-block message pairs.

theorem Cslib.Crypto.not_indcpa_unscaledNonceCTRCipher {family : List Bool → List Bool → List Bool} (hlen : ∀ (key query : List Bool), query.length = key.length → (family key query).length = key.length) :

Unscaled nonce-based counter mode is not CPA-secure ([BS23], Section 5.5.2, printed p. 203): for every length-preserving family family, using n-bit nonces directly as initial counters without scaling fails NonceCipher.INDCPA on whole-block message pairs (with no block cap; unscaledNonceCTRAdversary uses 2-block queries).