http_static: fix reply data leak
[vpp.git] / src / vnet / ipsec / esp.h
index d9ab1d8..1c3ce77 100644 (file)
 #define __ESP_H__
 
 #include <vnet/ip/ip.h>
+#include <vnet/crypto/crypto.h>
 #include <vnet/ipsec/ipsec.h>
-
-#include <openssl/hmac.h>
-#include <openssl/rand.h>
-#include <openssl/evp.h>
+#include <vnet/ipsec/ipsec.api_enum.h>
 
 typedef struct
 {
-  u32 spi;
+  union
+  {
+    u32 spi;
+    u8 spi_bytes[4];
+  };
   u32 seq;
   u8 data[0];
 } esp_header_t;
@@ -35,304 +37,283 @@ typedef struct
   u8 next_header;
 } esp_footer_t;
 
-/* *INDENT-OFF* */
 typedef CLIB_PACKED (struct {
   ip4_header_t ip4;
   esp_header_t esp;
 }) ip4_and_esp_header_t;
-/* *INDENT-ON* */
 
-/* *INDENT-OFF* */
+typedef CLIB_PACKED (struct {
+  ip4_header_t ip4;
+  udp_header_t udp;
+  esp_header_t esp;
+}) ip4_and_udp_and_esp_header_t;
+
 typedef CLIB_PACKED (struct {
   ip6_header_t ip6;
   esp_header_t esp;
 }) ip6_and_esp_header_t;
-/* *INDENT-ON* */
-
-typedef struct
-{
-  const EVP_CIPHER *type;
-} esp_crypto_alg_t;
 
+/**
+ * AES counter mode nonce
+ */
 typedef struct
 {
-  const EVP_MD *md;
-  u8 trunc_size;
-} esp_integ_alg_t;
+  u32 salt;
+  u64 iv;
+  u32 ctr; /* counter: 1 in big-endian for ctr, unused for gcm */
+} __clib_packed esp_ctr_nonce_t;
 
-typedef struct
-{
-  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
-  EVP_CIPHER_CTX *encrypt_ctx;
-#else
-  EVP_CIPHER_CTX encrypt_ctx;
-#endif
-    CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
-  EVP_CIPHER_CTX *decrypt_ctx;
-#else
-  EVP_CIPHER_CTX decrypt_ctx;
-#endif
-    CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
-  HMAC_CTX *hmac_ctx;
-#else
-  HMAC_CTX hmac_ctx;
-#endif
-  ipsec_crypto_alg_t last_encrypt_alg;
-  ipsec_crypto_alg_t last_decrypt_alg;
-  ipsec_integ_alg_t last_integ_alg;
-} esp_main_per_thread_data_t;
+STATIC_ASSERT_SIZEOF (esp_ctr_nonce_t, 16);
 
-typedef struct
+/**
+ * AES GCM Additional Authentication data
+ */
+typedef struct esp_aead_t_
 {
-  esp_crypto_alg_t *esp_crypto_algs;
-  esp_integ_alg_t *esp_integ_algs;
-  esp_main_per_thread_data_t *per_thread_data;
-} esp_main_t;
+  /**
+   * for GCM: when using ESN it's:
+   *   SPI, seq-hi, seg-low
+   * else
+   *   SPI, seq-low
+   */
+  u32 data[3];
+} __clib_packed esp_aead_t;
 
-extern esp_main_t esp_main;
-
-#define ESP_WINDOW_SIZE                (64)
-#define ESP_SEQ_MAX            (4294967295UL)
+#define ESP_SEQ_MAX            (4294967295UL)
 
 u8 *format_esp_header (u8 * s, va_list * args);
 
+/* TODO seq increment should be atomic to be accessed by multiple workers */
 always_inline int
-esp_replay_check (ipsec_sa_t * sa, u32 seq)
-{
-  u32 diff;
-
-  if (PREDICT_TRUE (seq > sa->last_seq))
-    return 0;
-
-  diff = sa->last_seq - seq;
-
-  if (ESP_WINDOW_SIZE > diff)
-    return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
-  else
-    return 1;
-
-  return 0;
-}
-
-always_inline int
-esp_replay_check_esn (ipsec_sa_t * sa, u32 seq)
+esp_seq_advance (ipsec_sa_t * sa)
 {
-  u32 tl = sa->last_seq;
-  u32 th = sa->last_seq_hi;
-  u32 diff = tl - seq;
-
-  if (PREDICT_TRUE (tl >= (ESP_WINDOW_SIZE - 1)))
+  if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa)))
     {
-      if (seq >= (tl - ESP_WINDOW_SIZE + 1))
-       {
-         sa->seq_hi = th;
-         if (seq <= tl)
-           return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
-         else
-           return 0;
-       }
-      else
+      if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
        {
-         sa->seq_hi = th + 1;
-         return 0;
+         if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
+                            sa->seq_hi == ESP_SEQ_MAX))
+           return 1;
+         sa->seq_hi++;
        }
