1 diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
2 index ae057ee1..55d0dd53 100644
3 --- a/scapy/layers/ipsec.py
4 +++ b/scapy/layers/ipsec.py
5 @@ -56,6 +56,7 @@ from scapy.fields import ByteEnumField, ByteField, IntField, PacketField, \
6 ShortField, StrField, XIntField, XStrField, XStrLenField
7 from scapy.packet import Packet, bind_layers, Raw
8 from scapy.layers.inet import IP, UDP
9 +from scapy.contrib.mpls import MPLS
10 import scapy.modules.six as six
11 from scapy.modules.six.moves import range
12 from scapy.layers.inet6 import IPv6, IPv6ExtHdrHopByHop, IPv6ExtHdrDestOpt, \
13 @@ -359,11 +360,8 @@ class CryptAlgo(object):
14 encryptor = cipher.encryptor()
18 - aad = struct.pack('!LLL', esp.spi, esn, esp.seq)
20 - aad = struct.pack('!LL', esp.spi, esp.seq)
21 - encryptor.authenticate_additional_data(aad)
22 + encryptor.authenticate_additional_data(sa.build_aead(esp))
24 data = encryptor.update(data) + encryptor.finalize()
25 data += encryptor.tag[:self.icv_size]
27 @@ -400,12 +398,7 @@ class CryptAlgo(object):
30 # Tag value check is done during the finalize method
32 - decryptor.authenticate_additional_data(
33 - struct.pack('!LLL', esp.spi, esn, esp.seq))
35 - decryptor.authenticate_additional_data(
36 - struct.pack('!LL', esp.spi, esp.seq))
37 + decryptor.authenticate_additional_data(sa.build_aead(esp))
39 data = decryptor.update(data) + decryptor.finalize()
40 except InvalidTag as err:
41 @@ -445,6 +438,7 @@ if algorithms:
42 CRYPT_ALGOS['AES-CTR'] = CryptAlgo('AES-CTR',
43 cipher=algorithms.AES,
48 format_mode_iv=_aes_ctr_format_mode_iv)
49 @@ -452,6 +446,7 @@ if algorithms:
50 CRYPT_ALGOS['AES-GCM'] = CryptAlgo('AES-GCM',
51 cipher=algorithms.AES,
57 @@ -460,6 +455,7 @@ if algorithms:
58 CRYPT_ALGOS['AES-CCM'] = CryptAlgo('AES-CCM',
59 cipher=algorithms.AES,
65 @@ -544,7 +540,7 @@ class AuthAlgo(object):
67 return self.mac(key, self.digestmod(), default_backend())
69 - def sign(self, pkt, key):
70 + def sign(self, pkt, key, trailer=None):
72 Sign an IPsec (ESP or AH) packet with this algo.
74 @@ -560,16 +556,20 @@ class AuthAlgo(object):
77 mac.update(raw(pkt[ESP]))
80 pkt[ESP].data += mac.finalize()[:self.icv_size]
82 elif pkt.haslayer(AH):
83 clone = zero_mutable_fields(pkt.copy(), sending=True)
84 mac.update(raw(clone))
87 pkt[AH].icv = mac.finalize()[:self.icv_size]
91 - def verify(self, pkt, key):
92 + def verify(self, pkt, key, trailer):
94 Check that the integrity check value (icv) of a packet is valid.
96 @@ -600,6 +600,8 @@ class AuthAlgo(object):
97 clone = zero_mutable_fields(pkt.copy(), sending=False)
99 mac.update(raw(clone))
101 + mac.update(trailer) # bytearray(4)) #raw(trailer))
102 computed_icv = mac.finalize()[:self.icv_size]
104 # XXX: Cannot use mac.verify because the ICV can be truncated
105 @@ -788,7 +790,7 @@ class SecurityAssociation(object):
106 This class is responsible of "encryption" and "decryption" of IPsec packets. # noqa: E501
109 - SUPPORTED_PROTOS = (IP, IPv6)
110 + SUPPORTED_PROTOS = (IP, IPv6, MPLS)
112 def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
113 auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None, esn_en=False, esn=0): # noqa: E501
114 @@ -862,6 +864,23 @@ class SecurityAssociation(object):
115 raise TypeError('nat_t_header must be %s' % UDP.name)
116 self.nat_t_header = nat_t_header
118 + def build_aead(self, esp):
120 + return (struct.pack('!LLL', esp.spi, self.seq_num >> 32, esp.seq))
122 + return (struct.pack('!LL', esp.spi, esp.seq))
124 + def build_seq_num(self, num):
125 + # only lower order bits are transmitted
126 + # higher order bits are used in the ICV
127 + lower = num & 0xffffffff
131 + return lower, struct.pack("!I", upper)
135 def check_spi(self, pkt):
136 if pkt.spi != self.spi:
137 raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
138 @@ -875,7 +894,8 @@ class SecurityAssociation(object):
139 if len(iv) != self.crypt_algo.iv_size:
140 raise TypeError('iv length must be %s' % self.crypt_algo.iv_size) # noqa: E501
142 - esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
143 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
144 + esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
146 if self.tunnel_header:
147 tunnel = self.tunnel_header.copy()
148 @@ -899,7 +919,7 @@ class SecurityAssociation(object):
149 esn_en=esn_en or self.esn_en,
152 - self.auth_algo.sign(esp, self.auth_key)
153 + self.auth_algo.sign(esp, self.auth_key, high_seq_num)
155 if self.nat_t_header:
156 nat_t_header = self.nat_t_header.copy()
157 @@ -926,7 +946,8 @@ class SecurityAssociation(object):
159 def _encrypt_ah(self, pkt, seq_num=None):
161 - ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
162 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
163 + ah = AH(spi=self.spi, seq=low_seq_num,
164 icv=b"\x00" * self.auth_algo.icv_size)
166 if self.tunnel_header:
167 @@ -966,7 +987,8 @@ class SecurityAssociation(object):
169 ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
171 - signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key) # noqa: E501
172 + signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
173 + self.auth_key, high_seq_num) # noqa: E501
175 # sequence number must always change, unless specified by the user
177 @@ -1003,11 +1025,12 @@ class SecurityAssociation(object):
179 def _decrypt_esp(self, pkt, verify=True, esn_en=None, esn=None):
181 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
186 - self.auth_algo.verify(encrypted, self.auth_key)
187 + self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
189 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
190 self.crypt_algo.icv_size or
191 @@ -1048,9 +1071,10 @@ class SecurityAssociation(object):
193 def _decrypt_ah(self, pkt, verify=True):
195 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
198 - self.auth_algo.verify(pkt, self.auth_key)
199 + self.auth_algo.verify(pkt, self.auth_key, high_seq_num)