Tutorial 4.12: IPsec Protocols: AH and ESP

📑 Table of Contents

🎯 Learning Objectives

After completing this tutorial, you should be able to:


📖 Overview

In Tutorial 4.11, we explored the overall architecture of IPsec, including Security Associations, the SPD and SAD, and the IKE protocol. We also introduced the two main protocols: Authentication Header (AH) and Encapsulating Security Payload (ESP). In this tutorial, we dive deep into these protocols, examining their packet formats, processing steps, and the cryptographic algorithms they use. Understanding the details of AH and ESP is essential for configuring IPsec, troubleshooting issues, and appreciating the trade-offs between the two protocols.

We begin with a recap of the services provided by AH and ESP. We then dissect the AH packet format, field by field, and explain how AH provides integrity and authentication by protecting the IP header and payload. We also describe the processing of AH on outbound and inbound packets. Next, we cover ESP in detail, including its header, trailer, and optional authentication fields. ESP provides confidentiality through encryption, and can also provide integrity and authentication when used with an integrity algorithm.

We discuss the cryptographic algorithms commonly used with IPsec, such as AES for encryption and SHA-2 for integrity, and the concept of combined mode (e.g., AES-GCM) that provides both encryption and authentication in a single algorithm. We illustrate the packet structures for both transport and tunnel modes, showing how the original packet is transformed. We then explain the inbound and outbound processing steps in detail, including the anti-replay mechanism using sequence numbers and sliding windows.

We also address performance considerations, including the overhead of encryption and the impact of different algorithms on throughput. Finally, real-world case studies illustrate practical deployments and common pitfalls. By the end of this tutorial, you will have a detailed understanding of how AH and ESP operate, enabling you to configure and troubleshoot IPsec implementations effectively.

This content aligns with Stallings & Brown (2024), Chapter 16 and IETF RFCs 4302 (AH) and 4303 (ESP).

1. Introduction to AH and ESP

1.1 Recap of Security Services

1.2 When to Use AH vs. ESP

Historically, AH was used when encryption was not required or allowed (e.g., due to export restrictions). Today, ESP with authentication is almost always preferred because it provides a complete security solution. AH is rarely used in modern networks, except in some legacy or specialized scenarios where header authentication is specifically needed and encryption is prohibited. ESP in tunnel mode is the standard for VPNs.

Key Takeaway: AH provides integrity and authentication only; ESP provides confidentiality and can also provide integrity/authentication. ESP with combined mode is the modern standard.

2. Authentication Header (AH): Packet Format and Processing

2.1 AH Packet Format

The AH header is inserted after the original IP header (in transport mode) or after the new IP header (in tunnel mode). Its format is defined in RFC 4302. The fields are:

┌──────────────┬──────────────┬──────────────┬──────────────┐ │ Next Header │ Payload Len │ Reserved │ SPI │ │ (8 bits) │ (8 bits) │ (16 bits) │ (32 bits) │ ├──────────────┼──────────────┼──────────────┼──────────────┤ │ Sequence Number Field (32 bits) │ ├──────────────────────────────────────────────────────────────┤ │ Authentication Data (variable, padded to 4-byte boundary) │ └──────────────────────────────────────────────────────────────┘

Figure 1: AH Packet Format

2.2 AH Integrity Coverage

AH protects the IP header fields that do not change in transit (e.g., source/destination IP, protocol, version, and some others) and the entire payload. Mutable fields like TTL, checksum, and options are set to zero before the MAC computation. This provides authentication of the source and ensures the packet has not been tampered with.

2.3 AH Processing (Outbound)

  1. Construct the IP packet as usual.
  2. Insert the AH header after the IP header (or after the new IP header in tunnel mode).
  3. Set the SPI and sequence number from the SA.
  4. Compute the Integrity Check Value (ICV) over the immutable fields of the IP header and the payload, using the agreed authentication algorithm (e.g., HMAC-SHA-256).
  5. Place the ICV in the Authentication Data field and send the packet.

2.4 AH Processing (Inbound)

  1. Receive the packet and extract the AH header.
  2. Use the SPI to look up the SA in the SAD.
  3. Verify the sequence number is within the anti-replay window.
  4. Recompute the ICV over the appropriate fields and compare it to the value in the Authentication Data field. If they match, the packet is authenticated; otherwise, it is discarded.

2.5 Limitations of AH

Key Takeaway: AH provides strong integrity and authentication but no confidentiality, and it has NAT traversal issues, making it less popular than ESP.

