Jaideep M. More

software engineer


Integrity

MAC - Message Authentication Code

Encryption in general does not solve the problem of message integrity.

The aim of message authentication code is to prevent any adversary from modifying the message sent by one party to another, or from injecting a new message, without the receiver detecting that the message did not originate from the intended party.

Formally, A message authentication code or MAC\text{MAC} consists of three probabilistic polynomial time algorithms (Gen, Mac, Vrfy)\text{(Gen, Mac, Vrfy)} such that:

  1. the key generation algorithm Gen\text{Gen} takes as input the security parameter 1n1^n and outputs a key kk with ∣k∣≥n|k| \ge n
  2. the tag generation algorithm Mac\text{Mac} takes as input a key kk and a message m∈{0,1}∗m \in \{0, 1\}^* and outputs a tag tt i.e. t←Mack(m)t \leftarrow \text{Mac}_k(m)
  3. the deterministic verification algorithm takes as input kk, a message mm and a tag tt. It outputs a bit bb with b=1b=1 as valid and b=0b=0 as invalid tag i.e. b:=Vrfyk(m,t)b := \text{Vrfy}_k(m, t)

Security of MACs:

Formally, The message authentication experiment Mac-forgeA,Π(n)\text{Mac-forge}_{\mathcal{A}, \Pi}(n):

  1. a key kk is generated by running Gen(1n)\text{Gen}(1^n)
  2. the adversary A\mathcal{A} is given input 1n1^n and oracle access to Mack(⋅)\text{Mac}_k(\cdot). the adversary eventually outputs (m,t)(m, t). Let Q\mathcal{Q} be the set of queries that A\mathcal{A} asked its oracle.
  3. A\mathcal{A} succeeds iff
    1. Vrfyk(m,t)=1\text{Vrfy}_k(m, t) = 1
    2. m∉Qm \notin \mathcal{Q}

The intuitive idea behind the definition is that no efficient adversary should be able to generate a valid tag on any new message that was not previously sent by one of the communicating parties.

A message authentication code is secure if for all PPT adversaries A\mathcal{A},

Pr[Mac-forgeA,Π(n)=1] is negligiblePr[\text{Mac-forge}_{\mathcal{A}, \Pi}(n) = 1] \text{ is negligible}

Fixed Length Mac:

Pseudorandom functions are a natural tool for constructing secure message authentication codes. If the tag tt is obtained by applying it on mm, then forging a tag requires the adversary to correctly guess the value of the pseudorandom function at a new input m′m^\prime.

Construction:

Let FF be a pseudorandom function.

  • Mac\text{Mac}: on input a key k∈{0,1}nk \in \{0, 1\}^n and a message m∈{0,1}nm \in \{0, 1\}^n, output the tag t:=Fk(m)t := F_k(m)
  • Vrfy\text{Vrfy}: on input a key k∈{0,1}nk \in \{0, 1\}^n, a message m∈{0,1}nm \in \{0, 1\}^n and a tag t∈{0,1}nt \in \{0, 1\}^n, output 11 iff t=Fk(m)t = F_k(m).

Discussion:

  • If FF is pseudorandom function, then the above construction is a secure fixed length MAC for messages of length nn.

CBC-MAC:

Basic Construction:

Insecure Construction Ahead

This basic scheme is not secure in the general case when messages of different lengths may be authenticated Please refer here to understand why this scheme is not secure.

Let FF be a pseudorandom function and a fix length function n>0n \gt 0

  • Mac\text{Mac}: on input a key k∈{0,1}nk \in \{0, 1\}^n and a message mm of length l⋅nl \cdot n
    1. Parse mm as m=m1,…,mlm = m_1, \dots, m_l where ∣mi∣=n|m_i| = n
    2. Set t0=0nt_0 = 0^n. Then ti=Fk(ti−1⊕mi)t_i = F_k(t_{i-1} \oplus m_i)
    3. Output tlt_l as the tag.
  • Vrfy\text{Vrfy}: on input a key k∈{0,1}nk \in \{0, 1\}^n, a message mm and tag tt
    1. if mm is not of length l.nl.n output 00
    2. output t==Mack(m)t == \text{Mac}_k(m)

CBC-MAC vs CBC encryption

  1. CBC mode encryption uses random IVIV and this is crucial for security. CBC-MAC’s IVIV is 0n0^n and this is also crucial for security. random IVIV is not secure for CBC-MAC

Secure CBC-MAC for arbitrary length messages:

There are two ways the above construction can be modified. Here we will discuss only one.

  1. Prepend the message mm with its length ∣m∣|m| and then compute basic CBC-MAC on the result

