← Back to blog

X25519 key pairs

Everything still open in the tunnel has the same fix: a handshake that creates fresh keys every time the tunnel starts. A handshake needs each peer to have an identity, something it can prove it owns. In WireGuard that’s an X25519 key pair, and this pull request adds them.

The tunnel doesn’t use them yet. It still encrypts with the key file, and the README says so at the top of the new section.

What a key pair is

Each peer generates a private key and keeps it secret. From it, it computes a public key and gives that to the other peer. The useful part is Diffie-Hellman: my private key combined with your public key gives the same 32 bytes as your private key combined with my public key. Both peers end up with a shared secret, and nobody watching the network can compute it, because neither private key was ever sent.

X25519 is Diffie-Hellman over Curve25519. Go has it in the standard library as crypto/ecdh, so there’s no curve code in the project, only a small package around it.

The identity package

The new internal/identity package has two types, PrivateKey and PublicKey, each wrapping the crypto/ecdh type. There are functions to generate a private key, parse either kind, and get the public key from a private one.

Keys are read and written as standard base64, the same format as wg genkey and wg pubkey. A key generated with wg works with govpn, and the other way round.

One thing is missing on purpose: PrivateKey has no String method. PublicKey has one, so it prints as base64. If PrivateKey had one too, a log.Printf("%v", key) written by mistake would put the private key in a log. Without it, getting the base64 out takes an explicit call to Encode. For the same reason, when ParsePrivateKey fails it doesn’t repeat the input in the error, and a test checks that an almost-valid key doesn’t show up in the error message.

The shared secret

SharedSecret is the Diffie-Hellman step. The handshake will call it, and it lives in this package because only this package can reach the private key:

func (k *PrivateKey) SharedSecret(peer *PublicKey) ([32]byte, error) {
	secretBytes, err := k.key.ECDH(peer.key)
	if err != nil {
		return [32]byte{}, fmt.Errorf("identity: computing shared secret: %w", err)
	}

	return [32]byte(secretBytes), nil
}

The error isn’t just for show. Some public keys, called low-order points, give an all-zero secret whatever the private key is. All zeros is the obvious one. A peer that sends one would make the “shared” secret something anyone can guess. crypto/ecdh returns an error for those, and SharedSecret passes it on instead of returning the zeros.

The secret also isn’t uniformly random, so it shouldn’t be used as an encryption key directly. It has to go through a key derivation function first, like the HKDF from the per-direction keys post. The comment on SharedSecret says so.

Testing against the RFC

RFC 7748, which defines X25519, includes a worked example: Alice’s and Bob’s private keys, their public keys, and the secret they share. The tests check that govpn gets the same public keys from those private keys and the same shared secret from both sides. Like the HKDF known-answer test, this catches a mistake that two copies of my own code would agree on.

Other tests cover parsing round trips, whitespace, invalid input, two generated keys never being equal, and SharedSecret rejecting three known low-order points.

genkey and pubkey

govpn now has two commands besides running the tunnel:

(umask 077; govpn genkey > private.key)
govpn pubkey < private.key > public.key

They work like WireGuard’s. genkey writes a new private key, and pubkey reads one on stdin and writes its public key. The umask 077 makes private.key readable only by its owner. Without it, the file usually ends up readable by every user on the machine.

Both commands write only the key to stdout, because stdout is usually redirected into a file. If they get extra arguments, the error and usage line go to stderr and govpn exits with status 2, so a usage message never ends up in a key file. A test uses a writer that always fails, like a full disk, and checks that both commands report the failed write instead of leaving an empty key file and no error.

A private key and the key file look the same: 32 bytes in base64. They’re different things, and the README says to keep them in separate files and not pass private.key to -key-file.

What’s next

The handshake itself. WireGuard uses the Noise protocol framework, which needs a hash function, a MAC, and HMAC on top of what’s already here. BLAKE2s is next.