Documentation

CslibFrontier.Crypto.Computational.Encryption.MACThenEncrypt

MAC-then-Encrypt Composition and Chosen-Ciphertext Counterexample #

This module formalizes the generic MAC-then-Encrypt symmetric encryption construction (macThenEncrypt), proves that it preserves INDCPA (indcpa_macThenEncrypt), and gives an explicit counterexample showing that generic MAC-then-Encrypt does not achieve chosen-ciphertext security (INDCCA or OneTimeINDCCA) or ciphertext integrity (CiphertextIntegrity), even when the underlying cipher is INDCPA-secure and the MAC is SUFCMA-secure ([KL20], Section 5.2; [BS23], Section 9.4.1).

  1. Generic MAC-then-Encrypt construction (macThenEncrypt) and INDCPA preservation: given a symmetric cipher E, a MAC scheme M authenticating n-bit messages (M.msgLen n = n), and a polynomial-time length schedule ptLen : ℕ → ℕ such that every encoded (msg, tag) pair on an n-bit message has length ptLen n, macThenEncrypt E M ptLen ... generates keys at (ptLen n, n) and encrypts/decrypts the framed (msg, tag) pair under E at security parameter ptLen n, discharging decryption completeness directly from E.decrypt_encrypt. When ptLen is strictly monotone and E is INDCPA, macThenEncrypt is INDCPA (indcpa_macThenEncrypt).
  2. Malleable CPA-preserving cipher extension (extendCipher): appends false to every ciphertext on encryption and ignores the last bit (ct.take (ct.length - 1)) during decryption. We prove extendCipher preserves INDCPA (INDCPA.extendCipher) and OneTimeINDCPA (OneTimeINDCPA.extendCipher).
  3. Explicit CI/CCA attack and PRF instantiation (macThenEncrypt_extendCipher_counterexample, prfMacThenEncryptExtend_counterexample): replacing the trailing false bit of any ciphertext with true yields a distinct ciphertext with identical decryption under macThenEncrypt E.extendCipher M ptLen ..., breaking OneTimeINDCCA and INDCCA with exact advantage 1 for every n ≥ 1 and CiphertextIntegrity with probability 1. Combining with indcpa_macThenEncrypt and instantiating on (prfCipher ...).extendCipher + prfMAC ... gives a non-vacuous counterexample from any PseudorandomFunction.

Main definitions #

Main results #

References #

noncomputable def Cslib.Crypto.macThenEncrypt (E : SymmetricCipher) (M : MACScheme) (ptLen : ℕ → ℕ) (hptLenPoly : Turing.MultiTapeTM.ComputableInPolyTime ptLen Turing.MultiTapeTM.encUnary Turing.MultiTapeTM.encUnary) (hmsgLen : ∀ (n : ℕ), M.msgLen n = n) (hpairLen : ∀ (n : ℕ), ∀ km ∈ (M.keyGen n).eval.support, ∀ (msg : List Bool), msg.length = n → ∀ tag ∈ (M.sign km msg).eval.support, ((Turing.MultiTapeTM.encPair Turing.MultiTapeTM.encBits Turing.MultiTapeTM.encBits) (msg, tag)).length = ptLen n) :

MAC-then-Encrypt symmetric cipher: signs n-bit messages under km ← M.keyGen n and encrypts the framed pair encPair encBits encBits (msg, tag) under ke ← E.keyGen (ptLen n) at security parameter ptLen n. Because hpairLen guarantees the framed (msg, tag) pair has length ptLen n, decryption completeness follows directly from E.decrypt_encrypt (ptLen n). Its ciphertext space is E's at parameter ptLen n.

IND-CPA Preservation for MAC-then-Encrypt #

noncomputable def Cslib.Crypto.mteToCPAHandler (M : MACScheme) (n ctCap : ℕ) (km : List Bool) (request : Unit × List Bool) :

Oracle handler used by mteToCPAAdversary to simulate the CPA oracle of macThenEncrypt E M ptLen against E's CPA oracle at parameter ptLen n. On valid n-bit message pairs (m0, m1), it signs both messages under km and forwards the pair of framed (msg, tag) pairs to E's CPA oracle, truncating the reply to ctCap bits.

noncomputable def Cslib.Crypto.mteToCPAAdversary (M : MACScheme) (ptLen ctBound : ℕ → ℕ) (adversary : OracleDistinguisher) :