+      sa->seq++;
     }
   else
     {
-      if (seq >= (tl - ESP_WINDOW_SIZE + 1))
-       {
-         sa->seq_hi = th - 1;
-         return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
-       }
-      else
-       {
-         sa->seq_hi = th;
-         if (seq <= tl)
-           return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
-         else
-           return 0;
-       }
+      if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
+                        sa->seq == ESP_SEQ_MAX))
+       return 1;
+      sa->seq++;
     }
 
   return 0;
 }
 
-/* TODO seq increment should be atomic to be accessed by multiple workers */
-always_inline void
-esp_replay_advance (ipsec_sa_t * sa, u32 seq)
+always_inline u16
+esp_aad_fill (u8 *data, const esp_header_t *esp, const ipsec_sa_t *sa,
+             u32 seq_hi)
 {
-  u32 pos;
+  esp_aead_t *aad;
 
-  if (seq > sa->last_seq)
+  aad = (esp_aead_t *) data;
+  aad->data[0] = esp->spi;
+
+  if (ipsec_sa_is_set_USE_ESN (sa))
     {
-      pos = seq - sa->last_seq;
-      if (pos < ESP_WINDOW_SIZE)
-       sa->replay_window = ((sa->replay_window) << pos) | 1;
-      else
-       sa->replay_window = 1;
-      sa->last_seq = seq;
+      /* SPI, seq-hi, seq-low */
+      aad->data[1] = (u32) clib_host_to_net_u32 (seq_hi);
+      aad->data[2] = esp->seq;
+      return 12;
     }
   else
     {
-      pos = sa->last_seq - seq;
-      sa->replay_window |= (1ULL << pos);
+      /* SPI, seq-low */
+      aad->data[1] = esp->seq;
+      return 8;
     }
 }
 
-always_inline void
-esp_replay_advance_esn (ipsec_sa_t * sa, u32 seq)
+always_inline u32
+esp_encrypt_err_to_sa_err (u32 err)
 {
-  int wrap = sa->seq_hi - sa->last_seq_hi;
-  u32 pos;
-
-  if (wrap == 0 && seq > sa->last_seq)
+  switch (err)
     {
-      pos = seq - sa->last_seq;
-      if (pos < ESP_WINDOW_SIZE)
-       sa->replay_window = ((sa->replay_window) << pos) | 1;
-      else
-       sa->replay_window = 1;
-      sa->last_seq = seq;
-    }
-  else if (wrap > 0)
-    {
-      pos = ~seq + sa->last_seq + 1;
-      if (pos < ESP_WINDOW_SIZE)
-       sa->replay_window = ((sa->replay_window) << pos) | 1;
-      else
-       sa->replay_window = 1;
-      sa->last_seq = seq;
-      sa->last_seq_hi = sa->seq_hi;
-    }
-  else if (wrap < 0)
-    {
-      pos = ~seq + sa->last_seq + 1;
-      sa->replay_window |= (1ULL << pos);
-    }
-  else
-    {
-      pos = sa->last_seq - seq;
-      sa->replay_window |= (1ULL << pos);
+    case ESP_ENCRYPT_ERROR_HANDOFF:
+      return IPSEC_SA_ERROR_HANDOFF;
+    case ESP_ENCRYPT_ERROR_SEQ_CYCLED:
+      return IPSEC_SA_ERROR_SEQ_CYCLED;
+    case ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR:
+      return IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR;
+    case ESP_ENCRYPT_ERROR_CRYPTO_QUEUE_FULL:
+      return IPSEC_SA_ERROR_CRYPTO_QUEUE_FULL;
+    case ESP_ENCRYPT_ERROR_NO_BUFFERS:
+      return IPSEC_SA_ERROR_NO_BUFFERS;
+    case ESP_ENCRYPT_ERROR_NO_ENCRYPTION:
+      return IPSEC_SA_ERROR_NO_ENCRYPTION;
     }
+  return ~0;
 }
 