3. Encapsulating Security Payload (ESP): Packet Format and Processing

3.1 ESP Packet Format

ESP is defined in RFC 4303. It consists of a header, payload, trailer, and optional authentication data. The format varies depending on whether authentication is used and whether the mode is transport or tunnel. The main fields are:

┌──────────────┬──────────────┬───────────────────────────────┐ │ SPI │ Sequence Number │ │ (32 bits) │ (32 bits) │ ├──────────────┼──────────────┼───────────────────────────────┤ │ Payload Data (variable length, includes original payload │ │ and any padding) │ ├────────────────────────────────────────────────────────────┤ │ Padding (0-255 bytes) │ ├──────────────┬──────────────┬─────────────────────────────┤ │ Pad Length │ Next Header │ Authentication Data (if any)│ │ (8 bits) │ (8 bits) │ (variable) │ └──────────────┴──────────────┴─────────────────────────────┘

Figure 2: ESP Packet Format (with authentication)

3.2 ESP Processing (Outbound)

  1. Construct the IP packet.
  2. Add the ESP header (SPI, Sequence Number) after the IP header.
  3. Encrypt the payload (including the original transport header or entire original IP packet) using the encryption algorithm (e.g., AES-CBC, AES-GCM).
  4. Add padding, Pad Length, and Next Header fields.
  5. If authentication is used, compute the ICV over the ESP header and the encrypted payload, and append it as Authentication Data.
  6. Send the packet.

3.3 ESP Processing (Inbound)

  1. Receive the packet and locate the ESP header (using the Next Header field of the previous header or by configuration).
  2. Extract SPI and Sequence Number; look up SA in SAD.
  3. Verify anti-replay using the sequence number.
  4. If authentication is used, recompute the ICV and verify it.
  5. Decrypt the payload using the encryption key.
  6. Remove padding and extract the original payload (or original IP packet in tunnel mode).

3.4 ESP with Authentication (Combined Mode)

ESP can provide integrity and authentication by including the Authentication Data field. In modern deployments, it is common to use AEAD (Authenticated Encryption with Associated Data) algorithms like AES-GCM or ChaCha20-Poly1305, which provide both encryption and authentication in a single pass, reducing complexity and improving performance. This is often called "combined mode" and is the recommended practice.

Key Takeaway: ESP provides confidentiality, and optionally integrity and authentication. Combined mode with AEAD is the modern standard for IPsec.

4. Cryptographic Algorithms and Keying

4.1 Encryption Algorithms

4.2 Integrity and Authentication Algorithms

4.3 Key Management and Key Lengths

4.4 Algorithm Negotiation

IKE negotiates the algorithms to be used for the IPsec SA. It is important to configure a policy that only allows strong algorithms and disallows weak ones (e.g., NULL encryption, MD5, SHA-1, DES). The IETF guidelines (e.g., BCP 195) provide recommendations.

Key Takeaway: Use strong, modern algorithms (AES-GCM, SHA-256, ECDH) and disable weak options for secure IPsec deployments.

5. Combined Mode: ESP with Authentication

In practice, ESP is almost always used with authentication. There are two approaches:

The AEAD approach is recommended by NIST and IETF, and is the default in most modern IPsec implementations. It simplifies configuration and reduces the risk of misconfiguration.

5.1 AES-GCM in IPsec

Key Takeaway: Combined mode with AEAD is the modern best practice for ESP, providing confidentiality and authentication efficiently and securely.

6. Transport and Tunnel Mode in Detail

6.1 Transport Mode Packet Structures

In transport mode, the original IP header is retained. The IPsec header (AH or ESP) is inserted after it. The payload (TCP/UDP segment) is protected.

AH Transport: IP Header | AH Header | TCP/UDP | Payload ESP Transport: IP Header | ESP Header | Encrypted (TCP/UDP + Payload) | ESP Trailer | ESP Auth (if any)

6.2 Tunnel Mode Packet Structures

In tunnel mode, a new IP header is added, and the entire original IP packet is treated as the payload. The new header may have different source/destination IPs (e.g., gateway addresses).

AH Tunnel: New IP Header | AH Header | Original IP Header | TCP/UDP | Payload ESP Tunnel: New IP Header | ESP Header | Encrypted (Original IP Header | TCP/UDP | Payload) | ESP Trailer | ESP Auth

6.3 Use Cases Revisited

6.4 Overhead Comparison

