Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 37 ยท VPNs and Tunnelling

WireGuard and OpenVPN

Compare WireGuard and OpenVPN: design, keys, cryptokey routing, configuration, performance and operations, with a runnable AllowedIPs routing and peer-policy simulation.

IPsec is powerful but sprawling. Two newer VPN tools took different routes to something more approachable. OpenVPN (from 2001) builds a tunnel on top of TLS, so it uses the same certificate machinery as the web and can pass through almost any firewall. WireGuard (in the Linux kernel since 2020) is a deliberately tiny design: a few thousand lines of code, a fixed set of modern cryptographic primitives, and a configuration that fits on a page. This lesson explains how each works, shows realistic (read-only) configurations, and lets you simulate WireGuard's central idea, cryptokey routing, in Python.

WireGuard

Design

WireGuard makes a few strong choices:

  • Fixed cryptography: Curve25519 for key exchange, ChaCha20-Poly1305 for encryption and authentication, BLAKE2s for hashing, plus the Noise protocol framework for the handshake. There is no cipher negotiation, so nothing to downgrade and nothing to misconfigure. If a primitive is ever broken, a new protocol version replaces it.
  • Identity is a public key: each peer has a private key and shares its public key with the others, much like SSH keys. No certificate authority and no user database are built in.
  • Silent by default: a WireGuard interface does not answer packets from unknown peers, so a port scan does not reveal it.
  • A network interface: wg0 behaves like any other interface, so you route to it, firewall it and monitor it with ordinary tools.
  • UDP only, with roaming support: when a peer's address changes (Wi-Fi to mobile), the next authenticated packet updates the endpoint.

Cryptokey routing

Each peer has an AllowedIPs list. This list works in two directions:

  • Outgoing: when the interface gets a packet to send, it looks at the destination address, finds the peer whose AllowedIPs contains it (longest prefix match) and encrypts the packet to that peer.
  • Incoming: after decrypting a packet from a peer, it accepts the packet only if the inner source address is inside that peer's AllowedIPs; otherwise it drops it.

That second check is a built-in anti-spoofing control: even an authenticated peer cannot inject traffic that claims to come from someone else's address.

A configuration (read-only)

INI
# /etc/wireguard/wg0.conf on the gateway (example, documentation addresses)
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <gateway-private-key>

# Laptop of Alice
[Peer]
PublicKey = <alice-public-key>
AllowedIPs = 10.8.0.2/32

# Branch office router: its LAN is also reachable through this peer
[Peer]
PublicKey = <branch-public-key>
AllowedIPs = 10.8.0.3/32, 192.168.50.0/24
INI
# on Alice's laptop (full tunnel: everything goes through the gateway)
[Interface]
Address = 10.8.0.2/32
PrivateKey = <alice-private-key>
DNS = 10.8.0.1

[Peer]
PublicKey = <gateway-public-key>
Endpoint = 203.0.113.1:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25

Generate keys with wg genkey | tee private.key | wg pubkey > public.key, bring the interface up with wg-quick up wg0, and inspect it with wg show. Keep private keys readable only by root, and share only public keys.

The simulation: routing and filtering by AllowedIPs

The program below implements the two AllowedIPs rules for a gateway with two peers. Outgoing packets are routed to a peer by longest prefix; incoming packets from a peer are accepted only when their source falls inside that peer's list.

Python Cryptokey routing with AllowedIPs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Alice's packet claiming to come from the branch LAN is dropped, and so is a branch packet claiming Alice's tunnel address. Every peer can only speak as itself.

OpenVPN

Design

OpenVPN runs in user space. It creates a virtual interface (tun for layer 3 routing, tap for layer 2 bridging) and carries packets over UDP (preferred) or TCP (to get through restrictive networks, often on port 443). A control channel uses TLS for authentication and key exchange, so you get X.509 certificates, a CA you control, and revocation lists. A data channel then encrypts packets with a negotiated cipher, typically AES-256-GCM or ChaCha20-Poly1305. Extras include username and password plugins, MFA, push of routes and DNS to clients, and a tls-crypt key that hides and authenticates the handshake so scanners and DoS attempts see nothing useful.

