TLS & Perfect Forward Secrecy

Vincent Bernat

Once the pri­vate key of some HTTPS web site is com­pro­mised, an at­tacker is able to build a man-in-the-mid­dle at­tack to in­ter­cept and de­crypt any com­mu­ni­ca­tion with the web site. The first step against such an at­tack is the re­vo­ca­tion of the as­so­ci­ated cer­tifi­cate through a CRL or a pro­to­col like OCSP. Un­for­tu­nately, the at­tacker could also have recorded past com­mu­ni­ca­tions pro­tected by this pri­vate key and there­fore de­crypt them.

For­ward se­crecy al­lows to­day in­for­ma­tion to be kept se­cret even if the pri­vate key is com­pro­mised in the fu­ture. Achiev­ing this prop­erty is usu­ally costly and there­fore, most web servers do not en­able it on pur­pose. Google re­cently an­nounced sup­port of for­ward se­crecy on their HTTPS sites. Adam Lan­g­ley wrote a post with more de­tails on what was achieved to in­crease ef­fi­ciency of such a mech­a­nism: with a few fel­low peo­ple, he wrote an ef­fi­cient im­ple­men­ta­tion of some el­lip­tic curve cryp­tog­ra­phy for OpenSSL.

Up­date (2019-08)

While the con­tent of this ar­ti­cle is still tech­ni­cally sound, en­sure you un­der­stand it was writ­ten by the end of 2011 and there­fore doesn’t take into ac­count many im­por­tant as­pects, like the fall of RC4 as an ap­pro­pri­ate ci­pher and the ubiq­uity of for­ward se­crecy in ac­tual de­ployments.

Without forward secrecy#

To un­der­stand the prob­lem when for­ward se­crecy is ab­sent, let’s look at the clas­sic TLS hand­shake when us­ing a ci­pher suite like AES128-SHA. Dur­ing this hand­shake, the server will present its cer­tifi­cate and both the client and the server will agree on a mas­ter secret.

TLS full handshake
Full TLS handshake

This se­cret is built from a 48-byte pre­mas­ter se­cret gen­er­ated and en­crypted by the client with the pub­lic key of the server. It is then sent in a Client Key Ex­change mes­sage to the server dur­ing the third step of the TLS hand­shake. The mas­ter se­cret is de­rived from this pre­mas­ter se­cret and ran­dom val­ues sent in clear-text with Client Hello and Server Hello messages.

This scheme is se­cure as long as only the server is able to de­crypt the pre­mas­ter se­cret (with its pri­vate key) sent by the client. Let’s sup­pose that an at­tacker records all ex­changes be­tween the server and clients dur­ing a year. Two years later, the server is de­com­mis­sioned and sent for re­cy­cling. The at­tacker is able to re­cover the hard drive with the pri­vate key. They can now de­crypt any ses­sion they recorded: the en­crypted pre­mas­ter se­cret sent by a client is de­crypted with the pri­vate key and the mas­ter se­cret is de­rived from it. The at­tacker can now re­cover pass­words and other sen­si­tive in­for­ma­tion that can still be valu­able today.

The main prob­lem lies in the fact that the pri­vate key is used for two pur­poses: au­then­ti­ca­tion of the server and en­cryp­tion of a shared se­cret. Au­then­ti­ca­tion only mat­ters while the com­mu­ni­ca­tion is es­tab­lished, but en­cryp­tion is ex­pected to last for years.

Diffie-Hellman with discrete logarithm#

One way to solve the prob­lem is to keep us­ing the pri­vate key for au­then­ti­ca­tion but use an in­de­pen­dent mech­a­nism to agree on a shared se­cret. For­tu­nately, there ex­ists a well-known pro­to­col for this: the Diffie-Hell­man key ex­change. It is a method of ex­chang­ing keys with­out any prior knowl­edge. Here is how it works in TLS:

  1. The server needs to gen­er­ate once (for ex­am­ple, with openssl dhparam command):

  2. The server picks a ran­dom in­te­ger aa and com­pute ga mod pg^a \bmod p. Af­ter send­ing its reg­u­lar Cer­tifi­cate mes­sage, it will also send a Server Key Ex­change mes­sage (not in­cluded in the hand­shake de­picted above) con­tain­ing, un­en­crypted but signed with its pri­vate key for au­then­ti­ca­tion pur­pose:

    • ran­dom value from the Client Hello message,
    • ran­dom value from the Server Hello message,
    • pp, gg,
    • ga mod p=Ag^a\bmod p=A.
  3. The client checks that the sig­na­ture is cor­rect. It also picks a ran­dom in­te­ger bb and sends gb mod p=Bg^b \bmod p=B in a Client Key Ex­change mes­sage. It will also com­pute Ab mod p=gab mod pA^b\bmod p=g^{ab}\bmod p which is the pre­mas­ter se­cret from which the mas­ter se­cret is de­rived.

  4. The server will re­ceive BB and com­pute Ba mod p=gab mod pB^a\bmod p=g^{ab}\bmod p which is the same pre­mas­ter se­cret known by the client.

