test: add result field to mod exp and inv
[dpdk.git] / app / test / test_cryptodev_asym.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Cavium Networks
3  */
4
5 #include <rte_bus_vdev.h>
6 #include <rte_common.h>
7 #include <rte_hexdump.h>
8 #include <rte_mbuf.h>
9 #include <rte_malloc.h>
10 #include <rte_memcpy.h>
11 #include <rte_pause.h>
12
13 #include <rte_cryptodev.h>
14 #include <rte_cryptodev_pmd.h>
15 #include <rte_crypto.h>
16
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"
23 #include "test.h"
24
25 #define TEST_NUM_BUFS 10
26 #define TEST_NUM_SESSIONS 4
27
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;
36 };
37
38 struct crypto_unittest_params {
39         struct rte_cryptodev_asym_session *sess;
40         struct rte_crypto_op *op;
41 };
42
43 static struct crypto_testsuite_params testsuite_params = { NULL };
44
45 static int
46 test_rsa_sign_verify(void)
47 {
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};
58
59         sess = rte_cryptodev_asym_session_create(sess_mpool);
60
61         if (!sess) {
62                 RTE_LOG(ERR, USER1, "line %u "
63                                 "FAILED: %s", __LINE__,
64                                 "Session creation failed");
65                 status = TEST_FAILED;
66                 goto error_exit;
67         }
68
69         if (rte_cryptodev_asym_session_init(dev_id, sess, &rsa_xform,
70                                 sess_mpool) < 0) {
71                 RTE_LOG(ERR, USER1,
72                                 "line %u FAILED: %s",
73                                 __LINE__, "unabled to config sym session");
74                 status = TEST_FAILED;
75                 goto error_exit;
76         }
77
78         /* set up crypto op data structure */
79         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
80         if (!op) {
81                 RTE_LOG(ERR, USER1,
82                                 "line %u FAILED: %s",
83                                 __LINE__,
84                                 "Failed to allocate asymmetric crypto "
85                                 "operation struct");
86                 status = TEST_FAILED;
87                 goto error_exit;
88         }
89
90         asym_op = op->asym;
91         /* Compute sign on the test vector */
92         asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_SIGN;
93
94         memcpy(input_buf, &rsaplaintext.data,
95                         rsaplaintext.len);
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;
100
101         debug_hexdump(stdout, "message", asym_op->rsa.message.data,
102                         asym_op->rsa.message.length);
103
104         /* attach asymmetric crypto session to crypto operations */
105         rte_crypto_op_attach_asym_session(op, sess);
106
107         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
108
109         /* Process crypto operation */
110         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
111                 RTE_LOG(ERR, USER1,
112                                 "line %u FAILED: %s",
113                                 __LINE__, "Error sending packet for operation");
114                 status = TEST_FAILED;
115                 goto error_exit;
116         }
117
118         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
119                 rte_pause();
120
121         if (result_op == NULL) {
122                 RTE_LOG(ERR, USER1,
123                                 "line %u FAILED: %s",
124                                 __LINE__, "Failed to process asym crypto op");
125                 status = TEST_FAILED;
126                 goto error_exit;
127         }
128         debug_hexdump(stdout, "signed message", asym_op->rsa.sign.data,
129                         asym_op->rsa.sign.length);
130         asym_op = result_op->asym;
131
132         /* Verify sign */
133         asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
134         asym_op->rsa.pad = RTE_CRYPTO_RSA_PKCS1_V1_5_BT2;
135
136         /* Process crypto operation */
137         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
138                 RTE_LOG(ERR, USER1,
139                                 "line %u FAILED: %s",
140                                 __LINE__, "Error sending packet for operation");
141                 status = TEST_FAILED;
142                 goto error_exit;
143         }
144
145         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
146                 rte_pause();
147
148         if (result_op == NULL) {
149                 RTE_LOG(ERR, USER1,
150                                 "line %u FAILED: %s",
151                                 __LINE__, "Failed to process asym crypto op");
152                 status = TEST_FAILED;
153                 goto error_exit;
154         }
155         status = TEST_SUCCESS;
156         if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
157                 RTE_LOG(ERR, USER1,
158                                 "line %u FAILED: %s",
159                                 __LINE__, "Failed to process asym crypto op");
160                 status = TEST_FAILED;
161                 goto error_exit;
162         }
163
164 error_exit:
165
166         if (sess) {
167                 rte_cryptodev_asym_session_clear(dev_id, sess);
168                 rte_cryptodev_asym_session_free(sess);
169         }
170
171         if (op)
172                 rte_crypto_op_free(op);
173
174         TEST_ASSERT_EQUAL(status, 0, "Test failed");
175
176         return status;
177 }
178
179 static int
180 test_rsa_enc_dec(void)
181 {
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};
191
192         sess = rte_cryptodev_asym_session_create(sess_mpool);
193
194         if (!sess) {
195                 RTE_LOG(ERR, USER1, "line %u "
196                                 "FAILED: %s", __LINE__,
197                                 "Session creation failed");
198                 status = TEST_FAILED;
199                 goto error_exit;
200         }
201
202         if (rte_cryptodev_asym_session_init(dev_id, sess, &rsa_xform,
203                                 sess_mpool) < 0) {
204                 RTE_LOG(ERR, USER1,
205                                 "line %u FAILED: %s",
206                                 __LINE__, "unabled to config sym session");
207                 status = TEST_FAILED;
208                 goto error_exit;
209         }
210
211         /* set up crypto op data structure */
212         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
213         if (!op) {
214                 RTE_LOG(ERR, USER1,
215                                 "line %u FAILED: %s",
216                                 __LINE__,
217                                 "Failed to allocate asymmetric crypto "
218                                 "operation struct");
219                 status = TEST_FAILED;
220                 goto error_exit;
221         }
222
223         asym_op = op->asym;
224         /*Compute encryption on the test vector */
225         asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_ENCRYPT;
226
227         memcpy(input_buf, rsaplaintext.data,
228                         rsaplaintext.len);
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;
232
233         debug_hexdump(stdout, "message", asym_op->rsa.message.data,
234                         asym_op->rsa.message.length);
235
236         /* attach asymmetric crypto session to crypto operations */
237         rte_crypto_op_attach_asym_session(op, sess);
238
239         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
240
241         /* Process crypto operation */
242         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
243                 RTE_LOG(ERR, USER1,
244                                 "line %u FAILED: %s",
245                                 __LINE__, "Error sending packet for operation");
246                 status = TEST_FAILED;
247                 goto error_exit;
248         }
249
250         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
251                 rte_pause();
252
253         if (result_op == NULL) {
254                 RTE_LOG(ERR, USER1,
255                                 "line %u FAILED: %s",
256                                 __LINE__, "Failed to process asym crypto op");
257                 status = TEST_FAILED;
258                 goto error_exit;
259         }
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;
266
267         /* Process crypto operation */
268         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
269                 RTE_LOG(ERR, USER1,
270                                 "line %u FAILED: %s",
271                                 __LINE__, "Error sending packet for operation");
272                 status = TEST_FAILED;
273                 goto error_exit;
274         }
275
276         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
277                 rte_pause();
278
279         if (result_op == NULL) {
280                 RTE_LOG(ERR, USER1,
281                                 "line %u FAILED: %s",
282                                 __LINE__, "Failed to process asym crypto op");
283                 status = TEST_FAILED;
284                 goto error_exit;
285         }
286         status = TEST_SUCCESS;
287         int ret = 0;
288         ret = rsa_verify(&rsaplaintext, result_op);
289         if (ret)
290                 status = TEST_FAILED;
291
292 error_exit:
293
294         if (sess) {
295                 rte_cryptodev_asym_session_clear(dev_id, sess);
296                 rte_cryptodev_asym_session_free(sess);
297         }
298
299         if (op)
300                 rte_crypto_op_free(op);
301
302         TEST_ASSERT_EQUAL(status, 0, "Test failed");
303
304         return status;
305 }
306
307 static int
308 testsuite_setup(void)
309 {
310         struct crypto_testsuite_params *ts_params = &testsuite_params;
311         struct rte_cryptodev_info info;
312         uint32_t i = 0, nb_devs, dev_id;
313         int ret;
314         uint16_t qp_id;
315
316         memset(ts_params, 0, sizeof(*ts_params));
317
318         ts_params->op_mpool = rte_crypto_op_pool_create(
319                         "CRYPTO_ASYM_OP_POOL",
320                         RTE_CRYPTO_OP_TYPE_ASYMMETRIC,
321                         TEST_NUM_BUFS, 0,
322                         0,
323                         rte_socket_id());
324         if (ts_params->op_mpool == NULL) {
325                 RTE_LOG(ERR, USER1, "Can't create ASYM_CRYPTO_OP_POOL\n");
326                 return TEST_FAILED;
327         }
328
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)));
335                 if (nb_devs < 1) {
336                         ret = rte_vdev_init(
337                                 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD),
338                                 NULL);
339
340                         TEST_ASSERT(ret == 0, "Failed to create "
341                                 "instance of pmd : %s",
342                                 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
343                 }
344         }
345
346         nb_devs = rte_cryptodev_count();
347         if (nb_devs < 1) {
348                 RTE_LOG(ERR, USER1, "No crypto devices found?\n");
349                 return TEST_FAILED;
350         }
351
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;
357         }
358
359         if (ts_params->valid_dev_count < 1)
360                 return TEST_FAILED;
361
362         /* Set up all the qps on the first of the valid devices found */
363
364         dev_id = ts_params->valid_devs[0];
365
366         rte_cryptodev_info_get(dev_id, &info);
367
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. "
372                                 "Test Skipped.\n");
373                 return TEST_FAILED;
374         }
375
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,
380                         &ts_params->conf),
381                         "Failed to configure cryptodev %u with %u qps",
382                         dev_id, ts_params->conf.nb_queue_pairs);
383
384         /* configure qp */
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",
393                         qp_id, dev_id);
394         }
395
396         /* setup asym session pool */
397         unsigned int session_size =
398                 rte_cryptodev_asym_get_private_session_size(dev_id);
399         /*
400          * Create mempool with TEST_NUM_SESSIONS * 2,
401          * to include the session headers
402          */
403         ts_params->session_mpool = rte_mempool_create(
404                                 "test_asym_sess_mp",
405                                 TEST_NUM_SESSIONS * 2,
406                                 session_size,
407                                 0, 0, NULL, NULL, NULL,
408                                 NULL, SOCKET_ID_ANY,
409                                 0);
410
411         TEST_ASSERT_NOT_NULL(ts_params->session_mpool,
412                         "session mempool allocation failed");
413
414         return TEST_SUCCESS;
415 }
416
417 static void
418 testsuite_teardown(void)
419 {
420         struct crypto_testsuite_params *ts_params = &testsuite_params;
421
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));
425         }
426
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;
431         }
432 }
433
434 static int
435 ut_setup(void)
436 {
437         struct crypto_testsuite_params *ts_params = &testsuite_params;
438
439         uint16_t qp_id;
440
441         /* Reconfigure device to default parameters */
442         ts_params->conf.socket_id = SOCKET_ID_ANY;
443
444         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(ts_params->valid_devs[0],
445                         &ts_params->conf),
446                         "Failed to configure cryptodev %u",
447                         ts_params->valid_devs[0]);
448
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,
452                         &ts_params->qp_conf,
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]);
456         }
457
458         rte_cryptodev_stats_reset(ts_params->valid_devs[0]);
459
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]);
464
465         return TEST_SUCCESS;
466 }
467
468 static void
469 ut_teardown(void)
470 {
471         struct crypto_testsuite_params *ts_params = &testsuite_params;
472         struct rte_cryptodev_stats stats;
473
474         rte_cryptodev_stats_get(ts_params->valid_devs[0], &stats);
475
476         /* Stop the device */
477         rte_cryptodev_stop(ts_params->valid_devs[0]);
478 }
479
480 static inline void print_asym_capa(
481                 const struct rte_cryptodev_asymmetric_xform_capability *capa)
482 {
483         int i = 0;
484
485         printf("\nxform type: %s\n===================\n",
486                         rte_crypto_asym_xform_strings[capa->xform_type]);
487         printf("operation supported -");
488
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))
492                         printf(" %s",
493                                         rte_crypto_asym_op_strings[i]);
494                 }
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",
502                                         capa->modlen.min,
503                                         capa->modlen.max,
504                                         capa->modlen.increment);
505                 break;
506                 default:
507                         break;
508                 }
509 }
510
511 static int
512 test_capability(void)
513 {
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;
518         int i = 0;
519         struct rte_cryptodev_asym_capability_idx idx;
520         const struct rte_cryptodev_asymmetric_xform_capability *capa;
521
522         rte_cryptodev_info_get(dev_id, &dev_info);
523         if (!(dev_info.feature_flags &
524                                 RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)) {
525                 RTE_LOG(INFO, USER1,
526                                 "Device doesn't support asymmetric. Test Skipped\n");
527                 return TEST_SUCCESS;
528         }
529
530         /* print xform capability */
531         for (i = 0;
532                 dev_info.capabilities[i].op != RTE_CRYPTO_OP_TYPE_UNDEFINED;
533                 i++) {
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;
538
539                         capa = rte_cryptodev_asym_capability_get(dev_id,
540                                 (const struct
541                                 rte_cryptodev_asym_capability_idx *) &idx);
542                         print_asym_capa(capa);
543                         }
544         }
545         return TEST_SUCCESS;
546 }
547
548 static int
549 test_dh_gen_shared_sec(struct rte_crypto_asym_xform *xfrm)
550 {
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";
562
563         sess = rte_cryptodev_asym_session_create(sess_mpool);
564         if (sess == NULL) {
565                 RTE_LOG(ERR, USER1,
566                                 "line %u FAILED: %s", __LINE__,
567                                 "Session creation failed");
568                 status = TEST_FAILED;
569                 goto error_exit;
570         }
571         /* set up crypto op data structure */
572         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
573         if (!op) {
574                 RTE_LOG(ERR, USER1,
575                         "line %u FAILED: %s",
576                         __LINE__, "Failed to allocate asymmetric crypto "
577                         "operation struct");
578                 status = TEST_FAILED;
579                 goto error_exit;
580         }
581         asym_op = op->asym;
582
583         /* Setup a xform and op to generate private key only */
584         xform.dh.type = RTE_CRYPTO_ASYM_OP_SHARED_SECRET_COMPUTE;
585         xform.next = NULL;
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);
592
593         if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
594                         sess_mpool) < 0) {
595                 RTE_LOG(ERR, USER1,
596                                 "line %u FAILED: %s",
597                                 __LINE__, "unabled to config sym session");
598                 status = TEST_FAILED;
599                 goto error_exit;
600         }
601
602         /* attach asymmetric crypto session to crypto operations */
603         rte_crypto_op_attach_asym_session(op, sess);
604
605         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
606
607         /* Process crypto operation */
608         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
609                 RTE_LOG(ERR, USER1,
610                         "line %u FAILED: %s",
611                         __LINE__, "Error sending packet for operation");
612                 status = TEST_FAILED;
613                 goto error_exit;
614         }
615
616         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
617                 rte_pause();
618
619         if (result_op == NULL) {
620                 RTE_LOG(ERR, USER1,
621                         "line %u FAILED: %s",
622                         __LINE__, "Failed to process asym crypto op");
623                 status = TEST_FAILED;
624                 goto error_exit;
625         }
626
627         debug_hexdump(stdout, "shared secret:",
628                         asym_op->dh.shared_secret.data,
629                         asym_op->dh.shared_secret.length);
630
631 error_exit:
632         if (sess != NULL) {
633                 rte_cryptodev_asym_session_clear(dev_id, sess);
634                 rte_cryptodev_asym_session_free(sess);
635         }
636         if (op != NULL)
637                 rte_crypto_op_free(op);
638         return status;
639 }
640
641 static int
642 test_dh_gen_priv_key(struct rte_crypto_asym_xform *xfrm)
643 {
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;
654
655         sess = rte_cryptodev_asym_session_create(sess_mpool);
656         if (sess == NULL) {
657                 RTE_LOG(ERR, USER1,
658                                  "line %u FAILED: %s", __LINE__,
659                                 "Session creation failed");
660                 status = TEST_FAILED;
661                 goto error_exit;
662         }
663         /* set up crypto op data structure */
664         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
665         if (!op) {
666                 RTE_LOG(ERR, USER1,
667                         "line %u FAILED: %s",
668                         __LINE__, "Failed to allocate asymmetric crypto "
669                         "operation struct");
670                 status = TEST_FAILED;
671                 goto error_exit;
672         }
673         asym_op = op->asym;
674
675         /* Setup a xform and op to generate private key only */
676         xform.dh.type = RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE;
677         xform.next = NULL;
678         asym_op->dh.priv_key.data = output;
679         asym_op->dh.priv_key.length = sizeof(output);
680
681         if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
682                         sess_mpool) < 0) {
683                 RTE_LOG(ERR, USER1,
684                                 "line %u FAILED: %s",
685                                 __LINE__, "unabled to config sym session");
686                 status = TEST_FAILED;
687                 goto error_exit;
688         }
689
690         /* attach asymmetric crypto session to crypto operations */
691         rte_crypto_op_attach_asym_session(op, sess);
692
693         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
694
695         /* Process crypto operation */
696         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
697                 RTE_LOG(ERR, USER1,
698                         "line %u FAILED: %s",
699                         __LINE__, "Error sending packet for operation");
700                 status = TEST_FAILED;
701                 goto error_exit;
702         }
703
704         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
705                 rte_pause();
706
707         if (result_op == NULL) {
708                 RTE_LOG(ERR, USER1,
709                         "line %u FAILED: %s",
710                         __LINE__, "Failed to process asym crypto op");
711                 status = TEST_FAILED;
712                 goto error_exit;
713         }
714
715         debug_hexdump(stdout, "private key:",
716                         asym_op->dh.priv_key.data,
717                         asym_op->dh.priv_key.length);
718
719
720 error_exit:
721         if (sess != NULL) {
722                 rte_cryptodev_asym_session_clear(dev_id, sess);
723                 rte_cryptodev_asym_session_free(sess);
724         }
725         if (op != NULL)
726                 rte_crypto_op_free(op);
727
728         return status;
729 }
730
731
732 static int
733 test_dh_gen_pub_key(struct rte_crypto_asym_xform *xfrm)
734 {
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;
745
746         sess = rte_cryptodev_asym_session_create(sess_mpool);
747         if (sess == NULL) {
748                 RTE_LOG(ERR, USER1,
749                                  "line %u FAILED: %s", __LINE__,
750                                 "Session creation failed");
751                 status = TEST_FAILED;
752                 goto error_exit;
753         }
754         /* set up crypto op data structure */
755         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
756         if (!op) {
757                 RTE_LOG(ERR, USER1,
758                         "line %u FAILED: %s",
759                         __LINE__, "Failed to allocate asymmetric crypto "
760                         "operation struct");
761                 status = TEST_FAILED;
762                 goto error_exit;
763         }
764         asym_op = op->asym;
765         /* Setup a xform chain to generate public key
766          * using test private key
767          *
768          */
769         xform.dh.type = RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE;
770         xform.next = NULL;
771
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,
777                                         0);
778         asym_op->dh.priv_key = dh_test_params.priv_key;
779
780         if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
781                         sess_mpool) < 0) {
782                 RTE_LOG(ERR, USER1,
783                                 "line %u FAILED: %s",
784                                 __LINE__, "unabled to config sym session");
785                 status = TEST_FAILED;
786                 goto error_exit;
787         }
788
789         /* attach asymmetric crypto session to crypto operations */
790         rte_crypto_op_attach_asym_session(op, sess);
791
792         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
793
794         /* Process crypto operation */
795         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
796                 RTE_LOG(ERR, USER1,
797                         "line %u FAILED: %s",
798                         __LINE__, "Error sending packet for operation");
799                 status = TEST_FAILED;
800                 goto error_exit;
801         }
802
803         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
804                 rte_pause();
805
806         if (result_op == NULL) {
807                 RTE_LOG(ERR, USER1,
808                         "line %u FAILED: %s",
809                         __LINE__, "Failed to process asym crypto op");
810                 status = TEST_FAILED;
811                 goto error_exit;
812         }
813
814         debug_hexdump(stdout, "pub key:",
815                         asym_op->dh.pub_key.data, asym_op->dh.pub_key.length);
816
817         debug_hexdump(stdout, "priv key:",
818                         asym_op->dh.priv_key.data, asym_op->dh.priv_key.length);
819
820 error_exit:
821         if (sess != NULL) {
822                 rte_cryptodev_asym_session_clear(dev_id, sess);
823                 rte_cryptodev_asym_session_free(sess);
824         }
825         if (op != NULL)
826                 rte_crypto_op_free(op);
827
828         return status;
829 }
830
831 static int
832 test_dh_gen_kp(struct rte_crypto_asym_xform *xfrm)
833 {
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;
846
847         sess = rte_cryptodev_asym_session_create(sess_mpool);
848         if (sess == NULL) {
849                 RTE_LOG(ERR, USER1,
850                                  "line %u FAILED: %s", __LINE__,
851                                 "Session creation failed");
852                 status = TEST_FAILED;
853                 goto error_exit;
854         }
855
856         /* set up crypto op data structure */
857         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
858         if (!op) {
859                 RTE_LOG(ERR, USER1,
860                         "line %u FAILED: %s",
861                         __LINE__, "Failed to allocate asymmetric crypto "
862                         "operation struct");
863                 status = TEST_FAILED;
864                 goto error_exit;
865         }
866         asym_op = op->asym;
867         /* Setup a xform chain to generate
868          * private key first followed by
869          * public key
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;
874
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,
880                         sess_mpool) < 0) {
881                 RTE_LOG(ERR, USER1,
882                                 "line %u FAILED: %s",
883                                 __LINE__, "unabled to config sym session");
884                 status = TEST_FAILED;
885                 goto error_exit;
886         }
887
888         /* attach asymmetric crypto session to crypto operations */
889         rte_crypto_op_attach_asym_session(op, sess);
890
891         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
892
893         /* Process crypto operation */
894         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
895                 RTE_LOG(ERR, USER1,
896                         "line %u FAILED: %s",
897                         __LINE__, "Error sending packet for operation");
898                 status = TEST_FAILED;
899                 goto error_exit;
900         }
901
902         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
903                 rte_pause();
904
905         if (result_op == NULL) {
906                 RTE_LOG(ERR, USER1,
907                         "line %u FAILED: %s",
908                         __LINE__, "Failed to process asym crypto op");
909                 status = TEST_FAILED;
910                 goto error_exit;
911         }
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);
916
917 error_exit:
918         if (sess != NULL) {
919                 rte_cryptodev_asym_session_clear(dev_id, sess);
920                 rte_cryptodev_asym_session_free(sess);
921         }
922         if (op != NULL)
923                 rte_crypto_op_free(op);
924
925         return status;
926 }
927
928 static int
929 test_mod_inv(void)
930 {
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};
942         int ret = 0;
943         uint8_t result[sizeof(mod_p)] = { 0 };
944
945         if (rte_cryptodev_asym_get_xform_enum(
946                 &modinv_xform.xform_type, "modinv") < 0) {
947                 RTE_LOG(ERR, USER1,
948                                  "Invalid ASYNC algorithm specified\n");
949                 return -1;
950         }
951
952         cap_idx.type = modinv_xform.xform_type;
953         capability = rte_cryptodev_asym_capability_get(dev_id,
954                                         &cap_idx);
955
956         if (rte_cryptodev_asym_xform_capability_check_modlen(
957                 capability,
958                 modinv_xform.modinv.modulus.length)) {
959                 RTE_LOG(ERR, USER1,
960                                  "Invalid MODULOUS length specified\n");
961                                 return -1;
962                 }
963
964         sess = rte_cryptodev_asym_session_create(sess_mpool);
965         if (!sess) {
966                 RTE_LOG(ERR, USER1, "line %u "
967                                 "FAILED: %s", __LINE__,
968                                 "Session creation failed");
969                 status = TEST_FAILED;
970                 goto error_exit;
971         }
972
973         if (rte_cryptodev_asym_session_init(dev_id, sess, &modinv_xform,
974                         sess_mpool) < 0) {
975                 RTE_LOG(ERR, USER1,
976                                 "line %u FAILED: %s",
977                                 __LINE__, "unabled to config sym session");
978                 status = TEST_FAILED;
979                 goto error_exit;
980         }
981
982         /* generate crypto op data structure */
983         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
984         if (!op) {
985                 RTE_LOG(ERR, USER1,
986                         "line %u FAILED: %s",
987                         __LINE__, "Failed to allocate asymmetric crypto "
988                         "operation struct");
989                 status = TEST_FAILED;
990                 goto error_exit;
991         }
992
993         asym_op = op->asym;
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);
999
1000         /* attach asymmetric crypto session to crypto operations */
1001         rte_crypto_op_attach_asym_session(op, sess);
1002
1003         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1004
1005         /* Process crypto operation */
1006         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1007                 RTE_LOG(ERR, USER1,
1008                         "line %u FAILED: %s",
1009                         __LINE__, "Error sending packet for operation");
1010                 status = TEST_FAILED;
1011                 goto error_exit;
1012         }
1013
1014         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1015                 rte_pause();
1016
1017         if (result_op == NULL) {
1018                 RTE_LOG(ERR, USER1,
1019                                 "line %u FAILED: %s",
1020                                 __LINE__, "Failed to process asym crypto op");
1021                 status = TEST_FAILED;
1022                 goto error_exit;
1023         }
1024
1025         ret = verify_modinv(mod_inv, result_op);
1026         if (ret) {
1027                 RTE_LOG(ERR, USER1,
1028                          "operation verification failed\n");
1029                 status = TEST_FAILED;
1030         }
1031
1032 error_exit:
1033         if (sess) {
1034                 rte_cryptodev_asym_session_clear(dev_id, sess);
1035                 rte_cryptodev_asym_session_free(sess);
1036         }
1037
1038         if (op)
1039                 rte_crypto_op_free(op);
1040
1041         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1042
1043         return status;
1044 }
1045
1046 static int
1047 test_mod_exp(void)
1048 {
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};
1060         int ret = 0;
1061         uint8_t result[sizeof(mod_p)] = { 0 };
1062
1063         if (rte_cryptodev_asym_get_xform_enum(&modex_xform.xform_type,
1064                 "modexp")
1065                 < 0) {
1066                 RTE_LOG(ERR, USER1,
1067                                 "Invalid ASYNC algorithm specified\n");
1068                 return -1;
1069         }
1070
1071         /* check for modlen capability */
1072         cap_idx.type = modex_xform.xform_type;
1073         capability = rte_cryptodev_asym_capability_get(dev_id, &cap_idx);
1074
1075         if (rte_cryptodev_asym_xform_capability_check_modlen(
1076                         capability, modex_xform.modex.modulus.length)) {
1077                 RTE_LOG(ERR, USER1,
1078                                 "Invalid MODULOUS length specified\n");
1079                                 return -1;
1080                 }
1081
1082         /* generate crypto op data structure */
1083         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1084         if (!op) {
1085                 RTE_LOG(ERR, USER1,
1086                         "line %u FAILED: %s",
1087                         __LINE__, "Failed to allocate asymmetric crypto "
1088                         "operation struct");
1089                 status = TEST_FAILED;
1090                 goto error_exit;
1091         }
1092
1093         sess = rte_cryptodev_asym_session_create(sess_mpool);
1094         if (!sess) {
1095                 RTE_LOG(ERR, USER1,
1096                                  "line %u "
1097                                 "FAILED: %s", __LINE__,
1098                                 "Session creation failed");
1099                 status = TEST_FAILED;
1100                 goto error_exit;
1101         }
1102
1103         if (rte_cryptodev_asym_session_init(dev_id, sess, &modex_xform,
1104                         sess_mpool) < 0) {
1105                 RTE_LOG(ERR, USER1,
1106                                 "line %u FAILED: %s",
1107                                 __LINE__, "unabled to config sym session");
1108                 status = TEST_FAILED;
1109                 goto error_exit;
1110         }
1111
1112         asym_op = op->asym;
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);
1120
1121         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1122         /* Process crypto operation */
1123         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1124                 RTE_LOG(ERR, USER1,
1125                                 "line %u FAILED: %s",
1126                                 __LINE__, "Error sending packet for operation");
1127                 status = TEST_FAILED;
1128                 goto error_exit;
1129         }
1130
1131         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1132                 rte_pause();
1133
1134         if (result_op == NULL) {
1135                 RTE_LOG(ERR, USER1,
1136                                 "line %u FAILED: %s",
1137                                 __LINE__, "Failed to process asym crypto op");
1138                 status = TEST_FAILED;
1139                 goto error_exit;
1140         }
1141
1142         ret = verify_modexp(mod_exp, result_op);
1143         if (ret) {
1144                 RTE_LOG(ERR, USER1,
1145                          "operation verification failed\n");
1146                 status = TEST_FAILED;
1147         }
1148
1149 error_exit:
1150         if (sess != NULL) {
1151                 rte_cryptodev_asym_session_clear(dev_id, sess);
1152                 rte_cryptodev_asym_session_free(sess);
1153         }
1154
1155         if (op != NULL)
1156                 rte_crypto_op_free(op);
1157
1158         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1159
1160         return status;
1161 }
1162
1163 static int
1164 test_dh_keygenration(void)
1165 {
1166         int status;
1167
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);
1172
1173         RTE_LOG(INFO, USER1,
1174                 "Test Public and Private key pair generation\n");
1175
1176         status = test_dh_gen_kp(&dh_xform);
1177         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1178
1179         RTE_LOG(INFO, USER1,
1180                 "Test Public Key Generation using pre-defined priv key\n");
1181
1182         status = test_dh_gen_pub_key(&dh_xform);
1183         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1184
1185         RTE_LOG(INFO, USER1,
1186                 "Test Private Key Generation only\n");
1187
1188         status = test_dh_gen_priv_key(&dh_xform);
1189         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1190
1191         RTE_LOG(INFO, USER1,
1192                 "Test shared secret compute\n");
1193
1194         status = test_dh_gen_shared_sec(&dh_xform);
1195         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1196
1197         return status;
1198 }
1199
1200 static int
1201 test_dsa_sign(void)
1202 {
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";
1214
1215         sess = rte_cryptodev_asym_session_create(sess_mpool);
1216         if (sess == NULL) {
1217                 RTE_LOG(ERR, USER1,
1218                                  "line %u FAILED: %s", __LINE__,
1219                                 "Session creation failed");
1220                 status = TEST_FAILED;
1221                 goto error_exit;
1222         }
1223         /* set up crypto op data structure */
1224         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1225         if (!op) {
1226                 RTE_LOG(ERR, USER1,
1227                         "line %u FAILED: %s",
1228                         __LINE__, "Failed to allocate asymmetric crypto "
1229                         "operation struct");
1230                 status = TEST_FAILED;
1231                 goto error_exit;
1232         }
1233         asym_op = op->asym;
1234
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);
1243
1244         if (rte_cryptodev_asym_session_init(dev_id, sess, &dsa_xform,
1245                                 sess_mpool) < 0) {
1246                 RTE_LOG(ERR, USER1,
1247                                 "line %u FAILED: %s",
1248                                 __LINE__, "unabled to config sym session");
1249                 status = TEST_FAILED;
1250                 goto error_exit;
1251         }
1252
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;
1262
1263         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1264
1265         /* Process crypto operation */
1266         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1267                 RTE_LOG(ERR, USER1,
1268                         "line %u FAILED: %s",
1269                         __LINE__, "Error sending packet for operation");
1270                 status = TEST_FAILED;
1271                 goto error_exit;
1272         }
1273
1274         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1275                 rte_pause();
1276
1277         if (result_op == NULL) {
1278                 RTE_LOG(ERR, USER1,
1279                         "line %u FAILED: %s",
1280                         __LINE__, "Failed to process asym crypto op");
1281                 status = TEST_FAILED;
1282                 goto error_exit;
1283         }
1284
1285         asym_op = result_op->asym;
1286
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);
1291
1292         /* Test PMD DSA sign verification using signer public key */
1293         asym_op->dsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
1294
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;
1298
1299         /* Process crypto operation */
1300         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1301                 RTE_LOG(ERR, USER1,
1302                         "line %u FAILED: %s",
1303                         __LINE__, "Error sending packet for operation");
1304                 status = TEST_FAILED;
1305                 goto error_exit;
1306         }
1307
1308         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1309                 rte_pause();
1310
1311         if (result_op == NULL) {
1312                 RTE_LOG(ERR, USER1,
1313                         "line %u FAILED: %s",
1314                         __LINE__, "Failed to process asym crypto op");
1315                 status = TEST_FAILED;
1316                 goto error_exit;
1317         }
1318
1319         if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
1320                 RTE_LOG(ERR, USER1,
1321                                 "line %u FAILED: %s",
1322                                 __LINE__, "Failed to process asym crypto op");
1323                 status = TEST_FAILED;
1324         }
1325 error_exit:
1326         if (sess != NULL) {
1327                 rte_cryptodev_asym_session_clear(dev_id, sess);
1328                 rte_cryptodev_asym_session_free(sess);
1329         }
1330         if (op != NULL)
1331                 rte_crypto_op_free(op);
1332         return status;
1333 }
1334
1335 static int
1336 test_dsa(void)
1337 {
1338         int status;
1339         status = test_dsa_sign();
1340         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1341         return status;
1342 }
1343
1344
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 */
1358         }
1359 };
1360
1361 static int
1362 test_cryptodev_openssl_asym(void)
1363 {
1364         gbl_driver_id = rte_cryptodev_driver_id_get(
1365                         RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
1366
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");
1371                 return TEST_FAILED;
1372         }
1373
1374         return unit_test_suite_runner(&cryptodev_openssl_asym_testsuite);
1375 }
1376
1377 REGISTER_TEST_COMMAND(cryptodev_openssl_asym_autotest,
1378                                           test_cryptodev_openssl_asym);