1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2017 Intel Corporation
5 #include <intel-ipsec-mb.h>
7 #include <rte_common.h>
8 #include <rte_hexdump.h>
9 #include <rte_cryptodev.h>
10 #include <rte_cryptodev_pmd.h>
11 #include <rte_bus_vdev.h>
12 #include <rte_malloc.h>
13 #include <rte_cpuflags.h>
14 #include <rte_per_lcore.h>
15 #include <rte_ether.h>
17 #include "aesni_mb_pmd_private.h"
19 #define AES_CCM_DIGEST_MIN_LEN 4
20 #define AES_CCM_DIGEST_MAX_LEN 16
21 #define HMAC_MAX_BLOCK_SIZE 128
22 static uint8_t cryptodev_driver_id;
25 * Needed to support CPU-CRYPTO API (rte_cryptodev_sym_cpu_crypto_process),
26 * as we still use JOB based API even for synchronous processing.
28 static RTE_DEFINE_PER_LCORE(MB_MGR *, sync_mb_mgr);
30 typedef void (*hash_one_block_t)(const void *data, void *digest);
31 typedef void (*aes_keyexp_t)(const void *key, void *enc_exp_keys, void *dec_exp_keys);
34 * Calculate the authentication pre-computes
36 * @param one_block_hash Function pointer to calculate digest on ipad/opad
37 * @param ipad Inner pad output byte array
38 * @param opad Outer pad output byte array
39 * @param hkey Authentication key
40 * @param hkey_len Authentication key length
41 * @param blocksize Block size of selected hash algo
44 calculate_auth_precomputes(hash_one_block_t one_block_hash,
45 uint8_t *ipad, uint8_t *opad,
46 const uint8_t *hkey, uint16_t hkey_len,
51 uint8_t ipad_buf[blocksize] __rte_aligned(16);
52 uint8_t opad_buf[blocksize] __rte_aligned(16);
54 /* Setup inner and outer pads */
55 memset(ipad_buf, HMAC_IPAD_VALUE, blocksize);
56 memset(opad_buf, HMAC_OPAD_VALUE, blocksize);
58 /* XOR hash key with inner and outer pads */
59 length = hkey_len > blocksize ? blocksize : hkey_len;
61 for (i = 0; i < length; i++) {
62 ipad_buf[i] ^= hkey[i];
63 opad_buf[i] ^= hkey[i];
66 /* Compute partial hashes */
67 (*one_block_hash)(ipad_buf, ipad);
68 (*one_block_hash)(opad_buf, opad);
71 memset(ipad_buf, 0, blocksize);
72 memset(opad_buf, 0, blocksize);
75 /** Get xform chain order */
76 static enum aesni_mb_operation
77 aesni_mb_get_chain_order(const struct rte_crypto_sym_xform *xform)
80 return AESNI_MB_OP_NOT_SUPPORTED;
82 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
83 if (xform->next == NULL)
84 return AESNI_MB_OP_CIPHER_ONLY;
85 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
86 return AESNI_MB_OP_CIPHER_HASH;
89 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
90 if (xform->next == NULL)
91 return AESNI_MB_OP_HASH_ONLY;
92 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
93 return AESNI_MB_OP_HASH_CIPHER;
95 #if IMB_VERSION_NUM > IMB_VERSION(0, 52, 0)
96 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
97 if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) {
99 * CCM requires to hash first and cipher later
102 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM)
103 return AESNI_MB_OP_AEAD_HASH_CIPHER;
105 return AESNI_MB_OP_AEAD_CIPHER_HASH;
107 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM)
108 return AESNI_MB_OP_AEAD_CIPHER_HASH;
110 return AESNI_MB_OP_AEAD_HASH_CIPHER;
114 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
115 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM ||
116 xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
117 if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
118 return AESNI_MB_OP_AEAD_CIPHER_HASH;
120 return AESNI_MB_OP_AEAD_HASH_CIPHER;
125 return AESNI_MB_OP_NOT_SUPPORTED;
128 /** Set session authentication parameters */
130 aesni_mb_set_session_auth_parameters(const MB_MGR *mb_mgr,
131 struct aesni_mb_session *sess,
132 const struct rte_crypto_sym_xform *xform)
134 hash_one_block_t hash_oneblock_fn = NULL;
135 unsigned int key_larger_block_size = 0;
136 uint8_t hashed_key[HMAC_MAX_BLOCK_SIZE] = { 0 };
137 uint32_t auth_precompute = 1;
140 sess->auth.algo = NULL_HASH;
144 if (xform->type != RTE_CRYPTO_SYM_XFORM_AUTH) {
145 AESNI_MB_LOG(ERR, "Crypto xform struct not of type auth");
149 /* Set the request digest size */
150 sess->auth.req_digest_len = xform->auth.digest_length;
152 /* Select auth generate/verify */
153 sess->auth.operation = xform->auth.op;
155 /* Set Authentication Parameters */
156 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC) {
157 sess->auth.algo = AES_XCBC;
159 uint16_t xcbc_mac_digest_len =
160 get_truncated_digest_byte_length(AES_XCBC);
161 if (sess->auth.req_digest_len != xcbc_mac_digest_len) {
162 AESNI_MB_LOG(ERR, "Invalid digest size\n");
165 sess->auth.gen_digest_len = sess->auth.req_digest_len;
167 IMB_AES_XCBC_KEYEXP(mb_mgr, xform->auth.key.data,
168 sess->auth.xcbc.k1_expanded,
169 sess->auth.xcbc.k2, sess->auth.xcbc.k3);
173 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_CMAC) {
176 sess->auth.algo = AES_CMAC;
178 uint16_t cmac_digest_len = get_digest_byte_length(AES_CMAC);
180 if (sess->auth.req_digest_len > cmac_digest_len) {
181 AESNI_MB_LOG(ERR, "Invalid digest size\n");
185 * Multi-buffer lib supports digest sizes from 4 to 16 bytes
186 * in version 0.50 and sizes of 12 and 16 bytes,
188 * If size requested is different, generate the full digest
189 * (16 bytes) in a temporary location and then memcpy
190 * the requested number of bytes.
192 if (sess->auth.req_digest_len < 4)
193 sess->auth.gen_digest_len = cmac_digest_len;
195 sess->auth.gen_digest_len = sess->auth.req_digest_len;
197 IMB_AES_KEYEXP_128(mb_mgr, xform->auth.key.data,
198 sess->auth.cmac.expkey, dust);
199 IMB_AES_CMAC_SUBKEY_GEN_128(mb_mgr, sess->auth.cmac.expkey,
200 sess->auth.cmac.skey1, sess->auth.cmac.skey2);
204 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
205 if (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) {
206 sess->cipher.direction = ENCRYPT;
207 sess->chain_order = CIPHER_HASH;
209 sess->cipher.direction = DECRYPT;
211 sess->auth.algo = AES_GMAC;
213 * Multi-buffer lib supports 8, 12 and 16 bytes of digest.
214 * If size requested is different, generate the full digest
215 * (16 bytes) in a temporary location and then memcpy
216 * the requested number of bytes.
218 if (sess->auth.req_digest_len != 16 &&
219 sess->auth.req_digest_len != 12 &&
220 sess->auth.req_digest_len != 8) {
221 sess->auth.gen_digest_len = 16;
223 sess->auth.gen_digest_len = sess->auth.req_digest_len;
225 sess->iv.length = xform->auth.iv.length;
226 sess->iv.offset = xform->auth.iv.offset;
228 switch (xform->auth.key.length) {
230 IMB_AES128_GCM_PRE(mb_mgr, xform->auth.key.data,
231 &sess->cipher.gcm_key);
232 sess->cipher.key_length_in_bytes = AES_128_BYTES;
235 IMB_AES192_GCM_PRE(mb_mgr, xform->auth.key.data,
236 &sess->cipher.gcm_key);
237 sess->cipher.key_length_in_bytes = AES_192_BYTES;
240 IMB_AES256_GCM_PRE(mb_mgr, xform->auth.key.data,
241 &sess->cipher.gcm_key);
242 sess->cipher.key_length_in_bytes = AES_256_BYTES;
245 RTE_LOG(ERR, PMD, "failed to parse test type\n");
252 switch (xform->auth.algo) {
253 case RTE_CRYPTO_AUTH_MD5_HMAC:
254 sess->auth.algo = MD5;
255 hash_oneblock_fn = mb_mgr->md5_one_block;
257 case RTE_CRYPTO_AUTH_SHA1_HMAC:
258 sess->auth.algo = SHA1;
259 hash_oneblock_fn = mb_mgr->sha1_one_block;
260 if (xform->auth.key.length > get_auth_algo_blocksize(SHA1)) {
262 xform->auth.key.data,
263 xform->auth.key.length,
265 key_larger_block_size = 1;
268 case RTE_CRYPTO_AUTH_SHA1:
269 sess->auth.algo = PLAIN_SHA1;
272 case RTE_CRYPTO_AUTH_SHA224_HMAC:
273 sess->auth.algo = SHA_224;
274 hash_oneblock_fn = mb_mgr->sha224_one_block;
275 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_224)) {
277 xform->auth.key.data,
278 xform->auth.key.length,
280 key_larger_block_size = 1;
283 case RTE_CRYPTO_AUTH_SHA224:
284 sess->auth.algo = PLAIN_SHA_224;
287 case RTE_CRYPTO_AUTH_SHA256_HMAC:
288 sess->auth.algo = SHA_256;
289 hash_oneblock_fn = mb_mgr->sha256_one_block;
290 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_256)) {
292 xform->auth.key.data,
293 xform->auth.key.length,
295 key_larger_block_size = 1;
298 case RTE_CRYPTO_AUTH_SHA256:
299 sess->auth.algo = PLAIN_SHA_256;
302 case RTE_CRYPTO_AUTH_SHA384_HMAC:
303 sess->auth.algo = SHA_384;
304 hash_oneblock_fn = mb_mgr->sha384_one_block;
305 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_384)) {
307 xform->auth.key.data,
308 xform->auth.key.length,
310 key_larger_block_size = 1;
313 case RTE_CRYPTO_AUTH_SHA384:
314 sess->auth.algo = PLAIN_SHA_384;
317 case RTE_CRYPTO_AUTH_SHA512_HMAC:
318 sess->auth.algo = SHA_512;
319 hash_oneblock_fn = mb_mgr->sha512_one_block;
320 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_512)) {
322 xform->auth.key.data,
323 xform->auth.key.length,
325 key_larger_block_size = 1;
328 case RTE_CRYPTO_AUTH_SHA512:
329 sess->auth.algo = PLAIN_SHA_512;
333 AESNI_MB_LOG(ERR, "Unsupported authentication algorithm selection");
336 uint16_t trunc_digest_size =
337 get_truncated_digest_byte_length(sess->auth.algo);
338 uint16_t full_digest_size =
339 get_digest_byte_length(sess->auth.algo);
341 if (sess->auth.req_digest_len > full_digest_size ||
342 sess->auth.req_digest_len == 0) {
343 AESNI_MB_LOG(ERR, "Invalid digest size\n");
347 if (sess->auth.req_digest_len != trunc_digest_size &&
348 sess->auth.req_digest_len != full_digest_size)
349 sess->auth.gen_digest_len = full_digest_size;
351 sess->auth.gen_digest_len = sess->auth.req_digest_len;
353 /* Plain SHA does not require precompute key */
354 if (auth_precompute == 0)
357 /* Calculate Authentication precomputes */
358 if (key_larger_block_size) {
359 calculate_auth_precomputes(hash_oneblock_fn,
360 sess->auth.pads.inner, sess->auth.pads.outer,
362 xform->auth.key.length,
363 get_auth_algo_blocksize(sess->auth.algo));
365 calculate_auth_precomputes(hash_oneblock_fn,
366 sess->auth.pads.inner, sess->auth.pads.outer,
367 xform->auth.key.data,
368 xform->auth.key.length,
369 get_auth_algo_blocksize(sess->auth.algo));
375 /** Set session cipher parameters */
377 aesni_mb_set_session_cipher_parameters(const MB_MGR *mb_mgr,
378 struct aesni_mb_session *sess,
379 const struct rte_crypto_sym_xform *xform)
383 uint8_t is_docsis = 0;
386 sess->cipher.mode = NULL_CIPHER;
390 if (xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) {
391 AESNI_MB_LOG(ERR, "Crypto xform struct not of type cipher");
395 /* Select cipher direction */
396 switch (xform->cipher.op) {
397 case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
398 sess->cipher.direction = ENCRYPT;
400 case RTE_CRYPTO_CIPHER_OP_DECRYPT:
401 sess->cipher.direction = DECRYPT;
404 AESNI_MB_LOG(ERR, "Invalid cipher operation parameter");
408 /* Select cipher mode */
409 switch (xform->cipher.algo) {
410 case RTE_CRYPTO_CIPHER_AES_CBC:
411 sess->cipher.mode = CBC;
414 case RTE_CRYPTO_CIPHER_AES_CTR:
415 sess->cipher.mode = CNTR;
418 case RTE_CRYPTO_CIPHER_AES_DOCSISBPI:
419 sess->cipher.mode = DOCSIS_SEC_BPI;
422 case RTE_CRYPTO_CIPHER_DES_CBC:
423 sess->cipher.mode = DES;
425 case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
426 sess->cipher.mode = DOCSIS_DES;
428 case RTE_CRYPTO_CIPHER_3DES_CBC:
429 sess->cipher.mode = DES3;
433 AESNI_MB_LOG(ERR, "Unsupported cipher mode parameter");
437 /* Set IV parameters */
438 sess->iv.offset = xform->cipher.iv.offset;
439 sess->iv.length = xform->cipher.iv.length;
441 /* Check key length and choose key expansion function for AES */
443 switch (xform->cipher.key.length) {
445 sess->cipher.key_length_in_bytes = AES_128_BYTES;
446 IMB_AES_KEYEXP_128(mb_mgr, xform->cipher.key.data,
447 sess->cipher.expanded_aes_keys.encode,
448 sess->cipher.expanded_aes_keys.decode);
451 sess->cipher.key_length_in_bytes = AES_192_BYTES;
452 IMB_AES_KEYEXP_192(mb_mgr, xform->cipher.key.data,
453 sess->cipher.expanded_aes_keys.encode,
454 sess->cipher.expanded_aes_keys.decode);
457 sess->cipher.key_length_in_bytes = AES_256_BYTES;
458 IMB_AES_KEYEXP_256(mb_mgr, xform->cipher.key.data,
459 sess->cipher.expanded_aes_keys.encode,
460 sess->cipher.expanded_aes_keys.decode);
463 AESNI_MB_LOG(ERR, "Invalid cipher key length");
466 } else if (is_docsis) {
467 switch (xform->cipher.key.length) {
469 sess->cipher.key_length_in_bytes = AES_128_BYTES;
470 IMB_AES_KEYEXP_128(mb_mgr, xform->cipher.key.data,
471 sess->cipher.expanded_aes_keys.encode,
472 sess->cipher.expanded_aes_keys.decode);
474 #if IMB_VERSION_NUM >= IMB_VERSION(0, 53, 3)
476 sess->cipher.key_length_in_bytes = AES_256_BYTES;
477 IMB_AES_KEYEXP_256(mb_mgr, xform->cipher.key.data,
478 sess->cipher.expanded_aes_keys.encode,
479 sess->cipher.expanded_aes_keys.decode);
483 AESNI_MB_LOG(ERR, "Invalid cipher key length");
486 } else if (is_3DES) {
487 uint64_t *keys[3] = {sess->cipher.exp_3des_keys.key[0],
488 sess->cipher.exp_3des_keys.key[1],
489 sess->cipher.exp_3des_keys.key[2]};
491 switch (xform->cipher.key.length) {
493 IMB_DES_KEYSCHED(mb_mgr, keys[0],
494 xform->cipher.key.data);
495 IMB_DES_KEYSCHED(mb_mgr, keys[1],
496 xform->cipher.key.data + 8);
497 IMB_DES_KEYSCHED(mb_mgr, keys[2],
498 xform->cipher.key.data + 16);
500 /* Initialize keys - 24 bytes: [K1-K2-K3] */
501 sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
502 sess->cipher.exp_3des_keys.ks_ptr[1] = keys[1];
503 sess->cipher.exp_3des_keys.ks_ptr[2] = keys[2];
506 IMB_DES_KEYSCHED(mb_mgr, keys[0],
507 xform->cipher.key.data);
508 IMB_DES_KEYSCHED(mb_mgr, keys[1],
509 xform->cipher.key.data + 8);
510 /* Initialize keys - 16 bytes: [K1=K1,K2=K2,K3=K1] */
511 sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
512 sess->cipher.exp_3des_keys.ks_ptr[1] = keys[1];
513 sess->cipher.exp_3des_keys.ks_ptr[2] = keys[0];
516 IMB_DES_KEYSCHED(mb_mgr, keys[0],
517 xform->cipher.key.data);
519 /* Initialize keys - 8 bytes: [K1 = K2 = K3] */
520 sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
521 sess->cipher.exp_3des_keys.ks_ptr[1] = keys[0];
522 sess->cipher.exp_3des_keys.ks_ptr[2] = keys[0];
525 AESNI_MB_LOG(ERR, "Invalid cipher key length");
529 sess->cipher.key_length_in_bytes = 24;
531 if (xform->cipher.key.length != 8) {
532 AESNI_MB_LOG(ERR, "Invalid cipher key length");
535 sess->cipher.key_length_in_bytes = 8;
537 IMB_DES_KEYSCHED(mb_mgr,
538 (uint64_t *)sess->cipher.expanded_aes_keys.encode,
539 xform->cipher.key.data);
540 IMB_DES_KEYSCHED(mb_mgr,
541 (uint64_t *)sess->cipher.expanded_aes_keys.decode,
542 xform->cipher.key.data);
549 aesni_mb_set_session_aead_parameters(const MB_MGR *mb_mgr,
550 struct aesni_mb_session *sess,
551 const struct rte_crypto_sym_xform *xform)
553 switch (xform->aead.op) {
554 case RTE_CRYPTO_AEAD_OP_ENCRYPT:
555 sess->cipher.direction = ENCRYPT;
556 sess->auth.operation = RTE_CRYPTO_AUTH_OP_GENERATE;
558 case RTE_CRYPTO_AEAD_OP_DECRYPT:
559 sess->cipher.direction = DECRYPT;
560 sess->auth.operation = RTE_CRYPTO_AUTH_OP_VERIFY;
563 AESNI_MB_LOG(ERR, "Invalid aead operation parameter");
567 switch (xform->aead.algo) {
568 case RTE_CRYPTO_AEAD_AES_CCM:
569 sess->cipher.mode = CCM;
570 sess->auth.algo = AES_CCM;
572 /* Check key length and choose key expansion function for AES */
573 switch (xform->aead.key.length) {
575 sess->cipher.key_length_in_bytes = AES_128_BYTES;
576 IMB_AES_KEYEXP_128(mb_mgr, xform->aead.key.data,
577 sess->cipher.expanded_aes_keys.encode,
578 sess->cipher.expanded_aes_keys.decode);
581 AESNI_MB_LOG(ERR, "Invalid cipher key length");
587 case RTE_CRYPTO_AEAD_AES_GCM:
588 sess->cipher.mode = GCM;
589 sess->auth.algo = AES_GMAC;
591 switch (xform->aead.key.length) {
593 sess->cipher.key_length_in_bytes = AES_128_BYTES;
594 IMB_AES128_GCM_PRE(mb_mgr, xform->aead.key.data,
595 &sess->cipher.gcm_key);
598 sess->cipher.key_length_in_bytes = AES_192_BYTES;
599 IMB_AES192_GCM_PRE(mb_mgr, xform->aead.key.data,
600 &sess->cipher.gcm_key);
603 sess->cipher.key_length_in_bytes = AES_256_BYTES;
604 IMB_AES256_GCM_PRE(mb_mgr, xform->aead.key.data,
605 &sess->cipher.gcm_key);
608 AESNI_MB_LOG(ERR, "Invalid cipher key length");
615 AESNI_MB_LOG(ERR, "Unsupported aead mode parameter");
619 /* Set IV parameters */
620 sess->iv.offset = xform->aead.iv.offset;
621 sess->iv.length = xform->aead.iv.length;
623 sess->auth.req_digest_len = xform->aead.digest_length;
624 /* CCM digests must be between 4 and 16 and an even number */
625 if (sess->auth.req_digest_len < AES_CCM_DIGEST_MIN_LEN ||
626 sess->auth.req_digest_len > AES_CCM_DIGEST_MAX_LEN ||
627 (sess->auth.req_digest_len & 1) == 1) {
628 AESNI_MB_LOG(ERR, "Invalid digest size\n");
631 sess->auth.gen_digest_len = sess->auth.req_digest_len;
636 /** Parse crypto xform chain and set private session parameters */
638 aesni_mb_set_session_parameters(const MB_MGR *mb_mgr,
639 struct aesni_mb_session *sess,
640 const struct rte_crypto_sym_xform *xform)
642 const struct rte_crypto_sym_xform *auth_xform = NULL;
643 const struct rte_crypto_sym_xform *cipher_xform = NULL;
644 const struct rte_crypto_sym_xform *aead_xform = NULL;
647 /* Select Crypto operation - hash then cipher / cipher then hash */
648 switch (aesni_mb_get_chain_order(xform)) {
649 case AESNI_MB_OP_HASH_CIPHER:
650 sess->chain_order = HASH_CIPHER;
652 cipher_xform = xform->next;
654 case AESNI_MB_OP_CIPHER_HASH:
655 sess->chain_order = CIPHER_HASH;
656 auth_xform = xform->next;
657 cipher_xform = xform;
659 case AESNI_MB_OP_HASH_ONLY:
660 sess->chain_order = HASH_CIPHER;
664 case AESNI_MB_OP_CIPHER_ONLY:
666 * Multi buffer library operates only at two modes,
667 * CIPHER_HASH and HASH_CIPHER. When doing ciphering only,
668 * chain order depends on cipher operation: encryption is always
669 * the first operation and decryption the last one.
671 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
672 sess->chain_order = CIPHER_HASH;
674 sess->chain_order = HASH_CIPHER;
676 cipher_xform = xform;
678 case AESNI_MB_OP_AEAD_CIPHER_HASH:
679 sess->chain_order = CIPHER_HASH;
680 sess->aead.aad_len = xform->aead.aad_length;
683 case AESNI_MB_OP_AEAD_HASH_CIPHER:
684 sess->chain_order = HASH_CIPHER;
685 sess->aead.aad_len = xform->aead.aad_length;
688 case AESNI_MB_OP_NOT_SUPPORTED:
690 AESNI_MB_LOG(ERR, "Unsupported operation chain order parameter");
694 /* Default IV length = 0 */
697 ret = aesni_mb_set_session_auth_parameters(mb_mgr, sess, auth_xform);
699 AESNI_MB_LOG(ERR, "Invalid/unsupported authentication parameters");
703 ret = aesni_mb_set_session_cipher_parameters(mb_mgr, sess,
706 AESNI_MB_LOG(ERR, "Invalid/unsupported cipher parameters");
711 ret = aesni_mb_set_session_aead_parameters(mb_mgr, sess,
714 AESNI_MB_LOG(ERR, "Invalid/unsupported aead parameters");
722 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
723 /** Check DOCSIS security session configuration is valid */
725 check_docsis_sec_session(struct rte_security_session_conf *conf)
727 struct rte_crypto_sym_xform *crypto_sym = conf->crypto_xform;
728 struct rte_security_docsis_xform *docsis = &conf->docsis;
730 /* Downlink: CRC generate -> Cipher encrypt */
731 if (docsis->direction == RTE_SECURITY_DOCSIS_DOWNLINK) {
733 if (crypto_sym != NULL &&
734 crypto_sym->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
735 crypto_sym->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
736 crypto_sym->cipher.algo ==
737 RTE_CRYPTO_CIPHER_AES_DOCSISBPI &&
738 (crypto_sym->cipher.key.length == IMB_KEY_AES_128_BYTES ||
739 crypto_sym->cipher.key.length == IMB_KEY_AES_256_BYTES) &&
740 crypto_sym->cipher.iv.length == AES_BLOCK_SIZE &&
741 crypto_sym->next == NULL) {
744 /* Uplink: Cipher decrypt -> CRC verify */
745 } else if (docsis->direction == RTE_SECURITY_DOCSIS_UPLINK) {
747 if (crypto_sym != NULL &&
748 crypto_sym->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
749 crypto_sym->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT &&
750 crypto_sym->cipher.algo ==
751 RTE_CRYPTO_CIPHER_AES_DOCSISBPI &&
752 (crypto_sym->cipher.key.length == IMB_KEY_AES_128_BYTES ||
753 crypto_sym->cipher.key.length == IMB_KEY_AES_256_BYTES) &&
754 crypto_sym->cipher.iv.length == AES_BLOCK_SIZE &&
755 crypto_sym->next == NULL) {
763 /** Set DOCSIS security session auth (CRC) parameters */
765 aesni_mb_set_docsis_sec_session_auth_parameters(struct aesni_mb_session *sess,
766 struct rte_security_docsis_xform *xform)
769 AESNI_MB_LOG(ERR, "Invalid DOCSIS xform");
773 /* Select CRC generate/verify */
774 if (xform->direction == RTE_SECURITY_DOCSIS_UPLINK) {
775 sess->auth.algo = IMB_AUTH_DOCSIS_CRC32;
776 sess->auth.operation = RTE_CRYPTO_AUTH_OP_VERIFY;
777 } else if (xform->direction == RTE_SECURITY_DOCSIS_DOWNLINK) {
778 sess->auth.algo = IMB_AUTH_DOCSIS_CRC32;
779 sess->auth.operation = RTE_CRYPTO_AUTH_OP_GENERATE;
781 AESNI_MB_LOG(ERR, "Unsupported DOCSIS direction");
785 sess->auth.req_digest_len = RTE_ETHER_CRC_LEN;
786 sess->auth.gen_digest_len = RTE_ETHER_CRC_LEN;
792 * Parse DOCSIS security session configuration and set private session
796 aesni_mb_set_docsis_sec_session_parameters(
797 __rte_unused struct rte_cryptodev *dev,
798 struct rte_security_session_conf *conf,
801 struct rte_security_docsis_xform *docsis_xform;
802 struct rte_crypto_sym_xform *cipher_xform;
803 struct aesni_mb_session *aesni_sess = sess;
804 struct aesni_mb_private *internals = dev->data->dev_private;
807 ret = check_docsis_sec_session(conf);
809 AESNI_MB_LOG(ERR, "Unsupported DOCSIS security configuration");
813 switch (conf->docsis.direction) {
814 case RTE_SECURITY_DOCSIS_UPLINK:
815 aesni_sess->chain_order = IMB_ORDER_CIPHER_HASH;
816 docsis_xform = &conf->docsis;
817 cipher_xform = conf->crypto_xform;
819 case RTE_SECURITY_DOCSIS_DOWNLINK:
820 aesni_sess->chain_order = IMB_ORDER_HASH_CIPHER;
821 cipher_xform = conf->crypto_xform;
822 docsis_xform = &conf->docsis;
828 /* Default IV length = 0 */
829 aesni_sess->iv.length = 0;
831 ret = aesni_mb_set_docsis_sec_session_auth_parameters(aesni_sess,
834 AESNI_MB_LOG(ERR, "Invalid/unsupported DOCSIS parameters");
838 ret = aesni_mb_set_session_cipher_parameters(internals->mb_mgr,
839 aesni_sess, cipher_xform);
842 AESNI_MB_LOG(ERR, "Invalid/unsupported cipher parameters");
851 * burst enqueue, place crypto operations on ingress queue for processing.
853 * @param __qp Queue Pair to process
854 * @param ops Crypto operations for processing
855 * @param nb_ops Number of crypto operations for processing
858 * - Number of crypto operations enqueued
861 aesni_mb_pmd_enqueue_burst(void *__qp, struct rte_crypto_op **ops,
864 struct aesni_mb_qp *qp = __qp;
866 unsigned int nb_enqueued;
868 nb_enqueued = rte_ring_enqueue_burst(qp->ingress_queue,
869 (void **)ops, nb_ops, NULL);
871 qp->stats.enqueued_count += nb_enqueued;
876 /** Get multi buffer session */
877 static inline struct aesni_mb_session *
878 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *op)
880 struct aesni_mb_session *sess = NULL;
882 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
883 if (likely(op->sym->session != NULL))
884 sess = (struct aesni_mb_session *)
885 get_sym_session_private_data(
887 cryptodev_driver_id);
888 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
889 } else if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
890 if (likely(op->sym->sec_session != NULL))
891 sess = (struct aesni_mb_session *)
892 get_sec_session_private_data(
893 op->sym->sec_session);
896 void *_sess = rte_cryptodev_sym_session_create(qp->sess_mp);
897 void *_sess_private_data = NULL;
902 if (rte_mempool_get(qp->sess_mp_priv,
903 (void **)&_sess_private_data))
906 sess = (struct aesni_mb_session *)_sess_private_data;
908 if (unlikely(aesni_mb_set_session_parameters(qp->mb_mgr,
909 sess, op->sym->xform) != 0)) {
910 rte_mempool_put(qp->sess_mp, _sess);
911 rte_mempool_put(qp->sess_mp_priv, _sess_private_data);
914 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
915 set_sym_session_private_data(op->sym->session,
916 cryptodev_driver_id, _sess_private_data);
919 if (unlikely(sess == NULL))
920 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
925 static inline uint64_t
926 auth_start_offset(struct rte_crypto_op *op, struct aesni_mb_session *session,
929 struct rte_mbuf *m_src, *m_dst;
930 uint8_t *p_src, *p_dst;
931 uintptr_t u_src, u_dst;
932 uint32_t cipher_end, auth_end;
934 /* Only cipher then hash needs special calculation. */
935 if (!oop || session->chain_order != CIPHER_HASH)
936 return op->sym->auth.data.offset;
938 m_src = op->sym->m_src;
939 m_dst = op->sym->m_dst;
941 p_src = rte_pktmbuf_mtod(m_src, uint8_t *);
942 p_dst = rte_pktmbuf_mtod(m_dst, uint8_t *);
943 u_src = (uintptr_t)p_src;
944 u_dst = (uintptr_t)p_dst + op->sym->auth.data.offset;
947 * Copy the content between cipher offset and auth offset for generating
950 if (op->sym->cipher.data.offset > op->sym->auth.data.offset)
951 memcpy(p_dst + op->sym->auth.data.offset,
952 p_src + op->sym->auth.data.offset,
953 op->sym->cipher.data.offset -
954 op->sym->auth.data.offset);
957 * Copy the content between (cipher offset + length) and (auth offset +
958 * length) for generating correct digest
960 cipher_end = op->sym->cipher.data.offset + op->sym->cipher.data.length;
961 auth_end = op->sym->auth.data.offset + op->sym->auth.data.length;
962 if (cipher_end < auth_end)
963 memcpy(p_dst + cipher_end, p_src + cipher_end,
964 auth_end - cipher_end);
967 * Since intel-ipsec-mb only supports positive values,
968 * we need to deduct the correct offset between src and dst.
971 return u_src < u_dst ? (u_dst - u_src) :
972 (UINT64_MAX - u_src + u_dst + 1);
976 set_cpu_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_session *session,
977 union rte_crypto_sym_ofs sofs, void *buf, uint32_t len,
978 void *iv, void *aad, void *digest, void *udata)
980 /* Set crypto operation */
981 job->chain_order = session->chain_order;
983 /* Set cipher parameters */
984 job->cipher_direction = session->cipher.direction;
985 job->cipher_mode = session->cipher.mode;
987 job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
989 /* Set authentication parameters */
990 job->hash_alg = session->auth.algo;
993 switch (job->hash_alg) {
995 job->u.XCBC._k1_expanded = session->auth.xcbc.k1_expanded;
996 job->u.XCBC._k2 = session->auth.xcbc.k2;
997 job->u.XCBC._k3 = session->auth.xcbc.k3;
999 job->aes_enc_key_expanded =
1000 session->cipher.expanded_aes_keys.encode;
1001 job->aes_dec_key_expanded =
1002 session->cipher.expanded_aes_keys.decode;
1006 job->u.CCM.aad = (uint8_t *)aad + 18;
1007 job->u.CCM.aad_len_in_bytes = session->aead.aad_len;
1008 job->aes_enc_key_expanded =
1009 session->cipher.expanded_aes_keys.encode;
1010 job->aes_dec_key_expanded =
1011 session->cipher.expanded_aes_keys.decode;
1016 job->u.CMAC._key_expanded = session->auth.cmac.expkey;
1017 job->u.CMAC._skey1 = session->auth.cmac.skey1;
1018 job->u.CMAC._skey2 = session->auth.cmac.skey2;
1019 job->aes_enc_key_expanded =
1020 session->cipher.expanded_aes_keys.encode;
1021 job->aes_dec_key_expanded =
1022 session->cipher.expanded_aes_keys.decode;
1026 if (session->cipher.mode == GCM) {
1027 job->u.GCM.aad = aad;
1028 job->u.GCM.aad_len_in_bytes = session->aead.aad_len;
1031 job->u.GCM.aad = buf;
1032 job->u.GCM.aad_len_in_bytes = len;
1033 job->cipher_mode = GCM;
1035 job->aes_enc_key_expanded = &session->cipher.gcm_key;
1036 job->aes_dec_key_expanded = &session->cipher.gcm_key;
1040 job->u.HMAC._hashed_auth_key_xor_ipad =
1041 session->auth.pads.inner;
1042 job->u.HMAC._hashed_auth_key_xor_opad =
1043 session->auth.pads.outer;
1045 if (job->cipher_mode == DES3) {
1046 job->aes_enc_key_expanded =
1047 session->cipher.exp_3des_keys.ks_ptr;
1048 job->aes_dec_key_expanded =
1049 session->cipher.exp_3des_keys.ks_ptr;
1051 job->aes_enc_key_expanded =
1052 session->cipher.expanded_aes_keys.encode;
1053 job->aes_dec_key_expanded =
1054 session->cipher.expanded_aes_keys.decode;
1059 * Multi-buffer library current only support returning a truncated
1060 * digest length as specified in the relevant IPsec RFCs
1063 /* Set digest location and length */
1064 job->auth_tag_output = digest;
1065 job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
1067 /* Set IV parameters */
1068 job->iv_len_in_bytes = session->iv.length;
1070 /* Data Parameters */
1072 job->dst = (uint8_t *)buf + sofs.ofs.cipher.head;
1073 job->cipher_start_src_offset_in_bytes = sofs.ofs.cipher.head;
1074 job->hash_start_src_offset_in_bytes = sofs.ofs.auth.head;
1075 if (job->hash_alg == AES_GMAC && session->cipher.mode != GCM) {
1076 job->msg_len_to_hash_in_bytes = 0;
1077 job->msg_len_to_cipher_in_bytes = 0;
1079 job->msg_len_to_hash_in_bytes = len - sofs.ofs.auth.head -
1081 job->msg_len_to_cipher_in_bytes = len - sofs.ofs.cipher.head -
1082 sofs.ofs.cipher.tail;
1085 job->user_data = udata;
1089 * Process a crypto operation and complete a JOB_AES_HMAC job structure for
1090 * submission to the multi buffer library for processing.
1092 * @param qp queue pair
1093 * @param job JOB_AES_HMAC structure to fill
1094 * @param m mbuf to process
1097 * - Completed JOB_AES_HMAC structure pointer on success
1098 * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
1101 set_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_qp *qp,
1102 struct rte_crypto_op *op, uint8_t *digest_idx)
1104 struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
1105 struct aesni_mb_session *session;
1106 uint32_t m_offset, oop;
1108 session = get_session(qp, op);
1109 if (session == NULL) {
1110 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1114 /* Set crypto operation */
1115 job->chain_order = session->chain_order;
1117 /* Set cipher parameters */
1118 job->cipher_direction = session->cipher.direction;
1119 job->cipher_mode = session->cipher.mode;
1121 job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
1123 /* Set authentication parameters */
1124 job->hash_alg = session->auth.algo;
1126 switch (job->hash_alg) {
1128 job->u.XCBC._k1_expanded = session->auth.xcbc.k1_expanded;
1129 job->u.XCBC._k2 = session->auth.xcbc.k2;
1130 job->u.XCBC._k3 = session->auth.xcbc.k3;
1132 job->aes_enc_key_expanded =
1133 session->cipher.expanded_aes_keys.encode;
1134 job->aes_dec_key_expanded =
1135 session->cipher.expanded_aes_keys.decode;
1139 job->u.CCM.aad = op->sym->aead.aad.data + 18;
1140 job->u.CCM.aad_len_in_bytes = session->aead.aad_len;
1141 job->aes_enc_key_expanded =
1142 session->cipher.expanded_aes_keys.encode;
1143 job->aes_dec_key_expanded =
1144 session->cipher.expanded_aes_keys.decode;
1148 job->u.CMAC._key_expanded = session->auth.cmac.expkey;
1149 job->u.CMAC._skey1 = session->auth.cmac.skey1;
1150 job->u.CMAC._skey2 = session->auth.cmac.skey2;
1151 job->aes_enc_key_expanded =
1152 session->cipher.expanded_aes_keys.encode;
1153 job->aes_dec_key_expanded =
1154 session->cipher.expanded_aes_keys.decode;
1158 if (session->cipher.mode == GCM) {
1159 job->u.GCM.aad = op->sym->aead.aad.data;
1160 job->u.GCM.aad_len_in_bytes = session->aead.aad_len;
1163 job->u.GCM.aad = rte_pktmbuf_mtod_offset(m_src,
1164 uint8_t *, op->sym->auth.data.offset);
1165 job->u.GCM.aad_len_in_bytes = op->sym->auth.data.length;
1166 job->cipher_mode = GCM;
1168 job->aes_enc_key_expanded = &session->cipher.gcm_key;
1169 job->aes_dec_key_expanded = &session->cipher.gcm_key;
1173 job->u.HMAC._hashed_auth_key_xor_ipad = session->auth.pads.inner;
1174 job->u.HMAC._hashed_auth_key_xor_opad = session->auth.pads.outer;
1176 if (job->cipher_mode == DES3) {
1177 job->aes_enc_key_expanded =
1178 session->cipher.exp_3des_keys.ks_ptr;
1179 job->aes_dec_key_expanded =
1180 session->cipher.exp_3des_keys.ks_ptr;
1182 job->aes_enc_key_expanded =
1183 session->cipher.expanded_aes_keys.encode;
1184 job->aes_dec_key_expanded =
1185 session->cipher.expanded_aes_keys.decode;
1189 if (!op->sym->m_dst) {
1190 /* in-place operation */
1193 } else if (op->sym->m_dst == op->sym->m_src) {
1194 /* in-place operation */
1198 /* out-of-place operation */
1199 m_dst = op->sym->m_dst;
1203 if (job->hash_alg == AES_CCM || (job->hash_alg == AES_GMAC &&
1204 session->cipher.mode == GCM))
1205 m_offset = op->sym->aead.data.offset;
1207 m_offset = op->sym->cipher.data.offset;
1209 /* Set digest output location */
1210 if (job->hash_alg != NULL_HASH &&
1211 session->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1212 job->auth_tag_output = qp->temp_digests[*digest_idx];
1213 *digest_idx = (*digest_idx + 1) % MAX_JOBS;
1215 if (job->hash_alg == AES_CCM || (job->hash_alg == AES_GMAC &&
1216 session->cipher.mode == GCM))
1217 job->auth_tag_output = op->sym->aead.digest.data;
1219 job->auth_tag_output = op->sym->auth.digest.data;
1221 if (session->auth.req_digest_len != session->auth.gen_digest_len) {
1222 job->auth_tag_output = qp->temp_digests[*digest_idx];
1223 *digest_idx = (*digest_idx + 1) % MAX_JOBS;
1227 * Multi-buffer library current only support returning a truncated
1228 * digest length as specified in the relevant IPsec RFCs
1231 /* Set digest length */
1232 job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
1234 /* Set IV parameters */
1235 job->iv_len_in_bytes = session->iv.length;
1237 /* Data Parameters */
1238 job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
1239 job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
1241 switch (job->hash_alg) {
1243 job->cipher_start_src_offset_in_bytes =
1244 op->sym->aead.data.offset;
1245 job->msg_len_to_cipher_in_bytes = op->sym->aead.data.length;
1246 job->hash_start_src_offset_in_bytes = op->sym->aead.data.offset;
1247 job->msg_len_to_hash_in_bytes = op->sym->aead.data.length;
1249 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1250 session->iv.offset + 1);
1254 if (session->cipher.mode == GCM) {
1255 job->cipher_start_src_offset_in_bytes =
1256 op->sym->aead.data.offset;
1257 job->hash_start_src_offset_in_bytes =
1258 op->sym->aead.data.offset;
1259 job->msg_len_to_cipher_in_bytes =
1260 op->sym->aead.data.length;
1261 job->msg_len_to_hash_in_bytes =
1262 op->sym->aead.data.length;
1264 job->cipher_start_src_offset_in_bytes =
1265 op->sym->auth.data.offset;
1266 job->hash_start_src_offset_in_bytes =
1267 op->sym->auth.data.offset;
1268 job->msg_len_to_cipher_in_bytes = 0;
1269 job->msg_len_to_hash_in_bytes = 0;
1272 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1273 session->iv.offset);
1277 job->cipher_start_src_offset_in_bytes =
1278 op->sym->cipher.data.offset;
1279 job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
1281 job->hash_start_src_offset_in_bytes = auth_start_offset(op,
1283 job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
1285 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1286 session->iv.offset);
1289 /* Set user data to be crypto operation data struct */
1290 job->user_data = op;
1295 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1297 * Process a crypto operation containing a security op and complete a
1298 * JOB_AES_HMAC job structure for submission to the multi buffer library for
1302 set_sec_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_qp *qp,
1303 struct rte_crypto_op *op, uint8_t *digest_idx)
1305 struct rte_mbuf *m_src, *m_dst;
1306 struct rte_crypto_sym_op *sym;
1307 struct aesni_mb_session *session;
1309 session = get_session(qp, op);
1310 if (unlikely(session == NULL)) {
1311 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1315 /* Only DOCSIS protocol operations supported now */
1316 if (session->cipher.mode != IMB_CIPHER_DOCSIS_SEC_BPI ||
1317 session->auth.algo != IMB_AUTH_DOCSIS_CRC32) {
1318 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1325 if (likely(sym->m_dst == NULL || sym->m_dst == m_src)) {
1326 /* in-place operation */
1329 /* out-of-place operation not supported */
1330 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1334 /* Set crypto operation */
1335 job->chain_order = session->chain_order;
1337 /* Set cipher parameters */
1338 job->cipher_direction = session->cipher.direction;
1339 job->cipher_mode = session->cipher.mode;
1341 job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
1342 job->aes_enc_key_expanded = session->cipher.expanded_aes_keys.encode;
1343 job->aes_dec_key_expanded = session->cipher.expanded_aes_keys.decode;
1345 /* Set IV parameters */
1346 job->iv_len_in_bytes = session->iv.length;
1347 job->iv = (uint8_t *)op + session->iv.offset;
1349 /* Set authentication parameters */
1350 job->hash_alg = session->auth.algo;
1352 /* Set digest output location */
1353 job->auth_tag_output = qp->temp_digests[*digest_idx];
1354 *digest_idx = (*digest_idx + 1) % MAX_JOBS;
1356 /* Set digest length */
1357 job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
1359 /* Set data parameters */
1360 job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
1361 job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *,
1362 sym->cipher.data.offset);
1364 job->cipher_start_src_offset_in_bytes = sym->cipher.data.offset;
1365 job->msg_len_to_cipher_in_bytes = sym->cipher.data.length;
1367 job->hash_start_src_offset_in_bytes = sym->auth.data.offset;
1368 job->msg_len_to_hash_in_bytes = sym->auth.data.length;
1370 job->user_data = op;
1376 verify_docsis_sec_crc(JOB_AES_HMAC *job, uint8_t *status)
1378 uint16_t crc_offset;
1381 if (!job->msg_len_to_hash_in_bytes)
1384 crc_offset = job->hash_start_src_offset_in_bytes +
1385 job->msg_len_to_hash_in_bytes -
1386 job->cipher_start_src_offset_in_bytes;
1387 crc = job->dst + crc_offset;
1389 /* Verify CRC (at the end of the message) */
1390 if (memcmp(job->auth_tag_output, crc, RTE_ETHER_CRC_LEN) != 0)
1391 *status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1396 verify_digest(JOB_AES_HMAC *job, void *digest, uint16_t len, uint8_t *status)
1398 /* Verify digest if required */
1399 if (memcmp(job->auth_tag_output, digest, len) != 0)
1400 *status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1404 generate_digest(JOB_AES_HMAC *job, struct rte_crypto_op *op,
1405 struct aesni_mb_session *sess)
1407 /* No extra copy needed */
1408 if (likely(sess->auth.req_digest_len == sess->auth.gen_digest_len))
1412 * This can only happen for HMAC, so only digest
1413 * for authentication algos is required
1415 memcpy(op->sym->auth.digest.data, job->auth_tag_output,
1416 sess->auth.req_digest_len);
1420 * Process a completed job and return rte_mbuf which job processed
1422 * @param qp Queue Pair to process
1423 * @param job JOB_AES_HMAC job to process
1426 * - Returns processed crypto operation.
1427 * - Returns NULL on invalid job
1429 static inline struct rte_crypto_op *
1430 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
1432 struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
1433 struct aesni_mb_session *sess = NULL;
1435 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1436 uint8_t is_docsis_sec = 0;
1438 if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1440 * Assuming at this point that if it's a security type op, that
1441 * this is for DOCSIS
1444 sess = get_sec_session_private_data(op->sym->sec_session);
1448 sess = get_sym_session_private_data(op->sym->session,
1449 cryptodev_driver_id);
1452 if (unlikely(sess == NULL)) {
1453 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1457 if (likely(op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)) {
1458 switch (job->status) {
1460 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1462 if (job->hash_alg == NULL_HASH)
1465 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1466 if (job->hash_alg == AES_CCM ||
1467 (job->hash_alg == AES_GMAC &&
1468 sess->cipher.mode == GCM))
1470 op->sym->aead.digest.data,
1471 sess->auth.req_digest_len,
1473 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1474 else if (is_docsis_sec)
1475 verify_docsis_sec_crc(job,
1480 op->sym->auth.digest.data,
1481 sess->auth.req_digest_len,
1484 generate_digest(job, op, sess);
1487 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1491 /* Free session if a session-less crypto op */
1492 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1493 memset(sess, 0, sizeof(struct aesni_mb_session));
1494 memset(op->sym->session, 0,
1495 rte_cryptodev_sym_get_existing_header_session_size(
1497 rte_mempool_put(qp->sess_mp_priv, sess);
1498 rte_mempool_put(qp->sess_mp, op->sym->session);
1499 op->sym->session = NULL;
1506 post_process_mb_sync_job(JOB_AES_HMAC *job)
1510 st = job->user_data;
1511 st[0] = (job->status == STS_COMPLETED) ? 0 : EBADMSG;
1515 * Process a completed JOB_AES_HMAC job and keep processing jobs until
1516 * get_completed_job return NULL
1518 * @param qp Queue Pair to process
1519 * @param job JOB_AES_HMAC job
1522 * - Number of processed jobs
1525 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job,
1526 struct rte_crypto_op **ops, uint16_t nb_ops)
1528 struct rte_crypto_op *op = NULL;
1529 unsigned processed_jobs = 0;
1531 while (job != NULL) {
1532 op = post_process_mb_job(qp, job);
1535 ops[processed_jobs++] = op;
1536 qp->stats.dequeued_count++;
1538 qp->stats.dequeue_err_count++;
1541 if (processed_jobs == nb_ops)
1544 job = IMB_GET_COMPLETED_JOB(qp->mb_mgr);
1547 return processed_jobs;
1550 static inline uint32_t
1551 handle_completed_sync_jobs(JOB_AES_HMAC *job, MB_MGR *mb_mgr)
1555 for (i = 0; job != NULL; i++, job = IMB_GET_COMPLETED_JOB(mb_mgr))
1556 post_process_mb_sync_job(job);
1561 static inline uint32_t
1562 flush_mb_sync_mgr(MB_MGR *mb_mgr)
1566 job = IMB_FLUSH_JOB(mb_mgr);
1567 return handle_completed_sync_jobs(job, mb_mgr);
1570 static inline uint16_t
1571 flush_mb_mgr(struct aesni_mb_qp *qp, struct rte_crypto_op **ops,
1574 int processed_ops = 0;
1576 /* Flush the remaining jobs */
1577 JOB_AES_HMAC *job = IMB_FLUSH_JOB(qp->mb_mgr);
1580 processed_ops += handle_completed_jobs(qp, job,
1581 &ops[processed_ops], nb_ops - processed_ops);
1583 return processed_ops;
1586 static inline JOB_AES_HMAC *
1587 set_job_null_op(JOB_AES_HMAC *job, struct rte_crypto_op *op)
1589 job->chain_order = HASH_CIPHER;
1590 job->cipher_mode = NULL_CIPHER;
1591 job->hash_alg = NULL_HASH;
1592 job->cipher_direction = DECRYPT;
1594 /* Set user data to be crypto operation data struct */
1595 job->user_data = op;
1601 aesni_mb_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1604 struct aesni_mb_qp *qp = queue_pair;
1606 struct rte_crypto_op *op;
1609 int retval, processed_jobs = 0;
1611 if (unlikely(nb_ops == 0))
1614 uint8_t digest_idx = qp->digest_idx;
1616 /* Get next free mb job struct from mb manager */
1617 job = IMB_GET_NEXT_JOB(qp->mb_mgr);
1618 if (unlikely(job == NULL)) {
1619 /* if no free mb job structs we need to flush mb_mgr */
1620 processed_jobs += flush_mb_mgr(qp,
1621 &ops[processed_jobs],
1622 nb_ops - processed_jobs);
1624 if (nb_ops == processed_jobs)
1627 job = IMB_GET_NEXT_JOB(qp->mb_mgr);
1631 * Get next operation to process from ingress queue.
1632 * There is no need to return the job to the MB_MGR
1633 * if there are no more operations to process, since the MB_MGR
1634 * can use that pointer again in next get_next calls.
1636 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
1640 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1641 if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION)
1642 retval = set_sec_mb_job_params(job, qp, op,
1646 retval = set_mb_job_params(job, qp, op, &digest_idx);
1648 if (unlikely(retval != 0)) {
1649 qp->stats.dequeue_err_count++;
1650 set_job_null_op(job, op);
1653 /* Submit job to multi-buffer for processing */
1654 #ifdef RTE_LIBRTE_PMD_AESNI_MB_DEBUG
1655 job = IMB_SUBMIT_JOB(qp->mb_mgr);
1657 job = IMB_SUBMIT_JOB_NOCHECK(qp->mb_mgr);
1660 * If submit returns a processed job then handle it,
1661 * before submitting subsequent jobs
1664 processed_jobs += handle_completed_jobs(qp, job,
1665 &ops[processed_jobs],
1666 nb_ops - processed_jobs);
1668 } while (processed_jobs < nb_ops);
1670 qp->digest_idx = digest_idx;
1672 if (processed_jobs < 1)
1673 processed_jobs += flush_mb_mgr(qp,
1674 &ops[processed_jobs],
1675 nb_ops - processed_jobs);
1677 return processed_jobs;
1681 alloc_init_mb_mgr(enum aesni_mb_vector_mode vector_mode)
1683 MB_MGR *mb_mgr = alloc_mb_mgr(0);
1687 switch (vector_mode) {
1688 case RTE_AESNI_MB_SSE:
1689 init_mb_mgr_sse(mb_mgr);
1691 case RTE_AESNI_MB_AVX:
1692 init_mb_mgr_avx(mb_mgr);
1694 case RTE_AESNI_MB_AVX2:
1695 init_mb_mgr_avx2(mb_mgr);
1697 case RTE_AESNI_MB_AVX512:
1698 init_mb_mgr_avx512(mb_mgr);
1701 AESNI_MB_LOG(ERR, "Unsupported vector mode %u\n", vector_mode);
1702 free_mb_mgr(mb_mgr);
1710 aesni_mb_fill_error_code(struct rte_crypto_sym_vec *vec, int32_t err)
1714 for (i = 0; i != vec->num; ++i)
1715 vec->status[i] = err;
1719 check_crypto_sgl(union rte_crypto_sym_ofs so, const struct rte_crypto_sgl *sgl)
1721 /* no multi-seg support with current AESNI-MB PMD */
1724 else if (so.ofs.cipher.head + so.ofs.cipher.tail > sgl->vec[0].len)
1729 static inline JOB_AES_HMAC *
1730 submit_sync_job(MB_MGR *mb_mgr)
1732 #ifdef RTE_LIBRTE_PMD_AESNI_MB_DEBUG
1733 return IMB_SUBMIT_JOB(mb_mgr);
1735 return IMB_SUBMIT_JOB_NOCHECK(mb_mgr);
1739 static inline uint32_t
1740 generate_sync_dgst(struct rte_crypto_sym_vec *vec,
1741 const uint8_t dgst[][DIGEST_LENGTH_MAX], uint32_t len)
1745 for (i = 0, k = 0; i != vec->num; i++) {
1746 if (vec->status[i] == 0) {
1747 memcpy(vec->digest[i], dgst[i], len);
1755 static inline uint32_t
1756 verify_sync_dgst(struct rte_crypto_sym_vec *vec,
1757 const uint8_t dgst[][DIGEST_LENGTH_MAX], uint32_t len)
1761 for (i = 0, k = 0; i != vec->num; i++) {
1762 if (vec->status[i] == 0) {
1763 if (memcmp(vec->digest[i], dgst[i], len) != 0)
1764 vec->status[i] = EBADMSG;
1774 aesni_mb_cpu_crypto_process_bulk(struct rte_cryptodev *dev,
1775 struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs sofs,
1776 struct rte_crypto_sym_vec *vec)
1779 uint32_t i, j, k, len;
1783 struct aesni_mb_private *priv;
1784 struct aesni_mb_session *s;
1785 uint8_t tmp_dgst[vec->num][DIGEST_LENGTH_MAX];
1787 s = get_sym_session_private_data(sess, dev->driver_id);
1789 aesni_mb_fill_error_code(vec, EINVAL);
1793 /* get per-thread MB MGR, create one if needed */
1794 mb_mgr = RTE_PER_LCORE(sync_mb_mgr);
1795 if (mb_mgr == NULL) {
1797 priv = dev->data->dev_private;
1798 mb_mgr = alloc_init_mb_mgr(priv->vector_mode);
1799 if (mb_mgr == NULL) {
1800 aesni_mb_fill_error_code(vec, ENOMEM);
1803 RTE_PER_LCORE(sync_mb_mgr) = mb_mgr;
1806 for (i = 0, j = 0, k = 0; i != vec->num; i++) {
1809 ret = check_crypto_sgl(sofs, vec->sgl + i);
1811 vec->status[i] = ret;
1815 buf = vec->sgl[i].vec[0].base;
1816 len = vec->sgl[i].vec[0].len;
1818 job = IMB_GET_NEXT_JOB(mb_mgr);
1820 k += flush_mb_sync_mgr(mb_mgr);
1821 job = IMB_GET_NEXT_JOB(mb_mgr);
1822 RTE_ASSERT(job != NULL);
1825 /* Submit job for processing */
1826 set_cpu_mb_job_params(job, s, sofs, buf, len,
1827 vec->iv[i], vec->aad[i], tmp_dgst[i],
1829 job = submit_sync_job(mb_mgr);
1832 /* handle completed jobs */
1833 k += handle_completed_sync_jobs(job, mb_mgr);
1836 /* flush remaining jobs */
1838 k += flush_mb_sync_mgr(mb_mgr);
1840 /* finish processing for successful jobs: check/update digest */
1842 if (s->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1843 k = verify_sync_dgst(vec,
1844 (const uint8_t (*)[DIGEST_LENGTH_MAX])tmp_dgst,
1845 s->auth.req_digest_len);
1847 k = generate_sync_dgst(vec,
1848 (const uint8_t (*)[DIGEST_LENGTH_MAX])tmp_dgst,
1849 s->auth.req_digest_len);
1855 static int cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev);
1858 vec_mode_to_flags(enum aesni_mb_vector_mode mode)
1861 case RTE_AESNI_MB_SSE:
1862 return RTE_CRYPTODEV_FF_CPU_SSE;
1863 case RTE_AESNI_MB_AVX:
1864 return RTE_CRYPTODEV_FF_CPU_AVX;
1865 case RTE_AESNI_MB_AVX2:
1866 return RTE_CRYPTODEV_FF_CPU_AVX2;
1867 case RTE_AESNI_MB_AVX512:
1868 return RTE_CRYPTODEV_FF_CPU_AVX512;
1870 AESNI_MB_LOG(ERR, "Unsupported vector mode %u\n", mode);
1876 cryptodev_aesni_mb_create(const char *name,
1877 struct rte_vdev_device *vdev,
1878 struct rte_cryptodev_pmd_init_params *init_params)
1880 struct rte_cryptodev *dev;
1881 struct aesni_mb_private *internals;
1882 enum aesni_mb_vector_mode vector_mode;
1884 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1885 struct rte_security_ctx *security_instance;
1888 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
1890 AESNI_MB_LOG(ERR, "failed to create cryptodev vdev");
1894 /* Check CPU for supported vector instruction set */
1895 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F))
1896 vector_mode = RTE_AESNI_MB_AVX512;
1897 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
1898 vector_mode = RTE_AESNI_MB_AVX2;
1899 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
1900 vector_mode = RTE_AESNI_MB_AVX;
1902 vector_mode = RTE_AESNI_MB_SSE;
1904 dev->driver_id = cryptodev_driver_id;
1905 dev->dev_ops = rte_aesni_mb_pmd_ops;
1907 /* register rx/tx burst functions for data path */
1908 dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
1909 dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
1911 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1912 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1913 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
1914 RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO |
1915 RTE_CRYPTODEV_FF_SYM_SESSIONLESS
1916 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1917 | RTE_CRYPTODEV_FF_SECURITY
1921 #ifdef AESNI_MB_DOCSIS_SEC_ENABLED
1922 security_instance = rte_malloc("aesni_mb_sec",
1923 sizeof(struct rte_security_ctx),
1924 RTE_CACHE_LINE_SIZE);
1925 if (security_instance == NULL) {
1926 AESNI_MB_LOG(ERR, "rte_security_ctx memory alloc failed");
1927 rte_cryptodev_pmd_destroy(dev);
1931 security_instance->device = (void *)dev;
1932 security_instance->ops = rte_aesni_mb_pmd_sec_ops;
1933 security_instance->sess_cnt = 0;
1934 dev->security_ctx = security_instance;
1937 /* Check CPU for support for AES instruction set */
1938 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES))
1939 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AESNI;
1941 AESNI_MB_LOG(WARNING, "AES instructions not supported by CPU");
1943 dev->feature_flags |= vec_mode_to_flags(vector_mode);
1945 mb_mgr = alloc_init_mb_mgr(vector_mode);
1946 if (mb_mgr == NULL) {
1947 rte_cryptodev_pmd_destroy(dev);
1951 /* Set vector instructions mode supported */
1952 internals = dev->data->dev_private;
1954 internals->vector_mode = vector_mode;
1955 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
1956 internals->mb_mgr = mb_mgr;
1958 AESNI_MB_LOG(INFO, "IPSec Multi-buffer library version used: %s\n",
1959 imb_get_version_str());
1964 cryptodev_aesni_mb_probe(struct rte_vdev_device *vdev)
1966 struct rte_cryptodev_pmd_init_params init_params = {
1968 sizeof(struct aesni_mb_private),
1970 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
1972 const char *name, *args;
1975 name = rte_vdev_device_name(vdev);
1979 args = rte_vdev_device_args(vdev);
1981 retval = rte_cryptodev_pmd_parse_input_args(&init_params, args);
1983 AESNI_MB_LOG(ERR, "Failed to parse initialisation arguments[%s]",
1988 return cryptodev_aesni_mb_create(name, vdev, &init_params);
1992 cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev)
1994 struct rte_cryptodev *cryptodev;
1995 struct aesni_mb_private *internals;
1998 name = rte_vdev_device_name(vdev);
2002 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
2003 if (cryptodev == NULL)
2006 internals = cryptodev->data->dev_private;
2008 free_mb_mgr(internals->mb_mgr);
2009 if (RTE_PER_LCORE(sync_mb_mgr)) {
2010 free_mb_mgr(RTE_PER_LCORE(sync_mb_mgr));
2011 RTE_PER_LCORE(sync_mb_mgr) = NULL;
2014 #ifdef RTE_LIBRTE_SECURITY
2015 rte_free(cryptodev->security_ctx);
2018 return rte_cryptodev_pmd_destroy(cryptodev);
2021 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
2022 .probe = cryptodev_aesni_mb_probe,
2023 .remove = cryptodev_aesni_mb_remove
2026 static struct cryptodev_driver aesni_mb_crypto_drv;
2028 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd_drv);
2029 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
2030 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
2031 "max_nb_queue_pairs=<int> "
2033 RTE_PMD_REGISTER_CRYPTO_DRIVER(aesni_mb_crypto_drv,
2034 cryptodev_aesni_mb_pmd_drv.driver,
2035 cryptodev_driver_id);
2036 RTE_LOG_REGISTER(aesni_mb_logtype_driver, pmd.crypto.aesni_mb, NOTICE);