Every time your Tor client adds a hop to a circuit, it runs a small Diffie-Hellman exchange with that relay. The exchange is called ntor, it uses Curve25519, and it fits in a single round trip: 84 bytes from the client, 64 bytes back. Out of that come the AES keys and digest seeds used for one layer of the onion.
It has three jobs. The client must end up sharing secret keys with the relay. The client must be sure it's talking to the relay it chose from the consensus and not someone in between. And the keys must have forward secrecy, so that stealing the relay's long-term keys later doesn't decrypt today's traffic. Note what's not on the list: the relay never learns who the client is. Authentication only goes one way, which is the whole point.
Where ntor came from
Tor's original handshake, TAP, used 1024-bit RSA to encrypt half of a 1024-bit Diffie-Hellman exchange. It worked but it was slow and the key sizes were aging badly. Ian Goldberg, Douglas Stebila and Berkant Ustaoglu published ntor in a 2013 paper on one-way authenticated key exchange, with a security proof, and Tor adopted it in the 0.2.4 series. TAP lingered for years for compatibility and has since been phased out.
What the client knows going in
From the relay's descriptor (via the consensus) the client already has two things:
ID: the relay's 20-byte identity digest (the SHA-1 of its RSA identity key).B: the relay's current ntor onion key, a 32-byte Curve25519 public key. The relay holds the private half,b.
Onion keys are medium-term. Relays rotate them every few weeks and keep the old one around for a grace period so circuits that were mid-build don't fail. That rotation is what gives some protection even if a relay's disk is later seized: old onion keys are gone.
The exchange
The client makes a fresh ephemeral keypair, x and X, and sends this inside a CREATE2 cell (or inside EXTEND2 for later hops), handshake type 2:
ID (20 bytes) which relay I think you are
B (32 bytes) which onion key I'm using
X (32 bytes) my ephemeral public key
= 84 bytes
Sending ID and B lets the relay check the client is targeting the right relay and the right key (it might have two during rotation). If they don't match, it rejects the handshake.
The relay makes its own ephemeral pair, y and Y, and computes two Diffie-Hellman results: X^y (ephemeral with ephemeral) and X^b (client ephemeral with relay's onion key). It concatenates them with the public values into one long string:
secret_input = X^y | X^b | ID | B | X | Y | PROTOID
PROTOID = "ntor-curve25519-sha256-1"
From that it derives a key seed and a verification value using HMAC-SHA256 with different fixed tags, then computes an authenticator:
KEY_SEED = H(secret_input, t_key)
verify = H(secret_input, t_verify)
auth_input = verify | ID | B | Y | X | PROTOID | "Server"
AUTH = H(auth_input, t_mac)
Here H(x, t) is HMAC-SHA256 keyed with the tag t, and the tags are the PROTOID with suffixes like ":key_extract", ":verify" and ":mac". The relay sends back Y and AUTH in a CREATED2 cell. That's 64 bytes.
The client computes the same two DH values from its side, Y^x and B^x, which are equal to the relay's X^y and X^b. It builds the same secret_input, recomputes AUTH and checks that it matches. It also checks that neither DH result is the all-zero point, which would indicate a malformed key.
Why two Diffie-Hellman values
This is the part worth understanding. Each DH term does one job.
X^y is between two ephemeral keys that are thrown away as soon as the handshake ends. Even if someone records the whole exchange and later steals every key the relay owns, they can't recompute it. That gives forward secrecy.
X^b involves the relay's onion key. Only the real relay has b, so only the real relay can compute this value, and therefore only the real relay can produce a correct AUTH. A man in the middle could make up its own Y, but without b its AUTH would fail the client's check. That's the authentication.
Mix them together and you get both properties from one round trip, without signatures. Signing is slower, and on a busy relay doing thousands of handshakes a second, the cost difference shows. This is also why ntor was such an improvement over TAP for relay CPU load: Curve25519 scalar multiplications are cheap.
Turning the secret into keys
Both sides feed the shared secret into HKDF-SHA256 (RFC 5869), with t_key as the salt and ":key_expand" as the info string, and read off as many bytes as they need:
Df 20 bytes forward digest seed
Db 20 bytes backward digest seed
Kf 16 bytes forward AES-128 key
Kb 16 bytes backward AES-128 key
KH 20 bytes (used for onion service binding)
The AES keys run in counter mode for that hop's layer of encryption, one per direction. The digest seeds initialise the running SHA-1 digests that fill the 4-byte digest field in relay cells, which is how a relay recognises cells meant for it. The layout is in Cells, Circuits and Streams.
Repeat this three times and you have three independent sets of keys, one per hop, which is everything onion routing needs. The relays never share keys with each other, and the guard never sees the handshake data for later hops in the clear: it's inside an EXTEND2 cell encrypted to the middle relay, which is in turn inside the guard's layer.
ntor v3
The original ntor has no room for anything except key material. That became a problem when Tor needed clients and relays to agree on options per circuit, for example whether to use the newer congestion control. ntor v3 (handshake type 3) keeps the same basic Diffie-Hellman structure but lets the client send an encrypted message to the relay alongside its key share, and lets the relay reply with one. Those messages carry extensions. Because they're encrypted to the relay's onion key, intermediate hops can't read or tamper with them. ntor v3 shipped around the 0.4.7 series, and it's the handshake used when congestion control is negotiated with exits.
Onion services use yet another variant, sometimes called hs-ntor, for the introduction and rendezvous step, where the service authenticates with keys derived from its onion address rather than a relay identity. The idea is the same.
What ntor doesn't protect
It authenticates the relay you picked. It doesn't tell you whether that relay is honest. A malicious relay runs the handshake perfectly and then logs what it can see. The handshake also does nothing about traffic analysis, and it isn't post-quantum: someone storing traffic now and holding a large enough quantum computer later could, in principle, recover the Curve25519 secrets. The developers have looked at hybrid post-quantum handshakes, and the extensible format of ntor v3 makes that easier to add. For more about the spec text itself, the CREATE and CREATED cells section of the Tor spec has the formal definitions and exact tag strings.