Tunnel mode has more overhead due to the additional IP header. Transport mode is more efficient but less flexible. For VPNs, tunnel mode is standard.

Key Takeaway: Transport mode protects only the payload; tunnel mode protects the entire packet. Tunnel mode is used for VPNs, transport mode for host-to-host.

7. Inbound and Outbound Processing

The processing steps for IPsec are detailed in RFC 4301. Below is a simplified overview.

7.1 Outbound Processing (for ESP with combined mode)

  1. Packet arrives from upper layer (or routing).
  2. SPD consulted: determine if packet should be protected (PROTECT), bypassed (BYPASS), or discarded.
  3. If PROTECT, select appropriate SA from SAD (or trigger IKE to establish one).
  4. Apply ESP: add ESP header (SPI, SeqNum), encrypt payload, add padding/trailer, compute ICV (if using AEAD, this is done in the same pass).
  5. If tunnel mode, prepend new IP header with appropriate addresses.
  6. Transmit the packet.

7.2 Inbound Processing (for ESP with combined mode)

  1. Packet arrives on the interface.
  2. Determine if it is IPsec (based on protocol field in IP header or next header).
  3. Extract SPI and sequence number; look up SA in SAD.
  4. Verify anti-replay (sequence number check).
  5. If AEAD, decrypt and verify ICV in one step. For separate algorithms, verify ICV then decrypt.
  6. Remove padding and extract the original payload.
  7. If tunnel mode, strip the outer IP header and process the inner packet.
  8. Pass the packet to the appropriate upper-layer protocol.

7.3 Error Handling

If any step fails (e.g., ICV mismatch, sequence number out of window), the packet is discarded and an error may be logged. This is critical for security.

Key Takeaway: Inbound and outbound processing follow strict sequences to ensure that only valid, authenticated, and non-replayed packets are accepted.

8. Anti-Replay Service

8.1 Sequence Numbers

Each IPsec packet includes a 32-bit sequence number in the AH or ESP header. For each SA, the sender increments a counter and includes it in each packet. The receiver maintains a sliding window of acceptable sequence numbers.

8.2 Sliding Window Mechanism

The receiver's window is typically 32, 64, or 128 packets. When a packet arrives, the receiver checks if the sequence number is greater than the highest received number minus the window size. If the number is within the window and not already received, the packet is accepted; otherwise, it is discarded. This prevents replay attacks where an attacker resends a captured packet.

8.3 Sequence Number Overflow

When the sequence number reaches 2^32 - 1, the SA must be renegotiated (or a new SA established) to avoid reuse. This is handled by IKE.

Key Takeaway: The anti-replay service uses sequence numbers and a sliding window to prevent attackers from replaying valid packets.

9. Performance Considerations

9.1 Cryptography Overhead

9.2 Packet Overhead

9.3 Latency

Cryptographic operations and IKE negotiations introduce latency. For real-time applications (e.g., VoIP), this can be a concern. Using faster algorithms and hardware acceleration helps.

9.4 Optimization Strategies

Key Takeaway: Performance is a key consideration in IPsec deployments; choosing the right algorithms and hardware can mitigate overhead.

10. Real-World Case Studies

10.1 Case Study: Migrating from AH to ESP

An organization used AH in transport mode for securing internal server communications. They encountered issues with NAT and performance. They migrated to ESP with AES-GCM in transport mode, which resolved NAT issues (ESP is more NAT-friendly) and improved performance due to the combined mode. The migration also simplified configuration.

10.2 Case Study: ESP Tunnel for Site-to-Site VPN

A global corporation deployed ESP tunnel mode with AES-256-GCM and SHA-256 for site-to-site VPNs between data centers. They enabled PFS and used IKEv2 with certificate-based authentication. The VPNs provided high throughput (10 Gbps) with hardware-accelerated encryption, ensuring secure and high-performance connectivity.

10.3 Case Study: Misconfiguration of Anti-Replay

A company experienced packet loss on their IPsec VPN. Investigation revealed that the anti-replay window was set too small (32 packets) for their high-traffic environment, causing valid packets to be dropped. Increasing the window to 128 packets resolved the issue. This highlights the need to tune anti-replay settings based on traffic patterns.

Key Takeaway: Real-world IPsec deployments require careful selection of protocols, algorithms, and parameters, and tuning to avoid performance and security issues.

📌 Summary

