Skip to contentSkip to conversation

test/cryptodev: add ECDH tests

PATCH Superseded Gowrishankar Muthukrishnan 2 participants 9 messages Oct 26, 2023 – Nov 9, 2023 Crypto API

lore
Share & tools
lore
#0 lore raw

cover test/cryptodev: add ECDH tests

This patch series adds ECDH testsuite. It also enables ECDH support in CNXK PMD.

Gowrishankar Muthukrishnan (4):
test/cryptodev: add ECDH tests
crypto/cnxk: use generic EC opcodes
crypto/cnxk: change order of ECFPM params
crypto/cnxk: add ECDH support

 app/test/test_cryptodev_asym.c                | 729 ++++++++++++++++++
 app/test/test_cryptodev_asym_util.h           |  12 +
 app/test/test_cryptodev_ecdh_test_vectors.h   | 577 ++++++++++++++
 app/test/test_cryptodev_ecdsa_test_vectors.h  |   3 +
 doc/guides/cryptodevs/features/cn10k.ini      |   1 +
 doc/guides/cryptodevs/features/cn9k.ini       |   1 +
 drivers/common/cnxk/roc_ae.h                  |  21 +-
 drivers/crypto/cnxk/cn10k_cryptodev_ops.c     |  12 +
 drivers/crypto/cnxk/cnxk_ae.h                 | 240 ++++--
 drivers/crypto/cnxk/cnxk_cryptodev.h          |   2 +-
 .../crypto/cnxk/cnxk_cryptodev_capabilities.c |  14 +
 11 files changed, 1543 insertions(+), 69 deletions(-)
 create mode 100644 app/test/test_cryptodev_ecdh_test_vectors.h
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/4 test/cryptodev: add ECDH tests

Add ECDH tests.

---
 app/test/test_cryptodev_asym.c               | 729 +++++++++++++++++++
 app/test/test_cryptodev_asym_util.h          |  12 +
 app/test/test_cryptodev_ecdh_test_vectors.h  | 577 +++++++++++++++
 app/test/test_cryptodev_ecdsa_test_vectors.h |   3 +
 4 files changed, 1321 insertions(+)
 create mode 100644 app/test/test_cryptodev_ecdh_test_vectors.h
app/test/test_cryptodev_asym.c +729 -0
index c6334380d7..965e71d0bc 100644
@@ -17,6 +17,7 @@
 #include "test_cryptodev.h"
 #include "test_cryptodev_dh_test_vectors.h"
 #include "test_cryptodev_dsa_test_vectors.h"
+#include "test_cryptodev_ecdh_test_vectors.h"
 #include "test_cryptodev_ecdsa_test_vectors.h"
 #include "test_cryptodev_ecpm_test_vectors.h"
 #include "test_cryptodev_mod_test_vectors.h"
@@ -1806,6 +1807,732 @@ test_ecpm_all_curve(void)
 	return overall_status;
 }
 
