Diffie-Hellman
The Diffie–Hellman key exchange is a method that generates a shared secret over a public channel. This method is based on the discrete logarithm problem which is believed to be hard to solve.
Key generation (Textbook DH)
Suppose a situation where Alice and Bob want to create a shared secret key. They will use a public channel to do so.
- They chose a standard prime number and a generator . is usually 2 or 5 to make computations easier. and are public and is a finite field.
- They create private keys and respectively. .
- They compute the public keys and and send them over the public channel.
- They can now both compute the shared secret key : Alice computes and Bob computes .
They can now use the shared secret to derive a symmetric key for AES for example, and use it to encrypt their messages.
Attacks
DH with weak prime using Pohlig–Hellman - Wikipedia
The public prime modulus must be chosen such that where is also a prime. If is smooth (i.e have a lot of small, under 1000, factors), the Pohlig–Hellman algorithm can be used to compute the discrete logarithm very quickly. Sagemath’s discrete_log function can be used to compute the discrete logarithm for such primes.
Use this script to generate smooth numbers of selected size.
Low-entropy secret with a partially smooth prime
Even when is not fully smooth, Pohlig–Hellman still leaks the private key modulo the smooth part of . This is enough to fully recover a low-entropy secret (for example a private key derived from a short password or hashed with a weak function).
Write where is the smooth part (product of the small prime factors) and is the rough cofactor. The map projects onto the unique subgroup of smooth order . With and , the public key becomes inside , so a discrete log in recovers
If the secret satisfies , this recovers entirely.
from sage.all import * N = prod(P**e for P, e in factor(p-1) if P < 10**12) # smooth part M = (p-1) // N f = discrete_log(Mod(F, p)**M, Mod(g, p)**M, ord=N) # f mod NDH with small prime
The security of Diffie-Hellman is lower than the number of bits in . Consequently, is p is too small (for example 64bits), it is possible to compute the discrete logarithm in a reasonable amount of time.
from sage.all import * a = discrete_log(Mod(A, p), Mod(g, p))