Documentation

CslibFrontier.Crypto.Protocols.MAC.GHASH

GHASH, the keyed hash of GCM #

[BS23], Section 9.7, defines GHASH by (9.18): for a key k and blocks z = (z[0], …, z[v - 1]) of the field GF(2^128), GHASH(k, z) := z[0] · k ^ v + z[1] · k ^ (v - 1) + ⋯ + z[v - 1] · k. Here ghash k z is this evaluation over any field, and ghashBits e is the same function on w-bit blocks for a field with an additive bit encoding e : F ≃+ BitString w. GCM hashes, on line (∗) of Section 9.7, the associated data d and the ciphertext c, each padded with zeros to whole 128-bit blocks, followed by one block holding their two lengths: gcmHashInput d c.

The book requires GHASH to be a DUF, and says that the length block maintains "the XOR-DUF property". An ε-XOR-DUF ([BS23], Remark 7.3) is an ε-DUF whose digests are bit strings, with XOR as the difference operator. Here it is Cslib.Probability.IsDeltaUniversalHash with digests in BitString n, where subtraction is XOR; there is no separate predicate.

Differences from the book:

Main definitions #

Main results #

References #

GHASH over a field #

def Cslib.Crypto.ghash {F : Type u_1} [Field F] (k : F) (z : List F) :
F

GHASH, [BS23], (9.18): ghash k z = z[0] k ^ v + z[1] k ^ (v - 1) + ⋯ + z[v - 1] k for v blocks.

@[simp]
theorem Cslib.Crypto.ghash_nil {F : Type u_1} [Field F] (k : F) :
ghash k [] = 0
theorem Cslib.Crypto.ghash_concat {F : Type u_1} [Field F] (k : F) (z : List F) (a : F) :
ghash k (z ++ [a]) = (ghash k z + a) * k

One more block is the Horner step (GHASH(k, z) + a) · k.

theorem Cslib.Crypto.ghash_cons {F : Type u_1} [Field F] (k a : F) (z : List F) :
ghash k (a :: z) = a * k ^ (z.length + 1) + ghash k z

A new first block is multiplied by the highest power of the key.

theorem Cslib.Crypto.ghash_eq_sum {F : Type u_1} [Field F] (k : F) (z : List F) :
ghash k z = ∑ i : Fin z.length, z[i] * k ^ (z.length - ↑i)

[BS23], (9.18) as printed: block i of v is multiplied by k ^ (v - i).

theorem Cslib.Crypto.ghash_cons_zero {F : Type u_1} [Field F] (k : F) (z : List F) :
ghash k (0 :: z) = ghash k z

A leading zero block does not change the hash, so (9.18) on its own does not separate inputs of different lengths.

theorem Cslib.Crypto.PolynomialEvaluation.hash_eq_ghash {F : Type u_1} [Field F] {ℓ : ℕ} (encodeLength : Fin ℓ → F) (k : F) (m : Message F ℓ) :
hash encodeLength k m = ghash k (coefficients encodeLength m)

The hash of [KL20], Theorem 4.17 is GHASH of the message followed by its length code.

