1 diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
2 index 69e7ae3b..0c69ba1a 100644
3 --- a/scapy/layers/ipsec.py
4 +++ b/scapy/layers/ipsec.py
5 @@ -344,7 +344,8 @@ class CryptAlgo(object):
6 encryptor = cipher.encryptor()
9 - aad = struct.pack('!LL', esp.spi, esp.seq)
10 + aad = struct.pack('!LQ' if sa.use_esn else '!LL',
12 encryptor.authenticate_additional_data(aad)
13 data = encryptor.update(data) + encryptor.finalize()
14 data += encryptor.tag[:self.icv_size]
15 @@ -381,9 +382,9 @@ class CryptAlgo(object):
17 # Tag value check is done during the finalize method
18 decryptor.authenticate_additional_data(
19 - struct.pack('!LL', esp.spi, esp.seq)
20 + struct.pack('!LQ' if sa.use_esn else '!LL',
25 data = decryptor.update(data) + decryptor.finalize()
26 except InvalidTag as err:
27 @@ -518,12 +519,16 @@ class AuthAlgo(object):
29 return self.mac(key, self.digestmod(), default_backend())
31 - def sign(self, pkt, key):
32 + def sign(self, pkt, key, trailer=None):
34 Sign an IPsec (ESP or AH) packet with this algo.
36 @param pkt: a packet that contains a valid encrypted ESP or AH layer
37 @param key: the authentication key, a byte string
38 + @param trailer: additional data appended to the packet for ICV
39 + calculation, but not trnasmitted with the packet.
40 + For example, the high order bits of the exteneded
43 @return: the signed packet
45 @@ -534,16 +539,20 @@ class AuthAlgo(object):
48 mac.update(raw(pkt[ESP]))
51 pkt[ESP].data += mac.finalize()[:self.icv_size]
53 elif pkt.haslayer(AH):
54 clone = zero_mutable_fields(pkt.copy(), sending=True)
55 mac.update(raw(clone))
58 pkt[AH].icv = mac.finalize()[:self.icv_size]
62 - def verify(self, pkt, key):
63 + def verify(self, pkt, key, trailer):
65 Check that the integrity check value (icv) of a packet is valid.
67 @@ -574,6 +583,8 @@ class AuthAlgo(object):
68 clone = zero_mutable_fields(pkt.copy(), sending=False)
70 mac.update(raw(clone))
72 + mac.update(trailer) # bytearray(4)) #raw(trailer))
73 computed_icv = mac.finalize()[:self.icv_size]
75 # XXX: Cannot use mac.verify because the ICV can be truncated
76 @@ -757,7 +768,8 @@ class SecurityAssociation(object):
77 SUPPORTED_PROTOS = (IP, IPv6)
79 def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
80 - auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None):
81 + auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None,
84 @param proto: the IPsec proto to use (ESP or AH)
85 @param spi: the Security Parameters Index of this SA
86 @@ -771,6 +783,7 @@ class SecurityAssociation(object):
87 to encapsulate the encrypted packets.
88 @param nat_t_header: an instance of a UDP header that will be used
90 + @param use_esn: Use Extended Sequence Numbers
93 if proto not in (ESP, AH, ESP.name, AH.name):
94 @@ -782,6 +795,7 @@ class SecurityAssociation(object):
97 self.seq_num = seq_num
98 + self.use_esn = use_esn
101 if crypt_algo not in CRYPT_ALGOS:
102 @@ -827,6 +841,17 @@ class SecurityAssociation(object):
103 raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
106 + def build_seq_num(self, num):
107 + # only lower order bits are transmitted
108 + # higher order bits are used in the ICV
109 + lower = num & 0xffffffff
113 + return lower, struct.pack("!I", upper)
117 def _encrypt_esp(self, pkt, seq_num=None, iv=None):
120 @@ -835,7 +860,8 @@ class SecurityAssociation(object):
121 if len(iv) != self.crypt_algo.iv_size:
122 raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
124 - esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
125 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
126 + esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
128 if self.tunnel_header:
129 tunnel = self.tunnel_header.copy()
130 @@ -857,7 +883,7 @@ class SecurityAssociation(object):
131 esp = self.crypt_algo.pad(esp)
132 esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
134 - self.auth_algo.sign(esp, self.auth_key)
135 + self.auth_algo.sign(esp, self.auth_key, high_seq_num)
137 if self.nat_t_header:
138 nat_t_header = self.nat_t_header.copy()
139 @@ -884,7 +910,8 @@ class SecurityAssociation(object):
141 def _encrypt_ah(self, pkt, seq_num=None):
143 - ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
144 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
145 + ah = AH(spi=self.spi, seq=low_seq_num,
146 icv = b"\x00" * self.auth_algo.icv_size)
148 if self.tunnel_header:
149 @@ -924,7 +951,8 @@ class SecurityAssociation(object):
151 ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
153 - signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key)
154 + signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
155 + self.auth_key, high_seq_num)
157 # sequence number must always change, unless specified by the user
159 @@ -955,11 +983,12 @@ class SecurityAssociation(object):
161 def _decrypt_esp(self, pkt, verify=True):
163 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
168 - self.auth_algo.verify(encrypted, self.auth_key)
169 + self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
171 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
172 self.crypt_algo.icv_size or
173 @@ -998,9 +1027,11 @@ class SecurityAssociation(object):
175 def _decrypt_ah(self, pkt, verify=True):
177 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
181 - self.auth_algo.verify(pkt, self.auth_key)
182 + self.auth_algo.verify(pkt, self.auth_key, high_seq_num)