Skip to contentSkip to conversation

app/test: add diffie-hellman test cases

PATCH Superseded Kai Ji 3 participants 8 messages Mar 14, 2023 – Jun 23, 2023

lore
Share & tools
lore

Kai Ji

author
#0 lore raw

PATCH 1/2 app/test: add diffie-hellman test cases

From: Arkadiusz Kusztal <[email protected]>

Added Diffie-Hellman tests. This tests adding possibility to check correctness of particular DH phases, which is not possible with current tests cases.

---
 app/test/test_cryptodev_asym.c            | 262 ++++++++++++++++++
 app/test/test_cryptodev_dh_test_vectors.h | 318 ++++++++++++++++++++++
 2 files changed, 580 insertions(+)
app/test/test_cryptodev_asym.c +262 -0
index 5b16dcab56..91550946e2 100644
@@ -64,6 +64,39 @@ static uint32_t test_index;
 
 static struct crypto_testsuite_params_asym testsuite_params = { NULL };
 
+static void
+test_crypto_rand(int len, uint8_t *buffer)
+{
+	int i;
+
+	for (i = 0; i < len; ++i)
+		buffer[i] = (uint8_t)(rand() % ((uint8_t)-1)) | 1;
+}
+
+static int
+process_crypto_request(uint8_t dev_id, struct rte_crypto_op **op,
+				struct rte_crypto_op **result_op)
+{
+	/* 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");
+		return -1;
+	}
+
+	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");
+		return -1;
+	}
+	return 0;
+}
+
 static int
 queue_ops_rsa_sign_verify(void *sess)
 {
@@ -2196,6 +2229,219 @@ test_ecpm_all_curve(void)
 	return overall_status;
 }
 
+static void *
+dh_alice_bob_set_session(uint8_t dev_id,
+	struct rte_crypto_op *op,
+	const struct test_dh_group *group)
+{
+	struct rte_crypto_asym_xform xform = { };
+	void *sess = NULL;
+	int ret = 0;
+
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_DH;
+	xform.dh.g.data = group->g.data;
+	xform.dh.g.length = group->g.bytesize;
+	xform.dh.p.data = group->p.data;
+	xform.dh.p.length = group->p.bytesize;
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform,
+			testsuite_params.session_mpool, &sess);
+	if (ret)
+		return NULL;
+
+	rte_crypto_op_attach_asym_session(op, sess);
+	return sess;
+}
+
+static void
+dh_alice_bob_gen_x(const struct test_dh_group *group,
+	uint8_t *private_data)
+{
+	test_crypto_rand(group->priv_ff_size, private_data);
+	if (private_data[0] > group->p.data[0])
+		private_data[0] = group->p.data[0] - 1;
+}
+
+static int
+dh_alice_bob_gen_y(struct rte_crypto_op *op,
+	const char *name,
+	const uint8_t dev_id,
+	uint8_t *y,
+	uint8_t *x,
+	const int ff_size)
+{
+	struct rte_crypto_op *result_op;
+	int ret = 0;
+
+	op->asym->dh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE;
+	op->asym->dh.pub_key.data = y;
+	op->asym->dh.priv_key.data = x;
+	op->asym->dh.priv_key.length = ff_size;
+
+	ret = process_crypto_request(dev_id, &op, &result_op);
+	TEST_ASSERT_SUCCESS(ret, "Failed to compute public key for %s", name);
+
+	return 0;
+}
+
+static int
+dh_alice_bob_shared_compute(uint8_t dev_id,
+	const char *name,
+	struct rte_crypto_op *op,
+	uint8_t *secret,
+	uint8_t *peer,
+	uint32_t peer_size)
+{
+	struct rte_crypto_op *result_op;
+	int ret = 0;
+
+	op->asym->dh.ke_type = RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
+	op->asym->dh.pub_key.data = peer;
+	op->asym->dh.pub_key.length = peer_size;
+	op->asym->dh.shared_secret.data = secret;
+
+	ret = process_crypto_request(dev_id, &op, &result_op);
+	TEST_ASSERT_SUCCESS(ret,
+		"Failed to compute shared secret for %s",
+		name);
+
+	return 0;
+}
+
+/* Diffie-Hellman test to verify the processed data */
+static int
+dh_alice_bob_verify(struct rte_crypto_op *alice_op,
+	struct rte_crypto_op *bob_op)
+{
+	int ret = 0;
+
+	/* Verify processed data */
+	ret = (alice_op->asym->dh.shared_secret.length ==
+			bob_op->asym->dh.shared_secret.length);
+	if (!ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Alice's and Bob's shared secret length do not match.");
+		return TEST_FAILED;
+	}
+	ret = memcmp(alice_op->asym->dh.shared_secret.data,
+		alice_op->asym->dh.shared_secret.data,
+		bob_op->asym->dh.shared_secret.length);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Alice's and Bob's shared secret do not match.");
+		return TEST_FAILED;
+	}
+	return TEST_SUCCESS;
+}
+
+static int
+test_dh_alice_and_bob(const void *test_data)
+{
+	uint8_t alice_x[TEST_DH_MOD_LEN] = { };
+	uint8_t alice_y[TEST_DH_MOD_LEN] = { };
+	uint8_t alice_secret[TEST_DH_MOD_LEN] = { };
+	uint8_t bob_x[TEST_DH_MOD_LEN] = { };
+	uint8_t bob_y[TEST_DH_MOD_LEN] = { };
+	uint8_t bob_secret[TEST_DH_MOD_LEN] = { };
+
+	const struct test_dh_group *group = test_data;
+	struct rte_crypto_op *alice_op = NULL;
+	struct rte_crypto_op *bob_op = NULL;
+	void *alice_session = NULL;
+	void *bob_session = NULL;
+
+	int ret = TEST_SUCCESS;
+	uint32_t alice_y_size = 0;
+	uint32_t bob_y_size = 0;
+	uint8_t dev_id = testsuite_params.valid_devs[0];
+
+	alice_op = rte_crypto_op_alloc(testsuite_params.op_mpool,
+			RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (alice_op == NULL) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Cannot allocate alice_op");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+	bob_op = rte_crypto_op_alloc(testsuite_params.op_mpool,
+			RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (bob_op == NULL) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Cannot allocate bob_op");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+
+	alice_session = dh_alice_bob_set_session(dev_id, alice_op,
+		group);
+	if (alice_session == NULL) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Cannot allocate alice_session");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+	bob_session = dh_alice_bob_set_session(dev_id, bob_op,
+		group);
+	if (bob_session == NULL) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Cannot allocate bob_session");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+
+	dh_alice_bob_gen_x(group, alice_x);
+	ret = dh_alice_bob_gen_y(alice_op, "Alice", dev_id,
+		alice_y, alice_x, group->priv_ff_size);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+		"Diffie-Hellman error\n");
+		goto error_exit;
+	}
+	alice_y_size = alice_op->asym->dh.pub_key.length;
+	dh_alice_bob_gen_x(group, bob_x);
+	ret = dh_alice_bob_gen_y(bob_op, "Bob", dev_id,
+		bob_y, bob_x, group->priv_ff_size);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+		"Diffie-Hellman error\n");
+		goto error_exit;
+	}
+	bob_y_size = bob_op->asym->dh.pub_key.length;
+
+	ret = dh_alice_bob_shared_compute(dev_id, "Alice", alice_op,
+		alice_secret, bob_y, bob_y_size);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+		"Diffie-Hellman error, error computing Alice's shared secret");
+		goto error_exit;
+	}
+
+	ret = dh_alice_bob_shared_compute(dev_id, "Bob", bob_op, bob_secret,
+		alice_y, alice_y_size);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+		"Diffie-Hellman error, error computing Bob's shared secret");
+		goto error_exit;
+	}
+
+	ret = dh_alice_bob_verify(alice_op, bob_op);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+		"Diffie-Hellman error");
+		goto error_exit;
+	}
+
+error_exit:
+	if (alice_session)
+		rte_cryptodev_asym_session_free(dev_id, alice_session);
+	if (bob_session)
+		rte_cryptodev_asym_session_free(dev_id, bob_session);
+	if (alice_op)
+		rte_crypto_op_free(alice_op);
+	if (bob_op != NULL)
+		rte_crypto_op_free(bob_op);
+	return ret;
+}
+
 static struct unit_test_suite cryptodev_openssl_asym_testsuite  = {
 	.suite_name = "Crypto Device OPENSSL ASYM Unit Test Suite",
 	.setup = testsuite_setup,
@@ -2215,6 +2461,22 @@ static struct unit_test_suite cryptodev_openssl_asym_testsuite  = {
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_mod_inv),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_mod_exp),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_one_by_one),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Diffie-Hellman Alice and Bob group 14 ikev2 test",
+			ut_setup_asym, ut_teardown_asym,
+			test_dh_alice_and_bob, &test_dh_ikev2group_14),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Diffie-Hellman Alice and Bob group 15 ikev2 test",
+			ut_setup_asym, ut_teardown_asym,
+			test_dh_alice_and_bob, &test_dh_ikev2group_15),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Diffie-Hellman Alice and Bob group 16 ikev2 test",
+			ut_setup_asym, ut_teardown_asym,
+			test_dh_alice_and_bob, &test_dh_ikev2group_16),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Diffie-Hellman Alice and Bob group 24 ikev2 test",
+			ut_setup_asym, ut_teardown_asym,
+			test_dh_alice_and_bob, &test_dh_ikev2group_24),
 		TEST_CASES_END() /**< NULL terminate unit test array */
 	}
 };