-always_inline int
-esp_seq_advance (ipsec_sa_t * sa)
+always_inline u32
+esp_decrypt_err_to_sa_err (u32 err)
 {
-  if (PREDICT_TRUE (sa->use_esn))
-    {
-      if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX))
-       {
-         if (PREDICT_FALSE
-             (sa->use_anti_replay && sa->seq_hi == ESP_SEQ_MAX))
-           return 1;
-         sa->seq_hi++;
-       }
-      sa->seq++;
-    }
-  else
+  switch (err)
     {
-      if (PREDICT_FALSE (sa->use_anti_replay && sa->seq == ESP_SEQ_MAX))
-       return 1;
-      sa->seq++;
+    case ESP_DECRYPT_ERROR_HANDOFF:
+      return IPSEC_SA_ERROR_HANDOFF;
+    case ESP_DECRYPT_ERROR_DECRYPTION_FAILED:
+      return IPSEC_SA_ERROR_DECRYPTION_FAILED;
+    case ESP_DECRYPT_ERROR_INTEG_ERROR:
+      return IPSEC_SA_ERROR_INTEG_ERROR;
+    case ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR:
+      return IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR;
+    case ESP_DECRYPT_ERROR_REPLAY:
+      return IPSEC_SA_ERROR_REPLAY;
+    case ESP_DECRYPT_ERROR_RUNT:
+      return IPSEC_SA_ERROR_RUNT;
+    case ESP_DECRYPT_ERROR_NO_BUFFERS:
+      return IPSEC_SA_ERROR_NO_BUFFERS;
+    case ESP_DECRYPT_ERROR_OVERSIZED_HEADER:
+      return IPSEC_SA_ERROR_OVERSIZED_HEADER;
+    case ESP_DECRYPT_ERROR_NO_TAIL_SPACE:
+      return IPSEC_SA_ERROR_NO_TAIL_SPACE;
+    case ESP_DECRYPT_ERROR_TUN_NO_PROTO:
+      return IPSEC_SA_ERROR_TUN_NO_PROTO;
+    case ESP_DECRYPT_ERROR_UNSUP_PAYLOAD:
+      return IPSEC_SA_ERROR_UNSUP_PAYLOAD;
     }
-
-  return 0;
+  return ~0;
 }
 
 always_inline void
-esp_init ()
+esp_encrypt_set_next_index (vlib_buffer_t *b, vlib_node_runtime_t *node,
+                           u32 thread_index, u32 err, u16 index, u16 *nexts,
+                           u16 drop_next, u32 sa_index)
 {
-  esp_main_t *em = &esp_main;
-  vlib_thread_main_t *tm = vlib_get_thread_main ();
-
-  memset (em, 0, sizeof (em[0]));
-
-  vec_validate (em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
-  em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = EVP_aes_128_cbc ();
-  em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = EVP_aes_192_cbc ();
-  em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = EVP_aes_256_cbc ();
+  ipsec_set_next_index (b, node, thread_index, err,
+                       esp_encrypt_err_to_sa_err (err), index, nexts,
+                       drop_next, sa_index);
+}
 
-  vec_validate (em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
-  esp_integ_alg_t *i;
+always_inline void
+esp_decrypt_set_next_index (vlib_buffer_t *b, vlib_node_runtime_t *node,
+                           u32 thread_index, u32 err, u16 index, u16 *nexts,
+                           u16 drop_next, u32 sa_index)
+{
+  ipsec_set_next_index (b, node, thread_index, err,
+                       esp_decrypt_err_to_sa_err (err), index, nexts,
+                       drop_next, sa_index);
+}
 
-  i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
-  i->md = EVP_sha1 ();
-  i->trunc_size = 12;
+/**
+ * The post data structure to for esp_encrypt/decrypt_inline to write to
+ * vib_buffer_t opaque unused field, and for post nodes to pick up after
+ * dequeue.
+ **/
+typedef struct
+{
+  union
+  {
+    struct
+    {
+      u8 icv_sz;
+      u8 iv_sz;
+      ipsec_sa_flags_t flags;
+      u32 sa_index;
+    };
+    u64 sa_data;
+  };
 
-  i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
-  i->md = EVP_sha256 ();
-  i->trunc_size = 12;
+  u32 seq;
+  i16 current_data;
+  i16 current_length;
+  u16 hdr_sz;
+  u16 is_chain;
+  u32 seq_hi;
+} esp_decrypt_packet_data_t;
+
+STATIC_ASSERT_SIZEOF (esp_decrypt_packet_data_t, 3 * sizeof (u64));
+STATIC_ASSERT_OFFSET_OF (esp_decrypt_packet_data_t, seq, sizeof (u64));
+
+/* we are forced to store the decrypt post data into 2 separate places -
+   vlib_opaque and opaque2. */
+typedef struct
+{
+  vlib_buffer_t *lb;
+  u32 free_buffer_index;
+  u8 icv_removed;
+} esp_decrypt_packet_data2_t;
 
-  i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
-  i->md = EVP_sha256 ();
-  i->trunc_size = 16;
+typedef union
+{
+  u16 next_index;
+  esp_decrypt_packet_data_t decrypt_data;
+} esp_post_data_t;
 
-  i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
-  i->md = EVP_sha384 ();
-  i->trunc_size = 24;
+STATIC_ASSERT (sizeof (esp_post_data_t) <=
+              STRUCT_SIZE_OF (vnet_buffer_opaque_t, unused),
+              "Custom meta-data too large for vnet_buffer_opaque_t");
 
-  i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
-  i->md = EVP_sha512 ();
-  i->trunc_size = 32;
+#define esp_post_data(b) \
+    ((esp_post_data_t *)((u8 *)((b)->opaque) \
+        + STRUCT_OFFSET_OF (vnet_buffer_opaque_t, unused)))
 
-  vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
-                       CLIB_CACHE_LINE_BYTES);
-  int thread_id;
+STATIC_ASSERT (sizeof (esp_decrypt_packet_data2_t) <=
+              STRUCT_SIZE_OF (vnet_buffer_opaque2_t, unused),
+              "Custom meta-data too large for vnet_buffer_opaque2_t");
 
-  for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
-    {
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
-      em->per_thread_data[thread_id].encrypt_ctx = EVP_CIPHER_CTX_new ();
-      em->per_thread_data[thread_id].decrypt_ctx = EVP_CIPHER_CTX_new ();
-      em->per_thread_data[thread_id].hmac_ctx = HMAC_CTX_new ();
-#else
-      EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].encrypt_ctx));
-      EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].decrypt_ctx));
-      HMAC_CTX_init (&(em->per_thread_data[thread_id].hmac_ctx));
-#endif
-    }
-}
+#define esp_post_data2(b) \
+    ((esp_decrypt_packet_data2_t *)((u8 *)((b)->opaque2) \
+        + STRUCT_OFFSET_OF (vnet_buffer_opaque2_t, unused)))
 