+static int
+test_ecdh_priv_key_generate(enum curve curve_id)
+{
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	const struct rte_cryptodev_asymmetric_xform_capability *capa;
+	struct rte_mempool *sess_mpool = ts_params->session_mpool;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	struct rte_cryptodev_asym_capability_idx idx;
+	uint8_t dev_id = ts_params->valid_devs[0];
+	struct rte_crypto_asym_xform xform = {0};
+	struct rte_crypto_op *result_op = NULL;
+	uint8_t output_buf[TEST_DATA_SIZE];
+	struct rte_crypto_asym_op *asym_op;
+	struct rte_crypto_op *op = NULL;
+	int ret, status = TEST_SUCCESS;
+	uint16_t output_buflen = 0;
+	void *sess = NULL;
+	int curve;
+
+	/* Check ECDH capability */
+	idx.type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	capa = rte_cryptodev_asym_capability_get(dev_id, &idx);
+	if (capa == NULL)
+		return -ENOTSUP;
+
+	if (!(capa->op_types & (1 <<  RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE)))
+		return -ENOTSUP;
+
+	switch (curve_id) {
+	case SECP192R1:
+		curve = RTE_CRYPTO_EC_GROUP_SECP192R1;
+		output_buflen = 24;
+		break;
+	case SECP224R1:
+		curve = RTE_CRYPTO_EC_GROUP_SECP224R1;
+		output_buflen = 28;
+		break;
+	case SECP256R1:
+		curve = RTE_CRYPTO_EC_GROUP_SECP256R1;
+		output_buflen = 32;
+		break;
+	case SECP384R1:
+		curve = RTE_CRYPTO_EC_GROUP_SECP384R1;
+		output_buflen = 48;
+		break;
+	case SECP521R1:
+		curve = RTE_CRYPTO_EC_GROUP_SECP521R1;
+		output_buflen = 66;
+		break;
+	default:
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Unsupported curve id\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	xform.ec.curve_id = curve;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Populate op with operational details */
+	asym_op->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE;
+
+	/* Init out buf */
+	asym_op->ecdh.priv_key.data = output_buf;
+	asym_op->ecdh.priv_key.length = output_buflen;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	asym_op = result_op->asym;
+
+	debug_hexdump(stdout, "priv_key:",
+		asym_op->ecdh.priv_key.data, asym_op->ecdh.priv_key.length);
+
+exit:
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+	return status;
+}
+
+static int
+test_ecdh_pub_key_generate(enum curve curve_id)
+{
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	const struct rte_cryptodev_asymmetric_xform_capability *capa;
+	struct rte_mempool *sess_mpool = ts_params->session_mpool;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	struct crypto_testsuite_ecdh_params input_params;
+	struct rte_cryptodev_asym_capability_idx idx;
+	uint8_t dev_id = ts_params->valid_devs[0];
+	struct rte_crypto_asym_xform xform = {0};
+	struct rte_crypto_op *result_op = NULL;
+	uint8_t output_buf_x[TEST_DATA_SIZE];
+	uint8_t output_buf_y[TEST_DATA_SIZE];
+	struct rte_crypto_asym_op *asym_op;
+	struct rte_crypto_op *op = NULL;
+	int ret, status = TEST_SUCCESS;
+	void *sess = NULL;
+
+	/* Check ECDH capability */
+	idx.type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	capa = rte_cryptodev_asym_capability_get(dev_id, &idx);
+	if (capa == NULL)
+		return -ENOTSUP;
+
+	if (!(capa->op_types & (1 <<  RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE)))
+		return -ENOTSUP;
+
+	switch (curve_id) {
+	case SECP192R1:
+		input_params = ecdh_param_secp192r1;
+		break;
+	case SECP224R1:
+		input_params = ecdh_param_secp224r1;
+		break;
+	case SECP256R1:
+		input_params = ecdh_param_secp256r1;
+		break;
+	case SECP384R1:
+		input_params = ecdh_param_secp384r1;
+		break;
+	case SECP521R1:
+		input_params = ecdh_param_secp521r1;
+		break;
+	default:
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Unsupported curve id\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	debug_hexdump(stdout, "pkey:",
+		input_params.pkey_A.data, input_params.pkey_A.length);
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	xform.ec.curve_id = input_params.curve;
+	xform.ec.pkey.data = input_params.pkey_A.data;
+	xform.ec.pkey.length = input_params.pkey_A.length;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Populate op with operational details */
+	asym_op->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE;
+
+	/* Init out buf */
+	asym_op->ecdh.pub_key.x.data = output_buf_x;
+	asym_op->ecdh.pub_key.y.data = output_buf_y;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	asym_op = result_op->asym;
+
+	debug_hexdump(stdout, "qx:",
+		asym_op->ecdh.pub_key.x.data, asym_op->ecdh.pub_key.x.length);
+	debug_hexdump(stdout, "qy:",
+		asym_op->ecdh.pub_key.y.data, asym_op->ecdh.pub_key.y.length);
+
+	ret = verify_ecdh_secret(input_params.pubkey_qA_x.data,
+				input_params.pubkey_qA_y.data, result_op);
+	if (ret) {
+		status = TEST_FAILED;
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"ECDH public key generation failed.\n");
+		goto exit;
+	}
+
+exit:
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+	return status;
+}
+
+static int
+test_ecdh_pub_key_verify(enum curve curve_id)
+{
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	const struct rte_cryptodev_asymmetric_xform_capability *capa;
+	struct rte_mempool *sess_mpool = ts_params->session_mpool;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	struct crypto_testsuite_ecdh_params input_params;
+	struct rte_cryptodev_asym_capability_idx idx;
+	uint8_t dev_id = ts_params->valid_devs[0];
+	struct rte_crypto_asym_xform xform = {0};
+	struct rte_crypto_op *result_op = NULL;
+	struct rte_crypto_asym_op *asym_op;
+	struct rte_crypto_op *op = NULL;
+	int ret, status = TEST_SUCCESS;
+	void *sess = NULL;
+
+	/* Check ECDH capability */
+	idx.type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	capa = rte_cryptodev_asym_capability_get(dev_id, &idx);
+	if (capa == NULL)
+		return -ENOTSUP;
+
+	if (!(capa->op_types & (1 <<  RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY)))
+		return -ENOTSUP;
+
+	switch (curve_id) {
+	case SECP192R1:
+		input_params = ecdh_param_secp192r1;
+		break;
+	case SECP224R1:
+		input_params = ecdh_param_secp224r1;
+		break;
+	case SECP256R1:
+		input_params = ecdh_param_secp256r1;
+		break;
+	case SECP384R1:
+		input_params = ecdh_param_secp384r1;
+		break;
+	case SECP521R1:
+		input_params = ecdh_param_secp521r1;
+		break;
+	default:
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Unsupported curve id\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	debug_hexdump(stdout, "qx:",
+		input_params.pubkey_qA_x.data, input_params.pubkey_qA_x.length);
+	debug_hexdump(stdout, "qy:",
+		input_params.pubkey_qA_y.data, input_params.pubkey_qA_y.length);
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	xform.ec.curve_id = input_params.curve;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Populate op with operational details */
+	asym_op->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY;
+	asym_op->ecdh.pub_key.x.data = input_params.pubkey_qA_x.data;
+	asym_op->ecdh.pub_key.x.length = input_params.pubkey_qA_x.length;
+	asym_op->ecdh.pub_key.y.data = input_params.pubkey_qA_y.data;
+	asym_op->ecdh.pub_key.y.length = input_params.pubkey_qA_y.length;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+exit:
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+	return status;
+}
+
+static int
+test_ecdh_shared_secret(enum curve curve_id)
+{
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	const struct rte_cryptodev_asymmetric_xform_capability *capa;
+	struct rte_mempool *sess_mpool = ts_params->session_mpool;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	struct crypto_testsuite_ecdh_params input_params;
+	struct rte_cryptodev_asym_capability_idx idx;
+	uint8_t dev_id = ts_params->valid_devs[0];
+	struct rte_crypto_asym_xform xform = {0};
+	struct rte_crypto_op *result_op = NULL;
+	uint8_t output_buf_x[TEST_DATA_SIZE];
+	uint8_t output_buf_y[TEST_DATA_SIZE];
+	struct rte_crypto_asym_op *asym_op;
+	struct rte_crypto_op *op = NULL;
+	int ret, status = TEST_SUCCESS;
+	void *sess = NULL;
+
+	/* Check ECDH capability */
+	idx.type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	capa = rte_cryptodev_asym_capability_get(dev_id, &idx);
+	if (capa == NULL)
+		return -ENOTSUP;
+
+	if (!(capa->op_types & (1 <<  RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE)))
+		return -ENOTSUP;
+
+	switch (curve_id) {
+	case SECP192R1:
+		input_params = ecdh_param_secp192r1;
+		break;
+	case SECP224R1:
+		input_params = ecdh_param_secp224r1;
+		break;
+	case SECP256R1:
+		input_params = ecdh_param_secp256r1;
+		break;
+	case SECP384R1:
+		input_params = ecdh_param_secp384r1;
+		break;
+	case SECP521R1:
+		input_params = ecdh_param_secp521r1;
+		break;
+	default:
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Unsupported curve id\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	/* zA = dA.QB */
+	debug_hexdump(stdout, "pkey:",
+		input_params.pkey_A.data, input_params.pkey_A.length);
+	debug_hexdump(stdout, "qx:",
+		input_params.pubkey_qB_x.data, input_params.pubkey_qB_x.length);
+	debug_hexdump(stdout, "qy:",
+		input_params.pubkey_qB_y.data, input_params.pubkey_qB_y.length);
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	xform.ec.curve_id = input_params.curve;
+	xform.ec.pkey.data = input_params.pkey_A.data;
+	xform.ec.pkey.length = input_params.pkey_A.length;
+	xform.ec.q.x.data = input_params.pubkey_qB_x.data;
+	xform.ec.q.x.length = input_params.pubkey_qB_x.length;
+	xform.ec.q.y.data = input_params.pubkey_qB_y.data;
+	xform.ec.q.y.length = input_params.pubkey_qB_y.length;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Populate op with operational details */
+	asym_op->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
+
+	/* Init out buf */
+	asym_op->ecdh.shared_secret.x.data = output_buf_x;
+	asym_op->ecdh.shared_secret.y.data = output_buf_y;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	asym_op = result_op->asym;
+
+	debug_hexdump(stdout, "secret_x:",
+		asym_op->ecdh.shared_secret.x.data, asym_op->ecdh.shared_secret.x.length);
+	debug_hexdump(stdout, "secret_y:",
+		asym_op->ecdh.shared_secret.y.data, asym_op->ecdh.shared_secret.y.length);
+
+	ret = verify_ecdh_secret(input_params.secret_x.data,
+				input_params.secret_y.data, result_op);
+	if (ret) {
+		status = TEST_FAILED;
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"ECDH shared secret compute failed.\n");
+		goto exit;
+	}
+
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+
+	/* zB = dB.QA */
+	debug_hexdump(stdout, "pkey:",
+		input_params.pkey_B.data, input_params.pkey_B.length);
+	debug_hexdump(stdout, "qx:",
+		input_params.pubkey_qA_x.data, input_params.pubkey_qA_x.length);
+	debug_hexdump(stdout, "qy:",
+		input_params.pubkey_qA_y.data, input_params.pubkey_qA_y.length);
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	xform.ec.curve_id = input_params.curve;
+	xform.ec.pkey.data = input_params.pkey_B.data;
+	xform.ec.pkey.length = input_params.pkey_B.length;
+	xform.ec.q.x.data = input_params.pubkey_qA_x.data;
+	xform.ec.q.x.length = input_params.pubkey_qA_x.length;
+	xform.ec.q.y.data = input_params.pubkey_qA_y.data;
+	xform.ec.q.y.length = input_params.pubkey_qA_y.length;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Populate op with operational details */
+	asym_op->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
+
+	/* Init out buf */
+	asym_op->ecdh.shared_secret.x.data = output_buf_x;
+	asym_op->ecdh.shared_secret.y.data = output_buf_y;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	asym_op = result_op->asym;
+
+	debug_hexdump(stdout, "secret_x:",
+			asym_op->ecdh.shared_secret.x.data, asym_op->ecdh.shared_secret.x.length);
+	debug_hexdump(stdout, "secret_y:",
+			asym_op->ecdh.shared_secret.y.data, asym_op->ecdh.shared_secret.y.length);
+
+	ret = verify_ecdh_secret(input_params.secret_x.data,
+				input_params.secret_y.data, result_op);
+	if (ret) {
+		status = TEST_FAILED;
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"ECDH shared secret compute failed.\n");
+		goto exit;
+	}
+
+exit:
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+	return status;
+}
+
+static int
+test_ecdh_all_curve(void)
+{
+	int status, overall_status = TEST_SUCCESS;
+	enum curve curve_id;
+	int test_index = 0;
+	const char *msg;
+
+	for (curve_id = SECP192R1; curve_id < END_OF_CURVE_LIST; curve_id++) {
+		status = test_ecdh_priv_key_generate(curve_id);
+		if (status == TEST_SUCCESS) {
+			msg = "succeeded";
+		} else {
+			msg = "failed";
+			overall_status = status;
+		}
+		printf("  %u) TestCase ECDH private key generation for Curve %s %s\n",
+		       test_index ++, curve[curve_id], msg);
+	}
+
+	for (curve_id = SECP192R1; curve_id < END_OF_CURVE_LIST; curve_id++) {
+		status = test_ecdh_pub_key_generate(curve_id);
+		if (status == TEST_SUCCESS) {
+			msg = "succeeded";
+		} else {
+			msg = "failed";
+			overall_status = status;
+		}
+		printf("  %u) TestCase ECDH public key generation for Curve %s %s\n",
+		       test_index ++, curve[curve_id], msg);
+	}
+
+	for (curve_id = SECP192R1; curve_id < END_OF_CURVE_LIST; curve_id++) {
+		status = test_ecdh_pub_key_verify(curve_id);
+		if (status == TEST_SUCCESS) {
+			msg = "succeeded";
+		} else {
+			msg = "failed";
+			overall_status = status;
+		}
+		printf("  %u) TestCase ECDH public key verification for Curve %s %s\n",
+		       test_index ++, curve[curve_id], msg);
+	}
+
+	for (curve_id = SECP192R1; curve_id < END_OF_CURVE_LIST; curve_id++) {
+		status = test_ecdh_shared_secret(curve_id);
+		if (status == TEST_SUCCESS) {
+			msg = "succeeded";
+		} else {
+			msg = "failed";
+			overall_status = status;
+		}
+		printf("  %u) TestCase ECDH shared secret compute for Curve %s %s\n",
+		       test_index ++, curve[curve_id], msg);
+	}
+
+	return overall_status;
+}
+
 static int
 test_sm2_sign(void)
 {
@@ -2848,6 +3575,8 @@ static struct unit_test_suite cryptodev_octeontx_asym_testsuite  = {
 			     test_ecdsa_sign_verify_all_curve),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_sm2_sign),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_sm2_verify),
