ipsec: add support for RFC-4543 ENCR_NULL_AUTH_AES_GMAC
Type: improvement
Change-Id: I830f7a2ea3ac0aff5185698b9fa7a278c45116b0
Signed-off-by: Benoît Ganne <bganne@cisco.com>
diff --git a/test/patches/scapy-2.4.3/ipsec.patch b/test/patches/scapy-2.4.3/ipsec.patch
index 196b78f..bfa27d2 100644
--- a/test/patches/scapy-2.4.3/ipsec.patch
+++ b/test/patches/scapy-2.4.3/ipsec.patch
@@ -1,5 +1,5 @@
diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
-index ae057ee1..55d0dd53 100644
+index ae057ee1..b6806f71 100644
--- a/scapy/layers/ipsec.py
+++ b/scapy/layers/ipsec.py
@@ -56,6 +56,7 @@ from scapy.fields import ByteEnumField, ByteField, IntField, PacketField, \
@@ -10,7 +10,7 @@
import scapy.modules.six as six
from scapy.modules.six.moves import range
from scapy.layers.inet6 import IPv6, IPv6ExtHdrHopByHop, IPv6ExtHdrDestOpt, \
-@@ -359,11 +360,8 @@ class CryptAlgo(object):
+@@ -359,13 +360,17 @@ class CryptAlgo(object):
encryptor = cipher.encryptor()
if self.is_aead:
@@ -18,15 +18,29 @@
- aad = struct.pack('!LLL', esp.spi, esn, esp.seq)
- else:
- aad = struct.pack('!LL', esp.spi, esp.seq)
-- encryptor.authenticate_additional_data(aad)
-+ encryptor.authenticate_additional_data(sa.build_aead(esp))
++ aad = sa.build_aead(esp)
++ if self.name == 'AES-NULL-GMAC':
++ aad = aad + esp.iv + data
++ aes_null_gmac_data = data
++ data = b''
+ encryptor.authenticate_additional_data(aad)
+
data = encryptor.update(data) + encryptor.finalize()
data += encryptor.tag[:self.icv_size]
++ if self.name == 'AES-NULL-GMAC':
++ data = aes_null_gmac_data + data
else:
-@@ -400,12 +398,7 @@ class CryptAlgo(object):
+ data = encryptor.update(data) + encryptor.finalize()
+
+@@ -399,17 +404,19 @@ class CryptAlgo(object):
+ decryptor = cipher.decryptor()
if self.is_aead:
++ aad = sa.build_aead(esp)
++ if self.name == 'AES-NULL-GMAC':
++ aad = aad + iv + data
++ aes_null_gmac_data = data
++ data = b''
# Tag value check is done during the finalize method
- if esn_en:
- decryptor.authenticate_additional_data(
@@ -34,11 +48,17 @@
- else:
- decryptor.authenticate_additional_data(
- struct.pack('!LL', esp.spi, esp.seq))
-+ decryptor.authenticate_additional_data(sa.build_aead(esp))
++ decryptor.authenticate_additional_data(aad)
try:
data = decryptor.update(data) + decryptor.finalize()
except InvalidTag as err:
-@@ -445,6 +438,7 @@ if algorithms:
+ raise IPSecIntegrityError(err)
++ if self.name == 'AES-NULL-GMAC':
++ data = aes_null_gmac_data + data
+
+ # extract padlen and nh
+ padlen = orb(data[-2])
+@@ -445,6 +452,7 @@ if algorithms:
CRYPT_ALGOS['AES-CTR'] = CryptAlgo('AES-CTR',
cipher=algorithms.AES,
mode=modes.CTR,
@@ -46,7 +66,7 @@
iv_size=8,
salt_size=4,
format_mode_iv=_aes_ctr_format_mode_iv)
-@@ -452,6 +446,7 @@ if algorithms:
+@@ -452,14 +460,24 @@ if algorithms:
CRYPT_ALGOS['AES-GCM'] = CryptAlgo('AES-GCM',
cipher=algorithms.AES,
mode=modes.GCM,
@@ -54,7 +74,16 @@
salt_size=4,
iv_size=8,
icv_size=16,
-@@ -460,6 +455,7 @@ if algorithms:
+ format_mode_iv=_salt_format_mode_iv)
++ CRYPT_ALGOS['AES-NULL-GMAC'] = CryptAlgo('AES-NULL-GMAC',
++ cipher=algorithms.AES,
++ mode=modes.GCM,
++ block_size=1,
++ salt_size=4,
++ iv_size=8,
++ icv_size=16,
++ format_mode_iv=_salt_format_mode_iv)
+ if hasattr(modes, 'CCM'):
CRYPT_ALGOS['AES-CCM'] = CryptAlgo('AES-CCM',
cipher=algorithms.AES,
mode=modes.CCM,
@@ -62,7 +91,7 @@
iv_size=8,
salt_size=3,
icv_size=16,
-@@ -544,7 +540,7 @@ class AuthAlgo(object):
+@@ -544,7 +562,7 @@ class AuthAlgo(object):
else:
return self.mac(key, self.digestmod(), default_backend())
@@ -71,7 +100,7 @@
"""
Sign an IPsec (ESP or AH) packet with this algo.
-@@ -560,16 +556,20 @@ class AuthAlgo(object):
+@@ -560,16 +578,20 @@ class AuthAlgo(object):
if pkt.haslayer(ESP):
mac.update(raw(pkt[ESP]))
@@ -93,7 +122,7 @@
"""
Check that the integrity check value (icv) of a packet is valid.
-@@ -600,6 +600,8 @@ class AuthAlgo(object):
+@@ -600,6 +622,8 @@ class AuthAlgo(object):
clone = zero_mutable_fields(pkt.copy(), sending=False)
mac.update(raw(clone))
@@ -102,7 +131,7 @@
computed_icv = mac.finalize()[:self.icv_size]
# XXX: Cannot use mac.verify because the ICV can be truncated
-@@ -788,7 +790,7 @@ class SecurityAssociation(object):
+@@ -788,7 +812,7 @@ class SecurityAssociation(object):
This class is responsible of "encryption" and "decryption" of IPsec packets. # noqa: E501
"""
@@ -111,7 +140,7 @@
def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None, esn_en=False, esn=0): # noqa: E501
-@@ -862,6 +864,23 @@ class SecurityAssociation(object):
+@@ -862,6 +886,23 @@ class SecurityAssociation(object):
raise TypeError('nat_t_header must be %s' % UDP.name)
self.nat_t_header = nat_t_header
@@ -135,7 +164,7 @@
def check_spi(self, pkt):
if pkt.spi != self.spi:
raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
-@@ -875,7 +894,8 @@ class SecurityAssociation(object):
+@@ -875,7 +916,8 @@ class SecurityAssociation(object):
if len(iv) != self.crypt_algo.iv_size:
raise TypeError('iv length must be %s' % self.crypt_algo.iv_size) # noqa: E501
@@ -145,7 +174,7 @@
if self.tunnel_header:
tunnel = self.tunnel_header.copy()
-@@ -899,7 +919,7 @@ class SecurityAssociation(object):
+@@ -899,7 +941,7 @@ class SecurityAssociation(object):
esn_en=esn_en or self.esn_en,
esn=esn or self.esn)
@@ -154,7 +183,7 @@
if self.nat_t_header:
nat_t_header = self.nat_t_header.copy()
-@@ -926,7 +946,8 @@ class SecurityAssociation(object):
+@@ -926,7 +968,8 @@ class SecurityAssociation(object):
def _encrypt_ah(self, pkt, seq_num=None):
@@ -164,7 +193,7 @@
icv=b"\x00" * self.auth_algo.icv_size)
if self.tunnel_header:
-@@ -966,7 +987,8 @@ class SecurityAssociation(object):
+@@ -966,7 +1009,8 @@ class SecurityAssociation(object):
else:
ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
@@ -174,7 +203,7 @@
# sequence number must always change, unless specified by the user
if seq_num is None:
-@@ -1003,11 +1025,12 @@ class SecurityAssociation(object):
+@@ -1003,11 +1047,12 @@ class SecurityAssociation(object):
def _decrypt_esp(self, pkt, verify=True, esn_en=None, esn=None):
@@ -188,7 +217,7 @@
esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
self.crypt_algo.icv_size or
-@@ -1048,9 +1071,10 @@ class SecurityAssociation(object):
+@@ -1048,9 +1093,10 @@ class SecurityAssociation(object):
def _decrypt_ah(self, pkt, verify=True):