Diffie-Hellman Key Establishment

For a very brief theory of Diffie-Hellman key exchange and their analysis, click here

The Diffie-Hellman key establishment protocol enables two users to derive a common secret over an insecure channel without directly transmitting that secret. In this experiment, Alice and Bob agree on public parameters and independently derive the same session key, which can then be used in symmetric encryption or message authentication.

How It Works

  1. Public Setup: Both parties agree on public parameters:
  • A large prime number pp
  • A generator gg of a suitable multiplicative subgroup modulo pp
  1. Private Key Generation: Each party chooses a private value:
  • Alice chooses private key aa
  • Bob chooses private key bb
  1. Public Key Calculation: Each party computes and publishes:
  • Alice: A=gamodpA = g^a \bmod p
  • Bob: B=gbmodpB = g^b \bmod p
  1. Key Exchange: Alice and Bob exchange AA and BB over the public channel.
  2. Shared Secret Derivation: Each side computes:
  • Alice: KA=BamodpK_A = B^a \bmod p
  • Bob: KB=AbmodpK_B = A^b \bmod p

Since

KA=(gb)amodp=gabmodp=(ga)bmodp=KB, K_A = (g^b)^a \bmod p = g^{ab} \bmod p = (g^a)^b \bmod p = K_B,

both sides derive the same key.

Mathematical Foundation

The security basis is the Discrete Logarithm Problem (DLP) in finite fields:

  • Easy direction: Given gg, xx, and pp, compute gxmodpg^x \bmod p efficiently.
  • Hard direction: Given gg, pp, and y=gxmodpy = g^x \bmod p, recover xx (discrete log) for appropriately chosen parameters.

For implementation relevance in CS/IT:

  • Modular exponentiation is computed efficiently using square-and-multiply in O(logx)O(\log x) multiplications.
  • Correctness requires arithmetic in modular groups and valid parameter ranges.
  • Real systems use very large parameters (or elliptic-curve variants) to keep DLP computationally infeasible.

Security Properties

The protocol provides the following properties when used with correct parameters:

  1. Confidential Key Establishment: Both users derive the same key K=gabmodpK = g^{ab} \bmod p without sending aa, bb, or KK.
  2. Public-Channel Operation: Public values (pp, gg, AA, BB) can be observed without directly revealing the shared secret.
  3. Composability: The derived secret can be fed into a KDF and then used by symmetric ciphers (AES) and MACs (HMAC).
  4. Forward-Secrecy Support: Ephemeral DH key pairs (DHE/ECDHE) enable session keys that are independent across connections.

Protocol Vulnerability

Basic Diffie-Hellman alone does not authenticate peers and is therefore vulnerable to active attacks:

  1. Man-in-the-Middle (MITM): An attacker can substitute public keys and establish separate secrets with Alice and Bob.
  2. Weak-Parameter Risk: Poor choices of pp or gg can reduce security or enable subgroup-related attacks.
  3. Implementation Errors: Missing validation of ranges (for private/public values) can produce incorrect or insecure outcomes.

Hence, secure deployments combine DH with authentication (digital signatures, certificates, PSK-based authentication).

Modern Applications

Diffie-Hellman key establishment is central to modern computer networks and security stacks:

  1. TLS 1.2/1.3: ECDHE-based handshake for HTTPS session keys.
  2. SSH: Session key negotiation between client and server.
  3. IPsec/IKE: Key establishment for VPN tunnels.
  4. Secure Messaging Protocols: Foundational component in end-to-end encryption designs.