This tutorial provided a detailed exploration of the two main IPsec protocols: Authentication Header (AH) and Encapsulating Security Payload (ESP). We examined the packet formats of both protocols, including the fields and their purposes. AH provides integrity and authentication by protecting the IP header and payload, but does not encrypt. ESP provides confidentiality through encryption, and can also provide integrity and authentication when used with an integrity algorithm or in combined mode (AEAD).

We covered the processing steps for both outbound and inbound packets, highlighting the role of the SPD and SAD, the sequence number for anti-replay, and the verification of the ICV. We discussed the anti-replay mechanism in detail, including the sliding window and sequence number management.

We compared transport and tunnel modes, illustrating the packet structures and use cases for each. We also reviewed cryptographic algorithms recommended for IPsec, emphasizing the importance of using strong algorithms (AES-GCM, SHA-256) and avoiding weak ones. The combined mode with AEAD was presented as the modern best practice.

We addressed performance considerations, including the overhead of encryption and the impact on throughput and latency, and provided optimization strategies. Real-world case studies demonstrated the practical application of AH and ESP, as well as common pitfalls and lessons learned.

By the end of this tutorial, you should be able to configure and troubleshoot IPsec using AH and ESP, select appropriate algorithms, and understand the trade-offs involved. The next tutorial will build on this by exploring virtual private networks (VPNs) and secure remote access in depth.

Next: Tutorial 4.13: Virtual Private Networks and Secure Remote Access.

📝 Quiz

1. Which IPsec protocol provides confidentiality (encryption)?

Answer
B. ESP.

2. In the AH header, what field is used to identify the Security Association?

Answer
B. SPI.

3. Which of the following is NOT provided by AH?

Answer
C. Confidentiality.

4. What is the purpose of the Sequence Number field in AH and ESP?

Answer
B. To prevent replay attacks.

5. In ESP, the Authentication Data field is used for:

Answer
B. Integrity and authentication.

6. Which mode of IPsec is typically used for site-to-site VPNs?

Answer
B. Tunnel mode.

7. Which algorithm is an example of AEAD (Authenticated Encryption with Associated Data) commonly used in ESP?

Answer
B. AES-GCM.

8. What happens when the sequence number in an IPsec SA reaches 2^32 - 1?

Answer
B. The SA must be renegotiated (new SA established).

9. In ESP, the Pad Length field indicates:

Answer
B. The number of padding bytes added.

10. Which field in the AH header is set to zero before computing the ICV?

Answer
D. Mutable fields in the IP header (e.g., TTL).

11. What is the main advantage of using ESP combined mode (AEAD) over separate encryption and integrity algorithms?

Answer
B. It reduces processing overhead and simplifies management.

12. Which of the following is a limitation of AH compared to ESP?

Answer
B. AH does not work with NAT (because it protects the IP header).

🛠️ Exercises

Exercise 1: Packet Format Analysis Intermediate

Draw the packet structure for an ESP packet in tunnel mode with authentication (using AES-GCM). Label each field with its size and purpose. Then, draw the structure for AH in transport mode and explain the differences.

Sample Solution

ESP Tunnel with AES-GCM:

  • New IP Header (20 bytes or more)
  • ESP Header: SPI (4), SeqNum (4) → 8 bytes
  • Encrypted Payload: Original IP Header + TCP/UDP + Data (variable), plus padding (0-255 bytes)
  • ESP Trailer: Pad Length (1), Next Header (1) → 2 bytes
  • ICV (16 bytes for AES-GCM)

AH Transport:

  • Original IP Header
  • AH Header: Next Header (1), Payload Len (1), Reserved (2), SPI (4), SeqNum (4), ICV (variable)
  • TCP/UDP + Payload

Key differences: ESP encrypts, AH doesn't; ESP adds padding, AH doesn't; AH protects the IP header, ESP doesn't (except in tunnel).

Exercise 2: Anti-Replay Window Calculation Intermediate

An IPsec receiver has an anti-replay window of size 64. The last valid sequence number received was 1000. A packet arrives with sequence number 950. Is it accepted? What if it is 1020? Explain the logic.

Sample Solution

The window covers numbers from (1000 - 64 + 1) = 937 to 1000. So, a packet with seq 950 is within the window and may be accepted if not already seen. Seq 1020 is greater than 1000, which is beyond the window; it would be accepted as long as it's not a replay, and the window would shift to cover 957 to 1020. However, if seq 950 is less than 937, it would be rejected.

Exercise 3: Algorithm Selection Introductory