+		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
+				test_ecdh_all_curve),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
 				test_ecpm_all_curve),
 		TEST_CASES_END() /**< NULL terminate unit test array */
app/test/test_cryptodev_asym_util.h +12 -0
index 83dc265dd7..07e6e831e8 100644
@@ -57,4 +57,16 @@ static inline int verify_ecpm(uint8_t *result_x, uint8_t *result_y,
 
 	return 0;
 }
+
+static inline int verify_ecdh_secret(uint8_t *result_x, uint8_t *result_y,
+			      struct rte_crypto_op *result_op)
+{
+	if (memcmp(result_x, result_op->asym->ecdh.shared_secret.x.data,
+		   result_op->asym->ecdh.shared_secret.x.length) ||
+		   memcmp(result_y, result_op->asym->ecdh.shared_secret.y.data,
+		   result_op->asym->ecdh.shared_secret.y.length))
+		return -1;
+
+	return 0;
+}
 #endif /* TEST_CRYPTODEV_ASYM_TEST_UTIL_H__ */
app/test/test_cryptodev_ecdh_test_vectors.h new file +577 -0

577 lines not on this page · raw message

app/test/test_cryptodev_ecdsa_test_vectors.h +3 -0
index 55fbda5979..9ac3189ce7 100644
@@ -7,6 +7,8 @@
 
 #include "rte_crypto_asym.h"
 
