summaryrefslogtreecommitdiffstats
path: root/src/tests/vnet/session
AgeCommit message (Expand)AuthorFilesLines
2019-01-25tcp_echo: support all ctrl msgs and io cleanupFlorin Coras1-95/+103
2019-01-02Fixes for buliding for 32bit targets:David Johnson2-3/+4
2018-12-03svm: use explicit svm fifo segment main for slavesFlorin Coras2-17/+20
2018-11-30session: segment handle in accept/connect notificationsFlorin Coras1-14/+8
2018-11-14Remove c-11 memcpy checks from perf-critical codeDave Barach2-12/+12
2018-10-28session: extend connect api for internal appsFlorin Coras1-3/+2
2018-10-23c11 safe string handling supportDave Barach2-24/+24
2018-10-04udp_echo: fix cut-through server modeFlorin Coras1-206/+217
2018-08-10vcl: support for eventfd mq signalingFlorin Coras1-52/+36
2018-08-09Fix the fifo-size limitation in tcp_echoYalei Wang1-3/+1
2018-08-04socket api: multiple fds in one msgFlorin Coras1-2/+2
2018-07-19session: coverity warningsFlorin Coras1-12/+7
2018-07-18Add config option to use dlmalloc instead of mheapDave Barach2-16/+4
2018-07-17session: send ctrl msg over mqFlorin Coras2-459/+453
2018-07-17session: use msg queue for eventsFlorin Coras2-30/+46
2018-06-19tcp: optimize tcp outputFlorin Coras1-1/+2
2018-06-04fix usage string missing argJerome Tollet1-1/+1
2018-05-19tcp_echo: support multiple connectionsFlorin Coras1-218/+250
2018-05-09session: cleanup session tx functionFlorin Coras2-49/+79
2018-04-18udp/session: refactor to support dgram modeFlorin Coras1-172/+303
2018-03-07tls: enforce certificate verificationFlorin Coras1-45/+49
2018-03-05tls: add stop listen handlerFlorin Coras1-11/+102
2018-02-14session: support local sessions and deprecate redirectsFlorin Coras2-44/+87
2018-02-05session: segment manager refactorFlorin Coras2-2/+2
2018-01-27session: use clib rwlocks instead of custom implementationFlorin Coras1-1/+1
2018-01-25session: add support for memfd segmentsFlorin Coras2-251/+313
2018-01-22svm: queue sub: Add conditional timed waitMohsin Kazmi2-4/+4
2018-01-09api: refactor vlibmemoryFlorin Coras2-40/+26
2018-01-05sock api: add infra for bootstrapping shm clientsFlorin Coras1-39/+73
2017-12-15apps: refactor uri and update build infraFlorin Coras2-0/+2452
#f8f8f2 } /* Name.Builtin */ .highlight .nc { color: #a6e22e } /* Name.Class */ .highlight .no { color: #66d9ef } /* Name.Constant */ .highlight .nd { color: #a6e22e } /* Name.Decorator */ .highlight .ni { color: #f8f8f2 } /* Name.Entity */ .highlight .ne { color: #a6e22e } /* Name.Exception */ .highlight .nf { color: #a6e22e } /* Name.Function */ .highlight .nl { color: #f8f8f2 } /* Name.Label */ .highlight .nn { color: #f8f8f2 } /* Name.Namespace */ .highlight .nx { color: #a6e22e } /* Name.Other */ .highlight .py { color: #f8f8f2 } /* Name.Property */ .highlight .nt { color: #f92672 } /* Name.Tag */ .highlight .nv { color: #f8f8f2 } /* Name.Variable */ .highlight .ow { color: #f92672 } /* Operator.Word */ .highlight .w { color: #f8f8f2 } /* Text.Whitespace */ .highlight .mb { color: #ae81ff } /* Literal.Number.Bin */ .highlight .mf { color: #ae81ff } /* Literal.Number.Float */ .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */ .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */ .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */ .highlight .sa { color: #e6db74 } /* Literal.String.Affix */ .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */ .highlight .sc { color: #e6db74 } /* Literal.String.Char */ .highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */ .highlight .sd { color: #e6db74 } /* Literal.String.Doc */ .highlight .s2 { color: #e6db74 } /* Literal.String.Double */ .highlight .se { color: #ae81ff } /* Literal.String.Escape */ .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */ .highlight .si { color: #e6db74 } /* Literal.String.Interpol */ .highlight .sx { color: #e6db74 } /* Literal.String.Other */ .highlight .sr { color: #e6db74 } /* Literal.String.Regex */ .highlight .s1 { color: #e6db74 } /* Literal.String.Single */ .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */ .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #a6e22e } /* Name.Function.Magic */ .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */ .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */ .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */ .highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */ .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */ } @media (prefers-color-scheme: light) { .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*
 *------------------------------------------------------------------
 * Copyright (c) 2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <vlib/vlib.h>