You are configuring a new IPsec VPN. Which combination of algorithms would you choose: (AES-CBC + HMAC-SHA-256) or (AES-GCM)? Explain your choice.

Sample Solution

Choose AES-GCM. It is an AEAD algorithm that provides both encryption and authentication in one pass, reducing overhead and complexity. It is also more secure against certain attacks (e.g., padding oracle). AES-GCM is recommended by NIST and IETF.

Exercise 4: Processing Order Advanced

For an outbound ESP packet with separate encryption (AES-CBC) and authentication (HMAC-SHA-256), describe the order of operations. Does the packet get encrypted first, then authenticated, or vice versa? Explain why.

Sample Solution

The order is: encrypt first, then authenticate (compute ICV over the encrypted payload). This is because the authentication covers the encrypted data to detect tampering with the ciphertext. If authentication were done first, the encryption would change the data and the authentication would fail. In practice, AEAD is preferred to avoid this complexity.

Exercise 5: Configuration Scenario Intermediate

A network administrator wants to set up IPsec between two hosts for secure management (SSH). They decide to use AH in transport mode. What are the implications of this choice? Would ESP be better? Explain.

Sample Solution

Using AH in transport mode provides authentication and integrity but no encryption. SSH itself provides encryption, so AH adds authentication of the IP headers. However, AH does not work with NAT, which may be a problem if the management path involves NAT. ESP with null encryption could be used, but modern recommendations are to use ESP with encryption (e.g., AES-GCM) to protect both the IP payload and the transport header. Since SSH already encrypts the payload, ESP in transport mode with encryption may be redundant but still adds integrity and authentication for the IP header if combined mode is used.

📚 Homework

Homework 1: Write a 2,000-word research paper comparing the security and performance characteristics of ESP with AES-CBC + HMAC-SHA-256 vs. ESP with AES-GCM. Discuss the cryptographic differences, potential vulnerabilities, and performance benchmarks. Include references.

Sample Answer

Key points: AES-CBC requires padding, is susceptible to padding oracle attacks if not used with integrity (but with HMAC, it's mitigated); AES-GCM is a stream cipher with integrated authentication, faster in hardware (AES-NI), and simpler. Performance: AES-GCM is generally faster because it combines operations. Security: both are secure when used properly, but AES-GCM is recommended for new deployments.

Homework 2: Using a packet capture tool (Wireshark) or a network simulator, capture IPsec traffic (ESP) and analyze the packet structure. Identify the SPI, sequence number, encrypted payload, and ICV. Write a lab report with screenshots and explanations.

Sample Answer

Practical assignment; report should include setup of IPsec (e.g., using strongSwan or a VPN), capture of traffic, and analysis of ESP packets showing the headers and encrypted data.

Homework 3: Investigate the NAT traversal mechanisms for IPsec (NAT-T). Explain how ESP can work through NAT using UDP encapsulation. Write a 1,500-word paper covering the challenges, the solution (RFC 3947/3948), and the configuration steps.

Sample Answer

Key points: NAT changes IP addresses, which breaks AH and ESP because the IP header or SPI may be modified. NAT-T encapsulates ESP in UDP (port 4500) and uses IKE to negotiate NAT presence. The UDP header is not protected, allowing NAT to change addresses. The encapsulation adds overhead but enables IPsec through NAT.

Homework 4: Design an IPsec policy for a secure connection between a branch office and the headquarters. Specify the mode (transport/tunnel), the protocols (AH/ESP), the algorithms (encryption, integrity), key management (IKE), and anti-replay parameters. Justify your choices.

Sample Answer

Policy: Use ESP in tunnel mode with AES-256-GCM (combined mode), IKEv2 with certificate-based authentication, PFS enabled, sequence number anti-replay with window size 128. Justification: tunnel mode protects all traffic; AES-GCM is efficient and secure; IKEv2 is robust; PFS ensures forward secrecy.

Homework 5: Research the vulnerability "CRIME" or "BEAST" (or other attacks on TLS) and discuss whether a similar attack could apply to IPsec. Write a case study analyzing the risks and mitigation.

Sample Answer

Key points: BEAST and CRIME are attacks on TLS/SSL that exploit CBC mode and compression. IPsec with ESP using CBC mode may be vulnerable to similar attacks if not properly implemented, but the risk is mitigated by using AEAD algorithms (AES-GCM) and disabling compression.


COMP400 – Computer and Network Security (Revision 3) • Unit 4: Security Systems and Models