Reduction from cpaGame on macThenEncrypt E M ptLen (at security parameter n) to cpaGame on E (at security parameter m = ptLen n): recovers n := GeneratorReindex.parameter ptLen m, checks ptLen n == m, samples a MAC key km ← M.keyGen n, and interprets adversary n [] via mteToCPAHandler.

theorem Cslib.Crypto.eval_cpaGame_mteToCPAAdversary (E : SymmetricCipher) (M : MACScheme) (ptLen : ℕ → ℕ) (hptLenPoly : Turing.MultiTapeTM.ComputableInPolyTime ptLen Turing.MultiTapeTM.encUnary Turing.MultiTapeTM.encUnary) (hmono : StrictMono ptLen) (hmsgLen : ∀ (n : ℕ), M.msgLen n = n) (hpairLen : ∀ (n : ℕ), ∀ km ∈ (M.keyGen n).eval.support, ∀ (msg : List Bool), msg.length = n → ∀ tag ∈ (M.sign km msg).eval.support, ((Turing.MultiTapeTM.encPair Turing.MultiTapeTM.encBits Turing.MultiTapeTM.encBits) (msg, tag)).length = ptLen n) (ctBound : ℕ → ℕ) (adversary : OracleDistinguisher) (bit : Bool) (n : ℕ) (hctBound : E.CiphertextLengthLE (ptLen n) (ctBound (ptLen n))) :
(E.cpaGame (mteToCPAAdversary M ptLen ctBound adversary) bit (ptLen n)).eval = ((macThenEncrypt E M ptLen hptLenPoly hmsgLen hpairLen).cpaGame adversary bit n).eval

When ptLen is strictly monotone and ctBound (ptLen n) bounds all ciphertexts produced by E.encrypt (ptLen n) on ptLen n-bit plaintexts under supported keys, E.cpaGame running mteToCPAAdversary at security parameter ptLen n has the exact same output distribution as (macThenEncrypt E M ptLen ...).cpaGame running adversary at n.

theorem Cslib.Crypto.indcpa_macThenEncrypt (E : SymmetricCipher) (M : MACScheme) (ptLen : ℕ → ℕ) (hptLenPoly : Turing.MultiTapeTM.ComputableInPolyTime ptLen Turing.MultiTapeTM.encUnary Turing.MultiTapeTM.encUnary) (hmono : StrictMono ptLen) (hmsgLen : ∀ (n : ℕ), M.msgLen n = n) (hpairLen : ∀ (n : ℕ), ∀ km ∈ (M.keyGen n).eval.support, ∀ (msg : List Bool), msg.length = n → ∀ tag ∈ (M.sign km msg).eval.support, ((Turing.MultiTapeTM.encPair Turing.MultiTapeTM.encBits Turing.MultiTapeTM.encBits) (msg, tag)).length = ptLen n) (hcpa : E.INDCPA) :
(macThenEncrypt E M ptLen hptLenPoly hmsgLen hpairLen).INDCPA

IND-CPA security preserves under MAC-then-Encrypt: when ptLen is strictly monotone and computable in polynomial time, every encoded (msg, tag) pair on an n-bit message has length ptLen n, and E is INDCPA-secure, then macThenEncrypt E M ptLen hptLenPoly hmsgLen hpairLen is INDCPA-secure.

Trailing-bit malleable extension of E: appends false to every ciphertext on encryption and ignores the final bit (ct.take (ct.length - 1)) during decryption. Its ciphertext space is the nonempty words whose prefix without the final bit lies in E.ctSpace.

Oracle handler used by extendCPAAdversary to answer E.extendCipher CPA queries using E's CPA oracle, truncating each response to ctBound bits and appending false.

Reduction from cpaGame on E.extendCipher to cpaGame on E: appends false to every valid ciphertext returned by E's CPA oracle.

theorem Cslib.Crypto.SymmetricCipher.eval_cpaGame_extendCPAAdversary (E : SymmetricCipher) (ctBound : ℕ → ℕ) (adversary : OracleDistinguisher) (bit : Bool) (n : ℕ) (hctBound : E.CiphertextLengthLE n (ctBound n)) :
(E.cpaGame (extendCPAAdversary ctBound adversary) bit n).eval = (E.extendCipher.cpaGame adversary bit n).eval

