2 *------------------------------------------------------------------
3 * Copyright (c) 2019 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
18 #include <vlib/vlib.h>
19 #include <vnet/plugin/plugin.h>
20 #include <vnet/crypto/crypto.h>
21 #include <crypto_native/crypto_native.h>
22 #include <vppinfra/crypto/aes_gcm.h>
24 #if __GNUC__ > 4 && !__clang__ && CLIB_DEBUG == 0
25 #pragma GCC optimize("O3")
28 static_always_inline u32
29 aes_ops_enc_aes_gcm (vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops,
32 crypto_native_main_t *cm = &crypto_native_main;
33 vnet_crypto_op_t *op = ops[0];
34 aes_gcm_key_data_t *kd;
38 kd = (aes_gcm_key_data_t *) cm->key_data[op->key_index];
39 aes_gcm (op->src, op->dst, op->aad, (u8 *) op->iv, op->tag, op->len,
40 op->aad_len, op->tag_len, kd, AES_KEY_ROUNDS (ks),
42 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
53 static_always_inline u32
54 aes_ops_dec_aes_gcm (vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops,
57 crypto_native_main_t *cm = &crypto_native_main;
58 vnet_crypto_op_t *op = ops[0];
59 aes_gcm_key_data_t *kd;
64 kd = (aes_gcm_key_data_t *) cm->key_data[op->key_index];
65 rv = aes_gcm (op->src, op->dst, op->aad, (u8 *) op->iv, op->tag, op->len,
66 op->aad_len, op->tag_len, kd, AES_KEY_ROUNDS (ks),
71 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
75 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
88 static_always_inline void *
89 aes_gcm_key_exp (vnet_crypto_key_t *key, aes_key_size_t ks)
91 aes_gcm_key_data_t *kd;
93 kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
95 clib_aes_gcm_key_expand (kd, key->data, ks);
100 #define foreach_aes_gcm_handler_type _ (128) _ (192) _ (256)
103 static u32 aes_ops_dec_aes_gcm_##x (vlib_main_t *vm, \
104 vnet_crypto_op_t *ops[], u32 n_ops) \
106 return aes_ops_dec_aes_gcm (vm, ops, n_ops, AES_KEY_##x); \
108 static u32 aes_ops_enc_aes_gcm_##x (vlib_main_t *vm, \
109 vnet_crypto_op_t *ops[], u32 n_ops) \
111 return aes_ops_enc_aes_gcm (vm, ops, n_ops, AES_KEY_##x); \
113 static void *aes_gcm_key_exp_##x (vnet_crypto_key_t *key) \
115 return aes_gcm_key_exp (key, AES_KEY_##x); \
118 foreach_aes_gcm_handler_type;
122 #if defined(__VAES__) && defined(__AVX512F__)
123 crypto_native_aes_gcm_init_icl (vlib_main_t *vm)
124 #elif defined(__VAES__)
125 crypto_native_aes_gcm_init_adl (vlib_main_t *vm)
127 crypto_native_aes_gcm_init_skx (vlib_main_t *vm)
129 crypto_native_aes_gcm_init_hsw (vlib_main_t *vm)
131 crypto_native_aes_gcm_init_neon (vlib_main_t *vm)
133 crypto_native_aes_gcm_init_slm (vlib_main_t *vm)
136 crypto_native_main_t *cm = &crypto_native_main;
139 vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index, \
140 VNET_CRYPTO_OP_AES_##x##_GCM_ENC, \
141 aes_ops_enc_aes_gcm_##x); \
142 vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index, \
143 VNET_CRYPTO_OP_AES_##x##_GCM_DEC, \
144 aes_ops_dec_aes_gcm_##x); \
145 cm->key_fn[VNET_CRYPTO_ALG_AES_##x##_GCM] = aes_gcm_key_exp_##x;
146 foreach_aes_gcm_handler_type;
152 * fd.io coding-style-patch-verification: ON
155 * eval: (c-set-style "gnu")