Again, the pri­vate key is only used for au­then­ti­ca­tion pur­poses. An eaves­drop­per will only know pp, gg, ga mod pg^a\bmod p and gb mod pg^b\bmod p. Com­put­ing gab mod pg^{ab}\bmod p from these val­ues is the dis­crete log­a­rithm prob­lem for which there is no known ef­fi­cient so­lu­tion.

Be­cause the Diffie-Hell­man ex­change de­scribed above al­ways uses new ran­dom val­ues aa and bb, it is called Ephemeral Diffie-Hell­man (EDH or DHE). Ci­pher suites like DHE-RSA-AES128-SHA use this pro­to­col to achieve per­fect for­ward se­crecy.1

To achieve a good level of se­cu­rity, pa­ram­e­ters of the same size as the key are usu­ally used (the se­cu­rity pro­vided by the dis­crete log­a­rithm prob­lem is about the same as the se­cu­rity pro­vided by fac­tori­sa­tion of two large prime num­bers) and there­fore, the ex­po­nen­ti­a­tion op­er­a­tions are pretty slow as we can see in the bench­mark below:

Graphical comparison of speed with and without DHE
Performances of stud on 6 cores, with and without DHE with a 1024-bit key

Up­date (2015-05)

The Log­jam at­tack demon­strated that the se­cu­rity of Diffie-Hell­man is lower than ex­pected, no­tably when the prime is shared by many servers as this is usu­ally the case. In this case, an at­tack against this prime can be pre­com­puted and used to ef­fi­ciently break con­nec­tions us­ing the same prime. En­sure that the DH pa­ram­e­ter matches the size of the as­so­ci­ated RSA key (at least 2048 bits).

Diffie-Hellman with elliptic curves#

For­tu­nately, there ex­ists an­other way to achieve a Diffie-Hell­man key ex­change with the help of el­lip­tic curve cryp­tog­ra­phy which is based on the al­ge­braic struc­ture of el­lip­tic curves over fi­nite fields. To get some back­ground on this, be sure to check first Wikipedia ar­ti­cle on el­lip­tic curves. El­lip­tic curve cryp­tog­ra­phy al­lows one to achieve the same level of se­cu­rity as RSA with smaller keys. For ex­am­ple, a 224-bit el­lip­tic curve is be­lieved to be as se­cure as a 2048-bit RSA key.

Some theory#

Diffie-Hell­man key ex­change de­scribed above can eas­ily be trans­lated to el­lip­tic curves. In­stead of defin­ing pp and gg, you get some el­lip­tic curve, y2=x3+αx+βy^2=x^3+\alpha x+\beta, a prime pp and a base point GG. All these pa­ram­e­ters are pub­lic. In fact, while they can be gen­er­ated by the server, this is a dif­fi­cult op­er­a­tion and they are usu­ally cho­sen among a set of pub­lished ones.

The use of el­lip­tic curves is an ex­ten­sion of TLS de­scribed in RFC 4492. Un­like with the clas­sic Diffie-Hell­man key ex­change, the client and the server need to agree on the var­i­ous pa­ram­e­ters. Most of this agree­ment is done in­side Client Hello and Server Hello mes­sages. While it is pos­si­ble to de­fine some ar­bi­trary pa­ram­e­ters, web browsers will only sup­port a hand­ful of pre­de­fined curves, usu­ally NIST P-256, P-384 and P-521. From here, the key ex­change with el­lip­tic curves is pretty sim­i­lar to the clas­sic Diffie-Hellman one:

  1. The server picks a ran­dom in­te­ger aa and com­putes aGaG which will be sent, un­en­crypted but signed with its pri­vate key for au­then­ti­ca­tion pur­poses, in a Server Key Ex­change mes­sage.
  2. The client checks that the sig­na­ture is cor­rect. It also picks a ran­dom in­te­ger bb and sends bGbG in a Client Key Ex­change mes­sage. It will also com­pute b⋅aG=abGb\cdot aG=abG which is the pre­mas­ter se­cret from which the mas­ter se­cret is de­rived.
  3. The server will re­ceive bGbG and com­pute a⋅bG=abGa\cdot bG=abG which is the same pre­mas­ter se­cret known by the client.

An eaves­drop­per will only see aGaG and bGbG and won’t be able to com­pute ef­fi­ciently abGabG.

Us­ing ECDHE-RSA-AES128-SHA ci­pher suite (with P-256 for ex­am­ple) is al­ready a huge speed im­prove­ment over DHE-RSA-AES128-SHA thanks to the re­duced size of the var­i­ous pa­ram­e­ters involved.