When ctBound n bounds E ciphertexts on n-bit plaintexts, E.cpaGame running extendCPAAdversary ctBound adversary has the exact same distribution as E.extendCipher.cpaGame running adversary.

Appending and ignoring a constant trailing bit preserves INDCPA.

Stateful oracle handler used by extendOneTimeCPAAdversary to answer at most one E.extendCipher CPA query using E's one-time CPA oracle.

Reduction from cpaOneTimeGame on E.extendCipher to cpaOneTimeGame on E.

theorem Cslib.Crypto.SymmetricCipher.eval_cpaOneTimeGame_extendOneTimeCPAAdversary (E : SymmetricCipher) (ctBound : ℕ → ℕ) (adversary : OracleDistinguisher) (bit : Bool) (n : ℕ) (hctBound : E.CiphertextLengthLE n (ctBound n)) :
(E.cpaOneTimeGame (extendOneTimeCPAAdversary ctBound adversary) bit n).eval = (E.extendCipher.cpaOneTimeGame adversary bit n).eval

Equivalence between E.cpaOneTimeGame running extendOneTimeCPAAdversary and E.extendCipher.cpaOneTimeGame running adversary.

Appending and ignoring a constant trailing bit preserves OneTimeINDCPA.

Explicit CI and CCA Attacks on macThenEncrypt E.extendCipher M ptLen ... #

One-query ciphertext-integrity adversary against macThenEncrypt E.extendCipher M ptLen ...: encrypts 0^n and flips the trailing false bit of the ciphertext to true.

theorem Cslib.Crypto.eval_ciOneTimeGame_macThenEncrypt_extendCipher (E : SymmetricCipher) (M : MACScheme) (ptLen : ℕ → ℕ) (hptLenPoly : Turing.MultiTapeTM.ComputableInPolyTime ptLen Turing.MultiTapeTM.encUnary Turing.MultiTapeTM.encUnary) (hmsgLen : ∀ (n : ℕ), M.msgLen n = n) (hpairLen : ∀ (n : ℕ), ∀ km ∈ (M.keyGen n).eval.support, ∀ (msg : List Bool), msg.length = n → ∀ tag ∈ (M.sign km msg).eval.support, ((Turing.MultiTapeTM.encPair Turing.MultiTapeTM.encBits Turing.MultiTapeTM.encBits) (msg, tag)).length = ptLen n) (ctBound : ℕ → ℕ) (n : ℕ) (hctBound : (macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).CiphertextLengthLE n (ctBound n)) :
((macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).ciOneTimeGame (macThenEncryptExtendCIAdversary ctBound) n).eval = PMF.pure true

macThenEncryptExtendCIAdversary succeeds with probability 1 in ciOneTimeGame against macThenEncrypt E.extendCipher M ptLen ....

macThenEncrypt E.extendCipher M ptLen ... fails OneTimeCiphertextIntegrity for every cipher E and MAC M satisfying hmsgLen and hpairLen.

theorem Cslib.Crypto.not_ciphertextIntegrity_macThenEncrypt_extendCipher (E : SymmetricCipher) (M : MACScheme) (ptLen : ℕ → ℕ) (hptLenPoly : Turing.MultiTapeTM.ComputableInPolyTime ptLen Turing.MultiTapeTM.encUnary Turing.MultiTapeTM.encUnary) (hmsgLen : ∀ (n : ℕ), M.msgLen n = n) (hpairLen : ∀ (n : ℕ), ∀ km ∈ (M.keyGen n).eval.support, ∀ (msg : List Bool), msg.length = n → ∀ tag ∈ (M.sign km msg).eval.support, ((Turing.MultiTapeTM.encPair Turing.MultiTapeTM.encBits Turing.MultiTapeTM.encBits) (msg, tag)).length = ptLen n) :
¬(macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).CiphertextIntegrity

macThenEncrypt E.extendCipher M ptLen ... fails CiphertextIntegrity for every cipher E and MAC M satisfying hmsgLen and hpairLen.

Two-query chosen-ciphertext distinguisher against macThenEncrypt E.extendCipher M ptLen ...: queries the challenge encryption oracle on (0^n, 1^n), flips the trailing false bit of the returned ciphertext ct to true, queries the decryption oracle on the distinct ciphertext ct', and outputs true iff the plaintext equals 1^n.

