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>
15 #include "rte_aesni_mb_pmd_private.h"
17 #define AES_CCM_DIGEST_MIN_LEN 4
18 #define AES_CCM_DIGEST_MAX_LEN 16
19 #define HMAC_MAX_BLOCK_SIZE 128
20 static uint8_t cryptodev_driver_id;
22 typedef void (*hash_one_block_t)(const void *data, void *digest);
23 typedef void (*aes_keyexp_t)(const void *key, void *enc_exp_keys, void *dec_exp_keys);
26 * Calculate the authentication pre-computes
28 * @param one_block_hash Function pointer to calculate digest on ipad/opad
29 * @param ipad Inner pad output byte array
30 * @param opad Outer pad output byte array
31 * @param hkey Authentication key
32 * @param hkey_len Authentication key length
33 * @param blocksize Block size of selected hash algo
36 calculate_auth_precomputes(hash_one_block_t one_block_hash,
37 uint8_t *ipad, uint8_t *opad,
38 uint8_t *hkey, uint16_t hkey_len,
43 uint8_t ipad_buf[blocksize] __rte_aligned(16);
44 uint8_t opad_buf[blocksize] __rte_aligned(16);
46 /* Setup inner and outer pads */
47 memset(ipad_buf, HMAC_IPAD_VALUE, blocksize);
48 memset(opad_buf, HMAC_OPAD_VALUE, blocksize);
50 /* XOR hash key with inner and outer pads */
51 length = hkey_len > blocksize ? blocksize : hkey_len;
53 for (i = 0; i < length; i++) {
54 ipad_buf[i] ^= hkey[i];
55 opad_buf[i] ^= hkey[i];
58 /* Compute partial hashes */
59 (*one_block_hash)(ipad_buf, ipad);
60 (*one_block_hash)(opad_buf, opad);
63 memset(ipad_buf, 0, blocksize);
64 memset(opad_buf, 0, blocksize);
67 /** Get xform chain order */
68 static enum aesni_mb_operation
69 aesni_mb_get_chain_order(const struct rte_crypto_sym_xform *xform)
72 return AESNI_MB_OP_NOT_SUPPORTED;
74 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
75 if (xform->next == NULL)
76 return AESNI_MB_OP_CIPHER_ONLY;
77 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
78 return AESNI_MB_OP_CIPHER_HASH;
81 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
82 if (xform->next == NULL)
83 return AESNI_MB_OP_HASH_ONLY;
84 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
85 return AESNI_MB_OP_HASH_CIPHER;
88 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
89 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM ||
90 xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
91 if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
92 return AESNI_MB_OP_AEAD_CIPHER_HASH;
94 return AESNI_MB_OP_AEAD_HASH_CIPHER;
98 return AESNI_MB_OP_NOT_SUPPORTED;
101 /** Set session authentication parameters */
103 aesni_mb_set_session_auth_parameters(const MB_MGR *mb_mgr,
104 struct aesni_mb_session *sess,
105 const struct rte_crypto_sym_xform *xform)
107 hash_one_block_t hash_oneblock_fn;
108 unsigned int key_larger_block_size = 0;
109 uint8_t hashed_key[HMAC_MAX_BLOCK_SIZE] = { 0 };
110 uint32_t auth_precompute = 1;
113 sess->auth.algo = NULL_HASH;
117 if (xform->type != RTE_CRYPTO_SYM_XFORM_AUTH) {
118 AESNI_MB_LOG(ERR, "Crypto xform struct not of type auth");
122 /* Set the request digest size */
123 sess->auth.req_digest_len = xform->auth.digest_length;
125 /* Select auth generate/verify */
126 sess->auth.operation = xform->auth.op;
128 /* Set Authentication Parameters */
129 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC) {
130 sess->auth.algo = AES_XCBC;
132 uint16_t xcbc_mac_digest_len =
133 get_truncated_digest_byte_length(AES_XCBC);
134 if (sess->auth.req_digest_len != xcbc_mac_digest_len) {
135 AESNI_MB_LOG(ERR, "Invalid digest size\n");
138 sess->auth.gen_digest_len = sess->auth.req_digest_len;
140 IMB_AES_XCBC_KEYEXP(mb_mgr, xform->auth.key.data,
141 sess->auth.xcbc.k1_expanded,
142 sess->auth.xcbc.k2, sess->auth.xcbc.k3);
146 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_CMAC) {
149 sess->auth.algo = AES_CMAC;
151 uint16_t cmac_digest_len = get_digest_byte_length(AES_CMAC);
153 if (sess->auth.req_digest_len > cmac_digest_len) {
154 AESNI_MB_LOG(ERR, "Invalid digest size\n");
158 * Multi-buffer lib supports digest sizes from 4 to 16 bytes
159 * in version 0.50 and sizes of 12 and 16 bytes,
161 * If size requested is different, generate the full digest
162 * (16 bytes) in a temporary location and then memcpy
163 * the requested number of bytes.
165 if (sess->auth.req_digest_len < 4)
166 sess->auth.gen_digest_len = cmac_digest_len;
168 sess->auth.gen_digest_len = sess->auth.req_digest_len;
170 IMB_AES_KEYEXP_128(mb_mgr, xform->auth.key.data,
171 sess->auth.cmac.expkey, dust);
172 IMB_AES_CMAC_SUBKEY_GEN_128(mb_mgr, sess->auth.cmac.expkey,
173 sess->auth.cmac.skey1, sess->auth.cmac.skey2);
177 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
178 if (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) {
179 sess->cipher.direction = ENCRYPT;
180 sess->chain_order = CIPHER_HASH;
182 sess->cipher.direction = DECRYPT;
184 sess->auth.algo = AES_GMAC;
186 * Multi-buffer lib supports 8, 12 and 16 bytes of digest.
187 * If size requested is different, generate the full digest
188 * (16 bytes) in a temporary location and then memcpy
189 * the requested number of bytes.
191 if (sess->auth.req_digest_len != 16 &&
192 sess->auth.req_digest_len != 12 &&
193 sess->auth.req_digest_len != 8) {
194 sess->auth.gen_digest_len = 16;
196 sess->auth.gen_digest_len = sess->auth.req_digest_len;
198 sess->iv.length = xform->auth.iv.length;
199 sess->iv.offset = xform->auth.iv.offset;
201 switch (xform->auth.key.length) {
203 IMB_AES128_GCM_PRE(mb_mgr, xform->auth.key.data,
204 &sess->cipher.gcm_key);
205 sess->cipher.key_length_in_bytes = AES_128_BYTES;
208 IMB_AES192_GCM_PRE(mb_mgr, xform->auth.key.data,
209 &sess->cipher.gcm_key);
210 sess->cipher.key_length_in_bytes = AES_192_BYTES;
213 IMB_AES256_GCM_PRE(mb_mgr, xform->auth.key.data,
214 &sess->cipher.gcm_key);
215 sess->cipher.key_length_in_bytes = AES_256_BYTES;
218 RTE_LOG(ERR, PMD, "failed to parse test type\n");
225 switch (xform->auth.algo) {
226 case RTE_CRYPTO_AUTH_MD5_HMAC:
227 sess->auth.algo = MD5;
228 hash_oneblock_fn = mb_mgr->md5_one_block;
230 case RTE_CRYPTO_AUTH_SHA1_HMAC:
231 sess->auth.algo = SHA1;
232 hash_oneblock_fn = mb_mgr->sha1_one_block;
233 if (xform->auth.key.length > get_auth_algo_blocksize(SHA1)) {
235 xform->auth.key.data,
236 xform->auth.key.length,
238 key_larger_block_size = 1;
241 case RTE_CRYPTO_AUTH_SHA1:
242 sess->auth.algo = PLAIN_SHA1;
245 case RTE_CRYPTO_AUTH_SHA224_HMAC:
246 sess->auth.algo = SHA_224;
247 hash_oneblock_fn = mb_mgr->sha224_one_block;
248 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_224)) {
250 xform->auth.key.data,
251 xform->auth.key.length,
253 key_larger_block_size = 1;
256 case RTE_CRYPTO_AUTH_SHA224:
257 sess->auth.algo = PLAIN_SHA_224;
260 case RTE_CRYPTO_AUTH_SHA256_HMAC:
261 sess->auth.algo = SHA_256;
262 hash_oneblock_fn = mb_mgr->sha256_one_block;
263 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_256)) {
265 xform->auth.key.data,
266 xform->auth.key.length,
268 key_larger_block_size = 1;
271 case RTE_CRYPTO_AUTH_SHA256:
272 sess->auth.algo = PLAIN_SHA_256;
275 case RTE_CRYPTO_AUTH_SHA384_HMAC:
276 sess->auth.algo = SHA_384;
277 hash_oneblock_fn = mb_mgr->sha384_one_block;
278 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_384)) {
280 xform->auth.key.data,
281 xform->auth.key.length,
283 key_larger_block_size = 1;
286 case RTE_CRYPTO_AUTH_SHA384:
287 sess->auth.algo = PLAIN_SHA_384;
290 case RTE_CRYPTO_AUTH_SHA512_HMAC:
291 sess->auth.algo = SHA_512;
292 hash_oneblock_fn = mb_mgr->sha512_one_block;
293 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_512)) {
295 xform->auth.key.data,
296 xform->auth.key.length,
298 key_larger_block_size = 1;
301 case RTE_CRYPTO_AUTH_SHA512:
302 sess->auth.algo = PLAIN_SHA_512;
306 AESNI_MB_LOG(ERR, "Unsupported authentication algorithm selection");
309 uint16_t trunc_digest_size =
310 get_truncated_digest_byte_length(sess->auth.algo);
311 uint16_t full_digest_size =
312 get_digest_byte_length(sess->auth.algo);
314 if (sess->auth.req_digest_len > full_digest_size ||
315 sess->auth.req_digest_len == 0) {
316 AESNI_MB_LOG(ERR, "Invalid digest size\n");
320 if (sess->auth.req_digest_len != trunc_digest_size &&
321 sess->auth.req_digest_len != full_digest_size)
322 sess->auth.gen_digest_len = full_digest_size;
324 sess->auth.gen_digest_len = sess->auth.req_digest_len;
326 /* Plain SHA does not require precompute key */
327 if (auth_precompute == 0)
330 /* Calculate Authentication precomputes */
331 if (key_larger_block_size) {
332 calculate_auth_precomputes(hash_oneblock_fn,
333 sess->auth.pads.inner, sess->auth.pads.outer,
335 xform->auth.key.length,
336 get_auth_algo_blocksize(sess->auth.algo));
338 calculate_auth_precomputes(hash_oneblock_fn,
339 sess->auth.pads.inner, sess->auth.pads.outer,
340 xform->auth.key.data,
341 xform->auth.key.length,
342 get_auth_algo_blocksize(sess->auth.algo));
348 /** Set session cipher parameters */
350 aesni_mb_set_session_cipher_parameters(const MB_MGR *mb_mgr,
351 struct aesni_mb_session *sess,
352 const struct rte_crypto_sym_xform *xform)
358 sess->cipher.mode = NULL_CIPHER;
362 if (xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) {
363 AESNI_MB_LOG(ERR, "Crypto xform struct not of type cipher");
367 /* Select cipher direction */
368 switch (xform->cipher.op) {
369 case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
370 sess->cipher.direction = ENCRYPT;
372 case RTE_CRYPTO_CIPHER_OP_DECRYPT:
373 sess->cipher.direction = DECRYPT;
376 AESNI_MB_LOG(ERR, "Invalid cipher operation parameter");
380 /* Select cipher mode */
381 switch (xform->cipher.algo) {
382 case RTE_CRYPTO_CIPHER_AES_CBC:
383 sess->cipher.mode = CBC;
386 case RTE_CRYPTO_CIPHER_AES_CTR:
387 sess->cipher.mode = CNTR;
390 case RTE_CRYPTO_CIPHER_AES_DOCSISBPI:
391 sess->cipher.mode = DOCSIS_SEC_BPI;
394 case RTE_CRYPTO_CIPHER_DES_CBC:
395 sess->cipher.mode = DES;
397 case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
398 sess->cipher.mode = DOCSIS_DES;
400 case RTE_CRYPTO_CIPHER_3DES_CBC:
401 sess->cipher.mode = DES3;
405 AESNI_MB_LOG(ERR, "Unsupported cipher mode parameter");
409 /* Set IV parameters */
410 sess->iv.offset = xform->cipher.iv.offset;
411 sess->iv.length = xform->cipher.iv.length;
413 /* Check key length and choose key expansion function for AES */
415 switch (xform->cipher.key.length) {
417 sess->cipher.key_length_in_bytes = AES_128_BYTES;
418 IMB_AES_KEYEXP_128(mb_mgr, xform->cipher.key.data,
419 sess->cipher.expanded_aes_keys.encode,
420 sess->cipher.expanded_aes_keys.decode);
423 sess->cipher.key_length_in_bytes = AES_192_BYTES;
424 IMB_AES_KEYEXP_192(mb_mgr, xform->cipher.key.data,
425 sess->cipher.expanded_aes_keys.encode,
426 sess->cipher.expanded_aes_keys.decode);
429 sess->cipher.key_length_in_bytes = AES_256_BYTES;
430 IMB_AES_KEYEXP_256(mb_mgr, xform->cipher.key.data,
431 sess->cipher.expanded_aes_keys.encode,
432 sess->cipher.expanded_aes_keys.decode);
435 AESNI_MB_LOG(ERR, "Invalid cipher key length");
438 } else if (is_3DES) {
439 uint64_t *keys[3] = {sess->cipher.exp_3des_keys.key[0],
440 sess->cipher.exp_3des_keys.key[1],
441 sess->cipher.exp_3des_keys.key[2]};
443 switch (xform->cipher.key.length) {
445 IMB_DES_KEYSCHED(mb_mgr, keys[0],
446 xform->cipher.key.data);
447 IMB_DES_KEYSCHED(mb_mgr, keys[1],
448 xform->cipher.key.data + 8);
449 IMB_DES_KEYSCHED(mb_mgr, keys[2],
450 xform->cipher.key.data + 16);
452 /* Initialize keys - 24 bytes: [K1-K2-K3] */
453 sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
454 sess->cipher.exp_3des_keys.ks_ptr[1] = keys[1];
455 sess->cipher.exp_3des_keys.ks_ptr[2] = keys[2];
458 IMB_DES_KEYSCHED(mb_mgr, keys[0],
459 xform->cipher.key.data);
460 IMB_DES_KEYSCHED(mb_mgr, keys[1],
461 xform->cipher.key.data + 8);
462 /* Initialize keys - 16 bytes: [K1=K1,K2=K2,K3=K1] */
463 sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
464 sess->cipher.exp_3des_keys.ks_ptr[1] = keys[1];
465 sess->cipher.exp_3des_keys.ks_ptr[2] = keys[0];
468 IMB_DES_KEYSCHED(mb_mgr, keys[0],
469 xform->cipher.key.data);
471 /* Initialize keys - 8 bytes: [K1 = K2 = K3] */
472 sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
473 sess->cipher.exp_3des_keys.ks_ptr[1] = keys[0];
474 sess->cipher.exp_3des_keys.ks_ptr[2] = keys[0];
477 AESNI_MB_LOG(ERR, "Invalid cipher key length");
481 sess->cipher.key_length_in_bytes = 24;
483 if (xform->cipher.key.length != 8) {
484 AESNI_MB_LOG(ERR, "Invalid cipher key length");
487 sess->cipher.key_length_in_bytes = 8;
489 IMB_DES_KEYSCHED(mb_mgr,
490 (uint64_t *)sess->cipher.expanded_aes_keys.encode,
491 xform->cipher.key.data);
492 IMB_DES_KEYSCHED(mb_mgr,
493 (uint64_t *)sess->cipher.expanded_aes_keys.decode,
494 xform->cipher.key.data);
501 aesni_mb_set_session_aead_parameters(const MB_MGR *mb_mgr,
502 struct aesni_mb_session *sess,
503 const struct rte_crypto_sym_xform *xform)
505 switch (xform->aead.op) {
506 case RTE_CRYPTO_AEAD_OP_ENCRYPT:
507 sess->cipher.direction = ENCRYPT;
508 sess->auth.operation = RTE_CRYPTO_AUTH_OP_GENERATE;
510 case RTE_CRYPTO_AEAD_OP_DECRYPT:
511 sess->cipher.direction = DECRYPT;
512 sess->auth.operation = RTE_CRYPTO_AUTH_OP_VERIFY;
515 AESNI_MB_LOG(ERR, "Invalid aead operation parameter");
519 switch (xform->aead.algo) {
520 case RTE_CRYPTO_AEAD_AES_CCM:
521 sess->cipher.mode = CCM;
522 sess->auth.algo = AES_CCM;
524 /* Check key length and choose key expansion function for AES */
525 switch (xform->aead.key.length) {
527 sess->cipher.key_length_in_bytes = AES_128_BYTES;
528 IMB_AES_KEYEXP_128(mb_mgr, xform->aead.key.data,
529 sess->cipher.expanded_aes_keys.encode,
530 sess->cipher.expanded_aes_keys.decode);
533 AESNI_MB_LOG(ERR, "Invalid cipher key length");
539 case RTE_CRYPTO_AEAD_AES_GCM:
540 sess->cipher.mode = GCM;
541 sess->auth.algo = AES_GMAC;
543 switch (xform->aead.key.length) {
545 sess->cipher.key_length_in_bytes = AES_128_BYTES;
546 IMB_AES128_GCM_PRE(mb_mgr, xform->aead.key.data,
547 &sess->cipher.gcm_key);
550 sess->cipher.key_length_in_bytes = AES_192_BYTES;
551 IMB_AES192_GCM_PRE(mb_mgr, xform->aead.key.data,
552 &sess->cipher.gcm_key);
555 sess->cipher.key_length_in_bytes = AES_256_BYTES;
556 IMB_AES256_GCM_PRE(mb_mgr, xform->aead.key.data,
557 &sess->cipher.gcm_key);
560 AESNI_MB_LOG(ERR, "Invalid cipher key length");
567 AESNI_MB_LOG(ERR, "Unsupported aead mode parameter");
571 /* Set IV parameters */
572 sess->iv.offset = xform->aead.iv.offset;
573 sess->iv.length = xform->aead.iv.length;
575 sess->auth.req_digest_len = xform->aead.digest_length;
576 /* CCM digests must be between 4 and 16 and an even number */
577 if (sess->auth.req_digest_len < AES_CCM_DIGEST_MIN_LEN ||
578 sess->auth.req_digest_len > AES_CCM_DIGEST_MAX_LEN ||
579 (sess->auth.req_digest_len & 1) == 1) {
580 AESNI_MB_LOG(ERR, "Invalid digest size\n");
583 sess->auth.gen_digest_len = sess->auth.req_digest_len;
588 /** Parse crypto xform chain and set private session parameters */
590 aesni_mb_set_session_parameters(const MB_MGR *mb_mgr,
591 struct aesni_mb_session *sess,
592 const struct rte_crypto_sym_xform *xform)
594 const struct rte_crypto_sym_xform *auth_xform = NULL;
595 const struct rte_crypto_sym_xform *cipher_xform = NULL;
596 const struct rte_crypto_sym_xform *aead_xform = NULL;
599 /* Select Crypto operation - hash then cipher / cipher then hash */
600 switch (aesni_mb_get_chain_order(xform)) {
601 case AESNI_MB_OP_HASH_CIPHER:
602 sess->chain_order = HASH_CIPHER;
604 cipher_xform = xform->next;
606 case AESNI_MB_OP_CIPHER_HASH:
607 sess->chain_order = CIPHER_HASH;
608 auth_xform = xform->next;
609 cipher_xform = xform;
611 case AESNI_MB_OP_HASH_ONLY:
612 sess->chain_order = HASH_CIPHER;
616 case AESNI_MB_OP_CIPHER_ONLY:
618 * Multi buffer library operates only at two modes,
619 * CIPHER_HASH and HASH_CIPHER. When doing ciphering only,
620 * chain order depends on cipher operation: encryption is always
621 * the first operation and decryption the last one.
623 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
624 sess->chain_order = CIPHER_HASH;
626 sess->chain_order = HASH_CIPHER;
628 cipher_xform = xform;
630 case AESNI_MB_OP_AEAD_CIPHER_HASH:
631 sess->chain_order = CIPHER_HASH;
632 sess->aead.aad_len = xform->aead.aad_length;
635 case AESNI_MB_OP_AEAD_HASH_CIPHER:
636 sess->chain_order = HASH_CIPHER;
637 sess->aead.aad_len = xform->aead.aad_length;
640 case AESNI_MB_OP_NOT_SUPPORTED:
642 AESNI_MB_LOG(ERR, "Unsupported operation chain order parameter");
646 /* Default IV length = 0 */
649 ret = aesni_mb_set_session_auth_parameters(mb_mgr, sess, auth_xform);
651 AESNI_MB_LOG(ERR, "Invalid/unsupported authentication parameters");
655 ret = aesni_mb_set_session_cipher_parameters(mb_mgr, sess,
658 AESNI_MB_LOG(ERR, "Invalid/unsupported cipher parameters");
663 ret = aesni_mb_set_session_aead_parameters(mb_mgr, sess,
666 AESNI_MB_LOG(ERR, "Invalid/unsupported aead parameters");
675 * burst enqueue, place crypto operations on ingress queue for processing.
677 * @param __qp Queue Pair to process
678 * @param ops Crypto operations for processing
679 * @param nb_ops Number of crypto operations for processing
682 * - Number of crypto operations enqueued
685 aesni_mb_pmd_enqueue_burst(void *__qp, struct rte_crypto_op **ops,
688 struct aesni_mb_qp *qp = __qp;
690 unsigned int nb_enqueued;
692 nb_enqueued = rte_ring_enqueue_burst(qp->ingress_queue,
693 (void **)ops, nb_ops, NULL);
695 qp->stats.enqueued_count += nb_enqueued;
700 /** Get multi buffer session */
701 static inline struct aesni_mb_session *
702 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *op)
704 struct aesni_mb_session *sess = NULL;
706 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
707 if (likely(op->sym->session != NULL))
708 sess = (struct aesni_mb_session *)
709 get_sym_session_private_data(
711 cryptodev_driver_id);
714 void *_sess_private_data = NULL;
716 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
719 if (rte_mempool_get(qp->sess_mp_priv,
720 (void **)&_sess_private_data))
723 sess = (struct aesni_mb_session *)_sess_private_data;
725 if (unlikely(aesni_mb_set_session_parameters(qp->mb_mgr,
726 sess, op->sym->xform) != 0)) {
727 rte_mempool_put(qp->sess_mp, _sess);
728 rte_mempool_put(qp->sess_mp_priv, _sess_private_data);
731 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
732 set_sym_session_private_data(op->sym->session,
733 cryptodev_driver_id, _sess_private_data);
736 if (unlikely(sess == NULL))
737 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
743 * Process a crypto operation and complete a JOB_AES_HMAC job structure for
744 * submission to the multi buffer library for processing.
746 * @param qp queue pair
747 * @param job JOB_AES_HMAC structure to fill
748 * @param m mbuf to process
751 * - Completed JOB_AES_HMAC structure pointer on success
752 * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
755 set_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_qp *qp,
756 struct rte_crypto_op *op, uint8_t *digest_idx)
758 struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
759 struct aesni_mb_session *session;
760 uint16_t m_offset = 0;
762 session = get_session(qp, op);
763 if (session == NULL) {
764 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
768 /* Set crypto operation */
769 job->chain_order = session->chain_order;
771 /* Set cipher parameters */
772 job->cipher_direction = session->cipher.direction;
773 job->cipher_mode = session->cipher.mode;
775 job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
777 /* Set authentication parameters */
778 job->hash_alg = session->auth.algo;
780 switch (job->hash_alg) {
782 job->u.XCBC._k1_expanded = session->auth.xcbc.k1_expanded;
783 job->u.XCBC._k2 = session->auth.xcbc.k2;
784 job->u.XCBC._k3 = session->auth.xcbc.k3;
786 job->aes_enc_key_expanded =
787 session->cipher.expanded_aes_keys.encode;
788 job->aes_dec_key_expanded =
789 session->cipher.expanded_aes_keys.decode;
793 job->u.CCM.aad = op->sym->aead.aad.data + 18;
794 job->u.CCM.aad_len_in_bytes = session->aead.aad_len;
795 job->aes_enc_key_expanded =
796 session->cipher.expanded_aes_keys.encode;
797 job->aes_dec_key_expanded =
798 session->cipher.expanded_aes_keys.decode;
802 job->u.CMAC._key_expanded = session->auth.cmac.expkey;
803 job->u.CMAC._skey1 = session->auth.cmac.skey1;
804 job->u.CMAC._skey2 = session->auth.cmac.skey2;
805 job->aes_enc_key_expanded =
806 session->cipher.expanded_aes_keys.encode;
807 job->aes_dec_key_expanded =
808 session->cipher.expanded_aes_keys.decode;
812 if (session->cipher.mode == GCM) {
813 job->u.GCM.aad = op->sym->aead.aad.data;
814 job->u.GCM.aad_len_in_bytes = session->aead.aad_len;
817 job->u.GCM.aad = rte_pktmbuf_mtod_offset(m_src,
818 uint8_t *, op->sym->auth.data.offset);
819 job->u.GCM.aad_len_in_bytes = op->sym->auth.data.length;
820 job->cipher_mode = GCM;
822 job->aes_enc_key_expanded = &session->cipher.gcm_key;
823 job->aes_dec_key_expanded = &session->cipher.gcm_key;
827 job->u.HMAC._hashed_auth_key_xor_ipad = session->auth.pads.inner;
828 job->u.HMAC._hashed_auth_key_xor_opad = session->auth.pads.outer;
830 if (job->cipher_mode == DES3) {
831 job->aes_enc_key_expanded =
832 session->cipher.exp_3des_keys.ks_ptr;
833 job->aes_dec_key_expanded =
834 session->cipher.exp_3des_keys.ks_ptr;
836 job->aes_enc_key_expanded =
837 session->cipher.expanded_aes_keys.encode;
838 job->aes_dec_key_expanded =
839 session->cipher.expanded_aes_keys.decode;
843 /* Mutable crypto operation parameters */
844 if (op->sym->m_dst) {
845 m_src = m_dst = op->sym->m_dst;
847 /* append space for output data to mbuf */
848 char *odata = rte_pktmbuf_append(m_dst,
849 rte_pktmbuf_data_len(op->sym->m_src));
851 AESNI_MB_LOG(ERR, "failed to allocate space in destination "
852 "mbuf for source data");
853 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
857 memcpy(odata, rte_pktmbuf_mtod(op->sym->m_src, void*),
858 rte_pktmbuf_data_len(op->sym->m_src));
861 if (job->hash_alg == AES_CCM || (job->hash_alg == AES_GMAC &&
862 session->cipher.mode == GCM))
863 m_offset = op->sym->aead.data.offset;
865 m_offset = op->sym->cipher.data.offset;
868 /* Set digest output location */
869 if (job->hash_alg != NULL_HASH &&
870 session->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
871 job->auth_tag_output = qp->temp_digests[*digest_idx];
872 *digest_idx = (*digest_idx + 1) % MAX_JOBS;
874 if (job->hash_alg == AES_CCM || (job->hash_alg == AES_GMAC &&
875 session->cipher.mode == GCM))
876 job->auth_tag_output = op->sym->aead.digest.data;
878 job->auth_tag_output = op->sym->auth.digest.data;
880 if (session->auth.req_digest_len != session->auth.gen_digest_len) {
881 job->auth_tag_output = qp->temp_digests[*digest_idx];
882 *digest_idx = (*digest_idx + 1) % MAX_JOBS;
886 * Multi-buffer library current only support returning a truncated
887 * digest length as specified in the relevant IPsec RFCs
890 /* Set digest length */
891 job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
893 /* Set IV parameters */
894 job->iv_len_in_bytes = session->iv.length;
897 job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
898 job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
900 switch (job->hash_alg) {
902 job->cipher_start_src_offset_in_bytes =
903 op->sym->aead.data.offset;
904 job->msg_len_to_cipher_in_bytes = op->sym->aead.data.length;
905 job->hash_start_src_offset_in_bytes = op->sym->aead.data.offset;
906 job->msg_len_to_hash_in_bytes = op->sym->aead.data.length;
908 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
909 session->iv.offset + 1);
913 if (session->cipher.mode == GCM) {
914 job->cipher_start_src_offset_in_bytes =
915 op->sym->aead.data.offset;
916 job->hash_start_src_offset_in_bytes =
917 op->sym->aead.data.offset;
918 job->msg_len_to_cipher_in_bytes =
919 op->sym->aead.data.length;
920 job->msg_len_to_hash_in_bytes =
921 op->sym->aead.data.length;
923 job->cipher_start_src_offset_in_bytes =
924 op->sym->auth.data.offset;
925 job->hash_start_src_offset_in_bytes =
926 op->sym->auth.data.offset;
927 job->msg_len_to_cipher_in_bytes = 0;
928 job->msg_len_to_hash_in_bytes = 0;
931 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
936 job->cipher_start_src_offset_in_bytes =
937 op->sym->cipher.data.offset;
938 job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
940 job->hash_start_src_offset_in_bytes = op->sym->auth.data.offset;
941 job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
943 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
947 /* Set user data to be crypto operation data struct */
954 verify_digest(JOB_AES_HMAC *job, void *digest, uint16_t len, uint8_t *status)
956 /* Verify digest if required */
957 if (memcmp(job->auth_tag_output, digest, len) != 0)
958 *status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
962 generate_digest(JOB_AES_HMAC *job, struct rte_crypto_op *op,
963 struct aesni_mb_session *sess)
965 /* No extra copy neeed */
966 if (likely(sess->auth.req_digest_len == sess->auth.gen_digest_len))
970 * This can only happen for HMAC, so only digest
971 * for authentication algos is required
973 memcpy(op->sym->auth.digest.data, job->auth_tag_output,
974 sess->auth.req_digest_len);
978 * Process a completed job and return rte_mbuf which job processed
980 * @param qp Queue Pair to process
981 * @param job JOB_AES_HMAC job to process
984 * - Returns processed crypto operation.
985 * - Returns NULL on invalid job
987 static inline struct rte_crypto_op *
988 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
990 struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
991 struct aesni_mb_session *sess = get_sym_session_private_data(
993 cryptodev_driver_id);
995 if (likely(op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)) {
996 switch (job->status) {
998 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1000 if (job->hash_alg == NULL_HASH)
1003 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1004 if (job->hash_alg == AES_CCM ||
1005 (job->hash_alg == AES_GMAC &&
1006 sess->cipher.mode == GCM))
1008 op->sym->aead.digest.data,
1009 sess->auth.req_digest_len,
1013 op->sym->auth.digest.data,
1014 sess->auth.req_digest_len,
1017 generate_digest(job, op, sess);
1020 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1024 /* Free session if a session-less crypto op */
1025 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1026 memset(sess, 0, sizeof(struct aesni_mb_session));
1027 memset(op->sym->session, 0,
1028 rte_cryptodev_sym_get_existing_header_session_size(
1030 rte_mempool_put(qp->sess_mp_priv, sess);
1031 rte_mempool_put(qp->sess_mp, op->sym->session);
1032 op->sym->session = NULL;
1039 * Process a completed JOB_AES_HMAC job and keep processing jobs until
1040 * get_completed_job return NULL
1042 * @param qp Queue Pair to process
1043 * @param job JOB_AES_HMAC job
1046 * - Number of processed jobs
1049 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job,
1050 struct rte_crypto_op **ops, uint16_t nb_ops)
1052 struct rte_crypto_op *op = NULL;
1053 unsigned processed_jobs = 0;
1055 while (job != NULL) {
1056 op = post_process_mb_job(qp, job);
1059 ops[processed_jobs++] = op;
1060 qp->stats.dequeued_count++;
1062 qp->stats.dequeue_err_count++;
1065 if (processed_jobs == nb_ops)
1068 job = IMB_GET_COMPLETED_JOB(qp->mb_mgr);
1071 return processed_jobs;
1074 static inline uint16_t
1075 flush_mb_mgr(struct aesni_mb_qp *qp, struct rte_crypto_op **ops,
1078 int processed_ops = 0;
1080 /* Flush the remaining jobs */
1081 JOB_AES_HMAC *job = IMB_FLUSH_JOB(qp->mb_mgr);
1084 processed_ops += handle_completed_jobs(qp, job,
1085 &ops[processed_ops], nb_ops - processed_ops);
1087 return processed_ops;
1090 static inline JOB_AES_HMAC *
1091 set_job_null_op(JOB_AES_HMAC *job, struct rte_crypto_op *op)
1093 job->chain_order = HASH_CIPHER;
1094 job->cipher_mode = NULL_CIPHER;
1095 job->hash_alg = NULL_HASH;
1096 job->cipher_direction = DECRYPT;
1098 /* Set user data to be crypto operation data struct */
1099 job->user_data = op;
1105 aesni_mb_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1108 struct aesni_mb_qp *qp = queue_pair;
1110 struct rte_crypto_op *op;
1113 int retval, processed_jobs = 0;
1115 if (unlikely(nb_ops == 0))
1118 uint8_t digest_idx = qp->digest_idx;
1120 /* Get next free mb job struct from mb manager */
1121 job = IMB_GET_NEXT_JOB(qp->mb_mgr);
1122 if (unlikely(job == NULL)) {
1123 /* if no free mb job structs we need to flush mb_mgr */
1124 processed_jobs += flush_mb_mgr(qp,
1125 &ops[processed_jobs],
1126 nb_ops - processed_jobs);
1128 if (nb_ops == processed_jobs)
1131 job = IMB_GET_NEXT_JOB(qp->mb_mgr);
1135 * Get next operation to process from ingress queue.
1136 * There is no need to return the job to the MB_MGR
1137 * if there are no more operations to process, since the MB_MGR
1138 * can use that pointer again in next get_next calls.
1140 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
1144 retval = set_mb_job_params(job, qp, op, &digest_idx);
1145 if (unlikely(retval != 0)) {
1146 qp->stats.dequeue_err_count++;
1147 set_job_null_op(job, op);
1150 /* Submit job to multi-buffer for processing */
1151 #ifdef RTE_LIBRTE_PMD_AESNI_MB_DEBUG
1152 job = IMB_SUBMIT_JOB(qp->mb_mgr);
1154 job = IMB_SUBMIT_JOB_NOCHECK(qp->mb_mgr);
1157 * If submit returns a processed job then handle it,
1158 * before submitting subsequent jobs
1161 processed_jobs += handle_completed_jobs(qp, job,
1162 &ops[processed_jobs],
1163 nb_ops - processed_jobs);
1165 } while (processed_jobs < nb_ops);
1167 qp->digest_idx = digest_idx;
1169 if (processed_jobs < 1)
1170 processed_jobs += flush_mb_mgr(qp,
1171 &ops[processed_jobs],
1172 nb_ops - processed_jobs);
1174 return processed_jobs;
1177 static int cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev);
1180 cryptodev_aesni_mb_create(const char *name,
1181 struct rte_vdev_device *vdev,
1182 struct rte_cryptodev_pmd_init_params *init_params)
1184 struct rte_cryptodev *dev;
1185 struct aesni_mb_private *internals;
1186 enum aesni_mb_vector_mode vector_mode;
1189 /* Check CPU for support for AES instruction set */
1190 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES)) {
1191 AESNI_MB_LOG(ERR, "AES instructions not supported by CPU");
1195 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
1197 AESNI_MB_LOG(ERR, "failed to create cryptodev vdev");
1201 /* Check CPU for supported vector instruction set */
1202 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F))
1203 vector_mode = RTE_AESNI_MB_AVX512;
1204 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
1205 vector_mode = RTE_AESNI_MB_AVX2;
1206 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
1207 vector_mode = RTE_AESNI_MB_AVX;
1209 vector_mode = RTE_AESNI_MB_SSE;
1211 dev->driver_id = cryptodev_driver_id;
1212 dev->dev_ops = rte_aesni_mb_pmd_ops;
1214 /* register rx/tx burst functions for data path */
1215 dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
1216 dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
1218 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1219 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1220 RTE_CRYPTODEV_FF_CPU_AESNI;
1222 mb_mgr = alloc_mb_mgr(0);
1226 switch (vector_mode) {
1227 case RTE_AESNI_MB_SSE:
1228 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
1229 init_mb_mgr_sse(mb_mgr);
1231 case RTE_AESNI_MB_AVX:
1232 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
1233 init_mb_mgr_avx(mb_mgr);
1235 case RTE_AESNI_MB_AVX2:
1236 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX2;
1237 init_mb_mgr_avx2(mb_mgr);
1239 case RTE_AESNI_MB_AVX512:
1240 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX512;
1241 init_mb_mgr_avx512(mb_mgr);
1244 AESNI_MB_LOG(ERR, "Unsupported vector mode %u\n", vector_mode);
1248 /* Set vector instructions mode supported */
1249 internals = dev->data->dev_private;
1251 internals->vector_mode = vector_mode;
1252 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
1253 internals->mb_mgr = mb_mgr;
1255 AESNI_MB_LOG(INFO, "IPSec Multi-buffer library version used: %s\n",
1256 imb_get_version_str());
1262 free_mb_mgr(mb_mgr);
1264 rte_cryptodev_pmd_destroy(dev);
1270 cryptodev_aesni_mb_probe(struct rte_vdev_device *vdev)
1272 struct rte_cryptodev_pmd_init_params init_params = {
1274 sizeof(struct aesni_mb_private),
1276 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
1278 const char *name, *args;
1281 name = rte_vdev_device_name(vdev);
1285 args = rte_vdev_device_args(vdev);
1287 retval = rte_cryptodev_pmd_parse_input_args(&init_params, args);
1289 AESNI_MB_LOG(ERR, "Failed to parse initialisation arguments[%s]",
1294 return cryptodev_aesni_mb_create(name, vdev, &init_params);
1298 cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev)
1300 struct rte_cryptodev *cryptodev;
1301 struct aesni_mb_private *internals;
1304 name = rte_vdev_device_name(vdev);
1308 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
1309 if (cryptodev == NULL)
1312 internals = cryptodev->data->dev_private;
1314 free_mb_mgr(internals->mb_mgr);
1316 return rte_cryptodev_pmd_destroy(cryptodev);
1319 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
1320 .probe = cryptodev_aesni_mb_probe,
1321 .remove = cryptodev_aesni_mb_remove
1324 static struct cryptodev_driver aesni_mb_crypto_drv;
1326 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd_drv);
1327 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
1328 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
1329 "max_nb_queue_pairs=<int> "
1331 RTE_PMD_REGISTER_CRYPTO_DRIVER(aesni_mb_crypto_drv,
1332 cryptodev_aesni_mb_pmd_drv.driver,
1333 cryptodev_driver_id);
1335 RTE_INIT(aesni_mb_init_log)
1337 aesni_mb_logtype_driver = rte_log_register("pmd.crypto.aesni_mb");