Every https:// request starts with a TLS handshake. Your browser and the server have never met. The network between them could be a coffee shop’s Wi-Fi, your ISP, or anyone along the way. Still, within one round trip, they end up with a secret key nobody else has.
In this article, let’s see how TLS 1.3 (RFC 8446) does that, starting from the trick at the very bottom.
Problem
TLS has to give you three things, and they “should be true even in the face of an attacker who has complete control of the network” 1:
- Confidentiality: only the two ends can read the data.
- Integrity: nobody can change the data without being caught.
- Authentication: you know you’re talking to the real server.
The first two are easy once both sides share a secret key: encrypt everything with it. So the real problem is getting that key. You can’t just send it, because anyone on the network would see it. And you need to be sure that the other end of the key is actually the server.
Let’s take those one at a time.
Agreeing on a secret in public
In 1976, Whitfield Diffie and Martin Hellman published a way for two parties to agree on a secret while someone listens to every message 2. Here it is with small numbers you can check by hand.
Everyone, including the attacker (let’s call her Eve), knows two public numbers: g = 5 and p = 23.
- The client picks a secret, a = 6. It sends A = 56 mod 23 = 8.
- The server picks a secret, b = 15. It sends B = 515 mod 23 = 19.
- The client computes Ba = 196 mod 23 = 2.
- The server computes Ab = 815 mod 23 = 2.
Both got 2. That’s not luck: (5b)a and (5a)b are both 5ab. And 2 never went over the wire.
Eve saw 5, 23, 8 and 19. To get the 2, she needs a or b, i.e., she has to find the x where 5x mod 23 = 8. With 23 that’s easy, there are only 22 options. With big numbers, nobody knows a fast way to do it. Try it:
The numbers in the widget are toys. TLS 1.3 does the same thing on elliptic curves instead of plain modular arithmetic, e.g., x25519 1. Breaking x25519 takes about 2128 operations, “slightly under the standard 128-bit level” 3.
Each side sends its half in a field called the key share. Hold on to that, it matters later.
Eve in the middle
Diffie-Hellman gives you a key shared with someone. It doesn’t say who. If Eve doesn’t just listen but sits in the middle, she can run the exchange twice: once with the client, once with the server. Step through it:
The fix is to tie the key to an identity. In TLS 1.3 the server sends its certificate, which holds its public key, and then a CertificateVerify: a signature over the whole handshake so far, made with the matching private key 1. Eve can swap the key shares, but then the signature no longer matches what the client saw, and she can’t make a new one without the server’s private key.
Why your browser trusts that certificate in the first place (certificate authorities) is a separate story. TLS itself leaves how to interpret certificates to the protocols built on top of it 1.
Finally, both sides send a Finished message: a MAC over the entire handshake, using the new keys 1. If anyone changed anything along the way, this check fails.
The handshake
Now put it together. Here is the TLS 1.3 handshake next to TLS 1.2, one flight of messages at a time. Turn on Eve’s view to see what she can read:
To give you a summary:
- The client sends its key share in the very first message, ClientHello 1. That’s the trick that saves a round trip. In TLS 1.2, the client sends its share only after it has heard from the server 4.
- As soon as the server has both shares, it has the key. So “all handshake messages after the ServerHello are now encrypted” 1, including the certificate. In TLS 1.2, Eve could read the certificate, and with it the server’s name.
- The client can send its request right after its Finished. That’s one round trip in TLS 1.3, against two in TLS 1.2 1 4.
What if the client guessed the wrong group in its ClientHello? Then the server answers with a HelloRetryRequest, the client sends a new key share, and you pay one more round trip 1.
Forward secrecy
TLS 1.2 also allowed a different way to get the key, called RSA key exchange: the client “generates a 48-byte premaster secret, encrypts it using the public key from the server’s certificate” and sends it 4. No Diffie-Hellman at all.
That works, but think about what it means. Every session key was encrypted with one long-term key. If someone records your traffic today and steals the server’s private key next year, they can decrypt all of it.
TLS 1.3 removed it: “Static RSA and Diffie-Hellman cipher suites have been removed; all public-key based key exchange mechanisms now provide forward secrecy” 1. Each connection uses fresh Diffie-Hellman secrets, and they are thrown away afterwards. Stealing the server’s key later lets you pretend to be the server, but it doesn’t open old recordings. That’s forward secrecy.
0-RTT
TLS 1.3 can go even faster. If the client talked to the server before, they can keep a pre-shared key (PSK) from that session. Next time, the client can send data in its very first flight, before any reply. This is 0-RTT, or early data 1.
But the RFC marks it with an “IMPORTANT NOTE”. Early data “is not forward secret”, and “there are no guarantees of non-replay between connections” 1. Eve can record that first flight and send it to the server again. Fine for loading a page. Not fine for a request that changes something (e.g., placing an order), which could then run twice.
Final Thoughts
TLS 1.3 isn’t magic. A few limits worth knowing:
- A wrong guess in the key share costs an extra round trip 1.
- 0-RTT trades safety for speed: no forward secrecy and no replay protection for that early data 1.
- TLS hides what you send, not how much. “TLS does not hide the length of the data it transmits” 1.
- All of it depends on the certificate being right, and that trust comes from outside TLS.
But the core is still the 1976 trick: two numbers in public, a secret that never crosses the wire, and a signature to know who’s on the other end.
Footnotes
-
E. Rescorla, RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3, August 2018. Goals in section 1; differences from TLS 1.2 in 1.2; the handshake in section 2 and Figure 1; HelloRetryRequest in 2.1; 0-RTT in 2.3; named groups including x25519 in 4.2.7; key_share in 4.2.8; CertificateVerify and Finished in 4.4.3 and 4.4.4. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14 ↩15
-
Whitfield Diffie and Martin E. Hellman, New Directions in Cryptography, IEEE Transactions on Information Theory, 1976. ↩
-
A. Langley, M. Hamburg and S. Turner, RFC 7748: Elliptic Curves for Security, section 7. ↩
-
T. Dierks and E. Rescorla, RFC 5246: The Transport Layer Security (TLS) Protocol Version 1.2. The full handshake in Figure 1 (section 7.3); RSA key exchange in 7.4.7.1. ↩ ↩2 ↩3