Documentation

CslibFrontier.Crypto.Computational.Encryption.CTRMode.Uniform

Randomized counter mode from a uniform pseudorandom function #

Randomized counter mode ([BS23], Section 5.4.2, printed p. 189) encrypts a message of v blocks m[0], …, m[v - 1] under a key k by drawing a uniform initial value x and sending (x, c) with c[j] = F(k, x + j mod N) ⊕ m[j]. Here the security parameter n is the block length and N = 2 ^ n: the PRF family takes n-bit keys and n-bit inputs to n-bit outputs, and the j-th counter block is ctrWordCounter n x j, the n-bit little-endian representation of x + j mod 2 ^ n. A message is any word made of whole blocks. ctrWordEncrypt outputs the word x ++ c, and ctrWordDecrypt inverts it; both are the shared envelope ivStreamEncrypt, ivStreamDecrypt with the counter keystream ctrWordKeystream.

Security is many-query left-or-right CPA security (SymmetricINDCPAOn, Attack Game 5.2) on equal-length pairs of whole blocks (blockPairValid), in the standard model, against uniform strict oracle-PPT adversaries. The PRF distinguisher ctrToPrfDistinguisher adversary bit is the shared stream reduction ivStreamToPrfDistinguisher with the counter keystream queries ctrKeystreamQueries: it runs the adversary and answers each admissible request itself, drawing the initial value and obtaining each keystream block with one query to its own function oracle, keeping at most n bits of the reply.

  1. It is uniform strict oracle PPT whenever the adversary is (isOraclePPT_ctrToPrfDistinguisher).
  2. Against the keyed oracle, its output law is that of the adversary in the CPA game with challenge bit bit (eval_prfRealGame_ctrToPrfDistinguisher).
  3. Against a uniform random function, it is the typed reduction ctrReduction ctrModCounter of CTRMode.lean run on the adversary read as a block-mode adversary (blockModeWordAdversary), and the typed collision bound advantage_idealExperiment_ctrReduction_le applies. This gives the concrete bound advantage_ctrWordCPAGame_le and then PseudorandomFunction.indcpa_ctrWordEncrypt.

Deviations from Theorem 5.3 #

Main definitions #

Main results #

References #

The cipher #

The j-th counter block from initial value iv: the n-bit little-endian representation of iv + j mod 2 ^ n, reading iv least significant bit first.

def Cslib.Crypto.ctrWordKeystream (family : List Bool → List Bool → List Bool) (n : ℕ) (k iv : List Bool) (blocks : ℕ) :

The keystream of blocks blocks under family k from initial value iv: the outputs at the counter blocks 0, …, blocks - 1, concatenated.

noncomputable def Cslib.Crypto.ctrWordEncrypt (family : List Bool → List Bool → List Bool) :

Randomized counter-mode encryption ([BS23], Section 5.4.2): draw a uniform n-bit initial value iv and output iv followed by the message XORed with the keystream of msg.length / n blocks.

Counter-mode decryption: accept an n-bit initial value followed by whole blocks, and XOR the blocks with the keystream from that initial value.

@[simp]
theorem Cslib.Crypto.length_ctrWordCounter (n : ℕ) (iv : List Bool) (j : ℕ) :
theorem Cslib.Crypto.length_ctrWordKeystream {family : List Bool → List Bool → List Bool} (hlen : ∀ (key query : List Bool), query.length = key.length → (family key query).length = key.length) {n : ℕ} {k : List Bool} (hk : k.length = n) (iv : List Bool) (blocks : ℕ) :
(ctrWordKeystream family n k iv blocks).length = blocks * n

Under a key of length n, a family with n-bit outputs on n-bit inputs produces a keystream of blocks * n bits.

theorem Cslib.Crypto.ctrWordDecrypt_ctrWordEncrypt {family : List Bool → List Bool → List Bool} (hlen : ∀ (key query : List Bool), query.length = key.length → (family key query).length = key.length) {n : ℕ} {k : List Bool} (hk : k.length = n) {msg : List Bool} (hmsg : msg.length % n = 0) {ct : List Bool} (hct : ct ∈ (ctrWordEncrypt family n k msg).eval.support) :
ctrWordDecrypt family n k ct = some msg

Decryption inverts encryption on whole-block messages under keys of length n.

