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};
944 if (rte_cryptodev_asym_get_xform_enum(
945 &modinv_xform.xform_type, "modinv") < 0) {
947 "Invalid ASYNC algorithm specified\n");
951 cap_idx.type = modinv_xform.xform_type;
952 capability = rte_cryptodev_asym_capability_get(dev_id,
955 if (rte_cryptodev_asym_xform_capability_check_modlen(
957 modinv_xform.modinv.modulus.length)) {
959 "Invalid MODULOUS length specified\n");
963 sess = rte_cryptodev_asym_session_create(sess_mpool);
965 RTE_LOG(ERR, USER1, "line %u "
966 "FAILED: %s", __LINE__,
967 "Session creation failed");
968 status = TEST_FAILED;
972 if (rte_cryptodev_asym_session_init(dev_id, sess, &modinv_xform,
975 "line %u FAILED: %s",
976 __LINE__, "unabled to config sym session");
977 status = TEST_FAILED;
981 /* generate crypto op data structure */
982 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
985 "line %u FAILED: %s",
986 __LINE__, "Failed to allocate asymmetric crypto "
988 status = TEST_FAILED;
993 memcpy(input, base, sizeof(base));
994 asym_op->modinv.base.data = input;
995 asym_op->modinv.base.length = sizeof(base);
997 /* attach asymmetric crypto session to crypto operations */
998 rte_crypto_op_attach_asym_session(op, sess);
1000 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1002 /* Process crypto operation */
1003 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1005 "line %u FAILED: %s",
1006 __LINE__, "Error sending packet for operation");
1007 status = TEST_FAILED;
1011 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1014 if (result_op == NULL) {
1016 "line %u FAILED: %s",
1017 __LINE__, "Failed to process asym crypto op");
1018 status = TEST_FAILED;
1022 ret = verify_modinv(mod_inv, result_op);
1025 "operation verification failed\n");
1026 status = TEST_FAILED;
1031 rte_cryptodev_asym_session_clear(dev_id, sess);
1032 rte_cryptodev_asym_session_free(sess);
1036 rte_crypto_op_free(op);
1038 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1046 struct crypto_testsuite_params *ts_params = &testsuite_params;
1047 struct rte_mempool *op_mpool = ts_params->op_mpool;
1048 struct rte_mempool *sess_mpool = ts_params->session_mpool;
1049 uint8_t dev_id = ts_params->valid_devs[0];
1050 struct rte_crypto_asym_op *asym_op = NULL;
1051 struct rte_crypto_op *op = NULL, *result_op = NULL;
1052 struct rte_cryptodev_asym_session *sess = NULL;
1053 int status = TEST_SUCCESS;
1054 struct rte_cryptodev_asym_capability_idx cap_idx;
1055 const struct rte_cryptodev_asymmetric_xform_capability *capability;
1056 uint8_t input[TEST_DATA_SIZE] = {0};
1059 if (rte_cryptodev_asym_get_xform_enum(&modex_xform.xform_type,
1063 "Invalid ASYNC algorithm specified\n");
1067 /* check for modlen capability */
1068 cap_idx.type = modex_xform.xform_type;
1069 capability = rte_cryptodev_asym_capability_get(dev_id, &cap_idx);
1071 if (rte_cryptodev_asym_xform_capability_check_modlen(
1072 capability, modex_xform.modex.modulus.length)) {
1074 "Invalid MODULOUS length specified\n");
1078 /* generate crypto op data structure */
1079 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1082 "line %u FAILED: %s",
1083 __LINE__, "Failed to allocate asymmetric crypto "
1084 "operation struct");
1085 status = TEST_FAILED;
1089 sess = rte_cryptodev_asym_session_create(sess_mpool);
1093 "FAILED: %s", __LINE__,
1094 "Session creation failed");
1095 status = TEST_FAILED;
1099 if (rte_cryptodev_asym_session_init(dev_id, sess, &modex_xform,
1102 "line %u FAILED: %s",
1103 __LINE__, "unabled to config sym session");
1104 status = TEST_FAILED;
1109 memcpy(input, base, sizeof(base));
1110 asym_op->modex.base.data = input;
1111 asym_op->modex.base.length = sizeof(base);
1112 /* attach asymmetric crypto session to crypto operations */
1113 rte_crypto_op_attach_asym_session(op, sess);
1115 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1116 /* Process crypto operation */
1117 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1119 "line %u FAILED: %s",
1120 __LINE__, "Error sending packet for operation");
1121 status = TEST_FAILED;
1125 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1128 if (result_op == NULL) {
1130 "line %u FAILED: %s",
1131 __LINE__, "Failed to process asym crypto op");
1132 status = TEST_FAILED;
1136 ret = verify_modexp(mod_exp, result_op);
1139 "operation verification failed\n");
1140 status = TEST_FAILED;
1145 rte_cryptodev_asym_session_clear(dev_id, sess);
1146 rte_cryptodev_asym_session_free(sess);
1150 rte_crypto_op_free(op);
1152 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1158 test_dh_keygenration(void)
1162 debug_hexdump(stdout, "p:", dh_xform.dh.p.data, dh_xform.dh.p.length);
1163 debug_hexdump(stdout, "g:", dh_xform.dh.g.data, dh_xform.dh.g.length);
1164 debug_hexdump(stdout, "priv_key:", dh_test_params.priv_key.data,
1165 dh_test_params.priv_key.length);
1167 RTE_LOG(INFO, USER1,
1168 "Test Public and Private key pair generation\n");
1170 status = test_dh_gen_kp(&dh_xform);
1171 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1173 RTE_LOG(INFO, USER1,
1174 "Test Public Key Generation using pre-defined priv key\n");
1176 status = test_dh_gen_pub_key(&dh_xform);
1177 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1179 RTE_LOG(INFO, USER1,
1180 "Test Private Key Generation only\n");
1182 status = test_dh_gen_priv_key(&dh_xform);
1183 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1185 RTE_LOG(INFO, USER1,
1186 "Test shared secret compute\n");
1188 status = test_dh_gen_shared_sec(&dh_xform);
1189 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1197 struct crypto_testsuite_params *ts_params = &testsuite_params;
1198 struct rte_mempool *op_mpool = ts_params->op_mpool;
1199 struct rte_mempool *sess_mpool = ts_params->session_mpool;
1200 uint8_t dev_id = ts_params->valid_devs[0];
1201 struct rte_crypto_asym_op *asym_op = NULL;
1202 struct rte_crypto_op *op = NULL, *result_op = NULL;
1203 struct rte_cryptodev_asym_session *sess = NULL;
1204 int status = TEST_SUCCESS;
1205 uint8_t r[TEST_DH_MOD_LEN];
1206 uint8_t s[TEST_DH_MOD_LEN];
1207 uint8_t dgst[] = "35d81554afaad2cf18f3a1770d5fedc4ea5be344";
1209 sess = rte_cryptodev_asym_session_create(sess_mpool);
1212 "line %u FAILED: %s", __LINE__,
1213 "Session creation failed");
1214 status = TEST_FAILED;
1217 /* set up crypto op data structure */
1218 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1221 "line %u FAILED: %s",
1222 __LINE__, "Failed to allocate asymmetric crypto "
1223 "operation struct");
1224 status = TEST_FAILED;
1229 debug_hexdump(stdout, "p: ", dsa_xform.dsa.p.data,
1230 dsa_xform.dsa.p.length);
1231 debug_hexdump(stdout, "q: ", dsa_xform.dsa.q.data,
1232 dsa_xform.dsa.q.length);
1233 debug_hexdump(stdout, "g: ", dsa_xform.dsa.g.data,
1234 dsa_xform.dsa.g.length);
1235 debug_hexdump(stdout, "priv_key: ", dsa_xform.dsa.x.data,
1236 dsa_xform.dsa.x.length);
1238 if (rte_cryptodev_asym_session_init(dev_id, sess, &dsa_xform,
1241 "line %u FAILED: %s",
1242 __LINE__, "unabled to config sym session");
1243 status = TEST_FAILED;
1247 /* attach asymmetric crypto session to crypto operations */
1248 rte_crypto_op_attach_asym_session(op, sess);
1249 asym_op->dsa.op_type = RTE_CRYPTO_ASYM_OP_SIGN;
1250 asym_op->dsa.message.data = dgst;
1251 asym_op->dsa.message.length = sizeof(dgst);
1252 asym_op->dsa.r.length = sizeof(r);
1253 asym_op->dsa.r.data = r;
1254 asym_op->dsa.s.length = sizeof(s);
1255 asym_op->dsa.s.data = s;
1257 RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1259 /* Process crypto operation */
1260 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1262 "line %u FAILED: %s",
1263 __LINE__, "Error sending packet for operation");
1264 status = TEST_FAILED;
1268 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1271 if (result_op == NULL) {
1273 "line %u FAILED: %s",
1274 __LINE__, "Failed to process asym crypto op");
1275 status = TEST_FAILED;
1279 asym_op = result_op->asym;
1281 debug_hexdump(stdout, "r:",
1282 asym_op->dsa.r.data, asym_op->dsa.r.length);
1283 debug_hexdump(stdout, "s:",
1284 asym_op->dsa.s.data, asym_op->dsa.s.length);
1286 /* Test PMD DSA sign verification using signer public key */
1287 asym_op->dsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
1289 /* copy signer public key */
1290 asym_op->dsa.y.data = dsa_test_params.y.data;
1291 asym_op->dsa.y.length = dsa_test_params.y.length;
1293 /* Process crypto operation */
1294 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1296 "line %u FAILED: %s",
1297 __LINE__, "Error sending packet for operation");
1298 status = TEST_FAILED;
1302 while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1305 if (result_op == NULL) {
1307 "line %u FAILED: %s",
1308 __LINE__, "Failed to process asym crypto op");
1309 status = TEST_FAILED;
1313 if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
1315 "line %u FAILED: %s",
1316 __LINE__, "Failed to process asym crypto op");
1317 status = TEST_FAILED;
1321 rte_cryptodev_asym_session_clear(dev_id, sess);
1322 rte_cryptodev_asym_session_free(sess);
1325 rte_crypto_op_free(op);
1333 status = test_dsa_sign();
1334 TEST_ASSERT_EQUAL(status, 0, "Test failed");
1339 static struct unit_test_suite cryptodev_openssl_asym_testsuite = {
1340 .suite_name = "Crypto Device OPENSSL ASYM Unit Test Suite",
1341 .setup = testsuite_setup,
1342 .teardown = testsuite_teardown,
1343 .unit_test_cases = {
1344 TEST_CASE_ST(ut_setup, ut_teardown, test_capability),
1345 TEST_CASE_ST(ut_setup, ut_teardown, test_dsa),
1346 TEST_CASE_ST(ut_setup, ut_teardown, test_dh_keygenration),
1347 TEST_CASE_ST(ut_setup, ut_teardown, test_rsa_enc_dec),
1348 TEST_CASE_ST(ut_setup, ut_teardown, test_rsa_sign_verify),
1349 TEST_CASE_ST(ut_setup, ut_teardown, test_mod_inv),
1350 TEST_CASE_ST(ut_setup, ut_teardown, test_mod_exp),
1351 TEST_CASES_END() /**< NULL terminate unit test array */
1356 test_cryptodev_openssl_asym(void)
1358 gbl_driver_id = rte_cryptodev_driver_id_get(
1359 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
1361 if (gbl_driver_id == -1) {
1362 RTE_LOG(ERR, USER1, "OPENSSL PMD must be loaded. Check if "
1363 "CONFIG_RTE_LIBRTE_PMD_OPENSSL is enabled "
1364 "in config file to run this testsuite.\n");
1368 return unit_test_suite_runner(&cryptodev_openssl_asym_testsuite);
1371 REGISTER_TEST_COMMAND(cryptodev_openssl_asym_autotest,
1372 test_cryptodev_openssl_asym);