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>
28 #define HMAC_MAX_BLOCK_SIZE SHA_512_BLOCK_SIZE
29 #define EXPANDED_KEY_N_BYTES (16 * 15)
35 } ipsecmb_per_thread_data_t;
41 aes_gcm_pre_t aes_gcm_pre;
43 hash_one_block_t hash_one_block;
47 typedef struct ipsecmb_main_t_
49 ipsecmb_per_thread_data_t *per_thread_data;
50 ipsecmb_alg_data_t alg_data[VNET_CRYPTO_N_ALGS];
56 u8 enc_key_exp[EXPANDED_KEY_N_BYTES];
57 u8 dec_key_exp[EXPANDED_KEY_N_BYTES];
58 } ipsecmb_aes_cbc_key_data_t;
60 static ipsecmb_main_t ipsecmb_main = { };
63 * (Alg, JOB_HASH_ALG, fn, block-size-bytes, hash-size-bytes, digest-size-bytes)
65 #define foreach_ipsecmb_hmac_op \
66 _(SHA1, SHA1, sha1, 64, 20, 20) \
67 _(SHA224, SHA_224, sha224, 64, 32, 28) \
68 _(SHA256, SHA_256, sha256, 64, 32, 32) \
69 _(SHA384, SHA_384, sha384, 128, 64, 48) \
70 _(SHA512, SHA_512, sha512, 128, 64, 64)
75 #define foreach_ipsecmb_cbc_cipher_op \
81 * (Alg, key-len-bytes, iv-len-bytes)
83 #define foreach_ipsecmb_gcm_cipher_op \
89 ipsecmb_retire_hmac_job (JOB_AES_HMAC * job, u32 * n_fail, u32 digest_size)
91 vnet_crypto_op_t *op = job->user_data;
92 u32 len = op->digest_len ? op->digest_len : digest_size;
94 if (STS_COMPLETED != job->status)
96 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
97 *n_fail = *n_fail + 1;
101 if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
103 if ((memcmp (op->digest, job->auth_tag_output, len)))
105 *n_fail = *n_fail + 1;
106 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
110 else if (len == digest_size)
111 clib_memcpy_fast (op->digest, job->auth_tag_output, digest_size);
113 clib_memcpy_fast (op->digest, job->auth_tag_output, len);
115 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
118 static_always_inline u32
119 ipsecmb_ops_hmac_inline (vlib_main_t * vm, vnet_crypto_op_t * ops[],
120 u32 n_ops, u32 block_size, u32 hash_size,
121 u32 digest_size, JOB_HASH_ALG alg)
123 ipsecmb_main_t *imbm = &ipsecmb_main;
124 ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data,
128 u8 scratch[n_ops][digest_size];
131 * queue all the jobs first ...
133 for (i = 0; i < n_ops; i++)
135 vnet_crypto_op_t *op = ops[i];
136 u8 *kd = (u8 *) imbm->key_data[op->key_index];
138 job = IMB_GET_NEXT_JOB (ptd->mgr);
141 job->hash_start_src_offset_in_bytes = 0;
142 job->msg_len_to_hash_in_bytes = op->len;
144 job->auth_tag_output_len_in_bytes = digest_size;
145 job->auth_tag_output = scratch[i];
147 job->cipher_mode = NULL_CIPHER;
148 job->cipher_direction = DECRYPT;
149 job->chain_order = HASH_CIPHER;
151 job->u.HMAC._hashed_auth_key_xor_ipad = kd;
152 job->u.HMAC._hashed_auth_key_xor_opad = kd + hash_size;
155 job = IMB_SUBMIT_JOB (ptd->mgr);
158 ipsecmb_retire_hmac_job (job, &n_fail, digest_size);
161 while ((job = IMB_FLUSH_JOB (ptd->mgr)))
162 ipsecmb_retire_hmac_job (job, &n_fail, digest_size);
164 return n_ops - n_fail;
167 #define _(a, b, c, d, e, f) \
168 static_always_inline u32 \
169 ipsecmb_ops_hmac_##a (vlib_main_t * vm, \
170 vnet_crypto_op_t * ops[], \
172 { return ipsecmb_ops_hmac_inline (vm, ops, n_ops, d, e, f, b); } \
174 foreach_ipsecmb_hmac_op;
178 ipsecmb_retire_cipher_job (JOB_AES_HMAC * job, u32 * n_fail)
180 vnet_crypto_op_t *op = job->user_data;
182 if (STS_COMPLETED != job->status)
184 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
185 *n_fail = *n_fail + 1;
188 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
191 static_always_inline u32
192 ipsecmb_ops_cbc_cipher_inline (vlib_main_t * vm, vnet_crypto_op_t * ops[],
193 u32 n_ops, u32 key_len,
194 JOB_CIPHER_DIRECTION direction)
196 ipsecmb_main_t *imbm = &ipsecmb_main;
197 ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data,
202 for (i = 0; i < n_ops; i++)
204 ipsecmb_aes_cbc_key_data_t *kd;
205 vnet_crypto_op_t *op = ops[i];
206 kd = (ipsecmb_aes_cbc_key_data_t *) imbm->key_data[op->key_index];
209 job = IMB_GET_NEXT_JOB (ptd->mgr);
213 job->msg_len_to_cipher_in_bytes = op->len;
214 job->cipher_start_src_offset_in_bytes = 0;
216 job->hash_alg = NULL_HASH;
217 job->cipher_mode = CBC;
218 job->cipher_direction = direction;
219 job->chain_order = (direction == ENCRYPT ? CIPHER_HASH : HASH_CIPHER);
221 if ((direction == ENCRYPT) && (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV))
224 _mm_storeu_si128 ((__m128i *) op->iv, iv);
225 ptd->cbc_iv = _mm_aesenc_si128 (iv, iv);
228 job->aes_key_len_in_bytes = key_len / 8;
229 job->aes_enc_key_expanded = kd->enc_key_exp;
230 job->aes_dec_key_expanded = kd->dec_key_exp;
232 job->iv_len_in_bytes = AES_BLOCK_SIZE;
236 job = IMB_SUBMIT_JOB (ptd->mgr);
239 ipsecmb_retire_cipher_job (job, &n_fail);
242 while ((job = IMB_FLUSH_JOB (ptd->mgr)))
243 ipsecmb_retire_cipher_job (job, &n_fail);
245 return n_ops - n_fail;
249 static_always_inline u32 \
250 ipsecmb_ops_cbc_cipher_enc_##a (vlib_main_t * vm, \
251 vnet_crypto_op_t * ops[], \
253 { return ipsecmb_ops_cbc_cipher_inline (vm, ops, n_ops, b, ENCRYPT); } \
255 static_always_inline u32 \
256 ipsecmb_ops_cbc_cipher_dec_##a (vlib_main_t * vm, \
257 vnet_crypto_op_t * ops[], \
259 { return ipsecmb_ops_cbc_cipher_inline (vm, ops, n_ops, b, DECRYPT); } \
261 foreach_ipsecmb_cbc_cipher_op;
265 static_always_inline u32 \
266 ipsecmb_ops_gcm_cipher_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
269 ipsecmb_main_t *imbm = &ipsecmb_main; \
270 ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data, \
272 MB_MGR *m = ptd->mgr; \
275 for (i = 0; i < n_ops; i++) \
277 struct gcm_key_data *kd; \
278 struct gcm_context_data ctx; \
279 vnet_crypto_op_t *op = ops[i]; \
281 kd = (struct gcm_key_data *) imbm->key_data[op->key_index]; \
282 IMB_AES##b##_GCM_ENC (m, kd, &ctx, op->dst, op->src, op->len, op->iv, \
283 op->aad, op->aad_len, op->tag, op->tag_len); \
285 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED; \
291 static_always_inline u32 \
292 ipsecmb_ops_gcm_cipher_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
295 ipsecmb_main_t *imbm = &ipsecmb_main; \
296 ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data, \
298 MB_MGR *m = ptd->mgr; \
299 u32 i, n_failed = 0; \
301 for (i = 0; i < n_ops; i++) \
303 struct gcm_key_data *kd; \
304 struct gcm_context_data ctx; \
305 vnet_crypto_op_t *op = ops[i]; \
308 kd = (struct gcm_key_data *) imbm->key_data[op->key_index]; \
309 IMB_AES##b##_GCM_DEC (m, kd, &ctx, op->dst, op->src, op->len, op->iv, \
310 op->aad, op->aad_len, scratch, op->tag_len); \
312 if ((memcmp (op->tag, scratch, op->tag_len))) \
314 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC; \
318 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED; \
321 return n_ops - n_failed; \
324 foreach_ipsecmb_gcm_cipher_op;
328 crypto_ipsecmb_iv_init (ipsecmb_main_t * imbm)
330 ipsecmb_per_thread_data_t *ptd;
331 clib_error_t *err = 0;
334 if ((fd = open ("/dev/urandom", O_RDONLY)) < 0)
335 return clib_error_return_unix (0, "failed to open '/dev/urandom'");
337 vec_foreach (ptd, imbm->per_thread_data)
339 if (read (fd, &ptd->cbc_iv, sizeof (ptd->cbc_iv)) != sizeof (ptd->cbc_iv))
341 err = clib_error_return_unix (0, "'/dev/urandom' read failure");
352 crypto_ipsecmb_key_handler (vlib_main_t * vm, vnet_crypto_key_op_t kop,
353 vnet_crypto_key_index_t idx)
355 ipsecmb_main_t *imbm = &ipsecmb_main;
356 vnet_crypto_key_t *key = vnet_crypto_get_key (idx);
357 ipsecmb_alg_data_t *ad = imbm->alg_data + key->alg;
361 if (kop == VNET_CRYPTO_KEY_OP_DEL)
363 if (idx >= vec_len (imbm->key_data))
366 if (imbm->key_data[idx] == 0)
369 clib_mem_free_s (imbm->key_data[idx]);
370 imbm->key_data[idx] = 0;
374 if (ad->data_size == 0)
377 vec_validate_aligned (imbm->key_data, idx, CLIB_CACHE_LINE_BYTES);
379 if (kop == VNET_CRYPTO_KEY_OP_MODIFY && imbm->key_data[idx])
381 clib_mem_free_s (imbm->key_data[idx]);
384 kd = imbm->key_data[idx] = clib_mem_alloc_aligned (ad->data_size,
385 CLIB_CACHE_LINE_BYTES);
387 /* AES CBC key expansion */
390 ad->keyexp (key->data, ((ipsecmb_aes_cbc_key_data_t *) kd)->enc_key_exp,
391 ((ipsecmb_aes_cbc_key_data_t *) kd)->dec_key_exp);
398 ad->aes_gcm_pre (key->data, (struct gcm_key_data *) kd);
403 if (ad->hash_one_block)
405 const int block_qw = HMAC_MAX_BLOCK_SIZE / sizeof (u64);
406 u64 pad[block_qw], key_hash[block_qw];
408 clib_memset_u8 (key_hash, 0, HMAC_MAX_BLOCK_SIZE);
409 if (vec_len (key->data) <= ad->block_size)
410 clib_memcpy_fast (key_hash, key->data, vec_len (key->data));
412 ad->hash_fn (key->data, vec_len (key->data), key_hash);
414 for (i = 0; i < block_qw; i++)
415 pad[i] = key_hash[i] ^ 0x3636363636363636;
416 ad->hash_one_block (pad, kd);
418 for (i = 0; i < block_qw; i++)
419 pad[i] = key_hash[i] ^ 0x5c5c5c5c5c5c5c5c;
420 ad->hash_one_block (pad, ((u8 *) kd) + (ad->data_size / 2));
426 static clib_error_t *
427 crypto_ipsecmb_init (vlib_main_t * vm)
429 ipsecmb_main_t *imbm = &ipsecmb_main;
430 ipsecmb_alg_data_t *ad;
431 ipsecmb_per_thread_data_t *ptd;
432 vlib_thread_main_t *tm = vlib_get_thread_main ();
438 if (!clib_cpu_supports_aes ())
442 * A priority that is better than OpenSSL but worse than VPP natvie
444 name = format (0, "Intel(R) Multi-Buffer Crypto for IPsec Library %s%c",
446 eidx = vnet_crypto_register_engine (vm, "ipsecmb", 80, (char *) name);
448 vec_validate (imbm->per_thread_data, tm->n_vlib_mains - 1);
451 vec_foreach (ptd, imbm->per_thread_data)
453 ptd->mgr = alloc_mb_mgr (0);
454 if (clib_cpu_supports_avx512f ())
455 init_mb_mgr_avx512 (ptd->mgr);
456 else if (clib_cpu_supports_avx2 ())
457 init_mb_mgr_avx2 (ptd->mgr);
459 init_mb_mgr_sse (ptd->mgr);
461 if (ptd == imbm->per_thread_data)
466 if (clib_cpu_supports_x86_aes () && (error = crypto_ipsecmb_iv_init (imbm)))
469 #define _(a, b, c, d, e, f) \
470 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
471 ipsecmb_ops_hmac_##a); \
472 ad = imbm->alg_data + VNET_CRYPTO_ALG_HMAC_##a; \
473 ad->block_size = d; \
474 ad->data_size = e * 2; \
475 ad->hash_one_block = m-> c##_one_block; \
476 ad->hash_fn = m-> c; \
478 foreach_ipsecmb_hmac_op;
481 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
482 ipsecmb_ops_cbc_cipher_enc_##a); \
483 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
484 ipsecmb_ops_cbc_cipher_dec_##a); \
485 ad = imbm->alg_data + VNET_CRYPTO_ALG_##a; \
486 ad->data_size = sizeof (ipsecmb_aes_cbc_key_data_t); \
487 ad->keyexp = m->keyexp_##b; \
489 foreach_ipsecmb_cbc_cipher_op;
492 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
493 ipsecmb_ops_gcm_cipher_enc_##a); \
494 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
495 ipsecmb_ops_gcm_cipher_dec_##a); \
496 ad = imbm->alg_data + VNET_CRYPTO_ALG_##a; \
497 ad->data_size = sizeof (struct gcm_key_data); \
498 ad->aes_gcm_pre = m->gcm##b##_pre; \
500 foreach_ipsecmb_gcm_cipher_op;
503 vnet_crypto_register_key_handler (vm, eidx, crypto_ipsecmb_key_handler);
508 VLIB_INIT_FUNCTION (crypto_ipsecmb_init) =
510 .runs_after = VLIB_INITS ("vnet_crypto_init"),
515 VLIB_PLUGIN_REGISTER () =
517 .version = VPP_BUILD_VER,
518 .description = "Intel IPSEC Multi-buffer Crypto Engine",
523 * fd.io coding-style-patch-verification: ON
526 * eval: (c-set-style "gnu")