summaryrefslogtreecommitdiffstats
path: root/src/vnet/osi/osi.h
AgeCommit message (Expand)AuthorFilesLines
2021-06-07pg: Reduce the inclusion of pg.hNeale Ranns1-12/+0
2017-03-27Mcast rewrite no memcpyNeale Ranns1-3/+0
2016-12-28Reorganize source tree to use single autotools instanceDamjan Marion1-0/+171
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
/*
 *------------------------------------------------------------------
 * 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

#if defined(__VAES__) && defined(__AVX512F__)
#define N		  16
#define u8xN		  u8x64
#define u32xN		  u32x16
#define u32xN_min_scalar  u32x16_min_scalar
#define u32xN_is_all_zero u32x16_is_all_zero
#define u32xN_splat	  u32x16_splat
#elif defined(__VAES__)
#define N		  8
#define u8xN		  u8x32
#define u32xN		  u32x8
#define u32xN_min_scalar  u32x8_min_scalar
#define u32xN_is_all_zero u32x8_is_all_zero
#define u32xN_splat	  u32x8_splat
#else
#define N		  4
#define u8xN		  u8x16
#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

typedef struct
{
  u8x16 encrypt_key[15];
  u8xN decrypt_key[15];
} 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__
#if defined(__VAES__) && defined(__AVX512F__)

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
aes4_cbc_dec_permute (u8x64 a, u8x64 b)
{
  return (u8x64) u64x8_shuffle2 (a, b, 6, 7, 8, 9, 10, 11, 12, 13);
}

static_always_inline void
aes4_cbc_dec (u8x64 *k, u8x64u *src, u8x64u *dst, u8x16u *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] ^= aes4_cbc_dec_permute (f, c[0]);
      dst[1] = r[1] ^= aes4_cbc_dec_permute (c[0], c[1]);
      dst[2] = r[2] ^= aes4_cbc_dec_permute (c[1], c[2]);
      dst[3] = r[3] ^= aes4_cbc_dec_permute (c[2], c[3]);
      f = c[3];

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

  if (n_blocks >= 12)
    {
      c[0] = src[0];
      c[1] = src[1];
      c[2] = src[2];

      r[0] = c[0] ^ k[0];
      r[1] = c[1] ^ k[0];
      r[2] = c[2] ^ 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[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]);

      dst[0] = r[0] ^= aes4_cbc_dec_permute (f, c[0]);
      dst[1] = r[1] ^= aes4_cbc_dec_permute (c[0], c[1]);
      dst[2] = r[2] ^= aes4_cbc_dec_permute (c[1], c[2]);
      f = c[2];

      n_blocks -= 12;
      src += 3;
      dst += 3;
    }
  else if (n_blocks >= 8)
    {
      c[0] = src[0];
      c[1] = src[1];

      r[0] = c[0] ^ k[0];
      r[1] = c[1] ^ 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[0] = aes_dec_last_round_x4 (r[0], k[i]);
      r[1] = aes_dec_last_round_x4 (r[1], k[i]);

      dst[0] = r[0] ^= aes4_cbc_dec_permute (f, c[0]);
      dst[1] = r[1] ^= aes4_cbc_dec_permute (c[0], c[1]);
      f = c[1];

      n_blocks -= 8;
      src += 2;
      dst += 2;
    }
  else if (n_blocks >= 4)
    {
      c[0] = src[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]);

      dst[0] = r[0] ^= aes4_cbc_dec_permute (f, c[0]);
      f = c[0];

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

  if (n_blocks > 0)
    {
      m = (1 << (n_blocks * 2)) - 1;
      c[0] = (u8x64) _mm512_mask_loadu_epi64 ((__m512i) c[0], m,
					      (__m512i *) src);
      f = aes4_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));
    }
}
#elif defined(__VAES__)

static_always_inline u8x32
aes_block_load_x2 (u8 *src[], int i)
{
  u8x32 r = {};
  r = u8x32_insert_lo (r, aes_block_load (src[0] + i));
  r = u8x32_insert_hi (r, aes_block_load (src[1] + i));
  return r;
}

static_always_inline void
aes_block_store_x2 (u8 *dst[], int i, u8x32 r)
{
  aes_block_store (dst[0] + i, u8x32_extract_lo (r));
  aes_block_store (dst[1] + i, u8x32_extract_hi (r));
}

static_always_inline u8x32
aes2_cbc_dec_permute (u8x32 a, u8x32 b)
{
  return (u8x32) u64x4_shuffle2 ((u64x4) a, (u64x4) b, 2, 3, 4, 5);
}

static_always_inline void
aes2_cbc_dec (u8x32 *k, u8x32u *src, u8x32u *dst, u8x16u *iv, int count,
	      aes_key_size_t rounds)
{
  u8x32 f = {}, r[4], c[4] = {};
  int i, n_blocks = count >> 4;

  f = u8x32_insert_hi (f, *iv);

  while (n_blocks >= 8)
    {
      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_x2 (r[0], k[i]);
	  r[1] = aes_dec_round_x2 (r[1], k[i]);
	  r[2] = aes_dec_round_x2 (r[2], k[i]);
	  r[3] = aes_dec_round_x2 (r[3], k[i]);
	}

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

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

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

  if (n_blocks >= 6)
    {
      c[0] = src[0];
      c[1] = src[1];
      c[2] = src[2];

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

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

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

      dst[0] = r[0] ^= aes2_cbc_dec_permute (f, c[0]);
      dst[1] = r[1] ^= aes2_cbc_dec_permute (c[0], c[1]);
      dst[2] = r[2] ^= aes2_cbc_dec_permute (c[1], c[2]);
      f = c[2];

      n_blocks -= 6;
      src += 3;
      dst += 3;
    }
  else if (n_blocks >= 4)
    {
      c[0] = src[0];
      c[1] = src[1];

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

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

      r[0] = aes_dec_last_round_x2 (r[0], k[i]);
      r[1] = aes_dec_last_round_x2 (r[1], k[i]);

      dst[0] = r[0] ^= aes2_cbc_dec_permute (f, c[0]);
      dst[1] = r[1] ^= aes2_cbc_dec_permute (c[0], c[1]);
      f = c[1];

      n_blocks -= 4;
      src += 2;
      dst += 2;
    }
  else if (n_blocks >= 2)
    {
      c[0] = src[0];
      r[0] = c[0] ^ k[0];

      for (i = 1; i < rounds; i++)
	r[0] = aes_dec_round_x2 (r[0], k[i]);

      r[0] = aes_dec_last_round_x2 (r[0], k[i]);
      dst[0] = r[0] ^= aes2_cbc_dec_permute (f, c[0]);
      f = c[0];

      n_blocks -= 2;
      src += 1;
      dst += 1;
    }

  if (n_blocks > 0)
    {
      u8x16 rl = *(u8x16u *) src ^ u8x32_extract_lo (k[0]);
      for (i = 1; i < rounds; i++)
	rl = aes_dec_round (rl, u8x32_extract_lo (k[i]));
      rl = aes_dec_last_round (rl, u8x32_extract_lo (k[i]));
      *(u8x16 *) dst = rl ^ u8x32_extract_hi (f);
    }
}
#endif
#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;
  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] = { };
  u8xN r[4] = {};
  u8xN k[15][4] = {};

  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 = aes_block_load (ops[0]->iv);
	    ((u8x16 *) r)[i] = t;

	    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++)
		  ((u8x16 *) k[j])[i] = kd->encrypt_key[j];
	      }
	    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)
    {
#if defined(__VAES__) && defined(__AVX512F__)
      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 + 4, i), k[0][1]);
      r[2] = u8x64_xor3 (r[2], aes_block_load_x4 (src + 8, i), k[0][2]);
      r[3] = u8x64_xor3 (r[3], aes_block_load_x4 (src + 12, 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]);
#elif defined(__VAES__)
      r[0] = u8x32_xor3 (r[0], aes_block_load_x2 (src, i), k[0][0]);
      r[1] = u8x32_xor3 (r[1], aes_block_load_x2 (src + 2, i), k[0][1]);
      r[2] = u8x32_xor3 (r[2], aes_block_load_x2 (src + 4, i), k[0][2]);
      r[3] = u8x32_xor3 (r[3], aes_block_load_x2 (src + 6, i), k[0][3]);

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

      aes_block_store_x2 (dst, i, r[0]);
      aes_block_store_x2 (dst + 2, i, r[1]);
      aes_block_store_x2 (dst + 4, i, r[2]);
      aes_block_store_x2 (dst + 6, 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:
#if defined(__VAES__) && defined(__AVX512F__)
  aes4_cbc_dec (kd->decrypt_key, (u8x64u *) op->src, (u8x64u *) op->dst,
		(u8x16u *) op->iv, op->len, rounds);
#elif defined(__VAES__)
  aes2_cbc_dec (kd->decrypt_key, (u8x32u *) op->src, (u8x32u *) 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 defined(__VAES__) && defined(__AVX512F__)
      kd->decrypt_key[i] = u8x64_splat_u8x16 (d[i]);
#elif defined(__VAES__)
      kd->decrypt_key[i] = u8x32_splat_u8x16 (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 *
#if defined(__VAES__) && defined(__AVX512F__)
crypto_native_aes_cbc_init_icl (vlib_main_t *vm)
#elif defined(__VAES__)
crypto_native_aes_cbc_init_adl (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;

#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 _

  return 0;
}

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