Diffie Hellman is an important cornerstone of internet security. It’s used to agree a shared secret over an insecure channel. It forms the basis of many Internet services such as providing forward secrecy in TLS’s ephemeral modes.
Although it could be used for public key infrastructure, RSA is the preferred choice. This is largely due to historical reasons such as RSA namely that RSA Security created a certificate authority for key signing that became Verisign. Diffie–Hellman cannot directly be used to sign certificates i.e. it does not provide authentication2 by itself.
How it works
The goal as stated above is to have two parties (Alice and Bob) come up with the same shared secret value without any prior arrangement
We need some base parameters: a large prime p and a generator g
It doesn’t matter if everyone knows these base parameters. They are usually defined in some spec and it’s usually safer to use these standards than come up with your own.
The generator g is a primitive root of p. A primitive root is a number that when raised to all powers from 1 to p -1 mod p will ‘visit’ all the numbers on the wheel
Another way to think about this is to follow this test to see if g is a primitive root:
- List all the relative primes of p (let’s call them a). Two numbers are relatively prime if their GCD is 1
- Calculate ga mod p for every a. If the result == g in all cases then g is a primitive root.
The next step shows how Diffie Hellman relies on the discrete log problem for it’s security. Simply put if an eavesdropper knows the public variables i.e. g, p, ga(mod p) and gb(mod p) can they calculate the shared secret gab(mod p)? This turns out to be a very hard problem and there is no known way except to know the private numbers a and b.