-always_inline unsigned int
-hmac_calc (ipsec_integ_alg_t alg,
-          u8 * key,
-          int key_len,
-          u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
+typedef struct
 {
-  esp_main_t *em = &esp_main;
-  u32 thread_index = vlib_get_thread_index ();
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
-  HMAC_CTX *ctx = em->per_thread_data[thread_index].hmac_ctx;
-#else
-  HMAC_CTX *ctx = &(em->per_thread_data[thread_index].hmac_ctx);
-#endif
-  const EVP_MD *md = NULL;
-  unsigned int len;
-
-  ASSERT (alg < IPSEC_INTEG_N_ALG);
-
-  if (PREDICT_FALSE (em->esp_integ_algs[alg].md == 0))
-    return 0;
-
-  if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_integ_alg))
+  /* esp post node index for async crypto */
+  u32 esp4_post_next;
+  u32 esp6_post_next;
+  u32 esp4_tun_post_next;
+  u32 esp6_tun_post_next;
+  u32 esp_mpls_tun_post_next;
+} esp_async_post_next_t;
+
+extern esp_async_post_next_t esp_encrypt_async_next;
+extern esp_async_post_next_t esp_decrypt_async_next;
+
+/* when submitting a frame is failed, drop all buffers in the frame */
+always_inline u32
+esp_async_recycle_failed_submit (vlib_main_t *vm, vnet_crypto_async_frame_t *f,
+                                vlib_node_runtime_t *node, u32 err,
+                                u32 ipsec_sa_err, u16 index, u32 *from,
+                                u16 *nexts, u16 drop_next_index,
+                                bool is_encrypt)
+{
+  vlib_buffer_t *b;
+  u32 n_drop = f->n_elts;
+  u32 *bi = f->buffer_indices;
+
+  while (n_drop--)
     {
-      md = em->esp_integ_algs[alg].md;
-      em->per_thread_data[thread_index].last_integ_alg = alg;
-    }
+      u32 sa_index;
 
-  HMAC_Init_ex (ctx, key, key_len, md, NULL);
+      from[index] = bi[0];
+      b = vlib_get_buffer (vm, bi[0]);
 
-  HMAC_Update (ctx, data, data_len);
+      if (is_encrypt)
+       {
+         sa_index = vnet_buffer (b)->ipsec.sad_index;
+       }
+      else
+       {
+         sa_index = esp_post_data (b)->decrypt_data.sa_index;
+       }
 
-  if (PREDICT_TRUE (use_esn))
-    HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
-  HMAC_Final (ctx, signature, &len);
+      ipsec_set_next_index (b, node, vm->thread_index, err, ipsec_sa_err,
+                           index, nexts, drop_next_index, sa_index);
+      bi++;
+      index++;
+    }
 
-  return em->esp_integ_algs[alg].trunc_size;
+  return (f->n_elts);
 }
 
 #endif /* __ESP_H__ */