Every piece of data your Tor client sends is encrypted three times before it leaves your machine, once for each relay in the path. Each relay peels off exactly one layer. That is the whole trick behind the name, and it is a more literal description than most technical metaphors manage.

The idea predates Tor. Paul Syverson, David Goldschlag and Michael Reed worked out onion routing at the US Naval Research Laboratory in the mid-1990s, and Tor (originally "The Onion Router") was the second-generation design that Roger Dingledine and Nick Mathewson built with Syverson in the early 2000s. If you want the people and politics, that story lives in our history of Tor. This page is about the mechanics.

Three keys, three layers

Before your client can send anything, it builds a circuit: a path through a guard relay, a middle relay and an exit relay. During that build it runs a separate key agreement with each relay, so it ends up holding three independent sets of symmetric keys. The guard knows only the first set, the middle only the second, the exit only the third. The handshake that produces these keys is covered in detail in the ntor handshake article.

When you want to send a chunk of data out through the exit, your client takes the 509-byte payload of a relay cell and does this:

  1. Encrypt it with the exit's forward key.
  2. Encrypt the result with the middle's forward key.
  3. Encrypt that with the guard's forward key.
  4. Send it down the TLS connection to the guard.

The guard decrypts with its key and gets something that still looks like random bytes, so it forwards the cell to the middle. The middle decrypts and again sees noise, so it forwards to the exit. The exit decrypts the last layer and finds a readable relay cell: a command, a stream ID, a length and some data. At that point it knows what to do, which is usually to write the data onto a TCP connection to some website.

Traffic coming back works the other way round. The exit encrypts the response once with its backward key, the middle adds a layer, the guard adds a third, and your client strips all three. Nobody on the path ever needs to strip more than one.

The current relay encryption uses AES-128 in counter mode, with a separate key and counter for each direction at each hop. Because counter mode is a stream cipher, the cell size never changes as layers are added or removed, which matters a lot for not leaking information through packet sizes. A redesign of this layer, called Counter Galois Onion, has been specified to fix some weaknesses in the old scheme (mainly that it's malleable, which makes tagging attacks possible) and is being rolled out in newer versions.

How a relay knows a cell is meant for it

This is the part people usually skip, and it's a neat bit of design. A relay can't tell from a cell's header whether the cell is addressed to it or should be passed on. The header only carries a circuit ID, and that's the same either way.

So each relay, after removing its layer, checks two things inside the relay header. There's a two-byte "recognized" field that should be zero, and a four-byte digest that should match a running hash the relay keeps over everything it has seen on that circuit. If both check out, the cell is for this relay. If not, it's still encrypted for someone further along, and the relay forwards it. A random-looking cell has a tiny chance of passing both checks by accident, which is why the digest is there and not just the zero field.

What each relay actually learns

RelayKnowsDoesn't know
GuardYour IP address, that you use Tor, timing and volume of your trafficWhere your traffic goes, what it says
MiddleThe guard's IP and the exit's IPYou, your destination, your content
ExitThe destination host and port, unencrypted content if you didn't use TLSWho you are

That table is the security argument for Tor in one place. No single relay can link you to your destination. To do that, an adversary has to see both ends, and once someone can see both ends, the encryption layers don't matter much, because they can match the timing of packets going in with packets coming out. That's a correlation attack, and onion routing by itself was never designed to stop it. The original Tor design paper says as much.

Why three hops

One hop is just a proxy: the single relay knows both who and where. Two hops would split that knowledge, and in a narrow sense that's enough. The reason for a third is mostly about the guard. With two hops, the exit knows which guard you use, and since you keep the same guard for months, an exit operator (or anyone watching a lot of exits) could quickly connect "traffic from guard G" with "probably the same person every time". A middle relay between them breaks that.

Going beyond three gets you very little. The realistic threat is end-to-end observation, and a fourth or fifth relay sits in the middle where it doesn't affect that at all. What it does do is add latency and multiply the load on the network. You can't easily configure Tor for longer exit circuits anyway, and you shouldn't want to. Onion service connections are a different case: they end up with six relays because both sides build their own three-hop circuits to a meeting point, which is explained in how onion services work. Every address in our onion directory is reached that way, with no exit involved at all.

Where the layers stop

Tor's encryption ends at the exit. People forget this more than anything else on this page. If you log in to a plain HTTP site over Tor, the exit relay sees your password exactly as the site does. Tor Browser now defaults to HTTPS-Only mode, which helps a lot, but other applications you route through Tor don't get that protection for free.

Even with HTTPS, the exit sees the hostname (via SNI or the DNS lookup it performs on your behalf) and the size and timing of what goes back and forth. It doesn't see URLs, cookies or page contents. That's a reasonable split, and it's why "use HTTPS" remains the single most useful piece of advice for anyone using exits.

If you want to see what goes over the wire at the byte level, the cell layout is in Cells, Circuits and Streams, and the authoritative version is the Tor protocol specification, which is surprisingly readable once you know the vocabulary.

#onion-routing #tor-network #encryption #relays