#include <vnet/plugin/plugin.h>
#include <vnet/crypto/crypto.h>
#include <crypto_native/crypto_native.h>
#include <crypto_native/aes.h>

#if __GNUC__ > 4  && !__clang__ && CLIB_DEBUG == 0
#pragma GCC optimize ("O3")
#endif

typedef struct
{
  u8x16 encrypt_key[15];
#if __VAES__
  u8x64 decrypt_key[15];
#else
  u8x16 decrypt_key[15];
#endif
} aes_cbc_key_data_t;


static_always_inline void __clib_unused
aes_cbc_dec (u8x16 * k, u8x16u * src, u8x16u * dst, u8x16u * iv, int count,
	     int rounds)
{
  u8x16 r[4], c[4], f;

  f = iv[0];
  while (count >= 64)
    {
      clib_prefetch_load (src + 8);
      clib_prefetch_load (dst + 8);

      c[0] = r[0] = src[0];
      c[1] = r[1] = src[1];
      c[2] = r[2] = src[2];
      c[3] = r[3] = src[3];

#if __x86_64__
      r[0] ^= k[0];
      r[1] ^= k[0];
      r[2] ^= k[0];
      r[3] ^= k[0];

      for (int i = 1; i < rounds; i++)
	{
	  r[0] = aes_dec_round (r[0], k[i]);
	  r[1] = aes_dec_round (r[1], k[i]);
	  r[2] = aes_dec_round (r[2], k[i]);
	  r[3] = aes_dec_round (r[3], k[i]);
	}

      r[0] = aes_dec_last_round (r[0], k[rounds]);
      r[1] = aes_dec_last_round (r[1], k[rounds]);
      r[2] = aes_dec_last_round (r[2], k[rounds]);
      r[3] = aes_dec_last_round (r[3], k[rounds]);
#else
      for (int i = 0; i < rounds - 1; i++)
	{
	  r[0] = vaesimcq_u8 (vaesdq_u8 (r[0], k[i]));
	  r[1] = vaesimcq_u8 (vaesdq_u8 (r[1], k[i]));
	  r[2] = vaesimcq_u8 (vaesdq_u8 (r[2], k[i]));
	  r[3] = vaesimcq_u8 (vaesdq_u8 (r[3], k[i]));
	}
      r[0] = vaesdq_u8 (r[0], k[rounds - 1]) ^ k[rounds];
      r[1] = vaesdq_u8 (r[1], k[rounds - 1]) ^ k[rounds];
      r[2] = vaesdq_u8 (r[2], k[rounds - 1]) ^ k[rounds];
      r[3] = vaesdq_u8 (r[3], k[rounds - 1]) ^ k[rounds];
#endif
      dst[0] = r[0] ^ f;
      dst[1] = r[1] ^ c[0];
      dst[2] = r[2] ^ c[1];
      dst[3] = r[3] ^ c[2];
      f = c[3];

      count -= 64;
      src += 4;
      dst += 4;
    }

  while (count > 0)
    {
      c[0] = r[0] = src[0];
#if __x86_64__
      r[0] ^= k[0];
      for (int i = 1; i < rounds; i++)
	r[0] = aes_dec_round (r[0], k[i]);
      r[0] = aes_dec_last_round (r[0], k[rounds]);
#else
      c[0] = r[0] = src[0];
      for (int i = 0; i < rounds - 1; i++)
	r[0] = vaesimcq_u8 (vaesdq_u8 (r[0], k[i]));
      r[0] = vaesdq_u8 (r[0], k[rounds - 1]) ^ k[rounds];
#endif
      dst[0] = r[0] ^ f;
      f = c[0];

      count -= 16;
      src += 1;
      dst += 1;
    }
}

