summaryrefslogtreecommitdiffstats
path: root/src/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/crypto_native/aes.h480
-rw-r--r--src/plugins/crypto_native/aes_cbc.c459
-rw-r--r--src/plugins/crypto_native/aes_gcm.c1164
-rw-r--r--src/plugins/crypto_native/ghash.h419
4 files changed, 72 insertions, 2450 deletions
diff --git a/src/plugins/crypto_native/aes.h b/src/plugins/crypto_native/aes.h
deleted file mode 100644
index 40fe681e2b7..00000000000
--- a/src/plugins/crypto_native/aes.h
+++ /dev/null
@@ -1,480 +0,0 @@
-/*
- *------------------------------------------------------------------
- * Copyright (c) 2020 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.
- *------------------------------------------------------------------
- */
-
-#ifndef __aesni_h__
-#define __aesni_h__
-
-typedef enum
-{
- AES_KEY_128 = 0,
- AES_KEY_192 = 1,
- AES_KEY_256 = 2,
-} aes_key_size_t;
-
-#define AES_KEY_ROUNDS(x) (10 + x * 2)
-#define AES_KEY_BYTES(x) (16 + x * 8)
-
-static const u8x16 byte_mask_scale = {
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
-};
-
-static_always_inline u8x16
-aes_block_load (u8 * p)
-{
- return *(u8x16u *) p;
-}
-
-static_always_inline u8x16
-aes_enc_round (u8x16 a, u8x16 k)
-{
-#if defined (__AES__)
- return (u8x16) _mm_aesenc_si128 ((__m128i) a, (__m128i) k);
-#elif defined (__ARM_FEATURE_CRYPTO)
- return vaesmcq_u8 (vaeseq_u8 (a, u8x16_splat (0))) ^ k;
-#endif
-}
-
-#if defined(__VAES__) && defined(__AVX512F__)
-static_always_inline u8x64
-aes_enc_round_x4 (u8x64 a, u8x64 k)
-{
- return (u8x64) _mm512_aesenc_epi128 ((__m512i) a, (__m512i) k);
-}
-
-static_always_inline u8x64
-aes_enc_last_round_x4 (u8x64 a, u8x64 k)
-{
- return (u8x64) _mm512_aesenclast_epi128 ((__m512i) a, (__m512i) k);
-}
-
-static_always_inline u8x64
-aes_dec_round_x4 (u8x64 a, u8x64 k)
-{
- return (u8x64) _mm512_aesdec_epi128 ((__m512i) a, (__m512i) k);
-}
-
-static_always_inline u8x64
-aes_dec_last_round_x4 (u8x64 a, u8x64 k)
-{
- return (u8x64) _mm512_aesdeclast_epi128 ((__m512i) a, (__m512i) k);
-}
-#endif
-
-#ifdef __VAES__
-static_always_inline u8x32
-aes_enc_round_x2 (u8x32 a, u8x32 k)
-{
- return (u8x32) _mm256_aesenc_epi128 ((__m256i) a, (__m256i) k);
-}
-
-static_always_inline u8x32
-aes_enc_last_round_x2 (u8x32 a, u8x32 k)
-{
- return (u8x32) _mm256_aesenclast_epi128 ((__m256i) a, (__m256i) k);
-}
-
-static_always_inline u8x32
-aes_dec_round_x2 (u8x32 a, u8x32 k)
-{
- return (u8x32) _mm256_aesdec_epi128 ((__m256i) a, (__m256i) k);
-}
-
-static_always_inline u8x32
-aes_dec_last_round_x2 (u8x32 a, u8x32 k)
-{
- return (u8x32) _mm256_aesdeclast_epi128 ((__m256i) a, (__m256i) k);
-}
-#endif
-
-static_always_inline u8x16
-aes_enc_last_round (u8x16 a, u8x16 k)
-{
-#if defined (__AES__)
- return (u8x16) _mm_aesenclast_si128 ((__m128i) a, (__m128i) k);
-#elif defined (__ARM_FEATURE_CRYPTO)
- return vaeseq_u8 (a, u8x16_splat (0)) ^ k;
-#endif
-}
-
-#ifdef __x86_64__
-
-static_always_inline u8x16
-aes_dec_round (u8x16 a, u8x16 k)
-{
- return (u8x16) _mm_aesdec_si128 ((__m128i) a, (__m128i) k);
-}
-
-static_always_inline u8x16
-aes_dec_last_round (u8x16 a, u8x16 k)
-{
- return (u8x16) _mm_aesdeclast_si128 ((__m128i) a, (__m128i) k);
-}
-#endif
-
-static_always_inline void
-aes_block_store (u8 * p, u8x16 r)
-{
- *(u8x16u *) p = r;
-}
-
-static_always_inline u8x16
-aes_byte_mask (u8x16 x, u8 n_bytes)
-{
- return x & (u8x16_splat (n_bytes) > byte_mask_scale);
-}
-
-static_always_inline u8x16
-aes_load_partial (u8x16u * p, int n_bytes)
-{
- ASSERT (n_bytes <= 16);
-#ifdef __AVX512F__
- __m128i zero = { };
- return (u8x16) _mm_mask_loadu_epi8 (zero, (1 << n_bytes) - 1, p);
-#else
- u8x16 v = {};
- CLIB_ASSUME (n_bytes < 16);
- clib_memcpy_fast (&v, p, n_bytes);
- return v;
-#endif
-}
-
-static_always_inline void
-aes_store_partial (void *p, u8x16 r, int n_bytes)
-{
-#if __aarch64__
- clib_memcpy_fast (p, &r, n_bytes);
-#else
-#ifdef __AVX512F__
- _mm_mask_storeu_epi8 (p, (1 << n_bytes) - 1, (__m128i) r);
-#else
- u8x16 mask = u8x16_splat (n_bytes) > byte_mask_scale;
- _mm_maskmoveu_si128 ((__m128i) r, (__m128i) mask, p);
-#endif
-#endif
-}
-
-
-static_always_inline u8x16
-aes_encrypt_block (u8x16 block, const u8x16 * round_keys, aes_key_size_t ks)
-{
- int rounds = AES_KEY_ROUNDS (ks);
- block ^= round_keys[0];
- for (int i = 1; i < rounds; i += 1)
- block = aes_enc_round (block, round_keys[i]);
- return aes_enc_last_round (block, round_keys[rounds]);
-}
-
-static_always_inline u8x16
-aes_inv_mix_column (u8x16 a)
-{
-#if defined (__AES__)
- return (u8x16) _mm_aesimc_si128 ((__m128i) a);
-#elif defined (__ARM_FEATURE_CRYPTO)
- return vaesimcq_u8 (a);
-#endif
-}
-
-#ifdef __x86_64__
-#define aes_keygen_assist(a, b) \
- (u8x16) _mm_aeskeygenassist_si128((__m128i) a, b)
-
-/* AES-NI based AES key expansion based on code samples from
- Intel(r) Advanced Encryption Standard (AES) New Instructions White Paper
- (323641-001) */
-
-static_always_inline void
-aes128_key_assist (u8x16 * rk, u8x16 r)
-{
- u8x16 t = rk[-1];
- t ^= u8x16_word_shift_left (t, 4);
- t ^= u8x16_word_shift_left (t, 4);
- t ^= u8x16_word_shift_left (t, 4);
- rk[0] = t ^ (u8x16) u32x4_shuffle ((u32x4) r, 3, 3, 3, 3);
-}
-
-static_always_inline void
-aes128_key_expand (u8x16 *rk, u8x16u const *k)
-{
- rk[0] = k[0];
- aes128_key_assist (rk + 1, aes_keygen_assist (rk[0], 0x01));
- aes128_key_assist (rk + 2, aes_keygen_assist (rk[1], 0x02));
- aes128_key_assist (rk + 3, aes_keygen_assist (rk[2], 0x04));
- aes128_key_assist (rk + 4, aes_keygen_assist (rk[3], 0x08));
- aes128_key_assist (rk + 5, aes_keygen_assist (rk[4], 0x10));
- aes128_key_assist (rk + 6, aes_keygen_assist (rk[5], 0x20));
- aes128_key_assist (rk + 7, aes_keygen_assist (rk[6], 0x40));
- aes128_key_assist (rk + 8, aes_keygen_assist (rk[7], 0x80));
- aes128_key_assist (rk + 9, aes_keygen_assist (rk[8], 0x1b));
- aes128_key_assist (rk + 10, aes_keygen_assist (rk[9], 0x36));
-}
-
-static_always_inline void
-aes192_key_assist (u8x16 * r1, u8x16 * r2, u8x16 key_assist)
-{
- u8x16 t;
- r1[0] ^= t = u8x16_word_shift_left (r1[0], 4);
- r1[0] ^= t = u8x16_word_shift_left (t, 4);
- r1[0] ^= u8x16_word_shift_left (t, 4);
- r1[0] ^= (u8x16) _mm_shuffle_epi32 ((__m128i) key_assist, 0x55);
- r2[0] ^= u8x16_word_shift_left (r2[0], 4);
- r2[0] ^= (u8x16) _mm_shuffle_epi32 ((__m128i) r1[0], 0xff);
-}
-
-static_always_inline void
-aes192_key_expand (u8x16 * rk, u8x16u const *k)
-{
- u8x16 r1, r2;
-
- rk[0] = r1 = k[0];
- /* *INDENT-OFF* */
- rk[1] = r2 = (u8x16) (u64x2) { *(u64 *) (k + 1), 0 };
- /* *INDENT-ON* */
-
- aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x1));
- rk[1] = (u8x16) _mm_shuffle_pd ((__m128d) rk[1], (__m128d) r1, 0);
- rk[2] = (u8x16) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1);
-
- aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x2));
- rk[3] = r1;
- rk[4] = r2;
-
- aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x4));
- rk[4] = (u8x16) _mm_shuffle_pd ((__m128d) rk[4], (__m128d) r1, 0);
- rk[5] = (u8x16) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1);
-
- aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x8));
- rk[6] = r1;
- rk[7] = r2;
-
- aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x10));
- rk[7] = (u8x16) _mm_shuffle_pd ((__m128d) rk[7], (__m128d) r1, 0);
- rk[8] = (u8x16) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1);
-
- aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x20));
- rk[9] = r1;
- rk[10] = r2;
-
- aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x40));
- rk[10] = (u8x16) _mm_shuffle_pd ((__m128d) rk[10], (__m128d) r1, 0);
- rk[11] = (u8x16) _mm_shuffle_pd ((__m128d) r1, (__m128d) r2, 1);
-
- aes192_key_assist (&r1, &r2, aes_keygen_assist (r2, 0x80));
- rk[12] = r1;
-}
-
-static_always_inline void
-aes256_key_assist (u8x16 * rk, int i, u8x16 key_assist)
-{
- u8x16 r, t;
- rk += i;
- r = rk[-2];
- r ^= t = u8x16_word_shift_left (r, 4);
- r ^= t = u8x16_word_shift_left (t, 4);
- r ^= u8x16_word_shift_left (t, 4);
- r ^= (u8x16) u32x4_shuffle ((u32x4) key_assist, 3, 3, 3, 3);
- rk[0] = r;
-
- if (i >= 14)
- return;
-
- key_assist = aes_keygen_assist (rk[0], 0x0);
- r = rk[-1];
- r ^= t = u8x16_word_shift_left (r, 4);
- r ^= t = u8x16_word_shift_left (t, 4);
- r ^= u8x16_word_shift_left (t, 4);
- r ^= (u8x16) u32x4_shuffle ((u32x4) key_assist, 2, 2, 2, 2);
- rk[1] = r;
-}
-
-static_always_inline void
-aes256_key_expand (u8x16 * rk, u8x16u const *k)
-{
- rk[0] = k[0];
- rk[1] = k[1];
- aes256_key_assist (rk, 2, aes_keygen_assist (rk[1], 0x01));
- aes256_key_assist (rk, 4, aes_keygen_assist (rk[3], 0x02));
- aes256_key_assist (rk, 6, aes_keygen_assist (rk[5], 0x04));
- aes256_key_assist (rk, 8, aes_keygen_assist (rk[7], 0x08));
- aes256_key_assist (rk, 10, aes_keygen_assist (rk[9], 0x10));
- aes256_key_assist (rk, 12, aes_keygen_assist (rk[11], 0x20));
- aes256_key_assist (rk, 14, aes_keygen_assist (rk[13], 0x40));
-}
-#endif
-
-#ifdef __aarch64__
-
-static const u8x16 aese_prep_mask1 =
- { 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12 };
-static const u8x16 aese_prep_mask2 =
- { 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15 };
-
-static_always_inline void
-aes128_key_expand_round_neon (u8x16 * rk, u32 rcon)
-{
- u8x16 r, t, last_round = rk[-1], z = { };
- r = vqtbl1q_u8 (last_round, aese_prep_mask1);
- r = vaeseq_u8 (r, z);
- r ^= (u8x16) vdupq_n_u32 (rcon);
- r ^= last_round;
- r ^= t = vextq_u8 (z, last_round, 12);
- r ^= t = vextq_u8 (z, t, 12);
- r ^= vextq_u8 (z, t, 12);
- rk[0] = r;
-}
-
-static_always_inline void
-aes128_key_expand (u8x16 *rk, u8x16u const *k)
-{
- rk[0] = k[0];
- aes128_key_expand_round_neon (rk + 1, 0x01);
- aes128_key_expand_round_neon (rk + 2, 0x02);
- aes128_key_expand_round_neon (rk + 3, 0x04);
- aes128_key_expand_round_neon (rk + 4, 0x08);
- aes128_key_expand_round_neon (rk + 5, 0x10);
- aes128_key_expand_round_neon (rk + 6, 0x20);
- aes128_key_expand_round_neon (rk + 7, 0x40);
- aes128_key_expand_round_neon (rk + 8, 0x80);
- aes128_key_expand_round_neon (rk + 9, 0x1b);
- aes128_key_expand_round_neon (rk + 10, 0x36);
-}
-
-static_always_inline void
-aes192_key_expand_round_neon (u8x8 * rk, u32 rcon)
-{
- u8x8 r, last_round = rk[-1], z = { };
- u8x16 r2, z2 = { };
-
- r2 = (u8x16) vdupq_lane_u64 ((uint64x1_t) last_round, 0);
- r2 = vqtbl1q_u8 (r2, aese_prep_mask1);
- r2 = vaeseq_u8 (r2, z2);
- r2 ^= (u8x16) vdupq_n_u32 (rcon);
-
- r = (u8x8) vdup_laneq_u64 ((u64x2) r2, 0);
- r ^= rk[-3];
- r ^= vext_u8 (z, rk[-3], 4);
- rk[0] = r;
-
- r = rk[-2] ^ vext_u8 (r, z, 4);
- r ^= vext_u8 (z, r, 4);
- rk[1] = r;
-
- if (rcon == 0x80)
- return;
-
- r = rk[-1] ^ vext_u8 (r, z, 4);
- r ^= vext_u8 (z, r, 4);
- rk[2] = r;
-}
-
-static_always_inline void
-aes192_key_expand (u8x16 * ek, const u8x16u * k)
-{
- u8x8 *rk = (u8x8 *) ek;
- ek[0] = k[0];
- rk[2] = *(u8x8u *) (k + 1);
- aes192_key_expand_round_neon (rk + 3, 0x01);
- aes192_key_expand_round_neon (rk + 6, 0x02);
- aes192_key_expand_round_neon (rk + 9, 0x04);
- aes192_key_expand_round_neon (rk + 12, 0x08);
- aes192_key_expand_round_neon (rk + 15, 0x10);
- aes192_key_expand_round_neon (rk + 18, 0x20);
- aes192_key_expand_round_neon (rk + 21, 0x40);
- aes192_key_expand_round_neon (rk + 24, 0x80);
-}
-
-
-static_always_inline void
-aes256_key_expand_round_neon (u8x16 * rk, u32 rcon)
-{
- u8x16 r, t, z = { };
-
- r = vqtbl1q_u8 (rk[-1], rcon ? aese_prep_mask1 : aese_prep_mask2);
- r = vaeseq_u8 (r, z);
- if (rcon)
- r ^= (u8x16) vdupq_n_u32 (rcon);
- r ^= rk[-2];
- r ^= t = vextq_u8 (z, rk[-2], 12);
- r ^= t = vextq_u8 (z, t, 12);
- r ^= vextq_u8 (z, t, 12);
- rk[0] = r;
-}
-
-static_always_inline void
-aes256_key_expand (u8x16 *rk, u8x16u const *k)
-{
- rk[0] = k[0];
- rk[1] = k[1];
- aes256_key_expand_round_neon (rk + 2, 0x01);
- aes256_key_expand_round_neon (rk + 3, 0);
- aes256_key_expand_round_neon (rk + 4, 0x02);
- aes256_key_expand_round_neon (rk + 5, 0);
- aes256_key_expand_round_neon (rk + 6, 0x04);
- aes256_key_expand_round_neon (rk + 7, 0);
- aes256_key_expand_round_neon (rk + 8, 0x08);
- aes256_key_expand_round_neon (rk + 9, 0);
- aes256_key_expand_round_neon (rk + 10, 0x10);
- aes256_key_expand_round_neon (rk + 11, 0);
- aes256_key_expand_round_neon (rk + 12, 0x20);
- aes256_key_expand_round_neon (rk + 13, 0);
- aes256_key_expand_round_neon (rk + 14, 0x40);
-}
-
-#endif
-
-static_always_inline void
-aes_key_expand (u8x16 * key_schedule, u8 const *key, aes_key_size_t ks)
-{
- switch (ks)
- {
- case AES_KEY_128:
- aes128_key_expand (key_schedule, (u8x16u const *) key);
- break;
- case AES_KEY_192:
- aes192_key_expand (key_schedule, (u8x16u const *) key);
- break;
- case AES_KEY_256:
- aes256_key_expand (key_schedule, (u8x16u const *) key);
- break;
- }
-}
-
-static_always_inline void
-aes_key_enc_to_dec (u8x16 * ke, u8x16 * kd, aes_key_size_t ks)
-{
- int rounds = AES_KEY_ROUNDS (ks);
-
- kd[rounds] = ke[0];
- kd[0] = ke[rounds];
-
- for (int i = 1; i < (rounds / 2); i++)
- {
- kd[rounds - i] = aes_inv_mix_column (ke[i]);
- kd[i] = aes_inv_mix_column (ke[rounds - i]);
- }
-
- kd[rounds / 2] = aes_inv_mix_column (ke[rounds / 2]);
-}
-
-#endif /* __aesni_h__ */
-
-/*
- * fd.io coding-style-patch-verification: ON
- *
- * Local Variables:
- * eval: (c-set-style "gnu")
- * End:
- */
diff --git a/src/plugins/crypto_native/aes_cbc.c b/src/plugins/crypto_native/aes_cbc.c
index 02d96b31c79..1f21dc149fa 100644
--- a/src/plugins/crypto_native/aes_cbc.c
+++ b/src/plugins/crypto_native/aes_cbc.c
@@ -19,7 +19,7 @@
#include <vnet/plugin/plugin.h>
#include <vnet/crypto/crypto.h>
#include <crypto_native/crypto_native.h>
-#include <crypto_native/aes.h>
+#include <vppinfra/crypto/aes_cbc.h>
#if __GNUC__ > 4 && !__clang__ && CLIB_DEBUG == 0
#pragma GCC optimize ("O3")
@@ -48,412 +48,6 @@
#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)
@@ -658,28 +252,6 @@ 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) \
@@ -689,12 +261,37 @@ static u32 aes_ops_dec_aes_cbc_##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 _
+static void *
+aes_cbc_key_exp_128 (vnet_crypto_key_t *key)
+{
+ aes_cbc_key_data_t *kd;
+ kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
+ clib_aes128_cbc_key_expand (kd, key->data);
+ return kd;
+}
+
+static void *
+aes_cbc_key_exp_192 (vnet_crypto_key_t *key)
+{
+ aes_cbc_key_data_t *kd;
+ kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
+ clib_aes192_cbc_key_expand (kd, key->data);
+ return kd;
+}
+
+static void *
+aes_cbc_key_exp_256 (vnet_crypto_key_t *key)
+{
+ aes_cbc_key_data_t *kd;
+ kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
+ clib_aes256_cbc_key_expand (kd, key->data);
+ return kd;
+}
+
#include <fcntl.h>
clib_error_t *
diff --git a/src/plugins/crypto_native/aes_gcm.c b/src/plugins/crypto_native/aes_gcm.c
index c13665e3fb1..6589d411975 100644
--- a/src/plugins/crypto_native/aes_gcm.c
+++ b/src/plugins/crypto_native/aes_gcm.c
@@ -19,1098 +19,26 @@
#include <vnet/plugin/plugin.h>
#include <vnet/crypto/crypto.h>
#include <crypto_native/crypto_native.h>
-#include <crypto_native/aes.h>
-#include <crypto_native/ghash.h>
+#include <vppinfra/crypto/aes_gcm.h>
-#if __GNUC__ > 4 && !__clang__ && CLIB_DEBUG == 0
-#pragma GCC optimize ("O3")
+#if __GNUC__ > 4 && !__clang__ && CLIB_DEBUG == 0
+#pragma GCC optimize("O3")
#endif
-#if defined(__VAES__) && defined(__AVX512F__)
-#define NUM_HI 32
-#else
-#define NUM_HI 8
-#endif
-
-typedef struct
-{
- /* pre-calculated hash key values */
- const u8x16 Hi[NUM_HI];
- /* extracted AES key */
- const u8x16 Ke[15];
-#if defined(__VAES__) && defined(__AVX512F__)
- const u8x64 Ke4[15];
-#endif
-} aes_gcm_key_data_t;
-
-typedef struct
-{
- u32 counter;
- union
- {
- u32x4 Y;
- u32x16 Y4;
- };
-} aes_gcm_counter_t;
-
-typedef enum
-{
- AES_GCM_F_WITH_GHASH = (1 << 0),
- AES_GCM_F_LAST_ROUND = (1 << 1),
- AES_GCM_F_ENCRYPT = (1 << 2),
- AES_GCM_F_DECRYPT = (1 << 3),
-} aes_gcm_flags_t;
-
-static const u32x4 ctr_inv_1 = { 0, 0, 0, 1 << 24 };
-
-static_always_inline void
-aes_gcm_enc_first_round (u8x16 * r, aes_gcm_counter_t * ctr, u8x16 k,
- int n_blocks)
-{
- if (PREDICT_TRUE ((u8) ctr->counter < (256 - 2 * n_blocks)))
- {
- for (int i = 0; i < n_blocks; i++)
- {
- r[i] = k ^ (u8x16) ctr->Y;
- ctr->Y += ctr_inv_1;
- }
- ctr->counter += n_blocks;
- }
- else
- {
- for (int i = 0; i < n_blocks; i++)
- {
- r[i] = k ^ (u8x16) ctr->Y;
- ctr->counter++;
- ctr->Y[3] = clib_host_to_net_u32 (ctr->counter + 1);
- }
- }
-}
-
-static_always_inline void
-aes_gcm_enc_round (u8x16 * r, u8x16 k, int n_blocks)
-{
- for (int i = 0; i < n_blocks; i++)
- r[i] = aes_enc_round (r[i], k);
-}
-
-static_always_inline void
-aes_gcm_enc_last_round (u8x16 * r, u8x16 * d, u8x16 const *k,
- int rounds, int n_blocks)
-{
-
- /* additional ronuds for AES-192 and AES-256 */
- for (int i = 10; i < rounds; i++)
- aes_gcm_enc_round (r, k[i], n_blocks);
-
- for (int i = 0; i < n_blocks; i++)
- d[i] ^= aes_enc_last_round (r[i], k[rounds]);
-}
-
-static_always_inline u8x16
-aes_gcm_ghash_blocks (u8x16 T, aes_gcm_key_data_t * kd,
- u8x16u * in, int n_blocks)
-{
- ghash_data_t _gd, *gd = &_gd;
- u8x16 *Hi = (u8x16 *) kd->Hi + NUM_HI - n_blocks;
- ghash_mul_first (gd, u8x16_reflect (in[0]) ^ T, Hi[0]);
- for (int i = 1; i < n_blocks; i++)
- ghash_mul_next (gd, u8x16_reflect ((in[i])), Hi[i]);
- ghash_reduce (gd);
- ghash_reduce2 (gd);
- return ghash_final (gd);
-}
-
-static_always_inline u8x16
-aes_gcm_ghash (u8x16 T, aes_gcm_key_data_t * kd, u8x16u * in, u32 n_left)
-{
-
- while (n_left >= 128)
- {
- T = aes_gcm_ghash_blocks (T, kd, in, 8);
- n_left -= 128;
- in += 8;
- }
-
- if (n_left >= 64)
- {
- T = aes_gcm_ghash_blocks (T, kd, in, 4);
- n_left -= 64;
- in += 4;
- }
-
- if (n_left >= 32)
- {
- T = aes_gcm_ghash_blocks (T, kd, in, 2);
- n_left -= 32;
- in += 2;
- }
-
- if (n_left >= 16)
- {
- T = aes_gcm_ghash_blocks (T, kd, in, 1);
- n_left -= 16;
- in += 1;
- }
-
- if (n_left)
- {
- u8x16 r = aes_load_partial (in, n_left);
- T = ghash_mul (u8x16_reflect (r) ^ T, kd->Hi[NUM_HI - 1]);
- }
- return T;
-}
-
-static_always_inline __clib_unused u8x16
-aes_gcm_calc (u8x16 T, aes_gcm_key_data_t *kd, u8x16 *d,
- aes_gcm_counter_t *ctr, u8x16u *inv, u8x16u *outv, int rounds,
- int n, int last_block_bytes, aes_gcm_flags_t f)
-{
- u8x16 r[n];
- ghash_data_t _gd = { }, *gd = &_gd;
- const u8x16 *rk = (u8x16 *) kd->Ke;
- int ghash_blocks = (f & AES_GCM_F_ENCRYPT) ? 4 : n, gc = 1;
- u8x16 *Hi = (u8x16 *) kd->Hi + NUM_HI - ghash_blocks;
-
- clib_prefetch_load (inv + 4);
-
- /* AES rounds 0 and 1 */
- aes_gcm_enc_first_round (r, ctr, rk[0], n);
- aes_gcm_enc_round (r, rk[1], n);
-
- /* load data - decrypt round */
- if (f & AES_GCM_F_DECRYPT)
- {
- for (int i = 0; i < n - ((f & AES_GCM_F_LAST_ROUND) != 0); i++)
- d[i] = inv[i];
-
- if (f & AES_GCM_F_LAST_ROUND)
- d[n - 1] = aes_load_partial (inv + n - 1, last_block_bytes);
- }
-
- /* GHASH multiply block 1 */
- if (f & AES_GCM_F_WITH_GHASH)
- ghash_mul_first (gd, u8x16_reflect (d[0]) ^ T, Hi[0]);
-
- /* AES rounds 2 and 3 */
- aes_gcm_enc_round (r, rk[2], n);
- aes_gcm_enc_round (r, rk[3], n);
-
- /* GHASH multiply block 2 */
- if ((f & AES_GCM_F_WITH_GHASH) && gc++ < ghash_blocks)
- ghash_mul_next (gd, u8x16_reflect (d[1]), Hi[1]);
-
- /* AES rounds 4 and 5 */
- aes_gcm_enc_round (r, rk[4], n);
- aes_gcm_enc_round (r, rk[5], n);
-
- /* GHASH multiply block 3 */
- if ((f & AES_GCM_F_WITH_GHASH) && gc++ < ghash_blocks)
- ghash_mul_next (gd, u8x16_reflect (d[2]), Hi[2]);
-
- /* AES rounds 6 and 7 */
- aes_gcm_enc_round (r, rk[6], n);
- aes_gcm_enc_round (r, rk[7], n);
-
- /* GHASH multiply block 4 */
- if ((f & AES_GCM_F_WITH_GHASH) && gc++ < ghash_blocks)
- ghash_mul_next (gd, u8x16_reflect (d[3]), Hi[3]);
-
- /* AES rounds 8 and 9 */
- aes_gcm_enc_round (r, rk[8], n);
- aes_gcm_enc_round (r, rk[9], n);
-
- /* GHASH reduce 1st step */
- if (f & AES_GCM_F_WITH_GHASH)
- ghash_reduce (gd);
-
- /* load data - encrypt round */
- if (f & AES_GCM_F_ENCRYPT)
- {
- for (int i = 0; i < n - ((f & AES_GCM_F_LAST_ROUND) != 0); i++)
- d[i] = inv[i];
-
- if (f & AES_GCM_F_LAST_ROUND)
- d[n - 1] = aes_load_partial (inv + n - 1, last_block_bytes);
- }
-
- /* GHASH reduce 2nd step */
- if (f & AES_GCM_F_WITH_GHASH)
- ghash_reduce2 (gd);
-
- /* AES last round(s) */
- aes_gcm_enc_last_round (r, d, rk, rounds, n);
-
- /* store data */
- for (int i = 0; i < n - ((f & AES_GCM_F_LAST_ROUND) != 0); i++)
- outv[i] = d[i];
-
- if (f & AES_GCM_F_LAST_ROUND)
- aes_store_partial (outv + n - 1, d[n - 1], last_block_bytes);
-
- /* GHASH final step */
- if (f & AES_GCM_F_WITH_GHASH)
- T = ghash_final (gd);
-
- return T;
-}
-
-static_always_inline __clib_unused u8x16
-aes_gcm_calc_double (u8x16 T, aes_gcm_key_data_t *kd, u8x16 *d,
- aes_gcm_counter_t *ctr, u8x16u *inv, u8x16u *outv,
- int rounds, aes_gcm_flags_t f)
-{
- u8x16 r[4];
- ghash_data_t _gd, *gd = &_gd;
- const u8x16 *rk = (u8x16 *) kd->Ke;
- u8x16 *Hi = (u8x16 *) kd->Hi + NUM_HI - 8;
-
- /* AES rounds 0 and 1 */
- aes_gcm_enc_first_round (r, ctr, rk[0], 4);
- aes_gcm_enc_round (r, rk[1], 4);
-
- /* load 4 blocks of data - decrypt round */
- if (f & AES_GCM_F_DECRYPT)
- {
- d[0] = inv[0];
- d[1] = inv[1];
- d[2] = inv[2];
- d[3] = inv[3];
- }
-
- /* GHASH multiply block 0 */
- ghash_mul_first (gd, u8x16_reflect (d[0]) ^ T, Hi[0]);
-
- /* AES rounds 2 and 3 */
- aes_gcm_enc_round (r, rk[2], 4);
- aes_gcm_enc_round (r, rk[3], 4);
-
- /* GHASH multiply block 1 */
- ghash_mul_next (gd, u8x16_reflect (d[1]), Hi[1]);
-
- /* AES rounds 4 and 5 */
- aes_gcm_enc_round (r, rk[4], 4);
- aes_gcm_enc_round (r, rk[5], 4);
-
- /* GHASH multiply block 2 */
- ghash_mul_next (gd, u8x16_reflect (d[2]), Hi[2]);
-
- /* AES rounds 6 and 7 */
- aes_gcm_enc_round (r, rk[6], 4);
- aes_gcm_enc_round (r, rk[7], 4);
-
- /* GHASH multiply block 3 */
- ghash_mul_next (gd, u8x16_reflect (d[3]), Hi[3]);
-
- /* AES rounds 8 and 9 */
- aes_gcm_enc_round (r, rk[8], 4);
- aes_gcm_enc_round (r, rk[9], 4);
-
- /* load 4 blocks of data - encrypt round */
- if (f & AES_GCM_F_ENCRYPT)
- {
- d[0] = inv[0];
- d[1] = inv[1];
- d[2] = inv[2];
- d[3] = inv[3];
- }
-
- /* AES last round(s) */
- aes_gcm_enc_last_round (r, d, rk, rounds, 4);
-
- /* store 4 blocks of data */
- outv[0] = d[0];
- outv[1] = d[1];
- outv[2] = d[2];
- outv[3] = d[3];
-
- /* load next 4 blocks of data data - decrypt round */
- if (f & AES_GCM_F_DECRYPT)
- {
- d[0] = inv[4];
- d[1] = inv[5];
- d[2] = inv[6];
- d[3] = inv[7];
- }
-
- /* GHASH multiply block 4 */
- ghash_mul_next (gd, u8x16_reflect (d[0]), Hi[4]);
-
- /* AES rounds 0, 1 and 2 */
- aes_gcm_enc_first_round (r, ctr, rk[0], 4);
- aes_gcm_enc_round (r, rk[1], 4);
- aes_gcm_enc_round (r, rk[2], 4);
-
- /* GHASH multiply block 5 */
- ghash_mul_next (gd, u8x16_reflect (d[1]), Hi[5]);
-
- /* AES rounds 3 and 4 */
- aes_gcm_enc_round (r, rk[3], 4);
- aes_gcm_enc_round (r, rk[4], 4);
-
- /* GHASH multiply block 6 */
- ghash_mul_next (gd, u8x16_reflect (d[2]), Hi[6]);
-
- /* AES rounds 5 and 6 */
- aes_gcm_enc_round (r, rk[5], 4);
- aes_gcm_enc_round (r, rk[6], 4);
-
- /* GHASH multiply block 7 */
- ghash_mul_next (gd, u8x16_reflect (d[3]), Hi[7]);
-
- /* AES rounds 7 and 8 */
- aes_gcm_enc_round (r, rk[7], 4);
- aes_gcm_enc_round (r, rk[8], 4);
-
- /* GHASH reduce 1st step */
- ghash_reduce (gd);
-
- /* AES round 9 */
- aes_gcm_enc_round (r, rk[9], 4);
-
- /* load data - encrypt round */
- if (f & AES_GCM_F_ENCRYPT)
- {
- d[0] = inv[4];
- d[1] = inv[5];
- d[2] = inv[6];
- d[3] = inv[7];
- }
-
- /* GHASH reduce 2nd step */
- ghash_reduce2 (gd);
-
- /* AES last round(s) */
- aes_gcm_enc_last_round (r, d, rk, rounds, 4);
-
- /* store data */
- outv[4] = d[0];
- outv[5] = d[1];
- outv[6] = d[2];
- outv[7] = d[3];
-
- /* GHASH final step */
- return ghash_final (gd);
-}
-
-static_always_inline __clib_unused u8x16
-aes_gcm_ghash_last (u8x16 T, aes_gcm_key_data_t *kd, u8x16 *d, int n_blocks,
- int n_bytes)
-{
- ghash_data_t _gd, *gd = &_gd;
- u8x16 *Hi = (u8x16 *) kd->Hi + NUM_HI - n_blocks;
-
- if (n_bytes)
- d[n_blocks - 1] = aes_byte_mask (d[n_blocks - 1], n_bytes);
-
- ghash_mul_first (gd, u8x16_reflect (d[0]) ^ T, Hi[0]);
- if (n_blocks > 1)
- ghash_mul_next (gd, u8x16_reflect (d[1]), Hi[1]);
- if (n_blocks > 2)
- ghash_mul_next (gd, u8x16_reflect (d[2]), Hi[2]);
- if (n_blocks > 3)
- ghash_mul_next (gd, u8x16_reflect (d[3]), Hi[3]);
- ghash_reduce (gd);
- ghash_reduce2 (gd);
- return ghash_final (gd);
-}
-
-#if defined(__VAES__) && defined(__AVX512F__)
-static const u32x16 ctr_inv_1234 = {
- 0, 0, 0, 1 << 24, 0, 0, 0, 2 << 24, 0, 0, 0, 3 << 24, 0, 0, 0, 4 << 24,
-};
-
-static const u32x16 ctr_inv_4444 = {
- 0, 0, 0, 4 << 24, 0, 0, 0, 4 << 24, 0, 0, 0, 4 << 24, 0, 0, 0, 4 << 24
-};
-
-static const u32x16 ctr_1234 = {
- 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0,
-};
-
-static_always_inline void
-aes4_gcm_enc_first_round (u8x64 * r, aes_gcm_counter_t * ctr, u8x64 k, int n)
-{
- u8 last_byte = (u8) ctr->counter;
- int i = 0;
-
- /* As counter is stored in network byte order for performance reasons we
- are incrementing least significant byte only except in case where we
- overlow. As we are processing four 512-blocks in parallel except the
- last round, overflow can happen only when n == 4 */
-
- if (n == 4)
- for (; i < 2; i++)
- {
- r[i] = k ^ (u8x64) ctr->Y4;
- ctr->Y4 += ctr_inv_4444;
- }
-
- if (n == 4 && PREDICT_TRUE (last_byte == 241))
- {
- u32x16 Yc, Yr = (u32x16) u8x64_reflect_u8x16 ((u8x64) ctr->Y4);
-
- for (; i < n; i++)
- {
- r[i] = k ^ (u8x64) ctr->Y4;
- Yc = u32x16_splat (ctr->counter + 4 * (i + 1)) + ctr_1234;
- Yr = (u32x16) u32x16_mask_blend (Yr, Yc, 0x1111);
- ctr->Y4 = (u32x16) u8x64_reflect_u8x16 ((u8x64) Yr);
- }
- }
- else
- {
- for (; i < n; i++)
- {
- r[i] = k ^ (u8x64) ctr->Y4;
- ctr->Y4 += ctr_inv_4444;
- }
- }
- ctr->counter += n * 4;
-}
-
-static_always_inline void
-aes4_gcm_enc_round (u8x64 * r, u8x64 k, int n_blocks)
-{
- for (int i = 0; i < n_blocks; i++)
- r[i] = aes_enc_round_x4 (r[i], k);
-}
-
-static_always_inline void
-aes4_gcm_enc_last_round (u8x64 * r, u8x64 * d, u8x64 const *k,
- int rounds, int n_blocks)
-{
-
- /* additional ronuds for AES-192 and AES-256 */
- for (int i = 10; i < rounds; i++)
- aes4_gcm_enc_round (r, k[i], n_blocks);
-
- for (int i = 0; i < n_blocks; i++)
- d[i] ^= aes_enc_last_round_x4 (r[i], k[rounds]);
-}
-
-static_always_inline u8x16
-aes4_gcm_calc (u8x16 T, aes_gcm_key_data_t * kd, u8x64 * d,
- aes_gcm_counter_t * ctr, u8x16u * in, u8x16u * out,
- int rounds, int n, int last_4block_bytes, aes_gcm_flags_t f)
-{
- ghash4_data_t _gd, *gd = &_gd;
- const u8x64 *rk = (u8x64 *) kd->Ke4;
- int i, ghash_blocks, gc = 1;
- u8x64u *Hi4, *inv = (u8x64u *) in, *outv = (u8x64u *) out;
- u8x64 r[4];
- u64 byte_mask = _bextr_u64 (-1LL, 0, last_4block_bytes);
-
- if (f & AES_GCM_F_ENCRYPT)
- {
- /* during encryption we either hash four 512-bit blocks from previous
- round or we don't hash at all */
- ghash_blocks = 4;
- Hi4 = (u8x64u *) (kd->Hi + NUM_HI - ghash_blocks * 4);
- }
- else
- {
- /* during deccryption we hash 1..4 512-bit blocks from current round */
- ghash_blocks = n;
- int n_128bit_blocks = n * 4;
- /* if this is last round of decryption, we may have less than 4
- 128-bit blocks in the last 512-bit data block, so we need to adjust
- Hi4 pointer accordingly */
- if (f & AES_GCM_F_LAST_ROUND)
- n_128bit_blocks += ((last_4block_bytes + 15) >> 4) - 4;
- Hi4 = (u8x64u *) (kd->Hi + NUM_HI - n_128bit_blocks);
- }
-
- /* AES rounds 0 and 1 */
- aes4_gcm_enc_first_round (r, ctr, rk[0], n);
- aes4_gcm_enc_round (r, rk[1], n);
-
- /* load 4 blocks of data - decrypt round */
- if (f & AES_GCM_F_DECRYPT)
- {
- for (i = 0; i < n - ((f & AES_GCM_F_LAST_ROUND) != 0); i++)
- d[i] = inv[i];
-
- if (f & AES_GCM_F_LAST_ROUND)
- d[i] = u8x64_mask_load (u8x64_splat (0), inv + i, byte_mask);
- }
-
- /* GHASH multiply block 0 */
- if (f & AES_GCM_F_WITH_GHASH)
- ghash4_mul_first (gd, u8x64_reflect_u8x16 (d[0]) ^
- u8x64_insert_u8x16 (u8x64_splat (0), T, 0), Hi4[0]);
-
- /* AES rounds 2 and 3 */
- aes4_gcm_enc_round (r, rk[2], n);
- aes4_gcm_enc_round (r, rk[3], n);
-
- /* GHASH multiply block 1 */
- if ((f & AES_GCM_F_WITH_GHASH) && gc++ < ghash_blocks)
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[1]), Hi4[1]);
-
- /* AES rounds 4 and 5 */
- aes4_gcm_enc_round (r, rk[4], n);
- aes4_gcm_enc_round (r, rk[5], n);
-
- /* GHASH multiply block 2 */
- if ((f & AES_GCM_F_WITH_GHASH) && gc++ < ghash_blocks)
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[2]), Hi4[2]);
-
- /* AES rounds 6 and 7 */
- aes4_gcm_enc_round (r, rk[6], n);
- aes4_gcm_enc_round (r, rk[7], n);
-
- /* GHASH multiply block 3 */
- if ((f & AES_GCM_F_WITH_GHASH) && gc++ < ghash_blocks)
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[3]), Hi4[3]);
-
- /* load 4 blocks of data - decrypt round */
- if (f & AES_GCM_F_ENCRYPT)
- {
- for (i = 0; i < n - ((f & AES_GCM_F_LAST_ROUND) != 0); i++)
- d[i] = inv[i];
-
- if (f & AES_GCM_F_LAST_ROUND)
- d[i] = u8x64_mask_load (u8x64_splat (0), inv + i, byte_mask);
- }
-
- /* AES rounds 8 and 9 */
- aes4_gcm_enc_round (r, rk[8], n);
- aes4_gcm_enc_round (r, rk[9], n);
-
- /* AES last round(s) */
- aes4_gcm_enc_last_round (r, d, rk, rounds, n);
-
- /* store 4 blocks of data */
- for (i = 0; i < n - ((f & AES_GCM_F_LAST_ROUND) != 0); i++)
- outv[i] = d[i];
-
- if (f & AES_GCM_F_LAST_ROUND)
- u8x64_mask_store (d[i], outv + i, byte_mask);
-
- /* GHASH reduce 1st step */
- ghash4_reduce (gd);
-
- /* GHASH reduce 2nd step */
- ghash4_reduce2 (gd);
-
- /* GHASH final step */
- return ghash4_final (gd);
-}
-
-static_always_inline u8x16
-aes4_gcm_calc_double (u8x16 T, aes_gcm_key_data_t * kd, u8x64 * d,
- aes_gcm_counter_t * ctr, u8x16u * in, u8x16u * out,
- int rounds, aes_gcm_flags_t f)
-{
- u8x64 r[4];
- ghash4_data_t _gd, *gd = &_gd;
- const u8x64 *rk = (u8x64 *) kd->Ke4;
- u8x64 *Hi4 = (u8x64 *) (kd->Hi + NUM_HI - 32);
- u8x64u *inv = (u8x64u *) in, *outv = (u8x64u *) out;
-
- /* AES rounds 0 and 1 */
- aes4_gcm_enc_first_round (r, ctr, rk[0], 4);
- aes4_gcm_enc_round (r, rk[1], 4);
-
- /* load 4 blocks of data - decrypt round */
- if (f & AES_GCM_F_DECRYPT)
- for (int i = 0; i < 4; i++)
- d[i] = inv[i];
-
- /* GHASH multiply block 0 */
- ghash4_mul_first (gd, u8x64_reflect_u8x16 (d[0]) ^
- u8x64_insert_u8x16 (u8x64_splat (0), T, 0), Hi4[0]);
-
- /* AES rounds 2 and 3 */
- aes4_gcm_enc_round (r, rk[2], 4);
- aes4_gcm_enc_round (r, rk[3], 4);
-
- /* GHASH multiply block 1 */
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[1]), Hi4[1]);
-
- /* AES rounds 4 and 5 */
- aes4_gcm_enc_round (r, rk[4], 4);
- aes4_gcm_enc_round (r, rk[5], 4);
-
- /* GHASH multiply block 2 */
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[2]), Hi4[2]);
-
- /* AES rounds 6 and 7 */
- aes4_gcm_enc_round (r, rk[6], 4);
- aes4_gcm_enc_round (r, rk[7], 4);
-
- /* GHASH multiply block 3 */
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[3]), Hi4[3]);
-
- /* AES rounds 8 and 9 */
- aes4_gcm_enc_round (r, rk[8], 4);
- aes4_gcm_enc_round (r, rk[9], 4);
-
- /* load 4 blocks of data - encrypt round */
- if (f & AES_GCM_F_ENCRYPT)
- for (int i = 0; i < 4; i++)
- d[i] = inv[i];
-
- /* AES last round(s) */
- aes4_gcm_enc_last_round (r, d, rk, rounds, 4);
-
- /* store 4 blocks of data */
- for (int i = 0; i < 4; i++)
- outv[i] = d[i];
-
- /* load 4 blocks of data - decrypt round */
- if (f & AES_GCM_F_DECRYPT)
- for (int i = 0; i < 4; i++)
- d[i] = inv[i + 4];
-
- /* GHASH multiply block 3 */
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[0]), Hi4[4]);
-
- /* AES rounds 0 and 1 */
- aes4_gcm_enc_first_round (r, ctr, rk[0], 4);
- aes4_gcm_enc_round (r, rk[1], 4);
-
- /* GHASH multiply block 5 */
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[1]), Hi4[5]);
-
- /* AES rounds 2 and 3 */
- aes4_gcm_enc_round (r, rk[2], 4);
- aes4_gcm_enc_round (r, rk[3], 4);
-
- /* GHASH multiply block 6 */
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[2]), Hi4[6]);
-
- /* AES rounds 4 and 5 */
- aes4_gcm_enc_round (r, rk[4], 4);
- aes4_gcm_enc_round (r, rk[5], 4);
-
- /* GHASH multiply block 7 */
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[3]), Hi4[7]);
-
- /* AES rounds 6 and 7 */
- aes4_gcm_enc_round (r, rk[6], 4);
- aes4_gcm_enc_round (r, rk[7], 4);
-
- /* GHASH reduce 1st step */
- ghash4_reduce (gd);
-
- /* AES rounds 8 and 9 */
- aes4_gcm_enc_round (r, rk[8], 4);
- aes4_gcm_enc_round (r, rk[9], 4);
-
- /* GHASH reduce 2nd step */
- ghash4_reduce2 (gd);
-
- /* load 4 blocks of data - encrypt round */
- if (f & AES_GCM_F_ENCRYPT)
- for (int i = 0; i < 4; i++)
- d[i] = inv[i + 4];
-
- /* AES last round(s) */
- aes4_gcm_enc_last_round (r, d, rk, rounds, 4);
-
- /* store 4 blocks of data */
- for (int i = 0; i < 4; i++)
- outv[i + 4] = d[i];
-
- /* GHASH final step */
- return ghash4_final (gd);
-}
-
-static_always_inline u8x16
-aes4_gcm_ghash_last (u8x16 T, aes_gcm_key_data_t * kd, u8x64 * d,
- int n, int last_4block_bytes)
-{
- ghash4_data_t _gd, *gd = &_gd;
- u8x64u *Hi4;
- int n_128bit_blocks;
- u64 byte_mask = _bextr_u64 (-1LL, 0, last_4block_bytes);
- n_128bit_blocks = (n - 1) * 4 + ((last_4block_bytes + 15) >> 4);
- Hi4 = (u8x64u *) (kd->Hi + NUM_HI - n_128bit_blocks);
-
- d[n - 1] = u8x64_mask_blend (u8x64_splat (0), d[n - 1], byte_mask);
- ghash4_mul_first (gd, u8x64_reflect_u8x16 (d[0]) ^
- u8x64_insert_u8x16 (u8x64_splat (0), T, 0), Hi4[0]);
- if (n > 1)
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[1]), Hi4[1]);
- if (n > 2)
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[2]), Hi4[2]);
- if (n > 3)
- ghash4_mul_next (gd, u8x64_reflect_u8x16 (d[3]), Hi4[3]);
- ghash4_reduce (gd);
- ghash4_reduce2 (gd);
- return ghash4_final (gd);
-}
-#endif
-
-static_always_inline u8x16
-aes_gcm_enc (u8x16 T, aes_gcm_key_data_t * kd, aes_gcm_counter_t * ctr,
- u8x16u * inv, u8x16u * outv, u32 n_left, int rounds)
-{
- aes_gcm_flags_t f = AES_GCM_F_ENCRYPT;
-
- if (n_left == 0)
- return T;
-
-#if defined(__VAES__) && defined(__AVX512F__)
- u8x64 d4[4];
- if (n_left < 256)
- {
- f |= AES_GCM_F_LAST_ROUND;
- if (n_left > 192)
- {
- n_left -= 192;
- aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 4, n_left, f);
- return aes4_gcm_ghash_last (T, kd, d4, 4, n_left);
- }
- else if (n_left > 128)
- {
- n_left -= 128;
- aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 3, n_left, f);
- return aes4_gcm_ghash_last (T, kd, d4, 3, n_left);
- }
- else if (n_left > 64)
- {
- n_left -= 64;
- aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 2, n_left, f);
- return aes4_gcm_ghash_last (T, kd, d4, 2, n_left);
- }
- else
- {
- aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 1, n_left, f);
- return aes4_gcm_ghash_last (T, kd, d4, 1, n_left);
- }
- }
-
- aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 4, 0, f);
-
- /* next */
- n_left -= 256;
- outv += 16;
- inv += 16;
-
- f |= AES_GCM_F_WITH_GHASH;
-
- while (n_left >= 512)
- {
- T = aes4_gcm_calc_double (T, kd, d4, ctr, inv, outv, rounds, f);
-
- /* next */
- n_left -= 512;
- outv += 32;
- inv += 32;
- }
-
- while (n_left >= 256)
- {
- T = aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 4, 0, f);
-
- /* next */
- n_left -= 256;
- outv += 16;
- inv += 16;
- }
-
- if (n_left == 0)
- return aes4_gcm_ghash_last (T, kd, d4, 4, 64);
-
- f |= AES_GCM_F_LAST_ROUND;
-
- if (n_left > 192)
- {
- n_left -= 192;
- T = aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 4, n_left, f);
- return aes4_gcm_ghash_last (T, kd, d4, 4, n_left);
- }
-
- if (n_left > 128)
- {
- n_left -= 128;
- T = aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 3, n_left, f);
- return aes4_gcm_ghash_last (T, kd, d4, 3, n_left);
- }
-
- if (n_left > 64)
- {
- n_left -= 64;
- T = aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 2, n_left, f);
- return aes4_gcm_ghash_last (T, kd, d4, 2, n_left);
- }
-
- T = aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 1, n_left, f);
- return aes4_gcm_ghash_last (T, kd, d4, 1, n_left);
-#else
- u8x16 d[4];
- if (n_left < 64)
- {
- f |= AES_GCM_F_LAST_ROUND;
- if (n_left > 48)
- {
- n_left -= 48;
- aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 4, n_left, f);
- return aes_gcm_ghash_last (T, kd, d, 4, n_left);
- }
- else if (n_left > 32)
- {
- n_left -= 32;
- aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 3, n_left, f);
- return aes_gcm_ghash_last (T, kd, d, 3, n_left);
- }
- else if (n_left > 16)
- {
- n_left -= 16;
- aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 2, n_left, f);
- return aes_gcm_ghash_last (T, kd, d, 2, n_left);
- }
- else
- {
- aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 1, n_left, f);
- return aes_gcm_ghash_last (T, kd, d, 1, n_left);
- }
- }
-
- aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 4, 0, f);
-
- /* next */
- n_left -= 64;
- outv += 4;
- inv += 4;
-
- f |= AES_GCM_F_WITH_GHASH;
-
- while (n_left >= 128)
- {
- T = aes_gcm_calc_double (T, kd, d, ctr, inv, outv, rounds, f);
-
- /* next */
- n_left -= 128;
- outv += 8;
- inv += 8;
- }
-
- if (n_left >= 64)
- {
- T = aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 4, 0, f);
-
- /* next */
- n_left -= 64;
- outv += 4;
- inv += 4;
- }
-
- if (n_left == 0)
- return aes_gcm_ghash_last (T, kd, d, 4, 0);
-
- f |= AES_GCM_F_LAST_ROUND;
-
- if (n_left > 48)
- {
- n_left -= 48;
- T = aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 4, n_left, f);
- return aes_gcm_ghash_last (T, kd, d, 4, n_left);
- }
-
- if (n_left > 32)
- {
- n_left -= 32;
- T = aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 3, n_left, f);
- return aes_gcm_ghash_last (T, kd, d, 3, n_left);
- }
-
- if (n_left > 16)
- {
- n_left -= 16;
- T = aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 2, n_left, f);
- return aes_gcm_ghash_last (T, kd, d, 2, n_left);
- }
-
- T = aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 1, n_left, f);
- return aes_gcm_ghash_last (T, kd, d, 1, n_left);
-#endif
-}
-
-static_always_inline u8x16
-aes_gcm_dec (u8x16 T, aes_gcm_key_data_t * kd, aes_gcm_counter_t * ctr,
- u8x16u * inv, u8x16u * outv, u32 n_left, int rounds)
-{
- aes_gcm_flags_t f = AES_GCM_F_WITH_GHASH | AES_GCM_F_DECRYPT;
-#if defined(__VAES__) && defined(__AVX512F__)
- u8x64 d4[4] = { };
-
- while (n_left >= 512)
- {
- T = aes4_gcm_calc_double (T, kd, d4, ctr, inv, outv, rounds, f);
-
- /* next */
- n_left -= 512;
- outv += 32;
- inv += 32;
- }
-
- while (n_left >= 256)
- {
- T = aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 4, 0, f);
-
- /* next */
- n_left -= 256;
- outv += 16;
- inv += 16;
- }
-
- if (n_left == 0)
- return T;
-
- f |= AES_GCM_F_LAST_ROUND;
-
- if (n_left > 192)
- return aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 4,
- n_left - 192, f);
- if (n_left > 128)
- return aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 3,
- n_left - 128, f);
- if (n_left > 64)
- return aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 2,
- n_left - 64, f);
- return aes4_gcm_calc (T, kd, d4, ctr, inv, outv, rounds, 1, n_left, f);
-#else
- u8x16 d[4] = {};
- while (n_left >= 128)
- {
- T = aes_gcm_calc_double (T, kd, d, ctr, inv, outv, rounds, f);
-
- /* next */
- n_left -= 128;
- outv += 8;
- inv += 8;
- }
-
- if (n_left >= 64)
- {
- T = aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 4, 0, f);
-
- /* next */
- n_left -= 64;
- outv += 4;
- inv += 4;
- }
-
- if (n_left == 0)
- return T;
-
- f |= AES_GCM_F_LAST_ROUND;
-
- if (n_left > 48)
- return aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 4, n_left - 48, f);
-
- if (n_left > 32)
- return aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 3, n_left - 32, f);
-
- if (n_left > 16)
- return aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 2, n_left - 16, f);
-
- return aes_gcm_calc (T, kd, d, ctr, inv, outv, rounds, 1, n_left, f);
-#endif
-}
-
-static_always_inline int
-aes_gcm (u8x16u *in, u8x16u *out, u8x16u *addt, u8 *ivp, u8x16u *tag,
- u32 data_bytes, u32 aad_bytes, u8 tag_len, aes_gcm_key_data_t *kd,
- int aes_rounds, int is_encrypt)
-{
- int i;
- u8x16 r, T = { };
- vec128_t Y0 = {};
- ghash_data_t _gd, *gd = &_gd;
- aes_gcm_counter_t _ctr, *ctr = &_ctr;
-
- clib_prefetch_load (ivp);
- clib_prefetch_load (in);
- clib_prefetch_load (in + 4);
-
- /* calculate ghash for AAD - optimized for ipsec common cases */
- if (aad_bytes == 8)
- T = aes_gcm_ghash (T, kd, addt, 8);
- else if (aad_bytes == 12)
- T = aes_gcm_ghash (T, kd, addt, 12);
- else
- T = aes_gcm_ghash (T, kd, addt, aad_bytes);
-
- /* initalize counter */
- ctr->counter = 1;
- Y0.as_u64x2[0] = *(u64u *) ivp;
- Y0.as_u32x4[2] = *(u32u *) (ivp + 8);
- Y0.as_u32x4 += ctr_inv_1;
-#if defined(__VAES__) && defined(__AVX512F__)
- ctr->Y4 = u32x16_splat_u32x4 (Y0.as_u32x4) + ctr_inv_1234;
-#else
- ctr->Y = Y0.as_u32x4 + ctr_inv_1;
-#endif
-
- /* ghash and encrypt/edcrypt */
- if (is_encrypt)
- T = aes_gcm_enc (T, kd, ctr, in, out, data_bytes, aes_rounds);
- else
- T = aes_gcm_dec (T, kd, ctr, in, out, data_bytes, aes_rounds);
-
- clib_prefetch_load (tag);
-
- /* Finalize ghash - data bytes and aad bytes converted to bits */
- /* *INDENT-OFF* */
- r = (u8x16) ((u64x2) {data_bytes, aad_bytes} << 3);
- /* *INDENT-ON* */
-
- /* interleaved computation of final ghash and E(Y0, k) */
- ghash_mul_first (gd, r ^ T, kd->Hi[NUM_HI - 1]);
- r = kd->Ke[0] ^ Y0.as_u8x16;
- for (i = 1; i < 5; i += 1)
- r = aes_enc_round (r, kd->Ke[i]);
- ghash_reduce (gd);
- ghash_reduce2 (gd);
- for (; i < 9; i += 1)
- r = aes_enc_round (r, kd->Ke[i]);
- T = ghash_final (gd);
- for (; i < aes_rounds; i += 1)
- r = aes_enc_round (r, kd->Ke[i]);
- r = aes_enc_last_round (r, kd->Ke[aes_rounds]);
- T = u8x16_reflect (T) ^ r;
-
- /* tag_len 16 -> 0 */
- tag_len &= 0xf;
-
- if (is_encrypt)
- {
- /* store tag */
- if (tag_len)
- aes_store_partial (tag, T, tag_len);
- else
- tag[0] = T;
- }
- else
- {
- /* check tag */
- u16 tag_mask = tag_len ? (1 << tag_len) - 1 : 0xffff;
- if ((u8x16_msb_mask (tag[0] == T) & tag_mask) != tag_mask)
- return 0;
- }
- return 1;
-}
-
static_always_inline u32
-aes_ops_enc_aes_gcm (vlib_main_t * vm, vnet_crypto_op_t * ops[],
- u32 n_ops, aes_key_size_t ks)
+aes_ops_enc_aes_gcm (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;
vnet_crypto_op_t *op = ops[0];
aes_gcm_key_data_t *kd;
u32 n_left = n_ops;
-
next:
kd = (aes_gcm_key_data_t *) cm->key_data[op->key_index];
- aes_gcm ((u8x16u *) op->src, (u8x16u *) op->dst, (u8x16u *) op->aad,
- (u8 *) op->iv, (u8x16u *) op->tag, op->len, op->aad_len,
- op->tag_len, kd, AES_KEY_ROUNDS (ks), /* is_encrypt */ 1);
+ aes_gcm (op->src, op->dst, op->aad, (u8 *) op->iv, op->tag, op->len,
+ op->aad_len, op->tag_len, kd, AES_KEY_ROUNDS (ks),
+ AES_GCM_OP_ENCRYPT);
op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
if (--n_left)
@@ -1123,7 +51,7 @@ next:
}
static_always_inline u32
-aes_ops_dec_aes_gcm (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
+aes_ops_dec_aes_gcm (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;
@@ -1134,10 +62,9 @@ aes_ops_dec_aes_gcm (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
next:
kd = (aes_gcm_key_data_t *) cm->key_data[op->key_index];
- rv = aes_gcm ((u8x16u *) op->src, (u8x16u *) op->dst, (u8x16u *) op->aad,
- (u8 *) op->iv, (u8x16u *) op->tag, op->len, op->aad_len,
- op->tag_len, kd, AES_KEY_ROUNDS (ks),
- /* is_encrypt */ 0);
+ rv = aes_gcm (op->src, op->dst, op->aad, (u8 *) op->iv, op->tag, op->len,
+ op->aad_len, op->tag_len, kd, AES_KEY_ROUNDS (ks),
+ AES_GCM_OP_DECRYPT);
if (rv)
{
@@ -1159,39 +86,34 @@ next:
}
static_always_inline void *
-aes_gcm_key_exp (vnet_crypto_key_t * key, aes_key_size_t ks)
+aes_gcm_key_exp (vnet_crypto_key_t *key, aes_key_size_t ks)
{
aes_gcm_key_data_t *kd;
- u8x16 H;
kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
- /* expand AES key */
- aes_key_expand ((u8x16 *) kd->Ke, key->data, ks);
+ clib_aes_gcm_key_expand (kd, key->data, ks);
- /* pre-calculate H */
- H = aes_encrypt_block (u8x16_splat (0), kd->Ke, ks);
- H = u8x16_reflect (H);
- ghash_precompute (H, (u8x16 *) kd->Hi, NUM_HI);
-#if defined(__VAES__) && defined(__AVX512F__)
- u8x64 *Ke4 = (u8x64 *) kd->Ke4;
- for (int i = 0; i < AES_KEY_ROUNDS (ks) + 1; i++)
- Ke4[i] = u8x64_splat_u8x16 (kd->Ke[i]);
-#endif
return kd;
}
-#define foreach_aes_gcm_handler_type _(128) _(192) _(256)
-
-#define _(x) \
-static u32 aes_ops_dec_aes_gcm_##x \
-(vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
-{ return aes_ops_dec_aes_gcm (vm, ops, n_ops, AES_KEY_##x); } \
-static u32 aes_ops_enc_aes_gcm_##x \
-(vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
-{ return aes_ops_enc_aes_gcm (vm, ops, n_ops, AES_KEY_##x); } \
-static void * aes_gcm_key_exp_##x (vnet_crypto_key_t *key) \
-{ return aes_gcm_key_exp (key, AES_KEY_##x); }
+#define foreach_aes_gcm_handler_type _ (128) _ (192) _ (256)
+
+#define _(x) \
+ static u32 aes_ops_dec_aes_gcm_##x (vlib_main_t *vm, \
+ vnet_crypto_op_t *ops[], u32 n_ops) \
+ { \
+ return aes_ops_dec_aes_gcm (vm, ops, n_ops, AES_KEY_##x); \
+ } \
+ static u32 aes_ops_enc_aes_gcm_##x (vlib_main_t *vm, \
+ vnet_crypto_op_t *ops[], u32 n_ops) \
+ { \
+ return aes_ops_enc_aes_gcm (vm, ops, n_ops, AES_KEY_##x); \
+ } \
+ static void *aes_gcm_key_exp_##x (vnet_crypto_key_t *key) \
+ { \
+ return aes_gcm_key_exp (key, AES_KEY_##x); \
+ }
foreach_aes_gcm_handler_type;
#undef _
@@ -1199,25 +121,27 @@ foreach_aes_gcm_handler_type;
clib_error_t *
#if defined(__VAES__) && defined(__AVX512F__)
crypto_native_aes_gcm_init_icl (vlib_main_t *vm)
+#elif defined(__VAES__)
+crypto_native_aes_gcm_init_adl (vlib_main_t *vm)
#elif __AVX512F__
-crypto_native_aes_gcm_init_skx (vlib_main_t * vm)
+crypto_native_aes_gcm_init_skx (vlib_main_t *vm)
#elif __AVX2__
-crypto_native_aes_gcm_init_hsw (vlib_main_t * vm)
+crypto_native_aes_gcm_init_hsw (vlib_main_t *vm)
#elif __aarch64__
-crypto_native_aes_gcm_init_neon (vlib_main_t * vm)
+crypto_native_aes_gcm_init_neon (vlib_main_t *vm)
#else
-crypto_native_aes_gcm_init_slm (vlib_main_t * vm)
+crypto_native_aes_gcm_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##_GCM_ENC, \
- aes_ops_enc_aes_gcm_##x); \
- vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index, \
- VNET_CRYPTO_OP_AES_##x##_GCM_DEC, \
- aes_ops_dec_aes_gcm_##x); \
+#define _(x) \
+ vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index, \
+ VNET_CRYPTO_OP_AES_##x##_GCM_ENC, \
+ aes_ops_enc_aes_gcm_##x); \
+ vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index, \
+ VNET_CRYPTO_OP_AES_##x##_GCM_DEC, \
+ aes_ops_dec_aes_gcm_##x); \
cm->key_fn[VNET_CRYPTO_ALG_AES_##x##_GCM] = aes_gcm_key_exp_##x;
foreach_aes_gcm_handler_type;
#undef _
diff --git a/src/plugins/crypto_native/ghash.h b/src/plugins/crypto_native/ghash.h
deleted file mode 100644
index 5f619cfa129..00000000000
--- a/src/plugins/crypto_native/ghash.h
+++ /dev/null
@@ -1,419 +0,0 @@
-/*
- *------------------------------------------------------------------
- * 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.
- *------------------------------------------------------------------
- */
-
-/*
- *------------------------------------------------------------------
- * Copyright(c) 2018, Intel Corporation All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Intel Corporation nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES * LOSS OF USE,
- * DATA, OR PROFITS * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *------------------------------------------------------------------
- */
-
-/*
- * Based on work by: Shay Gueron, Michael E. Kounavis, Erdinc Ozturk,
- * Vinodh Gopal, James Guilford, Tomasz Kantecki
- *
- * References:
- * [1] Vinodh Gopal et. al. Optimized Galois-Counter-Mode Implementation on
- * Intel Architecture Processors. August, 2010
- * [2] Erdinc Ozturk et. al. Enabling High-Performance Galois-Counter-Mode on
- * Intel Architecture Processors. October, 2012.
- * [3] intel-ipsec-mb library, https://github.com/01org/intel-ipsec-mb.git
- *
- * Definitions:
- * GF Galois Extension Field GF(2^128) - finite field where elements are
- * represented as polynomials with coefficients in GF(2) with the
- * highest degree of 127. Polynomials are represented as 128-bit binary
- * numbers where each bit represents one coefficient.
- * e.g. polynomial x^5 + x^3 + x + 1 is represented in binary 101011.
- * H hash key (128 bit)
- * POLY irreducible polynomial x^127 + x^7 + x^2 + x + 1
- * RPOLY irreducible polynomial x^128 + x^127 + x^126 + x^121 + 1
- * + addition in GF, which equals to XOR operation
- * * multiplication in GF
- *
- * GF multiplication consists of 2 steps:
- * - carry-less multiplication of two 128-bit operands into 256-bit result
- * - reduction of 256-bit result into 128-bit with modulo POLY
- *
- * GHash is calculated on 128-bit blocks of data according to the following
- * formula:
- * GH = (GH + data) * hash_key
- *
- * To avoid bit-reflection of data, this code uses GF multipication
- * with reversed polynomial:
- * a * b * x^-127 mod RPOLY
- *
- * To improve computation speed table Hi is precomputed with powers of H',
- * where H' is calculated as H<<1 mod RPOLY.
- * This allows us to improve performance by deferring reduction. For example
- * to caclulate ghash of 4 128-bit blocks of data (b0, b1, b2, b3), we can do:
- *
- * __i128 Hi[4];
- * ghash_precompute (H, Hi, 4);
- *
- * ghash_data_t _gd, *gd = &_gd;
- * ghash_mul_first (gd, GH ^ b0, Hi[3]);
- * ghash_mul_next (gd, b1, Hi[2]);
- * ghash_mul_next (gd, b2, Hi[1]);
- * ghash_mul_next (gd, b3, Hi[0]);
- * ghash_reduce (gd);
- * ghash_reduce2 (gd);
- * GH = ghash_final (gd);
- *
- * Reduction step is split into 3 functions so it can be better interleaved
- * with other code, (i.e. with AES computation).
- */
-
-#ifndef __ghash_h__
-#define __ghash_h__
-
-static_always_inline u8x16
-gmul_lo_lo (u8x16 a, u8x16 b)
-{
-#if defined (__PCLMUL__)
- return (u8x16) _mm_clmulepi64_si128 ((__m128i) a, (__m128i) b, 0x00);
-#elif defined (__ARM_FEATURE_CRYPTO)
- return (u8x16) vmull_p64 ((poly64_t) vget_low_p64 ((poly64x2_t) a),
- (poly64_t) vget_low_p64 ((poly64x2_t) b));
-#endif
-}
-
-static_always_inline u8x16
-gmul_hi_lo (u8x16 a, u8x16 b)
-{
-#if defined (__PCLMUL__)
- return (u8x16) _mm_clmulepi64_si128 ((__m128i) a, (__m128i) b, 0x01);
-#elif defined (__ARM_FEATURE_CRYPTO)
- return (u8x16) vmull_p64 ((poly64_t) vget_high_p64 ((poly64x2_t) a),
- (poly64_t) vget_low_p64 ((poly64x2_t) b));
-#endif
-}
-
-static_always_inline u8x16
-gmul_lo_hi (u8x16 a, u8x16 b)
-{
-#if defined (__PCLMUL__)
- return (u8x16) _mm_clmulepi64_si128 ((__m128i) a, (__m128i) b, 0x10);
-#elif defined (__ARM_FEATURE_CRYPTO)
- return (u8x16) vmull_p64 ((poly64_t) vget_low_p64 ((poly64x2_t) a),
- (poly64_t) vget_high_p64 ((poly64x2_t) b));
-#endif
-}
-
-static_always_inline u8x16
-gmul_hi_hi (u8x16 a, u8x16 b)
-{
-#if defined (__PCLMUL__)
- return (u8x16) _mm_clmulepi64_si128 ((__m128i) a, (__m128i) b, 0x11);
-#elif defined (__ARM_FEATURE_CRYPTO)
- return (u8x16) vmull_high_p64 ((poly64x2_t) a, (poly64x2_t) b);
-#endif
-}
-
-typedef struct
-{
- u8x16 mid, hi, lo, tmp_lo, tmp_hi;
- int pending;
-} ghash_data_t;
-
-static const u8x16 ghash_poly = {
- 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2
-};
-
-static const u8x16 ghash_poly2 = {
- 0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2
-};
-
-static_always_inline void
-ghash_mul_first (ghash_data_t * gd, u8x16 a, u8x16 b)
-{
- /* a1 * b1 */
- gd->hi = gmul_hi_hi (a, b);
- /* a0 * b0 */
- gd->lo = gmul_lo_lo (a, b);
- /* a0 * b1 ^ a1 * b0 */
- gd->mid = (gmul_hi_lo (a, b) ^ gmul_lo_hi (a, b));
-
- /* set gd->pending to 0 so next invocation of ghash_mul_next(...) knows that
- there is no pending data in tmp_lo and tmp_hi */
- gd->pending = 0;
-}
-
-static_always_inline void
-ghash_mul_next (ghash_data_t * gd, u8x16 a, u8x16 b)
-{
- /* a1 * b1 */
- u8x16 hi = gmul_hi_hi (a, b);
- /* a0 * b0 */
- u8x16 lo = gmul_lo_lo (a, b);
-
- /* this branch will be optimized out by the compiler, and it allows us to
- reduce number of XOR operations by using ternary logic */
- if (gd->pending)
- {
- /* there is peding data from previous invocation so we can XOR */
- gd->hi = u8x16_xor3 (gd->hi, gd->tmp_hi, hi);
- gd->lo = u8x16_xor3 (gd->lo, gd->tmp_lo, lo);
- gd->pending = 0;
- }
- else
- {
- /* there is no peding data from previous invocation so we postpone XOR */
- gd->tmp_hi = hi;
- gd->tmp_lo = lo;
- gd->pending = 1;
- }
-
- /* gd->mid ^= a0 * b1 ^ a1 * b0 */
- gd->mid = u8x16_xor3 (gd->mid, gmul_hi_lo (a, b), gmul_lo_hi (a, b));
-}
-
-static_always_inline void
-ghash_reduce (ghash_data_t * gd)
-{
- u8x16 r;
-
- /* Final combination:
- gd->lo ^= gd->mid << 64
- gd->hi ^= gd->mid >> 64 */
- u8x16 midl = u8x16_word_shift_left (gd->mid, 8);
- u8x16 midr = u8x16_word_shift_right (gd->mid, 8);
-
- if (gd->pending)
- {
- gd->lo = u8x16_xor3 (gd->lo, gd->tmp_lo, midl);
- gd->hi = u8x16_xor3 (gd->hi, gd->tmp_hi, midr);
- }
- else
- {
- gd->lo ^= midl;
- gd->hi ^= midr;
- }
- r = gmul_hi_lo (ghash_poly2, gd->lo);
- gd->lo ^= u8x16_word_shift_left (r, 8);
-}
-
-static_always_inline void
-ghash_reduce2 (ghash_data_t * gd)
-{
- gd->tmp_lo = gmul_lo_lo (ghash_poly2, gd->lo);
- gd->tmp_hi = gmul_lo_hi (ghash_poly2, gd->lo);
-}
-
-static_always_inline u8x16
-ghash_final (ghash_data_t * gd)
-{
- return u8x16_xor3 (gd->hi, u8x16_word_shift_right (gd->tmp_lo, 4),
- u8x16_word_shift_left (gd->tmp_hi, 4));
-}
-
-static_always_inline u8x16
-ghash_mul (u8x16 a, u8x16 b)
-{
- ghash_data_t _gd, *gd = &_gd;
- ghash_mul_first (gd, a, b);
- ghash_reduce (gd);
- ghash_reduce2 (gd);
- return ghash_final (gd);
-}
-
-#if defined(__VPCLMULQDQ__) && defined(__AVX512F__)
-
-static const u8x64 ghash4_poly2 = {
- 0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2,
- 0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2,
- 0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2,
- 0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2,
-};
-
-typedef struct
-{
- u8x64 hi, lo, mid, tmp_lo, tmp_hi;
- int pending;
-} ghash4_data_t;
-
-static_always_inline u8x64
-gmul4_lo_lo (u8x64 a, u8x64 b)
-{
- return (u8x64) _mm512_clmulepi64_epi128 ((__m512i) a, (__m512i) b, 0x00);
-}
-
-static_always_inline u8x64
-gmul4_hi_lo (u8x64 a, u8x64 b)
-{
- return (u8x64) _mm512_clmulepi64_epi128 ((__m512i) a, (__m512i) b, 0x01);
-}
-
-static_always_inline u8x64
-gmul4_lo_hi (u8x64 a, u8x64 b)
-{
- return (u8x64) _mm512_clmulepi64_epi128 ((__m512i) a, (__m512i) b, 0x10);
-}
-
-static_always_inline u8x64
-gmul4_hi_hi (u8x64 a, u8x64 b)
-{
- return (u8x64) _mm512_clmulepi64_epi128 ((__m512i) a, (__m512i) b, 0x11);
-}
-
-
-static_always_inline void
-ghash4_mul_first (ghash4_data_t * gd, u8x64 a, u8x64 b)
-{
- gd->hi = gmul4_hi_hi (a, b);
- gd->lo = gmul4_lo_lo (a, b);
- gd->mid = (gmul4_hi_lo (a, b) ^ gmul4_lo_hi (a, b));
- gd->pending = 0;
-}
-
-static_always_inline void
-ghash4_mul_next (ghash4_data_t * gd, u8x64 a, u8x64 b)
-{
- u8x64 hi = gmul4_hi_hi (a, b);
- u8x64 lo = gmul4_lo_lo (a, b);
-
- if (gd->pending)
- {
- /* there is peding data from previous invocation so we can XOR */
- gd->hi = u8x64_xor3 (gd->hi, gd->tmp_hi, hi);
- gd->lo = u8x64_xor3 (gd->lo, gd->tmp_lo, lo);
- gd->pending = 0;
- }
- else
- {
- /* there is no peding data from previous invocation so we postpone XOR */
- gd->tmp_hi = hi;
- gd->tmp_lo = lo;
- gd->pending = 1;
- }
- gd->mid = u8x64_xor3 (gd->mid, gmul4_hi_lo (a, b), gmul4_lo_hi (a, b));
-}
-
-static_always_inline void
-ghash4_reduce (ghash4_data_t * gd)
-{
- u8x64 r;
-
- /* Final combination:
- gd->lo ^= gd->mid << 64
- gd->hi ^= gd->mid >> 64 */
-
- u8x64 midl = u8x64_word_shift_left (gd->mid, 8);
- u8x64 midr = u8x64_word_shift_right (gd->mid, 8);
-
- if (gd->pending)
- {
- gd->lo = u8x64_xor3 (gd->lo, gd->tmp_lo, midl);
- gd->hi = u8x64_xor3 (gd->hi, gd->tmp_hi, midr);
- }
- else
- {
- gd->lo ^= midl;
- gd->hi ^= midr;
- }
-
- r = gmul4_hi_lo (ghash4_poly2, gd->lo);
- gd->lo ^= u8x64_word_shift_left (r, 8);
-
-}
-
-static_always_inline void
-ghash4_reduce2 (ghash4_data_t * gd)
-{
- gd->tmp_lo = gmul4_lo_lo (ghash4_poly2, gd->lo);
- gd->tmp_hi = gmul4_lo_hi (ghash4_poly2, gd->lo);
-}
-
-static_always_inline u8x16
-ghash4_final (ghash4_data_t * gd)
-{
- u8x64 r;
- u8x32 t;
-
- r = u8x64_xor3 (gd->hi, u8x64_word_shift_right (gd->tmp_lo, 4),
- u8x64_word_shift_left (gd->tmp_hi, 4));
-
- /* horizontal XOR of 4 128-bit lanes */
- t = u8x64_extract_lo (r) ^ u8x64_extract_hi (r);
- return u8x32_extract_hi (t) ^ u8x32_extract_lo (t);
-}
-#endif
-
-static_always_inline void
-ghash_precompute (u8x16 H, u8x16 * Hi, int n)
-{
- u8x16 r8;
- u32x4 r32;
- /* calcullate H<<1 mod poly from the hash key */
- r8 = (u8x16) ((u64x2) H >> 63);
- H = (u8x16) ((u64x2) H << 1);
- H |= u8x16_word_shift_left (r8, 8);
- r32 = (u32x4) u8x16_word_shift_right (r8, 8);
-#ifdef __SSE2__
- r32 = u32x4_shuffle (r32, 0, 1, 2, 0);
-#else
- r32[3] = r32[0];
-#endif
- /* *INDENT-OFF* */
- r32 = r32 == (u32x4) {1, 0, 0, 1};
- /* *INDENT-ON* */
- Hi[n - 1] = H = H ^ ((u8x16) r32 & ghash_poly);
-
- /* calculate H^(i + 1) */
- for (int i = n - 2; i >= 0; i--)
- Hi[i] = ghash_mul (H, Hi[i + 1]);
-}
-
-#endif /* __ghash_h__ */
-
-/*
- * fd.io coding-style-patch-verification: ON
- *
- * Local Variables:
- * eval: (c-set-style "gnu")
- * End:
- */