theorem Cslib.Crypto.eval_ccaOneTimeGame_macThenEncrypt_extendCipher (E : SymmetricCipher) (M : MACScheme) (ptLen : ℕ → ℕ) (hptLenPoly : Turing.MultiTapeTM.ComputableInPolyTime ptLen Turing.MultiTapeTM.encUnary Turing.MultiTapeTM.encUnary) (hmsgLen : ∀ (n : ℕ), M.msgLen n = n) (hpairLen : ∀ (n : ℕ), ∀ km ∈ (M.keyGen n).eval.support, ∀ (msg : List Bool), msg.length = n → ∀ tag ∈ (M.sign km msg).eval.support, ((Turing.MultiTapeTM.encPair Turing.MultiTapeTM.encBits Turing.MultiTapeTM.encBits) (msg, tag)).length = ptLen n) (ctBound : ℕ → ℕ) (bit : Bool) {n : ℕ} (hn : bit = true ∨ 1 ≤ n) (hctBound : (macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).CiphertextLengthLE n (ctBound n)) :
((macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).ccaOneTimeGame (macThenEncryptExtendCCAAdversary ctBound) bit n).eval = PMF.pure bit

In ccaOneTimeGame against macThenEncrypt E.extendCipher M ptLen ..., macThenEncryptExtendCCAAdversary outputs bit with probability 1 whenever bit = true or 1 ≤ n.

theorem Cslib.Crypto.advantage_ccaOneTimeGame_macThenEncrypt_extendCipher_eq_one (E : SymmetricCipher) (M : MACScheme) (ptLen : ℕ → ℕ) (hptLenPoly : Turing.MultiTapeTM.ComputableInPolyTime ptLen Turing.MultiTapeTM.encUnary Turing.MultiTapeTM.encUnary) (hmsgLen : ∀ (n : ℕ), M.msgLen n = n) (hpairLen : ∀ (n : ℕ), ∀ km ∈ (M.keyGen n).eval.support, ∀ (msg : List Bool), msg.length = n → ∀ tag ∈ (M.sign km msg).eval.support, ((Turing.MultiTapeTM.encPair Turing.MultiTapeTM.encBits Turing.MultiTapeTM.encBits) (msg, tag)).length = ptLen n) (ctBound : ℕ → ℕ) {n : ℕ} (hn : 1 ≤ n) (hctBound : (macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).CiphertextLengthLE n (ctBound n)) :
Game.advantage ((macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).ccaOneTimeGame (macThenEncryptExtendCCAAdversary ctBound) false n).eval ((macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).ccaOneTimeGame (macThenEncryptExtendCCAAdversary ctBound) true n).eval = 1

macThenEncryptExtendCCAAdversary achieves exact one-time CCA advantage 1 against macThenEncrypt E.extendCipher M ptLen ... for every n ≥ 1.

theorem Cslib.Crypto.not_oneTimeINDCCA_macThenEncrypt_extendCipher (E : SymmetricCipher) (M : MACScheme) (ptLen : ℕ → ℕ) (hptLenPoly : Turing.MultiTapeTM.ComputableInPolyTime ptLen Turing.MultiTapeTM.encUnary Turing.MultiTapeTM.encUnary) (hmsgLen : ∀ (n : ℕ), M.msgLen n = n) (hpairLen : ∀ (n : ℕ), ∀ km ∈ (M.keyGen n).eval.support, ∀ (msg : List Bool), msg.length = n → ∀ tag ∈ (M.sign km msg).eval.support, ((Turing.MultiTapeTM.encPair Turing.MultiTapeTM.encBits Turing.MultiTapeTM.encBits) (msg, tag)).length = ptLen n) :
¬(macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).OneTimeINDCCA

macThenEncrypt E.extendCipher M ptLen ... fails OneTimeINDCCA for every cipher E and MAC M satisfying hmsgLen and hpairLen.