theorem Cslib.Crypto.isDeltaUniversalHash_ghash {F : Type u_1} [Field F] {ℓ : ℕ} [Fintype F] {X : Type u_2} (enc : X → List F) (hinj : Function.Injective enc) (hlast : ∀ (x x' : X), (enc x).getLastD 0 = (enc x').getLastD 0 → (enc x).length = (enc x').length) (hlen : ∀ (x : X), (enc x).length ≤ ℓ) :
Probability.IsDeltaUniversalHash (Probability.PMF.uniformOfFintype F) (fun (k : F) (x : X) => ghash k (enc x)) (↑ℓ / ↑(Fintype.card F))

GHASH is ℓ / |F|-difference universal on every injective family of inputs of at most ℓ blocks in which inputs with the same last block have the same number of blocks; the empty input counts as ending in the block 0.

theorem Cslib.Crypto.not_isUniversalHash_ghash {F : Type u_1} [Field F] {ℓ : ℕ} [Fintype F] (hℓ : 1 ≤ ℓ) {ε : ENNReal} (hε : ε < 1) :
¬Probability.IsUniversalHash (Probability.PMF.uniformOfFintype F) (fun (k : F) (z : { z : List F // z.length ≤ ℓ }) => ghash k ↑z) ε

GHASH on all inputs of at most ℓ ≥ 1 blocks is not universal for any ε < 1: [] and [0] collide under every key.

GHASH on bit strings #

def Cslib.Crypto.ghashBits {F : Type u_1} [Field F] {w : ℕ} (e : F ≃+ BitString w) (k : BitString w) (z : List (BitString w)) :

GHASH on w-bit blocks: key and blocks are read in the field F through the additive bit encoding e, and the digest is written back with e.

theorem Cslib.Crypto.ghashBits_def {F : Type u_1} [Field F] {w : ℕ} (e : F ≃+ BitString w) (k : BitString w) (z : List (BitString w)) :
ghashBits e k z = e (ghash (e.symm k) (List.map (⇑e.symm) z))

ghashBits e is ghash in the field, read through e.

theorem Cslib.Crypto.isDeltaUniversalHash_ghashBits {F : Type u_1} [Field F] {ℓ w : ℕ} (e : F ≃+ BitString w) {X : Type u_2} (enc : X → List (BitString w)) (hinj : Function.Injective enc) (hlast : ∀ (x x' : X), (enc x).getLastD 0 = (enc x').getLastD 0 → (enc x).length = (enc x').length) (hlen : ∀ (x : X), (enc x).length ≤ ℓ) :
Probability.IsDeltaUniversalHash (Probability.PMF.uniformOfFintype (BitString w)) (fun (k : BitString w) (x : X) => ghashBits e k (enc x)) (↑ℓ / 2 ^ w)

GHASH on bit strings is an ℓ / 2 ^ w-XOR-DUF for a uniform w-bit key, on every injective family of inputs of at most ℓ blocks in which inputs with the same last block have the same number of blocks.

The input of line (∗) #

The bit string s padded with zeros to a multiple of 128 bits, as 128-bit blocks.

theorem Cslib.Crypto.gcmBlocks_inj {s s' : List Bool} (hlen : s.length = s'.length) (h : gcmBlocks s = gcmBlocks s') :
s = s'

Bit strings of equal length with equal padded blocks are equal.

The block length(d) ‖ length(c) of line (∗): the 64-bit numbers a and b, each most significant bit first.

theorem Cslib.Crypto.gcmLengthBlock_inj {a b a' b' : ℕ} (ha : a < 2 ^ 64) (hb : b < 2 ^ 64) (ha' : a' < 2 ^ 64) (hb' : b' < 2 ^ 64) (h : gcmLengthBlock a b = gcmLengthBlock a' b') :
a = a' ∧ b = b'

Lengths below 2 ^ 64 are recovered from the length block.

The input of line (∗), d' ‖ c' ‖ length(d) ‖ length(c): the associated data d and the ciphertext c padded with zeros to 128-bit blocks, then their lengths in bits.

theorem Cslib.Crypto.length_gcmHashInput (d c : List Bool) :
(gcmHashInput d c).length = (d.length + 127) / 128 + (c.length + 127) / 128 + 1
theorem Cslib.Crypto.isDeltaUniversalHash_ghashBits_gcmHashInput {F : Type u_1} [Field F] {ℓ : ℕ} (e : F ≃+ BitString 128) {X : Type u_2} (d c : X → List Bool) (hinj : ∀ (x x' : X), d x = d x' → c x = c x' → x = x') (hd : ∀ (x : X), (d x).length < 2 ^ 64) (hc : ∀ (x : X), (c x).length < 2 ^ 64) (hlen : ∀ (x : X), (gcmHashInput (d x) (c x)).length ≤ ℓ) :
Probability.IsDeltaUniversalHash (Probability.PMF.uniformOfFintype (BitString 128)) (fun (k : BitString 128) (x : X) => ghashBits e k (gcmHashInput (d x) (c x))) (↑ℓ / 2 ^ 128)

GHASH with GCM's padding and length block is an ℓ / 2 ^ 128-XOR-DUF. Let d x and c x be the associated data and the ciphertext of the input x, which together determine x, each shorter than 2 ^ 64 bits, with at most ℓ blocks hashed, the length block included. The field F is any field with an additive encoding e as 128-bit strings.