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_cbc.h>
24 #if __GNUC__ > 4 && !__clang__ && CLIB_DEBUG == 0
25 #pragma GCC optimize ("O3")
28 #if defined(__VAES__) && defined(__AVX512F__)
32 #define u32xN_min_scalar u32x16_min_scalar
33 #define u32xN_is_all_zero u32x16_is_all_zero
34 #define u32xN_splat u32x16_splat
35 #elif defined(__VAES__)
39 #define u32xN_min_scalar u32x8_min_scalar
40 #define u32xN_is_all_zero u32x8_is_all_zero
41 #define u32xN_splat u32x8_splat
46 #define u32xN_min_scalar u32x4_min_scalar
47 #define u32xN_is_all_zero u32x4_is_all_zero
48 #define u32xN_splat u32x4_splat
51 static_always_inline u32
52 aes_ops_enc_aes_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[],
53 u32 n_ops, aes_key_size_t ks)
55 crypto_native_main_t *cm = &crypto_native_main;
56 int rounds = AES_KEY_ROUNDS (ks);
58 u32 i, j, count, n_left = n_ops;
59 u32xN placeholder_mask = { };
61 vnet_crypto_key_index_t key_index[N];
67 for (i = 0; i < N; i++)
71 for (i = 0; i < N; i++)
76 /* no more work to enqueue, so we are enqueueing placeholder buffer */
77 src[i] = dst[i] = placeholder;
78 len[i] = sizeof (placeholder);
79 placeholder_mask[i] = 0;
83 u8x16 t = aes_block_load (ops[0]->iv);
89 placeholder_mask[i] = ~0;
90 if (key_index[i] != ops[0]->key_index)
92 aes_cbc_key_data_t *kd;
93 key_index[i] = ops[0]->key_index;
94 kd = (aes_cbc_key_data_t *) cm->key_data[key_index[i]];
95 for (j = 0; j < rounds + 1; j++)
96 ((u8x16 *) k[j])[i] = kd->encrypt_key[j];
98 ops[0]->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
104 count = u32xN_min_scalar (len);
106 ASSERT (count % 16 == 0);
108 for (i = 0; i < count; i += 16)
110 #if defined(__VAES__) && defined(__AVX512F__)
111 r[0] = u8x64_xor3 (r[0], aes_block_load_x4 (src, i), k[0][0]);
112 r[1] = u8x64_xor3 (r[1], aes_block_load_x4 (src + 4, i), k[0][1]);
113 r[2] = u8x64_xor3 (r[2], aes_block_load_x4 (src + 8, i), k[0][2]);
114 r[3] = u8x64_xor3 (r[3], aes_block_load_x4 (src + 12, i), k[0][3]);
116 for (j = 1; j < rounds; j++)
118 r[0] = aes_enc_round_x4 (r[0], k[j][0]);
119 r[1] = aes_enc_round_x4 (r[1], k[j][1]);
120 r[2] = aes_enc_round_x4 (r[2], k[j][2]);
121 r[3] = aes_enc_round_x4 (r[3], k[j][3]);
123 r[0] = aes_enc_last_round_x4 (r[0], k[j][0]);
124 r[1] = aes_enc_last_round_x4 (r[1], k[j][1]);
125 r[2] = aes_enc_last_round_x4 (r[2], k[j][2]);
126 r[3] = aes_enc_last_round_x4 (r[3], k[j][3]);
128 aes_block_store_x4 (dst, i, r[0]);
129 aes_block_store_x4 (dst + 4, i, r[1]);
130 aes_block_store_x4 (dst + 8, i, r[2]);
131 aes_block_store_x4 (dst + 12, i, r[3]);
132 #elif defined(__VAES__)
133 r[0] = u8x32_xor3 (r[0], aes_block_load_x2 (src, i), k[0][0]);
134 r[1] = u8x32_xor3 (r[1], aes_block_load_x2 (src + 2, i), k[0][1]);
135 r[2] = u8x32_xor3 (r[2], aes_block_load_x2 (src + 4, i), k[0][2]);
136 r[3] = u8x32_xor3 (r[3], aes_block_load_x2 (src + 6, i), k[0][3]);
138 for (j = 1; j < rounds; j++)
140 r[0] = aes_enc_round_x2 (r[0], k[j][0]);
141 r[1] = aes_enc_round_x2 (r[1], k[j][1]);
142 r[2] = aes_enc_round_x2 (r[2], k[j][2]);
143 r[3] = aes_enc_round_x2 (r[3], k[j][3]);
145 r[0] = aes_enc_last_round_x2 (r[0], k[j][0]);
146 r[1] = aes_enc_last_round_x2 (r[1], k[j][1]);
147 r[2] = aes_enc_last_round_x2 (r[2], k[j][2]);
148 r[3] = aes_enc_last_round_x2 (r[3], k[j][3]);
150 aes_block_store_x2 (dst, i, r[0]);
151 aes_block_store_x2 (dst + 2, i, r[1]);
152 aes_block_store_x2 (dst + 4, i, r[2]);
153 aes_block_store_x2 (dst + 6, i, r[3]);
156 r[0] = u8x16_xor3 (r[0], aes_block_load (src[0] + i), k[0][0]);
157 r[1] = u8x16_xor3 (r[1], aes_block_load (src[1] + i), k[0][1]);
158 r[2] = u8x16_xor3 (r[2], aes_block_load (src[2] + i), k[0][2]);
159 r[3] = u8x16_xor3 (r[3], aes_block_load (src[3] + i), k[0][3]);
161 for (j = 1; j < rounds; j++)
163 r[0] = aes_enc_round (r[0], k[j][0]);
164 r[1] = aes_enc_round (r[1], k[j][1]);
165 r[2] = aes_enc_round (r[2], k[j][2]);
166 r[3] = aes_enc_round (r[3], k[j][3]);
169 r[0] = aes_enc_last_round (r[0], k[j][0]);
170 r[1] = aes_enc_last_round (r[1], k[j][1]);
171 r[2] = aes_enc_last_round (r[2], k[j][2]);
172 r[3] = aes_enc_last_round (r[3], k[j][3]);
174 aes_block_store (dst[0] + i, r[0]);
175 aes_block_store (dst[1] + i, r[1]);
176 aes_block_store (dst[2] + i, r[2]);
177 aes_block_store (dst[3] + i, r[3]);
179 r[0] ^= aes_block_load (src[0] + i);
180 r[1] ^= aes_block_load (src[1] + i);
181 r[2] ^= aes_block_load (src[2] + i);
182 r[3] ^= aes_block_load (src[3] + i);
183 for (j = 0; j < rounds - 1; j++)
185 r[0] = vaesmcq_u8 (vaeseq_u8 (r[0], k[j][0]));
186 r[1] = vaesmcq_u8 (vaeseq_u8 (r[1], k[j][1]));
187 r[2] = vaesmcq_u8 (vaeseq_u8 (r[2], k[j][2]));
188 r[3] = vaesmcq_u8 (vaeseq_u8 (r[3], k[j][3]));
190 r[0] = vaeseq_u8 (r[0], k[j][0]) ^ k[rounds][0];
191 r[1] = vaeseq_u8 (r[1], k[j][1]) ^ k[rounds][1];
192 r[2] = vaeseq_u8 (r[2], k[j][2]) ^ k[rounds][2];
193 r[3] = vaeseq_u8 (r[3], k[j][3]) ^ k[rounds][3];
194 aes_block_store (dst[0] + i, r[0]);
195 aes_block_store (dst[1] + i, r[1]);
196 aes_block_store (dst[2] + i, r[2]);
197 aes_block_store (dst[3] + i, r[3]);
202 len -= u32xN_splat (count);
204 for (i = 0; i < N; i++)
213 if (!u32xN_is_all_zero (len & placeholder_mask))
220 static_always_inline u32
221 aes_ops_dec_aes_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[],
222 u32 n_ops, aes_key_size_t ks)
224 crypto_native_main_t *cm = &crypto_native_main;
225 int rounds = AES_KEY_ROUNDS (ks);
226 vnet_crypto_op_t *op = ops[0];
227 aes_cbc_key_data_t *kd = (aes_cbc_key_data_t *) cm->key_data[op->key_index];
233 #if defined(__VAES__) && defined(__AVX512F__)
234 aes4_cbc_dec (kd->decrypt_key, (u8x64u *) op->src, (u8x64u *) op->dst,
235 (u8x16u *) op->iv, op->len, rounds);
236 #elif defined(__VAES__)
237 aes2_cbc_dec (kd->decrypt_key, (u8x32u *) op->src, (u8x32u *) op->dst,
238 (u8x16u *) op->iv, op->len, rounds);
240 aes_cbc_dec (kd->decrypt_key, (u8x16u *) op->src, (u8x16u *) op->dst,
241 (u8x16u *) op->iv, op->len, rounds);
243 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
248 kd = (aes_cbc_key_data_t *) cm->key_data[op->key_index];
255 #define foreach_aes_cbc_handler_type _(128) _(192) _(256)
258 static u32 aes_ops_dec_aes_cbc_##x \
259 (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
260 { return aes_ops_dec_aes_cbc (vm, ops, n_ops, AES_KEY_##x); } \
261 static u32 aes_ops_enc_aes_cbc_##x \
262 (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
263 { return aes_ops_enc_aes_cbc (vm, ops, n_ops, AES_KEY_##x); } \
265 foreach_aes_cbc_handler_type;
269 aes_cbc_key_exp_128 (vnet_crypto_key_t *key)
271 aes_cbc_key_data_t *kd;
272 kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
273 clib_aes128_cbc_key_expand (kd, key->data);
278 aes_cbc_key_exp_192 (vnet_crypto_key_t *key)
280 aes_cbc_key_data_t *kd;
281 kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
282 clib_aes192_cbc_key_expand (kd, key->data);
287 aes_cbc_key_exp_256 (vnet_crypto_key_t *key)
289 aes_cbc_key_data_t *kd;
290 kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
291 clib_aes256_cbc_key_expand (kd, key->data);
298 #if defined(__VAES__) && defined(__AVX512F__)
299 crypto_native_aes_cbc_init_icl (vlib_main_t *vm)
300 #elif defined(__VAES__)
301 crypto_native_aes_cbc_init_adl (vlib_main_t *vm)
303 crypto_native_aes_cbc_init_skx (vlib_main_t * vm)
305 crypto_native_aes_cbc_init_neon (vlib_main_t * vm)
307 crypto_native_aes_cbc_init_hsw (vlib_main_t * vm)
309 crypto_native_aes_cbc_init_slm (vlib_main_t * vm)
312 crypto_native_main_t *cm = &crypto_native_main;
315 vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index, \
316 VNET_CRYPTO_OP_AES_##x##_CBC_ENC, \
317 aes_ops_enc_aes_cbc_##x); \
318 vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index, \
319 VNET_CRYPTO_OP_AES_##x##_CBC_DEC, \
320 aes_ops_dec_aes_cbc_##x); \
321 cm->key_fn[VNET_CRYPTO_ALG_AES_##x##_CBC] = aes_cbc_key_exp_##x;
322 foreach_aes_cbc_handler_type;
329 * fd.io coding-style-patch-verification: ON
332 * eval: (c-set-style "gnu")