← Back to blog

Encrypting tunnel traffic with ChaCha20-Poly1305

Until now, anyone watching the network between the two peers could read every packet in the tunnel. This pull request encrypts and authenticates each one with ChaCha20-Poly1305, using a key shared ahead of time. It’s the first stage of adding WireGuard-style cryptography, and it has a known weakness, which I’ll get to.

The message format

Each encrypted packet is sent in the same format as a WireGuard transport data message:

type (4) + 3 zero bytes | receiver index | counter    | ciphertext | tag
4 bytes                 | 4 bytes        | 8 bytes LE | variable   | 16 bytes

The receiver index is always 0 for now, since there’s only one session. Matching WireGuard’s format costs nothing today, and it means this layer can stay as it is when a handshake arrives later. The header plus the tag is the 32 bytes I reserved in the MTU.

Nonces from a counter

ChaCha20-Poly1305 needs a 12-byte nonce, and the one rule is that a nonce must never be used twice with the same key. Like WireGuard, I build it from a 64-bit counter: 4 zero bytes followed by the counter in little-endian. The counter is also sent in the header, so the receiver can rebuild the nonce.

Seal takes the next value with one atomic operation, so it’s safe to call from several goroutines:

func (s *Session) Seal(dst, packet []byte) ([]byte, error) {
	count := s.counter.Add(1) - 1
	if count >= counterLimit {
		return nil, ErrCounterExhausted
	}

	nonce := makeNonce(count)
	dst = binary.LittleEndian.AppendUint32(dst, messageType)
	dst = binary.LittleEndian.AppendUint32(dst, receiverIndex)
	dst = binary.LittleEndian.AppendUint64(dst, count)
	return s.aead.Seal(dst, nonce[:], packet, nil), nil
}

The limit is WireGuard’s (2^64 - 2^13 - 1). No tunnel will reach it in practice, but if one did, govpn shuts down instead of wrapping around and reusing nonces.

No allocation per packet

The outbound loop reads each packet from the TUN device at offset 16 of its buffer, leaving room for the header. Seal then writes the header into those 16 bytes and encrypts the packet in place, since Go’s AEAD interface allows the output to overlap the input exactly. The buffer is allocated once and reused for every packet.

Dropping bad packets quietly

Open checks the length, type, and receiver index before decrypting, and then the tag. Anything that fails is dropped without a reply or a log line. Anyone can send datagrams to the port, and logging each bad one would let them flood the logs.

The key file

A new required -key-file flag points to a 32-byte key in base64, the same format as wg genkey. govpn refuses to start if the group or others can access the file, and prints the chmod command that fixes it. The permission check and the read use the same open file, so the file can’t be swapped in between.

With tcpdump on the lab’s virtual cable, each payload now starts with 0400 0000, then the receiver index and a counter that goes up by one per packet. The rest looks random. Before this change, the inner IPv4 header and the 10.0.0.x addresses were in plain sight.

The weakness

Both peers use the same key, and each peer’s counter starts at 0. So packet n from one side and packet n from the other are encrypted with the same key and nonce. Restarting a peer resets its counter and reuses nonces too. With ChaCha20-Poly1305, that lets an observer learn about the plaintext and forge packets.

I merged it anyway, on purpose. The format, the sealing, and the integration are correct and tested, and the fix is a separate change. The weakness is documented in session.New and in the README, which says plainly not to use the project to protect real data. There is also no replay protection and no key exchange yet.

What’s next

Fixing the nonce reuse. WireGuard avoids it with a separate key for each direction, and that’s what I’m doing next.