blob: 7eb0123a2115bafa8fcd441408dd4830eb44dede [file] [log] [blame]
Neale Ranns3833ffd2019-03-21 14:34:09 +00001diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -05002index 69e7ae3b..d9c1705b 100644
Neale Ranns3833ffd2019-03-21 14:34:09 +00003--- a/scapy/layers/ipsec.py
4+++ b/scapy/layers/ipsec.py
Neale Ranns6afaae12019-07-17 15:07:14 +00005@@ -344,8 +344,7 @@ class CryptAlgo(object):
Neale Ranns47feb112019-04-11 15:14:07 +00006 encryptor = cipher.encryptor()
7
8 if self.is_aead:
9- aad = struct.pack('!LL', esp.spi, esp.seq)
Neale Ranns6afaae12019-07-17 15:07:14 +000010- encryptor.authenticate_additional_data(aad)
11+ encryptor.authenticate_additional_data(sa.build_aead(esp))
Neale Ranns47feb112019-04-11 15:14:07 +000012 data = encryptor.update(data) + encryptor.finalize()
13 data += encryptor.tag[:self.icv_size]
Neale Ranns6afaae12019-07-17 15:07:14 +000014 else:
15@@ -380,10 +379,7 @@ class CryptAlgo(object):
16
Neale Ranns47feb112019-04-11 15:14:07 +000017 if self.is_aead:
18 # Tag value check is done during the finalize method
Neale Ranns6afaae12019-07-17 15:07:14 +000019- decryptor.authenticate_additional_data(
Neale Ranns47feb112019-04-11 15:14:07 +000020- struct.pack('!LL', esp.spi, esp.seq)
Neale Ranns6afaae12019-07-17 15:07:14 +000021- )
Neale Ranns47feb112019-04-11 15:14:07 +000022-
Neale Ranns6afaae12019-07-17 15:07:14 +000023+ decryptor.authenticate_additional_data(sa.build_aead(esp))
Neale Ranns47feb112019-04-11 15:14:07 +000024 try:
25 data = decryptor.update(data) + decryptor.finalize()
26 except InvalidTag as err:
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -050027@@ -422,6 +418,7 @@ if algorithms:
28 CRYPT_ALGOS['AES-CTR'] = CryptAlgo('AES-CTR',
29 cipher=algorithms.AES,
30 mode=modes.CTR,
31+ block_size=1,
32 iv_size=8,
33 salt_size=4,
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,
38 mode=modes.GCM,
39+ block_size=1,
40 salt_size=4,
41 iv_size=8,
42 icv_size=16,
43@@ -437,6 +435,7 @@ if algorithms:
44 CRYPT_ALGOS['AES-CCM'] = CryptAlgo('AES-CCM',
45 cipher=algorithms.AES,
46 mode=modes.CCM,
47+ block_size=1,
48 iv_size=8,
49 salt_size=3,
50 icv_size=16,
51@@ -518,12 +517,16 @@ class AuthAlgo(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +000052 else:
53 return self.mac(key, self.digestmod(), default_backend())
54
55- def sign(self, pkt, key):
56+ def sign(self, pkt, key, trailer=None):
57 """
58 Sign an IPsec (ESP or AH) packet with this algo.
59
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
65+ sequence number.
66
67 @return: the signed packet
68 """
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -050069@@ -534,16 +537,20 @@ class AuthAlgo(object):
Neale Ranns49e7ef62019-04-10 17:24:29 +000070
71 if pkt.haslayer(ESP):
72 mac.update(raw(pkt[ESP]))
73+ if trailer:
74+ mac.update(trailer)
75 pkt[ESP].data += mac.finalize()[:self.icv_size]
76
Neale Ranns3833ffd2019-03-21 14:34:09 +000077 elif pkt.haslayer(AH):
78 clone = zero_mutable_fields(pkt.copy(), sending=True)
79 mac.update(raw(clone))
80+ if trailer:
81+ mac.update(trailer)
82 pkt[AH].icv = mac.finalize()[:self.icv_size]
83
84 return pkt
85
86- def verify(self, pkt, key):
87+ def verify(self, pkt, key, trailer):
88 """
89 Check that the integrity check value (icv) of a packet is valid.
90
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -050091@@ -574,6 +581,8 @@ class AuthAlgo(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +000092 clone = zero_mutable_fields(pkt.copy(), sending=False)
93
94 mac.update(raw(clone))
95+ if trailer:
96+ mac.update(trailer) # bytearray(4)) #raw(trailer))
97 computed_icv = mac.finalize()[:self.icv_size]
98
99 # XXX: Cannot use mac.verify because the ICV can be truncated
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500100@@ -757,7 +766,8 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000101 SUPPORTED_PROTOS = (IP, IPv6)
102
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,
106+ use_esn=False):
107 """
108 @param proto: the IPsec proto to use (ESP or AH)
109 @param spi: the Security Parameters Index of this SA
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500110@@ -771,6 +781,7 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000111 to encapsulate the encrypted packets.
112 @param nat_t_header: an instance of a UDP header that will be used
113 for NAT-Traversal.
114+ @param use_esn: Use Extended Sequence Numbers
115 """
116
117 if proto not in (ESP, AH, ESP.name, AH.name):
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500118@@ -782,6 +793,7 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000119
120 self.spi = spi
121 self.seq_num = seq_num
122+ self.use_esn = use_esn
123
124 if crypt_algo:
125 if crypt_algo not in CRYPT_ALGOS:
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500126@@ -827,6 +839,23 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000127 raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
128 (pkt.spi, self.spi))
129
Neale Ranns6afaae12019-07-17 15:07:14 +0000130+ def build_aead(self, esp):
131+ if self.use_esn:
132+ return (struct.pack('!LLL', esp.spi, self.seq_num >> 32, esp.seq))
133+ else:
134+ return (struct.pack('!LL', esp.spi, esp.seq))
135+
Neale Ranns3833ffd2019-03-21 14:34:09 +0000136+ 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
140+ upper = num >> 32
141+
142+ if self.use_esn:
Neale Ranns47feb112019-04-11 15:14:07 +0000143+ return lower, struct.pack("!I", upper)
Neale Ranns3833ffd2019-03-21 14:34:09 +0000144+ else:
145+ return lower, None
146+
147 def _encrypt_esp(self, pkt, seq_num=None, iv=None):
148
149 if iv is None:
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500150@@ -835,7 +864,8 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000151 if len(iv) != self.crypt_algo.iv_size:
152 raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
153
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)
157
158 if self.tunnel_header:
159 tunnel = self.tunnel_header.copy()
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500160@@ -857,7 +887,7 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000161 esp = self.crypt_algo.pad(esp)
162 esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
163
164- self.auth_algo.sign(esp, self.auth_key)
165+ self.auth_algo.sign(esp, self.auth_key, high_seq_num)
166
167 if self.nat_t_header:
168 nat_t_header = self.nat_t_header.copy()
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500169@@ -884,7 +914,8 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000170
171 def _encrypt_ah(self, pkt, seq_num=None):
172
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)
177
178 if self.tunnel_header:
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500179@@ -924,7 +955,8 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000180 else:
181 ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
182
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)
186
187 # sequence number must always change, unless specified by the user
188 if seq_num is None:
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500189@@ -955,11 +987,12 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000190
191 def _decrypt_esp(self, pkt, verify=True):
192
193+ low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
194 encrypted = pkt[ESP]
195
196 if verify:
197 self.check_spi(pkt)
198- self.auth_algo.verify(encrypted, self.auth_key)
199+ self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
200
201 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
202 self.crypt_algo.icv_size or
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500203@@ -998,9 +1031,11 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000204
205 def _decrypt_ah(self, pkt, verify=True):
206
207+ low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
208+
209 if verify:
210 self.check_spi(pkt)
211- self.auth_algo.verify(pkt, self.auth_key)
212+ self.auth_algo.verify(pkt, self.auth_key, high_seq_num)
213
214 ah = pkt[AH]
215 payload = ah.payload