diff options
author | Neale Ranns <nranns@cisco.com> | 2019-04-11 15:14:07 +0000 |
---|---|---|
committer | Neale Ranns <nranns@cisco.com> | 2019-04-17 00:12:05 +0000 |
commit | 32b13bba2e3013a1b2a67eca8fe6a177d8e927ed (patch) | |
tree | 97dbae11a361e7537be09329c80ae7c3a3acb799 /test | |
parent | 07a4d675c8d1b1bc19fa1846369f610ab91a9aba (diff) |
IPSEC: support GCM in ESP
Change-Id: Id2ddb77b4ec3dd543d6e638bc882923f2bac011d
Signed-off-by: Neale Ranns <nranns@cisco.com>
(cherry picked from commit 47feb1146ec3b0e1cf2ebd83cd5211e1df261194)
Diffstat (limited to 'test')
-rw-r--r-- | test/patches/scapy-2.4/ipsec.patch | 52 | ||||
-rw-r--r-- | test/template_ipsec.py | 10 | ||||
-rw-r--r-- | test/test_ipsec_esp.py | 60 | ||||
-rw-r--r-- | test/test_ipsec_tun_if_esp.py | 127 |
4 files changed, 216 insertions, 33 deletions
diff --git a/test/patches/scapy-2.4/ipsec.patch b/test/patches/scapy-2.4/ipsec.patch index b19112db2b7..731fe3ccc25 100644 --- a/test/patches/scapy-2.4/ipsec.patch +++ b/test/patches/scapy-2.4/ipsec.patch @@ -1,8 +1,30 @@ diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py -index 69e7ae3b..b35da2b1 100644 +index 69e7ae3b..0c69ba1a 100644 --- a/scapy/layers/ipsec.py +++ b/scapy/layers/ipsec.py -@@ -518,12 +518,16 @@ class AuthAlgo(object): +@@ -344,7 +344,8 @@ 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) + data = encryptor.update(data) + encryptor.finalize() + data += encryptor.tag[:self.icv_size] +@@ -381,9 +382,9 @@ class CryptAlgo(object): + if self.is_aead: + # Tag value check is done during the finalize method + decryptor.authenticate_additional_data( +- struct.pack('!LL', esp.spi, esp.seq) ++ struct.pack('!LQ' if sa.use_esn else '!LL', ++ esp.spi, esp.seq) + ) +- + try: + data = decryptor.update(data) + decryptor.finalize() + except InvalidTag as err: +@@ -518,12 +519,16 @@ class AuthAlgo(object): else: return self.mac(key, self.digestmod(), default_backend()) @@ -20,7 +42,7 @@ index 69e7ae3b..b35da2b1 100644 @return: the signed packet """ -@@ -534,16 +538,20 @@ class AuthAlgo(object): +@@ -534,16 +539,20 @@ class AuthAlgo(object): if pkt.haslayer(ESP): mac.update(raw(pkt[ESP])) @@ -42,7 +64,7 @@ index 69e7ae3b..b35da2b1 100644 """ Check that the integrity check value (icv) of a packet is valid. -@@ -574,6 +582,8 @@ class AuthAlgo(object): +@@ -574,6 +583,8 @@ class AuthAlgo(object): clone = zero_mutable_fields(pkt.copy(), sending=False) mac.update(raw(clone)) @@ -51,7 +73,7 @@ index 69e7ae3b..b35da2b1 100644 computed_icv = mac.finalize()[:self.icv_size] # XXX: Cannot use mac.verify because the ICV can be truncated -@@ -757,7 +767,8 @@ class SecurityAssociation(object): +@@ -757,7 +768,8 @@ class SecurityAssociation(object): SUPPORTED_PROTOS = (IP, IPv6) def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None, @@ -61,7 +83,7 @@ index 69e7ae3b..b35da2b1 100644 """ @param proto: the IPsec proto to use (ESP or AH) @param spi: the Security Parameters Index of this SA -@@ -771,6 +782,7 @@ class SecurityAssociation(object): +@@ -771,6 +783,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. @@ -69,7 +91,7 @@ index 69e7ae3b..b35da2b1 100644 """ if proto not in (ESP, AH, ESP.name, AH.name): -@@ -782,6 +794,7 @@ class SecurityAssociation(object): +@@ -782,6 +795,7 @@ class SecurityAssociation(object): self.spi = spi self.seq_num = seq_num @@ -77,7 +99,7 @@ index 69e7ae3b..b35da2b1 100644 if crypt_algo: if crypt_algo not in CRYPT_ALGOS: -@@ -827,6 +840,17 @@ class SecurityAssociation(object): +@@ -827,6 +841,17 @@ class SecurityAssociation(object): raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' % (pkt.spi, self.spi)) @@ -88,14 +110,14 @@ index 69e7ae3b..b35da2b1 100644 + upper = num >> 32 + + if self.use_esn: -+ return lower, struct.pack(">I", upper) ++ return lower, struct.pack("!I", upper) + else: + return lower, None + def _encrypt_esp(self, pkt, seq_num=None, iv=None): if iv is None: -@@ -835,7 +859,8 @@ class SecurityAssociation(object): +@@ -835,7 +860,8 @@ class SecurityAssociation(object): if len(iv) != self.crypt_algo.iv_size: raise TypeError('iv length must be %s' % self.crypt_algo.iv_size) @@ -105,7 +127,7 @@ index 69e7ae3b..b35da2b1 100644 if self.tunnel_header: tunnel = self.tunnel_header.copy() -@@ -857,7 +882,7 @@ class SecurityAssociation(object): +@@ -857,7 +883,7 @@ class SecurityAssociation(object): esp = self.crypt_algo.pad(esp) esp = self.crypt_algo.encrypt(self, esp, self.crypt_key) @@ -114,7 +136,7 @@ index 69e7ae3b..b35da2b1 100644 if self.nat_t_header: nat_t_header = self.nat_t_header.copy() -@@ -884,7 +909,8 @@ class SecurityAssociation(object): +@@ -884,7 +910,8 @@ class SecurityAssociation(object): def _encrypt_ah(self, pkt, seq_num=None): @@ -124,7 +146,7 @@ index 69e7ae3b..b35da2b1 100644 icv = b"\x00" * self.auth_algo.icv_size) if self.tunnel_header: -@@ -924,7 +950,8 @@ class SecurityAssociation(object): +@@ -924,7 +951,8 @@ class SecurityAssociation(object): else: ip_header.plen = len(ip_header.payload) + len(ah) + len(payload) @@ -134,7 +156,7 @@ index 69e7ae3b..b35da2b1 100644 # sequence number must always change, unless specified by the user if seq_num is None: -@@ -955,11 +982,12 @@ class SecurityAssociation(object): +@@ -955,11 +983,12 @@ class SecurityAssociation(object): def _decrypt_esp(self, pkt, verify=True): @@ -148,7 +170,7 @@ index 69e7ae3b..b35da2b1 100644 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key, self.crypt_algo.icv_size or -@@ -998,9 +1026,11 @@ class SecurityAssociation(object): +@@ -998,9 +1027,11 @@ class SecurityAssociation(object): def _decrypt_ah(self, pkt, verify=True): diff --git a/test/template_ipsec.py b/test/template_ipsec.py index de779648645..efe49f1a01f 100644 --- a/test/template_ipsec.py +++ b/test/template_ipsec.py @@ -42,6 +42,7 @@ class IPsecIPv4Params(object): IPSEC_API_CRYPTO_ALG_AES_CBC_128) self.crypt_algo = 'AES-CBC' # scapy name self.crypt_key = 'JPjyOWBeVEQiMe7h' + self.crypt_salt = '' self.flags = 0 self.nat_header = None @@ -77,6 +78,7 @@ class IPsecIPv6Params(object): IPSEC_API_CRYPTO_ALG_AES_CBC_128) self.crypt_algo = 'AES-CBC' # scapy name self.crypt_key = 'JPjyOWBeVEQiMe7h' + self.crypt_salt = '' self.flags = 0 self.nat_header = None @@ -87,7 +89,7 @@ def config_tun_params(p, encryption_type, tun_if): IPSEC_API_SAD_FLAG_USE_ESN)) p.scapy_tun_sa = SecurityAssociation( encryption_type, spi=p.vpp_tun_spi, - crypt_algo=p.crypt_algo, crypt_key=p.crypt_key, + crypt_algo=p.crypt_algo, crypt_key=p.crypt_key + p.crypt_salt, auth_algo=p.auth_algo, auth_key=p.auth_key, tunnel_header=ip_class_by_addr_type[p.addr_type]( src=tun_if.remote_addr[p.addr_type], @@ -96,7 +98,7 @@ def config_tun_params(p, encryption_type, tun_if): use_esn=use_esn) p.vpp_tun_sa = SecurityAssociation( encryption_type, spi=p.scapy_tun_spi, - crypt_algo=p.crypt_algo, crypt_key=p.crypt_key, + crypt_algo=p.crypt_algo, crypt_key=p.crypt_key + p.crypt_salt, auth_algo=p.auth_algo, auth_key=p.auth_key, tunnel_header=ip_class_by_addr_type[p.addr_type]( dst=tun_if.remote_addr[p.addr_type], @@ -112,7 +114,7 @@ def config_tra_params(p, encryption_type): encryption_type, spi=p.vpp_tra_spi, crypt_algo=p.crypt_algo, - crypt_key=p.crypt_key, + crypt_key=p.crypt_key + p.crypt_salt, auth_algo=p.auth_algo, auth_key=p.auth_key, nat_t_header=p.nat_header, @@ -121,7 +123,7 @@ def config_tra_params(p, encryption_type): encryption_type, spi=p.scapy_tra_spi, crypt_algo=p.crypt_algo, - crypt_key=p.crypt_key, + crypt_key=p.crypt_key + p.crypt_salt, auth_algo=p.auth_algo, auth_key=p.auth_key, nat_t_header=p.nat_header, diff --git a/test/test_ipsec_esp.py b/test/test_ipsec_esp.py index 34c09c78a24..3f2e8a21f3b 100644 --- a/test/test_ipsec_esp.py +++ b/test/test_ipsec_esp.py @@ -1,5 +1,6 @@ import socket import unittest +import struct from scapy.layers.ipsec import ESP from scapy.layers.inet import UDP @@ -356,23 +357,51 @@ class TestIpsecEspAll(ConfigIpsecESP, super(TestIpsecEspAll, self).tearDown() def test_crypto_algs(self): - """All engines AES-CBC-[128, 192, 256] w/ & w/o ESN""" + """All engines AES-[CBC, GCM]-[128, 192, 256] w/ & w/o ESN""" # foreach VPP crypto engine engines = ["ia32", "ipsecmb", "openssl"] # foreach crypto algorithm - algos = [{'vpp': VppEnum.vl_api_ipsec_crypto_alg_t. - IPSEC_API_CRYPTO_ALG_AES_CBC_128, - 'scapy': "AES-CBC", + algos = [{'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_GCM_128), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_NONE), + 'scapy-crypto': "AES-GCM", + 'scapy-integ': "NULL", + 'key': "JPjyOWBeVEQiMe7h", + 'salt': struct.pack("!L", 0)}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_GCM_256), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_NONE), + 'scapy-crypto': "AES-GCM", + 'scapy-integ': "NULL", + 'key': "JPjyOWBeVEQiMe7h0123456787654321", + 'salt': struct.pack("!L", 0)}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_CBC_128), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_SHA1_96), + 'scapy-crypto': "AES-CBC", + 'scapy-integ': "HMAC-SHA1-96", + 'salt': '', 'key': "JPjyOWBeVEQiMe7h"}, - {'vpp': VppEnum.vl_api_ipsec_crypto_alg_t. - IPSEC_API_CRYPTO_ALG_AES_CBC_192, - 'scapy': "AES-CBC", + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_CBC_192), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_SHA1_96), + 'scapy-crypto': "AES-CBC", + 'scapy-integ': "HMAC-SHA1-96", + 'salt': '', 'key': "JPjyOWBeVEQiMe7hJPjyOWBe"}, - {'vpp': VppEnum.vl_api_ipsec_crypto_alg_t. - IPSEC_API_CRYPTO_ALG_AES_CBC_256, - 'scapy': "AES-CBC", + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_CBC_256), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_SHA1_96), + 'scapy-crypto': "AES-CBC", + 'scapy-integ': "HMAC-SHA1-96", + 'salt': '', 'key': "JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h"}] # with and without ESN @@ -403,9 +432,12 @@ class TestIpsecEspAll(ConfigIpsecESP, self.ipv6_params} for _, p in self.params.items(): - p.crypt_algo_vpp_id = algo['vpp'] - p.crypt_algo = algo['scapy'] + p.auth_algo_vpp_id = algo['vpp-integ'] + p.crypt_algo_vpp_id = algo['vpp-crypto'] + p.crypt_algo = algo['scapy-crypto'] + p.auth_algo = algo['scapy-integ'] p.crypt_key = algo['key'] + p.crypt_salt = algo['salt'] p.flags = p.flags | flag # @@ -420,8 +452,8 @@ class TestIpsecEspAll(ConfigIpsecESP, # self.verify_tra_basic6(count=17) self.verify_tra_basic4(count=17) - self.verify_tun_66(self.params[socket.AF_INET6], 1) - self.verify_tun_44(self.params[socket.AF_INET], 1) + self.verify_tun_66(self.params[socket.AF_INET6], 17) + self.verify_tun_44(self.params[socket.AF_INET], 17) # # remove the SPDs, SAs, etc diff --git a/test/test_ipsec_tun_if_esp.py b/test/test_ipsec_tun_if_esp.py index 9c5a15377e6..833bbd47bb3 100644 --- a/test/test_ipsec_tun_if_esp.py +++ b/test/test_ipsec_tun_if_esp.py @@ -1,6 +1,8 @@ import unittest import socket import copy +import struct + from scapy.layers.ipsec import ESP from scapy.layers.l2 import Ether, Raw, GRE from scapy.layers.inet import IP, UDP @@ -13,6 +15,7 @@ from vpp_ip_route import VppIpRoute, VppRoutePath, DpoProto from vpp_ipsec import VppIpsecSA from vpp_l2 import VppBridgeDomain, VppBridgeDomainPort from util import ppp +from vpp_papi import VppEnum class TemplateIpsec4TunIfEsp(TemplateIpsec): @@ -200,6 +203,130 @@ class TestIpsec4MultiTunIfEsp(TemplateIpsec, IpsecTun4): self.assertEqual(c['packets'], 127) +class TestIpsec4TunIfEspAll(TemplateIpsec, IpsecTun4): + """ IPsec IPv4 Tunnel interface all Algos """ + + encryption_type = ESP + tun4_encrypt_node_name = "esp4-encrypt" + tun4_decrypt_node_name = "esp4-decrypt" + + def config_network(self, p): + config_tun_params(p, self.encryption_type, self.tun_if) + + p.tun_if = VppIpsecTunInterface(self, self.pg0, p.vpp_tun_spi, + p.scapy_tun_spi, + p.crypt_algo_vpp_id, + p.crypt_key, p.crypt_key, + p.auth_algo_vpp_id, p.auth_key, + p.auth_key) + p.tun_if.add_vpp_config() + p.tun_if.admin_up() + p.tun_if.config_ip4() + self.logger.info(self.vapi.cli("sh ipsec sa 0")) + self.logger.info(self.vapi.cli("sh ipsec sa 1")) + + p.route = VppIpRoute(self, p.remote_tun_if_host, 32, + [VppRoutePath(p.tun_if.remote_ip4, + 0xffffffff)]) + p.route.add_vpp_config() + + def unconfig_network(self, p): + p.tun_if.unconfig_ip4() + p.tun_if.remove_vpp_config() + p.route.remove_vpp_config() + + def setUp(self): + super(TestIpsec4TunIfEspAll, self).setUp() + + self.tun_if = self.pg0 + + def tearDown(self): + super(TestIpsec4TunIfEspAll, self).tearDown() + + def test_tun_44(self): + """IPSEC tunnel all algos """ + + # foreach VPP crypto engine + engines = ["ia32", "ipsecmb", "openssl"] + + # foreach crypto algorithm + algos = [{'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_GCM_128), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_NONE), + 'scapy-crypto': "AES-GCM", + 'scapy-integ': "NULL", + 'key': "JPjyOWBeVEQiMe7h", + 'salt': struct.pack("!L", 0)}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_GCM_192), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_NONE), + 'scapy-crypto': "AES-GCM", + 'scapy-integ': "NULL", + 'key': "JPjyOWBeVEQiMe7hJPjyOWBe", + 'salt': struct.pack("!L", 0)}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_GCM_256), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_NONE), + 'scapy-crypto': "AES-GCM", + 'scapy-integ': "NULL", + 'key': "JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h", + 'salt': struct.pack("!L", 0)}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_CBC_128), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_SHA1_96), + 'scapy-crypto': "AES-CBC", + 'scapy-integ': "HMAC-SHA1-96", + 'salt': '', + 'key': "JPjyOWBeVEQiMe7h"}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_CBC_192), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_SHA1_96), + 'scapy-crypto': "AES-CBC", + 'scapy-integ': "HMAC-SHA1-96", + 'salt': '', + 'key': "JPjyOWBeVEQiMe7hJPjyOWBe"}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_CBC_256), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_SHA1_96), + 'scapy-crypto': "AES-CBC", + 'scapy-integ': "HMAC-SHA1-96", + 'salt': '', + 'key': "JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h"}] + + for engine in engines: + self.vapi.cli("set crypto handler all %s" % engine) + + # + # loop through each of the algorithms + # + for algo in algos: + # with self.subTest(algo=algo['scapy']): + + p = copy.copy(self.ipv4_params) + p.auth_algo_vpp_id = algo['vpp-integ'] + p.crypt_algo_vpp_id = algo['vpp-crypto'] + p.crypt_algo = algo['scapy-crypto'] + p.auth_algo = algo['scapy-integ'] + p.crypt_key = algo['key'] + p.crypt_salt = algo['salt'] + + self.config_network(p) + + self.verify_tun_44(p, count=127) + c = p.tun_if.get_rx_stats() + self.assertEqual(c['packets'], 127) + c = p.tun_if.get_tx_stats() + self.assertEqual(c['packets'], 127) + + self.unconfig_network(p) + + class TestIpsec6MultiTunIfEsp(TemplateIpsec, IpsecTun6): """ IPsec IPv6 Multi Tunnel interface """ |