#if __x86_64__
#ifdef __VAES__

static_always_inline u8x64
aes_block_load_x4 (u8 * src[], int i)
{
  u8x64 r = { };
  r = u8x64_insert_u8x16 (r, aes_block_load (src[0] + i), 0);
  r = u8x64_insert_u8x16 (r, aes_block_load (src[1] + i), 1);
  r = u8x64_insert_u8x16 (r, aes_block_load (src[2] + i), 2);
  r = u8x64_insert_u8x16 (r, aes_block_load (src[3] + i), 3);
  return r;
}

static_always_inline void
aes_block_store_x4 (u8 * dst[], int i, u8x64 r)
{
  aes_block_store (dst[0] + i, u8x64_extract_u8x16 (r, 0));
  aes_block_store (dst[1] + i, u8x64_extract_u8x16 (r, 1));
  aes_block_store (dst[2] + i, u8x64_extract_u8x16 (r, 2));
  aes_block_store (dst[3] + i, u8x64_extract_u8x16 (r, 3));
}

static_always_inline u8x64
aes_cbc_dec_permute (u8x64 a, u8x64 b)
{
  __m512i perm = { 6, 7, 8, 9, 10, 11, 12, 13 };
  return (u8x64) _mm512_permutex2var_epi64 ((__m512i) a, perm, (__m512i) b);
}

static_always_inline void
vaes_cbc_dec (u8x64 * k, u8x64u * src, u8x64u * dst, u8x16 * iv, int count,
	      aes_key_size_t rounds)
{
  u8x64 f, r[4], c[4] = { };
  __mmask8 m;
  int i, n_blocks = count >> 4;

  f = (u8x64) _mm512_mask_loadu_epi64 (_mm512_setzero_si512 (), 0xc0,
				       (__m512i *) (iv - 3));

  while (n_blocks >= 16)
    {
      c[0] = src[0];
      c[1] = src[1];
      c[2] = src[2];
      c[3] = src[3];

      r[0] = c[0] ^ k[0];
      r[1] = c[1] ^ k[0];
      r[2] = c[2] ^ k[0];
      r[3] = c[3] ^ k[0];

      for (i = 1; i < rounds; i++)
	{
	  r[0] = aes_dec_round_x4 (r[0], k[i]);
	  r[1] = aes_dec_round_x4 (r[1], k[i]);
	  r[2] = aes_dec_round_x4 (r[2], k[i]);
	  r[3] = aes_dec_round_x4 (r[3], k[i]);
	}

      r[0] = aes_dec_last_round_x4 (r[0], k[i]);
      r[1] = aes_dec_last_round_x4 (r[1], k[i]);
      r[2] = aes_dec_last_round_x4 (r[2], k[i]);
      r[3] = aes_dec_last_round_x4 (r[3], k[i]);

      dst[0] = r[0] ^= aes_cbc_dec_permute (f, c[0]);
      dst[1] = r[1] ^= aes_cbc_dec_permute (c[0], c[1]);
      dst[2] = r[2] ^= aes_cbc_dec_permute (c[1], c[2]);
      dst[4] = r[3] ^= aes_cbc_dec_permute (c[2], c[3]);
      f = c[3];

      n_blocks -= 16;
      src += 4;
      dst += 4;
    }

  while (n_blocks > 0)
    {
      m = (1 << (n_blocks * 2)) - 1;
      c[0] = (u8x64) _mm512_mask_loadu_epi64 ((__m512i) c[0], m,
					      (__m512i *) src);
      f = aes_cbc_dec_permute (f, c[0]);
      r[0] = c[0] ^ k[0];
      for (i = 1; i < rounds; i++)
	r[0] = aes_dec_round_x4 (r[0], k[i]);
      r[0] = aes_dec_last_round_x4 (r[0], k[i]);
      _mm512_mask_storeu_epi64 ((__m512i *) dst, m, (__m512i) (r[0] ^ f));
      f = c[0];
      n_blocks -= 4;
      src += 1;
      dst += 1;
    }
}
#endif
#endif

