One key per direction
The previous post ended with a known flaw. Both peers encrypted with the same key, and both started their counters at 0, so packet n from each side was sealed with the same key and nonce. This pull request gives each direction of the tunnel its own key.
Why reusing a nonce is so bad
ChaCha20 turns the key and nonce into a keystream and XORs it with the packet. Two packets sealed with the same key and nonce use the same keystream, so XORing the two ciphertexts cancels it out and leaves the XOR of the two plaintexts. IP packets are full of predictable fields, so that leaks a lot. The Poly1305 key is also derived from the key and nonce, and reusing it lets an attacker forge packets that pass authentication.
Two keys from one file
There were two ways to get a key per direction: two key files, or one file and a way to derive two keys from it. I kept the single file and derive both keys with HKDF-SHA256, the standard way to turn one secret into several independent keys. Go has it in the standard library as crypto/hkdf.
Each key gets its own label:
const (
infoInitiatorToResponder = "govpn v1 initiator to responder"
infoResponderToInitiator = "govpn v1 responder to initiator"
)
func deriveKey(shared Key, info string) (Key, error) {
key, err := hkdf.Key(sha256.New, shared[:], nil, info, chacha20poly1305.KeySize)
if err != nil {
return Key{}, fmt.Errorf("hkdf: %w", err)
}
return Key(key), nil
}
The label names the protocol, a version, and the direction. Different labels give unrelated keys, and the version means a future change can’t collide with keys from today’s format.
Who is who
Both peers now derive the same two keys, but each one has to know which key to send with. A new -initiator flag decides it: the initiator seals with the initiator-to-responder key and opens with the other, and the responder does the opposite. Initiator and responder are the names WireGuard uses for the two sides of its handshake, which is where this is going.
The flag must be set on exactly one peer. If both or neither set it, both peers seal with the same key again. Nothing is logged, because neither peer knows how the other was started and every packet just fails to decrypt. The README says so, and in the Docker setup govpn-a is the initiator.
Inside the session package the change is small. A Session now holds a send cipher, a receive cipher, and a counter for the send side only.
Testing the keys themselves
The tests check that two sessions built as the two ends of a tunnel can talk to each other, and that their first messages, both sealed with counter 0, produce different ciphertexts. With one shared key they would be identical.
Those tests only compare derived keys with each other. If I changed the hash, the salt, or a label, both directions would change together and every test would still pass, but peers running different versions would derive different keys and the tunnel would silently stop working. So there is also a known-answer test that checks DeriveKeys against keys I computed outside Go, with HKDF written by hand from RFC 5869.
What’s still open
This fixes nonce reuse between the two peers, but not across restarts. The keys depend only on the key file, so a restarted peer derives the same send key, starts its counter at 0 again, and reuses nonces it already sent. There is still no forward secrecy and no replay protection either.
All three have the same fix: a handshake that creates new keys every time the tunnel starts. DeriveKeys is a stopgap until then, and its comment says so.
What’s next
The handshake, so every start of the tunnel gets fresh keys.