IPsec Basics: ESP, IKE Phases, and SAs

IPsec Basics: ESP, IKE Phases, and SAs

IPsec is the framework that turns the open internet into a private link. It is not one protocol but a kit — two data-plane protocols, a key-exchange protocol with a life of its own, and a pile of negotiable algorithms. Most IPsec troubleshooting is really one question: which of those negotiations failed? This walk-through goes from the four services IPsec provides to IKEv2, SPIs, and the NAT problem that breaks tunnels in the field.

1. The four services, and two ways to deliver them

IPsec provides confidentiality (encryption), integrity (the packet was not altered), authentication (it came from the real peer), and anti-replay (an attacker cannot resend captured packets). Two protocols carry them: AH (protocol 51) authenticates but does not encrypt, and ESP (protocol 50) encrypts and authenticates. In practice everyone uses ESP — AH’s lack of encryption and its incompatibility with NAT retired it.

IPsec: AH vs ESPNET BY MEAH (protocol 51)Integrity + authenticationNo encryptionBreaks through NAT · rarely usedESP (protocol 50)Encryption + integrity + authAnti-replay via sequence numbersThe one you actually deploy

2. Transport mode vs tunnel mode

IPsec protects a packet in one of two shapes. Transport mode encrypts only the payload and keeps the original IP header — host-to-host, where the endpoints are the crypto endpoints. Tunnel mode wraps the entire original packet inside a new IP header — the shape every site-to-site VPN uses, because the gateways encrypt on behalf of the hosts behind them, hiding the internal addresses.

Transport mode vs tunnel modeNET BY METransport modeIP headerESP + encrypted payloadoriginal IP header kept · host-to-hostTunnel modeNEW IP hdrESP + [original IP hdr + payload]all encryptedwhole packet wrapped · site-to-site VPNTunnel mode hides the internal source/destination; transport mode does not

3. IKE builds the tunnel in phases

Before any data is protected, the peers must agree on keys and algorithms — that is IKE‘s job, and it runs over UDP 500. IKEv1 does it in two phases: Phase 1 builds a secure management channel (the ISAKMP SA) and Phase 2 negotiates the actual IPsec SAs that protect traffic. IKEv2 collapses this into far fewer messages, but the mental model — secure the negotiation first, then negotiate the data protection — is the same.

IKE phasesNET BY MEPhase 1 — ISAKMP SAauthenticate peers, build secure channelPhase 2 — IPsec SAnegotiate keys/algorithms for the dataIKEv1: main mode (6 msgs) or aggressive (3) + quick mode (3). IKEv2: 4 messages total.Secure the negotiation channel first, then negotiate the data protection

4. Phase 1 — Diffie-Hellman and peer authentication

Phase 1 solves the hardest problem: agreeing on a secret key over a line an attacker is watching. Diffie-Hellman lets both sides derive a shared key by exchanging only public values — the eavesdropper sees the exchange but cannot compute the key. The DH group sets its strength. The peers then prove who they are with a pre-shared key or certificates; a PSK mismatch is the single most common Phase 1 failure.

Diffie-Hellman key exchangeNET BY MEPeer APeer Bpublic value Apublic value Bboth derive the SAME shared keyauth: PSK or cert

5. Security Associations and the SPI

The negotiated result is a Security Association (SA) — a bundle of keys, algorithms, and lifetime. A crucial detail: IPsec SAs are unidirectional, so every tunnel has at least two, one per direction. Each is identified by a Security Parameter Index (SPI) carried in the packet, which tells the receiver which SA — which key set — to use to decrypt. When you read show crypto output, you are reading SAs and SPIs.

Security Associations and SPINET BY MEGateway AGateway BSA (SPI 0xA1) A -> BSA (SPI 0xB2) B -> ASAs are unidirectional; the SPI in each packet selects the key set to decrypt with

6. What an ESP packet actually looks like

Seeing the ESP encapsulation makes the rest concrete. ESP adds a header (with the SPI and a sequence number for anti-replay) in front of the protected data, and a trailer plus an authentication value behind it. The original payload — and in tunnel mode the original IP header too — sits encrypted in the middle, unreadable without the SA’s key.

ESP packet structureNET BY MENew IP hdr(tunnel)ESP headerSPI + seq#encrypted: payload (+ orig IP hdr)ESP trailerESP auth(ICV)SPI + sequence number live in the ESP header; the data is encrypted and authenticated

7. IKEv2 — why the newer version wins

IKEv2 is not a tweak; it is a cleaner protocol. It establishes a tunnel in a single four-message exchange instead of IKEv1’s six-plus-three, so it sets up faster. It builds in NAT traversal, dead-peer detection, and EAP authentication that IKEv1 bolted on awkwardly. It also reliably handles rekeying and asymmetric authentication. For anything new, IKEv2 is the default; IKEv1 lingers only for legacy interoperability.

IKEv1 vs IKEv2NET BY MEIKEv16+3 messages · NAT-T, DPD bolted ontwo separate phases, more round tripsIKEv24 messages · NAT-T, DPD, EAP built incleaner rekeying, faster setup

8. The three things that break tunnels in the field

Three real-world mechanisms cause most production incidents. NAT-T: ESP has no port numbers, so a NAT device cannot track it; IKEv2 detects NAT and wraps ESP in UDP 4500 so it survives — if that is blocked, the tunnel builds but no traffic passes. DPD (dead-peer detection) notices a silently dead peer and tears the SA down instead of blackholing traffic. PFS (perfect forward secrecy) forces a fresh Diffie-Hellman at each rekey, so compromising one key does not expose past or future sessions — and a PFS mismatch between peers silently drops rekeys.

NAT-T, DPD, and PFSNET BY MENAT-T (UDP 4500)wraps ESP so NAT survivesblocked -> tunnel up, no dataDPDdetect a dead peertear down instead of blackholePFSfresh DH at each rekeymismatch -> rekeys drop

Takeaways

IPsec is a negotiation wrapped in a negotiation: IKE agrees how to protect the agreement, then agrees how to protect your data, producing unidirectional SAs selected by SPI. Know ESP over AH, tunnel over transport for VPNs, IKEv2 over IKEv1, and the NAT-T/DPD/PFS trio — and the mysterious tunnel that builds but passes no traffic stops being mysterious.

Mokrane ASSAM

Comments are closed.