Sub topic 6
Confidentiality, integrity and authentication
A tunnel on its own only moves packets. It does not hide them, protect them from changes, or prove who sent them. This chapter covers the three properties that turn a tunnel into a VPN, and the primitives that provide each one.
This chapter assumes you know what encapsulation is, that you can read a routing table, and that you can tell TCP from UDP. It uses no key-exchange details. Those come in the next chapter.
Why a plain tunnel is not enough
A GRE tunnel wraps an inner packet in an outer IP packet. Anyone on the path between the two tunnel endpoints can still do three things:
- Read the inner packet, because it travels as plain bytes.
- Change the inner packet, and the receiver will not notice.
- Send a forged GRE packet that claims to come from the other endpoint.
Each of these corresponds to a security property that a VPN adds.
| Property | Question it answers | Attack it stops |
|---|---|---|
| Confidentiality | Can an observer read the data? | Eavesdropping |
| Integrity | Was the data changed on the way? | Tampering |
| Authentication | Who sent this, and who am I talking to? | Impersonation, forged packets |
The three are separate. You can have any one without the others, and a design that gets one wrong is often broken even if the other two are correct.
Confidentiality: encryption
Encryption turns readable data (plaintext) into unreadable data (ciphertext) using a key. Decryption reverses it. VPNs use symmetric encryption for the bulk traffic: both ends hold the same secret key, and the same key encrypts and decrypts. Symmetric ciphers are fast enough to run at line rate, often with hardware support.
Common symmetric ciphers in VPNs are AES (usually in GCM mode) and ChaCha20. IPsec commonly uses AES-GCM. WireGuard uses ChaCha20 only. Both are considered strong when used with a correct key and a correct nonce.
The nonce rule
A cipher also takes a nonce, a number that must never repeat for the same key. Reusing a nonce with the same key can leak plaintext relationships or let an attacker forge messages. This is why VPN protocols number their packets. The packet counter is usually the source of the nonce, and the key is replaced before the counter could wrap around.
What encryption does not give you
Encryption alone does not detect modification. With some cipher modes, an attacker can flip bits in the ciphertext and cause predictable changes in the plaintext without knowing the key. Encryption hides content. It does not guarantee that the content is the original. That is the job of integrity checks.
Integrity: MACs and AEAD
A message authentication code (MAC) is a short tag computed from the message and a secret key. The sender attaches the tag. The receiver recomputes it and compares. If any bit of the message changed, the tags differ and the packet is dropped.
HMAC is the common construction. For example, HMAC-SHA-256 computes a keyed hash of the data. An attacker who does not know the key cannot produce a valid tag for a modified message.
A MAC is different from a plain hash. Anyone can compute SHA-256 of a message, including an attacker who has altered it. Only a holder of the key can compute a valid MAC.
AEAD: encryption and integrity together
Combining encryption and a MAC by hand is easy to get wrong. Modern protocols use authenticated encryption with associated data (AEAD). An AEAD cipher encrypts the payload and produces an authentication tag in one operation. AES-GCM and ChaCha20-Poly1305 are AEAD ciphers.
"Associated data" is data that is authenticated but not encrypted. A tunnel needs this for headers that routers must read, such as the outer sequence number. The tag covers them, so they cannot be altered, but they remain visible.
# Requires: pip install cryptography
import os
from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
from cryptography.exceptions import InvalidTag
key = ChaCha20Poly1305.generate_key()
aead = ChaCha20Poly1305(key)
nonce = os.urandom(12) # must be unique per message for this key
header = b"seq=1" # authenticated, not encrypted
payload = b"inner packet bytes"
sealed = aead.encrypt(nonce, payload, header)
print("ciphertext+tag length:", len(sealed)) # payload length + 16-byte tag
print(aead.decrypt(nonce, sealed, header)) # original payload
# Flip one bit of the ciphertext: decryption must fail.
tampered = bytearray(sealed)
tampered[0] ^= 1
try:
aead.decrypt(nonce, bytes(tampered), header)
except InvalidTag:
print("tampering detected")
# Change the associated data: also fails.
try:
aead.decrypt(nonce, sealed, b"seq=2")
except InvalidTag:
print("header change detected")The sealed output is 16 bytes longer than the plaintext. That tag is part of the per-packet overhead you will count in the MTU chapter.
Authentication: who is on the other end
Integrity with a shared key tells you that the sender knew the key. That is useful, but it leaves a question: how did the right party get the key, and how do you know you are not talking to an attacker who negotiated one with you? Authentication answers that. There are two levels.
Peer authentication
Before traffic flows, each side must be sure of the other's identity. Three common methods are:
- Pre-shared key (PSK): both sides hold the same secret. Simple, but the secret must be distributed securely, and everyone who knows it can impersonate anyone else who knows it.
- Public-key signatures or certificates: each side holds a private key and proves possession of it. Others verify with the public key. A certificate binds a public key to a name and is signed by a certificate authority (CA).
- Static public keys, pinned in configuration: WireGuard's model. Each peer lists the public keys it accepts. No CA is involved.
Asymmetric cryptography, where the private key signs or decrypts and the public key verifies or encrypts, is slow relative to symmetric ciphers. VPNs use it only during setup, then switch to symmetric keys for the data.
Packet authentication
After setup, every packet must be tied to the session. The AEAD tag does this: only a holder of the session key can produce a packet that passes verification. Packets that fail are dropped silently or counted, never processed.
Replay protection
An attacker who records a valid encrypted packet can send it again later. The tag is still valid, because the packet is unchanged. Without protection, the receiver might accept a replayed command or duplicate a payment request.
Tunnels defend with a sequence number in each packet and a sliding window at the receiver. The receiver tracks which numbers in a recent range it has already seen. A packet whose number is too old or already seen is dropped. Because the number is covered by the tag, an attacker cannot change it.
The window also explains a side effect: packets that arrive very late or heavily reordered can be discarded even though they are genuine. This sometimes shows up as unexplained loss on links with large reordering.
Note the order. The tag is checked before anything else is trusted, including the sequence number.
Putting the three together
The following table maps each property to where it appears in the protocols you will meet later.
| Protocol | Encryption and integrity | Peer authentication |
|---|---|---|
| IPsec ESP | AES-GCM or AES with HMAC | PSK or certificates, via IKE |
| TLS-based VPN | AEAD in the TLS record layer | Certificates, optionally plus passwords |
| WireGuard | ChaCha20-Poly1305 | Pinned static public keys |
| SSH tunnel | AEAD or cipher plus MAC | Host keys and user keys |
Common errors and misconceptions
- "Encrypted means safe from modification." Only an integrity check, usually built into AEAD, detects changes. Avoid any configuration that offers encryption without integrity.
- "A hash is a MAC." A hash has no secret. Use a keyed construction.
- "A VPN authenticates the user." Often it authenticates a device or a key. Someone holding a stolen key or laptop is authenticated.
- Weak pre-shared keys. A short or guessable PSK can be attacked offline if an attacker captures the handshake. Use long random values.
- Skipping certificate or key verification. A client that accepts any server certificate encrypts perfectly to an attacker. Encryption without authentication protects only against passive observers.
- Mismatched algorithm proposals. In IPsec and TLS VPNs, both ends must share at least one acceptable cipher suite. A mismatch produces a failed negotiation, not a weaker connection.
Check your understanding
Exercise
A tunnel encrypts every packet with a symmetric cipher but attaches no MAC or AEAD tag. An attacker on the path flips bits in captured ciphertext before forwarding it. Which of the three properties is missing, and what is the consequence? Which two mechanisms in a modern VPN provide the missing property?
Show answer
Integrity is missing. The receiver cannot detect the modification, so it decrypts altered data and passes corrupted or attacker-shaped inner packets on, even though the attacker never learned the key. Integrity is provided by a MAC such as HMAC, or more commonly by an AEAD cipher such as AES-GCM or ChaCha20-Poly1305, which produces a tag that the receiver verifies before accepting the packet.
Exercise
An attacker records a valid encrypted, authenticated packet and resends it ten minutes later, unchanged. The tag still verifies. What stops the receiver from accepting it, and why can the attacker not simply change the sequence number to get around it?
Show answer
Replay protection stops it. The receiver keeps a sliding window of sequence numbers it has seen and drops any packet that is already recorded or older than the window. The sequence number is covered by the authentication tag, so changing it invalidates the tag and the packet is dropped.