The building blocks of the WireGuard handshake
The handshake is a long sequence of small cryptographic steps: hash this, derive a key from that, encrypt a field, mix the result into a running state. This pull request adds those steps as a new package, internal/noise, without the handshake itself. The tunnel doesn’t use any of it yet.
Why the pieces come first
If one step of the handshake is wrong, the whole handshake fails with a single unhelpful error: some field doesn’t decrypt. Nothing says which of the steps before it went wrong. So I built each step on its own and tested it against values computed outside Go, with Python’s hashlib and hmac modules. When the handshake fails later, I’ll know the pieces are right and the mistake is in how they’re put together.
All of these come from section 5.4 of the WireGuard whitepaper, and the code uses the same names.
Three ways to use BLAKE2s
WireGuard uses BLAKE2s as its hash function, in three different ways:
hash: plain BLAKE2s with a 32-byte output. It keeps a running hash of everything sent during the handshake.mac: keyed BLAKE2s with a 16-byte output. It’s used for themac1field of each handshake message.hmac: HMAC built on BLAKE2s, with a 32-byte output. It’s only used inside the key derivation functions.
mac is the easy one to get wrong. It looks like it could be HMAC, or the first 16 bytes of a keyed 32-byte hash. It’s neither. Keyed BLAKE2s is a mode of BLAKE2s itself, and BLAKE2s mixes the output length into its starting state, so a 16-byte output isn’t the first half of a 32-byte one. There’s a test for exactly those two mistakes.
Deriving keys
The handshake derives a new key at almost every step. WireGuard does it with three functions, KDF1, KDF2, and KDF3, which return one, two, or three 32-byte outputs:
func kdf2(key [32]byte, input []byte) ([32]byte, [32]byte) {
t0 := hmac(key, input)
t1 := hmac(t0, []byte{0x1})
t2 := hmac(t0, t1[:], []byte{0x2})
return t1, t2
}
This is HKDF, the same thing I used for the per-direction keys, with BLAKE2s instead of SHA-256 and an empty label. Besides the Python values, a test checks kdf3 against Go’s own crypto/hkdf running on BLAKE2s.
Where every handshake starts
Every WireGuard handshake starts from the same two values, a chaining key and a hash, computed from two fixed strings:
const (
construction = "Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s"
identifier = "WireGuard v1 zx2c4 [email protected]"
)
The first names the protocol: Noise, the IK pattern, with X25519, ChaCha20-Poly1305, and BLAKE2s. Every WireGuard peer uses these exact strings, so a typo would break every handshake with one confusing error. A test compares the two starting values with the ones wireguard-go hardcodes.
The handshake state
During the handshake, each peer keeps two 32-byte values:
- The chaining key collects the secrets. Every Diffie-Hellman result is mixed into it, so only the two peers can compute it. The keys for each encrypted field, and at the end the transport keys, are derived from it.
- The hash collects the transcript. Everything sent is mixed into it, and it’s used as associated data for every encrypted field. If any earlier byte of the handshake was changed, the next field fails to decrypt.
Four small methods update them, depending on whether a step adds a secret, adds something to the transcript, or needs a key for the next field. They’re one or two lines each, and the tests check that each one changes only what it should.
A fixed nonce, used safely
The handshake encrypts its fields with ChaCha20-Poly1305 and a nonce of all zeros. After the encryption post, that should look alarming: reusing a nonce with the same key is exactly the mistake the per-direction keys post fixed.
It’s safe here because no key is ever used twice. The handshake derives a fresh key before each encrypted field, so every key encrypts exactly one thing. The function’s comment says so, and says never to call it twice with the same key.
Timestamps
The first handshake message carries an encrypted timestamp, and the responder rejects any timestamp that isn’t newer than the last one it accepted. That stops someone from recording a handshake message and sending it again later.
The format is TAI64N: 8 bytes of seconds and 4 bytes of nanoseconds, both big-endian. Big-endian matters here, because it means comparing the raw bytes gives the same order as comparing the times. The responder can check “is this newer” with bytes.Compare, without decoding anything.
What’s next
Before using any of this in the tunnel, the forwarding loops need tests. Right now they can only run as root, with a real TUN device and socket.