1 diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
2 index 69e7ae3b..3a1724b2 100644
3 --- a/scapy/layers/ipsec.py
4 +++ b/scapy/layers/ipsec.py
5 @@ -344,8 +344,7 @@ class CryptAlgo(object):
6 encryptor = cipher.encryptor()
9 - aad = struct.pack('!LL', esp.spi, esp.seq)
10 - encryptor.authenticate_additional_data(aad)
11 + encryptor.authenticate_additional_data(sa.build_aead(esp))
12 data = encryptor.update(data) + encryptor.finalize()
13 data += encryptor.tag[:self.icv_size]
15 @@ -380,10 +379,7 @@ class CryptAlgo(object):
18 # Tag value check is done during the finalize method
19 - decryptor.authenticate_additional_data(
20 - struct.pack('!LL', esp.spi, esp.seq)
23 + decryptor.authenticate_additional_data(sa.build_aead(esp))
25 data = decryptor.update(data) + decryptor.finalize()
26 except InvalidTag as err:
27 @@ -518,12 +514,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 +534,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 +578,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 +763,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 +778,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 +790,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 +836,23 @@ class SecurityAssociation(object):
103 raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
106 + def build_aead(self, esp):
108 + return (struct.pack('!LLL', esp.spi, self.seq_num >> 32, esp.seq))
110 + return (struct.pack('!LL', esp.spi, esp.seq))
112 + def build_seq_num(self, num):
113 + # only lower order bits are transmitted
114 + # higher order bits are used in the ICV
115 + lower = num & 0xffffffff
119 + return lower, struct.pack("!I", upper)
123 def _encrypt_esp(self, pkt, seq_num=None, iv=None):
126 @@ -835,7 +861,8 @@ class SecurityAssociation(object):
127 if len(iv) != self.crypt_algo.iv_size:
128 raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
130 - esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
131 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
132 + esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
134 if self.tunnel_header:
135 tunnel = self.tunnel_header.copy()
136 @@ -857,7 +884,7 @@ class SecurityAssociation(object):
137 esp = self.crypt_algo.pad(esp)
138 esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
140 - self.auth_algo.sign(esp, self.auth_key)
141 + self.auth_algo.sign(esp, self.auth_key, high_seq_num)
143 if self.nat_t_header:
144 nat_t_header = self.nat_t_header.copy()
145 @@ -884,7 +911,8 @@ class SecurityAssociation(object):
147 def _encrypt_ah(self, pkt, seq_num=None):
149 - ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
150 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
151 + ah = AH(spi=self.spi, seq=low_seq_num,
152 icv = b"\x00" * self.auth_algo.icv_size)
154 if self.tunnel_header:
155 @@ -924,7 +952,8 @@ class SecurityAssociation(object):
157 ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
159 - signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key)
160 + signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
161 + self.auth_key, high_seq_num)
163 # sequence number must always change, unless specified by the user
165 @@ -955,11 +984,12 @@ class SecurityAssociation(object):
167 def _decrypt_esp(self, pkt, verify=True):
169 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
174 - self.auth_algo.verify(encrypted, self.auth_key)
175 + self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
177 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
178 self.crypt_algo.icv_size or
179 @@ -998,9 +1028,11 @@ class SecurityAssociation(object):
181 def _decrypt_ah(self, pkt, verify=True):
183 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
187 - self.auth_algo.verify(pkt, self.auth_key)
188 + self.auth_algo.verify(pkt, self.auth_key, high_seq_num)