IP security: AH, ESP and VPNs
IPsec provides packet-level security for IP: the Authentication Header (AH) and Encapsulating Security Payload (ESP) give integrity/authentication and confidentiality+integrity respectively; Security Associations (SAs) + IKE build keys and policies used by VPNs (site-to-site and remote-access).
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
IP-level security is used whenever an engineer must protect traffic between hosts or networks without changing applications. Typical uses: building VPNs (virtual private networks) for secure site-to-site links or remote access, protecting routing/management traffic, and adding confidentiality or integrity to legacy IP-based systems.
Key ideas
Start from first principles: IP packets travel across untrusted networks. To prevent eavesdropping, tampering and replay, IPsec adds cryptographic protection at the IP layer.
Authentication Header (AH) — a protocol that provides integrity, data origin authentication, and optional anti-replay for IP packets. AH carries an integrity check value (ICV) computed over the packet (most header fields that change in transit are zeroed for computation). AH does not provide confidentiality (no encryption).
Exact AH header fields to state and draw (for IPv4):
- Next Header (1 byte) — indicates payload protocol
- Payload Length (1 byte) — length of AH in 32-bit words minus 2
- Reserved (2 bytes)
- Security Parameters Index (SPI) (4 bytes) — identifies the SA
- Sequence Number (4 bytes) — anti-replay counter
- Authentication Data (variable, e.g., 12 or 16 bytes) — ICV (HMAC output truncated)
When calculating the ICV, the Authentication Data field is set to zero; mutable IP header fields (like TTL) are treated as zero or ignored according to standards.
Encapsulating Security Payload (ESP) — a protocol that provides confidentiality (encryption), and optionally integrity and authentication. ESP can be used with encryption alone or with combined authenticated-encryption algorithms (AEAD) such as AES-GCM.
ESP header/trailer fields to state and draw (for transport of an IPv4 payload):
- SPI (4 bytes)
- Sequence Number (4 bytes)
- Initialization Vector / Explicit Nonce (algorithm-dependent, per-packet; e.g., 16 B IV for AES-CBC; 8 B explicit nonce for AES-GCM) — carried in the Payload Data field
- Payload Data (variable) — the protected content (e.g., transport header + application data), plus any Padding as required
- Padding (0–255 bytes) — alignment (to 4-byte boundary and to block size for CBC)
- Pad Length (1 byte)
- Next Header (1 byte)
- Authentication Data / ICV (variable, optional) — integrity check/tag if used
Note: With AEAD (e.g., AES-GCM), an explicit nonce (commonly 8 B) is carried and the AEAD authentication tag (commonly 16 B) is appended after the ciphertext; with non-AEAD + HMAC, the ICV is computed over the ciphertext and ESP header (encrypt-then-MAC).
Security Association (SA) — an agreement (state) between two IPsec peers that defines the following: SPI, destination, protocol (AH or ESP), encryption algorithm and key, integrity algorithm and key, mode (transport/tunnel), lifetime, sequence number state. An SA is unidirectional. Two directions require two SAs.
Security Policy Database (SPD) — a set of rules that decide whether traffic should be protected (IPsec), bypassed, or discarded. SPD entries map traffic selectors (src/dst IP, ports, protocol) to actions and SAs.
Security Association Database (SAD) — holds active SAs used to process packets (SPI → keys, algorithms, mode, etc.). During inbound processing the system uses SPI + destination IP + protocol to find the SA in SAD.
Modes
- Transport mode — IPsec (AH or ESP) protects the payload (and ESP also encrypts the payload). The original IP header remains, AH/ESP inserted between IP header and transport header. Use when endpoints are the actual hosts.
- Tunnel mode — a new IP header is created; the original IP packet is wrapped (encrypted/authenticated) as the payload of the new IP packet. Use for gateway-to-gateway (site-to-site) VPNs.
IKE (Internet Key Exchange) — protocol (IKEv1, IKEv2) used to negotiate SAs and key material. Typical ports: UDP 500 for IKE; NAT traversal uses UDP 4500 (encapsulating ESP in UDP). IKE performs authentication (PSK, certificates) and sets up keys and SA parameters.
Anti-replay — a sliding window check on 32-bit sequence numbers to discard replays. Default window sizes are commonly 64 (implementation-dependent). Packets with sequence numbers below the left edge of the window are dropped (even if unseen); packets within the window are accepted only if not seen before. When the sequence number approaches 2^32−1, the SA must be rekeyed; the optional ESN feature extends the counter to 64 bits.
Order of protection — critically important: do you encrypt then authenticate, or authenticate then encrypt? Modern guidance: prefer encrypt-then-MAC (authenticate after encryption) or use AEAD modes (e.g., AES-GCM) which combine confidentiality and integrity safely. ESP with separate encryption and HMAC historically used encrypt-then-MAC for best security.
Diagrams to draw (exam requirement):
- For AH in transport mode: draw one IPv4 packet with boxes labelled [IP header] [AH] [TCP header] [TCP payload]. Show SPI inside AH. Label which bytes are covered by ICV and which mutable fields are zeroed.
- For ESP in tunnel mode: draw two hosts A and B with gateways G-A and G-B. Show original packet from A to B boxed as [original IP header|payload]. Then show new outer IP header added and ESP header between outer IP and encrypted payload. Label SPI and Sequence Number.
Formulas
total_packet_length = IP_hdr + proto_hdr + payload + ESP_overhead — total length in bytes; used when computing MTU impact.
- IP_hdr = size of IP header (20 bytes for IPv4 without options)
- proto_hdr = transport header (e.g., 20 bytes for TCP)
- payload = application data length
- ESP_overhead = ESP_header (8 bytes: SPI+Seq) + IV_len (algorithm-dependent; e.g., 16 B for AES-CBC IV, 8 B explicit nonce for AES-GCM) + padding + pad_len(1) + next_header(1) + ICV/tag_len (if used) — total added bytes relative to plaintext
padding_length = (block_size - ((payload_length + 2) mod block_size)) mod block_size — pad to align to cipher block size; payload_length is the plaintext length before padding; block_size in bytes (e.g., 16 for AES).
- pad_len and next_header occupy 2 bytes after the padding.
SA_lookup_key = (SPI, Destination_IP, Protocol) — SAD/inbound lookup key.
ICV = Truncate(HMAC_k(message), t) — Authentication Data is a truncation of keyed-hash output; k is key, t is ICV length in bytes.
Explain symbols and units where used: lengths in bytes. No SI unit needed beyond byte.
Worked examples
- Padding and packet size with ESP (numerical) Given: IPv4 header 20 B, TCP header 20 B, application payload 100 B, use ESP in transport mode with AES-CBC (block_size = 16 B), ESP header fields: SPI+Seq = 8 B, IV (AES-CBC) = 16 B, PadLen+NextHdr = 2 B, Authentication Data (ICV) = 12 B (HMAC truncated). Compute padding_length, ESP_overhead, total packet length. Steps:
- payload_length = TCP header + application = 20 + 100 = 120 B
- For ESP, the protected plaintext is (TCP header + application + padding + pad_len + next_hdr); AES-CBC also carries a 16 B IV per packet.
- Compute padding_length = (16 - ((120 + 2) mod 16)) mod 16 = (16 - (122 mod 16)) mod 16 = (16 - 10) mod 16 = 6 B
- ESP_overhead = 8 (SPI+Seq) + 16 (IV) + 6 (padding) + 2 (pad_len+next_hdr) + 12 (ICV) = 44 B
- total_packet_length = IP_hdr 20 + proto_hdr 20 + payload 100 + ESP_overhead 44 = 184 B Final answer: total packet length = 184 bytes.
- SA lookup and inbound processing (algorithm trace) Scenario: a router receives an IPv4 packet whose Protocol field indicates ESP (protocol number 50), and SPI = 0xA1B2C3D4, dest IP = 198.51.100.1. Steps to process:
- Parse IP header, note Protocol=50 → ESP; extract SPI and Sequence Number from the ESP header.
- Use SA_lookup_key = (SPI, dest IP, protocol) to find SA in SAD. If none, drop or (policy permitting) trigger IKE to create an SA.
- Check sequence number against the anti-replay window; if replayed or below the window’s left edge, drop.
- Verify integrity (ICV) before decryption if using HMAC (encrypt-then-MAC). For AEAD, perform the combined decrypt+verify operation.
- Decrypt the payload using the SA’s encryption algorithm/key.
- Perform inbound SPD check on the decapsulated (inner) packet; if it matches a bypass/protect policy as expected, forward accordingly. Otherwise, drop.
- Anti-replay sliding window (small trace) Given window size 8, highest-accepted = 100. Receive packet with sequence 97, 101, 94 in that order.
- Seq=97: 100 - 97 = 3 < 8, not seen before ⇒ accept and mark bit for 97.
- Seq=101: >100 ⇒ accept, update highest-accepted=101, slide window accordingly.
- Seq=94: 101 - 94 = 7 < 8 but was not received earlier ⇒ accept and mark. If a later packet repeats one of these seq numbers already marked, it will be rejected.
Common mistakes
- Omitting Pad Length and Next Header bytes when computing ESP padding.
- Using wrong padding formula: remember the +2 for pad_len and next_header before modding by block size.
- Mixing protocol numbers and UDP ports: ESP and AH are IP protocol numbers (50 and 51) not TCP/UDP ports; IKE uses UDP 500 (and NAT-T uses UDP 4500).
- Treating AH as compatible with NAT — AH covers parts of the IP header so NAT changes break AH; ESP with NAT-T (UDP encapsulation) is used instead.
For your exam
Question types you will see: definition (AH, ESP, SA), short note (transport vs tunnel, anti-replay), derivation (padding formula, packet size calculation), protocol flow (IKE negotiation steps), diagram (where to place AH/ESP in transport/tunnel), and a step-by-step IPsec packet processing trace. What a full-marks answer includes:
- Precise definitions with bold terms and main fields listed (SPI, Sequence Number, ICV, Pad Length, Next Header).
- Correct distinction between transport and tunnel modes and an appropriate diagram described or drawn.
- Exact padding formula and a worked numeric example showing units and final answer.
- Correct SA/SAD/SPD roles and IKE basic purpose (key negotiation and authentication).
Quick check
- What does SPI stand for and what is its role?
- Give the protocol numbers for ESP and AH.
- For AES block size 16 B, payload 46 B, compute padding_length used by ESP (show formula result). Answers: 1) SPI = Security Parameters Index; it identifies which SA to use for processing inbound packets. 2) ESP = 50, AH = 51. 3) padding_length = (16 - ((46 + 2) mod 16)) mod 16 = (16 - (48 mod 16)) mod 16 = (16 - 0) mod 16 = 0 bytes (so pad_len = 0).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?