vppinfra: native AES-CTR implementation
[vpp.git] / src / plugins / crypto_native / aes_cbc.c
1 /*
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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  *------------------------------------------------------------------
16  */
17
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>
23
24 #if __GNUC__ > 4  && !__clang__ && CLIB_DEBUG == 0
25 #pragma GCC optimize ("O3")
26 #endif
27
28 #if defined(__VAES__) && defined(__AVX512F__)
29 #define u8xN              u8x64
30 #define u32xN             u32x16
31 #define u32xN_min_scalar  u32x16_min_scalar
32 #define u32xN_is_all_zero u32x16_is_all_zero
33 #define u32xN_splat       u32x16_splat
34 #elif defined(__VAES__)
35 #define u8xN              u8x32
36 #define u32xN             u32x8
37 #define u32xN_min_scalar  u32x8_min_scalar
38 #define u32xN_is_all_zero u32x8_is_all_zero
39 #define u32xN_splat       u32x8_splat
40 #else
41 #define u8xN              u8x16
42 #define u32xN             u32x4
43 #define u32xN_min_scalar  u32x4_min_scalar
44 #define u32xN_is_all_zero u32x4_is_all_zero
45 #define u32xN_splat       u32x4_splat
46 #endif
47
48 static_always_inline u32
49 aes_ops_enc_aes_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[],
50                      u32 n_ops, aes_key_size_t ks)
51 {
52   crypto_native_main_t *cm = &crypto_native_main;
53   int rounds = AES_KEY_ROUNDS (ks);
54   u8 placeholder[8192];
55   u32 i, j, count, n_left = n_ops;
56   u32xN placeholder_mask = { };
57   u32xN len = { };
58   vnet_crypto_key_index_t key_index[N_AES_BYTES];
59   u8 *src[N_AES_BYTES] = {};
60   u8 *dst[N_AES_BYTES] = {};
61   u8xN r[4] = {};
62   u8xN k[15][4] = {};
63
64   for (i = 0; i < N_AES_BYTES; i++)
65     key_index[i] = ~0;
66
67 more:
68   for (i = 0; i < N_AES_BYTES; i++)
69     if (len[i] == 0)
70       {
71         if (n_left == 0)
72           {
73             /* no more work to enqueue, so we are enqueueing placeholder buffer */
74             src[i] = dst[i] = placeholder;
75             len[i] = sizeof (placeholder);
76             placeholder_mask[i] = 0;
77           }
78         else
79           {
80             u8x16 t = aes_block_load (ops[0]->iv);
81             ((u8x16 *) r)[i] = t;
82
83             src[i] = ops[0]->src;
84             dst[i] = ops[0]->dst;
85             len[i] = ops[0]->len;
86             placeholder_mask[i] = ~0;
87             if (key_index[i] != ops[0]->key_index)
88               {
89                 aes_cbc_key_data_t *kd;
90                 key_index[i] = ops[0]->key_index;
91                 kd = (aes_cbc_key_data_t *) cm->key_data[key_index[i]];
92                 for (j = 0; j < rounds + 1; j++)
93                   ((u8x16 *) k[j])[i] = kd->encrypt_key[j];
94               }
95             ops[0]->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
96             n_left--;
97             ops++;
98           }
99       }
100
101   count = u32xN_min_scalar (len);
102
103   ASSERT (count % 16 == 0);
104
105   for (i = 0; i < count; i += 16)
106     {
107 #if defined(__VAES__) && defined(__AVX512F__)
108       r[0] = u8x64_xor3 (r[0], aes_block_load_x4 (src, i), k[0][0]);
109       r[1] = u8x64_xor3 (r[1], aes_block_load_x4 (src + 4, i), k[0][1]);
110       r[2] = u8x64_xor3 (r[2], aes_block_load_x4 (src + 8, i), k[0][2]);
111       r[3] = u8x64_xor3 (r[3], aes_block_load_x4 (src + 12, i), k[0][3]);
112
113       for (j = 1; j < rounds; j++)
114         {
115           r[0] = aes_enc_round_x4 (r[0], k[j][0]);
116           r[1] = aes_enc_round_x4 (r[1], k[j][1]);
117           r[2] = aes_enc_round_x4 (r[2], k[j][2]);
118           r[3] = aes_enc_round_x4 (r[3], k[j][3]);
119         }
120       r[0] = aes_enc_last_round_x4 (r[0], k[j][0]);
121       r[1] = aes_enc_last_round_x4 (r[1], k[j][1]);
122       r[2] = aes_enc_last_round_x4 (r[2], k[j][2]);
123       r[3] = aes_enc_last_round_x4 (r[3], k[j][3]);
124
125       aes_block_store_x4 (dst, i, r[0]);
126       aes_block_store_x4 (dst + 4, i, r[1]);
127       aes_block_store_x4 (dst + 8, i, r[2]);
128       aes_block_store_x4 (dst + 12, i, r[3]);
129 #elif defined(__VAES__)
130       r[0] = u8x32_xor3 (r[0], aes_block_load_x2 (src, i), k[0][0]);
131       r[1] = u8x32_xor3 (r[1], aes_block_load_x2 (src + 2, i), k[0][1]);
132       r[2] = u8x32_xor3 (r[2], aes_block_load_x2 (src + 4, i), k[0][2]);
133       r[3] = u8x32_xor3 (r[3], aes_block_load_x2 (src + 6, i), k[0][3]);
134
135       for (j = 1; j < rounds; j++)
136         {
137           r[0] = aes_enc_round_x2 (r[0], k[j][0]);
138           r[1] = aes_enc_round_x2 (r[1], k[j][1]);
139           r[2] = aes_enc_round_x2 (r[2], k[j][2]);
140           r[3] = aes_enc_round_x2 (r[3], k[j][3]);
141         }
142       r[0] = aes_enc_last_round_x2 (r[0], k[j][0]);
143       r[1] = aes_enc_last_round_x2 (r[1], k[j][1]);
144       r[2] = aes_enc_last_round_x2 (r[2], k[j][2]);
145       r[3] = aes_enc_last_round_x2 (r[3], k[j][3]);
146
147       aes_block_store_x2 (dst, i, r[0]);
148       aes_block_store_x2 (dst + 2, i, r[1]);
149       aes_block_store_x2 (dst + 4, i, r[2]);
150       aes_block_store_x2 (dst + 6, i, r[3]);
151 #else
152 #if __x86_64__
153       r[0] = u8x16_xor3 (r[0], aes_block_load (src[0] + i), k[0][0]);
154       r[1] = u8x16_xor3 (r[1], aes_block_load (src[1] + i), k[0][1]);
155       r[2] = u8x16_xor3 (r[2], aes_block_load (src[2] + i), k[0][2]);
156       r[3] = u8x16_xor3 (r[3], aes_block_load (src[3] + i), k[0][3]);
157
158       for (j = 1; j < rounds; j++)
159         {
160           r[0] = aes_enc_round_x1 (r[0], k[j][0]);
161           r[1] = aes_enc_round_x1 (r[1], k[j][1]);
162           r[2] = aes_enc_round_x1 (r[2], k[j][2]);
163           r[3] = aes_enc_round_x1 (r[3], k[j][3]);
164         }
165
166       r[0] = aes_enc_last_round_x1 (r[0], k[j][0]);
167       r[1] = aes_enc_last_round_x1 (r[1], k[j][1]);
168       r[2] = aes_enc_last_round_x1 (r[2], k[j][2]);
169       r[3] = aes_enc_last_round_x1 (r[3], k[j][3]);
170
171       aes_block_store (dst[0] + i, r[0]);
172       aes_block_store (dst[1] + i, r[1]);
173       aes_block_store (dst[2] + i, r[2]);
174       aes_block_store (dst[3] + i, r[3]);
175 #else
176       r[0] ^= aes_block_load (src[0] + i);
177       r[1] ^= aes_block_load (src[1] + i);
178       r[2] ^= aes_block_load (src[2] + i);
179       r[3] ^= aes_block_load (src[3] + i);
180       for (j = 0; j < rounds - 1; j++)
181         {
182           r[0] = vaesmcq_u8 (vaeseq_u8 (r[0], k[j][0]));
183           r[1] = vaesmcq_u8 (vaeseq_u8 (r[1], k[j][1]));
184           r[2] = vaesmcq_u8 (vaeseq_u8 (r[2], k[j][2]));
185           r[3] = vaesmcq_u8 (vaeseq_u8 (r[3], k[j][3]));
186         }
187       r[0] = vaeseq_u8 (r[0], k[j][0]) ^ k[rounds][0];
188       r[1] = vaeseq_u8 (r[1], k[j][1]) ^ k[rounds][1];
189       r[2] = vaeseq_u8 (r[2], k[j][2]) ^ k[rounds][2];
190       r[3] = vaeseq_u8 (r[3], k[j][3]) ^ k[rounds][3];
191       aes_block_store (dst[0] + i, r[0]);
192       aes_block_store (dst[1] + i, r[1]);
193       aes_block_store (dst[2] + i, r[2]);
194       aes_block_store (dst[3] + i, r[3]);
195 #endif
196 #endif
197     }
198
199   len -= u32xN_splat (count);
200
201   for (i = 0; i < N_AES_BYTES; i++)
202     {
203       src[i] += count;
204       dst[i] += count;
205     }
206
207   if (n_left > 0)
208     goto more;
209
210   if (!u32xN_is_all_zero (len & placeholder_mask))
211     goto more;
212
213   return n_ops;
214 }
215
216
217 static_always_inline u32
218 aes_ops_dec_aes_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[],
219                      u32 n_ops, aes_key_size_t ks)
220 {
221   crypto_native_main_t *cm = &crypto_native_main;
222   int rounds = AES_KEY_ROUNDS (ks);
223   vnet_crypto_op_t *op = ops[0];
224   aes_cbc_key_data_t *kd = (aes_cbc_key_data_t *) cm->key_data[op->key_index];
225   u32 n_left = n_ops;
226
227   ASSERT (n_ops >= 1);
228
229 decrypt:
230 #if defined(__VAES__) && defined(__AVX512F__)
231   aes4_cbc_dec (kd->decrypt_key, (u8x64u *) op->src, (u8x64u *) op->dst,
232                 (u8x16u *) op->iv, op->len, rounds);
233 #elif defined(__VAES__)
234   aes2_cbc_dec (kd->decrypt_key, (u8x32u *) op->src, (u8x32u *) op->dst,
235                 (u8x16u *) op->iv, op->len, rounds);
236 #else
237   aes_cbc_dec (kd->decrypt_key, (u8x16u *) op->src, (u8x16u *) op->dst,
238                (u8x16u *) op->iv, op->len, rounds);
239 #endif
240   op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
241
242   if (--n_left)
243     {
244       op += 1;
245       kd = (aes_cbc_key_data_t *) cm->key_data[op->key_index];
246       goto decrypt;
247     }
248
249   return n_ops;
250 }
251
252 #define foreach_aes_cbc_handler_type _(128) _(192) _(256)
253
254 #define _(x) \
255 static u32 aes_ops_dec_aes_cbc_##x \
256 (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
257 { return aes_ops_dec_aes_cbc (vm, ops, n_ops, AES_KEY_##x); } \
258 static u32 aes_ops_enc_aes_cbc_##x \
259 (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
260 { return aes_ops_enc_aes_cbc (vm, ops, n_ops, AES_KEY_##x); } \
261
262 foreach_aes_cbc_handler_type;
263 #undef _
264
265 static void *
266 aes_cbc_key_exp_128 (vnet_crypto_key_t *key)
267 {
268   aes_cbc_key_data_t *kd;
269   kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
270   clib_aes128_cbc_key_expand (kd, key->data);
271   return kd;
272 }
273
274 static void *
275 aes_cbc_key_exp_192 (vnet_crypto_key_t *key)
276 {
277   aes_cbc_key_data_t *kd;
278   kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
279   clib_aes192_cbc_key_expand (kd, key->data);
280   return kd;
281 }
282
283 static void *
284 aes_cbc_key_exp_256 (vnet_crypto_key_t *key)
285 {
286   aes_cbc_key_data_t *kd;
287   kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);
288   clib_aes256_cbc_key_expand (kd, key->data);
289   return kd;
290 }
291
292 #include <fcntl.h>
293
294 clib_error_t *
295 #if defined(__VAES__) && defined(__AVX512F__)
296 crypto_native_aes_cbc_init_icl (vlib_main_t *vm)
297 #elif defined(__VAES__)
298 crypto_native_aes_cbc_init_adl (vlib_main_t *vm)
299 #elif __AVX512F__
300 crypto_native_aes_cbc_init_skx (vlib_main_t * vm)
301 #elif __aarch64__
302 crypto_native_aes_cbc_init_neon (vlib_main_t * vm)
303 #elif __AVX2__
304 crypto_native_aes_cbc_init_hsw (vlib_main_t * vm)
305 #else
306 crypto_native_aes_cbc_init_slm (vlib_main_t * vm)
307 #endif
308 {
309   crypto_native_main_t *cm = &crypto_native_main;
310
311 #define _(x) \
312   vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index, \
313                                     VNET_CRYPTO_OP_AES_##x##_CBC_ENC, \
314                                     aes_ops_enc_aes_cbc_##x); \
315   vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index, \
316                                     VNET_CRYPTO_OP_AES_##x##_CBC_DEC, \
317                                     aes_ops_dec_aes_cbc_##x); \
318   cm->key_fn[VNET_CRYPTO_ALG_AES_##x##_CBC] = aes_cbc_key_exp_##x;
319   foreach_aes_cbc_handler_type;
320 #undef _
321
322   return 0;
323 }
324
325 /*
326  * fd.io coding-style-patch-verification: ON
327  *
328  * Local Variables:
329  * eval: (c-set-style "gnu")
330  * End:
331  */