2 * ipsecmb.c - Intel IPSec Multi-buffer library Crypto Engine
4 * Copyright (c) 2019 Cisco Systemss
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include <intel-ipsec-mb.h>
22 #include <vnet/vnet.h>
23 #include <vnet/plugin/plugin.h>
24 #include <vpp/app/version.h>
25 #include <vnet/crypto/crypto.h>
26 #include <vppinfra/cpu.h>
32 } ipsecmb_per_thread_data_t;
34 typedef struct ipsecmb_main_t_
36 ipsecmb_per_thread_data_t *per_thread_data;
39 static ipsecmb_main_t ipsecmb_main;
41 #define foreach_ipsecmb_hmac_op \
43 _(SHA256, SHA_256, sha256) \
44 _(SHA384, SHA_384, sha384) \
45 _(SHA512, SHA_512, sha512)
47 #define foreach_ipsecmb_cipher_op \
48 _(AES_128_CBC, 128, 16, 16) \
49 _(AES_192_CBC, 192, 24, 16) \
50 _(AES_256_CBC, 256, 32, 16)
53 hash_expand_keys (const MB_MGR * mgr,
57 u8 ipad[256], u8 opad[256], hash_one_block_t fn)
62 if (length > block_size)
67 memset (buf, 0x36, sizeof (buf));
68 for (i = 0; i < length; i++)
74 memset (buf, 0x5c, sizeof (buf));
76 for (i = 0; i < length; i++)
84 ipsecmb_retire_hmac_job (JOB_AES_HMAC * job, u32 * n_fail)
86 vnet_crypto_op_t *op = job->user_data;
88 if (STS_COMPLETED != job->status)
90 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
91 *n_fail = *n_fail + 1;
94 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
96 if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
98 if ((memcmp (op->digest, job->auth_tag_output, op->digest_len)))
100 *n_fail = *n_fail + 1;
101 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
105 clib_memcpy_fast (op->digest, job->auth_tag_output, op->digest_len);
108 static_always_inline u32
109 ipsecmb_ops_hmac_inline (vlib_main_t * vm,
110 const ipsecmb_per_thread_data_t * ptd,
111 vnet_crypto_op_t * ops[],
114 hash_one_block_t fn, JOB_HASH_ALG alg)
118 u8 scratch[n_ops][64];
121 * queue all the jobs first ...
123 for (i = 0; i < n_ops; i++)
125 vnet_crypto_op_t *op = ops[i];
126 u8 ipad[256], opad[256];
128 hash_expand_keys (ptd->mgr, op->key, op->key_len,
129 block_size, ipad, opad, fn);
131 job = IMB_GET_NEXT_JOB (ptd->mgr);
134 job->hash_start_src_offset_in_bytes = 0;
135 job->msg_len_to_hash_in_bytes = op->len;
137 job->auth_tag_output_len_in_bytes = op->digest_len;
138 job->auth_tag_output = scratch[i];
140 job->cipher_mode = NULL_CIPHER;
141 job->cipher_direction = DECRYPT;
142 job->chain_order = HASH_CIPHER;
144 job->aes_key_len_in_bytes = op->key_len;
146 job->u.HMAC._hashed_auth_key_xor_ipad = ipad;
147 job->u.HMAC._hashed_auth_key_xor_opad = opad;
150 job = IMB_SUBMIT_JOB (ptd->mgr);
153 ipsecmb_retire_hmac_job (job, &n_fail);
157 * .. then flush (i.e. complete) them
158 * We will have queued enough to satisfy the 'multi' buffer
160 while ((job = IMB_FLUSH_JOB (ptd->mgr)))
162 ipsecmb_retire_hmac_job (job, &n_fail);
165 return n_ops - n_fail;
169 static_always_inline u32 \
170 ipsecmb_ops_hmac_##a (vlib_main_t * vm, \
171 vnet_crypto_op_t * ops[], \
174 ipsecmb_per_thread_data_t *ptd; \
175 ipsecmb_main_t *imbm; \
177 imbm = &ipsecmb_main; \
178 ptd = vec_elt_at_index (imbm->per_thread_data, vm->thread_index); \
180 return ipsecmb_ops_hmac_inline (vm, ptd, ops, n_ops, \
182 ptd->mgr->c##_one_block, \
185 foreach_ipsecmb_hmac_op;
188 #define EXPANDED_KEY_N_BYTES (16 * 15)
191 ipsecmb_retire_cipher_job (JOB_AES_HMAC * job, u32 * n_fail)
193 vnet_crypto_op_t *op = job->user_data;
195 if (STS_COMPLETED != job->status)
197 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
198 *n_fail = *n_fail + 1;
201 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
204 static_always_inline u32
205 ipsecmb_ops_cipher_inline (vlib_main_t * vm,
206 ipsecmb_per_thread_data_t * ptd,
207 vnet_crypto_op_t * ops[],
208 u32 n_ops, u32 key_len, u32 iv_len,
209 keyexp_t fn, JOB_CIPHER_DIRECTION direction)
215 * queue all the jobs first ...
217 for (i = 0; i < n_ops; i++)
219 u8 aes_enc_key_expanded[EXPANDED_KEY_N_BYTES];
220 u8 aes_dec_key_expanded[EXPANDED_KEY_N_BYTES];
221 vnet_crypto_op_t *op = ops[i];
224 fn (op->key, aes_enc_key_expanded, aes_dec_key_expanded);
226 job = IMB_GET_NEXT_JOB (ptd->mgr);
230 job->msg_len_to_cipher_in_bytes = op->len;
231 job->cipher_start_src_offset_in_bytes = 0;
233 job->hash_alg = NULL_HASH;
234 job->cipher_mode = CBC;
235 job->cipher_direction = direction;
236 job->chain_order = (direction == ENCRYPT ? CIPHER_HASH : HASH_CIPHER);
238 if ((direction == ENCRYPT) && (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV))
241 _mm_storeu_si128 ((__m128i *) op->iv, iv);
242 ptd->cbc_iv = _mm_aesenc_si128 (iv, iv);
245 job->aes_key_len_in_bytes = key_len;
246 job->aes_enc_key_expanded = aes_enc_key_expanded;
247 job->aes_dec_key_expanded = aes_dec_key_expanded;
249 job->iv_len_in_bytes = iv_len;
253 job = IMB_SUBMIT_JOB (ptd->mgr);
256 ipsecmb_retire_cipher_job (job, &n_fail);
260 * .. then flush (i.e. complete) them
261 * We will have queued enough to satisfy the 'multi' buffer
263 while ((job = IMB_FLUSH_JOB (ptd->mgr)))
265 ipsecmb_retire_cipher_job (job, &n_fail);
268 return n_ops - n_fail;
271 #define _(a, b, c, d) \
272 static_always_inline u32 \
273 ipsecmb_ops_cipher_enc_##a (vlib_main_t * vm, \
274 vnet_crypto_op_t * ops[], \
277 ipsecmb_per_thread_data_t *ptd; \
278 ipsecmb_main_t *imbm; \
280 imbm = &ipsecmb_main; \
281 ptd = vec_elt_at_index (imbm->per_thread_data, vm->thread_index); \
283 return ipsecmb_ops_cipher_inline (vm, ptd, ops, n_ops, c, d, \
284 ptd->mgr->keyexp_##b, \
287 foreach_ipsecmb_cipher_op;
290 #define _(a, b, c, d) \
291 static_always_inline u32 \
292 ipsecmb_ops_cipher_dec_##a (vlib_main_t * vm, \
293 vnet_crypto_op_t * ops[], \
296 ipsecmb_per_thread_data_t *ptd; \
297 ipsecmb_main_t *imbm; \
299 imbm = &ipsecmb_main; \
300 ptd = vec_elt_at_index (imbm->per_thread_data, vm->thread_index); \
302 return ipsecmb_ops_cipher_inline (vm, ptd, ops, n_ops, c, d, \
303 ptd->mgr->keyexp_##b, \
306 foreach_ipsecmb_cipher_op;
310 crypto_ipsecmb_iv_init (ipsecmb_main_t * imbm)
312 ipsecmb_per_thread_data_t *ptd;
313 clib_error_t *err = 0;
316 if ((fd = open ("/dev/urandom", O_RDONLY)) < 0)
317 return clib_error_return_unix (0, "failed to open '/dev/urandom'");
319 vec_foreach (ptd, imbm->per_thread_data)
321 if (read (fd, &ptd->cbc_iv, sizeof (ptd->cbc_iv)) != sizeof (ptd->cbc_iv))
323 err = clib_error_return_unix (0, "'/dev/urandom' read failure");
331 static clib_error_t *
332 crypto_ipsecmb_init (vlib_main_t * vm)
334 ipsecmb_main_t *imbm = &ipsecmb_main;
335 ipsecmb_per_thread_data_t *ptd;
336 vlib_thread_main_t *tm = vlib_get_thread_main ();
340 if ((error = vlib_call_init_function (vm, vnet_crypto_init)))
344 * A priority that is better than OpenSSL but worse than VPP natvie
346 eidx = vnet_crypto_register_engine (vm, "ipsecmb", 80,
347 "Intel IPSEC multi-buffer");
349 vec_validate (imbm->per_thread_data, tm->n_vlib_mains - 1);
351 if (clib_cpu_supports_avx512f ())
353 vec_foreach (ptd, imbm->per_thread_data)
355 ptd->mgr = alloc_mb_mgr (0);
356 init_mb_mgr_avx512 (ptd->mgr);
359 else if (clib_cpu_supports_avx2 ())
361 vec_foreach (ptd, imbm->per_thread_data)
363 ptd->mgr = alloc_mb_mgr (0);
364 init_mb_mgr_avx2 (ptd->mgr);
369 vec_foreach (ptd, imbm->per_thread_data)
371 ptd->mgr = alloc_mb_mgr (0);
372 init_mb_mgr_sse (ptd->mgr);
376 if (clib_cpu_supports_x86_aes () && (error = crypto_ipsecmb_iv_init (imbm)))
381 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
382 ipsecmb_ops_hmac_##a); \
384 foreach_ipsecmb_hmac_op;
386 #define _(a, b, c, d) \
387 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
388 ipsecmb_ops_cipher_enc_##a); \
390 foreach_ipsecmb_cipher_op;
392 #define _(a, b, c, d) \
393 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
394 ipsecmb_ops_cipher_dec_##a); \
396 foreach_ipsecmb_cipher_op;
402 VLIB_INIT_FUNCTION (crypto_ipsecmb_init);
405 VLIB_PLUGIN_REGISTER () =
407 .version = VPP_BUILD_VER,
408 .description = "Intel IPSEC multi-buffer",
413 * fd.io coding-style-patch-verification: ON
416 * eval: (c-set-style "gnu")