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)
33 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
36 } ipsecmb_per_thread_data_t;
42 aes_gcm_pre_t aes_gcm_pre;
44 hash_one_block_t hash_one_block;
48 typedef struct ipsecmb_main_t_
50 ipsecmb_per_thread_data_t *per_thread_data;
51 ipsecmb_alg_data_t alg_data[VNET_CRYPTO_N_ALGS];
57 u8 enc_key_exp[EXPANDED_KEY_N_BYTES];
58 u8 dec_key_exp[EXPANDED_KEY_N_BYTES];
59 } ipsecmb_aes_cbc_key_data_t;
61 static ipsecmb_main_t ipsecmb_main = { };
64 * (Alg, JOB_HASH_ALG, fn, block-size-bytes, hash-size-bytes, digest-size-bytes)
66 #define foreach_ipsecmb_hmac_op \
67 _(SHA1, SHA1, sha1, 64, 20, 20) \
68 _(SHA224, SHA_224, sha224, 64, 32, 28) \
69 _(SHA256, SHA_256, sha256, 64, 32, 32) \
70 _(SHA384, SHA_384, sha384, 128, 64, 48) \
71 _(SHA512, SHA_512, sha512, 128, 64, 64)
76 #define foreach_ipsecmb_cbc_cipher_op \
82 * (Alg, key-len-bytes, iv-len-bytes)
84 #define foreach_ipsecmb_gcm_cipher_op \
89 static_always_inline vnet_crypto_op_status_t
90 ipsecmb_status_job (JOB_STS status)
95 return VNET_CRYPTO_OP_STATUS_COMPLETED;
96 case STS_BEING_PROCESSED:
97 case STS_COMPLETED_AES:
98 case STS_COMPLETED_HMAC:
99 return VNET_CRYPTO_OP_STATUS_WORK_IN_PROGRESS;
100 case STS_INVALID_ARGS:
101 case STS_INTERNAL_ERROR:
103 return VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR;
106 return VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR;
110 ipsecmb_retire_hmac_job (JOB_AES_HMAC * job, u32 * n_fail, u32 digest_size)
112 vnet_crypto_op_t *op = job->user_data;
113 u32 len = op->digest_len ? op->digest_len : digest_size;
115 if (PREDICT_FALSE (STS_COMPLETED != job->status))
117 op->status = ipsecmb_status_job (job->status);
118 *n_fail = *n_fail + 1;
122 if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
124 if ((memcmp (op->digest, job->auth_tag_output, len)))
126 *n_fail = *n_fail + 1;
127 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
131 else if (len == digest_size)
132 clib_memcpy_fast (op->digest, job->auth_tag_output, digest_size);
134 clib_memcpy_fast (op->digest, job->auth_tag_output, len);
136 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
139 static_always_inline u32
140 ipsecmb_ops_hmac_inline (vlib_main_t * vm, vnet_crypto_op_t * ops[],
141 u32 n_ops, u32 block_size, u32 hash_size,
142 u32 digest_size, JOB_HASH_ALG alg)
144 ipsecmb_main_t *imbm = &ipsecmb_main;
145 ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data,
149 u8 scratch[n_ops][digest_size];
152 * queue all the jobs first ...
154 for (i = 0; i < n_ops; i++)
156 vnet_crypto_op_t *op = ops[i];
157 u8 *kd = (u8 *) imbm->key_data[op->key_index];
159 job = IMB_GET_NEXT_JOB (ptd->mgr);
162 job->hash_start_src_offset_in_bytes = 0;
163 job->msg_len_to_hash_in_bytes = op->len;
165 job->auth_tag_output_len_in_bytes = digest_size;
166 job->auth_tag_output = scratch[i];
168 job->cipher_mode = NULL_CIPHER;
169 job->cipher_direction = DECRYPT;
170 job->chain_order = HASH_CIPHER;
172 job->u.HMAC._hashed_auth_key_xor_ipad = kd;
173 job->u.HMAC._hashed_auth_key_xor_opad = kd + hash_size;
176 job = IMB_SUBMIT_JOB (ptd->mgr);
179 ipsecmb_retire_hmac_job (job, &n_fail, digest_size);
182 while ((job = IMB_FLUSH_JOB (ptd->mgr)))
183 ipsecmb_retire_hmac_job (job, &n_fail, digest_size);
185 return n_ops - n_fail;
188 #define _(a, b, c, d, e, f) \
189 static_always_inline u32 \
190 ipsecmb_ops_hmac_##a (vlib_main_t * vm, \
191 vnet_crypto_op_t * ops[], \
193 { return ipsecmb_ops_hmac_inline (vm, ops, n_ops, d, e, f, b); } \
195 foreach_ipsecmb_hmac_op;
199 ipsecmb_retire_cipher_job (JOB_AES_HMAC * job, u32 * n_fail)
201 vnet_crypto_op_t *op = job->user_data;
203 if (PREDICT_FALSE (STS_COMPLETED != job->status))
205 op->status = ipsecmb_status_job (job->status);
206 *n_fail = *n_fail + 1;
209 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
212 static_always_inline u32
213 ipsecmb_ops_cbc_cipher_inline (vlib_main_t * vm, vnet_crypto_op_t * ops[],
214 u32 n_ops, u32 key_len,
215 JOB_CIPHER_DIRECTION direction)
217 ipsecmb_main_t *imbm = &ipsecmb_main;
218 ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data,
223 for (i = 0; i < n_ops; i++)
225 ipsecmb_aes_cbc_key_data_t *kd;
226 vnet_crypto_op_t *op = ops[i];
227 kd = (ipsecmb_aes_cbc_key_data_t *) imbm->key_data[op->key_index];
230 job = IMB_GET_NEXT_JOB (ptd->mgr);
234 job->msg_len_to_cipher_in_bytes = op->len;
235 job->cipher_start_src_offset_in_bytes = 0;
237 job->hash_alg = NULL_HASH;
238 job->cipher_mode = CBC;
239 job->cipher_direction = direction;
240 job->chain_order = (direction == ENCRYPT ? CIPHER_HASH : HASH_CIPHER);
242 if ((direction == ENCRYPT) && (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV))
245 _mm_storeu_si128 ((__m128i *) op->iv, iv);
246 ptd->cbc_iv = _mm_aesenc_si128 (iv, iv);
249 job->aes_key_len_in_bytes = key_len / 8;
250 job->aes_enc_key_expanded = kd->enc_key_exp;
251 job->aes_dec_key_expanded = kd->dec_key_exp;
253 job->iv_len_in_bytes = AES_BLOCK_SIZE;
257 job = IMB_SUBMIT_JOB (ptd->mgr);
260 ipsecmb_retire_cipher_job (job, &n_fail);
263 while ((job = IMB_FLUSH_JOB (ptd->mgr)))
264 ipsecmb_retire_cipher_job (job, &n_fail);
266 return n_ops - n_fail;
270 static_always_inline u32 \
271 ipsecmb_ops_cbc_cipher_enc_##a (vlib_main_t * vm, \
272 vnet_crypto_op_t * ops[], \
274 { return ipsecmb_ops_cbc_cipher_inline (vm, ops, n_ops, b, ENCRYPT); } \
276 static_always_inline u32 \
277 ipsecmb_ops_cbc_cipher_dec_##a (vlib_main_t * vm, \
278 vnet_crypto_op_t * ops[], \
280 { return ipsecmb_ops_cbc_cipher_inline (vm, ops, n_ops, b, DECRYPT); } \
282 foreach_ipsecmb_cbc_cipher_op;
286 static_always_inline u32 \
287 ipsecmb_ops_gcm_cipher_enc_##a##_chained (vlib_main_t * vm, \
288 vnet_crypto_op_t * ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops) \
290 ipsecmb_main_t *imbm = &ipsecmb_main; \
291 ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data, \
293 MB_MGR *m = ptd->mgr; \
294 vnet_crypto_op_chunk_t *chp; \
297 for (i = 0; i < n_ops; i++) \
299 struct gcm_key_data *kd; \
300 struct gcm_context_data ctx; \
301 vnet_crypto_op_t *op = ops[i]; \
303 kd = (struct gcm_key_data *) imbm->key_data[op->key_index]; \
304 ASSERT (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS); \
305 IMB_AES##b##_GCM_INIT(m, kd, &ctx, op->iv, op->aad, op->aad_len); \
306 chp = chunks + op->chunk_index; \
307 for (j = 0; j < op->n_chunks; j++) \
309 IMB_AES##b##_GCM_ENC_UPDATE (m, kd, &ctx, chp->dst, chp->src, \
313 IMB_AES##b##_GCM_ENC_FINALIZE(m, kd, &ctx, op->tag, op->tag_len); \
315 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED; \
321 static_always_inline u32 \
322 ipsecmb_ops_gcm_cipher_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
325 ipsecmb_main_t *imbm = &ipsecmb_main; \
326 ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data, \
328 MB_MGR *m = ptd->mgr; \
331 for (i = 0; i < n_ops; i++) \
333 struct gcm_key_data *kd; \
334 struct gcm_context_data ctx; \
335 vnet_crypto_op_t *op = ops[i]; \
337 kd = (struct gcm_key_data *) imbm->key_data[op->key_index]; \
338 IMB_AES##b##_GCM_ENC (m, kd, &ctx, op->dst, op->src, op->len, op->iv, \
339 op->aad, op->aad_len, op->tag, op->tag_len); \
341 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED; \
347 static_always_inline u32 \
348 ipsecmb_ops_gcm_cipher_dec_##a##_chained (vlib_main_t * vm, \
349 vnet_crypto_op_t * ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops) \
351 ipsecmb_main_t *imbm = &ipsecmb_main; \
352 ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data, \
354 MB_MGR *m = ptd->mgr; \
355 vnet_crypto_op_chunk_t *chp; \
356 u32 i, j, n_failed = 0; \
358 for (i = 0; i < n_ops; i++) \
360 struct gcm_key_data *kd; \
361 struct gcm_context_data ctx; \
362 vnet_crypto_op_t *op = ops[i]; \
365 kd = (struct gcm_key_data *) imbm->key_data[op->key_index]; \
366 ASSERT (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS); \
367 IMB_AES##b##_GCM_INIT(m, kd, &ctx, op->iv, op->aad, op->aad_len); \
368 chp = chunks + op->chunk_index; \
369 for (j = 0; j < op->n_chunks; j++) \
371 IMB_AES##b##_GCM_DEC_UPDATE (m, kd, &ctx, chp->dst, chp->src, \
375 IMB_AES##b##_GCM_DEC_FINALIZE(m, kd, &ctx, scratch, op->tag_len); \
377 if ((memcmp (op->tag, scratch, op->tag_len))) \
379 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC; \
383 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED; \
386 return n_ops - n_failed; \
389 static_always_inline u32 \
390 ipsecmb_ops_gcm_cipher_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], \
393 ipsecmb_main_t *imbm = &ipsecmb_main; \
394 ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data, \
396 MB_MGR *m = ptd->mgr; \
397 u32 i, n_failed = 0; \
399 for (i = 0; i < n_ops; i++) \
401 struct gcm_key_data *kd; \
402 struct gcm_context_data ctx; \
403 vnet_crypto_op_t *op = ops[i]; \
406 kd = (struct gcm_key_data *) imbm->key_data[op->key_index]; \
407 IMB_AES##b##_GCM_DEC (m, kd, &ctx, op->dst, op->src, op->len, op->iv, \
408 op->aad, op->aad_len, scratch, op->tag_len); \
410 if ((memcmp (op->tag, scratch, op->tag_len))) \
412 op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC; \
416 op->status = VNET_CRYPTO_OP_STATUS_COMPLETED; \
419 return n_ops - n_failed; \
422 foreach_ipsecmb_gcm_cipher_op;
426 crypto_ipsecmb_iv_init (ipsecmb_main_t * imbm)
428 ipsecmb_per_thread_data_t *ptd;
429 clib_error_t *err = 0;
432 if ((fd = open ("/dev/urandom", O_RDONLY)) < 0)
433 return clib_error_return_unix (0, "failed to open '/dev/urandom'");
435 vec_foreach (ptd, imbm->per_thread_data)
437 if (read (fd, &ptd->cbc_iv, sizeof (ptd->cbc_iv)) != sizeof (ptd->cbc_iv))
439 err = clib_error_return_unix (0, "'/dev/urandom' read failure");
450 crypto_ipsecmb_key_handler (vlib_main_t * vm, vnet_crypto_key_op_t kop,
451 vnet_crypto_key_index_t idx)
453 ipsecmb_main_t *imbm = &ipsecmb_main;
454 vnet_crypto_key_t *key = vnet_crypto_get_key (idx);
455 ipsecmb_alg_data_t *ad = imbm->alg_data + key->alg;
459 /** TODO: add linked alg support **/
460 if (key->type == VNET_CRYPTO_KEY_TYPE_LINK)
463 if (kop == VNET_CRYPTO_KEY_OP_DEL)
465 if (idx >= vec_len (imbm->key_data))
468 if (imbm->key_data[idx] == 0)
471 clib_mem_free_s (imbm->key_data[idx]);
472 imbm->key_data[idx] = 0;
476 if (ad->data_size == 0)
479 vec_validate_aligned (imbm->key_data, idx, CLIB_CACHE_LINE_BYTES);
481 if (kop == VNET_CRYPTO_KEY_OP_MODIFY && imbm->key_data[idx])
483 clib_mem_free_s (imbm->key_data[idx]);
486 kd = imbm->key_data[idx] = clib_mem_alloc_aligned (ad->data_size,
487 CLIB_CACHE_LINE_BYTES);
489 /* AES CBC key expansion */
492 ad->keyexp (key->data, ((ipsecmb_aes_cbc_key_data_t *) kd)->enc_key_exp,
493 ((ipsecmb_aes_cbc_key_data_t *) kd)->dec_key_exp);
500 ad->aes_gcm_pre (key->data, (struct gcm_key_data *) kd);
505 if (ad->hash_one_block)
507 const int block_qw = HMAC_MAX_BLOCK_SIZE / sizeof (u64);
508 u64 pad[block_qw], key_hash[block_qw];
510 clib_memset_u8 (key_hash, 0, HMAC_MAX_BLOCK_SIZE);
511 if (vec_len (key->data) <= ad->block_size)
512 clib_memcpy_fast (key_hash, key->data, vec_len (key->data));
514 ad->hash_fn (key->data, vec_len (key->data), key_hash);
516 for (i = 0; i < block_qw; i++)
517 pad[i] = key_hash[i] ^ 0x3636363636363636;
518 ad->hash_one_block (pad, kd);
520 for (i = 0; i < block_qw; i++)
521 pad[i] = key_hash[i] ^ 0x5c5c5c5c5c5c5c5c;
522 ad->hash_one_block (pad, ((u8 *) kd) + (ad->data_size / 2));
528 static clib_error_t *
529 crypto_ipsecmb_init (vlib_main_t * vm)
531 ipsecmb_main_t *imbm = &ipsecmb_main;
532 ipsecmb_alg_data_t *ad;
533 ipsecmb_per_thread_data_t *ptd;
534 vlib_thread_main_t *tm = vlib_get_thread_main ();
540 if (!clib_cpu_supports_aes ())
544 * A priority that is better than OpenSSL but worse than VPP natvie
546 name = format (0, "Intel(R) Multi-Buffer Crypto for IPsec Library %s%c",
548 eidx = vnet_crypto_register_engine (vm, "ipsecmb", 80, (char *) name);
550 vec_validate_aligned (imbm->per_thread_data, tm->n_vlib_mains - 1,
551 CLIB_CACHE_LINE_BYTES);
554 vec_foreach (ptd, imbm->per_thread_data)
556 ptd->mgr = alloc_mb_mgr (0);
557 if (clib_cpu_supports_avx512f ())
558 init_mb_mgr_avx512 (ptd->mgr);
559 else if (clib_cpu_supports_avx2 ())
560 init_mb_mgr_avx2 (ptd->mgr);
562 init_mb_mgr_sse (ptd->mgr);
564 if (ptd == imbm->per_thread_data)
569 if (clib_cpu_supports_x86_aes () && (error = crypto_ipsecmb_iv_init (imbm)))
572 #define _(a, b, c, d, e, f) \
573 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
574 ipsecmb_ops_hmac_##a); \
575 ad = imbm->alg_data + VNET_CRYPTO_ALG_HMAC_##a; \
576 ad->block_size = d; \
577 ad->data_size = e * 2; \
578 ad->hash_one_block = m-> c##_one_block; \
579 ad->hash_fn = m-> c; \
581 foreach_ipsecmb_hmac_op;
584 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
585 ipsecmb_ops_cbc_cipher_enc_##a); \
586 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
587 ipsecmb_ops_cbc_cipher_dec_##a); \
588 ad = imbm->alg_data + VNET_CRYPTO_ALG_##a; \
589 ad->data_size = sizeof (ipsecmb_aes_cbc_key_data_t); \
590 ad->keyexp = m->keyexp_##b; \
592 foreach_ipsecmb_cbc_cipher_op;
595 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
596 ipsecmb_ops_gcm_cipher_enc_##a); \
597 vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
598 ipsecmb_ops_gcm_cipher_dec_##a); \
599 vnet_crypto_register_chained_ops_handler \
600 (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
601 ipsecmb_ops_gcm_cipher_enc_##a##_chained); \
602 vnet_crypto_register_chained_ops_handler \
603 (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
604 ipsecmb_ops_gcm_cipher_dec_##a##_chained); \
605 ad = imbm->alg_data + VNET_CRYPTO_ALG_##a; \
606 ad->data_size = sizeof (struct gcm_key_data); \
607 ad->aes_gcm_pre = m->gcm##b##_pre; \
609 foreach_ipsecmb_gcm_cipher_op;
612 vnet_crypto_register_key_handler (vm, eidx, crypto_ipsecmb_key_handler);
617 VLIB_INIT_FUNCTION (crypto_ipsecmb_init) =
619 .runs_after = VLIB_INITS ("vnet_crypto_init"),
624 VLIB_PLUGIN_REGISTER () =
626 .version = VPP_BUILD_VER,
627 .description = "Intel IPSEC Multi-buffer Crypto Engine",
632 * fd.io coding-style-patch-verification: ON
635 * eval: (c-set-style "gnu")