What is the purpose of dhparam?

2.4K    Asked by asutos_8102 in Cyber Security , Asked on Sep 26, 2022

For a Diffie–Hellman (D-H) key exchange (TLS) the server generates a prime p and a generator g, which is a primitive root modulo p.

When setting up a web server with SSL/TLS (e.g. nginx) one can use a directive ssl_dhparam dhparam 4096.pem The dhparam 4096.pem file can be generated using openssl dhparam -out dhparam 4096.pem 4096

What exactly is the purpose of these D-H Parameters?

Can they be public? (i.e. can I publish my dhparam 4096.pem file?)

Here are the contents of my dhparam 4096.pem file:

That seems to be a hexadecimal representation of a 4096bit integer, is that correct?


Answered by Aswini Lobo

The dhparam defines how OpenSSL performs the Diffie-Hellman (DH) key-exchange. As you stated correctly they include a field prime p and a generator g. The purpose of the availability to customise these parameters is to allow everyone to use his / her own parameters for this. This can be used to prevent being affected from the Logjam attack (which doesn't really apply to 4096 bit field primes).

So what do they define?

A Diffie-Hellman key exchange operates as follows (for TLS 1.2 and before1):

The server Bob uses these parameters to calculate B=g^b mod p. He sends (B,g,p) to the client Alice who computes A=g^a mod p on her own along with K=B^a mod p. She sends A to Bob and he computes K=A^b mod p. As A^b=g^(a*b)=g^(b*a)=B^a mod p holds both parties will agree on a shared key. The parameters p and g define the security of this key-exchange. A larger p will make finding the shared secret K a lot harder, defending against passive attackers.

And why do you have to pre-compute them?

Finding the prime p means finding a value for p for which p=2q+1 holds, with q being a prime. p is then called a safe prime. Finding such primes is really computational intensive and can't be afforded on each connection, so they're pre-computed. Yes, there's no risk of publishing them. In fact they're sent out for every key-exchange that involves some Diffie-Hellman (DH) key exchange. There are even a few such parameters standardized for example in RFC 5114. The only possible problems with publishing may be that a powerful attacker may be interested in performing some computations on them, enabling him to perform the Logjam attack. However as your parameters use a 4096 bit field prime p this isn't a risk. To explain why publishing them isn't a risk you may want to take a look at the above key-exchange description and note that the parameters are only used as a base for the computations but all the secrets (a,b) are completely independent of g,p.

Your Answer


Parent Categories