app/test/test_cryptodev_dh_test_vectors.h +318 -0

328 lines not on this page · raw message

--
2.17.1

author
#1 lore raw

PATCH 2/2 app/test: add ecdh test cases

From: Arkadiusz Kusztal <[email protected]>

Added Elliptic-Curve Diffie Hellman test cases.

---
 app/test/test_cryptodev_asym.c              | 356 ++++++++++++++++++++
 app/test/test_cryptodev_ecdh_test_vectors.h | 144 ++++++++
 2 files changed, 500 insertions(+)
 create mode 100644 app/test/test_cryptodev_ecdh_test_vectors.h
app/test/test_cryptodev_asym.c +356 -0
index 91550946e2..9e475051e0 100644
@@ -19,6 +19,7 @@
 #include "test_cryptodev_dsa_test_vectors.h"
 #include "test_cryptodev_ecdsa_test_vectors.h"
 #include "test_cryptodev_ecpm_test_vectors.h"
+#include "test_cryptodev_ecdh_test_vectors.h"
 #include "test_cryptodev_mod_test_vectors.h"
 #include "test_cryptodev_rsa_test_vectors.h"
 #include "test_cryptodev_asym_util.h"
@@ -2229,6 +2230,357 @@ test_ecpm_all_curve(void)
 	return overall_status;
 }
 