Web browsers only sup­port a hand­ful of well-de­fined el­lip­tic curves, cho­sen to ease an ef­fi­cient im­ple­men­ta­tion. Bodo Möller, Emilia Käsper and Adam Lan­g­ley have pro­vided 64-bit op­ti­mized ver­sions of NIST P-224, P-256 and P-521 for OpenSSL. To get even more de­tails on the mat­ter, you can read the end of the in­tro­duc­tion on el­lip­tic curves from Adam Lan­g­ley, then a short pa­per from Emilia Käsper which presents a 64-bit op­ti­mized im­ple­men­ta­tion of the NIST el­lip­tic curve NIST P-224.

In practice#

First, keep in mind that el­lip­tic curve cryp­tog­ra­phy is not sup­ported by all browsers. Re­cent ver­sions of Fire­fox and Chrome should han­dle NIST P-256, P-384 and P-521 but for In­ter­net Ex­plorer on Win­dows XP, you are cur­rently out of luck. There­fore, you need to keep ac­cept­ing other ci­pher suites.

You need a re­cent ver­sion of OpenSSL. Sup­port for ECDHE ci­pher suites has been added in OpenSSL 1.0.0. Check with openssl ciphers ECDH that your ver­sion sup­ports them. If you want to use the 64-bit op­ti­mized ver­sion, you need to run a snap­shot of OpenSSL 1.0.1, con­fig­ured with enable-ec_​nistp_​64_​gcc_​128 op­tion. A re­cent GCC is also re­quired in this case.

Next, you need to choose the ap­pro­pri­ate ci­pher suites. If for­ward se­crecy is an op­tion for you, you can opt for ECDHE-RSA-AES128-SHA:​AES128-SHA:​RC4-SHA ci­pher suites which should be com­pat­i­ble with most browsers. If you re­ally need for­ward se­crecy, you may opt for ECDHE-RSA-AES128-SHA:​DHE-RSA-AES128-SHA:​EDH-DSS-DES-CBC3-SHA instead.

Then, you need to en­sure the or­der of ci­pher suites is re­spected. On ng­inx, this is done with ssl_​prefer_​server_​ciphers on. On Apache, this is SSLHonorCipherOrder on.

Up­date (2011-11)

You need to check ECDHE sup­port for your web server. For ng­inx, the sup­port has been added in 1.0.6 and 1.1.0. The curve se­lected de­faults to NIST P-256. You can spec­ify an­other one with ssl_​ecdh_​curve di­rec­tive. For Apache, it has been added in 2.3.3 and does not ex­ist in the cur­rent sta­ble branch. Adding sup­port for ECDHE is quite easy. You can check how I added it in stud. This is­sue also ex­ists for DHE ci­pher suites, in which case you also might have to spec­ify DH pa­ram­e­ters to use (gen­er­ated with openssl dhparam) us­ing some spe­cial di­rec­tive or by ap­pend­ing the pa­ram­e­ters to the cer­tifi­cate. Check Im­merda Tech­blog ar­ti­cle for more back­ground about this point.

The im­ple­men­ta­tion of TLS ses­sion tick­ets may be in­com­pat­i­ble with for­ward se­crecy, de­pend­ing on how they are im­ple­mented. When they are pro­tected by a ran­dom key gen­er­ated at the start of the server, the same key could be used for months. Some im­ple­men­ta­tions2 may de­rive the key from the pri­vate key. In this case, for­ward se­crecy is bro­ken. If for­ward se­crecy is a re­quire­ment for you, you need to ei­ther dis­able tick­ets or en­sure that key ro­ta­tion hap­pens often.

Check with openssl s_​client -tls1 -cipher ECDH -connect 127.0.0.1:443 that ev­ery­thing works as expected.

Some benchmarks#

With the help of the mi­cro-bench­mark tool that I de­vel­oped for my pre­vi­ous ar­ti­cle, we can com­pare the ef­fi­ciency of ci­pher suites pro­vid­ing for­ward secrecy:

Speed comparison with/without DHE/ECDHE
Compared performance for 1000 handshakes of various cipher suites (RSA 2048, DHE, ECDHE, optimized ECDHE)

I have used a snap­shot of OpenSSL 1.0.1 (2011/11/25). The op­ti­mized ver­sion of ECDHE is the one you get by us­ing enable-ec_​nistp_​64_​gcc_​128 op­tion when con­fig­ur­ing OpenSSL.

Let’s fo­cus on the server part. En­abling DHE-RSA-AES128-SHA ci­pher suite hin­ders the per­for­mance of TLS hand­shakes by a fac­tor of 3. Us­ing ECDHE-RSA-AES128-SHA in­stead only adds an over­head of 27%. How­ever, if we use the 64-bit op­ti­mized ver­sion, the cost is only 15%. The over­head is only per full TLS hand­shake. If 3 out of 4 of your hand­shakes are re­sumed, you need to ad­just the numbers.

Your mileage may vary but the com­pu­ta­tional cost for en­abling per­fect for­ward se­crecy with an ECDHE ci­pher suite seems a small sac­ri­fice for bet­ter security.