+#ifndef __TEST_CRYPTODEV_EC_CURVES__
+#define __TEST_CRYPTODEV_EC_CURVES__
 /* EC curve id */
 enum curve {
 	SECP192R1,
@@ -22,6 +24,7 @@ const char *curve[] = {"SECP192R1",
 		       "SECP256R1",
 		       "SECP384R1",
 		       "SECP521R1"};
+#endif
 
 struct crypto_testsuite_ecdsa_params {
 	rte_crypto_param pubkey_qx;
Signature · 1 line
2.25.1
#2 lore raw
Quoted 6 lines

Subject: [PATCH v1 1/4] test/cryptodev: add ECDH tests

Add ECDH tests.

---

Please rebase this patch.

Applied the PMD patches of this series on dpdk-next-crypto. Send the test app patch again after rebasing on next-crypto.

Thanks.

#3 lore raw

PATCH v2 1/1 test/cryptodev: add ECDH tests

In reply to Gowrishankar Muthukrishnan

Add ECDH tests.

Change-Id I88099e2ba8e058fbb519a06d09ebb3ece7c7e27f

---
v2:
- rebased due to patch conflict.

---
 app/test/test_cryptodev_asym.c              | 741 ++++++++++++++++++++
 app/test/test_cryptodev_asym_util.h         |  12 +
 app/test/test_cryptodev_ecdh_test_vectors.h | 556 +++++++++++++++
 3 files changed, 1309 insertions(+)
 create mode 100644 app/test/test_cryptodev_ecdh_test_vectors.h
app/test/test_cryptodev_asym.c +741 -0
index c4f985dba0..17daf734e8 100644
@@ -17,6 +17,7 @@
 #include "test_cryptodev.h"
 #include "test_cryptodev_dh_test_vectors.h"
 #include "test_cryptodev_dsa_test_vectors.h"
+#include "test_cryptodev_ecdh_test_vectors.h"
 #include "test_cryptodev_ecdsa_test_vectors.h"
 #include "test_cryptodev_ecpm_test_vectors.h"
 #include "test_cryptodev_mod_test_vectors.h"
@@ -1811,6 +1812,744 @@ test_ecpm_all_curve(void)
 	return overall_status;
 }
 
+static int
+test_ecdh_priv_key_generate(enum curve curve_id)
+{
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	const struct rte_cryptodev_asymmetric_xform_capability *capa;
+	struct rte_mempool *sess_mpool = ts_params->session_mpool;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	struct rte_cryptodev_asym_capability_idx idx;
+	uint8_t dev_id = ts_params->valid_devs[0];
+	struct rte_crypto_asym_xform xform = {0};
+	struct rte_crypto_op *result_op = NULL;
+	uint8_t output_buf[TEST_DATA_SIZE];
+	struct rte_crypto_asym_op *asym_op;
+	struct rte_crypto_op *op = NULL;
+	int ret, status = TEST_SUCCESS;
+	uint16_t output_buflen = 0;
+	void *sess = NULL;
+	int curve;
+
+	/* Check ECDH capability */
+	idx.type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	capa = rte_cryptodev_asym_capability_get(dev_id, &idx);
+	if (capa == NULL)
+		return -ENOTSUP;
+
+	if (!(capa->op_types & (1 <<  RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE)))
+		return -ENOTSUP;
+
+	switch (curve_id) {
+	case SECP192R1:
+		curve = RTE_CRYPTO_EC_GROUP_SECP192R1;
+		output_buflen = 24;
+		break;
+	case SECP224R1:
+		curve = RTE_CRYPTO_EC_GROUP_SECP224R1;
+		output_buflen = 28;
+		break;
+	case SECP256R1:
+		curve = RTE_CRYPTO_EC_GROUP_SECP256R1;
+		output_buflen = 32;
+		break;
+	case SECP384R1:
+		curve = RTE_CRYPTO_EC_GROUP_SECP384R1;
+		output_buflen = 48;
+		break;
+	case SECP521R1:
+		curve = RTE_CRYPTO_EC_GROUP_SECP521R1;
+		output_buflen = 66;
+		break;
+	default:
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Unsupported curve id\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	xform.ec.curve_id = curve;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Populate op with operational details */
+	asym_op->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE;
+
+	/* Init out buf */
+	asym_op->ecdh.priv_key.data = output_buf;
+	asym_op->ecdh.priv_key.length = output_buflen;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	asym_op = result_op->asym;
+
+	debug_hexdump(stdout, "priv_key:",
+		asym_op->ecdh.priv_key.data, asym_op->ecdh.priv_key.length);
+
+exit:
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+	return status;
+}
+
+static int
+test_ecdh_pub_key_generate(enum curve curve_id)
+{
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	const struct rte_cryptodev_asymmetric_xform_capability *capa;
+	struct rte_mempool *sess_mpool = ts_params->session_mpool;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	struct crypto_testsuite_ecdh_params input_params;
+	struct rte_cryptodev_asym_capability_idx idx;
+	uint8_t dev_id = ts_params->valid_devs[0];
+	struct rte_crypto_asym_xform xform = {0};
+	struct rte_crypto_op *result_op = NULL;
+	uint8_t output_buf_x[TEST_DATA_SIZE];
+	uint8_t output_buf_y[TEST_DATA_SIZE];
+	struct rte_crypto_asym_op *asym_op;
+	struct rte_crypto_op *op = NULL;
+	int ret, status = TEST_SUCCESS;
+	void *sess = NULL;
+
+	/* Check ECDH capability */
+	idx.type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	capa = rte_cryptodev_asym_capability_get(dev_id, &idx);
+	if (capa == NULL)
+		return -ENOTSUP;
+
+	if (!(capa->op_types & (1 <<  RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE)))
+		return -ENOTSUP;
+
+	switch (curve_id) {
+	case SECP192R1:
+		input_params = ecdh_param_secp192r1;
+		break;
+	case SECP224R1:
+		input_params = ecdh_param_secp224r1;
+		break;
+	case SECP256R1:
+		input_params = ecdh_param_secp256r1;
+		break;
+	case SECP384R1:
+		input_params = ecdh_param_secp384r1;
+		break;
+	case SECP521R1:
+		input_params = ecdh_param_secp521r1;
+		break;
+	default:
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Unsupported curve id\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	debug_hexdump(stdout, "pkey:",
+		input_params.pkey_A.data, input_params.pkey_A.length);
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	xform.ec.curve_id = input_params.curve;
+	xform.ec.pkey.data = input_params.pkey_A.data;
+	xform.ec.pkey.length = input_params.pkey_A.length;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Populate op with operational details */
+	asym_op->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE;
+
+	/* Init out buf */
+	asym_op->ecdh.pub_key.x.data = output_buf_x;
+	asym_op->ecdh.pub_key.y.data = output_buf_y;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	asym_op = result_op->asym;
+
+	debug_hexdump(stdout, "qx:",
+		asym_op->ecdh.pub_key.x.data, asym_op->ecdh.pub_key.x.length);
+	debug_hexdump(stdout, "qy:",
+		asym_op->ecdh.pub_key.y.data, asym_op->ecdh.pub_key.y.length);
+
+	ret = verify_ecdh_secret(input_params.pubkey_qA_x.data,
+				input_params.pubkey_qA_y.data, result_op);
+	if (ret) {
+		status = TEST_FAILED;
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"ECDH public key generation failed.\n");
+		goto exit;
+	}
+
+exit:
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+	return status;
+}
+
+static int
+test_ecdh_pub_key_verify(enum curve curve_id)
+{
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	const struct rte_cryptodev_asymmetric_xform_capability *capa;
+	struct rte_mempool *sess_mpool = ts_params->session_mpool;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	struct crypto_testsuite_ecdh_params input_params;
+	struct rte_cryptodev_asym_capability_idx idx;
+	uint8_t dev_id = ts_params->valid_devs[0];
+	struct rte_crypto_asym_xform xform = {0};
+	struct rte_crypto_op *result_op = NULL;
+	struct rte_crypto_asym_op *asym_op;
+	struct rte_crypto_op *op = NULL;
+	int ret, status = TEST_SUCCESS;
+	void *sess = NULL;
+
+	/* Check ECDH capability */
+	idx.type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	capa = rte_cryptodev_asym_capability_get(dev_id, &idx);
+	if (capa == NULL)
+		return -ENOTSUP;
+
+	if (!(capa->op_types & (1 <<  RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY)))
+		return -ENOTSUP;
+
+	switch (curve_id) {
+	case SECP192R1:
+		input_params = ecdh_param_secp192r1;
+		break;
+	case SECP224R1:
+		input_params = ecdh_param_secp224r1;
+		break;
+	case SECP256R1:
+		input_params = ecdh_param_secp256r1;
+		break;
+	case SECP384R1:
+		input_params = ecdh_param_secp384r1;
+		break;
+	case SECP521R1:
+		input_params = ecdh_param_secp521r1;
+		break;
+	default:
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Unsupported curve id\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	debug_hexdump(stdout, "qx:",
+		input_params.pubkey_qA_x.data, input_params.pubkey_qA_x.length);
+	debug_hexdump(stdout, "qy:",
+		input_params.pubkey_qA_y.data, input_params.pubkey_qA_y.length);
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	xform.ec.curve_id = input_params.curve;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Populate op with operational details */
+	asym_op->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY;
+	asym_op->ecdh.pub_key.x.data = input_params.pubkey_qA_x.data;
+	asym_op->ecdh.pub_key.x.length = input_params.pubkey_qA_x.length;
+	asym_op->ecdh.pub_key.y.data = input_params.pubkey_qA_y.data;
+	asym_op->ecdh.pub_key.y.length = input_params.pubkey_qA_y.length;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+exit:
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+	return status;
+}
+
+static int
+test_ecdh_shared_secret(enum curve curve_id)
+{
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	const struct rte_cryptodev_asymmetric_xform_capability *capa;
+	struct rte_mempool *sess_mpool = ts_params->session_mpool;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	struct crypto_testsuite_ecdh_params input_params;
+	struct rte_cryptodev_asym_capability_idx idx;
+	uint8_t dev_id = ts_params->valid_devs[0];
+	struct rte_crypto_asym_xform xform = {0};
+	struct rte_crypto_op *result_op = NULL;
+	uint8_t output_buf_x[TEST_DATA_SIZE];
+	uint8_t output_buf_y[TEST_DATA_SIZE];
+	struct rte_crypto_asym_op *asym_op;
+	struct rte_crypto_op *op = NULL;
+	int ret, status = TEST_SUCCESS;
+	void *sess = NULL;
+
+	/* Check ECDH capability */
+	idx.type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	capa = rte_cryptodev_asym_capability_get(dev_id, &idx);
+	if (capa == NULL)
+		return -ENOTSUP;
+
+	if (!(capa->op_types & (1 <<  RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE)))
+		return -ENOTSUP;
+
+	switch (curve_id) {
+	case SECP192R1:
+		input_params = ecdh_param_secp192r1;
+		break;
+	case SECP224R1:
+		input_params = ecdh_param_secp224r1;
+		break;
+	case SECP256R1:
+		input_params = ecdh_param_secp256r1;
+		break;
+	case SECP384R1:
+		input_params = ecdh_param_secp384r1;
+		break;
+	case SECP521R1:
+		input_params = ecdh_param_secp521r1;
+		break;
+	default:
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Unsupported curve id\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	/* zA = dA.QB */
+	debug_hexdump(stdout, "pkey:",
+		input_params.pkey_A.data, input_params.pkey_A.length);
+	debug_hexdump(stdout, "qx:",
+		input_params.pubkey_qB_x.data, input_params.pubkey_qB_x.length);
+	debug_hexdump(stdout, "qy:",
+		input_params.pubkey_qB_y.data, input_params.pubkey_qB_y.length);
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	xform.ec.curve_id = input_params.curve;
+	xform.ec.pkey.data = input_params.pkey_A.data;
+	xform.ec.pkey.length = input_params.pkey_A.length;
+	xform.ec.q.x.data = input_params.pubkey_qB_x.data;
+	xform.ec.q.x.length = input_params.pubkey_qB_x.length;
+	xform.ec.q.y.data = input_params.pubkey_qB_y.data;
+	xform.ec.q.y.length = input_params.pubkey_qB_y.length;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Populate op with operational details */
+	asym_op->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
+
+	/* Init out buf */
+	asym_op->ecdh.shared_secret.x.data = output_buf_x;
+	asym_op->ecdh.shared_secret.y.data = output_buf_y;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	asym_op = result_op->asym;
+
+	debug_hexdump(stdout, "secret_x:",
+		asym_op->ecdh.shared_secret.x.data, asym_op->ecdh.shared_secret.x.length);
+	debug_hexdump(stdout, "secret_y:",
+		asym_op->ecdh.shared_secret.y.data, asym_op->ecdh.shared_secret.y.length);
+
+	ret = verify_ecdh_secret(input_params.secret_x.data,
+				input_params.secret_y.data, result_op);
+	if (ret) {
+		status = TEST_FAILED;
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"ECDH shared secret compute failed.\n");
+		goto exit;
+	}
+
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+
+	/* zB = dB.QA */
+	debug_hexdump(stdout, "pkey:",
+		input_params.pkey_B.data, input_params.pkey_B.length);
+	debug_hexdump(stdout, "qx:",
+		input_params.pubkey_qA_x.data, input_params.pubkey_qA_x.length);
+	debug_hexdump(stdout, "qy:",
+		input_params.pubkey_qA_y.data, input_params.pubkey_qA_y.length);
+
+	/* Setup crypto op data structure */
+	op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to allocate asymmetric crypto "
+				"operation struct\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+	asym_op = op->asym;
+
+	/* Setup asym xform */
+	xform.next = NULL;
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH;
+	xform.ec.curve_id = input_params.curve;
+	xform.ec.pkey.data = input_params.pkey_B.data;
+	xform.ec.pkey.length = input_params.pkey_B.length;
+	xform.ec.q.x.data = input_params.pubkey_qA_x.data;
+	xform.ec.q.x.length = input_params.pubkey_qA_x.length;
+	xform.ec.q.y.data = input_params.pubkey_qA_y.data;
+	xform.ec.q.y.length = input_params.pubkey_qA_y.length;
+
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Session creation failed\n");
+		status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
+		goto exit;
+	}
+
+	/* Attach asymmetric crypto session to crypto operations */
+	rte_crypto_op_attach_asym_session(op, sess);
+
+	/* Populate op with operational details */
+	asym_op->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
+
+	/* Init out buf */
+	asym_op->ecdh.shared_secret.x.data = output_buf_x;
+	asym_op->ecdh.shared_secret.y.data = output_buf_y;
+
+	RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
+
+	/* Process crypto operation */
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Error sending packet for operation\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
+		rte_pause();
+
+	if (result_op == NULL) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"Failed to process asym crypto op\n");
+		status = TEST_FAILED;
+		goto exit;
+	}
+
+	asym_op = result_op->asym;
+
+	debug_hexdump(stdout, "secret_x:",
+			asym_op->ecdh.shared_secret.x.data, asym_op->ecdh.shared_secret.x.length);
+	debug_hexdump(stdout, "secret_y:",
+			asym_op->ecdh.shared_secret.y.data, asym_op->ecdh.shared_secret.y.length);
+
+	ret = verify_ecdh_secret(input_params.secret_x.data,
+				input_params.secret_y.data, result_op);
+	if (ret) {
+		status = TEST_FAILED;
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"ECDH shared secret compute failed.\n");
+		goto exit;
+	}
+
+exit:
+	if (sess != NULL)
+		rte_cryptodev_asym_session_free(dev_id, sess);
+	rte_crypto_op_free(op);
+	return status;
+}
+
+static int
+test_ecdh_all_curve(void)
+{
+	int status, overall_status = TEST_SUCCESS;
+	enum curve curve_id;
+	int test_index = 0;
+	const char *msg;
+
+	for (curve_id = SECP192R1; curve_id < END_OF_CURVE_LIST; curve_id++) {
+		if (curve_id == SECP521R1_UA)
+			continue;
+
+		status = test_ecdh_priv_key_generate(curve_id);
+		if (status == TEST_SUCCESS) {
+			msg = "succeeded";
+		} else {
+			msg = "failed";
+			overall_status = status;
+		}
+		printf("  %u) TestCase ECDH private key generation for Curve %s %s\n",
+		       test_index ++, curve[curve_id], msg);
+	}
+
+	for (curve_id = SECP192R1; curve_id < END_OF_CURVE_LIST; curve_id++) {
+		if (curve_id == SECP521R1_UA)
+			continue;
+
+		status = test_ecdh_pub_key_generate(curve_id);
+		if (status == TEST_SUCCESS) {
+			msg = "succeeded";
+		} else {
+			msg = "failed";
+			overall_status = status;
+		}
+		printf("  %u) TestCase ECDH public key generation for Curve %s %s\n",
+		       test_index ++, curve[curve_id], msg);
+	}
+
+	for (curve_id = SECP192R1; curve_id < END_OF_CURVE_LIST; curve_id++) {
+		if (curve_id == SECP521R1_UA)
+			continue;
+
+		status = test_ecdh_pub_key_verify(curve_id);
+		if (status == TEST_SUCCESS) {
+			msg = "succeeded";
+		} else {
+			msg = "failed";
+			overall_status = status;
+		}
+		printf("  %u) TestCase ECDH public key verification for Curve %s %s\n",
+		       test_index ++, curve[curve_id], msg);
+	}
+
+	for (curve_id = SECP192R1; curve_id < END_OF_CURVE_LIST; curve_id++) {
+		if (curve_id == SECP521R1_UA)
+			continue;
+
+		status = test_ecdh_shared_secret(curve_id);
+		if (status == TEST_SUCCESS) {
+			msg = "succeeded";
+		} else {
+			msg = "failed";
+			overall_status = status;
+		}
+		printf("  %u) TestCase ECDH shared secret compute for Curve %s %s\n",
+		       test_index ++, curve[curve_id], msg);
+	}
+
+	return overall_status;
+}
+
 static int
 test_sm2_sign(void)
 {
@@ -2853,6 +3592,8 @@ static struct unit_test_suite cryptodev_octeontx_asym_testsuite  = {
 			     test_ecdsa_sign_verify_all_curve),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_sm2_sign),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_sm2_verify),
