IPSEC: ESP with ESN tests and fixes 99/18799/3
authorNeale Ranns <nranns@cisco.com>
Wed, 10 Apr 2019 17:24:29 +0000 (17:24 +0000)
committerDamjan Marion <dmarion@me.com>
Thu, 11 Apr 2019 19:42:34 +0000 (19:42 +0000)
Change-Id: Ie42b26e6d5cdb7b23f370ea2933c65079e8d1089
Signed-off-by: Neale Ranns <nranns@cisco.com>
src/vnet/ipsec/esp_decrypt.c
src/vnet/ipsec/esp_encrypt.c
test/patches/scapy-2.4/ipsec.patch
test/test_ipsec_esp.py

index fc4a99a..1386f4c 100644 (file)
@@ -177,7 +177,7 @@ esp_decrypt_inline (vlib_main_t * vm,
       payload = b[0]->data + pd->current_data;
 
       /* we need 4 extra bytes for HMAC calculation when ESN are used */
-      if ((sa0->flags & IPSEC_SA_FLAG_USE_ESN) && pd->icv_sz &&
+      if (ipsec_sa_is_set_USE_ESN (sa0) && pd->icv_sz &&
          (pd->current_data + pd->current_length + 4 > buffer_data_size))
        {
          b[0]->error = node->errors[ESP_DECRYPT_ERROR_NO_TAIL_SPACE];
@@ -211,7 +211,7 @@ esp_decrypt_inline (vlib_main_t * vm,
          op->digest = payload + len;
          op->digest_len = cpd.icv_sz;
          op->len = len;
-         if (PREDICT_TRUE (sa0->flags & IPSEC_SA_FLAG_USE_ESN))
+         if (ipsec_sa_is_set_USE_ESN (sa0))
            {
              /* shift ICV for 4 bytes to insert ESN */
              u8 tmp[ESP_MAX_ICV_SIZE], sz = sizeof (sa0->seq_hi);
@@ -219,7 +219,7 @@ esp_decrypt_inline (vlib_main_t * vm,
              clib_memcpy_fast (payload + len, &sa0->seq_hi, sz);
              clib_memcpy_fast (payload + len + sz, tmp, ESP_MAX_ICV_SIZE);
              op->len += sz;
-             op->dst += sz;
+             op->digest += sz;
            }
        }
 
index bb1effd..fbc5166 100644 (file)
@@ -453,7 +453,7 @@ esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
          if (ipsec_sa_is_set_USE_ESN (sa0))
            {
              u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
-             clib_memcpy_fast (op->dst, &seq_hi, sizeof (seq_hi));
+             clib_memcpy_fast (op->digest, &seq_hi, sizeof (seq_hi));
              op->len += sizeof (seq_hi);
            }
        }
index 5a64432..b19112d 100644 (file)
@@ -1,8 +1,8 @@
 diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
-index 69e7ae3b..99373466 100644
+index 69e7ae3b..b35da2b1 100644
 --- a/scapy/layers/ipsec.py
 +++ b/scapy/layers/ipsec.py
-@@ -518,12 +517,16 @@ class AuthAlgo(object):
+@@ -518,12 +518,16 @@ class AuthAlgo(object):
          else:
              return self.mac(key, self.digestmod(), default_backend())
  
@@ -20,7 +20,14 @@ index 69e7ae3b..99373466 100644
  
          @return: the signed packet
          """
-@@ -539,11 +542,13 @@ class AuthAlgo(object):
+@@ -534,16 +538,20 @@ class AuthAlgo(object):
+         if pkt.haslayer(ESP):
+             mac.update(raw(pkt[ESP]))
++            if trailer:
++                mac.update(trailer)
+             pkt[ESP].data += mac.finalize()[:self.icv_size]
          elif pkt.haslayer(AH):
              clone = zero_mutable_fields(pkt.copy(), sending=True)
              mac.update(raw(clone))
@@ -35,7 +42,7 @@ index 69e7ae3b..99373466 100644
          """
          Check that the integrity check value (icv) of a packet is valid.
  
-@@ -574,6 +579,8 @@ class AuthAlgo(object):
+@@ -574,6 +582,8 @@ class AuthAlgo(object):
              clone = zero_mutable_fields(pkt.copy(), sending=False)
  
          mac.update(raw(clone))
@@ -44,7 +51,7 @@ index 69e7ae3b..99373466 100644
          computed_icv = mac.finalize()[:self.icv_size]
  
          # XXX: Cannot use mac.verify because the ICV can be truncated
-@@ -757,7 +764,8 @@ class SecurityAssociation(object):
+@@ -757,7 +767,8 @@ class SecurityAssociation(object):
      SUPPORTED_PROTOS = (IP, IPv6)
  
      def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
@@ -54,7 +61,7 @@ index 69e7ae3b..99373466 100644
          """
          @param proto: the IPsec proto to use (ESP or AH)
          @param spi: the Security Parameters Index of this SA
-@@ -771,6 +779,7 @@ class SecurityAssociation(object):
+@@ -771,6 +782,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.
@@ -62,7 +69,7 @@ index 69e7ae3b..99373466 100644
          """
  
          if proto not in (ESP, AH, ESP.name, AH.name):
-@@ -782,6 +791,7 @@ class SecurityAssociation(object):
+@@ -782,6 +794,7 @@ class SecurityAssociation(object):
  
          self.spi = spi
          self.seq_num = seq_num
@@ -70,7 +77,7 @@ index 69e7ae3b..99373466 100644
  
          if crypt_algo:
              if crypt_algo not in CRYPT_ALGOS:
-@@ -827,6 +837,17 @@ class SecurityAssociation(object):
+@@ -827,6 +840,17 @@ class SecurityAssociation(object):
              raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
                              (pkt.spi, self.spi))
  
@@ -88,7 +95,7 @@ index 69e7ae3b..99373466 100644
      def _encrypt_esp(self, pkt, seq_num=None, iv=None):
  
          if iv is None:
-@@ -835,7 +856,8 @@ class SecurityAssociation(object):
+@@ -835,7 +859,8 @@ class SecurityAssociation(object):
              if len(iv) != self.crypt_algo.iv_size:
                  raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
  
@@ -98,7 +105,7 @@ index 69e7ae3b..99373466 100644
  
          if self.tunnel_header:
              tunnel = self.tunnel_header.copy()
-@@ -857,7 +879,7 @@ class SecurityAssociation(object):
+@@ -857,7 +882,7 @@ class SecurityAssociation(object):
          esp = self.crypt_algo.pad(esp)
          esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
  
@@ -107,7 +114,7 @@ index 69e7ae3b..99373466 100644
  
          if self.nat_t_header:
              nat_t_header = self.nat_t_header.copy()
-@@ -884,7 +906,8 @@ class SecurityAssociation(object):
+@@ -884,7 +909,8 @@ class SecurityAssociation(object):
  
      def _encrypt_ah(self, pkt, seq_num=None):
  
@@ -117,7 +124,7 @@ index 69e7ae3b..99373466 100644
                  icv = b"\x00" * self.auth_algo.icv_size)
  
          if self.tunnel_header:
-@@ -924,7 +947,8 @@ class SecurityAssociation(object):
+@@ -924,7 +950,8 @@ class SecurityAssociation(object):
          else:
              ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
  
@@ -127,7 +134,7 @@ index 69e7ae3b..99373466 100644
  
          # sequence number must always change, unless specified by the user
          if seq_num is None:
-@@ -955,11 +979,12 @@ class SecurityAssociation(object):
+@@ -955,11 +982,12 @@ class SecurityAssociation(object):
  
      def _decrypt_esp(self, pkt, verify=True):
  
@@ -141,7 +148,7 @@ index 69e7ae3b..99373466 100644
  
          esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
                                        self.crypt_algo.icv_size or
-@@ -998,9 +1023,11 @@ class SecurityAssociation(object):
+@@ -998,9 +1026,11 @@ class SecurityAssociation(object):
  
      def _decrypt_ah(self, pkt, verify=True):
  
index 1580374..403f0bb 100644 (file)
@@ -100,6 +100,7 @@ class ConfigIpsecESP(TemplateIpsec):
         addr_any = params.addr_any
         addr_bcast = params.addr_bcast
         e = VppEnum.vl_api_ipsec_spd_action_t
+        flags = params.flags
         objs = []
 
         params.tun_sa_in = VppIpsecSA(self, scapy_tun_sa_id, scapy_tun_spi,
@@ -107,13 +108,15 @@ class ConfigIpsecESP(TemplateIpsec):
                                       crypt_algo_vpp_id, crypt_key,
                                       self.vpp_esp_protocol,
                                       self.tun_if.local_addr[addr_type],
-                                      self.tun_if.remote_addr[addr_type])
+                                      self.tun_if.remote_addr[addr_type],
+                                      flags=flags)
         params.tun_sa_out = VppIpsecSA(self, vpp_tun_sa_id, vpp_tun_spi,
                                        auth_algo_vpp_id, auth_key,
                                        crypt_algo_vpp_id, crypt_key,
                                        self.vpp_esp_protocol,
                                        self.tun_if.remote_addr[addr_type],
-                                       self.tun_if.local_addr[addr_type])
+                                       self.tun_if.local_addr[addr_type],
+                                       flags=flags)
         objs.append(params.tun_sa_in)
         objs.append(params.tun_sa_out)
 