Hash Functions

  • Hash functions are simply functions that take inputs of some length and compress then into short fixed-length outputs
  • A good hash function is the one that yields few collisions, where a collision is a pair of distinct items xx and x′x^\prime such that H(x)=H(x′)H(x) = H(x^\prime)
  • Collision-resistant hash functions are similar in spirit. The goal is to avoid collisions. However there are fundamental differences. For one, the desire to minimise collisions in the setting of data structures becomes a requirement in the setting of cryptography.

Collisions resistance

A function is collision resistant if it is infeasible for any probabilistic polynomial time algorithm to find a collision in HH.

Formally, we consider keyed hash functions. That is HH is a two-input function that takes as input a key ss and a string xx, and outputs a string.

The requirement is that it must be hard to find a collision in HsH^s for a randomly generated key ss.

There are at least one differences between the keys in this context and what we have seen till now. The key ss is generally not kept secret and collision resistance is required even when the adversary is given the key ss.

Unkeyed Hash Functions

Cryptographic hash functions used in practice generally have a fixed output length and are usually unkeyed meaning that the hash function is just a fixed function

H:{0,1}∗→{0,1}lH:\{0, 1\}^* \rightarrow \{0, 1\}^l

This is problematic from a theoretic standpoint since for any such function there is always a constant time algorithm that outputs a collision in HH

However, the unkeyed cryptographic hash functions used in real world are collisions resistant for all practical purposes since colliding pairs are unknown and computationally difficult to find.

Domain Extension: Merkle Damgard Transform

Hash functions are often constructed by first designing a collision resistant compression function handling fixed length inputs and then using domain extension to handle arbitrary length inputs.

Merkle Damgard transform is a common approach for extending a compression functions to a full fledged hash function while maintaining the collision resistant property.

The existence of this transform means that when designing collision resistant hash functions we can restrict our attention to the fixed length case.

Message Authentication using Hash functions

Hash and MAC

The idea behind hash and MAC approach is simple

  1. an arbitrarily long message mm is hashed down to a fixed length string Hs(m)H^s(m) using a collision resistant hash function
  2. A fixed length MAC\text{MAC} is applied to the result

The above construction is secure for arbitrarily long messages mm if the MAC\text{MAC} is a secure MAC function and the hash function HsH^s is a collision resistant hash function.

HMAC:

good reference: https://crypto.stackexchange.com/questions/1070/why-is-hk-mathbin-vert-x-not-a-secure-mac-construction

Construction:

  • Let (GenH,H)(\text{Gen}_H, H) be a hash function constructed using Merkle Damgard transform to a compression function (GenH,h)(Gen_H, h) taking inputs of length n+n′n + n^\prime.

  • Let opad\text{opad}, ipad\text{ipad} be a fixed constants of length n′n^\prime

  • define MAC as follows:

  • Gen\text{Gen}: on input 1n1^n, run GenH(1n)\text{Gen}_H(1^n) to obtain key ss. Also choose uniform k∈{0,1}n′k \in \{0, 1\}^{n^{\prime}}. Output key ⟨s,k⟩\langle s, k\rangle

  • Mac\text{Mac}: on input key ⟨s,k⟩\langle s, k\rangle and message m∈{0,1}∗m \in \{0, 1\}^*, output

    t:=Hs((k⊕opad)∣∣Hs((k⊕ipad)∣∣m))t := H^s((k \oplus \text{opad}) \mathbin{||} H^s((k \oplus \text{ipad}) \mathbin{||} m))
  • Vrfy\text{Vrfy}: on input key ⟨s,k⟩\langle s, k\rangle and message m∈{0,1}∗m \in \{0, 1\}^* and a tag tt, output 11 iff

    t=Hs((k⊕opad)∣∣Hs((k⊕ipad)∣∣m))t = H^s((k \oplus \text{opad}) \mathbin{||} H^s((k \oplus \text{ipad}) \mathbin{||} m))

Roles of ipad\text{ipad} and opad\text{opad}

The reason is that this allows security of HMAC to be based on the potentially weaker assumption that (GenH,H)(Gen_H,H) is weakly collision resistant.

This is a defensive strategy that paid off when it was discovered that the hash function MD5\text{MD5} is not collision resistant.

The collision finding attacks on MD5\text{MD5} did not violate weak collision resistance and HMAC-MD5\text{HMAC-MD5} was not broken even though MD5\text{MD5} was.

This gave developers time to replace MD5\text{MD5} in HMAC\text{HMAC} implementations without immediate fear of attack.

The above discussion suggests that independent keys should be used in the outer and inner computations. for reasons of efficiency, a single key kk is used but the key is used in combination with ipad\text{ipad} ad opad\text{opad} to derive two other keys.