+		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
+				test_ecdh_all_curve),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
 				test_ecpm_all_curve),
 		TEST_CASES_END() /**< NULL terminate unit test array */
app/test/test_cryptodev_asym_util.h +12 -0
index 83dc265dd7..07e6e831e8 100644
@@ -57,4 +57,16 @@ static inline int verify_ecpm(uint8_t *result_x, uint8_t *result_y,
 
 	return 0;
 }
+
+static inline int verify_ecdh_secret(uint8_t *result_x, uint8_t *result_y,
+			      struct rte_crypto_op *result_op)
+{
+	if (memcmp(result_x, result_op->asym->ecdh.shared_secret.x.data,
+		   result_op->asym->ecdh.shared_secret.x.length) ||
+		   memcmp(result_y, result_op->asym->ecdh.shared_secret.y.data,
+		   result_op->asym->ecdh.shared_secret.y.length))
+		return -1;
+
+	return 0;
+}
 #endif /* TEST_CRYPTODEV_ASYM_TEST_UTIL_H__ */
app/test/test_cryptodev_ecdh_test_vectors.h new file +556 -0

556 lines not on this page · raw message

Signature · 1 line
2.25.1
Applied
#4 lore raw
Quoted 9 lines

Subject: [PATCH v2] test/cryptodev: add ECDH tests