theorem Cslib.Crypto.not_indcca_macThenEncrypt_extendCipher (E : SymmetricCipher) (M : MACScheme) (ptLen : ℕ → ℕ) (hptLenPoly : Turing.MultiTapeTM.ComputableInPolyTime ptLen Turing.MultiTapeTM.encUnary Turing.MultiTapeTM.encUnary) (hmsgLen : ∀ (n : ℕ), M.msgLen n = n) (hpairLen : ∀ (n : ℕ), ∀ km ∈ (M.keyGen n).eval.support, ∀ (msg : List Bool), msg.length = n → ∀ tag ∈ (M.sign km msg).eval.support, ((Turing.MultiTapeTM.encPair Turing.MultiTapeTM.encBits Turing.MultiTapeTM.encBits) (msg, tag)).length = ptLen n) :
¬(macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).INDCCA

macThenEncrypt E.extendCipher M ptLen ... fails INDCCA for every cipher E and MAC M satisfying hmsgLen and hpairLen.

Combined Separation Theorem and PRF Instantiation #

theorem Cslib.Crypto.macThenEncrypt_extendCipher_counterexample (E : SymmetricCipher) (M : MACScheme) (ptLen : ℕ → ℕ) (hptLenPoly : Turing.MultiTapeTM.ComputableInPolyTime ptLen Turing.MultiTapeTM.encUnary Turing.MultiTapeTM.encUnary) (hmono : StrictMono ptLen) (hmsgLen : ∀ (n : ℕ), M.msgLen n = n) (hpairLen : ∀ (n : ℕ), ∀ km ∈ (M.keyGen n).eval.support, ∀ (msg : List Bool), msg.length = n → ∀ tag ∈ (M.sign km msg).eval.support, ((Turing.MultiTapeTM.encPair Turing.MultiTapeTM.encBits Turing.MultiTapeTM.encBits) (msg, tag)).length = ptLen n) (hcpa : E.INDCPA) (hsuf : M.SUFCMA) :
E.extendCipher.INDCPA ∧ M.SUFCMA ∧ (macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).INDCPA ∧ ¬(macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).OneTimeCiphertextIntegrity ∧ ¬(macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).CiphertextIntegrity ∧ ¬(macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).OneTimeINDCCA ∧ ¬(macThenEncrypt E.extendCipher M ptLen hptLenPoly hmsgLen hpairLen).INDCCA

Generic MAC-then-Encrypt CCA/CI counterexample theorem: given any INDCPA-secure cipher E, any SUFCMA-secure MAC M on n-bit messages, and a strictly monotone polynomial-time plaintext-tag length schedule ptLen, the trailing-bit malleable extension E.extendCipher remains INDCPA, macThenEncrypt E.extendCipher M ptLen ... remains INDCPA, yet fails OneTimeCiphertextIntegrity, CiphertextIntegrity, OneTimeINDCCA, and INDCCA.

theorem Cslib.Crypto.prfMAC_pair_length (family : List Bool → List Bool → List Bool) (hpoly : Turing.MultiTapeTM.ComputableInPolyTime (fun (pair : List Bool × List Bool) => family pair.1 pair.2) (Turing.MultiTapeTM.encPair Turing.MultiTapeTM.encBits Turing.MultiTapeTM.encBits) Turing.MultiTapeTM.encBits) (hlen : ∀ (key query : List Bool), query.length = key.length → (family key query).length = key.length) (n : ℕ) (km : List Bool) :
km ∈ ((prfMAC family hpoly).keyGen n).eval.support → ∀ (msg : List Bool), msg.length = n → ∀ tag ∈ ((prfMAC family hpoly).sign km msg).eval.support, ((Turing.MultiTapeTM.encPair Turing.MultiTapeTM.encBits Turing.MultiTapeTM.encBits) (msg, tag)).length = 3 * n + 1

On an n-bit message under a supported key, every encoded (msg, tag) pair produced by prfMAC family hpoly has exact length 3 * n + 1.

Concrete MAC-then-Encrypt counterexample cipher instantiated on (prfCipher family).extendCipher and prfMAC family with plaintext-tag length schedule ptLen n := 3 * n + 1.

Non-vacuous end-to-end MAC-then-Encrypt counterexample from any PseudorandomFunction: for every uniform PseudorandomFunction family, (prfCipher ...).extendCipher is INDCPA, prfMAC ... is SUFCMA, prfMacThenEncryptExtend is INDCPA, and prfMacThenEncryptExtend fails OneTimeCiphertextIntegrity, CiphertextIntegrity, OneTimeINDCCA, and INDCCA.