1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2024 Cisco Systems, Inc.
6 #include <vnet/plugin/plugin.h>
7 #include <vnet/crypto/crypto.h>
8 #include <crypto_native/crypto_native.h>
9 #include <vppinfra/crypto/aes_ctr.h>
11 #if __GNUC__ > 4 && !__clang__ && CLIB_DEBUG == 0
12 #pragma GCC optimize("O3")
15 static_always_inline u32
16 aes_ops_aes_ctr (vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops,
17 vnet_crypto_op_chunk_t *chunks, aes_key_size_t ks,
20 crypto_native_main_t *cm = &crypto_native_main;
21 vnet_crypto_op_t *op = ops[0];
22 aes_ctr_key_data_t *kd;
27 kd = (aes_ctr_key_data_t *) cm->key_data[op->key_index];
29 clib_aes_ctr_init (&ctx, kd, op->iv, ks);
30 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
32 vnet_crypto_op_chunk_t *chp = chunks + op->chunk_index;
33 for (int j = 0; j < op->n_chunks; j++, chp++)
34 clib_aes_ctr_transform (&ctx, chp->src, chp->dst, chp->len, ks);
37 clib_aes_ctr_transform (&ctx, op->src, op->dst, op->len, ks);
39 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
50 static_always_inline void *
51 aes_ctr_key_exp (vnet_crypto_key_t *key, aes_key_size_t ks)
53 aes_ctr_key_data_t *kd;
55 kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
57 clib_aes_ctr_key_expand (kd, key->data, ks);
62 #define foreach_aes_ctr_handler_type _ (128) _ (192) _ (256)
65 static u32 aes_ops_aes_ctr_##x (vlib_main_t *vm, vnet_crypto_op_t *ops[], \
68 return aes_ops_aes_ctr (vm, ops, n_ops, 0, AES_KEY_##x, 0); \
70 static u32 aes_ops_aes_ctr_##x##_chained ( \
71 vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, \
74 return aes_ops_aes_ctr (vm, ops, n_ops, chunks, AES_KEY_##x, 1); \
76 static void *aes_ctr_key_exp_##x (vnet_crypto_key_t *key) \
78 return aes_ctr_key_exp (key, AES_KEY_##x); \
81 foreach_aes_ctr_handler_type;
85 #if defined(__VAES__) && defined(__AVX512F__)
86 crypto_native_aes_ctr_init_icl (vlib_main_t *vm)
87 #elif defined(__VAES__)
88 crypto_native_aes_ctr_init_adl (vlib_main_t *vm)
90 crypto_native_aes_ctr_init_skx (vlib_main_t *vm)
92 crypto_native_aes_ctr_init_hsw (vlib_main_t *vm)
94 crypto_native_aes_ctr_init_neon (vlib_main_t *vm)
96 crypto_native_aes_ctr_init_slm (vlib_main_t *vm)
99 crypto_native_main_t *cm = &crypto_native_main;
102 vnet_crypto_register_ops_handlers ( \
103 vm, cm->crypto_engine_index, VNET_CRYPTO_OP_AES_##x##_CTR_ENC, \
104 aes_ops_aes_ctr_##x, aes_ops_aes_ctr_##x##_chained); \
105 vnet_crypto_register_ops_handlers ( \
106 vm, cm->crypto_engine_index, VNET_CRYPTO_OP_AES_##x##_CTR_DEC, \
107 aes_ops_aes_ctr_##x, aes_ops_aes_ctr_##x##_chained); \
108 cm->key_fn[VNET_CRYPTO_ALG_AES_##x##_CTR] = aes_ctr_key_exp_##x;
109 foreach_aes_ctr_handler_type;