Add ECDH tests.

Change-Id I88099e2ba8e058fbb519a06d09ebb3ece7c7e27f

---
v2:
- rebased due to patch conflict.

Applied to dpdk-next-crypto
Removed change-id while merging.

#5 lore raw

PATCH 2/4 crypto/cnxk: use generic EC opcodes

In reply to Gowrishankar Muthukrishnan

Use generic EC opcodes for sign and verify ops in ECDSA and SM2 implementations.

---
 drivers/common/cnxk/roc_ae.h  | 14 +++++++---
 drivers/crypto/cnxk/cnxk_ae.h | 50 +++++++++++++++++++----------------
 2 files changed, 38 insertions(+), 26 deletions(-)
drivers/common/cnxk/roc_ae.h +11 -3
index d459c5e680..eaf12ab254 100644
@@ -5,9 +5,11 @@
 #ifndef __ROC_AE_H__
 #define __ROC_AE_H__
 
+#include "roc_platform.h"
+
 /* AE opcodes */
 #define ROC_AE_MAJOR_OP_MODEX	     0x03
-#define ROC_AE_MAJOR_OP_ECDSA	     0x04
+#define ROC_AE_MAJOR_OP_EC	     0x04
 #define ROC_AE_MAJOR_OP_ECC	     0x05
 #define ROC_AE_MINOR_OP_MODEX	     0x01
 #define ROC_AE_MINOR_OP_PKCS_ENC     0x02
