1 diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
2 index 69e7ae3b..d9c1705b 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 @@ -422,6 +418,7 @@ if algorithms:
28 CRYPT_ALGOS['AES-CTR'] = CryptAlgo('AES-CTR',
29 cipher=algorithms.AES,
34 format_mode_iv=_aes_ctr_format_mode_iv)
35 @@ -429,6 +426,7 @@ if algorithms:
36 CRYPT_ALGOS['AES-GCM'] = CryptAlgo('AES-GCM',
37 cipher=algorithms.AES,
43 @@ -437,6 +435,7 @@ if algorithms:
44 CRYPT_ALGOS['AES-CCM'] = CryptAlgo('AES-CCM',
45 cipher=algorithms.AES,
51 @@ -518,12 +517,16 @@ class AuthAlgo(object):
53 return self.mac(key, self.digestmod(), default_backend())
55 - def sign(self, pkt, key):
56 + def sign(self, pkt, key, trailer=None):
58 Sign an IPsec (ESP or AH) packet with this algo.
60 @param pkt: a packet that contains a valid encrypted ESP or AH layer
61 @param key: the authentication key, a byte string
62 + @param trailer: additional data appended to the packet for ICV
63 + calculation, but not trnasmitted with the packet.
64 + For example, the high order bits of the exteneded
67 @return: the signed packet
69 @@ -534,16 +537,20 @@ class AuthAlgo(object):
72 mac.update(raw(pkt[ESP]))
75 pkt[ESP].data += mac.finalize()[:self.icv_size]
77 elif pkt.haslayer(AH):
78 clone = zero_mutable_fields(pkt.copy(), sending=True)
79 mac.update(raw(clone))
82 pkt[AH].icv = mac.finalize()[:self.icv_size]
86 - def verify(self, pkt, key):
87 + def verify(self, pkt, key, trailer):
89 Check that the integrity check value (icv) of a packet is valid.
91 @@ -574,6 +581,8 @@ class AuthAlgo(object):
92 clone = zero_mutable_fields(pkt.copy(), sending=False)
94 mac.update(raw(clone))
96 + mac.update(trailer) # bytearray(4)) #raw(trailer))
97 computed_icv = mac.finalize()[:self.icv_size]
99 # XXX: Cannot use mac.verify because the ICV can be truncated
100 @@ -757,7 +766,8 @@ class SecurityAssociation(object):
101 SUPPORTED_PROTOS = (IP, IPv6)
103 def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
104 - auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None):
105 + auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None,
108 @param proto: the IPsec proto to use (ESP or AH)
109 @param spi: the Security Parameters Index of this SA
110 @@ -771,6 +781,7 @@ class SecurityAssociation(object):
111 to encapsulate the encrypted packets.
112 @param nat_t_header: an instance of a UDP header that will be used
114 + @param use_esn: Use Extended Sequence Numbers
117 if proto not in (ESP, AH, ESP.name, AH.name):
118 @@ -782,6 +793,7 @@ class SecurityAssociation(object):
121 self.seq_num = seq_num
122 + self.use_esn = use_esn
125 if crypt_algo not in CRYPT_ALGOS:
126 @@ -827,6 +839,23 @@ class SecurityAssociation(object):
127 raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
130 + def build_aead(self, esp):
132 + return (struct.pack('!LLL', esp.spi, self.seq_num >> 32, esp.seq))
134 + return (struct.pack('!LL', esp.spi, esp.seq))
136 + def build_seq_num(self, num):
137 + # only lower order bits are transmitted
138 + # higher order bits are used in the ICV
139 + lower = num & 0xffffffff
143 + return lower, struct.pack("!I", upper)
147 def _encrypt_esp(self, pkt, seq_num=None, iv=None):
150 @@ -835,7 +864,8 @@ class SecurityAssociation(object):
151 if len(iv) != self.crypt_algo.iv_size:
152 raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
154 - esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
155 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
156 + esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
158 if self.tunnel_header:
159 tunnel = self.tunnel_header.copy()
160 @@ -857,7 +887,7 @@ class SecurityAssociation(object):
161 esp = self.crypt_algo.pad(esp)
162 esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
164 - self.auth_algo.sign(esp, self.auth_key)
165 + self.auth_algo.sign(esp, self.auth_key, high_seq_num)
167 if self.nat_t_header:
168 nat_t_header = self.nat_t_header.copy()
169 @@ -884,7 +914,8 @@ class SecurityAssociation(object):
171 def _encrypt_ah(self, pkt, seq_num=None):
173 - ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
174 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
175 + ah = AH(spi=self.spi, seq=low_seq_num,
176 icv = b"\x00" * self.auth_algo.icv_size)
178 if self.tunnel_header:
179 @@ -924,7 +955,8 @@ class SecurityAssociation(object):
181 ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
183 - signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key)
184 + signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
185 + self.auth_key, high_seq_num)
187 # sequence number must always change, unless specified by the user
189 @@ -955,11 +987,12 @@ class SecurityAssociation(object):
191 def _decrypt_esp(self, pkt, verify=True):
193 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
198 - self.auth_algo.verify(encrypted, self.auth_key)
199 + self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
201 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
202 self.crypt_algo.icv_size or
203 @@ -998,9 +1031,11 @@ class SecurityAssociation(object):
205 def _decrypt_ah(self, pkt, verify=True):
207 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
211 - self.auth_algo.verify(pkt, self.auth_key)
212 + self.auth_algo.verify(pkt, self.auth_key, high_seq_num)