1 diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
2 index 69e7ae3b..b35da2b1 100644
3 --- a/scapy/layers/ipsec.py
4 +++ b/scapy/layers/ipsec.py
5 @@ -518,12 +518,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 @@ -534,16 +538,20 @@ class AuthAlgo(object):
26 mac.update(raw(pkt[ESP]))
29 pkt[ESP].data += mac.finalize()[:self.icv_size]
31 elif pkt.haslayer(AH):
32 clone = zero_mutable_fields(pkt.copy(), sending=True)
33 mac.update(raw(clone))
36 pkt[AH].icv = mac.finalize()[:self.icv_size]
40 - def verify(self, pkt, key):
41 + def verify(self, pkt, key, trailer):
43 Check that the integrity check value (icv) of a packet is valid.
45 @@ -574,6 +582,8 @@ class AuthAlgo(object):
46 clone = zero_mutable_fields(pkt.copy(), sending=False)
48 mac.update(raw(clone))
50 + mac.update(trailer) # bytearray(4)) #raw(trailer))
51 computed_icv = mac.finalize()[:self.icv_size]
53 # XXX: Cannot use mac.verify because the ICV can be truncated
54 @@ -757,7 +767,8 @@ class SecurityAssociation(object):
55 SUPPORTED_PROTOS = (IP, IPv6)
57 def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
58 - auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None):
59 + auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None,
62 @param proto: the IPsec proto to use (ESP or AH)
63 @param spi: the Security Parameters Index of this SA
64 @@ -771,6 +782,7 @@ class SecurityAssociation(object):
65 to encapsulate the encrypted packets.
66 @param nat_t_header: an instance of a UDP header that will be used
68 + @param use_esn: Use Extended Sequence Numbers
71 if proto not in (ESP, AH, ESP.name, AH.name):
72 @@ -782,6 +794,7 @@ class SecurityAssociation(object):
75 self.seq_num = seq_num
76 + self.use_esn = use_esn
79 if crypt_algo not in CRYPT_ALGOS:
80 @@ -827,6 +840,17 @@ class SecurityAssociation(object):
81 raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
84 + def build_seq_num(self, num):
85 + # only lower order bits are transmitted
86 + # higher order bits are used in the ICV
87 + lower = num & 0xffffffff
91 + return lower, struct.pack(">I", upper)
95 def _encrypt_esp(self, pkt, seq_num=None, iv=None):
98 @@ -835,7 +859,8 @@ class SecurityAssociation(object):
99 if len(iv) != self.crypt_algo.iv_size:
100 raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
102 - esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
103 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
104 + esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
106 if self.tunnel_header:
107 tunnel = self.tunnel_header.copy()
108 @@ -857,7 +882,7 @@ class SecurityAssociation(object):
109 esp = self.crypt_algo.pad(esp)
110 esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
112 - self.auth_algo.sign(esp, self.auth_key)
113 + self.auth_algo.sign(esp, self.auth_key, high_seq_num)
115 if self.nat_t_header:
116 nat_t_header = self.nat_t_header.copy()
117 @@ -884,7 +909,8 @@ class SecurityAssociation(object):
119 def _encrypt_ah(self, pkt, seq_num=None):
121 - ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
122 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
123 + ah = AH(spi=self.spi, seq=low_seq_num,
124 icv = b"\x00" * self.auth_algo.icv_size)
126 if self.tunnel_header:
127 @@ -924,7 +950,8 @@ class SecurityAssociation(object):
129 ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
131 - signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key)
132 + signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
133 + self.auth_key, high_seq_num)
135 # sequence number must always change, unless specified by the user
137 @@ -955,11 +982,12 @@ class SecurityAssociation(object):
139 def _decrypt_esp(self, pkt, verify=True):
141 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
146 - self.auth_algo.verify(encrypted, self.auth_key)
147 + self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
149 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
150 self.crypt_algo.icv_size or
151 @@ -998,9 +1026,11 @@ class SecurityAssociation(object):
153 def _decrypt_ah(self, pkt, verify=True):
155 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
159 - self.auth_algo.verify(pkt, self.auth_key)
160 + self.auth_algo.verify(pkt, self.auth_key, high_seq_num)