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;
157 ret = rsa_verify(&rsaplaintext, result_op);
159 status = TEST_FAILED;
164 rte_cryptodev_asym_session_clear(dev_id, sess);
165 rte_cryptodev_asym_session_free(sess);
169 rte_crypto_op_free(op);
171 TEST_ASSERT_EQUAL(status, 0, "Test failed");
177 test_rsa_enc_dec(void)
179 struct crypto_testsuite_params *ts_params = &testsuite_params;
180 struct rte_mempool *op_mpool = ts_params->op_mpool;
181 struct rte_mempool *sess_mpool = ts_params->session_mpool;
182 uint8_t dev_id = ts_params->valid_devs[0];
183 struct rte_crypto_asym_op *asym_op = NULL;
184 struct rte_crypto_op *op = NULL, *result_op = NULL;
185 struct rte_cryptodev_asym_session *sess = NULL;
186 int status = TEST_SUCCESS;
187 uint8_t input_buf[TEST_DATA_SIZE] = {0};
189 sess = rte_cryptodev_asym_session_create(sess_mpool);
192 RTE_LOG(ERR, USER1, "line %u "
193 "FAILED: %s", __LINE__,
194 "Session creation failed");
195 status = TEST_FAILED;
199 if (rte_cryptodev_asym_session_init(dev_id, sess, &rsa_xform,
202 "line %u FAILED: %s",
203 __LINE__, "unabled to config sym session");
204 status = TEST_FAILED;
208 /* set up crypto op data structure */
209 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
212 "line %u FAILED: %s",
214 "Failed to allocate asymmetric crypto "
216 status = TEST_FAILED;
221 /*Compute encryption on the test vector */
222 asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_ENCRYPT;
224 memcpy(input_buf, rsaplaintext.data,
226 asym_op->rsa.message.data = input_buf;
227 asym_op->rsa.message.length = rsaplaintext.len;
228 asym_op->rsa.pad = RTE_CRYPTO_RSA_PKCS1_V1_5_BT2;
230 debug_hexdump(stdout, "message", asym_op->rsa.message.data,
231 asym_op->rsa.message.length);
233 /* attach asymmetric crypto session to crypto operations */
234 rte_crypto_op_attach_asym_session(op, sess);
236 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
238 /* Process crypto operation */
239 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
241 "line %u FAILED: %s",
242 __LINE__, "Error sending packet for operation");
243 status = TEST_FAILED;
247 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
250 if (result_op == NULL) {
252 "line %u FAILED: %s",
253 __LINE__, "Failed to process asym crypto op");
254 status = TEST_FAILED;
257 debug_hexdump(stdout, "encrypted message", asym_op->rsa.message.data,
258 asym_op->rsa.message.length);
259 /* Use the resulted output as decryption Input vector*/
260 asym_op = result_op->asym;
261 asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_DECRYPT;
262 asym_op->rsa.pad = RTE_CRYPTO_RSA_PKCS1_V1_5_BT1;
264 /* Process crypto operation */
265 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
267 "line %u FAILED: %s",
268 __LINE__, "Error sending packet for operation");
269 status = TEST_FAILED;
273 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
276 if (result_op == NULL) {
278 "line %u FAILED: %s",
279 __LINE__, "Failed to process asym crypto op");
280 status = TEST_FAILED;
283 status = TEST_SUCCESS;
285 ret = rsa_verify(&rsaplaintext, result_op);
287 status = TEST_FAILED;
292 rte_cryptodev_asym_session_clear(dev_id, sess);
293 rte_cryptodev_asym_session_free(sess);
297 rte_crypto_op_free(op);
299 TEST_ASSERT_EQUAL(status, 0, "Test failed");
305 testsuite_setup(void)
307 struct crypto_testsuite_params *ts_params = &testsuite_params;
308 struct rte_cryptodev_info info;
309 uint32_t i = 0, nb_devs, dev_id;
313 memset(ts_params, 0, sizeof(*ts_params));
315 ts_params->op_mpool = rte_crypto_op_pool_create(
316 "CRYPTO_ASYM_OP_POOL",
317 RTE_CRYPTO_OP_TYPE_ASYMMETRIC,
321 if (ts_params->op_mpool == NULL) {
322 RTE_LOG(ERR, USER1, "Can't create ASYM_CRYPTO_OP_POOL\n");
326 /* Create an OPENSSL device if required */
327 if (gbl_driver_id == rte_cryptodev_driver_id_get(
328 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD))) {
329 nb_devs = rte_cryptodev_device_count_by_driver(
330 rte_cryptodev_driver_id_get(
331 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD)));
334 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD),
337 TEST_ASSERT(ret == 0, "Failed to create "
338 "instance of pmd : %s",
339 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
343 nb_devs = rte_cryptodev_count();
345 RTE_LOG(ERR, USER1, "No crypto devices found?\n");
349 /* Create list of valid crypto devs */
350 for (i = 0; i < nb_devs; i++) {
351 rte_cryptodev_info_get(i, &info);
352 if (info.driver_id == gbl_driver_id)
353 ts_params->valid_devs[ts_params->valid_dev_count++] = i;
356 if (ts_params->valid_dev_count < 1)
359 /* Set up all the qps on the first of the valid devices found */
361 dev_id = ts_params->valid_devs[0];
363 rte_cryptodev_info_get(dev_id, &info);
365 /* check if device support asymmetric, skip if not */
366 if (!(info.feature_flags &
367 RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)) {
368 RTE_LOG(ERR, USER1, "Device doesn't support asymmetric. "
373 /* configure device with num qp */
374 ts_params->conf.nb_queue_pairs = info.max_nb_queue_pairs;
375 ts_params->conf.socket_id = SOCKET_ID_ANY;
376 TEST_ASSERT_SUCCESS(rte_cryptodev_configure(dev_id,
378 "Failed to configure cryptodev %u with %u qps",
379 dev_id, ts_params->conf.nb_queue_pairs);
382 ts_params->qp_conf.nb_descriptors = DEFAULT_NUM_OPS_INFLIGHT;
383 for (qp_id = 0; qp_id < info.max_nb_queue_pairs; qp_id++) {
384 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
385 dev_id, qp_id, &ts_params->qp_conf,
386 rte_cryptodev_socket_id(dev_id),
387 ts_params->session_mpool),
388 "Failed to setup queue pair %u on cryptodev %u ASYM",
392 /* setup asym session pool */
393 unsigned int session_size =
394 rte_cryptodev_asym_get_private_session_size(dev_id);
396 * Create mempool with TEST_NUM_SESSIONS * 2,
397 * to include the session headers
399 ts_params->session_mpool = rte_mempool_create(
401 TEST_NUM_SESSIONS * 2,
403 0, 0, NULL, NULL, NULL,
407 TEST_ASSERT_NOT_NULL(ts_params->session_mpool,
408 "session mempool allocation failed");
414 testsuite_teardown(void)
416 struct crypto_testsuite_params *ts_params = &testsuite_params;
418 if (ts_params->op_mpool != NULL) {
419 RTE_LOG(DEBUG, USER1, "CRYPTO_OP_POOL count %u\n",
420 rte_mempool_avail_count(ts_params->op_mpool));
423 /* Free session mempools */
424 if (ts_params->session_mpool != NULL) {
425 rte_mempool_free(ts_params->session_mpool);
426 ts_params->session_mpool = NULL;
433 struct crypto_testsuite_params *ts_params = &testsuite_params;
437 /* Reconfigure device to default parameters */
438 ts_params->conf.socket_id = SOCKET_ID_ANY;
440 TEST_ASSERT_SUCCESS(rte_cryptodev_configure(ts_params->valid_devs[0],
442 "Failed to configure cryptodev %u",
443 ts_params->valid_devs[0]);
445 for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs ; qp_id++) {
446 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
447 ts_params->valid_devs[0], qp_id,
449 rte_cryptodev_socket_id(ts_params->valid_devs[0]),
450 ts_params->session_mpool),
451 "Failed to setup queue pair %u on cryptodev %u",
452 qp_id, ts_params->valid_devs[0]);
455 rte_cryptodev_stats_reset(ts_params->valid_devs[0]);
457 /* Start the device */
458 TEST_ASSERT_SUCCESS(rte_cryptodev_start(ts_params->valid_devs[0]),
459 "Failed to start cryptodev %u",
460 ts_params->valid_devs[0]);
468 struct crypto_testsuite_params *ts_params = &testsuite_params;
469 struct rte_cryptodev_stats stats;
471 rte_cryptodev_stats_get(ts_params->valid_devs[0], &stats);
473 /* Stop the device */
474 rte_cryptodev_stop(ts_params->valid_devs[0]);
477 static inline void print_asym_capa(
478 const struct rte_cryptodev_asymmetric_xform_capability *capa)
482 printf("\nxform type: %s\n===================\n",
483 rte_crypto_asym_xform_strings[capa->xform_type]);
484 printf("operation supported -");
486 for (i = 0; i < RTE_CRYPTO_ASYM_OP_LIST_END; i++) {
487 /* check supported operations */
488 if (rte_cryptodev_asym_xform_capability_check_optype(capa, i))
490 rte_crypto_asym_op_strings[i]);
492 switch (capa->xform_type) {
493 case RTE_CRYPTO_ASYM_XFORM_RSA:
494 case RTE_CRYPTO_ASYM_XFORM_MODINV:
495 case RTE_CRYPTO_ASYM_XFORM_MODEX:
496 case RTE_CRYPTO_ASYM_XFORM_DH:
497 case RTE_CRYPTO_ASYM_XFORM_DSA:
498 printf(" modlen: min %d max %d increment %d\n",
501 capa->modlen.increment);
509 test_capability(void)
511 struct crypto_testsuite_params *ts_params = &testsuite_params;
512 uint8_t dev_id = ts_params->valid_devs[0];
513 struct rte_cryptodev_info dev_info;
514 const struct rte_cryptodev_capabilities *dev_capa;
516 struct rte_cryptodev_asym_capability_idx idx;
517 const struct rte_cryptodev_asymmetric_xform_capability *capa;
519 rte_cryptodev_info_get(dev_id, &dev_info);
520 if (!(dev_info.feature_flags &
521 RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)) {
523 "Device doesn't support asymmetric. Test Skipped\n");
527 /* print xform capability */
529 dev_info.capabilities[i].op != RTE_CRYPTO_OP_TYPE_UNDEFINED;
531 dev_capa = &(dev_info.capabilities[i]);
532 if (dev_info.capabilities[i].op ==
533 RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
534 idx.type = dev_capa->asym.xform_capa.xform_type;
536 capa = rte_cryptodev_asym_capability_get(dev_id,
538 rte_cryptodev_asym_capability_idx *) &idx);
539 print_asym_capa(capa);
546 test_dh_gen_shared_sec(struct rte_crypto_asym_xform *xfrm)
548 struct crypto_testsuite_params *ts_params = &testsuite_params;
549 struct rte_mempool *op_mpool = ts_params->op_mpool;
550 struct rte_mempool *sess_mpool = ts_params->session_mpool;
551 uint8_t dev_id = ts_params->valid_devs[0];
552 struct rte_crypto_asym_op *asym_op = NULL;
553 struct rte_crypto_op *op = NULL, *result_op = NULL;
554 struct rte_cryptodev_asym_session *sess = NULL;
555 int status = TEST_SUCCESS;
556 uint8_t output[TEST_DH_MOD_LEN];
557 struct rte_crypto_asym_xform xform = *xfrm;
558 uint8_t peer[] = "01234567890123456789012345678901234567890123456789";
560 sess = rte_cryptodev_asym_session_create(sess_mpool);
563 "line %u FAILED: %s", __LINE__,
564 "Session creation failed");
565 status = TEST_FAILED;
568 /* set up crypto op data structure */
569 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
572 "line %u FAILED: %s",
573 __LINE__, "Failed to allocate asymmetric crypto "
575 status = TEST_FAILED;
580 /* Setup a xform and op to generate private key only */
581 xform.dh.type = RTE_CRYPTO_ASYM_OP_SHARED_SECRET_COMPUTE;
583 asym_op->dh.priv_key.data = dh_test_params.priv_key.data;
584 asym_op->dh.priv_key.length = dh_test_params.priv_key.length;
585 asym_op->dh.pub_key.data = (uint8_t *)peer;
586 asym_op->dh.pub_key.length = sizeof(peer);
587 asym_op->dh.shared_secret.data = output;
588 asym_op->dh.shared_secret.length = sizeof(output);
590 if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
593 "line %u FAILED: %s",
594 __LINE__, "unabled to config sym session");
595 status = TEST_FAILED;
599 /* attach asymmetric crypto session to crypto operations */
600 rte_crypto_op_attach_asym_session(op, sess);
602 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
604 /* Process crypto operation */
605 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
607 "line %u FAILED: %s",
608 __LINE__, "Error sending packet for operation");
609 status = TEST_FAILED;
613 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
616 if (result_op == NULL) {
618 "line %u FAILED: %s",
619 __LINE__, "Failed to process asym crypto op");
620 status = TEST_FAILED;
624 debug_hexdump(stdout, "shared secret:",
625 asym_op->dh.shared_secret.data,
626 asym_op->dh.shared_secret.length);
630 rte_cryptodev_asym_session_clear(dev_id, sess);
631 rte_cryptodev_asym_session_free(sess);
634 rte_crypto_op_free(op);
639 test_dh_gen_priv_key(struct rte_crypto_asym_xform *xfrm)
641 struct crypto_testsuite_params *ts_params = &testsuite_params;
642 struct rte_mempool *op_mpool = ts_params->op_mpool;
643 struct rte_mempool *sess_mpool = ts_params->session_mpool;
644 uint8_t dev_id = ts_params->valid_devs[0];
645 struct rte_crypto_asym_op *asym_op = NULL;
646 struct rte_crypto_op *op = NULL, *result_op = NULL;
647 struct rte_cryptodev_asym_session *sess = NULL;
648 int status = TEST_SUCCESS;
649 uint8_t output[TEST_DH_MOD_LEN];
650 struct rte_crypto_asym_xform xform = *xfrm;
652 sess = rte_cryptodev_asym_session_create(sess_mpool);
655 "line %u FAILED: %s", __LINE__,
656 "Session creation failed");
657 status = TEST_FAILED;
660 /* set up crypto op data structure */
661 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
664 "line %u FAILED: %s",
665 __LINE__, "Failed to allocate asymmetric crypto "
667 status = TEST_FAILED;
672 /* Setup a xform and op to generate private key only */
673 xform.dh.type = RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE;
675 asym_op->dh.priv_key.data = output;
676 asym_op->dh.priv_key.length = sizeof(output);
678 if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
681 "line %u FAILED: %s",
682 __LINE__, "unabled to config sym session");
683 status = TEST_FAILED;
687 /* attach asymmetric crypto session to crypto operations */
688 rte_crypto_op_attach_asym_session(op, sess);
690 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
692 /* Process crypto operation */
693 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
695 "line %u FAILED: %s",
696 __LINE__, "Error sending packet for operation");
697 status = TEST_FAILED;
701 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
704 if (result_op == NULL) {
706 "line %u FAILED: %s",
707 __LINE__, "Failed to process asym crypto op");
708 status = TEST_FAILED;
712 debug_hexdump(stdout, "private key:",
713 asym_op->dh.priv_key.data,
714 asym_op->dh.priv_key.length);
719 rte_cryptodev_asym_session_clear(dev_id, sess);
720 rte_cryptodev_asym_session_free(sess);
723 rte_crypto_op_free(op);
730 test_dh_gen_pub_key(struct rte_crypto_asym_xform *xfrm)
732 struct crypto_testsuite_params *ts_params = &testsuite_params;
733 struct rte_mempool *op_mpool = ts_params->op_mpool;
734 struct rte_mempool *sess_mpool = ts_params->session_mpool;
735 uint8_t dev_id = ts_params->valid_devs[0];
736 struct rte_crypto_asym_op *asym_op = NULL;
737 struct rte_crypto_op *op = NULL, *result_op = NULL;
738 struct rte_cryptodev_asym_session *sess = NULL;
739 int status = TEST_SUCCESS;
740 uint8_t output[TEST_DH_MOD_LEN];
741 struct rte_crypto_asym_xform xform = *xfrm;
743 sess = rte_cryptodev_asym_session_create(sess_mpool);
746 "line %u FAILED: %s", __LINE__,
747 "Session creation failed");
748 status = TEST_FAILED;
751 /* set up crypto op data structure */
752 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
755 "line %u FAILED: %s",
756 __LINE__, "Failed to allocate asymmetric crypto "
758 status = TEST_FAILED;
762 /* Setup a xform chain to generate public key
763 * using test private key
766 xform.dh.type = RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE;
769 asym_op->dh.pub_key.data = output;
770 asym_op->dh.pub_key.length = sizeof(output);
771 /* load pre-defined private key */
772 asym_op->dh.priv_key.data = rte_malloc(NULL,
773 dh_test_params.priv_key.length,
775 asym_op->dh.priv_key = dh_test_params.priv_key;
777 if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
780 "line %u FAILED: %s",
781 __LINE__, "unabled to config sym session");
782 status = TEST_FAILED;
786 /* attach asymmetric crypto session to crypto operations */
787 rte_crypto_op_attach_asym_session(op, sess);
789 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
791 /* Process crypto operation */
792 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
794 "line %u FAILED: %s",
795 __LINE__, "Error sending packet for operation");
796 status = TEST_FAILED;
800 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
803 if (result_op == NULL) {
805 "line %u FAILED: %s",
806 __LINE__, "Failed to process asym crypto op");
807 status = TEST_FAILED;
811 debug_hexdump(stdout, "pub key:",
812 asym_op->dh.pub_key.data, asym_op->dh.pub_key.length);
814 debug_hexdump(stdout, "priv key:",
815 asym_op->dh.priv_key.data, asym_op->dh.priv_key.length);
819 rte_cryptodev_asym_session_clear(dev_id, sess);
820 rte_cryptodev_asym_session_free(sess);
823 rte_crypto_op_free(op);
829 test_dh_gen_kp(struct rte_crypto_asym_xform *xfrm)
831 struct crypto_testsuite_params *ts_params = &testsuite_params;
832 struct rte_mempool *op_mpool = ts_params->op_mpool;
833 struct rte_mempool *sess_mpool = ts_params->session_mpool;
834 uint8_t dev_id = ts_params->valid_devs[0];
835 struct rte_crypto_asym_op *asym_op = NULL;
836 struct rte_crypto_op *op = NULL, *result_op = NULL;
837 struct rte_cryptodev_asym_session *sess = NULL;
838 int status = TEST_SUCCESS;
839 uint8_t out_pub_key[TEST_DH_MOD_LEN];
840 uint8_t out_prv_key[TEST_DH_MOD_LEN];
841 struct rte_crypto_asym_xform pub_key_xform;
842 struct rte_crypto_asym_xform xform = *xfrm;
844 sess = rte_cryptodev_asym_session_create(sess_mpool);
847 "line %u FAILED: %s", __LINE__,
848 "Session creation failed");
849 status = TEST_FAILED;
853 /* set up crypto op data structure */
854 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
857 "line %u FAILED: %s",
858 __LINE__, "Failed to allocate asymmetric crypto "
860 status = TEST_FAILED;
864 /* Setup a xform chain to generate
865 * private key first followed by
867 */xform.dh.type = RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE;
868 pub_key_xform.xform_type = RTE_CRYPTO_ASYM_XFORM_DH;
869 pub_key_xform.dh.type = RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE;
870 xform.next = &pub_key_xform;
872 asym_op->dh.pub_key.data = out_pub_key;
873 asym_op->dh.pub_key.length = sizeof(out_pub_key);
874 asym_op->dh.priv_key.data = out_prv_key;
875 asym_op->dh.priv_key.length = sizeof(out_prv_key);
876 if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
879 "line %u FAILED: %s",
880 __LINE__, "unabled to config sym session");
881 status = TEST_FAILED;
885 /* attach asymmetric crypto session to crypto operations */
886 rte_crypto_op_attach_asym_session(op, sess);
888 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
890 /* Process crypto operation */
891 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
893 "line %u FAILED: %s",
894 __LINE__, "Error sending packet for operation");
895 status = TEST_FAILED;
899 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
902 if (result_op == NULL) {
904 "line %u FAILED: %s",
905 __LINE__, "Failed to process asym crypto op");
906 status = TEST_FAILED;
909 debug_hexdump(stdout, "priv key:",
910 out_prv_key, asym_op->dh.priv_key.length);
911 debug_hexdump(stdout, "pub key:",
912 out_pub_key, asym_op->dh.pub_key.length);
916 rte_cryptodev_asym_session_clear(dev_id, sess);
917 rte_cryptodev_asym_session_free(sess);
920 rte_crypto_op_free(op);
928 struct crypto_testsuite_params *ts_params = &testsuite_params;
929 struct rte_mempool *op_mpool = ts_params->op_mpool;
930 struct rte_mempool *sess_mpool = ts_params->session_mpool;
931 uint8_t dev_id = ts_params->valid_devs[0];
932 struct rte_crypto_asym_op *asym_op = NULL;
933 struct rte_crypto_op *op = NULL, *result_op = NULL;
934 struct rte_cryptodev_asym_session *sess = NULL;
935 int status = TEST_SUCCESS;
936 struct rte_cryptodev_asym_capability_idx cap_idx;
937 const struct rte_cryptodev_asymmetric_xform_capability *capability;
938 uint8_t input[TEST_DATA_SIZE] = {0};
941 if (rte_cryptodev_asym_get_xform_enum(
942 &modinv_xform.xform_type, "modinv") < 0) {
944 "Invalid ASYNC algorithm specified\n");
948 cap_idx.type = modinv_xform.xform_type;
949 capability = rte_cryptodev_asym_capability_get(dev_id,
952 if (rte_cryptodev_asym_xform_capability_check_modlen(
954 modinv_xform.modinv.modulus.length)) {
956 "Invalid MODULOUS length specified\n");
960 sess = rte_cryptodev_asym_session_create(sess_mpool);
962 RTE_LOG(ERR, USER1, "line %u "
963 "FAILED: %s", __LINE__,
964 "Session creation failed");
965 status = TEST_FAILED;
969 if (rte_cryptodev_asym_session_init(dev_id, sess, &modinv_xform,
972 "line %u FAILED: %s",
973 __LINE__, "unabled to config sym session");
974 status = TEST_FAILED;
978 /* generate crypto op data structure */
979 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
982 "line %u FAILED: %s",
983 __LINE__, "Failed to allocate asymmetric crypto "
985 status = TEST_FAILED;
990 memcpy(input, base, sizeof(base));
991 asym_op->modinv.base.data = input;
992 asym_op->modinv.base.length = sizeof(base);
994 /* attach asymmetric crypto session to crypto operations */
995 rte_crypto_op_attach_asym_session(op, sess);
997 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
999 /* Process crypto operation */
1000 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1002 "line %u FAILED: %s",
1003 __LINE__, "Error sending packet for operation");
1004 status = TEST_FAILED;
1008 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1011 if (result_op == NULL) {
1013 "line %u FAILED: %s",
1014 __LINE__, "Failed to process asym crypto op");
1015 status = TEST_FAILED;
1019 ret = verify_modinv(mod_inv, result_op);
1022 "operation verification failed\n");
1023 status = TEST_FAILED;
1028 rte_cryptodev_asym_session_clear(dev_id, sess);
1029 rte_cryptodev_asym_session_free(sess);
1033 rte_crypto_op_free(op);
1035 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1043 struct crypto_testsuite_params *ts_params = &testsuite_params;
1044 struct rte_mempool *op_mpool = ts_params->op_mpool;
1045 struct rte_mempool *sess_mpool = ts_params->session_mpool;
1046 uint8_t dev_id = ts_params->valid_devs[0];
1047 struct rte_crypto_asym_op *asym_op = NULL;
1048 struct rte_crypto_op *op = NULL, *result_op = NULL;
1049 struct rte_cryptodev_asym_session *sess = NULL;
1050 int status = TEST_SUCCESS;
1051 struct rte_cryptodev_asym_capability_idx cap_idx;
1052 const struct rte_cryptodev_asymmetric_xform_capability *capability;
1053 uint8_t input[TEST_DATA_SIZE] = {0};
1056 if (rte_cryptodev_asym_get_xform_enum(&modex_xform.xform_type,
1060 "Invalid ASYNC algorithm specified\n");
1064 /* check for modlen capability */
1065 cap_idx.type = modex_xform.xform_type;
1066 capability = rte_cryptodev_asym_capability_get(dev_id, &cap_idx);
1068 if (rte_cryptodev_asym_xform_capability_check_modlen(
1069 capability, modex_xform.modex.modulus.length)) {
1071 "Invalid MODULOUS length specified\n");
1075 /* generate crypto op data structure */
1076 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1079 "line %u FAILED: %s",
1080 __LINE__, "Failed to allocate asymmetric crypto "
1081 "operation struct");
1082 status = TEST_FAILED;
1086 sess = rte_cryptodev_asym_session_create(sess_mpool);
1090 "FAILED: %s", __LINE__,
1091 "Session creation failed");
1092 status = TEST_FAILED;
1096 if (rte_cryptodev_asym_session_init(dev_id, sess, &modex_xform,
1099 "line %u FAILED: %s",
1100 __LINE__, "unabled to config sym session");
1101 status = TEST_FAILED;
1106 memcpy(input, base, sizeof(base));
1107 asym_op->modex.base.data = input;
1108 asym_op->modex.base.length = sizeof(base);
1109 /* attach asymmetric crypto session to crypto operations */
1110 rte_crypto_op_attach_asym_session(op, sess);
1112 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1113 /* Process crypto operation */
1114 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1116 "line %u FAILED: %s",
1117 __LINE__, "Error sending packet for operation");
1118 status = TEST_FAILED;
1122 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1125 if (result_op == NULL) {
1127 "line %u FAILED: %s",
1128 __LINE__, "Failed to process asym crypto op");
1129 status = TEST_FAILED;
1133 ret = verify_modexp(mod_exp, result_op);
1136 "operation verification failed\n");
1137 status = TEST_FAILED;
1142 rte_cryptodev_asym_session_clear(dev_id, sess);
1143 rte_cryptodev_asym_session_free(sess);
1147 rte_crypto_op_free(op);
1149 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1155 test_dh_keygenration(void)
1159 debug_hexdump(stdout, "p:", dh_xform.dh.p.data, dh_xform.dh.p.length);
1160 debug_hexdump(stdout, "g:", dh_xform.dh.g.data, dh_xform.dh.g.length);
1161 debug_hexdump(stdout, "priv_key:", dh_test_params.priv_key.data,
1162 dh_test_params.priv_key.length);
1164 RTE_LOG(INFO, USER1,
1165 "Test Public and Private key pair generation\n");
1167 status = test_dh_gen_kp(&dh_xform);
1168 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1170 RTE_LOG(INFO, USER1,
1171 "Test Public Key Generation using pre-defined priv key\n");
1173 status = test_dh_gen_pub_key(&dh_xform);
1174 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1176 RTE_LOG(INFO, USER1,
1177 "Test Private Key Generation only\n");
1179 status = test_dh_gen_priv_key(&dh_xform);
1180 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1182 RTE_LOG(INFO, USER1,
1183 "Test shared secret compute\n");
1185 status = test_dh_gen_shared_sec(&dh_xform);
1186 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1194 struct crypto_testsuite_params *ts_params = &testsuite_params;
1195 struct rte_mempool *op_mpool = ts_params->op_mpool;
1196 struct rte_mempool *sess_mpool = ts_params->session_mpool;
1197 uint8_t dev_id = ts_params->valid_devs[0];
1198 struct rte_crypto_asym_op *asym_op = NULL;
1199 struct rte_crypto_op *op = NULL, *result_op = NULL;
1200 struct rte_cryptodev_asym_session *sess = NULL;
1201 int status = TEST_SUCCESS;
1202 uint8_t r[TEST_DH_MOD_LEN];
1203 uint8_t s[TEST_DH_MOD_LEN];
1204 uint8_t dgst[] = "35d81554afaad2cf18f3a1770d5fedc4ea5be344";
1206 sess = rte_cryptodev_asym_session_create(sess_mpool);
1209 "line %u FAILED: %s", __LINE__,
1210 "Session creation failed");
1211 status = TEST_FAILED;
1214 /* set up crypto op data structure */
1215 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1218 "line %u FAILED: %s",
1219 __LINE__, "Failed to allocate asymmetric crypto "
1220 "operation struct");
1221 status = TEST_FAILED;
1226 debug_hexdump(stdout, "p: ", dsa_xform.dsa.p.data,
1227 dsa_xform.dsa.p.length);
1228 debug_hexdump(stdout, "q: ", dsa_xform.dsa.q.data,
1229 dsa_xform.dsa.q.length);
1230 debug_hexdump(stdout, "g: ", dsa_xform.dsa.g.data,
1231 dsa_xform.dsa.g.length);
1232 debug_hexdump(stdout, "priv_key: ", dsa_xform.dsa.x.data,
1233 dsa_xform.dsa.x.length);
1235 if (rte_cryptodev_asym_session_init(dev_id, sess, &dsa_xform,
1238 "line %u FAILED: %s",
1239 __LINE__, "unabled to config sym session");
1240 status = TEST_FAILED;
1244 /* attach asymmetric crypto session to crypto operations */
1245 rte_crypto_op_attach_asym_session(op, sess);
1246 asym_op->dsa.op_type = RTE_CRYPTO_ASYM_OP_SIGN;
1247 asym_op->dsa.message.data = dgst;
1248 asym_op->dsa.message.length = sizeof(dgst);
1249 asym_op->dsa.r.length = sizeof(r);
1250 asym_op->dsa.r.data = r;
1251 asym_op->dsa.s.length = sizeof(s);
1252 asym_op->dsa.s.data = s;
1254 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1256 /* Process crypto operation */
1257 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1259 "line %u FAILED: %s",
1260 __LINE__, "Error sending packet for operation");
1261 status = TEST_FAILED;
1265 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1268 if (result_op == NULL) {
1270 "line %u FAILED: %s",
1271 __LINE__, "Failed to process asym crypto op");
1272 status = TEST_FAILED;
1276 asym_op = result_op->asym;
1278 debug_hexdump(stdout, "r:",
1279 asym_op->dsa.r.data, asym_op->dsa.r.length);
1280 debug_hexdump(stdout, "s:",
1281 asym_op->dsa.s.data, asym_op->dsa.s.length);
1283 /* Test PMD DSA sign verification using signer public key */
1284 asym_op->dsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
1286 /* copy signer public key */
1287 asym_op->dsa.y.data = dsa_test_params.y.data;
1288 asym_op->dsa.y.length = dsa_test_params.y.length;
1290 /* Process crypto operation */
1291 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1293 "line %u FAILED: %s",
1294 __LINE__, "Error sending packet for operation");
1295 status = TEST_FAILED;
1299 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1302 if (result_op == NULL) {
1304 "line %u FAILED: %s",
1305 __LINE__, "Failed to process asym crypto op");
1306 status = TEST_FAILED;
1310 if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
1312 "line %u FAILED: %s",
1313 __LINE__, "Failed to process asym crypto op");
1314 status = TEST_FAILED;
1318 rte_cryptodev_asym_session_clear(dev_id, sess);
1319 rte_cryptodev_asym_session_free(sess);
1322 rte_crypto_op_free(op);
1330 status = test_dsa_sign();
1331 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1336 static struct unit_test_suite cryptodev_openssl_asym_testsuite = {
1337 .suite_name = "Crypto Device OPENSSL ASYM Unit Test Suite",
1338 .setup = testsuite_setup,
1339 .teardown = testsuite_teardown,
1340 .unit_test_cases = {
1341 TEST_CASE_ST(ut_setup, ut_teardown, test_capability),
1342 TEST_CASE_ST(ut_setup, ut_teardown, test_dsa),
1343 TEST_CASE_ST(ut_setup, ut_teardown, test_dh_keygenration),
1344 TEST_CASE_ST(ut_setup, ut_teardown, test_rsa_enc_dec),
1345 TEST_CASE_ST(ut_setup, ut_teardown, test_rsa_sign_verify),
1346 TEST_CASE_ST(ut_setup, ut_teardown, test_mod_inv),
1347 TEST_CASE_ST(ut_setup, ut_teardown, test_mod_exp),
1348 TEST_CASES_END() /**< NULL terminate unit test array */
1353 test_cryptodev_openssl_asym(void)
1355 gbl_driver_id = rte_cryptodev_driver_id_get(
1356 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
1358 if (gbl_driver_id == -1) {
1359 RTE_LOG(ERR, USER1, "OPENSSL PMD must be loaded. Check if "
1360 "CONFIG_RTE_LIBRTE_PMD_OPENSSL is enabled "
1361 "in config file to run this testsuite.\n");
1365 return unit_test_suite_runner(&cryptodev_openssl_asym_testsuite);
1368 REGISTER_TEST_COMMAND(cryptodev_openssl_asym_autotest,
1369 test_cryptodev_openssl_asym);