Learn › Part 3 · Cryptography and secure protocols
IPsec: AH, ESP, transport and tunnel modes
Where IPsec sits compared with TLS, its services, SPD and SAD, mutable header fields, AH and ESP formats, modes, NAT and anti-replay.
TLS versus IPsec
| TLS | IPsec | |
|---|---|---|
| Layer | Above TCP, inside each application | IP layer, in the OS kernel or gateway |
| Changes needed | Every application must use TLS | None in applications; configure hosts or gateways |
| Typical use | Web, mail, APIs | VPNs: site-to-site and remote access |
Services (RFC 4301): access control, connectionless integrity, data-origin authentication, anti-replay, confidentiality, and limited traffic-flow confidentiality. IKE authenticates peers and negotiates SAs and keys with Diffie–Hellman; AH and ESP protect packets.
SPD and SAD
- SPD (policy): ordered rules matched on selectors (addresses, protocol, ports) with action discard, bypass or apply IPsec.
- SAD (parameters): one entry per security association, which is one-way, so two-way traffic needs two SAs. An SA is identified by (SPI, destination address, protocol 51/50) and stores algorithms and keys, lifetime, sequence counter, anti-replay window, mode and path MTU.
Mutable and immutable IPv4 fields
AH's ICV covers the IP header, except fields that routers legitimately change, which are zeroed for the calculation.
| Immutable (covered) | Mutable (zeroed) |
|---|---|
| Version, IHL, total length, identification, protocol, source and destination address | TOS/DSCP, flags, fragment offset, TTL, header checksum |
AH and ESP
| AH (protocol 51) | ESP (protocol 50) | |
|---|---|---|
| Confidentiality | No | Yes |
| Integrity / origin / anti-replay | Yes | Yes (optional authentication) |
| Covers outer IP header | Yes (immutable fields) | No |
| Through NAT | Breaks: NAT changes covered addresses | Works with NAT-Traversal (ESP in UDP 4500) |
- AH header: Next Header, Payload Length (= AH length in 32-bit words − 2), Reserved, SPI, Sequence Number, ICV. With a 96-bit ICV the AH is 24 bytes = 6 words, so Payload Length = 4.
- ESP: header (SPI, sequence) in clear; payload, padding (0–255 bytes), pad length and next header encrypted; ICV after them. Pad so payload + padding + 2 is a multiple of the cipher block (16 for AES): a 45-byte payload needs 1 byte of padding.
- Next Header: 6 for TCP (transport mode), 4 for IPv4 (tunnel mode).
Transport and tunnel modes
Transport: [orig IP hdr][AH/ESP][TCP/UDP + data]
Tunnel: [new IP hdr][AH/ESP][orig IP hdr][TCP/UDP + data]
Transport mode suits host-to-host links and exposes the real addresses. Tunnel mode suits gateway-to-gateway and remote-access VPNs; with ESP it hides internal addresses behind the gateways. The usual VPN choice is ESP tunnel mode with authentication, plus NAT-T when clients sit behind NAT.
Anti-replay window
The receiver tracks a sliding window (say 50–80). A new number inside it is accepted once and marked; a duplicate or a number left of the window (say 42) is dropped; a number to the right (say 85) is accepted after authentication and slides the window forward.
Common pitfalls
- Plain ESP has no ports, so PAT cannot map it; NAT-T is needed.
- Identification is immutable; flags and fragment offset are mutable.
- AH never encrypts.