#ifdef __VAES__
#define N 16
#define u32xN u32x16
#define u32xN_min_scalar u32x16_min_scalar
#define u32xN_is_all_zero u32x16_is_all_zero
#define u32xN_splat u32x16_splat
#else
#define N 4
#define u32xN u32x4
#define u32xN_min_scalar u32x4_min_scalar
#define u32xN_is_all_zero u32x4_is_all_zero
#define u32xN_splat u32x4_splat
#endif

static_always_inline u32
aes_ops_enc_aes_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[],
		     u32 n_ops, aes_key_size_t ks)
{
  crypto_native_main_t *cm = &crypto_native_main;
  crypto_native_per_thread_data_t *ptd =
    vec_elt_at_index (cm->per_thread_data, vm->thread_index);
  int rounds = AES_KEY_ROUNDS (ks);
  u8 placeholder[8192];
  u32 i, j, count, n_left = n_ops;
  u32xN placeholder_mask = { };
  u32xN len = { };
  vnet_crypto_key_index_t key_index[N];
  u8 *src[N] = { };
  u8 *dst[N] = { };
#if __VAES__
  u8x64 r[N / 4] = { };
  u8x64 k[15][N / 4] = { };
  u8x16 *kq, *rq = (u8x16 *) r;
#else
  u8x16 r[N] = { };
  u8x16 k[15][N] = { };
#endif

  for (i = 0; i < N; i++)
    key_index[i] = ~0;

more:
  for (i = 0; i < N; i++)
    if (len[i] == 0)
      {
	if (n_left == 0)
	  {
	    /* no more work to enqueue, so we are enqueueing placeholder buffer */
	    src[i] = dst[i] = placeholder;
	    len[i] = sizeof (placeholder);
	    placeholder_mask[i] = 0;
	  }
	else
	  {
	    u8x16 t;
	    if (ops[0]->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
	      {
		t = ptd->cbc_iv[i];
		*(u8x16u *) ops[0]->iv = t;
		ptd->cbc_iv[i] = aes_enc_round (t, t);
	      }
	    else
	      t = aes_block_load (ops[0]->iv);
#if __VAES__
	    rq[i] = t;
#else
	    r[i] = t;
#endif

	    src[i] = ops[0]->src;
	    dst[i] = ops[0]->dst;
	    len[i] = ops[0]->len;
	    placeholder_mask[i] = ~0;
	    if (key_index[i] != ops[0]->key_index)
	      {
		aes_cbc_key_data_t *kd;
		key_index[i] = ops[0]->key_index;
		kd = (aes_cbc_key_data_t *) cm->key_data[key_index[i]];
		for (j = 0; j < rounds + 1; j++)
		  {
#if __VAES__
		    kq = (u8x16 *) k[j];
		    kq[i] = kd->encrypt_key[j];
#else
		    k[j][i] = kd->encrypt_key[j];
#endif
		  }
	      }
	    ops[0]->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
	    n_left--;
	    ops++;
	  }
      }

  count = u32xN_min_scalar (len);

  ASSERT (count % 16 == 0);

  for (i = 0; i < count; i += 16)
    {
#ifdef __VAES__
      r[0] = u8x64_xor3 (r[0], aes_block_load_x4 (src, i), k[0][0]);
      r[1] = u8x64_xor3 (r[1], aes_block_load_x4 (src, i), k[0][1]);
      r[2] = u8x64_xor3 (r[2], aes_block_load_x4 (src, i), k[0][2]);
      r[3] = u8x64_xor3 (r[3], aes_block_load_x4 (src, i), k[0][3]);

      for (j = 1; j < rounds; j++)
	{
	  r[0] = aes_enc_round_x4 (r[0], k[j][0]);
	  r[1] = aes_enc_round_x4 (r[1], k[j][1]);
	  r[2] = aes_enc_round_x4 (r[2], k[j][2]);
	  r[3] = aes_enc_round_x4 (r[3], k[j][3]);
	}
      r[0] = aes_enc_last_round_x4 (r[0], k[j][0]);
      r[1] = aes_enc_last_round_x4 (r[1], k[j][1]);
      r[2] = aes_enc_last_round_x4 (r[2], k[j][2]);
      r[3] = aes_enc_last_round_x4 (r[3], k[j][3]);

      aes_block_store_x4 (dst, i, r[0]);
      aes_block_store_x4 (dst + 4, i, r[1]);
      aes_block_store_x4 (dst + 8, i, r[2]);
      aes_block_store_x4 (dst + 12, i, r[3]);
#else
#if __x86_64__
      r[0] = u8x16_xor3 (r[0], aes_block_load (src[0] + i), k[0][0]);
      r[1] = u8x16_xor3 (r[1], aes_block_load (src[1] + i), k[0][1]);
      r[2] = u8x16_xor3 (r[2], aes_block_load (src[2] + i), k[0][2]);
      r[3] = u8x16_xor3 (r[3], aes_block_load (src[3] + i), k[0][3]);

      for (j = 1; j < rounds; j++)
	{
	  r[0] = aes_enc_round (r[0], k[j][0]);
	  r[1] = aes_enc_round (r[1], k[j][1]);
	  r[2] = aes_enc_round (r[2], k[j][2]);
	  r[3] = aes_enc_round (r[3], k[j][3]);
	}

      r[0] = aes_enc_last_round (r[0], k[j][0]);
      r[1] = aes_enc_last_round (r[1], k[j][1]);
      r[2] = aes_enc_last_round (r[2], k[j][2]);
      r[3] = aes_enc_last_round (r[3], k[j][3]);

      aes_block_store (dst[0] + i, r[0]);
      aes_block_store (dst[1] + i, r[1]);
      aes_block_store (dst[2] + i, r[2]);
      aes_block_store (dst[3] + i, r[3]);
#else
      r[0] ^= aes_block_load (src[0] + i);
      r[1] ^= aes_block_load (src[1] + i);
      r[2] ^= aes_block_load (src[2] + i);
      r[3] ^= aes_block_load (src[3] + i);
      for (j = 0; j < rounds - 1; j++)
	{
	  r[0] = vaesmcq_u8 (vaeseq_u8 (r[0], k[j][0]));
	  r[1] = vaesmcq_u8 (vaeseq_u8 (r[1], k[j][1]));
	  r[2] = vaesmcq_u8 (vaeseq_u8 (r[2], k[j][2]));
	  r[3] = vaesmcq_u8 (vaeseq_u8 (r[3], k[j][3]));
	}
      r[0] = vaeseq_u8 (r[0], k[j][0]) ^ k[rounds][0];
      r[1] = vaeseq_u8 (r[1], k[j][1]) ^ k[rounds][1];
      r[2] = vaeseq_u8 (r[2], k[j][2]) ^ k[rounds][2];
      r[3] = vaeseq_u8 (r[3], k[j][3]) ^ k[rounds][3];
      aes_block_store (dst[0] + i, r[0]);
      aes_block_store (dst[1] + i, r[1]);
      aes_block_store (dst[2] + i, r[2]);
      aes_block_store (dst[3] + i, r[3]);
#endif
#endif
    }

  len -= u32xN_splat (count);

  for (i = 0; i < N; i++)
    {
      src[i] += count;
      dst[i] += count;
    }

  if (n_left > 0)
    goto more;

  if (!u32xN_is_all_zero (len & placeholder_mask))
    goto more;

  return n_ops;
}


static_always_inline u32
aes_ops_dec_aes_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[],
		     u32 n_ops, aes_key_size_t ks)
{
  crypto_native_main_t *cm = &crypto_native_main;
  int rounds = AES_KEY_ROUNDS (ks);
  vnet_crypto_op_t *op = ops[0];
  aes_cbc_key_data_t *kd = (aes_cbc_key_data_t *) cm->key_data[op->key_index];
  u32 n_left = n_ops;

  ASSERT (n_ops >= 1);

decrypt:
#ifdef __VAES__
  vaes_cbc_dec (kd->decrypt_key, (u8x64u *) op->src, (u8x64u *) op->dst,
		(u8x16u *) op->iv, op->len, rounds);
#else
  aes_cbc_dec (kd->decrypt_key, (u8x16u *) op->src, (u8x16u *) op->dst,
	       (u8x16u *) op->iv, op->len, rounds);
#endif
  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;

  if (--n_left)
    {
      op += 1;
      kd = (aes_cbc_key_data_t *) cm->key_data[op->key_index];
      goto decrypt;
    }

  return n_ops;
}