Strengths and costs

  • Flexible: runs over TCP 443, works through proxies, supports many authentication methods, has mature clients on every platform and rich server-side control (per-client routes, scripts).
  • Larger and slower: user-space processing and a large codebase (including the TLS library) mean lower throughput and a bigger attack surface than WireGuard.
  • Many options: every option is a chance for a mistake. Keep configuration short and follow a hardened template.

A hardened server fragment (read-only)

TEXT
port 1194
proto udp
dev tun
topology subnet
server 10.9.0.0 255.255.255.0

ca ca.crt
cert server.crt
key server.key
dh none
tls-crypt tc.key
tls-version-min 1.2
data-ciphers AES-256-GCM:CHACHA20-POLY1305

push "route 192.168.50.0 255.255.255.0"
crl-verify crl.pem
user nobody
group nogroup
persist-key
persist-tun
verb 3

dh none uses elliptic-curve exchange; crl-verify rejects revoked client certificates; user nobody drops privileges after start; push "route ..." gives clients only the internal subnet they need (split tunnel).

Choosing between them

Question WireGuard OpenVPN
Codebase and attack surface Very small Large
Cipher agility None by design Configurable
Speed Excellent (kernel) Good (user space)
Passes restrictive firewalls Only UDP Yes, TCP 443
User management Manual or via tooling Certificates, PKI, plugins
Dynamic address assignment Static per peer Built in
Best for Site-to-site, servers, simple remote access, mesh Large user bases, legacy and restrictive networks

Many teams use WireGuard for infrastructure links and a management layer or a platform built on it for users (key rotation and revocation are your job with plain WireGuard).

Operating a VPN safely

Python Review peers: key age and overbroad AllowedIPs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Good operating habits for both tools:

  • Rotate and revoke: remove peers when people leave; keep an inventory of keys with an owner and date.
  • Least privilege AllowedIPs / pushed routes: a contractor should reach one application, not 10.0.0.0/8.
  • Firewall the VPN interface like any other zone: VPN clients land in their own zone with specific rules (see segmentation).
  • Monitor handshakes: wg show reports the latest handshake per peer; a peer that never handshakes, or one connecting from an odd country, is worth a look.
  • Update: both projects fix security bugs; run supported versions.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What are WireGuard's AllowedIPs used for?

    Cryptokey routing uses AllowedIPs for outgoing routing (longest prefix) and as an anti-spoofing check on decrypted incoming packets.
  2. 2. Why can WireGuard not be downgraded to a weak cipher by an attacker?

    There is no algorithm negotiation to manipulate. If a primitive needs replacing, a new protocol version is used.
  3. 3. Which is a reason to choose OpenVPN over WireGuard?

    OpenVPN over TCP 443 works where UDP is blocked, and its TLS-based design integrates with certificates and revocation.
  4. 4. A contractor's WireGuard peer has AllowedIPs 10.0.0.0/8. What is the concern?

    Overbroad AllowedIPs gives far more access than required. Apply least privilege and narrow it.
  5. 5. What does 'tls-crypt' in OpenVPN help with?

    tls-crypt encrypts and authenticates the control channel with a pre-shared key, reducing exposure to scanning and some attacks.

Summary

  • WireGuard: tiny, fixed modern cryptography, public-key identity, silent to unknown peers, routes and filters by AllowedIPs (cryptokey routing).
  • OpenVPN: user-space, TLS-based, certificates and PKI, TCP or UDP, flexible but larger and more complex.
  • Pick WireGuard for speed and simplicity, OpenVPN where you need TCP 443, PKI or legacy support.
  • Operate both with key rotation and revocation, narrow routes, zone firewalling, handshake monitoring and updates.

Next: SSH Tunnelling and Port Forwarding