IPsec (Internet Protocol Security) is a suite of protocols that provides security for IP communications by authenticating and/or encrypting each IP packet. It operates at the network layer and is commonly used for VPNs.
- Transport Mode: Encrypts only the payload and ESP trailer. Used for end-to-end (host-to-host) communication.
- Tunnel Mode: Encrypts the entire IP packet and wraps it in a new IP header. Used for site-to-site VPNs or host-to-gateway.
- AH (Authentication Header): Integrity and authentication, no encryption.
- ESP (Encapsulating Security Payload): Provides encryption, integrity, and authentication.
- A one-way relationship between sender and receiver.
- Defines keys, algorithms, and parameters.
- Identified by:
- SPI (Security Parameter Index) — 32-bit ID
- Destination IP
- Protocol (AH/ESP)
- Usually done using IKE (Internet Key Exchange).
- Negotiates SAs and manages keys.
- SPI is a unique ID used to look up the SA.
- It's included in the IPsec header (AH or ESP).
- When a packet arrives:
- The system looks at the SPI, protocol, and destination IP.
- It retrieves the SA.
- It uses the SA to decrypt/authenticate the packet.
- Alice (
10.0.0.1
) sends data to Bob (10.0.0.2
) via IPsec tunnel between gateways:- Alice's gateway:
GW-A
(192.168.1.1
) - Bob's gateway:
GW-B
(192.168.2.1
)
- Alice's gateway:
- Using ESP in tunnel mode.
Alice sends:
Payload: "GET /index.html HTTP/1.1..."
TCP Header: Src=10.0.0.1:12345, Dst=10.0.0.2:80
+ Payload
IP Header: Src=10.0.0.1, Dst=10.0.0.2
+ TCP Header
+ Payload
- Wraps the entire original IP packet.
- Adds:
- ESP Header (contains SPI, sequence number)
- ESP Trailer (padding, Next Header)
- ESP Authentication (optional)
- Encrypts:
- TCP Header + Payload + ESP Trailer
- Then adds a new IP header:
New IP Header: Src=192.168.1.1, Dst=192.168.2.1
ESP Header (SPI=0xAABBCCDD, Seq=42)
Encrypted {
Original IP Header (10.0.0.1 → 10.0.0.2)
TCP Header
Payload
ESP Trailer
}
ESP Auth (optional)
Standard IP routing happens based on:
New IP Header: 192.168.1.1 → 192.168.2.1
- Uses:
- SPI + destination IP + protocol (ESP)
- Finds the SA.
- Verifies/authenticates packet.
- Decrypts inner content.
- Removes ESP and new IP header.
IP Header: Src=10.0.0.1, Dst=10.0.0.2
TCP Header
Payload
Layer | Content Before IPsec | Content After IPsec (Tunnel, ESP) |
---|---|---|
App | GET /index.html... |
Same |
TCP | TCP Header + Payload | Encrypted |
IP | IP Header (A → B) | Encrypted |
IPsec | — | ESP Header + Encrypted Block |
Outer IP | — | New IP Header (GW-A → GW-B) |