← Back to blog

The WireGuard handshake, message by message

With the building blocks in place, this pull request builds the handshake itself: two messages that let two peers agree on transport keys using only their key pairs. The tunnel doesn’t run it yet. main still derives its keys from the key file, and wiring the handshake in is the next pull request.

What the handshake has to do

When it’s done, both peers should have the same two transport keys, one per direction, like the per-direction keys from before. But now:

  • The keys are new every time. Each handshake uses fresh random keys, so a restart no longer reuses nonces, and a key stolen later can’t decrypt old traffic.
  • Only the two configured peers can finish it. Each side proves it holds its private key.
  • The initiator’s identity stays hidden. Someone watching the network can’t tell who is connecting.
  • An old handshake can’t be replayed.

The ingredients

Each peer has a static key pair, its long-term identity from the key pairs post. For each handshake, each side also generates an ephemeral key pair, used once and thrown away.

That gives four possible Diffie-Hellman combinations, and the handshake uses all of them. They’re named by which keys go in, e for ephemeral and s for static, initiator first:

  • es: the initiator’s ephemeral key with the responder’s static key
  • ss: both static keys
  • ee: both ephemeral keys
  • se: the responder’s ephemeral key with the initiator’s static key

Each result is mixed into the chaining key from the previous post. The initiator knows the responder’s static public key before it starts. That’s the “K” in the IK pattern, and it’s why the first message can already be encrypted.

Message 1: initiation

The initiator sends a 148-byte message:

  1. Its ephemeral public key, in the clear.
  2. Its static public key, encrypted with a key derived after es. Only the responder can compute that, so only the responder learns who is calling.
  3. A timestamp, encrypted with a key derived after ss. Decrypting it proves the sender holds the static private key, and the responder rejects it unless it’s newer than the last one it accepted.
  4. A random index, and the mac1 field.

The responder repeats every step from its side. If its state ends up different, decryption fails. It also checks that the static key inside is the configured peer’s, not just any valid key.

Message 2: response

The responder answers with a 92-byte message:

  1. Its own ephemeral public key, in the clear.
  2. ee and se are mixed in. ee is what gives forward secrecy: it depends only on keys that are deleted after the handshake. se means only the real initiator can follow from here.
  3. An optional preshared key is mixed in. WireGuard supports one as an extra layer, and here it’s all zeros for now.
  4. An empty encrypted field. It holds no data, only the 16-byte tag. If the initiator can verify that tag, the responder reached the same state, which proves it’s the real responder.

Both sides then call KDF2 on the final chaining key to get the two transport keys, and set the state to zero, as the whitepaper requires.

mac1 comes first

Diffie-Hellman is the expensive part. If every datagram that looked like a handshake message cost a few of them, anyone could keep a peer busy by sending junk. So every message carries mac1, a MAC over the rest of the message, keyed with the receiver’s public key. Checking it is cheap, and it’s the first thing the receiver does after checking the size and type. Someone who doesn’t know the peer’s public key can’t produce a valid one, so their messages are dropped before any Diffie-Hellman.

The comparison uses hmac.Equal, which takes the same time wherever the bytes differ. A normal comparison stops at the first wrong byte, and the timing could tell an attacker how much of a forged mac1 was right.

WireGuard also has a mac2 field and cookie replies for peers under load. Those are left for later.

A failed message changes nothing

Every function that reads a message works on a copy of the handshake state, and saves it only if every check passed:

state := *h.state

The state is two arrays, so this one line is a full, independent copy. If decryption fails halfway, the copy is thrown away. A forged message can’t disturb a real handshake in progress or move the last accepted timestamp, and the tests check that a real message is still accepted after a forged one.

Testing against an independent implementation

The round-trip tests run the whole handshake and check that both sides end with the same keys. That catches a lot, but not everything. If I got a step wrong in the same way on both sides, they’d still agree with each other, and still fail with every real WireGuard peer.

So the pull request includes testdata/handshake_vectors.py, a second implementation of the handshake, written in Python from the whitepaper and not from my Go code. It uses only the standard library. Python has BLAKE2s and HMAC built in, but not X25519 or ChaCha20-Poly1305, so the script includes its own, and checks them against the test vectors in their RFCs before it runs anything else.

Given fixed keys, timestamp, and indices, the script prints both messages and both transport keys, and TestHandshakeKnownAnswer checks the Go code against them byte for byte. For that, the functions that pick random values have …With versions that take the ephemeral key, index, and time as arguments.

Real receiver indices

Until now, every transport message carried a receiver index of 0. In the handshake, each side picks a random 32-bit index and sends it to the other. From then on, each side writes the peer’s index into every message it sends, so the receiver can tell which session a message belongs to.

session.New now takes both indices. Open checks the index before decrypting, so a message meant for another session costs no decryption and fails with a new error, ErrWrongIndex. Since the handshake doesn’t run yet, main passes 0 for both.

What’s next

Wiring the handshake into the tunnel: running it when the tunnel starts, and using its keys and indices instead of the key file. That’s what finally fixes nonce reuse after a restart and gives the tunnel forward secrecy.