@@ -337,7 +340,7 @@ class TemplateIpsecEspUdp(ConfigIpsecESP):
         self.logger.info(self.vapi.cli("show hardware"))
 
 
-class TestIpsecEspUdp(TemplateIpsecEspUdp, IpsecTra4Tests, IpsecTun4Tests):
+class TestIpsecEspUdp(TemplateIpsecEspUdp, IpsecTra4Tests):
     """ Ipsec NAT-T ESP UDP tests """
     pass
 
@@ -354,7 +357,7 @@ class TestIpsecEspAll(ConfigIpsecESP,
         super(TestIpsecEspAll, self).tearDown()
 
     def test_crypto_algs(self):
-        """All engines AES-CBC-[128, 192, 256] w/o ESN"""
+        """All engines AES-CBC-[128, 192, 256] w/ & w/o ESN"""
 
         # foreach VPP crypto engine
         engines = ["ia32", "ipsecmb", "openssl"]
@@ -373,9 +376,9 @@ class TestIpsecEspAll(ConfigIpsecESP,
                   'scapy': "AES-CBC",
                   'key': "JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h"}]
 
-        # bug found in VPP needs fixing with flag
-        # (VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_USE_ESN)
-        flags = [0]
+        # with and without ESN
+        flags = [0,
+                 VppEnum.vl_api_ipsec_sad_flags_t.IPSEC_API_SAD_FLAG_USE_ESN]
 
         #
         # loop through the VPP engines
@@ -418,8 +421,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], 17)
-                    self.verify_tun_44(self.params[socket.AF_INET], 17)
+                    self.verify_tun_66(self.params[socket.AF_INET6], 1)
+                    self.verify_tun_44(self.params[socket.AF_INET], 1)
 
                     #
                     # remove the SPDs, SAs, etc