+static int
+test_ecdh(const void *input_vector)
+{
+	const struct ECDH_test_vector *test_vector = input_vector;
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	struct rte_crypto_asym_xform xform = {
+		.next = NULL,
+		.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH,
+		.ec.curve_id = test_vector->curve_id
+	};
+	const uint8_t dev_id = ts_params->valid_devs[0];
+	uint8_t alice_x[test_vector->curve_bytesize],
+		alice_y[test_vector->curve_bytesize];
+	uint8_t bob_x[test_vector->curve_bytesize],
+		bob_y[test_vector->curve_bytesize];
+	uint8_t alice_xZ[test_vector->curve_bytesize],
+		alice_yZ[test_vector->curve_bytesize];
+	uint8_t bob_xZ[test_vector->curve_bytesize],
+		bob_yZ[test_vector->curve_bytesize];
+	struct rte_crypto_op *alice_op = NULL, *bob_op = NULL;
+
+	alice_op = rte_crypto_op_alloc(op_mpool,
+		RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	bob_op = rte_crypto_op_alloc(op_mpool,
+		RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+
+	/*
+	 * STEP 1a:
+	 * Generate Alice public key
+	 */
+
+	alice_op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
+	alice_op->asym->xform = &xform;
+	alice_op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE;
+	alice_op->asym->ecdh.priv_key = test_vector->alice_d;
+	alice_op->asym->ecdh.pub_key.x.data = alice_x;
+	alice_op->asym->ecdh.pub_key.y.data = alice_y;
+
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &alice_op, 1) != 1) {
+		RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
+		return -1;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &alice_op, 1) == 0)
+		rte_pause();
+
+	if (alice_op->asym->ecdh.pub_key.x.length !=
+			test_vector->alice_q.x.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice public key length (X coeff)\n");
+		RTE_LOG(ERR, USER1,
+			"Received length = %d Expected length = %d\n",
+			(unsigned int)test_vector->alice_q.x.length,
+			(unsigned int)alice_op->asym->ecdh.pub_key.x.length
+		);
+		goto error;
+	}
+	if (alice_op->asym->ecdh.pub_key.y.length !=
+			test_vector->alice_q.y.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice public key length (X coeff)\n");
+		RTE_LOG(ERR, USER1,
+			"Received length = %d Expected length = %d\n",
+			(unsigned int)test_vector->alice_q.y.length,
+			(unsigned int)alice_op->asym->ecdh.pub_key.y.length
+		);
+		goto error;
+	}
+	if (memcmp(alice_op->asym->ecdh.pub_key.x.data,
+			test_vector->alice_q.x.data,
+			test_vector->alice_q.x.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice public key X\n");
+		debug_hexdump(stdout, "Generated PublicKey x (Alice):",
+			alice_op->asym->ecdh.pub_key.x.data,
+			alice_op->asym->ecdh.pub_key.x.length);
+		goto error;
+	}
+	if (memcmp(alice_op->asym->ecdh.pub_key.y.data,
+			test_vector->alice_q.y.data,
+			test_vector->alice_q.y.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice public key Y\n");
+		debug_hexdump(stdout, "Generated PublicKey y (Alice):",
+				alice_op->asym->ecdh.pub_key.y.data,
+				alice_op->asym->ecdh.pub_key.y.length);
+		goto error;
+	}
+
+
+	/*
+	 * STEP 1b:
+	 * Generate Bob public key
+	 */
+
+	bob_op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
+	bob_op->asym->xform = &xform;
+	bob_op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE;
+	bob_op->asym->ecdh.priv_key = test_vector->bob_d;
+	bob_op->asym->ecdh.pub_key.x.data = bob_x;
+	bob_op->asym->ecdh.pub_key.y.data = bob_y;
+
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &bob_op, 1) != 1) {
+		RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
+		goto error;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &bob_op, 1) == 0)
+		rte_pause();
+
+	if (bob_op->asym->ecdh.pub_key.x.length !=
+			test_vector->bob_q.x.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's public key length (X coeff)\n");
+		RTE_LOG(ERR, USER1,
+			"Received length = %d Expected length = %d\n",
+			(unsigned int)test_vector->bob_q.x.length,
+			(unsigned int)bob_op->asym->ecdh.pub_key.x.length
+		);
+		goto error;
+	}
+	if (bob_op->asym->ecdh.pub_key.y.length !=
+			test_vector->bob_q.y.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's public key length (Y coeff)\n");
+		RTE_LOG(ERR, USER1,
+			"Received length = %d Expected length = %d\n",
+			(unsigned int)test_vector->bob_q.y.length,
+			(unsigned int)bob_op->asym->ecdh.pub_key.y.length
+		);
+		goto error;
+	}
+	if (memcmp(bob_op->asym->ecdh.pub_key.x.data,
+			test_vector->bob_q.x.data,
+			test_vector->bob_q.x.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's public key X\n");
+		debug_hexdump(stdout,
+			"Generated PublicKey x (bob):",
+			bob_op->asym->ecdh.pub_key.x.data,
+			bob_op->asym->ecdh.pub_key.x.length
+		);
+		goto error;
+	}
+	if (memcmp(bob_op->asym->ecdh.pub_key.y.data,
+			test_vector->bob_q.y.data,
+			test_vector->bob_q.y.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's public key Y\n");
+		debug_hexdump(stdout,
+			"Generated PublicKey y (bob):",
+			bob_op->asym->ecdh.pub_key.y.data,
+			bob_op->asym->ecdh.pub_key.y.length
+		);
+		goto error;
+	}
+
+	/*
+	 * STEP 1.5a:
+	 * Alice verifies Bob's public key, reusing op, other parameters
+	 * then already set, only what Alice needs to do is to set public
+	 * key of Bob and change key exchange type to VERIFY.
+	 */
+
+	alice_op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY;
+	alice_op->asym->ecdh.pub_key.x.data = bob_x;
+	alice_op->asym->ecdh.pub_key.x.length =
+		bob_op->asym->ecdh.pub_key.x.length;
+	alice_op->asym->ecdh.pub_key.y.data = bob_y;
+	alice_op->asym->ecdh.pub_key.y.length =
+		bob_op->asym->ecdh.pub_key.y.length;
+
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &alice_op, 1) != 1) {
+		RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
+		goto error;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &alice_op, 1) == 0)
+		rte_pause();
+
+	if (alice_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+			"line %u FAILED: %s", __LINE__,
+			"Failed to verify Bob's public key, it does not belongs to curve points\n"
+		);
+		return TEST_FAILED;
+	}
+
+	/*
+	 * STEP 1.5b:
+	 * Bob verifies Alice's public key, reusing op, other parameters
+	 * then already set, only what Bob needs to do is to set public
+	 * key of Alice and change key exchange type to VERIFY.
+	 */
+
+	bob_op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY;
+	bob_op->asym->ecdh.pub_key.x.data = alice_x;
+	bob_op->asym->ecdh.pub_key.x.length =
+		alice_op->asym->ecdh.pub_key.x.length;
+	bob_op->asym->ecdh.pub_key.y.data = alice_y;
+	bob_op->asym->ecdh.pub_key.y.length =
+		alice_op->asym->ecdh.pub_key.y.length;
+
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &bob_op, 1) != 1) {
+		RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
+		goto error;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &bob_op, 1) == 0)
+		rte_pause();
+
+	if (bob_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+			"line %u FAILED: %s", __LINE__,
+			"Failed to verify Alice's public key it does not belongs to curve points\n"
+		);
+		goto error;
+	}
+
+	/*
+	 * STEP 2a:
+	 * Alice generates shared secret Z, since Bob's public key was already
+	 * set when doing verification only key exchange type need to be changed,
+	 * and setting place where Z will be copied by the PMD.
+	 */
+
+	alice_op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
+	alice_op->asym->ecdh.shared_secret.x.data = alice_xZ;
+	alice_op->asym->ecdh.shared_secret.y.data = alice_yZ;
+
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &alice_op, 1) != 1) {
+		RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
+		goto error;
+	}
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &alice_op, 1) == 0)
+		rte_pause();
+
+	if (alice_op->asym->ecdh.shared_secret.x.length !=
+			test_vector->Z.x.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice's shared secret length X\n");
+		RTE_LOG(ERR, USER1,
+			"Received = %d Expected = %d\n",
+			(unsigned int)alice_op->asym->ecdh.shared_secret.x.length,
+			(unsigned int)test_vector->Z.x.length
+		);
+		goto error;
+	}
+	if (alice_op->asym->ecdh.shared_secret.y.length != test_vector->Z.y.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice's shared secret length Y\n");
+		RTE_LOG(ERR, USER1,
+			"Received = %d Expected = %d\n",
+			(unsigned int)alice_op->asym->ecdh.shared_secret.y.length,
+			(unsigned int)test_vector->Z.y.length
+		);
+		goto error;
+	}
+
+	if (memcmp(alice_op->asym->ecdh.shared_secret.x.data,
+			test_vector->Z.x.data,
+			test_vector->Z.x.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice's shared secret X\n");
+		debug_hexdump(stdout,
+			"Generated SharedSecret x (Alice):",
+			alice_op->asym->ecdh.shared_secret.x.data,
+			alice_op->asym->ecdh.shared_secret.x.length
+		);
+		goto error;
+	}
+	if (memcmp(alice_op->asym->ecdh.shared_secret.y.data,
+			test_vector->Z.y.data,
+			test_vector->Z.y.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice's shared secret Y\n");
+		debug_hexdump(stdout,
+			"Generated SharedSecret y (Alice):",
+			alice_op->asym->ecdh.shared_secret.y.data,
+			alice_op->asym->ecdh.shared_secret.y.length
+		);
+		goto error;
+	}
+
+	/*
+	 * STEP 2b:
+	 * Bob generates shared secret Z, since Alice's public key was already
+	 * set when doing verification only, key exchange type needs to be
+	 * changed, and setting place where Z will be copied by the PMD.
+	 */
+
+	bob_op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
+	bob_op->asym->ecdh.shared_secret.x.data = bob_xZ;
+	bob_op->asym->ecdh.shared_secret.y.data = bob_yZ;
+
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &bob_op, 1) != 1) {
+		RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
+		goto error;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &bob_op, 1) == 0)
+		rte_pause();
+
+	if (bob_op->asym->ecdh.shared_secret.x.length != test_vector->Z.x.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's shared secret length X\n");
+		RTE_LOG(ERR, USER1,
+			"Received = %d Expected = %d\n",
+			(unsigned int)bob_op->asym->ecdh.shared_secret.x.length,
+			(unsigned int)test_vector->Z.x.length
+		);
+		goto error;
+	}
+	if (bob_op->asym->ecdh.shared_secret.y.length != test_vector->Z.y.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's shared secret length Y\n");
+		RTE_LOG(ERR, USER1,
+			"Received = %d Expected = %d\n",
+			(unsigned int)bob_op->asym->ecdh.shared_secret.y.length,
+			(unsigned int)test_vector->Z.y.length
+		);
+		goto error;
+	}
+
+	if (memcmp(bob_op->asym->ecdh.shared_secret.x.data,
+			test_vector->Z.x.data,
+			test_vector->Z.x.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's shared secret X\n");
+		debug_hexdump(stdout,
+			"Generated SharedSecret x (Bob):",
+			bob_op->asym->ecdh.shared_secret.x.data,
+			bob_op->asym->ecdh.shared_secret.x.length
+		);
+		goto error;
+	}
+	if (memcmp(bob_op->asym->ecdh.shared_secret.y.data,
+			test_vector->Z.y.data,
+			test_vector->Z.y.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's shared secret Y\n");
+		debug_hexdump(stdout,
+			"Generated SharedSecret y (Bob):",
+			bob_op->asym->ecdh.shared_secret.y.data,
+			bob_op->asym->ecdh.shared_secret.y.length
+		);
+		goto error;
+	}
+
+	/* Both shared secret where correctly verified -> Test passed */
+
+	return TEST_SUCCESS;
+error:
+	rte_crypto_op_free(alice_op);
+	rte_crypto_op_free(bob_op);
+	return TEST_FAILED;
+}
+
 static void *
 dh_alice_bob_set_session(uint8_t dev_id,
 	struct rte_crypto_op *op,
@@ -2487,6 +2839,10 @@ static struct unit_test_suite cryptodev_qat_asym_testsuite  = {
 	.teardown = testsuite_teardown,
 	.unit_test_cases = {
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_one_by_one),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Test - ECDH secp256r1, RFC 5114 256",
+			ut_setup_asym, ut_teardown_asym,
+			test_ecdh, &rfc5114_secp256r1),
 		TEST_CASES_END() /**< NULL terminate unit test array */
 	}
 };
app/test/test_cryptodev_ecdh_test_vectors.h new file +144 -0

144 lines not on this page · raw message

--
2.17.1

author
#3 lore raw

v2 cover app/test: add asymmetric test cases

In reply to Kai Ji

This patch add in the new DH, ECDH and ECDSA asymc test cases.

Arkadiusz Kusztal (3):
app/test: add diffie-hellman test cases
app/test: add ecdh test cases
app/test: add ecdsa kat sessionless tests

 app/test/test_cryptodev_asym.c              | 839 ++++++++++++++++++++
 app/test/test_cryptodev_dh_test_vectors.h   | 318 ++++++++
 app/test/test_cryptodev_ecdh_test_vectors.h | 144 ++++
 3 files changed, 1301 insertions(+)
 create mode 100644 app/test/test_cryptodev_ecdh_test_vectors.h

--
2.17.1

author
#4 lore raw

PATCH v2 1/3 app/test: add diffie-hellman test cases

From: Arkadiusz Kusztal <[email protected]>

Added Diffie-Hellman tests. This tests adding possibility to check correctness of particular DH phases, which is not possible with current tests cases.

---
 app/test/test_cryptodev_asym.c            | 264 ++++++++++++++++++
 app/test/test_cryptodev_dh_test_vectors.h | 318 ++++++++++++++++++++++
 2 files changed, 582 insertions(+)
app/test/test_cryptodev_asym.c +264 -0
index 5b16dcab56..b9034b637a 100644
@@ -64,6 +64,39 @@ static uint32_t test_index;
 
 static struct crypto_testsuite_params_asym testsuite_params = { NULL };
 
+static void
+test_crypto_rand(int len, uint8_t *buffer)
+{
+	int i;
+
+	for (i = 0; i < len; ++i)
+		buffer[i] = (uint8_t)(rand() % ((uint8_t)-1)) | 1;
+}
+
+static int
+process_crypto_request(uint8_t dev_id, struct rte_crypto_op **op,
+				struct rte_crypto_op **result_op)
+{
+	/* 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");
+		return -1;
+	}
+
+	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");
+		return -1;
+	}
+	return 0;
+}
+
 static int
 queue_ops_rsa_sign_verify(void *sess)
 {
@@ -809,6 +842,8 @@ testsuite_setup(void)
 	test_vector.size = 0;
 	load_test_vectors();
 
+	srand(time(NULL));
+
 	/* Device, op pool and session configuration for asymmetric crypto. 8< */
 	ts_params->op_mpool = rte_crypto_op_pool_create(
 			"CRYPTO_ASYM_OP_POOL",
@@ -2196,6 +2231,219 @@ test_ecpm_all_curve(void)
 	return overall_status;
 }
 
+static void *
+dh_alice_bob_set_session(uint8_t dev_id,
+	struct rte_crypto_op *op,
+	const struct test_dh_group *group)
+{
+	struct rte_crypto_asym_xform xform = { };
+	void *sess = NULL;
+	int ret = 0;
+
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_DH;
+	xform.dh.g.data = group->g.data;
+	xform.dh.g.length = group->g.bytesize;
+	xform.dh.p.data = group->p.data;
+	xform.dh.p.length = group->p.bytesize;
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform,
+			testsuite_params.session_mpool, &sess);
+	if (ret)
+		return NULL;
+
+	rte_crypto_op_attach_asym_session(op, sess);
+	return sess;
+}
+
+static void
+dh_alice_bob_gen_x(const struct test_dh_group *group,
+	uint8_t *private_data)
+{
+	test_crypto_rand(group->priv_ff_size, private_data);
+	if (private_data[0] > group->p.data[0])
+		private_data[0] = group->p.data[0] - 1;
+}
+
+static int
+dh_alice_bob_gen_y(struct rte_crypto_op *op,
+	const char *name,
+	const uint8_t dev_id,
+	uint8_t *y,
+	uint8_t *x,
+	const int ff_size)
+{
+	struct rte_crypto_op *result_op;
+	int ret = 0;
+
+	op->asym->dh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE;
+	op->asym->dh.pub_key.data = y;
+	op->asym->dh.priv_key.data = x;
+	op->asym->dh.priv_key.length = ff_size;
+
+	ret = process_crypto_request(dev_id, &op, &result_op);
+	TEST_ASSERT_SUCCESS(ret, "Failed to compute public key for %s", name);
+
+	return 0;
+}
+
+static int
+dh_alice_bob_shared_compute(uint8_t dev_id,
+	const char *name,
+	struct rte_crypto_op *op,
+	uint8_t *secret,
+	uint8_t *peer,
+	uint32_t peer_size)
+{
+	struct rte_crypto_op *result_op;
+	int ret = 0;
+
+	op->asym->dh.ke_type = RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
+	op->asym->dh.pub_key.data = peer;
+	op->asym->dh.pub_key.length = peer_size;
+	op->asym->dh.shared_secret.data = secret;
+
+	ret = process_crypto_request(dev_id, &op, &result_op);
+	TEST_ASSERT_SUCCESS(ret,
+		"Failed to compute shared secret for %s",
+		name);
+
+	return 0;
+}
+
+/* Diffie-Hellman test to verify the processed data */
+static int
+dh_alice_bob_verify(struct rte_crypto_op *alice_op,
+	struct rte_crypto_op *bob_op)
+{
+	int ret = 0;
+
+	/* Verify processed data */
+	ret = (alice_op->asym->dh.shared_secret.length ==
+			bob_op->asym->dh.shared_secret.length);
+	if (!ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Alice's and Bob's shared secret length do not match.");
+		return TEST_FAILED;
+	}
+	ret = memcmp(alice_op->asym->dh.shared_secret.data,
+		alice_op->asym->dh.shared_secret.data,
+		bob_op->asym->dh.shared_secret.length);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Alice's and Bob's shared secret do not match.");
+		return TEST_FAILED;
+	}
+	return TEST_SUCCESS;
+}
+
+static int
+test_dh_alice_and_bob(const void *test_data)
+{
+	uint8_t alice_x[TEST_DH_MOD_LEN] = { };
+	uint8_t alice_y[TEST_DH_MOD_LEN] = { };
+	uint8_t alice_secret[TEST_DH_MOD_LEN] = { };
+	uint8_t bob_x[TEST_DH_MOD_LEN] = { };
+	uint8_t bob_y[TEST_DH_MOD_LEN] = { };
+	uint8_t bob_secret[TEST_DH_MOD_LEN] = { };
+
+	const struct test_dh_group *group = test_data;
+	struct rte_crypto_op *alice_op = NULL;
+	struct rte_crypto_op *bob_op = NULL;
+	void *alice_session = NULL;
+	void *bob_session = NULL;
+
+	int ret = TEST_SUCCESS;
+	uint32_t alice_y_size = 0;
+	uint32_t bob_y_size = 0;
+	uint8_t dev_id = testsuite_params.valid_devs[0];
+
+	alice_op = rte_crypto_op_alloc(testsuite_params.op_mpool,
+			RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (alice_op == NULL) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Cannot allocate alice_op");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+	bob_op = rte_crypto_op_alloc(testsuite_params.op_mpool,
+			RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (bob_op == NULL) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Cannot allocate bob_op");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+
+	alice_session = dh_alice_bob_set_session(dev_id, alice_op,
+		group);
+	if (alice_session == NULL) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Cannot allocate alice_session");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+	bob_session = dh_alice_bob_set_session(dev_id, bob_op,
+		group);
+	if (bob_session == NULL) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Cannot allocate bob_session");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+
+	dh_alice_bob_gen_x(group, alice_x);
+	ret = dh_alice_bob_gen_y(alice_op, "Alice", dev_id,
+		alice_y, alice_x, group->priv_ff_size);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+		"Diffie-Hellman error\n");
+		goto error_exit;
+	}
+	alice_y_size = alice_op->asym->dh.pub_key.length;
+	dh_alice_bob_gen_x(group, bob_x);
+	ret = dh_alice_bob_gen_y(bob_op, "Bob", dev_id,
+		bob_y, bob_x, group->priv_ff_size);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+		"Diffie-Hellman error\n");
+		goto error_exit;
+	}
+	bob_y_size = bob_op->asym->dh.pub_key.length;
+
+	ret = dh_alice_bob_shared_compute(dev_id, "Alice", alice_op,
+		alice_secret, bob_y, bob_y_size);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+		"Diffie-Hellman error, error computing Alice's shared secret");
+		goto error_exit;
+	}
+
+	ret = dh_alice_bob_shared_compute(dev_id, "Bob", bob_op, bob_secret,
+		alice_y, alice_y_size);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+		"Diffie-Hellman error, error computing Bob's shared secret");
+		goto error_exit;
+	}
+
+	ret = dh_alice_bob_verify(alice_op, bob_op);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+		"Diffie-Hellman error");
+		goto error_exit;
+	}
+
+error_exit:
+	if (alice_session)
+		rte_cryptodev_asym_session_free(dev_id, alice_session);
+	if (bob_session)
+		rte_cryptodev_asym_session_free(dev_id, bob_session);
+	if (alice_op)
+		rte_crypto_op_free(alice_op);
+	if (bob_op != NULL)
+		rte_crypto_op_free(bob_op);
+	return ret;
+}
+
 static struct unit_test_suite cryptodev_openssl_asym_testsuite  = {
 	.suite_name = "Crypto Device OPENSSL ASYM Unit Test Suite",
 	.setup = testsuite_setup,
@@ -2215,6 +2463,22 @@ static struct unit_test_suite cryptodev_openssl_asym_testsuite  = {
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_mod_inv),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_mod_exp),
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_one_by_one),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Diffie-Hellman Alice and Bob group 14 ikev2 test",
+			ut_setup_asym, ut_teardown_asym,
+			test_dh_alice_and_bob, &test_dh_ikev2group_14),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Diffie-Hellman Alice and Bob group 15 ikev2 test",
+			ut_setup_asym, ut_teardown_asym,
+			test_dh_alice_and_bob, &test_dh_ikev2group_15),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Diffie-Hellman Alice and Bob group 16 ikev2 test",
+			ut_setup_asym, ut_teardown_asym,
+			test_dh_alice_and_bob, &test_dh_ikev2group_16),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Diffie-Hellman Alice and Bob group 24 ikev2 test",
+			ut_setup_asym, ut_teardown_asym,
+			test_dh_alice_and_bob, &test_dh_ikev2group_24),
 		TEST_CASES_END() /**< NULL terminate unit test array */
 	}
 };
app/test/test_cryptodev_dh_test_vectors.h +318 -0

328 lines not on this page · raw message

--
2.17.1

#5 lore raw

app/test/test_cryptodev_asym.c static uint32_t test_index; · line 68

Quoted 10 lines
app/test/test_cryptodev_asym.c +2 -0
index 5b16dcab56..b9034b637a 100644
@@ -64,6 +64,39 @@ static uint32_t test_index;
 
 static struct crypto_testsuite_params_asym testsuite_params = { NULL };
 
+static void
+test_crypto_rand(int len, uint8_t *buffer) {

Just a minor suggestion on params order here (below), for ease in readability. test_crypto_rand(uint8_t *buffer, int len)

app/test/test_cryptodev_asym.c static uint32_t test_index;

Quoted 4 lines
app/test/test_cryptodev_asym.c +4 -0
+	int i;
+
+	for (i = 0; i < len; ++i)
+		buffer[i] = (uint8_t)(rand() % ((uint8_t)-1)) | 1; }

rand() % UINT8_MAX easier ?
Also } in next line.

Quoted 62 lines
app/test/test_cryptodev_asym.c +27 -0
+
+static int
+process_crypto_request(uint8_t dev_id, struct rte_crypto_op **op,
+				struct rte_crypto_op **result_op)
+{
+	/* 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");
+		return -1;
+	}
+
+	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");
+		return -1;
+	}
+	return 0;
+}
+
 static int
 queue_ops_rsa_sign_verify(void *sess)
 {
@@ -809,6 +842,8 @@ testsuite_setup(void)
 	test_vector.size = 0;
 	load_test_vectors();
 
+	srand(time(NULL));
+
 	/* Device, op pool and session configuration for asymmetric crypto. 8<
 */
 	ts_params->op_mpool = rte_crypto_op_pool_create(
app/test/test_cryptodev_asym.c +20 -0
 			"CRYPTO_ASYM_OP_POOL",
@@ -2196,6 +2231,219 @@ test_ecpm_all_curve(void)
 	return overall_status;
 }
 
+static void *
+dh_alice_bob_set_session(uint8_t dev_id,
+	struct rte_crypto_op *op,
+	const struct test_dh_group *group)
+{
+	struct rte_crypto_asym_xform xform = { };
+	void *sess = NULL;
+	int ret = 0;
+
+	xform.xform_type = RTE_CRYPTO_ASYM_XFORM_DH;
+	xform.dh.g.data = group->g.data;
+	xform.dh.g.length = group->g.bytesize;
+	xform.dh.p.data = group->p.data;
+	xform.dh.p.length = group->p.bytesize;
+	ret = rte_cryptodev_asym_session_create(dev_id, &xform,
+			testsuite_params.session_mpool, &sess);
+	if (ret)
+		return NULL;
+
+	rte_crypto_op_attach_asym_session(op, sess);

app/test/test_cryptodev_asym.c test_ecpm_all_curve(void) · line 2263

Quoted 10 lines
app/test/test_cryptodev_asym.c +10 -0
+	return sess;
+}
+
+static void
+dh_alice_bob_gen_x(const struct test_dh_group *group,
+	uint8_t *private_data)
+{
+	test_crypto_rand(group->priv_ff_size, private_data);
+	if (private_data[0] > group->p.data[0])
+		private_data[0] = group->p.data[0] - 1; }

} in next line.

app/test/test_cryptodev_asym.c test_ecpm_all_curve(void) · line 2269

Quoted 5 lines
app/test/test_cryptodev_asym.c +5 -0
+
+static int
+dh_alice_bob_gen_y(struct rte_crypto_op *op,
+	const char *name,
+	const uint8_t dev_id,

Could this order be dev_id, name, op etc ??

Quoted 35 lines
app/test/test_cryptodev_asym.c +33 -0
+	uint8_t *y,
+	uint8_t *x,
+	const int ff_size)
+{
+	struct rte_crypto_op *result_op;
+	int ret = 0;
+
+	op->asym->dh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE;
+	op->asym->dh.pub_key.data = y;
+	op->asym->dh.priv_key.data = x;
+	op->asym->dh.priv_key.length = ff_size;
+
+	ret = process_crypto_request(dev_id, &op, &result_op);
+	TEST_ASSERT_SUCCESS(ret, "Failed to compute public key for %s",
 name);
+
+	return 0;
+}
+
+static int
+dh_alice_bob_shared_compute(uint8_t dev_id,
+	const char *name,
+	struct rte_crypto_op *op,
+	uint8_t *secret,
+	uint8_t *peer,
+	uint32_t peer_size)
+{
+	struct rte_crypto_op *result_op;
+	int ret = 0;
+
+	op->asym->dh.ke_type =
 RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
+	op->asym->dh.pub_key.data = peer;
+	op->asym->dh.pub_key.length = peer_size;
+	op->asym->dh.shared_secret.data = secret;

app/test/test_cryptodev_asym.c test_ecpm_all_curve(void)

Quoted 10 lines
app/test/test_cryptodev_asym.c +10 -0
+
+	ret = process_crypto_request(dev_id, &op, &result_op);
+	TEST_ASSERT_SUCCESS(ret,
+		"Failed to compute shared secret for %s",
+		name);
+
+	return 0;
+}
+
+/* Diffie-Hellman test to verify the processed data */ static int

Patch format incorrect ?

app/test/test_cryptodev_asym.c test_ecpm_all_curve(void) · line 2321

Quoted 8 lines
app/test/test_cryptodev_asym.c +8 -0
+dh_alice_bob_verify(struct rte_crypto_op *alice_op,
+	struct rte_crypto_op *bob_op)
+{
+	int ret = 0;
+
+	/* Verify processed data */
+	ret = (alice_op->asym->dh.shared_secret.length ==
+			bob_op->asym->dh.shared_secret.length);

Is 0 length allowed ?

Quoted 16 lines
app/test/test_cryptodev_asym.c +16 -0
+	if (!ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Alice's and Bob's shared secret length do not match.");
+		return TEST_FAILED;
+	}
+	ret = memcmp(alice_op->asym->dh.shared_secret.data,
+		alice_op->asym->dh.shared_secret.data,
+		bob_op->asym->dh.shared_secret.length);
+	if (ret) {
+		RTE_LOG(ERR, USER1, "line %u FAILED: %s", __LINE__,
+			"Alice's and Bob's shared secret do not match.");
+		return TEST_FAILED;
+	}
+	return TEST_SUCCESS;
+}
+

<cut..>

Quoted 6 lines
app/test/test_cryptodev_asym.c +6 -0
+static const struct test_dh_group test_dh_ikev2group_24 = {
+	.id = 0,
+	/*
+	 * Officially 24, ikev2
+	 */
+	.g = {

app/test/test_cryptodev_asym.c

Quoted 10 lines
app/test/test_cryptodev_asym.c +10 -0
+		.data = test_dh_ikev2group24_g,
+		.bytesize = sizeof(test_dh_ikev2group24_g),
+	},
+	.p = {
+		.data = test_dh_ikev2group24_p,
+		.bytesize = sizeof(test_dh_ikev2group24_p),
+	},
+	.priv_ff_size = 32,
+};
+

Below are suppose to be 2/3 patch right ?

Quoted 17 lines
app/test/test_cryptodev_asym.c +14 -1
+static const struct test_dh_group test_ecdh_secp256r1 = {
+	.id = RTE_CRYPTO_EC_GROUP_SECP256R1,
+	.priv_ff_size = 32,
+};
+static const struct test_dh_group test_ecdh_secp384r1 = {
+	.id = RTE_CRYPTO_EC_GROUP_SECP384R1,
+	.priv_ff_size = 48,
+};
+static const struct test_dh_group test_ecdh_secp521r1 = {
+	.id = RTE_CRYPTO_EC_GROUP_SECP521R1,
+	.priv_ff_size = 64,
+};
+
+
 #endif /* TEST_CRYPTODEV_DH_TEST_VECTORS_H__ */
--

2.17.1

author
#6 lore raw

PATCH v2 2/3 app/test: add ecdh test cases

In reply to Kai Ji

From: Arkadiusz Kusztal <[email protected]>

Added Elliptic-Curve Diffie Hellman test cases.

---
 app/test/test_cryptodev_asym.c              | 366 ++++++++++++++++++++
 app/test/test_cryptodev_ecdh_test_vectors.h | 144 ++++++++
 2 files changed, 510 insertions(+)
 create mode 100644 app/test/test_cryptodev_ecdh_test_vectors.h
app/test/test_cryptodev_asym.c +366 -0
index b9034b637a..fd5c5788b2 100644
@@ -19,6 +19,7 @@
 #include "test_cryptodev_dsa_test_vectors.h"
 #include "test_cryptodev_ecdsa_test_vectors.h"
 #include "test_cryptodev_ecpm_test_vectors.h"
+#include "test_cryptodev_ecdh_test_vectors.h"
 #include "test_cryptodev_mod_test_vectors.h"
 #include "test_cryptodev_rsa_test_vectors.h"
 #include "test_cryptodev_asym_util.h"
@@ -2231,6 +2232,367 @@ test_ecpm_all_curve(void)
 	return overall_status;
 }
 
+static int
+test_ecdh(const void *input_vector)
+{
+	const struct ECDH_test_vector *test_vector = input_vector;
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	struct rte_crypto_asym_xform xform = {
+		.next = NULL,
+		.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH,
+		.ec.curve_id = test_vector->curve_id
+	};
+	const uint8_t dev_id = ts_params->valid_devs[0];
+	uint8_t alice_x[test_vector->curve_bytesize],
+		alice_y[test_vector->curve_bytesize];
+	uint8_t bob_x[test_vector->curve_bytesize],
+		bob_y[test_vector->curve_bytesize];
+	uint8_t alice_xZ[test_vector->curve_bytesize],
+		alice_yZ[test_vector->curve_bytesize];
+	uint8_t bob_xZ[test_vector->curve_bytesize],
+		bob_yZ[test_vector->curve_bytesize];
+	struct rte_crypto_op *alice_op = NULL, *bob_op = NULL;
+
+	alice_op = rte_crypto_op_alloc(op_mpool,
+		RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (alice_op == NULL) {
+		RTE_LOG(ERR, USER1, "Error when allocationg alice_op\n");
+		goto error;
+	}
+	bob_op = rte_crypto_op_alloc(op_mpool,
+		RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
+	if (bob_op == NULL) {
+		RTE_LOG(ERR, USER1, "Error when allocationg bob_op\n");
+		goto error;
+	}
+
+	/*
+	 * STEP 1a:
+	 * Generate Alice public key
+	 */
+
+	alice_op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
+	alice_op->asym->xform = &xform;
+	alice_op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE;
+	alice_op->asym->ecdh.priv_key = test_vector->alice_d;
+	alice_op->asym->ecdh.pub_key.x.data = alice_x;
+	alice_op->asym->ecdh.pub_key.y.data = alice_y;
+
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &alice_op, 1) != 1) {
+		RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
+		goto error;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &alice_op, 1) == 0)
+		rte_pause();
+
+	if (alice_op->asym->ecdh.pub_key.x.length !=
+			test_vector->alice_q.x.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice public key length (X coeff)\n");
+		RTE_LOG(ERR, USER1,
+			"Received length = %d Expected length = %d\n",
+			(unsigned int)test_vector->alice_q.x.length,
+			(unsigned int)alice_op->asym->ecdh.pub_key.x.length
+		);
+		goto error;
+	}
+	if (alice_op->asym->ecdh.pub_key.y.length !=
+			test_vector->alice_q.y.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice public key length (Y coeff)\n");
+		RTE_LOG(ERR, USER1,
+			"Received length = %d Expected length = %d\n",
+			(unsigned int)test_vector->alice_q.y.length,
+			(unsigned int)alice_op->asym->ecdh.pub_key.y.length
+		);
+		goto error;
+	}
+	if (memcmp(alice_op->asym->ecdh.pub_key.x.data,
+			test_vector->alice_q.x.data,
+			test_vector->alice_q.x.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice public key X\n");
+		debug_hexdump(stdout, "Generated PublicKey x (Alice):",
+			alice_op->asym->ecdh.pub_key.x.data,
+			alice_op->asym->ecdh.pub_key.x.length);
+		goto error;
+	}
+	if (memcmp(alice_op->asym->ecdh.pub_key.y.data,
+			test_vector->alice_q.y.data,
+			test_vector->alice_q.y.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice public key Y\n");
+		debug_hexdump(stdout, "Generated PublicKey y (Alice):",
+				alice_op->asym->ecdh.pub_key.y.data,
+				alice_op->asym->ecdh.pub_key.y.length);
+		goto error;
+	}
+
+
+	/*
+	 * STEP 1b:
+	 * Generate Bob public key
+	 */
+
+	bob_op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
+	bob_op->asym->xform = &xform;
+	bob_op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE;
+	bob_op->asym->ecdh.priv_key = test_vector->bob_d;
+	bob_op->asym->ecdh.pub_key.x.data = bob_x;
+	bob_op->asym->ecdh.pub_key.y.data = bob_y;
+
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &bob_op, 1) != 1) {
+		RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
+		goto error;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &bob_op, 1) == 0)
+		rte_pause();
+
+	if (bob_op->asym->ecdh.pub_key.x.length !=
+			test_vector->bob_q.x.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's public key length (X coeff)\n");
+		RTE_LOG(ERR, USER1,
+			"Received length = %d Expected length = %d\n",
+			(unsigned int)test_vector->bob_q.x.length,
+			(unsigned int)bob_op->asym->ecdh.pub_key.x.length
+		);
+		goto error;
+	}
+	if (bob_op->asym->ecdh.pub_key.y.length !=
+			test_vector->bob_q.y.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's public key length (Y coeff)\n");
+		RTE_LOG(ERR, USER1,
+			"Received length = %d Expected length = %d\n",
+			(unsigned int)test_vector->bob_q.y.length,
+			(unsigned int)bob_op->asym->ecdh.pub_key.y.length
+		);
+		goto error;
+	}
+	if (memcmp(bob_op->asym->ecdh.pub_key.x.data,
+			test_vector->bob_q.x.data,
+			test_vector->bob_q.x.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's public key X\n");
+		debug_hexdump(stdout,
+			"Generated PublicKey x (bob):",
+			bob_op->asym->ecdh.pub_key.x.data,
+			bob_op->asym->ecdh.pub_key.x.length
+		);
+		goto error;
+	}
+	if (memcmp(bob_op->asym->ecdh.pub_key.y.data,
+			test_vector->bob_q.y.data,
+			test_vector->bob_q.y.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's public key Y\n");
+		debug_hexdump(stdout,
+			"Generated PublicKey y (bob):",
+			bob_op->asym->ecdh.pub_key.y.data,
+			bob_op->asym->ecdh.pub_key.y.length
+		);
+		goto error;
+	}
+
+	/*
+	 * STEP 1.5a:
+	 * Alice verifies Bob's public key, reusing op, other parameters
+	 * then already set, only what Alice needs to do is to set public
+	 * key of Bob and change key exchange type to VERIFY.
+	 */
+
+	alice_op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY;
+	alice_op->asym->ecdh.pub_key.x.data = bob_x;
+	alice_op->asym->ecdh.pub_key.x.length =
+		bob_op->asym->ecdh.pub_key.x.length;
+	alice_op->asym->ecdh.pub_key.y.data = bob_y;
+	alice_op->asym->ecdh.pub_key.y.length =
+		bob_op->asym->ecdh.pub_key.y.length;
+
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &alice_op, 1) != 1) {
+		RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
+		goto error;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &alice_op, 1) == 0)
+		rte_pause();
+
+	if (alice_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+			"line %u FAILED: %s", __LINE__,
+			"Failed to verify Bob's public key, it does not belongs to curve points\n"
+		);
+		return TEST_FAILED;
+	}
+
+	/*
+	 * STEP 1.5b:
+	 * Bob verifies Alice's public key, reusing op, other parameters
+	 * then already set, only what Bob needs to do is to set public
+	 * key of Alice and change key exchange type to VERIFY.
+	 */
+
+	bob_op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY;
+	bob_op->asym->ecdh.pub_key.x.data = alice_x;
+	bob_op->asym->ecdh.pub_key.x.length =
+		alice_op->asym->ecdh.pub_key.x.length;
+	bob_op->asym->ecdh.pub_key.y.data = alice_y;
+	bob_op->asym->ecdh.pub_key.y.length =
+		alice_op->asym->ecdh.pub_key.y.length;
+
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &bob_op, 1) != 1) {
+		RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
+		goto error;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &bob_op, 1) == 0)
+		rte_pause();
+
+	if (bob_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+			"line %u FAILED: %s", __LINE__,
+			"Failed to verify Alice's public key it does not belongs to curve points\n"
+		);
+		goto error;
+	}
+
+	/*
+	 * STEP 2a:
+	 * Alice generates shared secret Z, since Bob's public key was already
+	 * set when doing verification only key exchange type need to be changed,
+	 * and setting place where Z will be copied by the PMD.
+	 */
+
+	alice_op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
+	alice_op->asym->ecdh.shared_secret.x.data = alice_xZ;
+	alice_op->asym->ecdh.shared_secret.y.data = alice_yZ;
+
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &alice_op, 1) != 1) {
+		RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
+		goto error;
+	}
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &alice_op, 1) == 0)
+		rte_pause();
+
+	if (alice_op->asym->ecdh.shared_secret.x.length !=
+			test_vector->Z.x.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice's shared secret length X\n");
+		RTE_LOG(ERR, USER1,
+			"Received = %d Expected = %d\n",
+			(unsigned int)alice_op->asym->ecdh.shared_secret.x.length,
+			(unsigned int)test_vector->Z.x.length
+		);
+		goto error;
+	}
+	if (alice_op->asym->ecdh.shared_secret.y.length != test_vector->Z.y.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice's shared secret length Y\n");
+		RTE_LOG(ERR, USER1,
+			"Received = %d Expected = %d\n",
+			(unsigned int)alice_op->asym->ecdh.shared_secret.y.length,
+			(unsigned int)test_vector->Z.y.length
+		);
+		goto error;
+	}
+
+	if (memcmp(alice_op->asym->ecdh.shared_secret.x.data,
+			test_vector->Z.x.data,
+			test_vector->Z.x.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice's shared secret X\n");
+		debug_hexdump(stdout,
+			"Generated SharedSecret x (Alice):",
+			alice_op->asym->ecdh.shared_secret.x.data,
+			alice_op->asym->ecdh.shared_secret.x.length
+		);
+		goto error;
+	}
+	if (memcmp(alice_op->asym->ecdh.shared_secret.y.data,
+			test_vector->Z.y.data,
+			test_vector->Z.y.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Alice's shared secret Y\n");
+		debug_hexdump(stdout,
+			"Generated SharedSecret y (Alice):",
+			alice_op->asym->ecdh.shared_secret.y.data,
+			alice_op->asym->ecdh.shared_secret.y.length
+		);
+		goto error;
+	}
+
+	/*
+	 * STEP 2b:
+	 * Bob generates shared secret Z, since Alice's public key was already
+	 * set when doing verification only, key exchange type needs to be
+	 * changed, and setting place where Z will be copied by the PMD.
+	 */
+
+	bob_op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
+	bob_op->asym->ecdh.shared_secret.x.data = bob_xZ;
+	bob_op->asym->ecdh.shared_secret.y.data = bob_yZ;
+
+	if (rte_cryptodev_enqueue_burst(dev_id, 0, &bob_op, 1) != 1) {
+		RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
+		goto error;
+	}
+
+	while (rte_cryptodev_dequeue_burst(dev_id, 0, &bob_op, 1) == 0)
+		rte_pause();
+
+	if (bob_op->asym->ecdh.shared_secret.x.length != test_vector->Z.x.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's shared secret length X\n");
+		RTE_LOG(ERR, USER1,
+			"Received = %d Expected = %d\n",
+			(unsigned int)bob_op->asym->ecdh.shared_secret.x.length,
+			(unsigned int)test_vector->Z.x.length
+		);
+		goto error;
+	}
+	if (bob_op->asym->ecdh.shared_secret.y.length != test_vector->Z.y.length) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's shared secret length Y\n");
+		RTE_LOG(ERR, USER1,
+			"Received = %d Expected = %d\n",
+			(unsigned int)bob_op->asym->ecdh.shared_secret.y.length,
+			(unsigned int)test_vector->Z.y.length
+		);
+		goto error;
+	}
+
+	if (memcmp(bob_op->asym->ecdh.shared_secret.x.data,
+			test_vector->Z.x.data,
+			test_vector->Z.x.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's shared secret X\n");
+		debug_hexdump(stdout,
+			"Generated SharedSecret x (Bob):",
+			bob_op->asym->ecdh.shared_secret.x.data,
+			bob_op->asym->ecdh.shared_secret.x.length
+		);
+		goto error;
+	}
+	if (memcmp(bob_op->asym->ecdh.shared_secret.y.data,
+			test_vector->Z.y.data,
+			test_vector->Z.y.length)
+	) {
+		RTE_LOG(ERR, USER1, "Incorrect Bob's shared secret Y\n");
+		debug_hexdump(stdout,
+			"Generated SharedSecret y (Bob):",
+			bob_op->asym->ecdh.shared_secret.y.data,
+			bob_op->asym->ecdh.shared_secret.y.length
+		);
+		goto error;
+	}
+
+	/* Both shared secret where correctly verified -> Test passed */
+
+	rte_crypto_op_free(alice_op);
+	rte_crypto_op_free(bob_op);
+	return TEST_SUCCESS;
+error:
+	rte_crypto_op_free(alice_op);
+	rte_crypto_op_free(bob_op);
+	return TEST_FAILED;
+}
+
 static void *
 dh_alice_bob_set_session(uint8_t dev_id,
 	struct rte_crypto_op *op,
@@ -2489,6 +2851,10 @@ static struct unit_test_suite cryptodev_qat_asym_testsuite  = {
 	.teardown = testsuite_teardown,
 	.unit_test_cases = {
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_one_by_one),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Test - ECDH secp256r1, RFC 5114 256",
+			ut_setup_asym, ut_teardown_asym,
+			test_ecdh, &rfc5114_secp256r1),
 		TEST_CASES_END() /**< NULL terminate unit test array */
 	}
 };
app/test/test_cryptodev_ecdh_test_vectors.h new file +144 -0

144 lines not on this page · raw message

--
2.17.1

author
#7 lore raw

PATCH v2 3/3 app/test: add ecdsa kat sessionless tests

In reply to Kai Ji

From: Arkadiusz Kusztal <[email protected]>

Added sessionless ECDSA known answer test cases to QAT asym crypto testsuite.

---
 app/test/test_cryptodev_asym.c | 209 +++++++++++++++++++++++++++++++++
 1 file changed, 209 insertions(+)
app/test/test_cryptodev_asym.c +209 -0
index fd5c5788b2..cc603ae805 100644
@@ -2232,6 +2232,191 @@ test_ecpm_all_curve(void)
 	return overall_status;
 }
 
+/*
+ * ECDSA Signature generation function
+ */
+static int
+test_ecdsa_sign(const void *input_vector)
+{
+	const struct crypto_testsuite_ecdsa_params *test_vector = input_vector;
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	struct rte_crypto_op *op;
+	struct rte_crypto_op *result_op = NULL;
+	struct rte_crypto_asym_xform xform = {
+		.next = NULL,
+		.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDSA,
+		.ec.curve_id = test_vector->curve
+	};
+	uint8_t R[test_vector->sign_r.length];
+	uint8_t S[test_vector->sign_s.length];
+	const uint8_t dev_id = ts_params->valid_devs[0];
+	int ret = TEST_SUCCESS;
+
+	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");
+		return TEST_FAILED;
+	}
+	/*
+	 * Set input data
+	 * - Sessionless operation, no session will be created. User is responsible
+	 * for taking care of xform lifetime -> in case of ECDSA no keys are stored
+	 * in xform so it is not security crucial to clear data, although it is
+	 * recommended to clear xform anyway.
+	 * - Generation of signature pair R, S.
+	 * - Message is a digest of message to be signed.
+	 * - k is a random integer in interval (1, n - 1)
+	 * - pkey is a private key
+	 */
+	op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
+	op->asym->xform = &xform;
+	op->asym->ecdsa.op_type = RTE_CRYPTO_ASYM_OP_SIGN;
+	op->asym->ecdsa.message.data = test_vector->digest.data;
+	op->asym->ecdsa.message.length = test_vector->digest.length;
+	op->asym->ecdsa.k.data = test_vector->scalar.data;
+	op->asym->ecdsa.k.length = test_vector->scalar.length;
+	op->asym->ecdsa.pkey.data = test_vector->pkey.data;
+	op->asym->ecdsa.pkey.length = test_vector->pkey.length;
+
+	/*
+	 * Set output data
+	 * Signature pair R, S. Positive integers.
+	 */
+	op->asym->ecdsa.r.data = R;
+	op->asym->ecdsa.s.data = S;
+
+	/* Sending packet for signature generation */
+	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");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+	/* Waiting for packet to be dequeued */
+	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");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"ECDSA generation failed.\n");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+
+	/* Generated signature R, S */
+	debug_hexdump(stdout, "R:", result_op->asym->ecdsa.r.data,
+			result_op->asym->ecdsa.r.length);
+	debug_hexdump(stdout, "S:", result_op->asym->ecdsa.s.data,
+			result_op->asym->ecdsa.s.length);
+
+error_exit:
+	rte_crypto_op_free(op);
+	return ret;
+}
+
+/*
+ * ECDSA Signature verification function
+ */
+static int
+test_ecdsa_verify(const void *input_vector)
+{
+	const struct crypto_testsuite_ecdsa_params *test_vector = input_vector;
+	struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
+	struct rte_mempool *op_mpool = ts_params->op_mpool;
+	struct rte_crypto_op *op;
+	struct rte_crypto_op *result_op = NULL;
+	struct rte_crypto_asym_xform xform = {
+		.next = NULL,
+		.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDSA,
+		.ec.curve_id = test_vector->curve
+	};
+	const uint8_t dev_id = ts_params->valid_devs[0];
+	int ret = TEST_SUCCESS;
+
+	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");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+	/*
+	 * Set input data
+	 * - Sessionless operation, no session will be created. User is responsible
+	 * for taking care of xform lifetime -> in case of ECDSA no keys are stored
+	 * in xform so it is not security crucial to clear data, although it is
+	 * recommended to clear xform anyway.
+	 * - Verification of signature pair R, S.
+	 * - Message is a digest of message that was signed.
+	 * - q is a public key
+	 */
+	op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
+	op->asym->xform = &xform;
+	op->asym->ecdsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
+	op->asym->ecdsa.message.data = test_vector->digest.data;
+	op->asym->ecdsa.message.length = test_vector->digest.length;
+	op->asym->ecdsa.q.x.data = test_vector->pubkey_qx.data;
+	op->asym->ecdsa.q.x.length = test_vector->pubkey_qx.length;
+	op->asym->ecdsa.q.y.data = test_vector->pubkey_qy.data;
+	op->asym->ecdsa.q.y.length = test_vector->pubkey_qx.length;
+	op->asym->ecdsa.r.data =  test_vector->sign_r.data;
+	op->asym->ecdsa.r.length =  test_vector->sign_r.length;
+	op->asym->ecdsa.s.data =  test_vector->sign_s.data;
+	op->asym->ecdsa.s.length =  test_vector->sign_s.length;
+
+	/* Sending packet for signature generation */
+	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");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+	/* Waiting for packet to be dequeued */
+	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");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+
+	/*
+	 * If result_op->status equals RTE_CRYPTO_OP_STATUS_SUCCESS
+	 * signature was verified correctly
+	 */
+	if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
+		RTE_LOG(ERR, USER1,
+				"line %u FAILED: %s", __LINE__,
+				"ECDSA verify failed.\n");
+		ret = TEST_FAILED;
+		goto error_exit;
+	}
+
+error_exit:
+	rte_crypto_op_free(op);
+	return ret;
+}
+
 static int
 test_ecdh(const void *input_vector)
 {
@@ -2851,6 +3036,30 @@ static struct unit_test_suite cryptodev_qat_asym_testsuite  = {
 	.teardown = testsuite_teardown,
 	.unit_test_cases = {
 		TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_one_by_one),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Test - ECDSA secp256r1 signature generation",
+			ut_setup_asym, ut_teardown_asym,
+			test_ecdsa_sign, &ecdsa_param_secp256r1),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Test - ECDSA secp256r1 signature verification",
+			ut_setup_asym, ut_teardown_asym,
+			test_ecdsa_verify, &ecdsa_param_secp256r1),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Test - ECDSA secp384r1 signature generation",
+			ut_setup_asym, ut_teardown_asym,
+			test_ecdsa_sign, &ecdsa_param_secp384r1),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Test - ECDSA secp384r1 signature verification",
+			ut_setup_asym, ut_teardown_asym,
+			test_ecdsa_verify, &ecdsa_param_secp384r1),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Test - ECDSA secp521r1 signature generation",
+			ut_setup_asym, ut_teardown_asym,
+			test_ecdsa_sign, &ecdsa_param_secp521r1),
+		TEST_CASE_NAMED_WITH_DATA(
+			"Test - ECDSA secp521r1 signature verification",
+			ut_setup_asym, ut_teardown_asym,
+			test_ecdsa_verify, &ecdsa_param_secp521r1),
 		TEST_CASE_NAMED_WITH_DATA(
 			"Test - ECDH secp256r1, RFC 5114 256",
 			ut_setup_asym, ut_teardown_asym,

--
2.17.1