Efficiency of the honest algorithms #

Counter blocks are computable in polynomial time from a unary width, the initial value and a unary offset.

The reduction to the PRF game #

One keystream block from the function oracle: query the j-th counter block, keep at most n bits of the reply and append them to the keystream so far.

The keystream of blocks blocks from the function oracle, with one query per block.

@[reducible, inline]

The PRF distinguisher built from a left-or-right CPA adversary against counter mode ([BS23], proof of Theorem 5.3): the shared stream reduction ivStreamToPrfDistinguisher with the counter keystream queries ctrKeystreamQueries.

theorem Cslib.Crypto.run_ctrKeystreamQueries_of_length {n : ℕ} {g : List Bool → List Bool} (hg : ∀ (x : List Bool), x.length = n → (g x).length = n) (iv : List Bool) (blocks : ℕ) :
(ProbOracleComp.run (ctrKeystreamQueries n iv blocks) fun (request : Unit × List Bool) => pure (g request.2)) = pure (List.flatMap (fun (j : ℕ) => g (ctrWordCounter n iv j)) (List.range blocks))

Answered by a deterministic oracle g with n-bit answers on n-bit requests, the keystream queries return the concatenated answers at the counter blocks.

theorem Cslib.Crypto.run_ctrKeystreamQueries {family : List Bool → List Bool → List Bool} (hlen : ∀ (key query : List Bool), query.length = key.length → (family key query).length = key.length) {n : ℕ} {k : List Bool} (hk : k.length = n) (iv : List Bool) (blocks : ℕ) :
(ProbOracleComp.run (ctrKeystreamQueries n iv blocks) fun (request : Unit × List Bool) => pure (prfOracle family n k request.2)) = pure (ctrWordKeystream family n k iv blocks)

Answered by the keyed oracle under a key of length n, the keystream queries compute the keystream.

theorem Cslib.Crypto.eval_prfRealGame_ctrToPrfDistinguisher {family : List Bool → List Bool → List Bool} (hlen : ∀ (key query : List Bool), query.length = key.length → (family key query).length = key.length) (adversary : OracleDistinguisher) (bit : Bool) (n : ℕ) :

Real game. Against the keyed oracle, ctrToPrfDistinguisher adversary bit has the output law of adversary in the counter-mode CPA game with challenge bit bit.

The keystream queries are strict oracle PPT: each step makes one charged query and grows the encoded state by at most n + 2 bits.

Efficiency of the reduction. For every uniform strict oracle-PPT adversary, ctrToPrfDistinguisher adversary bit is uniform strict oracle PPT.

The ideal game #

theorem Cslib.Crypto.advantage_ctrWordCPAGame_le {family : List Bool → List Bool → List Bool} (hlen : ∀ (key query : List Bool), query.length = key.length → (family key query).length = key.length) (adversary : OracleDistinguisher) {n Q L : ℕ} (hQ : ProbOracleComp.IsQueryBounded (adversary n []) Q) (hL : ProbOracleComp.QueriesSatisfy (fun (request : Unit × List Bool) => request.2.length ≤ L) (adversary n [])) (hLn : L ≤ 2 ^ n) :

Concrete CPA bound for counter mode, after [BS23], Theorem 5.3, (5.14). At parameter n, an adversary making at most Q requests of at most L ≤ 2 ^ n bits has CPA advantage against ctrWordEncrypt family at most the PRF advantages of ctrToPrfDistinguisher adversary false and ctrToPrfDistinguisher adversary true plus 2 (Q L) ^ 2 / 2 ^ n. Requests of L bits ask for at most L blocks, so Q L bounds the blocks encrypted in total. This is not the bound (5.14), which has one distinguisher with a factor 2 and the collision term 2 Q ^ 2 ℓ / N for ℓ blocks per request; see the module's deviations.

Counter mode is CPA-secure for a pseudorandom function ([BS23], Section 5.4.2, Theorem 5.3): if family is a PseudorandomFunction, randomized counter mode with n-bit blocks at security parameter n has negligible many-query left-or-right CPA advantage on equal-length pairs of whole blocks, against every uniform strict oracle-PPT adversary. The source fixes a poly-bounded cap ℓ on the number of blocks; here there is no cap, which implies the statement for every ℓ.