Understanding IPsec: How It Secures Data in Transit

IPsec is a suite of protocols that protects IP traffic at the network layer, so every application benefits without changes. It uses ESP (Encapsulating Security Payload) to encrypt and authenticate packets, and optionally AH (Authentication Header) for integrity without encryption. IKEv2 (RFC 7296) authenticates the two endpoints and negotiates the keys and algorithms, creating security associations.

May 13, 2025 - 12:37
Updated: 
101.8k

Network Security in Cybersecurity & Ethical Hacking · Explainer

Understanding IPsec: How It Secures Data in Transit

Quick answer: IPsec is a suite of protocols that protects IP traffic at the network layer, so every application benefits without changes. It uses ESP (Encapsulating Security Payload) to encrypt and authenticate packets, and optionally AH (Authentication Header) for integrity without encryption. IKEv2 (RFC 7296) authenticates the two endpoints and negotiates the keys and algorithms, creating security associations. IPsec runs in transport mode (host to host) or tunnel mode (site to site or remote access VPN). NIST SP 800-77 Rev. 1 recommends IKEv2 with ESP and modern algorithms such as AES-GCM.

Key takeaways

  • IPsec secures traffic at Layer 3, below TCP and UDP.
  • ESP provides confidentiality and integrity; AH provides integrity only and breaks with NAT.
  • IKEv2 handles mutual authentication and key exchange.
  • Tunnel mode wraps the whole original packet; transport mode protects only the payload.
  • Use strong algorithms, certificates and patched VPN gateways.

Table of Contents

What Is IPsec and Where Does It Sit?

The IPsec architecture is defined in RFC 4301. Because it works at the IP layer, it protects any protocol above it: web, email, file sharing or custom applications. Each protected direction of traffic uses a security association (SA) that records the keys, algorithms and lifetime. Policies decide which traffic must be protected, bypassed or dropped.

What Is the Difference Between AH and ESP?

FeatureAH (RFC 4302)ESP (RFC 4303)
IP protocol number5150
EncryptionNoYes
Integrity and origin authenticationYes, including parts of the outer IP headerYes, for the ESP payload
Anti-replayYesYes
Works through NATNoYes, with UDP encapsulation
Common todayRareStandard choice

See RFC 4302 and RFC 4303. In practice almost all deployments use ESP with authenticated encryption.

What Are Transport Mode and Tunnel Mode?

Transport mode keeps the original IP header and protects the payload. It suits host-to-host links, such as securing traffic between two servers. Tunnel mode encrypts the entire original packet and adds a new outer IP header between gateways. It is used for site-to-site VPNs between offices and for remote access VPNs. Our guide to VPN types including IPsec, L2TP and SSL VPN compares where each fits.

How Does IKEv2 Set Up a Secure Connection?

IKEv2 (RFC 7296) runs over UDP port 500 and normally needs two exchanges:

  1. IKE_SA_INIT: the peers agree algorithms, exchange Diffie-Hellman values and nonces, and derive keys for a protected IKE channel.
  2. IKE_AUTH: inside that channel, each side proves its identity with certificates, EAP or a pre-shared key, and the first child SA for ESP is created.

Later exchanges (CREATE_CHILD_SA) rekey SAs before their lifetimes expire. IKEv1 is older and more complex; NIST guidance prefers IKEv2.

How Does IPsec Work Through NAT?

NAT devices rewrite addresses and ports, which ESP alone does not have. IKEv2 detects NAT during setup, then both peers wrap ESP inside UDP port 4500 as defined in RFC 3948. Firewalls protecting IPsec gateways therefore typically allow UDP 500, UDP 4500 and IP protocol 50.

Which Algorithms Should You Use?

PurposeRecommendedAvoid
ESP encryptionAES-GCM (128 or 256 bit)DES, 3DES, NULL encryption
Integrity (if not AEAD)HMAC-SHA-256 or strongerHMAC-MD5
Key exchange groupsECDH P-256/P-384, Curve25519, 3072-bit MODP or largerMODP 768 and 1024
Peer authenticationCertificatesShort or shared pre-shared keys

These follow RFC 8221, RFC 8247 and NIST SP 800-77 Rev. 1.

How Do You Harden and Troubleshoot IPsec?

  • Patch VPN gateways quickly; CISA and NSA guidance notes that remote access VPNs are frequent targets.
  • Require MFA for remote access users and log every tunnel up and down event.
  • Disable IKEv1 and weak proposals where your devices allow.
  • On Linux with strongSwan, check tunnel state read-only:
sudo swanctl --list-sas
sudo ip xfrm state

If you are choosing between IPsec and TLS-based VPNs, read our SSL/TLS VPN vs IPsec VPN comparison. Under CERT-In directions, VPN logs are among those organisations should retain for 180 days. To configure IPsec in labs, see our network security training.

References

Frequently Asked Questions

IPsec protects IP packets with encryption, integrity and origin authentication, so any application above the IP layer is covered.

ESP encrypts and authenticates packet payloads, while AH only authenticates and does not work through NAT, so ESP is standard.

IKE uses UDP port 500, NAT traversal uses UDP port 4500, and native ESP uses IP protocol number 50 rather than a port.

Tunnel mode encrypts the whole original packet behind a new header; transport mode protects only the payload between two hosts.

IKEv2 is simpler, needs fewer messages, supports NAT traversal and EAP natively, and is preferred by NIST guidance.

Long random pre-shared keys can work for small setups, but certificates are stronger and easier to revoke at scale.

Neither is always better. IPsec suits site-to-site links, while TLS VPNs are often easier for remote users behind restrictive firewalls.

NIST SP 800-77 Revision 1, Guide to IPsec VPNs, covers architecture, algorithm choices and deployment planning.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0
Vaishnavi

Vaishnavi is a skilled tech professional at the Ethical Hacking Training Institute in Pune, responsible for managing and optimizing the technical infrastructure that supports advanced cybersecurity education. With deep expertise in network security, backend operations, and system performance, she ensures that practical labs, online modules, and assessments run smoothly and securely. Her behind-the-scenes contributions play a vital role in delivering a seamless and secure learning experience for aspiring ethical hackers.