DPDK patches and discussions
 help / color / mirror / Atom feed
From: Hanxiao Li <li.hanxiao@zte.com.cn>
To: dev@dpdk.org
Cc: Hanxiao Li <li.hanxiao@zte.com.cn>
Subject: [PATCH v3 7/8] zsda: add zsda crypto-sym
Date: Fri, 30 Aug 2024 14:04:21 +0800	[thread overview]
Message-ID: <20240830060429.3039717-7-li.hanxiao@zte.com.cn> (raw)
In-Reply-To: <20240830060429.3039717-1-li.hanxiao@zte.com.cn>


[-- Attachment #1.1.1: Type: text/plain, Size: 10019 bytes --]

Add new file zsda_sym.c, zsda_sym.h in drivers/crypto/zsda

Signed-off-by: Hanxiao Li <li.hanxiao@zte.com.cn>
---
 drivers/crypto/zsda/zsda_sym.c | 286 +++++++++++++++++++++++++++++++++
 drivers/crypto/zsda/zsda_sym.h |  25 +++
 2 files changed, 311 insertions(+)
 create mode 100644 drivers/crypto/zsda/zsda_sym.c
 create mode 100644 drivers/crypto/zsda/zsda_sym.h

diff --git a/drivers/crypto/zsda/zsda_sym.c b/drivers/crypto/zsda/zsda_sym.c
new file mode 100644
index 0000000000..b4001476a6
--- /dev/null
+++ b/drivers/crypto/zsda/zsda_sym.c
@@ -0,0 +1,286 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 ZTE Corporation
+ */
+
+#include "cryptodev_pmd.h"
+
+#include "zsda_logs.h"
+#include "zsda_sym.h"
+#include "zsda_sym_pmd.h"
+#include "zsda_sym_session.h"
+
+#define choose_dst_mbuf(mbuf_src, mbuf_dst) ((mbuf_dst) == NULL ? (mbuf_src) : (mbuf_dst))
+#define LBADS_MAX_REMAINDER (16 - 1)
+
+void
+zsda_reverse_memcpy(uint8_t *dst, const uint8_t *src, size_t n)
+{
+	size_t i;
+
+	for (i = 0; i < n; ++i)
+		dst[n - 1 - i] = src[i];
+}
+
+static uint8_t
+zsda_get_opcode_hash(struct zsda_sym_session *sess)
+{
+	switch (sess->auth.algo) {
+	case RTE_CRYPTO_AUTH_SHA1:
+		return ZSDA_OPC_HASH_SHA1;
+
+	case RTE_CRYPTO_AUTH_SHA224:
+		return ZSDA_OPC_HASH_SHA2_224;
+
+	case RTE_CRYPTO_AUTH_SHA256:
+		return ZSDA_OPC_HASH_SHA2_256;
+
+	case RTE_CRYPTO_AUTH_SHA384:
+		return ZSDA_OPC_HASH_SHA2_384;
+
+	case RTE_CRYPTO_AUTH_SHA512:
+		return ZSDA_OPC_HASH_SHA2_512;
+
+	case RTE_CRYPTO_AUTH_SM3:
+		return ZSDA_OPC_HASH_SM3;
+	default:
+		break;
+	}
+
+	return ZSDA_OPC_INVALID;
+}
+
+static uint8_t
+zsda_get_opcode_crypto(struct zsda_sym_session *sess)
+{
+
+	if (sess->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
+		if (sess->cipher.algo == RTE_CRYPTO_CIPHER_AES_XTS &&
+		    sess->cipher.key_encry.length == 16)
+			return ZSDA_OPC_EC_AES_XTS_256;
+		else if (sess->cipher.algo == RTE_CRYPTO_CIPHER_AES_XTS &&
+			 sess->cipher.key_encry.length == 32)
+			return ZSDA_OPC_EC_AES_XTS_512;
+		else if (sess->cipher.algo == RTE_CRYPTO_CIPHER_SM4_XTS)
+			return ZSDA_OPC_EC_SM4_XTS_256;
+	} else if (sess->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
+		if (sess->cipher.algo == RTE_CRYPTO_CIPHER_AES_XTS &&
+		    sess->cipher.key_decry.length == 16)
+			return ZSDA_OPC_DC_AES_XTS_256;
+		else if (sess->cipher.algo == RTE_CRYPTO_CIPHER_AES_XTS &&
+			 sess->cipher.key_decry.length == 32)
+			return ZSDA_OPC_DC_AES_XTS_512;
+		else if (sess->cipher.algo == RTE_CRYPTO_CIPHER_SM4_XTS)
+			return ZSDA_OPC_DC_SM4_XTS_256;
+	}
+	return ZSDA_OPC_INVALID;
+}
+
+int
+zsda_encry_match(const void *op_in)
+{
+	const struct rte_crypto_op *op = (const struct rte_crypto_op *)op_in;
+	struct rte_cryptodev_sym_session *session = op->sym->session;
+	struct zsda_sym_session *sess =
+		(struct zsda_sym_session *)session->driver_priv_data;
+
+	if (sess->chain_order == ZSDA_SYM_CHAIN_ONLY_CIPHER &&
+	    sess->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
+		return 1;
+	else
+		return 0;
+}
+
+int
+zsda_decry_match(const void *op_in)
+{
+	const struct rte_crypto_op *op = (const struct rte_crypto_op *)op_in;
+	struct rte_cryptodev_sym_session *session = op->sym->session;
+	struct zsda_sym_session *sess =
+		(struct zsda_sym_session *)session->driver_priv_data;
+
+	if (sess->chain_order == ZSDA_SYM_CHAIN_ONLY_CIPHER &&
+	    sess->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT)
+		return 1;
+	else
+		return 0;
+}
+
+int
+zsda_hash_match(const void *op_in)
+{
+	const struct rte_crypto_op *op = (const struct rte_crypto_op *)op_in;
+	struct rte_cryptodev_sym_session *session = op->sym->session;
+	struct zsda_sym_session *sess =
+		(struct zsda_sym_session *)session->driver_priv_data;
+
+	if (sess->chain_order == ZSDA_SYM_CHAIN_ONLY_AUTH)
+		return 1;
+	else
+		return 0;
+}
+
+static int
+zsda_check_len_lbads(uint32_t data_len, uint32_t lbads_size)
+{
+	if (data_len < 16) {
+		ZSDA_LOG(ERR, "data_len wrong!");
+		return ZSDA_FAILED;
+	}
+	if (lbads_size != 0) {
+		if (!(((data_len % lbads_size) == 0) ||
+		      ((data_len % lbads_size) > LBADS_MAX_REMAINDER))) {
+			ZSDA_LOG(ERR, "data_len wrong!");
+			return ZSDA_FAILED;
+		}
+	}
+
+	return 0;
+}
+
+int
+zsda_build_cipher_request(void *op_in, const struct zsda_queue *queue,
+			 void **op_cookies, const uint16_t new_tail)
+{
+	struct rte_crypto_op *op = (struct rte_crypto_op *)op_in;
+
+	struct rte_cryptodev_sym_session *sesssion =
+		(struct rte_cryptodev_sym_session *)op->sym->session;
+	struct zsda_sym_session *sess =
+		(struct zsda_sym_session *)sesssion->driver_priv_data;
+
+	struct zsda_wqe_crpt *wqe =
+		(struct zsda_wqe_crpt *)(queue->base_addr +
+					 (new_tail * queue->msg_size));
+	struct zsda_op_cookie *cookie =
+		(struct zsda_op_cookie *)op_cookies[new_tail];
+	struct zsda_sgl *sgl_src = (struct zsda_sgl *)&cookie->sgl_src;
+	struct zsda_sgl *sgl_dst = (struct zsda_sgl *)&cookie->sgl_dst;
+	struct rte_mbuf *mbuf;
+
+	int ret;
+	uint32_t op_offset;
+	uint32_t op_src_len;
+	uint32_t op_dst_len;
+	const uint8_t *iv_addr = NULL;
+	uint8_t iv_len = 0;
+
+	ret = zsda_check_len_lbads(op->sym->cipher.data.length,
+				   sess->cipher.dataunit_len);
+	if (ret)
+		return ZSDA_FAILED;
+
+	op_offset = op->sym->cipher.data.offset;
+	op_src_len = op->sym->cipher.data.length;
+	mbuf = op->sym->m_src;
+	ret = zsda_fill_sgl(mbuf, op_offset, sgl_src, cookie->sgl_src_phys_addr,
+			    op_src_len, NULL);
+
+	mbuf = choose_dst_mbuf(op->sym->m_src, op->sym->m_dst);
+	op_dst_len = mbuf->pkt_len - op_offset;
+	ret |= zsda_fill_sgl(mbuf, op_offset, sgl_dst,
+			     cookie->sgl_dst_phys_addr, op_dst_len, NULL);
+
+	if (ret) {
+		ZSDA_LOG(ERR, E_FUNC);
+		return ret;
+	}
+
+	cookie->used = true;
+	cookie->sid = new_tail;
+	cookie->op = op;
+
+	memset(wqe, 0, sizeof(struct zsda_wqe_crpt));
+	wqe->rx_length = op_src_len;
+	wqe->tx_length = op_dst_len;
+	wqe->valid = queue->valid;
+	wqe->op_code = zsda_get_opcode_crypto(sess);
+	wqe->sid = cookie->sid;
+	wqe->rx_sgl_type = SGL_ELM_TYPE_LIST;
+	wqe->tx_sgl_type = SGL_ELM_TYPE_LIST;
+	wqe->rx_addr = cookie->sgl_src_phys_addr;
+	wqe->tx_addr = cookie->sgl_dst_phys_addr;
+	wqe->cfg.lbads = sess->cipher.dataunit_len;
+
+	if (sess->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
+		memcpy((uint8_t *)wqe->cfg.key, sess->cipher.key_encry.data,
+		       ZSDA_CIPHER_KEY_MAX_LEN);
+	else
+		memcpy((uint8_t *)wqe->cfg.key, sess->cipher.key_decry.data,
+		       ZSDA_CIPHER_KEY_MAX_LEN);
+
+	iv_addr = (const uint8_t *)rte_crypto_op_ctod_offset(
+			       op, char *, sess->cipher.iv.offset);
+	iv_len = sess->cipher.iv.length;
+	zsda_reverse_memcpy((uint8_t *)wqe->cfg.slba_H, iv_addr, iv_len / 2);
+	zsda_reverse_memcpy((uint8_t *)wqe->cfg.slba_L, iv_addr + 8, iv_len / 2);
+
+	return ret;
+}
+
+int
+zsda_build_hash_request(void *op_in, const struct zsda_queue *queue,
+	       void **op_cookies, const uint16_t new_tail)
+{
+	struct rte_crypto_op *op = (struct rte_crypto_op *)op_in;
+
+	struct rte_cryptodev_sym_session *sesssion =
+		(struct rte_cryptodev_sym_session *)op->sym->session;
+	struct zsda_sym_session *sess =
+		(struct zsda_sym_session *)sesssion->driver_priv_data;
+
+	struct zsda_wqe_crpt *wqe =
+		(struct zsda_wqe_crpt *)(queue->base_addr +
+					 (new_tail * queue->msg_size));
+	struct zsda_op_cookie *cookie =
+		(struct zsda_op_cookie *)op_cookies[new_tail];
+	struct zsda_sgl *sgl_src = (struct zsda_sgl *)&cookie->sgl_src;
+	uint8_t opcode;
+	uint32_t op_offset;
+	uint32_t op_src_len;
+	int ret = 0;
+
+	memset(wqe, 0, sizeof(struct zsda_wqe_crpt));
+	wqe->rx_length = op->sym->auth.data.length;
+	wqe->tx_length = sess->auth.digest_length;
+
+	opcode = zsda_get_opcode_hash(sess);
+	if (opcode == ZSDA_OPC_INVALID) {
+		ZSDA_LOG(ERR, E_FUNC);
+		return ZSDA_FAILED;
+	}
+
+	op_offset = op->sym->auth.data.offset;
+	op_src_len = op->sym->auth.data.length;
+	ret = zsda_fill_sgl(op->sym->m_src, op_offset, sgl_src,
+				   cookie->sgl_src_phys_addr, op_src_len, NULL);
+	if (ret) {
+		ZSDA_LOG(ERR, E_FUNC);
+		return ret;
+	}
+
+	cookie->used = true;
+	cookie->sid = new_tail;
+	cookie->op = op;
+	wqe->valid = queue->valid;
+	wqe->op_code = opcode;
+	wqe->sid = cookie->sid;
+	wqe->rx_sgl_type = SGL_ELM_TYPE_LIST;
+	wqe->tx_sgl_type = SGL_ELM_TYPE_PHYS_ADDR;
+	wqe->rx_addr = cookie->sgl_src_phys_addr;
+	wqe->tx_addr = op->sym->auth.digest.phys_addr;
+
+	return ret;
+}
+
+void
+zsda_crypto_callback(void *cookie_in, const struct zsda_cqe *cqe)
+{
+	struct zsda_op_cookie *tmp_cookie = (struct zsda_op_cookie *)cookie_in;
+	struct rte_crypto_op *op = (struct rte_crypto_op *)tmp_cookie->op;
+
+	if (!(CQE_ERR0(cqe->err0) || CQE_ERR1(cqe->err1)))
+		op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
+	else
+		op->status = RTE_CRYPTO_OP_STATUS_ERROR;
+
+}
diff --git a/drivers/crypto/zsda/zsda_sym.h b/drivers/crypto/zsda/zsda_sym.h
new file mode 100644
index 0000000000..c822fd38f7
--- /dev/null
+++ b/drivers/crypto/zsda/zsda_sym.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 ZTE Corporation
+ */
+
+#ifndef _ZSDA_SYM_H_
+#define _ZSDA_SYM_H_
+
+#include "zsda_common.h"
+#include "zsda_qp.h"
+
+int zsda_build_cipher_request(void *op_in, const struct zsda_queue *queue,
+			 void **op_cookies, const uint16_t new_tail);
+
+int zsda_build_hash_request(void *op_in, const struct zsda_queue *queue,
+		       void **op_cookies, const uint16_t new_tail);
+
+int zsda_encry_match(const void *op_in);
+int zsda_decry_match(const void *op_in);
+int zsda_hash_match(const void *op_in);
+
+void zsda_reverse_memcpy(uint8_t *dst, const uint8_t *src, size_t n);
+
+void zsda_crypto_callback(void *cookie_in, const struct zsda_cqe *cqe);
+
+#endif /* _ZSDA_SYM_H_ */
-- 
2.27.0

[-- Attachment #1.1.2: Type: text/html , Size: 21987 bytes --]

  parent reply	other threads:[~2024-08-30  6:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-30  6:04 [PATCH v3 1/8] zsda: Introduce zsda device drivers Hanxiao Li
2024-08-30  6:04 ` [PATCH v3 2/8] zsda: add support for zsdadev operations Hanxiao Li
2024-08-30  6:04 ` [PATCH v3 3/8] zsda: add support for queue operation Hanxiao Li
2024-08-30  6:04 ` [PATCH v3 4/8] zsda: add zsda compressdev driver and interface Hanxiao Li
2024-08-30  6:04 ` [PATCH v3 5/8] zsda: modify files for introducing zsda cryptodev Hanxiao Li
2024-08-30  6:04 ` [PATCH v3 6/8] zsda: add zsda crypto-pmd Hanxiao Li
2024-08-30  6:04 ` Hanxiao Li [this message]
2024-08-30  6:04 ` [PATCH v3 8/8] zsda: add zsda crypto-session and compile file Hanxiao Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240830060429.3039717-7-li.hanxiao@zte.com.cn \
    --to=li.hanxiao@zte.com.cn \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).