From 6afaae156a9ab9de79474367d8873407f3b12a71 Mon Sep 17 00:00:00 2001 From: Neale Ranns Date: Wed, 17 Jul 2019 15:07:14 +0000 Subject: ipsec: GCM, Anti-replay and ESN fixess Type: fix Several Fixes: 1 - Anti-replay did not work with GCM becuase it overwrote the sequence number in the ESP header. To fix i added the seq num to the per-packet data so it is preserved 2 - The high sequence number was not byte swapped during ESP encrypt. 3 - openssl engine was the only one to return FAIL_DECRYPT for bad GCM the others return BAD_HMAC. removed the former 4 - improved tracing to show the low and high seq numbers 5 - documented the anti-replay window checks 6 - fixed scapy patch for ESN support for GCM 7 - tests for anti-reply (w/ and w/o ESN) for each crypto algo Change-Id: Id65d96b6d1d4dd821b2ab557e87468fff6d70e5b Signed-off-by: Neale Ranns --- test/patches/scapy-2.4/ipsec.patch | 52 +++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 23 deletions(-) (limited to 'test/patches') diff --git a/test/patches/scapy-2.4/ipsec.patch b/test/patches/scapy-2.4/ipsec.patch index 731fe3ccc25..083b0b94961 100644 --- a/test/patches/scapy-2.4/ipsec.patch +++ b/test/patches/scapy-2.4/ipsec.patch @@ -1,30 +1,30 @@ diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py -index 69e7ae3b..0c69ba1a 100644 +index 69e7ae3b..3a1724b2 100644 --- a/scapy/layers/ipsec.py +++ b/scapy/layers/ipsec.py -@@ -344,7 +344,8 @@ class CryptAlgo(object): +@@ -344,8 +344,7 @@ class CryptAlgo(object): encryptor = cipher.encryptor() if self.is_aead: - aad = struct.pack('!LL', esp.spi, esp.seq) -+ aad = struct.pack('!LQ' if sa.use_esn else '!LL', -+ esp.spi, esp.seq) - encryptor.authenticate_additional_data(aad) +- encryptor.authenticate_additional_data(aad) ++ encryptor.authenticate_additional_data(sa.build_aead(esp)) data = encryptor.update(data) + encryptor.finalize() data += encryptor.tag[:self.icv_size] -@@ -381,9 +382,9 @@ class CryptAlgo(object): + else: +@@ -380,10 +379,7 @@ class CryptAlgo(object): + if self.is_aead: # Tag value check is done during the finalize method - decryptor.authenticate_additional_data( +- decryptor.authenticate_additional_data( - struct.pack('!LL', esp.spi, esp.seq) -+ struct.pack('!LQ' if sa.use_esn else '!LL', -+ esp.spi, esp.seq) - ) +- ) - ++ decryptor.authenticate_additional_data(sa.build_aead(esp)) try: data = decryptor.update(data) + decryptor.finalize() except InvalidTag as err: -@@ -518,12 +519,16 @@ class AuthAlgo(object): +@@ -518,12 +514,16 @@ class AuthAlgo(object): else: return self.mac(key, self.digestmod(), default_backend()) @@ -42,7 +42,7 @@ index 69e7ae3b..0c69ba1a 100644 @return: the signed packet """ -@@ -534,16 +539,20 @@ class AuthAlgo(object): +@@ -534,16 +534,20 @@ class AuthAlgo(object): if pkt.haslayer(ESP): mac.update(raw(pkt[ESP])) @@ -64,7 +64,7 @@ index 69e7ae3b..0c69ba1a 100644 """ Check that the integrity check value (icv) of a packet is valid. -@@ -574,6 +583,8 @@ class AuthAlgo(object): +@@ -574,6 +578,8 @@ class AuthAlgo(object): clone = zero_mutable_fields(pkt.copy(), sending=False) mac.update(raw(clone)) @@ -73,7 +73,7 @@ index 69e7ae3b..0c69ba1a 100644 computed_icv = mac.finalize()[:self.icv_size] # XXX: Cannot use mac.verify because the ICV can be truncated -@@ -757,7 +768,8 @@ class SecurityAssociation(object): +@@ -757,7 +763,8 @@ class SecurityAssociation(object): SUPPORTED_PROTOS = (IP, IPv6) def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None, @@ -83,7 +83,7 @@ index 69e7ae3b..0c69ba1a 100644 """ @param proto: the IPsec proto to use (ESP or AH) @param spi: the Security Parameters Index of this SA -@@ -771,6 +783,7 @@ class SecurityAssociation(object): +@@ -771,6 +778,7 @@ class SecurityAssociation(object): to encapsulate the encrypted packets. @param nat_t_header: an instance of a UDP header that will be used for NAT-Traversal. @@ -91,7 +91,7 @@ index 69e7ae3b..0c69ba1a 100644 """ if proto not in (ESP, AH, ESP.name, AH.name): -@@ -782,6 +795,7 @@ class SecurityAssociation(object): +@@ -782,6 +790,7 @@ class SecurityAssociation(object): self.spi = spi self.seq_num = seq_num @@ -99,10 +99,16 @@ index 69e7ae3b..0c69ba1a 100644 if crypt_algo: if crypt_algo not in CRYPT_ALGOS: -@@ -827,6 +841,17 @@ class SecurityAssociation(object): +@@ -827,6 +836,23 @@ class SecurityAssociation(object): raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' % (pkt.spi, self.spi)) ++ def build_aead(self, esp): ++ if self.use_esn: ++ return (struct.pack('!LLL', esp.spi, self.seq_num >> 32, esp.seq)) ++ else: ++ return (struct.pack('!LL', esp.spi, esp.seq)) ++ + def build_seq_num(self, num): + # only lower order bits are transmitted + # higher order bits are used in the ICV @@ -117,7 +123,7 @@ index 69e7ae3b..0c69ba1a 100644 def _encrypt_esp(self, pkt, seq_num=None, iv=None): if iv is None: -@@ -835,7 +860,8 @@ class SecurityAssociation(object): +@@ -835,7 +861,8 @@ class SecurityAssociation(object): if len(iv) != self.crypt_algo.iv_size: raise TypeError('iv length must be %s' % self.crypt_algo.iv_size) @@ -127,7 +133,7 @@ index 69e7ae3b..0c69ba1a 100644 if self.tunnel_header: tunnel = self.tunnel_header.copy() -@@ -857,7 +883,7 @@ class SecurityAssociation(object): +@@ -857,7 +884,7 @@ class SecurityAssociation(object): esp = self.crypt_algo.pad(esp) esp = self.crypt_algo.encrypt(self, esp, self.crypt_key) @@ -136,7 +142,7 @@ index 69e7ae3b..0c69ba1a 100644 if self.nat_t_header: nat_t_header = self.nat_t_header.copy() -@@ -884,7 +910,8 @@ class SecurityAssociation(object): +@@ -884,7 +911,8 @@ class SecurityAssociation(object): def _encrypt_ah(self, pkt, seq_num=None): @@ -146,7 +152,7 @@ index 69e7ae3b..0c69ba1a 100644 icv = b"\x00" * self.auth_algo.icv_size) if self.tunnel_header: -@@ -924,7 +951,8 @@ class SecurityAssociation(object): +@@ -924,7 +952,8 @@ class SecurityAssociation(object): else: ip_header.plen = len(ip_header.payload) + len(ah) + len(payload) @@ -156,7 +162,7 @@ index 69e7ae3b..0c69ba1a 100644 # sequence number must always change, unless specified by the user if seq_num is None: -@@ -955,11 +983,12 @@ class SecurityAssociation(object): +@@ -955,11 +984,12 @@ class SecurityAssociation(object): def _decrypt_esp(self, pkt, verify=True): @@ -170,7 +176,7 @@ index 69e7ae3b..0c69ba1a 100644 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key, self.crypt_algo.icv_size or -@@ -998,9 +1027,11 @@ class SecurityAssociation(object): +@@ -998,9 +1028,11 @@ class SecurityAssociation(object): def _decrypt_ah(self, pkt, verify=True): -- cgit 1.2.3-korg