Diffie-Hellman (Textbook Construction)
- •
What it is
- •
Diffie-Hellman key exchange lets two parties agree on a shared secret over a public channel, using the presumed difficulty of the discrete logarithm problem. Working in the multiplicative group of a finite field (integers mod a prime , excluding 0), both parties agree on a public prime and a generator (a primitive element, whose powers eventually produce every nonzero element of ). Each party picks a private integer ( for Alice, for Bob), computes and shares a public value (, ), and each combines their own private value with the other's public value to arrive at the same shared secret: — computable by either party, but (assumed) infeasible for an eavesdropper who only sees . The raw shared secret integer is typically not used directly as an encryption key; instead it's hashed (e.g. SHA-1) and truncated to derive a symmetric key (for AES, etc.).
- •
- •
When to apply
- •
The starting point for reasoning about any Diffie-Hellman challenge — every attack in this domain (weak parameter choices, MITM parameter substitution, small/weak groups) is a deviation from this baseline that breaks one of its security assumptions.
- •
- •
Symbols and assumptions
- •
is prime; generates a subgroup of order . Private exponents are ; public values are and . Group elements reduce modulo , while equivalent exponents differ by .
- •
- •
Math — step by step
- •
- Alice receives and computes . Bob receives and computes . Equality follows from multiplication of the exponents.
- •
- A passive observer knows but not . Recovering a private exponent solves a DLP; weak group order, bad public values, or an unauthenticated exchange can defeat the intended security.
- •
- Convert the shared group element to bytes according to the protocol and derive a key with its specified KDF. The mathematical shared value is not automatically an AES key.
- •
- •
- •
Python
- •
A = pow(g, a, p) # public value shared_secret = pow(B, a, p) # == pow(A, b, p), computed by the other party import hashlib key = hashlib.sha1(str(shared_secret).encode()).digest()[:16] # derive an AES key
- •
- •
- •
- •
Cards
- •
What mathematical assumption does Diffie-Hellman's security rest on?
- •
The discrete logarithm problem is hard in the chosen group — recovering a from g and A=g^a mod p is assumed computationally infeasible for well-chosen parameters.
- •
- •
Why is a "safe prime" (p=2q+1 for prime q) a common parameter choice?
- •
It keeps p-1's factorization simple ({2,q}), protecting against Pohlig-Hellman-style attacks that exploit a smooth (many small prime factors) p-1.
- •
- •
Linked references 8
- Additive Group Diffie-Hellman Break (Trivial Discrete Log in Additive Groups)
- cryptohack
- Diffie-Hellman Downgrade Attack (Export-Grade - Weak Group Negotiation)
- Diffie-Hellman Parameter-Public-Value Injection (MITM)
- Discrete Logarithm Problem (DLP)
- ECDH (Elliptic Curve Diffie-Hellman) — Textbook Construction
- Static-Key Generator Substitution Attack (Diffie-Hellman)
- XOR-Instead-of-Exponentiation Notation Bug (Diffie-Hellman)