1 diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
2 index 69e7ae3b..99373466 100644
3 --- a/scapy/layers/ipsec.py
4 +++ b/scapy/layers/ipsec.py
5 @@ -518,12 +517,16 @@ class AuthAlgo(object):
7 return self.mac(key, self.digestmod(), default_backend())
9 - def sign(self, pkt, key):
10 + def sign(self, pkt, key, trailer=None):
12 Sign an IPsec (ESP or AH) packet with this algo.
14 @param pkt: a packet that contains a valid encrypted ESP or AH layer
15 @param key: the authentication key, a byte string
16 + @param trailer: additional data appended to the packet for ICV
17 + calculation, but not trnasmitted with the packet.
18 + For example, the high order bits of the exteneded
21 @return: the signed packet
23 @@ -539,11 +542,13 @@ class AuthAlgo(object):
24 elif pkt.haslayer(AH):
25 clone = zero_mutable_fields(pkt.copy(), sending=True)
26 mac.update(raw(clone))
29 pkt[AH].icv = mac.finalize()[:self.icv_size]
33 - def verify(self, pkt, key):
34 + def verify(self, pkt, key, trailer):
36 Check that the integrity check value (icv) of a packet is valid.
38 @@ -574,6 +579,8 @@ class AuthAlgo(object):
39 clone = zero_mutable_fields(pkt.copy(), sending=False)
41 mac.update(raw(clone))
43 + mac.update(trailer) # bytearray(4)) #raw(trailer))
44 computed_icv = mac.finalize()[:self.icv_size]
46 # XXX: Cannot use mac.verify because the ICV can be truncated
47 @@ -757,7 +764,8 @@ class SecurityAssociation(object):
48 SUPPORTED_PROTOS = (IP, IPv6)
50 def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
51 - auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None):
52 + auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None,
55 @param proto: the IPsec proto to use (ESP or AH)
56 @param spi: the Security Parameters Index of this SA
57 @@ -771,6 +779,7 @@ class SecurityAssociation(object):
58 to encapsulate the encrypted packets.
59 @param nat_t_header: an instance of a UDP header that will be used
61 + @param use_esn: Use Extended Sequence Numbers
64 if proto not in (ESP, AH, ESP.name, AH.name):
65 @@ -782,6 +791,7 @@ class SecurityAssociation(object):
68 self.seq_num = seq_num
69 + self.use_esn = use_esn
72 if crypt_algo not in CRYPT_ALGOS:
73 @@ -827,6 +837,17 @@ class SecurityAssociation(object):
74 raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
77 + def build_seq_num(self, num):
78 + # only lower order bits are transmitted
79 + # higher order bits are used in the ICV
80 + lower = num & 0xffffffff
84 + return lower, struct.pack(">I", upper)
88 def _encrypt_esp(self, pkt, seq_num=None, iv=None):
91 @@ -835,7 +856,8 @@ class SecurityAssociation(object):
92 if len(iv) != self.crypt_algo.iv_size:
93 raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
95 - esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
96 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
97 + esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
99 if self.tunnel_header:
100 tunnel = self.tunnel_header.copy()
101 @@ -857,7 +879,7 @@ class SecurityAssociation(object):
102 esp = self.crypt_algo.pad(esp)
103 esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
105 - self.auth_algo.sign(esp, self.auth_key)
106 + self.auth_algo.sign(esp, self.auth_key, high_seq_num)
108 if self.nat_t_header:
109 nat_t_header = self.nat_t_header.copy()
110 @@ -884,7 +906,8 @@ class SecurityAssociation(object):
112 def _encrypt_ah(self, pkt, seq_num=None):
114 - ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
115 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
116 + ah = AH(spi=self.spi, seq=low_seq_num,
117 icv = b"\x00" * self.auth_algo.icv_size)
119 if self.tunnel_header:
120 @@ -924,7 +947,8 @@ class SecurityAssociation(object):
122 ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
124 - signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key)
125 + signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
126 + self.auth_key, high_seq_num)
128 # sequence number must always change, unless specified by the user
130 @@ -955,11 +979,12 @@ class SecurityAssociation(object):
132 def _decrypt_esp(self, pkt, verify=True):
134 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
139 - self.auth_algo.verify(encrypted, self.auth_key)
140 + self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
142 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
143 self.crypt_algo.icv_size or
144 @@ -998,9 +1023,11 @@ class SecurityAssociation(object):
146 def _decrypt_ah(self, pkt, verify=True):
148 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
152 - self.auth_algo.verify(pkt, self.auth_key)
153 + self.auth_algo.verify(pkt, self.auth_key, high_seq_num)