@@ -15,8 +17,8 @@
 #define ROC_AE_MINOR_OP_PKCS_DEC     0x04
 #define ROC_AE_MINOR_OP_PKCS_DEC_CRT 0x05
 #define ROC_AE_MINOR_OP_MODEX_CRT    0x06
-#define ROC_AE_MINOR_OP_ECDSA_SIGN   0x01
-#define ROC_AE_MINOR_OP_ECDSA_VERIFY 0x02
+#define ROC_AE_MINOR_OP_EC_SIGN      0x01
+#define ROC_AE_MINOR_OP_EC_VERIFY    0x02
 #define ROC_AE_MINOR_OP_ECC_UMP	     0x03
 #define ROC_AE_MINOR_OP_ECC_FPM	     0x04
 
@@ -38,6 +40,12 @@ typedef enum {
 	ROC_AE_EC_ID_PMAX
 } roc_ae_ec_id;
 
+/* EC param1 fields */
+#define ROC_AE_EC_PARAM1_ECDSA     (0 << 7)
+#define ROC_AE_EC_PARAM1_SM2       (1 << 7)
+#define ROC_AE_EC_PARAM1_NIST      (0 << 6)
+#define ROC_AE_EC_PARAM1_NONNIST   (1 << 6)
+
 /* Prime and order fields of built-in elliptic curves */
 struct roc_ae_ec_group {
 	struct {
drivers/crypto/cnxk/cnxk_ae.h +27 -23

102 lines not on this page · raw message

Signature · 1 line
2.25.1
#6 lore raw

PATCH 3/4 crypto/cnxk: change order of ECFPM params

In reply to Gowrishankar Muthukrishnan

Change order of ECFPM params to match changes in v2.0 microcode.

---
 drivers/crypto/cnxk/cnxk_ae.h | 49 ++++++++++++++---------------------
 1 file changed, 20 insertions(+), 29 deletions(-)
drivers/crypto/cnxk/cnxk_ae.h +20 -29
index 6e61ccb0c5..f05ae4f4a1 100644
@@ -923,7 +923,7 @@ static __rte_always_inline int
 cnxk_ae_ecfpm_prep(struct rte_crypto_ecpm_op_param *ecpm,
 		   struct roc_ae_buf_ptr *meta_buf, uint64_t *fpm_iova,
 		   struct roc_ae_ec_group *ec_grp, uint8_t curveid,
-		   struct cpt_inst_s *inst, int cpt_ver)
+		   struct cpt_inst_s *inst)
 {
 	uint16_t scalar_align, p_align;
 	uint16_t dlen, prime_len;
@@ -942,34 +942,26 @@ cnxk_ae_ecfpm_prep(struct rte_crypto_ecpm_op_param *ecpm,
 	scalar_align = RTE_ALIGN_CEIL(ecpm->scalar.length, 8);
 
 	/*
-	 * Set dlen = sum(prime, scalar length, table address and
-	 * optionally ROUNDUP8(input point(x and y coordinates)).
+	 * Set dlen = sum(ROUNDUP8(input point(x and y coordinates), prime,
+	 * scalar length),
 	 * Please note point length is equivalent to prime of the curve
 	 */
-	if (cpt_ver == ROC_CPT_REVISION_ID_96XX_B0 || cpt_ver == ROC_CPT_REVISION_ID_96XX_C0 ||
-	    cpt_ver == ROC_CPT_REVISION_ID_98XX) {
-		dlen = sizeof(fpm_table_iova) + 3 * p_align + scalar_align;
-		memset(dptr, 0, dlen);
-		*(uint64_t *)dptr = fpm_table_iova;
-		dptr += sizeof(fpm_table_iova);
-		memcpy(dptr, ecpm->scalar.data, ecpm->scalar.length);
-		dptr += scalar_align;
-		memcpy(dptr, ec_grp->prime.data, ec_grp->prime.length);
-		dptr += p_align;
-		memcpy(dptr, ec_grp->consta.data, ec_grp->consta.length);
-		dptr += p_align;
-		memcpy(dptr, ec_grp->constb.data, ec_grp->constb.length);
-		dptr += p_align;
-	} else {
-		dlen = sizeof(fpm_table_iova) + p_align + scalar_align;
-		memset(dptr, 0, dlen);
-		memcpy(dptr, ecpm->scalar.data, ecpm->scalar.length);
-		dptr += scalar_align;
-		memcpy(dptr, ec_grp->prime.data, ec_grp->prime.length);
-		dptr += p_align;
-		*(uint64_t *)dptr = fpm_table_iova;
-		dptr += sizeof(fpm_table_iova);
-	}
+	dlen = sizeof(fpm_table_iova) + 3 * p_align + scalar_align;
+
+	memset(dptr, 0, dlen);
+
+	*(uint64_t *)dptr = fpm_table_iova;
+	dptr += sizeof(fpm_table_iova);
+
+	/* Copy scalar, prime */
+	memcpy(dptr, ecpm->scalar.data, ecpm->scalar.length);
+	dptr += scalar_align;
+	memcpy(dptr, ec_grp->prime.data, ec_grp->prime.length);
+	dptr += p_align;
+	memcpy(dptr, ec_grp->consta.data, ec_grp->consta.length);
+	dptr += p_align;
+	memcpy(dptr, ec_grp->constb.data, ec_grp->constb.length);
+	dptr += p_align;
 
 	/* Setup opcodes */
 	w4.s.opcode_major = ROC_AE_MAJOR_OP_ECC;
@@ -1224,8 +1216,7 @@ cnxk_ae_enqueue(struct cnxk_cpt_qp *qp, struct rte_crypto_op *op,
 		ret = cnxk_ae_ecfpm_prep(&asym_op->ecpm, &meta_buf,
 					 sess->cnxk_fpm_iova,
 					 sess->ec_grp[sess->ec_ctx.curveid],
-					 sess->ec_ctx.curveid, inst,
-					 sess->lf->roc_cpt->cpt_revision);
+					 sess->ec_ctx.curveid, inst);
 		if (unlikely(ret))
 			goto req_fail;
 		break;
Signature · 1 line
2.25.1
#7 lore raw

PATCH 4/4 crypto/cnxk: add ECDH support

In reply to Gowrishankar Muthukrishnan

Add ECDH support in CNXK PMD.

---
 doc/guides/cryptodevs/features/cn10k.ini      |   1 +
 doc/guides/cryptodevs/features/cn9k.ini       |   1 +
 drivers/common/cnxk/roc_ae.h                  |   7 +
 drivers/crypto/cnxk/cn10k_cryptodev_ops.c     |  12 ++
 drivers/crypto/cnxk/cnxk_ae.h                 | 143 ++++++++++++++++--
 drivers/crypto/cnxk/cnxk_cryptodev.h          |   2 +-
 .../crypto/cnxk/cnxk_cryptodev_capabilities.c |  14 ++
 7 files changed, 165 insertions(+), 15 deletions(-)
doc/guides/cryptodevs/features/cn10k.ini +1 -0
index 4f542c6038..ea8a22eb46 100644
@@ -102,6 +102,7 @@ DSA                     =
 Modular Exponentiation  = Y
 Modular Inversion       =
 Diffie-hellman          =
+ECDH                    = Y
 ECDSA                   = Y
 ECPM                    = Y
 SM2                     = Y
doc/guides/cryptodevs/features/cn9k.ini +1 -0
index bf0e1a98b2..d70771d9bd 100644
@@ -96,6 +96,7 @@ DSA                     =
 Modular Exponentiation  = Y
 Modular Inversion       =
 Diffie-hellman          =
+ECDH                    = Y
 ECDSA                   = Y
 ECPM                    = Y
 
drivers/common/cnxk/roc_ae.h +7 -0
index eaf12ab254..a9a08d9fb9 100644
@@ -8,9 +8,11 @@
 #include "roc_platform.h"
 
 /* AE opcodes */
+#define ROC_AE_MAJOR_OP_RANDOM	     0x32
 #define ROC_AE_MAJOR_OP_MODEX	     0x03
 #define ROC_AE_MAJOR_OP_EC	     0x04
 #define ROC_AE_MAJOR_OP_ECC	     0x05
+#define ROC_AE_MINOR_OP_RANDOM	     0x00
 #define ROC_AE_MINOR_OP_MODEX	     0x01
 #define ROC_AE_MINOR_OP_PKCS_ENC     0x02
 #define ROC_AE_MINOR_OP_PKCS_ENC_CRT 0x03
@@ -46,6 +48,11 @@ typedef enum {
 #define ROC_AE_EC_PARAM1_NIST      (0 << 6)
 #define ROC_AE_EC_PARAM1_NONNIST   (1 << 6)
 
+typedef enum {
+	ROC_AE_ERR_ECC_PAI = 0x0b,
+	ROC_AE_ERR_ECC_POINT_NOT_ON_CURVE = 0x11
+} roc_ae_error_code;
+
 /* Prime and order fields of built-in elliptic curves */
 struct roc_ae_ec_group {
 	struct {
drivers/crypto/cnxk/cn10k_cryptodev_ops.c +12 -0
index 5f181e8839..997110e3d3 100644
@@ -860,6 +860,18 @@ cn10k_cpt_dequeue_post_process(struct cnxk_cpt_qp *qp,
 		}
 
 		return;
+	} else if (cop->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC &&
+			   cop->sess_type == RTE_CRYPTO_OP_WITH_SESSION &&
+			   cop->asym->ecdh.ke_type == RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY) {
+		if (likely(compcode == CPT_COMP_GOOD)) {
+			if (uc_compcode == ROC_AE_ERR_ECC_POINT_NOT_ON_CURVE) {
+				cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
+				return;
+			} else if (uc_compcode == ROC_AE_ERR_ECC_PAI) {
+				cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
+				return;
+			}
+		}
 	}
 
 	if (likely(compcode == CPT_COMP_GOOD)) {
drivers/crypto/cnxk/cnxk_ae.h +129 -14

239 lines not on this page · raw message

drivers/crypto/cnxk/cnxk_cryptodev.h +1 -1
index 9a321aa8c9..d0ad881f2f 100644
@@ -10,7 +10,7 @@
 
 #include "roc_cpt.h"
 
-#define CNXK_CPT_MAX_CAPS	 54
+#define CNXK_CPT_MAX_CAPS	 55
 #define CNXK_SEC_CRYPTO_MAX_CAPS 16
 #define CNXK_SEC_MAX_CAPS	 9
 #define CNXK_AE_EC_ID_MAX	 9
drivers/crypto/cnxk/cnxk_cryptodev_capabilities.c +14 -0
index b4864f66bf..2676b52832 100644
@@ -92,6 +92,20 @@ static const struct rte_cryptodev_capabilities caps_mul[] = {
 			},
 		}
 	},
+	{	/* ECDH */
+		.op = RTE_CRYPTO_OP_TYPE_ASYMMETRIC,
+		{.asym = {
+			.xform_capa = {
+				.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH,
+				.op_types = ((1 << RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE) |
+						(1 << RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE) |
+						(1 << RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY) |
+						(1 << RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE)
+						),
+				}
+			},
+		}
+	},
 };
 
 static const struct rte_cryptodev_capabilities caps_sha1_sha2[] = {
Signature · 1 line
2.25.1