30 Mins

8. Key Exchange (Diffie-Hellman & Elliptic Curves)

Diffie-Hellman Key Exchange (DH)

The Diffie-Hellman (DH) protocol allows two parties to establish a shared secret over an insecure channel without ever transmitting the secret itself. It relies on the computational hardness of the discrete logarithm problem.

The process involves picking a large prime number p and a generator g. Each party generates a private key a or b, computes a public key as 'A = g^a mod p', and swaps these. By computing 'B^a mod p' or 'A^b mod p', both arrive at the same shared secret.

You can inspect DH parameters in a TLS handshake using OpenSSL. This reveals the prime and generator used during the negotiation phase.

Security Operator Note: DH is vulnerable to Man-in-the-Middle (MitM) attacks if not authenticated with digital signatures.
Text
openssl s_client -connect google.com:443 -cipher 'ECDHE-RSA-AES128-GCM-SHA256' < /dev/null | grep 'Server Temp Key'

The Discrete Logarithm Problem

The security of classical DH depends on the difficulty of finding the exponent x in the equation 'y = g^x mod p' when y, g, and p are known.

While modular exponentiation is computationally efficient, reversing the operation requires exponential time for large primes. However, modern computing power mandates primes of at least 2048 bits for acceptable security.

If an attacker can solve the discrete logarithm, they can derive the session key and decrypt all traffic associated with that exchange.

Security Operator Note: Always use 'pow(base, exp, mod)' in Python for efficient modular exponentiation; it prevents memory overflow.
Python 3
p = 23; g = 5; a = 6; A = pow(g, a, p)
print(f'Public Key A: {A}')

Elliptic Curve Cryptography (ECC)

ECC provides the same level of security as RSA or DH but with significantly smaller keys. This reduces computational overhead, battery usage on mobile devices, and bandwidth requirements.

ECC relies on the algebraic structure of elliptic curves over finite fields. A point on a curve is defined by y^2 = x^3 + ax + b.

ProtocolKey Advantage
Ed25519High performance and side-channel attack resistance.
Secp256k1Used primarily in Bitcoin/blockchain applications.
Security Operator Note: ECC 256-bit keys offer security comparable to 3072-bit RSA keys.
Text
openssl ecparam -list_curves | grep -E 'secp256k1|Ed25519'

Elliptic Curve Diffie-Hellman (ECDH) in Python

ECDH is the variant of Diffie-Hellman that uses elliptic curves. Each party generates a key pair where the private key is a random integer and the public key is a point on the curve.

Below is a demonstration using the cryptography library to perform a key exchange sequence.

The shared secret must be passed through a Key Derivation Function (KDF) like HKDF to ensure the output has high entropy.

Security Operator Note: Never use the raw ECC point as a symmetric key; always hash it first.
Python 3
from cryptography.hazmat.primitives.asymmetric import ec
private_key = ec.generate_private_key(ec.SECP256R1())
public_key = private_key.public_key()
print(f'Public point x: {public_key.public_numbers().x}')

Knowledge Check

Ready to test your understanding of 8. Key Exchange (Diffie-Hellman & Elliptic Curves)?

Back