static_always_inline void *
aes_cbc_key_exp (vnet_crypto_key_t * key, aes_key_size_t ks)
{
  u8x16 e[15], d[15];
  aes_cbc_key_data_t *kd;
  kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
  aes_key_expand (e, key->data, ks);
  aes_key_enc_to_dec (e, d, ks);
  for (int i = 0; i < AES_KEY_ROUNDS (ks) + 1; i++)
    {
#if __VAES__
      kd->decrypt_key[i] = (u8x64) _mm512_broadcast_i64x2 ((__m128i) d[i]);
#else
      kd->decrypt_key[i] = d[i];
#endif
      kd->encrypt_key[i] = e[i];
    }
  return kd;
}

#define foreach_aes_cbc_handler_type _(128) _(192) _(256)

#define _(x) \
static u32 aes_ops_dec_aes_cbc_##x \
(vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
{ return aes_ops_dec_aes_cbc (vm, ops, n_ops, AES_KEY_##x); } \
static u32 aes_ops_enc_aes_cbc_##x \
(vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
{ return aes_ops_enc_aes_cbc (vm, ops, n_ops, AES_KEY_##x); } \
static void * aes_cbc_key_exp_##x (vnet_crypto_key_t *key) \
{ return aes_cbc_key_exp (key, AES_KEY_##x); }

foreach_aes_cbc_handler_type;
#undef _

#include <fcntl.h>

clib_error_t *
#ifdef __VAES__
crypto_native_aes_cbc_init_icl (vlib_main_t * vm)
#elif __AVX512F__
crypto_native_aes_cbc_init_skx (vlib_main_t * vm)
#elif __aarch64__
crypto_native_aes_cbc_init_neon (vlib_main_t * vm)
#elif __AVX2__
crypto_native_aes_cbc_init_hsw (vlib_main_t * vm)
#else
crypto_native_aes_cbc_init_slm (vlib_main_t * vm)
#endif
{
  crypto_native_main_t *cm = &crypto_native_main;
  crypto_native_per_thread_data_t *ptd;
  clib_error_t *err = 0;
  int fd;

  if ((fd = open ("/dev/urandom", O_RDONLY)) < 0)
    return clib_error_return_unix (0, "failed to open '/dev/urandom'");

  /* *INDENT-OFF* */
  vec_foreach (ptd, cm->per_thread_data)
    {
      for (int i = 0; i < 4; i++)
	{
	  if (read(fd, ptd->cbc_iv, sizeof (ptd->cbc_iv)) !=
	      sizeof (ptd->cbc_iv))
	    {
	      err = clib_error_return_unix (0, "'/dev/urandom' read failure");
	      goto error;
	    }
	}
    }
  /* *INDENT-ON* */

#define _(x) \
  vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index, \
				    VNET_CRYPTO_OP_AES_##x##_CBC_ENC, \
				    aes_ops_enc_aes_cbc_##x); \
  vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index, \
				    VNET_CRYPTO_OP_AES_##x##_CBC_DEC, \
				    aes_ops_dec_aes_cbc_##x); \
  cm->key_fn[VNET_CRYPTO_ALG_AES_##x##_CBC] = aes_cbc_key_exp_##x;
  foreach_aes_cbc_handler_type;
#undef _

error:
  close (fd);
  return err;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */