1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Cavium Networks
5 #include <rte_bus_vdev.h>
6 #include <rte_common.h>
7 #include <rte_hexdump.h>
9 #include <rte_malloc.h>
10 #include <rte_memcpy.h>
11 #include <rte_pause.h>
13 #include <rte_cryptodev.h>
14 #include <rte_cryptodev_pmd.h>
15 #include <rte_crypto.h>
17 #include "test_cryptodev.h"
18 #include "test_cryptodev_dh_test_vectors.h"
19 #include "test_cryptodev_dsa_test_vectors.h"
20 #include "test_cryptodev_mod_test_vectors.h"
21 #include "test_cryptodev_rsa_test_vectors.h"
22 #include "test_cryptodev_asym_util.h"
25 #define TEST_NUM_BUFS 10
26 #define TEST_NUM_SESSIONS 4
28 static int gbl_driver_id;
29 struct crypto_testsuite_params {
30 struct rte_mempool *op_mpool;
31 struct rte_mempool *session_mpool;
32 struct rte_cryptodev_config conf;
33 struct rte_cryptodev_qp_conf qp_conf;
34 uint8_t valid_devs[RTE_CRYPTO_MAX_DEVS];
35 uint8_t valid_dev_count;
38 struct crypto_unittest_params {
39 struct rte_cryptodev_asym_session *sess;
40 struct rte_crypto_op *op;
43 static struct crypto_testsuite_params testsuite_params = { NULL };
46 test_rsa_sign_verify(void)
48 struct crypto_testsuite_params *ts_params = &testsuite_params;
49 struct rte_mempool *op_mpool = ts_params->op_mpool;
50 struct rte_mempool *sess_mpool = ts_params->session_mpool;
51 uint8_t dev_id = ts_params->valid_devs[0];
52 struct rte_crypto_asym_op *asym_op = NULL;
53 struct rte_crypto_op *op = NULL, *result_op = NULL;
54 struct rte_cryptodev_asym_session *sess = NULL;
55 int status = TEST_SUCCESS;
56 uint8_t output_buf[TEST_DATA_SIZE] = {0};
57 uint8_t input_buf[TEST_DATA_SIZE] = {0};
59 sess = rte_cryptodev_asym_session_create(sess_mpool);
62 RTE_LOG(ERR, USER1, "line %u "
63 "FAILED: %s", __LINE__,
64 "Session creation failed");
69 if (rte_cryptodev_asym_session_init(dev_id, sess, &rsa_xform,
73 __LINE__, "unabled to config sym session");
78 /* set up crypto op data structure */
79 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
84 "Failed to allocate asymmetric crypto "
91 /* Compute sign on the test vector */
92 asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_SIGN;
94 memcpy(input_buf, &rsaplaintext.data,
96 asym_op->rsa.message.data = input_buf;
97 asym_op->rsa.message.length = rsaplaintext.len;
98 asym_op->rsa.sign.data = output_buf;
99 asym_op->rsa.pad = RTE_CRYPTO_RSA_PKCS1_V1_5_BT1;
101 debug_hexdump(stdout, "message", asym_op->rsa.message.data,
102 asym_op->rsa.message.length);
104 /* attach asymmetric crypto session to crypto operations */
105 rte_crypto_op_attach_asym_session(op, sess);
107 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
109 /* Process crypto operation */
110 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
112 "line %u FAILED: %s",
113 __LINE__, "Error sending packet for operation");
114 status = TEST_FAILED;
118 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
121 if (result_op == NULL) {
123 "line %u FAILED: %s",
124 __LINE__, "Failed to process asym crypto op");
125 status = TEST_FAILED;
128 debug_hexdump(stdout, "signed message", asym_op->rsa.sign.data,
129 asym_op->rsa.sign.length);
130 asym_op = result_op->asym;
133 asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
134 asym_op->rsa.pad = RTE_CRYPTO_RSA_PKCS1_V1_5_BT2;
136 /* Process crypto operation */
137 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
139 "line %u FAILED: %s",
140 __LINE__, "Error sending packet for operation");
141 status = TEST_FAILED;
145 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
148 if (result_op == NULL) {
150 "line %u FAILED: %s",
151 __LINE__, "Failed to process asym crypto op");
152 status = TEST_FAILED;
155 status = TEST_SUCCESS;
156 if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
158 "line %u FAILED: %s",
159 __LINE__, "Failed to process asym crypto op");
160 status = TEST_FAILED;
167 rte_cryptodev_asym_session_clear(dev_id, sess);
168 rte_cryptodev_asym_session_free(sess);
172 rte_crypto_op_free(op);
174 TEST_ASSERT_EQUAL(status, 0, "Test failed");
180 test_rsa_enc_dec(void)
182 struct crypto_testsuite_params *ts_params = &testsuite_params;
183 struct rte_mempool *op_mpool = ts_params->op_mpool;
184 struct rte_mempool *sess_mpool = ts_params->session_mpool;
185 uint8_t dev_id = ts_params->valid_devs[0];
186 struct rte_crypto_asym_op *asym_op = NULL;
187 struct rte_crypto_op *op = NULL, *result_op = NULL;
188 struct rte_cryptodev_asym_session *sess = NULL;
189 int status = TEST_SUCCESS;
190 uint8_t input_buf[TEST_DATA_SIZE] = {0};
192 sess = rte_cryptodev_asym_session_create(sess_mpool);
195 RTE_LOG(ERR, USER1, "line %u "
196 "FAILED: %s", __LINE__,
197 "Session creation failed");
198 status = TEST_FAILED;
202 if (rte_cryptodev_asym_session_init(dev_id, sess, &rsa_xform,
205 "line %u FAILED: %s",
206 __LINE__, "unabled to config sym session");
207 status = TEST_FAILED;
211 /* set up crypto op data structure */
212 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
215 "line %u FAILED: %s",
217 "Failed to allocate asymmetric crypto "
219 status = TEST_FAILED;
224 /*Compute encryption on the test vector */
225 asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_ENCRYPT;
227 memcpy(input_buf, rsaplaintext.data,
229 asym_op->rsa.message.data = input_buf;
230 asym_op->rsa.message.length = rsaplaintext.len;
231 asym_op->rsa.pad = RTE_CRYPTO_RSA_PKCS1_V1_5_BT2;
233 debug_hexdump(stdout, "message", asym_op->rsa.message.data,
234 asym_op->rsa.message.length);
236 /* attach asymmetric crypto session to crypto operations */
237 rte_crypto_op_attach_asym_session(op, sess);
239 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
241 /* Process crypto operation */
242 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
244 "line %u FAILED: %s",
245 __LINE__, "Error sending packet for operation");
246 status = TEST_FAILED;
250 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
253 if (result_op == NULL) {
255 "line %u FAILED: %s",
256 __LINE__, "Failed to process asym crypto op");
257 status = TEST_FAILED;
260 debug_hexdump(stdout, "encrypted message", asym_op->rsa.message.data,
261 asym_op->rsa.message.length);
262 /* Use the resulted output as decryption Input vector*/
263 asym_op = result_op->asym;
264 asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_DECRYPT;
265 asym_op->rsa.pad = RTE_CRYPTO_RSA_PKCS1_V1_5_BT1;
267 /* Process crypto operation */
268 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
270 "line %u FAILED: %s",
271 __LINE__, "Error sending packet for operation");
272 status = TEST_FAILED;
276 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
279 if (result_op == NULL) {
281 "line %u FAILED: %s",
282 __LINE__, "Failed to process asym crypto op");
283 status = TEST_FAILED;
286 status = TEST_SUCCESS;
288 ret = rsa_verify(&rsaplaintext, result_op);
290 status = TEST_FAILED;
295 rte_cryptodev_asym_session_clear(dev_id, sess);
296 rte_cryptodev_asym_session_free(sess);
300 rte_crypto_op_free(op);
302 TEST_ASSERT_EQUAL(status, 0, "Test failed");
308 testsuite_setup(void)
310 struct crypto_testsuite_params *ts_params = &testsuite_params;
311 struct rte_cryptodev_info info;
312 uint32_t i = 0, nb_devs, dev_id;
316 memset(ts_params, 0, sizeof(*ts_params));
318 ts_params->op_mpool = rte_crypto_op_pool_create(
319 "CRYPTO_ASYM_OP_POOL",
320 RTE_CRYPTO_OP_TYPE_ASYMMETRIC,
324 if (ts_params->op_mpool == NULL) {
325 RTE_LOG(ERR, USER1, "Can't create ASYM_CRYPTO_OP_POOL\n");
329 /* Create an OPENSSL device if required */
330 if (gbl_driver_id == rte_cryptodev_driver_id_get(
331 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD))) {
332 nb_devs = rte_cryptodev_device_count_by_driver(
333 rte_cryptodev_driver_id_get(
334 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD)));
337 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD),
340 TEST_ASSERT(ret == 0, "Failed to create "
341 "instance of pmd : %s",
342 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
346 nb_devs = rte_cryptodev_count();
348 RTE_LOG(ERR, USER1, "No crypto devices found?\n");
352 /* Create list of valid crypto devs */
353 for (i = 0; i < nb_devs; i++) {
354 rte_cryptodev_info_get(i, &info);
355 if (info.driver_id == gbl_driver_id)
356 ts_params->valid_devs[ts_params->valid_dev_count++] = i;
359 if (ts_params->valid_dev_count < 1)
362 /* Set up all the qps on the first of the valid devices found */
364 dev_id = ts_params->valid_devs[0];
366 rte_cryptodev_info_get(dev_id, &info);
368 /* check if device support asymmetric, skip if not */
369 if (!(info.feature_flags &
370 RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)) {
371 RTE_LOG(ERR, USER1, "Device doesn't support asymmetric. "
376 /* configure device with num qp */
377 ts_params->conf.nb_queue_pairs = info.max_nb_queue_pairs;
378 ts_params->conf.socket_id = SOCKET_ID_ANY;
379 TEST_ASSERT_SUCCESS(rte_cryptodev_configure(dev_id,
381 "Failed to configure cryptodev %u with %u qps",
382 dev_id, ts_params->conf.nb_queue_pairs);
385 ts_params->qp_conf.nb_descriptors = DEFAULT_NUM_OPS_INFLIGHT;
386 ts_params->qp_conf.mp_session = ts_params->session_mpool;
387 ts_params->qp_conf.mp_session_private = ts_params->session_mpool;
388 for (qp_id = 0; qp_id < info.max_nb_queue_pairs; qp_id++) {
389 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
390 dev_id, qp_id, &ts_params->qp_conf,
391 rte_cryptodev_socket_id(dev_id)),
392 "Failed to setup queue pair %u on cryptodev %u ASYM",
396 /* setup asym session pool */
397 unsigned int session_size =
398 rte_cryptodev_asym_get_private_session_size(dev_id);
400 * Create mempool with TEST_NUM_SESSIONS * 2,
401 * to include the session headers
403 ts_params->session_mpool = rte_mempool_create(
405 TEST_NUM_SESSIONS * 2,
407 0, 0, NULL, NULL, NULL,
411 TEST_ASSERT_NOT_NULL(ts_params->session_mpool,
412 "session mempool allocation failed");
418 testsuite_teardown(void)
420 struct crypto_testsuite_params *ts_params = &testsuite_params;
422 if (ts_params->op_mpool != NULL) {
423 RTE_LOG(DEBUG, USER1, "CRYPTO_OP_POOL count %u\n",
424 rte_mempool_avail_count(ts_params->op_mpool));
427 /* Free session mempools */
428 if (ts_params->session_mpool != NULL) {
429 rte_mempool_free(ts_params->session_mpool);
430 ts_params->session_mpool = NULL;
437 struct crypto_testsuite_params *ts_params = &testsuite_params;
441 /* Reconfigure device to default parameters */
442 ts_params->conf.socket_id = SOCKET_ID_ANY;
444 TEST_ASSERT_SUCCESS(rte_cryptodev_configure(ts_params->valid_devs[0],
446 "Failed to configure cryptodev %u",
447 ts_params->valid_devs[0]);
449 for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs ; qp_id++) {
450 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
451 ts_params->valid_devs[0], qp_id,
453 rte_cryptodev_socket_id(ts_params->valid_devs[0])),
454 "Failed to setup queue pair %u on cryptodev %u",
455 qp_id, ts_params->valid_devs[0]);
458 rte_cryptodev_stats_reset(ts_params->valid_devs[0]);
460 /* Start the device */
461 TEST_ASSERT_SUCCESS(rte_cryptodev_start(ts_params->valid_devs[0]),
462 "Failed to start cryptodev %u",
463 ts_params->valid_devs[0]);
471 struct crypto_testsuite_params *ts_params = &testsuite_params;
472 struct rte_cryptodev_stats stats;
474 rte_cryptodev_stats_get(ts_params->valid_devs[0], &stats);
476 /* Stop the device */
477 rte_cryptodev_stop(ts_params->valid_devs[0]);
480 static inline void print_asym_capa(
481 const struct rte_cryptodev_asymmetric_xform_capability *capa)
485 printf("\nxform type: %s\n===================\n",
486 rte_crypto_asym_xform_strings[capa->xform_type]);
487 printf("operation supported -");
489 for (i = 0; i < RTE_CRYPTO_ASYM_OP_LIST_END; i++) {
490 /* check supported operations */
491 if (rte_cryptodev_asym_xform_capability_check_optype(capa, i))
493 rte_crypto_asym_op_strings[i]);
495 switch (capa->xform_type) {
496 case RTE_CRYPTO_ASYM_XFORM_RSA:
497 case RTE_CRYPTO_ASYM_XFORM_MODINV:
498 case RTE_CRYPTO_ASYM_XFORM_MODEX:
499 case RTE_CRYPTO_ASYM_XFORM_DH:
500 case RTE_CRYPTO_ASYM_XFORM_DSA:
501 printf(" modlen: min %d max %d increment %d\n",
504 capa->modlen.increment);
512 test_capability(void)
514 struct crypto_testsuite_params *ts_params = &testsuite_params;
515 uint8_t dev_id = ts_params->valid_devs[0];
516 struct rte_cryptodev_info dev_info;
517 const struct rte_cryptodev_capabilities *dev_capa;
519 struct rte_cryptodev_asym_capability_idx idx;
520 const struct rte_cryptodev_asymmetric_xform_capability *capa;
522 rte_cryptodev_info_get(dev_id, &dev_info);
523 if (!(dev_info.feature_flags &
524 RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)) {
526 "Device doesn't support asymmetric. Test Skipped\n");
530 /* print xform capability */
532 dev_info.capabilities[i].op != RTE_CRYPTO_OP_TYPE_UNDEFINED;
534 dev_capa = &(dev_info.capabilities[i]);
535 if (dev_info.capabilities[i].op ==
536 RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
537 idx.type = dev_capa->asym.xform_capa.xform_type;
539 capa = rte_cryptodev_asym_capability_get(dev_id,
541 rte_cryptodev_asym_capability_idx *) &idx);
542 print_asym_capa(capa);
549 test_dh_gen_shared_sec(struct rte_crypto_asym_xform *xfrm)
551 struct crypto_testsuite_params *ts_params = &testsuite_params;
552 struct rte_mempool *op_mpool = ts_params->op_mpool;
553 struct rte_mempool *sess_mpool = ts_params->session_mpool;
554 uint8_t dev_id = ts_params->valid_devs[0];
555 struct rte_crypto_asym_op *asym_op = NULL;
556 struct rte_crypto_op *op = NULL, *result_op = NULL;
557 struct rte_cryptodev_asym_session *sess = NULL;
558 int status = TEST_SUCCESS;
559 uint8_t output[TEST_DH_MOD_LEN];
560 struct rte_crypto_asym_xform xform = *xfrm;
561 uint8_t peer[] = "01234567890123456789012345678901234567890123456789";
563 sess = rte_cryptodev_asym_session_create(sess_mpool);
566 "line %u FAILED: %s", __LINE__,
567 "Session creation failed");
568 status = TEST_FAILED;
571 /* set up crypto op data structure */
572 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
575 "line %u FAILED: %s",
576 __LINE__, "Failed to allocate asymmetric crypto "
578 status = TEST_FAILED;
583 /* Setup a xform and op to generate private key only */
584 xform.dh.type = RTE_CRYPTO_ASYM_OP_SHARED_SECRET_COMPUTE;
586 asym_op->dh.priv_key.data = dh_test_params.priv_key.data;
587 asym_op->dh.priv_key.length = dh_test_params.priv_key.length;
588 asym_op->dh.pub_key.data = (uint8_t *)peer;
589 asym_op->dh.pub_key.length = sizeof(peer);
590 asym_op->dh.shared_secret.data = output;
591 asym_op->dh.shared_secret.length = sizeof(output);
593 if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
596 "line %u FAILED: %s",
597 __LINE__, "unabled to config sym session");
598 status = TEST_FAILED;
602 /* attach asymmetric crypto session to crypto operations */
603 rte_crypto_op_attach_asym_session(op, sess);
605 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
607 /* Process crypto operation */
608 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
610 "line %u FAILED: %s",
611 __LINE__, "Error sending packet for operation");
612 status = TEST_FAILED;
616 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
619 if (result_op == NULL) {
621 "line %u FAILED: %s",
622 __LINE__, "Failed to process asym crypto op");
623 status = TEST_FAILED;
627 debug_hexdump(stdout, "shared secret:",
628 asym_op->dh.shared_secret.data,
629 asym_op->dh.shared_secret.length);
633 rte_cryptodev_asym_session_clear(dev_id, sess);
634 rte_cryptodev_asym_session_free(sess);
637 rte_crypto_op_free(op);
642 test_dh_gen_priv_key(struct rte_crypto_asym_xform *xfrm)
644 struct crypto_testsuite_params *ts_params = &testsuite_params;
645 struct rte_mempool *op_mpool = ts_params->op_mpool;
646 struct rte_mempool *sess_mpool = ts_params->session_mpool;
647 uint8_t dev_id = ts_params->valid_devs[0];
648 struct rte_crypto_asym_op *asym_op = NULL;
649 struct rte_crypto_op *op = NULL, *result_op = NULL;
650 struct rte_cryptodev_asym_session *sess = NULL;
651 int status = TEST_SUCCESS;
652 uint8_t output[TEST_DH_MOD_LEN];
653 struct rte_crypto_asym_xform xform = *xfrm;
655 sess = rte_cryptodev_asym_session_create(sess_mpool);
658 "line %u FAILED: %s", __LINE__,
659 "Session creation failed");
660 status = TEST_FAILED;
663 /* set up crypto op data structure */
664 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
667 "line %u FAILED: %s",
668 __LINE__, "Failed to allocate asymmetric crypto "
670 status = TEST_FAILED;
675 /* Setup a xform and op to generate private key only */
676 xform.dh.type = RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE;
678 asym_op->dh.priv_key.data = output;
679 asym_op->dh.priv_key.length = sizeof(output);
681 if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
684 "line %u FAILED: %s",
685 __LINE__, "unabled to config sym session");
686 status = TEST_FAILED;
690 /* attach asymmetric crypto session to crypto operations */
691 rte_crypto_op_attach_asym_session(op, sess);
693 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
695 /* Process crypto operation */
696 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
698 "line %u FAILED: %s",
699 __LINE__, "Error sending packet for operation");
700 status = TEST_FAILED;
704 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
707 if (result_op == NULL) {
709 "line %u FAILED: %s",
710 __LINE__, "Failed to process asym crypto op");
711 status = TEST_FAILED;
715 debug_hexdump(stdout, "private key:",
716 asym_op->dh.priv_key.data,
717 asym_op->dh.priv_key.length);
722 rte_cryptodev_asym_session_clear(dev_id, sess);
723 rte_cryptodev_asym_session_free(sess);
726 rte_crypto_op_free(op);
733 test_dh_gen_pub_key(struct rte_crypto_asym_xform *xfrm)
735 struct crypto_testsuite_params *ts_params = &testsuite_params;
736 struct rte_mempool *op_mpool = ts_params->op_mpool;
737 struct rte_mempool *sess_mpool = ts_params->session_mpool;
738 uint8_t dev_id = ts_params->valid_devs[0];
739 struct rte_crypto_asym_op *asym_op = NULL;
740 struct rte_crypto_op *op = NULL, *result_op = NULL;
741 struct rte_cryptodev_asym_session *sess = NULL;
742 int status = TEST_SUCCESS;
743 uint8_t output[TEST_DH_MOD_LEN];
744 struct rte_crypto_asym_xform xform = *xfrm;
746 sess = rte_cryptodev_asym_session_create(sess_mpool);
749 "line %u FAILED: %s", __LINE__,
750 "Session creation failed");
751 status = TEST_FAILED;
754 /* set up crypto op data structure */
755 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
758 "line %u FAILED: %s",
759 __LINE__, "Failed to allocate asymmetric crypto "
761 status = TEST_FAILED;
765 /* Setup a xform chain to generate public key
766 * using test private key
769 xform.dh.type = RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE;
772 asym_op->dh.pub_key.data = output;
773 asym_op->dh.pub_key.length = sizeof(output);
774 /* load pre-defined private key */
775 asym_op->dh.priv_key.data = rte_malloc(NULL,
776 dh_test_params.priv_key.length,
778 asym_op->dh.priv_key = dh_test_params.priv_key;
780 if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
783 "line %u FAILED: %s",
784 __LINE__, "unabled to config sym session");
785 status = TEST_FAILED;
789 /* attach asymmetric crypto session to crypto operations */
790 rte_crypto_op_attach_asym_session(op, sess);
792 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
794 /* Process crypto operation */
795 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
797 "line %u FAILED: %s",
798 __LINE__, "Error sending packet for operation");
799 status = TEST_FAILED;
803 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
806 if (result_op == NULL) {
808 "line %u FAILED: %s",
809 __LINE__, "Failed to process asym crypto op");
810 status = TEST_FAILED;
814 debug_hexdump(stdout, "pub key:",
815 asym_op->dh.pub_key.data, asym_op->dh.pub_key.length);
817 debug_hexdump(stdout, "priv key:",
818 asym_op->dh.priv_key.data, asym_op->dh.priv_key.length);
822 rte_cryptodev_asym_session_clear(dev_id, sess);
823 rte_cryptodev_asym_session_free(sess);
826 rte_crypto_op_free(op);
832 test_dh_gen_kp(struct rte_crypto_asym_xform *xfrm)
834 struct crypto_testsuite_params *ts_params = &testsuite_params;
835 struct rte_mempool *op_mpool = ts_params->op_mpool;
836 struct rte_mempool *sess_mpool = ts_params->session_mpool;
837 uint8_t dev_id = ts_params->valid_devs[0];
838 struct rte_crypto_asym_op *asym_op = NULL;
839 struct rte_crypto_op *op = NULL, *result_op = NULL;
840 struct rte_cryptodev_asym_session *sess = NULL;
841 int status = TEST_SUCCESS;
842 uint8_t out_pub_key[TEST_DH_MOD_LEN];
843 uint8_t out_prv_key[TEST_DH_MOD_LEN];
844 struct rte_crypto_asym_xform pub_key_xform;
845 struct rte_crypto_asym_xform xform = *xfrm;
847 sess = rte_cryptodev_asym_session_create(sess_mpool);
850 "line %u FAILED: %s", __LINE__,
851 "Session creation failed");
852 status = TEST_FAILED;
856 /* set up crypto op data structure */
857 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
860 "line %u FAILED: %s",
861 __LINE__, "Failed to allocate asymmetric crypto "
863 status = TEST_FAILED;
867 /* Setup a xform chain to generate
868 * private key first followed by
870 */xform.dh.type = RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE;
871 pub_key_xform.xform_type = RTE_CRYPTO_ASYM_XFORM_DH;
872 pub_key_xform.dh.type = RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE;
873 xform.next = &pub_key_xform;
875 asym_op->dh.pub_key.data = out_pub_key;
876 asym_op->dh.pub_key.length = sizeof(out_pub_key);
877 asym_op->dh.priv_key.data = out_prv_key;
878 asym_op->dh.priv_key.length = sizeof(out_prv_key);
879 if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
882 "line %u FAILED: %s",
883 __LINE__, "unabled to config sym session");
884 status = TEST_FAILED;
888 /* attach asymmetric crypto session to crypto operations */
889 rte_crypto_op_attach_asym_session(op, sess);
891 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
893 /* Process crypto operation */
894 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
896 "line %u FAILED: %s",
897 __LINE__, "Error sending packet for operation");
898 status = TEST_FAILED;
902 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
905 if (result_op == NULL) {
907 "line %u FAILED: %s",
908 __LINE__, "Failed to process asym crypto op");
909 status = TEST_FAILED;
912 debug_hexdump(stdout, "priv key:",
913 out_prv_key, asym_op->dh.priv_key.length);
914 debug_hexdump(stdout, "pub key:",
915 out_pub_key, asym_op->dh.pub_key.length);
919 rte_cryptodev_asym_session_clear(dev_id, sess);
920 rte_cryptodev_asym_session_free(sess);
923 rte_crypto_op_free(op);
931 struct crypto_testsuite_params *ts_params = &testsuite_params;
932 struct rte_mempool *op_mpool = ts_params->op_mpool;
933 struct rte_mempool *sess_mpool = ts_params->session_mpool;
934 uint8_t dev_id = ts_params->valid_devs[0];
935 struct rte_crypto_asym_op *asym_op = NULL;
936 struct rte_crypto_op *op = NULL, *result_op = NULL;
937 struct rte_cryptodev_asym_session *sess = NULL;
938 int status = TEST_SUCCESS;
939 struct rte_cryptodev_asym_capability_idx cap_idx;
940 const struct rte_cryptodev_asymmetric_xform_capability *capability;
941 uint8_t input[TEST_DATA_SIZE] = {0};
943 uint8_t result[sizeof(mod_p)] = { 0 };
945 if (rte_cryptodev_asym_get_xform_enum(
946 &modinv_xform.xform_type, "modinv") < 0) {
948 "Invalid ASYNC algorithm specified\n");
952 cap_idx.type = modinv_xform.xform_type;
953 capability = rte_cryptodev_asym_capability_get(dev_id,
956 if (rte_cryptodev_asym_xform_capability_check_modlen(
958 modinv_xform.modinv.modulus.length)) {
960 "Invalid MODULOUS length specified\n");
964 sess = rte_cryptodev_asym_session_create(sess_mpool);
966 RTE_LOG(ERR, USER1, "line %u "
967 "FAILED: %s", __LINE__,
968 "Session creation failed");
969 status = TEST_FAILED;
973 if (rte_cryptodev_asym_session_init(dev_id, sess, &modinv_xform,
976 "line %u FAILED: %s",
977 __LINE__, "unabled to config sym session");
978 status = TEST_FAILED;
982 /* generate crypto op data structure */
983 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
986 "line %u FAILED: %s",
987 __LINE__, "Failed to allocate asymmetric crypto "
989 status = TEST_FAILED;
994 memcpy(input, base, sizeof(base));
995 asym_op->modinv.base.data = input;
996 asym_op->modinv.base.length = sizeof(base);
997 asym_op->modinv.result.data = result;
998 asym_op->modinv.result.length = sizeof(result);
1000 /* attach asymmetric crypto session to crypto operations */
1001 rte_crypto_op_attach_asym_session(op, sess);
1003 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1005 /* Process crypto operation */
1006 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1008 "line %u FAILED: %s",
1009 __LINE__, "Error sending packet for operation");
1010 status = TEST_FAILED;
1014 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1017 if (result_op == NULL) {
1019 "line %u FAILED: %s",
1020 __LINE__, "Failed to process asym crypto op");
1021 status = TEST_FAILED;
1025 ret = verify_modinv(mod_inv, result_op);
1028 "operation verification failed\n");
1029 status = TEST_FAILED;
1034 rte_cryptodev_asym_session_clear(dev_id, sess);
1035 rte_cryptodev_asym_session_free(sess);
1039 rte_crypto_op_free(op);
1041 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1049 struct crypto_testsuite_params *ts_params = &testsuite_params;
1050 struct rte_mempool *op_mpool = ts_params->op_mpool;
1051 struct rte_mempool *sess_mpool = ts_params->session_mpool;
1052 uint8_t dev_id = ts_params->valid_devs[0];
1053 struct rte_crypto_asym_op *asym_op = NULL;
1054 struct rte_crypto_op *op = NULL, *result_op = NULL;
1055 struct rte_cryptodev_asym_session *sess = NULL;
1056 int status = TEST_SUCCESS;
1057 struct rte_cryptodev_asym_capability_idx cap_idx;
1058 const struct rte_cryptodev_asymmetric_xform_capability *capability;
1059 uint8_t input[TEST_DATA_SIZE] = {0};
1061 uint8_t result[sizeof(mod_p)] = { 0 };
1063 if (rte_cryptodev_asym_get_xform_enum(&modex_xform.xform_type,
1067 "Invalid ASYNC algorithm specified\n");
1071 /* check for modlen capability */
1072 cap_idx.type = modex_xform.xform_type;
1073 capability = rte_cryptodev_asym_capability_get(dev_id, &cap_idx);
1075 if (rte_cryptodev_asym_xform_capability_check_modlen(
1076 capability, modex_xform.modex.modulus.length)) {
1078 "Invalid MODULOUS length specified\n");
1082 /* generate crypto op data structure */
1083 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1086 "line %u FAILED: %s",
1087 __LINE__, "Failed to allocate asymmetric crypto "
1088 "operation struct");
1089 status = TEST_FAILED;
1093 sess = rte_cryptodev_asym_session_create(sess_mpool);
1097 "FAILED: %s", __LINE__,
1098 "Session creation failed");
1099 status = TEST_FAILED;
1103 if (rte_cryptodev_asym_session_init(dev_id, sess, &modex_xform,
1106 "line %u FAILED: %s",
1107 __LINE__, "unabled to config sym session");
1108 status = TEST_FAILED;
1113 memcpy(input, base, sizeof(base));
1114 asym_op->modex.base.data = input;
1115 asym_op->modex.base.length = sizeof(base);
1116 asym_op->modex.result.data = result;
1117 asym_op->modex.result.length = sizeof(result);
1118 /* attach asymmetric crypto session to crypto operations */
1119 rte_crypto_op_attach_asym_session(op, sess);
1121 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1122 /* Process crypto operation */
1123 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1125 "line %u FAILED: %s",
1126 __LINE__, "Error sending packet for operation");
1127 status = TEST_FAILED;
1131 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1134 if (result_op == NULL) {
1136 "line %u FAILED: %s",
1137 __LINE__, "Failed to process asym crypto op");
1138 status = TEST_FAILED;
1142 ret = verify_modexp(mod_exp, result_op);
1145 "operation verification failed\n");
1146 status = TEST_FAILED;
1151 rte_cryptodev_asym_session_clear(dev_id, sess);
1152 rte_cryptodev_asym_session_free(sess);
1156 rte_crypto_op_free(op);
1158 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1164 test_dh_keygenration(void)
1168 debug_hexdump(stdout, "p:", dh_xform.dh.p.data, dh_xform.dh.p.length);
1169 debug_hexdump(stdout, "g:", dh_xform.dh.g.data, dh_xform.dh.g.length);
1170 debug_hexdump(stdout, "priv_key:", dh_test_params.priv_key.data,
1171 dh_test_params.priv_key.length);
1173 RTE_LOG(INFO, USER1,
1174 "Test Public and Private key pair generation\n");
1176 status = test_dh_gen_kp(&dh_xform);
1177 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1179 RTE_LOG(INFO, USER1,
1180 "Test Public Key Generation using pre-defined priv key\n");
1182 status = test_dh_gen_pub_key(&dh_xform);
1183 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1185 RTE_LOG(INFO, USER1,
1186 "Test Private Key Generation only\n");
1188 status = test_dh_gen_priv_key(&dh_xform);
1189 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1191 RTE_LOG(INFO, USER1,
1192 "Test shared secret compute\n");
1194 status = test_dh_gen_shared_sec(&dh_xform);
1195 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1203 struct crypto_testsuite_params *ts_params = &testsuite_params;
1204 struct rte_mempool *op_mpool = ts_params->op_mpool;
1205 struct rte_mempool *sess_mpool = ts_params->session_mpool;
1206 uint8_t dev_id = ts_params->valid_devs[0];
1207 struct rte_crypto_asym_op *asym_op = NULL;
1208 struct rte_crypto_op *op = NULL, *result_op = NULL;
1209 struct rte_cryptodev_asym_session *sess = NULL;
1210 int status = TEST_SUCCESS;
1211 uint8_t r[TEST_DH_MOD_LEN];
1212 uint8_t s[TEST_DH_MOD_LEN];
1213 uint8_t dgst[] = "35d81554afaad2cf18f3a1770d5fedc4ea5be344";
1215 sess = rte_cryptodev_asym_session_create(sess_mpool);
1218 "line %u FAILED: %s", __LINE__,
1219 "Session creation failed");
1220 status = TEST_FAILED;
1223 /* set up crypto op data structure */
1224 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1227 "line %u FAILED: %s",
1228 __LINE__, "Failed to allocate asymmetric crypto "
1229 "operation struct");
1230 status = TEST_FAILED;
1235 debug_hexdump(stdout, "p: ", dsa_xform.dsa.p.data,
1236 dsa_xform.dsa.p.length);
1237 debug_hexdump(stdout, "q: ", dsa_xform.dsa.q.data,
1238 dsa_xform.dsa.q.length);
1239 debug_hexdump(stdout, "g: ", dsa_xform.dsa.g.data,
1240 dsa_xform.dsa.g.length);
1241 debug_hexdump(stdout, "priv_key: ", dsa_xform.dsa.x.data,
1242 dsa_xform.dsa.x.length);
1244 if (rte_cryptodev_asym_session_init(dev_id, sess, &dsa_xform,
1247 "line %u FAILED: %s",
1248 __LINE__, "unabled to config sym session");
1249 status = TEST_FAILED;
1253 /* attach asymmetric crypto session to crypto operations */
1254 rte_crypto_op_attach_asym_session(op, sess);
1255 asym_op->dsa.op_type = RTE_CRYPTO_ASYM_OP_SIGN;
1256 asym_op->dsa.message.data = dgst;
1257 asym_op->dsa.message.length = sizeof(dgst);
1258 asym_op->dsa.r.length = sizeof(r);
1259 asym_op->dsa.r.data = r;
1260 asym_op->dsa.s.length = sizeof(s);
1261 asym_op->dsa.s.data = s;
1263 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1265 /* Process crypto operation */
1266 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1268 "line %u FAILED: %s",
1269 __LINE__, "Error sending packet for operation");
1270 status = TEST_FAILED;
1274 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1277 if (result_op == NULL) {
1279 "line %u FAILED: %s",
1280 __LINE__, "Failed to process asym crypto op");
1281 status = TEST_FAILED;
1285 asym_op = result_op->asym;
1287 debug_hexdump(stdout, "r:",
1288 asym_op->dsa.r.data, asym_op->dsa.r.length);
1289 debug_hexdump(stdout, "s:",
1290 asym_op->dsa.s.data, asym_op->dsa.s.length);
1292 /* Test PMD DSA sign verification using signer public key */
1293 asym_op->dsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
1295 /* copy signer public key */
1296 asym_op->dsa.y.data = dsa_test_params.y.data;
1297 asym_op->dsa.y.length = dsa_test_params.y.length;
1299 /* Process crypto operation */
1300 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1302 "line %u FAILED: %s",
1303 __LINE__, "Error sending packet for operation");
1304 status = TEST_FAILED;
1308 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1311 if (result_op == NULL) {
1313 "line %u FAILED: %s",
1314 __LINE__, "Failed to process asym crypto op");
1315 status = TEST_FAILED;
1319 if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
1321 "line %u FAILED: %s",
1322 __LINE__, "Failed to process asym crypto op");
1323 status = TEST_FAILED;
1327 rte_cryptodev_asym_session_clear(dev_id, sess);
1328 rte_cryptodev_asym_session_free(sess);
1331 rte_crypto_op_free(op);
1339 status = test_dsa_sign();
1340 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1345 static struct unit_test_suite cryptodev_openssl_asym_testsuite = {
1346 .suite_name = "Crypto Device OPENSSL ASYM Unit Test Suite",
1347 .setup = testsuite_setup,
1348 .teardown = testsuite_teardown,
1349 .unit_test_cases = {
1350 TEST_CASE_ST(ut_setup, ut_teardown, test_capability),
1351 TEST_CASE_ST(ut_setup, ut_teardown, test_dsa),
1352 TEST_CASE_ST(ut_setup, ut_teardown, test_dh_keygenration),
1353 TEST_CASE_ST(ut_setup, ut_teardown, test_rsa_enc_dec),
1354 TEST_CASE_ST(ut_setup, ut_teardown, test_rsa_sign_verify),
1355 TEST_CASE_ST(ut_setup, ut_teardown, test_mod_inv),
1356 TEST_CASE_ST(ut_setup, ut_teardown, test_mod_exp),
1357 TEST_CASES_END() /**< NULL terminate unit test array */
1362 test_cryptodev_openssl_asym(void)
1364 gbl_driver_id = rte_cryptodev_driver_id_get(
1365 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
1367 if (gbl_driver_id == -1) {
1368 RTE_LOG(ERR, USER1, "OPENSSL PMD must be loaded. Check if "
1369 "CONFIG_RTE_LIBRTE_PMD_OPENSSL is enabled "
1370 "in config file to run this testsuite.\n");
1374 return unit_test_suite_runner(&cryptodev_openssl_asym_testsuite);
1377 REGISTER_TEST_COMMAND(cryptodev_openssl_asym_autotest,
1378 test_cryptodev_openssl_asym);