ea3005b25d26b30136c8267d8847a7f994283638
[dpdk.git] / app / test / test_cryptodev_asym.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Cavium Networks
3  * Copyright (c) 2019 Intel Corporation
4  */
5
6 #include <rte_bus_vdev.h>
7 #include <rte_common.h>
8 #include <rte_hexdump.h>
9 #include <rte_mbuf.h>
10 #include <rte_malloc.h>
11 #include <rte_memcpy.h>
12 #include <rte_pause.h>
13
14 #include <rte_cryptodev.h>
15 #include <rte_cryptodev_pmd.h>
16 #include <rte_crypto.h>
17
18 #include "test_cryptodev.h"
19 #include "test_cryptodev_dh_test_vectors.h"
20 #include "test_cryptodev_dsa_test_vectors.h"
21 #include "test_cryptodev_mod_test_vectors.h"
22 #include "test_cryptodev_rsa_test_vectors.h"
23 #include "test_cryptodev_asym_util.h"
24 #include "test.h"
25
26 #define TEST_NUM_BUFS 10
27 #define TEST_NUM_SESSIONS 4
28
29 #ifndef ARRAY_SIZE
30 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
31 #endif
32
33 #ifndef TEST_DATA_SIZE
34         #define TEST_DATA_SIZE 4096
35 #endif
36 #define ASYM_TEST_MSG_LEN 256
37 #define TEST_VECTOR_SIZE 256
38
39 static int gbl_driver_id;
40 struct crypto_testsuite_params {
41         struct rte_mempool *op_mpool;
42         struct rte_mempool *session_mpool;
43         struct rte_cryptodev_config conf;
44         struct rte_cryptodev_qp_conf qp_conf;
45         uint8_t valid_devs[RTE_CRYPTO_MAX_DEVS];
46         uint8_t valid_dev_count;
47 };
48
49 struct crypto_unittest_params {
50         struct rte_cryptodev_asym_session *sess;
51         struct rte_crypto_op *op;
52 };
53
54 union test_case_structure {
55         struct modex_test_data modex;
56         struct modinv_test_data modinv;
57 };
58
59 struct test_cases_array {
60         uint32_t size;
61         const void *address[TEST_VECTOR_SIZE];
62 };
63 static struct test_cases_array test_vector = {0, { NULL } };
64
65 static uint32_t test_index;
66
67 static struct crypto_testsuite_params testsuite_params = { NULL };
68
69 static int
70 queue_ops_rsa_sign_verify(struct rte_cryptodev_asym_session *sess)
71 {
72         struct crypto_testsuite_params *ts_params = &testsuite_params;
73         struct rte_mempool *op_mpool = ts_params->op_mpool;
74         uint8_t dev_id = ts_params->valid_devs[0];
75         struct rte_crypto_op *op, *result_op;
76         struct rte_crypto_asym_op *asym_op;
77         uint8_t output_buf[TEST_DATA_SIZE];
78         int status = TEST_SUCCESS;
79
80         /* Set up crypto op data structure */
81         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
82         if (!op) {
83                 RTE_LOG(ERR, USER1, "Failed to allocate asymmetric crypto "
84                         "operation struct\n");
85                 return TEST_FAILED;
86         }
87
88         asym_op = op->asym;
89
90         /* Compute sign on the test vector */
91         asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_SIGN;
92
93         asym_op->rsa.message.data = rsaplaintext.data;
94         asym_op->rsa.message.length = rsaplaintext.len;
95         asym_op->rsa.sign.length = 0;
96         asym_op->rsa.sign.data = output_buf;
97         asym_op->rsa.pad = RTE_CRYPTO_RSA_PADDING_PKCS1_5;
98
99         debug_hexdump(stdout, "message", asym_op->rsa.message.data,
100                       asym_op->rsa.message.length);
101
102         /* Attach asymmetric crypto session to crypto operations */
103         rte_crypto_op_attach_asym_session(op, sess);
104
105         RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
106
107         /* Process crypto operation */
108         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
109                 RTE_LOG(ERR, USER1, "Error sending packet for sign\n");
110                 status = TEST_FAILED;
111                 goto error_exit;
112         }
113
114         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
115                 rte_pause();
116
117         if (result_op == NULL) {
118                 RTE_LOG(ERR, USER1, "Failed to process sign op\n");
119                 status = TEST_FAILED;
120                 goto error_exit;
121         }
122
123         debug_hexdump(stdout, "signed message", asym_op->rsa.sign.data,
124                       asym_op->rsa.sign.length);
125         asym_op = result_op->asym;
126
127         /* Verify sign */
128         asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
129         asym_op->rsa.pad = RTE_CRYPTO_RSA_PADDING_PKCS1_5;
130
131         /* Process crypto operation */
132         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
133                 RTE_LOG(ERR, USER1, "Error sending packet for verify\n");
134                 status = TEST_FAILED;
135                 goto error_exit;
136         }
137
138         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
139                 rte_pause();
140
141         if (result_op == NULL) {
142                 RTE_LOG(ERR, USER1, "Failed to process verify op\n");
143                 status = TEST_FAILED;
144                 goto error_exit;
145         }
146
147         status = TEST_SUCCESS;
148         if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
149                 RTE_LOG(ERR, USER1, "Failed to process sign-verify op\n");
150                 status = TEST_FAILED;
151         }
152
153 error_exit:
154
155         rte_crypto_op_free(op);
156
157         return status;
158 }
159
160 static int
161 queue_ops_rsa_enc_dec(struct rte_cryptodev_asym_session *sess)
162 {
163         struct crypto_testsuite_params *ts_params = &testsuite_params;
164         struct rte_mempool *op_mpool = ts_params->op_mpool;
165         uint8_t dev_id = ts_params->valid_devs[0];
166         struct rte_crypto_op *op, *result_op;
167         struct rte_crypto_asym_op *asym_op;
168         uint8_t cipher_buf[TEST_DATA_SIZE] = {0};
169         int ret, status = TEST_SUCCESS;
170
171         /* Set up crypto op data structure */
172         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
173         if (!op) {
174                 RTE_LOG(ERR, USER1, "Failed to allocate asymmetric crypto "
175                         "operation struct\n");
176                 return TEST_FAILED;
177         }
178
179         asym_op = op->asym;
180
181         /* Compute encryption on the test vector */
182         asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_ENCRYPT;
183
184         asym_op->rsa.message.data = rsaplaintext.data;
185         asym_op->rsa.cipher.data = cipher_buf;
186         asym_op->rsa.cipher.length = 0;
187         asym_op->rsa.message.length = rsaplaintext.len;
188         asym_op->rsa.pad = RTE_CRYPTO_RSA_PADDING_PKCS1_5;
189
190         debug_hexdump(stdout, "message", asym_op->rsa.message.data,
191                       asym_op->rsa.message.length);
192
193         /* Attach asymmetric crypto session to crypto operations */
194         rte_crypto_op_attach_asym_session(op, sess);
195
196         RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
197
198         /* Process crypto operation */
199         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
200                 RTE_LOG(ERR, USER1, "Error sending packet for encryption\n");
201                 status = TEST_FAILED;
202                 goto error_exit;
203         }
204
205         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
206                 rte_pause();
207
208         if (result_op == NULL) {
209                 RTE_LOG(ERR, USER1, "Failed to process encryption op\n");
210                 status = TEST_FAILED;
211                 goto error_exit;
212         }
213         debug_hexdump(stdout, "encrypted message", asym_op->rsa.message.data,
214                       asym_op->rsa.message.length);
215
216         /* Use the resulted output as decryption Input vector*/
217         asym_op = result_op->asym;
218         asym_op->rsa.message.length = 0;
219         asym_op->rsa.op_type = RTE_CRYPTO_ASYM_OP_DECRYPT;
220         asym_op->rsa.pad = RTE_CRYPTO_RSA_PADDING_PKCS1_5;
221
222         /* Process crypto operation */
223         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
224                 RTE_LOG(ERR, USER1, "Error sending packet for decryption\n");
225                 status = TEST_FAILED;
226                 goto error_exit;
227         }
228
229         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
230                 rte_pause();
231
232         if (result_op == NULL) {
233                 RTE_LOG(ERR, USER1, "Failed to process decryption op\n");
234                 status = TEST_FAILED;
235                 goto error_exit;
236         }
237         status = TEST_SUCCESS;
238         ret = rsa_verify(&rsaplaintext, result_op);
239         if (ret)
240                 status = TEST_FAILED;
241
242 error_exit:
243
244         rte_crypto_op_free(op);
245
246         return status;
247 }
248 static int
249 test_cryptodev_asym_ver(union test_case_structure *data_tc,
250                                                 struct rte_crypto_op *result_op)
251 {
252         int status = TEST_SUCCESS;
253         int ret = 0;
254         uint8_t *data_expected = NULL, *data_received = NULL;
255         size_t data_size = 0;
256
257         switch (data_tc->modex.xform_type) {
258         case RTE_CRYPTO_ASYM_XFORM_MODEX:
259                 data_expected = data_tc->modex.reminder.data;
260                 data_received = result_op->asym->modex.result.data;
261                 data_size = result_op->asym->modex.result.length;
262                 break;
263         case RTE_CRYPTO_ASYM_XFORM_MODINV:
264                 data_expected = data_tc->modinv.inverse.data;
265                 data_received = result_op->asym->modinv.result.data;
266                 data_size = result_op->asym->modinv.result.length;
267                 break;
268         case RTE_CRYPTO_ASYM_XFORM_DH:
269         case RTE_CRYPTO_ASYM_XFORM_DSA:
270         case RTE_CRYPTO_ASYM_XFORM_RSA:
271         case RTE_CRYPTO_ASYM_XFORM_NONE:
272         case RTE_CRYPTO_ASYM_XFORM_UNSPECIFIED:
273         default:
274                 break;
275         }
276         ret = memcmp(data_expected, data_received, data_size);
277         if (ret)
278                 status = TEST_FAILED;
279
280         return status;
281 }
282
283 static int
284 test_cryptodev_asym_op(struct crypto_testsuite_params *ts_params,
285         union test_case_structure *data_tc,
286         char *test_msg, int sessionless)
287 {
288         struct rte_crypto_asym_op *asym_op = NULL;
289         struct rte_crypto_op *op = NULL;
290         struct rte_crypto_op *result_op = NULL;
291         struct rte_crypto_asym_xform xform_tc;
292         struct rte_cryptodev_asym_session *sess = NULL;
293         struct rte_cryptodev_asym_capability_idx cap_idx;
294         const struct rte_cryptodev_asymmetric_xform_capability *capability;
295         uint8_t dev_id = ts_params->valid_devs[0];
296         uint8_t input[TEST_DATA_SIZE] = {0};
297         uint8_t *result = NULL;
298
299         int status = TEST_SUCCESS;
300
301         xform_tc.next = NULL;
302         xform_tc.xform_type = data_tc->modex.xform_type;
303
304         cap_idx.type = xform_tc.xform_type;
305         capability = rte_cryptodev_asym_capability_get(dev_id, &cap_idx);
306
307         if (capability == NULL) {
308                 RTE_LOG(INFO, USER1,
309                         "Device doesn't support MODEX. Test Skipped\n");
310                 return -ENOTSUP;
311         }
312
313         /* Generate crypto op data structure */
314         op = rte_crypto_op_alloc(ts_params->op_mpool,
315                 RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
316
317         if (!op) {
318                 snprintf(test_msg, ASYM_TEST_MSG_LEN,
319                         "line %u FAILED: %s",
320                         __LINE__, "Failed to allocate asymmetric crypto "
321                         "operation struct");
322                 status = TEST_FAILED;
323                 goto error_exit;
324         }
325
326         asym_op = op->asym;
327
328         switch (xform_tc.xform_type) {
329         case RTE_CRYPTO_ASYM_XFORM_MODEX:
330                 result = rte_zmalloc(NULL, data_tc->modex.result_len, 0);
331                 xform_tc.modex.modulus.data = data_tc->modex.modulus.data;
332                 xform_tc.modex.modulus.length = data_tc->modex.modulus.len;
333                 xform_tc.modex.exponent.data = data_tc->modex.exponent.data;
334                 xform_tc.modex.exponent.length = data_tc->modex.exponent.len;
335                 memcpy(input, data_tc->modex.base.data,
336                         data_tc->modex.base.len);
337                 asym_op->modex.base.data = input;
338                 asym_op->modex.base.length = data_tc->modex.base.len;
339                 asym_op->modex.result.data = result;
340                 asym_op->modex.result.length = data_tc->modex.result_len;
341                 if (rte_cryptodev_asym_xform_capability_check_modlen(capability,
342                                 xform_tc.modex.modulus.length)) {
343                         snprintf(test_msg, ASYM_TEST_MSG_LEN,
344                                 "line %u "
345                                 "FAILED: %s", __LINE__,
346                                 "Invalid MODULUS length specified");
347                         status = TEST_FAILED;
348                         goto error_exit;
349                 }
350                 break;
351         case RTE_CRYPTO_ASYM_XFORM_MODINV:
352                 result = rte_zmalloc(NULL, data_tc->modinv.result_len, 0);
353                 xform_tc.modinv.modulus.data = data_tc->modinv.modulus.data;
354                 xform_tc.modinv.modulus.length = data_tc->modinv.modulus.len;
355                 memcpy(input, data_tc->modinv.base.data,
356                         data_tc->modinv.base.len);
357                 asym_op->modinv.base.data = input;
358                 asym_op->modinv.base.length = data_tc->modinv.base.len;
359                 asym_op->modinv.result.data = result;
360                 asym_op->modinv.result.length = data_tc->modinv.result_len;
361                 if (rte_cryptodev_asym_xform_capability_check_modlen(capability,
362                                 xform_tc.modinv.modulus.length)) {
363                         snprintf(test_msg, ASYM_TEST_MSG_LEN,
364                                 "line %u "
365                                 "FAILED: %s", __LINE__,
366                                 "Invalid MODULUS length specified");
367                         status = TEST_FAILED;
368                         goto error_exit;
369                 }
370                 break;
371         case RTE_CRYPTO_ASYM_XFORM_DH:
372         case RTE_CRYPTO_ASYM_XFORM_DSA:
373         case RTE_CRYPTO_ASYM_XFORM_RSA:
374         case RTE_CRYPTO_ASYM_XFORM_NONE:
375         case RTE_CRYPTO_ASYM_XFORM_UNSPECIFIED:
376         default:
377                 snprintf(test_msg, ASYM_TEST_MSG_LEN,
378                                 "line %u "
379                                 "FAILED: %s", __LINE__,
380                                 "Invalid ASYM algorithm specified");
381                 status = TEST_FAILED;
382                 goto error_exit;
383         }
384
385         if (!sessionless) {
386                 sess = rte_cryptodev_asym_session_create(ts_params->session_mpool);
387                 if (!sess) {
388                         snprintf(test_msg, ASYM_TEST_MSG_LEN,
389                                         "line %u "
390                                         "FAILED: %s", __LINE__,
391                                         "Session creation failed");
392                         status = TEST_FAILED;
393                         goto error_exit;
394                 }
395
396                 if (rte_cryptodev_asym_session_init(dev_id, sess, &xform_tc,
397                                 ts_params->session_mpool) < 0) {
398                         snprintf(test_msg, ASYM_TEST_MSG_LEN,
399                                         "line %u FAILED: %s",
400                                         __LINE__, "unabled to config sym session");
401                         status = TEST_FAILED;
402                         goto error_exit;
403                 }
404
405                 rte_crypto_op_attach_asym_session(op, sess);
406         } else {
407                 asym_op->xform = &xform_tc;
408                 op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
409         }
410         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
411
412         /* Process crypto operation */
413         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
414                 snprintf(test_msg, ASYM_TEST_MSG_LEN,
415                                 "line %u FAILED: %s",
416                                 __LINE__, "Error sending packet for operation");
417                 status = TEST_FAILED;
418                 goto error_exit;
419         }
420
421         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
422                 rte_pause();
423
424         if (result_op == NULL) {
425                 snprintf(test_msg, ASYM_TEST_MSG_LEN,
426                                 "line %u FAILED: %s",
427                                 __LINE__, "Failed to process asym crypto op");
428                 status = TEST_FAILED;
429                 goto error_exit;
430         }
431
432         if (test_cryptodev_asym_ver(data_tc, result_op) != TEST_SUCCESS) {
433                 snprintf(test_msg, ASYM_TEST_MSG_LEN,
434                         "line %u FAILED: %s",
435                         __LINE__, "Verification failed ");
436                 status = TEST_FAILED;
437                 goto error_exit;
438         }
439
440         if (!sessionless)
441                 snprintf(test_msg, ASYM_TEST_MSG_LEN, "PASS");
442         else
443                 snprintf(test_msg, ASYM_TEST_MSG_LEN, "SESSIONLESS PASS");
444
445 error_exit:
446                 if (sess != NULL) {
447                         rte_cryptodev_asym_session_clear(dev_id, sess);
448                         rte_cryptodev_asym_session_free(sess);
449                 }
450
451                 if (op != NULL)
452                         rte_crypto_op_free(op);
453
454                 if (result != NULL)
455                         rte_free(result);
456
457         return status;
458 }
459
460 static int
461 test_one_case(const void *test_case, int sessionless)
462 {
463         int status = TEST_SUCCESS;
464         char test_msg[ASYM_TEST_MSG_LEN + 1];
465
466         /* Map the case to union */
467         union test_case_structure tc;
468         memcpy(&tc, test_case, sizeof(tc));
469
470         status = test_cryptodev_asym_op(&testsuite_params, &tc, test_msg,
471                         sessionless);
472
473         printf("  %u) TestCase %s %s\n", test_index++,
474                 tc.modex.description, test_msg);
475
476         return status;
477 }
478
479 static int
480 load_test_vectors(void)
481 {
482         uint32_t i = 0, v_size = 0;
483         /* Load MODEX vector*/
484         v_size = ARRAY_SIZE(modex_test_case);
485         for (i = 0; i < v_size; i++) {
486                 if (test_vector.size >= (TEST_VECTOR_SIZE)) {
487                         RTE_LOG(DEBUG, USER1,
488                                 "TEST_VECTOR_SIZE too small\n");
489                         return -1;
490                 }
491                 test_vector.address[test_vector.size] = &modex_test_case[i];
492                 test_vector.size++;
493         }
494         /* Load MODINV vector*/
495         v_size = ARRAY_SIZE(modinv_test_case);
496         for (i = 0; i < v_size; i++) {
497                 if (test_vector.size >= (TEST_VECTOR_SIZE)) {
498                         RTE_LOG(DEBUG, USER1,
499                                 "TEST_VECTOR_SIZE too small\n");
500                         return -1;
501                 }
502                 test_vector.address[test_vector.size] = &modinv_test_case[i];
503                 test_vector.size++;
504         }
505         return 0;
506 }
507
508 static int
509 test_one_by_one(void)
510 {
511         int status = TEST_SUCCESS;
512         struct crypto_testsuite_params *ts_params = &testsuite_params;
513         uint32_t i = 0;
514         uint8_t dev_id = ts_params->valid_devs[0];
515         struct rte_cryptodev_info dev_info;
516         int sessionless = 0;
517
518         rte_cryptodev_info_get(dev_id, &dev_info);
519         if ((dev_info.feature_flags &
520                         RTE_CRYPTODEV_FF_ASYM_SESSIONLESS)) {
521                 sessionless = 1;
522         }
523
524         /* Go through all test cases */
525         test_index = 0;
526         for (i = 0; i < test_vector.size; i++) {
527                 if (test_one_case(test_vector.address[i], 0) != TEST_SUCCESS)
528                         status = TEST_FAILED;
529         }
530         if (sessionless) {
531                 for (i = 0; i < test_vector.size; i++) {
532                         if (test_one_case(test_vector.address[i], 1)
533                                         != TEST_SUCCESS)
534                                 status = TEST_FAILED;
535                 }
536         }
537
538         TEST_ASSERT_EQUAL(status, 0, "Test failed");
539         return status;
540 }
541
542 static int
543 test_rsa_sign_verify(void)
544 {
545         struct crypto_testsuite_params *ts_params = &testsuite_params;
546         struct rte_mempool *sess_mpool = ts_params->session_mpool;
547         uint8_t dev_id = ts_params->valid_devs[0];
548         struct rte_cryptodev_asym_session *sess;
549         struct rte_cryptodev_info dev_info;
550         int status = TEST_SUCCESS;
551
552         /* Test case supports op with exponent key only,
553          * Check in PMD feature flag for RSA exponent key type support.
554          */
555         rte_cryptodev_info_get(dev_id, &dev_info);
556         if (!(dev_info.feature_flags &
557                                 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP)) {
558                 RTE_LOG(INFO, USER1, "Device doesn't support sign op with "
559                         "exponent key type. Test Skipped\n");
560                 return -ENOTSUP;
561         }
562
563         sess = rte_cryptodev_asym_session_create(sess_mpool);
564
565         if (!sess) {
566                 RTE_LOG(ERR, USER1, "Session creation failed for "
567                         "sign_verify\n");
568                 return TEST_FAILED;
569         }
570
571         if (rte_cryptodev_asym_session_init(dev_id, sess, &rsa_xform,
572                                 sess_mpool) < 0) {
573                 RTE_LOG(ERR, USER1, "Unable to config asym session for "
574                         "sign_verify\n");
575                 status = TEST_FAILED;
576                 goto error_exit;
577         }
578
579         status = queue_ops_rsa_sign_verify(sess);
580
581 error_exit:
582
583         rte_cryptodev_asym_session_clear(dev_id, sess);
584         rte_cryptodev_asym_session_free(sess);
585
586         TEST_ASSERT_EQUAL(status, 0, "Test failed");
587
588         return status;
589 }
590
591 static int
592 test_rsa_enc_dec(void)
593 {
594         struct crypto_testsuite_params *ts_params = &testsuite_params;
595         struct rte_mempool *sess_mpool = ts_params->session_mpool;
596         uint8_t dev_id = ts_params->valid_devs[0];
597         struct rte_cryptodev_asym_session *sess;
598         struct rte_cryptodev_info dev_info;
599         int status = TEST_SUCCESS;
600
601         /* Test case supports op with exponent key only,
602          * Check in PMD feature flag for RSA exponent key type support.
603          */
604         rte_cryptodev_info_get(dev_id, &dev_info);
605         if (!(dev_info.feature_flags &
606                                 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP)) {
607                 RTE_LOG(INFO, USER1, "Device doesn't support decrypt op with "
608                         "exponent key type. Test skipped\n");
609                 return -ENOTSUP;
610         }
611
612         sess = rte_cryptodev_asym_session_create(sess_mpool);
613
614         if (!sess) {
615                 RTE_LOG(ERR, USER1, "Session creation failed for enc_dec\n");
616                 return TEST_FAILED;
617         }
618
619         if (rte_cryptodev_asym_session_init(dev_id, sess, &rsa_xform,
620                                 sess_mpool) < 0) {
621                 RTE_LOG(ERR, USER1, "Unable to config asym session for "
622                         "enc_dec\n");
623                 status = TEST_FAILED;
624                 goto error_exit;
625         }
626
627         status = queue_ops_rsa_enc_dec(sess);
628
629 error_exit:
630
631         rte_cryptodev_asym_session_clear(dev_id, sess);
632         rte_cryptodev_asym_session_free(sess);
633
634         TEST_ASSERT_EQUAL(status, 0, "Test failed");
635
636         return status;
637 }
638
639 static int
640 test_rsa_sign_verify_crt(void)
641 {
642         struct crypto_testsuite_params *ts_params = &testsuite_params;
643         struct rte_mempool *sess_mpool = ts_params->session_mpool;
644         uint8_t dev_id = ts_params->valid_devs[0];
645         struct rte_cryptodev_asym_session *sess;
646         struct rte_cryptodev_info dev_info;
647         int status = TEST_SUCCESS;
648
649         /* Test case supports op with quintuple format key only,
650          * Check im PMD feature flag for RSA quintuple key type support.
651          */
652         rte_cryptodev_info_get(dev_id, &dev_info);
653         if (!(dev_info.feature_flags & RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT)) {
654                 RTE_LOG(INFO, USER1, "Device doesn't support sign op with "
655                         "quintuple key type. Test skipped\n");
656                 return -ENOTSUP;
657         }
658
659         sess = rte_cryptodev_asym_session_create(sess_mpool);
660
661         if (!sess) {
662                 RTE_LOG(ERR, USER1, "Session creation failed for "
663                         "sign_verify_crt\n");
664                 status = TEST_FAILED;
665                 return status;
666         }
667
668         if (rte_cryptodev_asym_session_init(dev_id, sess, &rsa_xform_crt,
669                                 sess_mpool) < 0) {
670                 RTE_LOG(ERR, USER1, "Unable to config asym session for "
671                         "sign_verify_crt\n");
672                 status = TEST_FAILED;
673                 goto error_exit;
674         }
675         status = queue_ops_rsa_sign_verify(sess);
676
677 error_exit:
678
679         rte_cryptodev_asym_session_clear(dev_id, sess);
680         rte_cryptodev_asym_session_free(sess);
681
682         TEST_ASSERT_EQUAL(status, 0, "Test failed");
683
684         return status;
685 }
686
687 static int
688 test_rsa_enc_dec_crt(void)
689 {
690         struct crypto_testsuite_params *ts_params = &testsuite_params;
691         struct rte_mempool *sess_mpool = ts_params->session_mpool;
692         uint8_t dev_id = ts_params->valid_devs[0];
693         struct rte_cryptodev_asym_session *sess;
694         struct rte_cryptodev_info dev_info;
695         int status = TEST_SUCCESS;
696
697         /* Test case supports op with quintuple format key only,
698          * Check in PMD feature flag for RSA quintuple key type support.
699          */
700         rte_cryptodev_info_get(dev_id, &dev_info);
701         if (!(dev_info.feature_flags & RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT)) {
702                 RTE_LOG(INFO, USER1, "Device doesn't support decrypt op with "
703                         "quintuple key type. Test skipped\n");
704                 return -ENOTSUP;
705         }
706
707         sess = rte_cryptodev_asym_session_create(sess_mpool);
708
709         if (!sess) {
710                 RTE_LOG(ERR, USER1, "Session creation failed for "
711                         "enc_dec_crt\n");
712                 return TEST_FAILED;
713         }
714
715         if (rte_cryptodev_asym_session_init(dev_id, sess, &rsa_xform_crt,
716                                 sess_mpool) < 0) {
717                 RTE_LOG(ERR, USER1, "Unable to config asym session for "
718                         "enc_dec_crt\n");
719                 status = TEST_FAILED;
720                 goto error_exit;
721         }
722         status = queue_ops_rsa_enc_dec(sess);
723
724 error_exit:
725
726         rte_cryptodev_asym_session_clear(dev_id, sess);
727         rte_cryptodev_asym_session_free(sess);
728
729         TEST_ASSERT_EQUAL(status, 0, "Test failed");
730
731         return status;
732 }
733
734 static int
735 testsuite_setup(void)
736 {
737         struct crypto_testsuite_params *ts_params = &testsuite_params;
738         uint8_t valid_devs[RTE_CRYPTO_MAX_DEVS];
739         struct rte_cryptodev_info info;
740         int ret, dev_id = -1;
741         uint32_t i, nb_devs;
742         uint16_t qp_id;
743
744         memset(ts_params, 0, sizeof(*ts_params));
745
746         test_vector.size = 0;
747         load_test_vectors();
748
749         ts_params->op_mpool = rte_crypto_op_pool_create(
750                         "CRYPTO_ASYM_OP_POOL",
751                         RTE_CRYPTO_OP_TYPE_ASYMMETRIC,
752                         TEST_NUM_BUFS, 0,
753                         0,
754                         rte_socket_id());
755         if (ts_params->op_mpool == NULL) {
756                 RTE_LOG(ERR, USER1, "Can't create ASYM_CRYPTO_OP_POOL\n");
757                 return TEST_FAILED;
758         }
759
760         /* Create an OPENSSL device if required */
761         if (gbl_driver_id == rte_cryptodev_driver_id_get(
762                         RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD))) {
763                 nb_devs = rte_cryptodev_device_count_by_driver(
764                                 rte_cryptodev_driver_id_get(
765                                 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD)));
766                 if (nb_devs < 1) {
767                         ret = rte_vdev_init(
768                                 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD),
769                                 NULL);
770
771                         TEST_ASSERT(ret == 0, "Failed to create "
772                                 "instance of pmd : %s",
773                                 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
774                 }
775         }
776
777         /* Get list of valid crypto devs */
778         nb_devs = rte_cryptodev_devices_get(
779                                 rte_cryptodev_driver_name_get(gbl_driver_id),
780                                 valid_devs, RTE_CRYPTO_MAX_DEVS);
781         if (nb_devs < 1) {
782                 RTE_LOG(ERR, USER1, "No crypto devices found?\n");
783                 return TEST_FAILED;
784         }
785
786         /*
787          * Get first valid asymmetric device found in test suite param and
788          * break
789          */
790         for (i = 0; i < nb_devs ; i++) {
791                 rte_cryptodev_info_get(valid_devs[i], &info);
792                 if (info.feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO) {
793                         dev_id = ts_params->valid_devs[0] = valid_devs[i];
794                         break;
795                 }
796         }
797
798         if (dev_id == -1) {
799                 RTE_LOG(ERR, USER1, "Device doesn't support asymmetric. "
800                         "Test skipped.\n");
801                 return TEST_FAILED;
802         }
803
804         /* Set valid device count */
805         ts_params->valid_dev_count = nb_devs;
806
807         /* configure device with num qp */
808         ts_params->conf.nb_queue_pairs = info.max_nb_queue_pairs;
809         ts_params->conf.socket_id = SOCKET_ID_ANY;
810         ts_params->conf.ff_disable = RTE_CRYPTODEV_FF_SECURITY |
811                         RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO;
812         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(dev_id,
813                         &ts_params->conf),
814                         "Failed to configure cryptodev %u with %u qps",
815                         dev_id, ts_params->conf.nb_queue_pairs);
816
817         /* configure qp */
818         ts_params->qp_conf.nb_descriptors = DEFAULT_NUM_OPS_INFLIGHT;
819         ts_params->qp_conf.mp_session = ts_params->session_mpool;
820         ts_params->qp_conf.mp_session_private = ts_params->session_mpool;
821         for (qp_id = 0; qp_id < info.max_nb_queue_pairs; qp_id++) {
822                 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
823                         dev_id, qp_id, &ts_params->qp_conf,
824                         rte_cryptodev_socket_id(dev_id)),
825                         "Failed to setup queue pair %u on cryptodev %u ASYM",
826                         qp_id, dev_id);
827         }
828
829         /* setup asym session pool */
830         unsigned int session_size =
831                 rte_cryptodev_asym_get_private_session_size(dev_id);
832         /*
833          * Create mempool with TEST_NUM_SESSIONS * 2,
834          * to include the session headers
835          */
836         ts_params->session_mpool = rte_mempool_create(
837                                 "test_asym_sess_mp",
838                                 TEST_NUM_SESSIONS * 2,
839                                 session_size,
840                                 0, 0, NULL, NULL, NULL,
841                                 NULL, SOCKET_ID_ANY,
842                                 0);
843
844         TEST_ASSERT_NOT_NULL(ts_params->session_mpool,
845                         "session mempool allocation failed");
846
847         return TEST_SUCCESS;
848 }
849
850 static void
851 testsuite_teardown(void)
852 {
853         struct crypto_testsuite_params *ts_params = &testsuite_params;
854
855         if (ts_params->op_mpool != NULL) {
856                 RTE_LOG(DEBUG, USER1, "CRYPTO_OP_POOL count %u\n",
857                 rte_mempool_avail_count(ts_params->op_mpool));
858         }
859
860         /* Free session mempools */
861         if (ts_params->session_mpool != NULL) {
862                 rte_mempool_free(ts_params->session_mpool);
863                 ts_params->session_mpool = NULL;
864         }
865 }
866
867 static int
868 ut_setup(void)
869 {
870         struct crypto_testsuite_params *ts_params = &testsuite_params;
871
872         uint16_t qp_id;
873
874         /* Reconfigure device to default parameters */
875         ts_params->conf.socket_id = SOCKET_ID_ANY;
876
877         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(ts_params->valid_devs[0],
878                         &ts_params->conf),
879                         "Failed to configure cryptodev %u",
880                         ts_params->valid_devs[0]);
881
882         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs ; qp_id++) {
883                 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
884                         ts_params->valid_devs[0], qp_id,
885                         &ts_params->qp_conf,
886                         rte_cryptodev_socket_id(ts_params->valid_devs[0])),
887                         "Failed to setup queue pair %u on cryptodev %u",
888                         qp_id, ts_params->valid_devs[0]);
889         }
890
891         rte_cryptodev_stats_reset(ts_params->valid_devs[0]);
892
893         /* Start the device */
894         TEST_ASSERT_SUCCESS(rte_cryptodev_start(ts_params->valid_devs[0]),
895                                                 "Failed to start cryptodev %u",
896                                                 ts_params->valid_devs[0]);
897
898         return TEST_SUCCESS;
899 }
900
901 static void
902 ut_teardown(void)
903 {
904         struct crypto_testsuite_params *ts_params = &testsuite_params;
905         struct rte_cryptodev_stats stats;
906
907         rte_cryptodev_stats_get(ts_params->valid_devs[0], &stats);
908
909         /* Stop the device */
910         rte_cryptodev_stop(ts_params->valid_devs[0]);
911 }
912
913 static inline void print_asym_capa(
914                 const struct rte_cryptodev_asymmetric_xform_capability *capa)
915 {
916         int i = 0;
917
918         printf("\nxform type: %s\n===================\n",
919                         rte_crypto_asym_xform_strings[capa->xform_type]);
920         printf("operation supported -");
921
922         for (i = 0; i < RTE_CRYPTO_ASYM_OP_LIST_END; i++) {
923                 /* check supported operations */
924                 if (rte_cryptodev_asym_xform_capability_check_optype(capa, i))
925                         printf(" %s",
926                                         rte_crypto_asym_op_strings[i]);
927                 }
928                 switch (capa->xform_type) {
929                 case RTE_CRYPTO_ASYM_XFORM_RSA:
930                 case RTE_CRYPTO_ASYM_XFORM_MODINV:
931                 case RTE_CRYPTO_ASYM_XFORM_MODEX:
932                 case RTE_CRYPTO_ASYM_XFORM_DH:
933                 case RTE_CRYPTO_ASYM_XFORM_DSA:
934                         printf(" modlen: min %d max %d increment %d\n",
935                                         capa->modlen.min,
936                                         capa->modlen.max,
937                                         capa->modlen.increment);
938                 break;
939                 default:
940                         break;
941                 }
942 }
943
944 static int
945 test_capability(void)
946 {
947         struct crypto_testsuite_params *ts_params = &testsuite_params;
948         uint8_t dev_id = ts_params->valid_devs[0];
949         struct rte_cryptodev_info dev_info;
950         const struct rte_cryptodev_capabilities *dev_capa;
951         int i = 0;
952         struct rte_cryptodev_asym_capability_idx idx;
953         const struct rte_cryptodev_asymmetric_xform_capability *capa;
954
955         rte_cryptodev_info_get(dev_id, &dev_info);
956         if (!(dev_info.feature_flags &
957                                 RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)) {
958                 RTE_LOG(INFO, USER1,
959                                 "Device doesn't support asymmetric. Test Skipped\n");
960                 return TEST_SUCCESS;
961         }
962
963         /* print xform capability */
964         for (i = 0;
965                 dev_info.capabilities[i].op != RTE_CRYPTO_OP_TYPE_UNDEFINED;
966                 i++) {
967                 dev_capa = &(dev_info.capabilities[i]);
968                 if (dev_info.capabilities[i].op ==
969                                 RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
970                         idx.type = dev_capa->asym.xform_capa.xform_type;
971
972                         capa = rte_cryptodev_asym_capability_get(dev_id,
973                                 (const struct
974                                 rte_cryptodev_asym_capability_idx *) &idx);
975                         print_asym_capa(capa);
976                         }
977         }
978         return TEST_SUCCESS;
979 }
980
981 static int
982 test_dh_gen_shared_sec(struct rte_crypto_asym_xform *xfrm)
983 {
984         struct crypto_testsuite_params *ts_params = &testsuite_params;
985         struct rte_mempool *op_mpool = ts_params->op_mpool;
986         struct rte_mempool *sess_mpool = ts_params->session_mpool;
987         uint8_t dev_id = ts_params->valid_devs[0];
988         struct rte_crypto_asym_op *asym_op = NULL;
989         struct rte_crypto_op *op = NULL, *result_op = NULL;
990         struct rte_cryptodev_asym_session *sess = NULL;
991         int status = TEST_SUCCESS;
992         uint8_t output[TEST_DH_MOD_LEN];
993         struct rte_crypto_asym_xform xform = *xfrm;
994         uint8_t peer[] = "01234567890123456789012345678901234567890123456789";
995
996         sess = rte_cryptodev_asym_session_create(sess_mpool);
997         if (sess == NULL) {
998                 RTE_LOG(ERR, USER1,
999                                 "line %u FAILED: %s", __LINE__,
1000                                 "Session creation failed");
1001                 status = TEST_FAILED;
1002                 goto error_exit;
1003         }
1004         /* set up crypto op data structure */
1005         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1006         if (!op) {
1007                 RTE_LOG(ERR, USER1,
1008                         "line %u FAILED: %s",
1009                         __LINE__, "Failed to allocate asymmetric crypto "
1010                         "operation struct");
1011                 status = TEST_FAILED;
1012                 goto error_exit;
1013         }
1014         asym_op = op->asym;
1015
1016         /* Setup a xform and op to generate private key only */
1017         xform.dh.type = RTE_CRYPTO_ASYM_OP_SHARED_SECRET_COMPUTE;
1018         xform.next = NULL;
1019         asym_op->dh.priv_key.data = dh_test_params.priv_key.data;
1020         asym_op->dh.priv_key.length = dh_test_params.priv_key.length;
1021         asym_op->dh.pub_key.data = (uint8_t *)peer;
1022         asym_op->dh.pub_key.length = sizeof(peer);
1023         asym_op->dh.shared_secret.data = output;
1024         asym_op->dh.shared_secret.length = sizeof(output);
1025
1026         if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
1027                         sess_mpool) < 0) {
1028                 RTE_LOG(ERR, USER1,
1029                                 "line %u FAILED: %s",
1030                                 __LINE__, "unabled to config sym session");
1031                 status = TEST_FAILED;
1032                 goto error_exit;
1033         }
1034
1035         /* attach asymmetric crypto session to crypto operations */
1036         rte_crypto_op_attach_asym_session(op, sess);
1037
1038         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1039
1040         /* Process crypto operation */
1041         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1042                 RTE_LOG(ERR, USER1,
1043                         "line %u FAILED: %s",
1044                         __LINE__, "Error sending packet for operation");
1045                 status = TEST_FAILED;
1046                 goto error_exit;
1047         }
1048
1049         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1050                 rte_pause();
1051
1052         if (result_op == NULL) {
1053                 RTE_LOG(ERR, USER1,
1054                         "line %u FAILED: %s",
1055                         __LINE__, "Failed to process asym crypto op");
1056                 status = TEST_FAILED;
1057                 goto error_exit;
1058         }
1059
1060         debug_hexdump(stdout, "shared secret:",
1061                         asym_op->dh.shared_secret.data,
1062                         asym_op->dh.shared_secret.length);
1063
1064 error_exit:
1065         if (sess != NULL) {
1066                 rte_cryptodev_asym_session_clear(dev_id, sess);
1067                 rte_cryptodev_asym_session_free(sess);
1068         }
1069         if (op != NULL)
1070                 rte_crypto_op_free(op);
1071         return status;
1072 }
1073
1074 static int
1075 test_dh_gen_priv_key(struct rte_crypto_asym_xform *xfrm)
1076 {
1077         struct crypto_testsuite_params *ts_params = &testsuite_params;
1078         struct rte_mempool *op_mpool = ts_params->op_mpool;
1079         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1080         uint8_t dev_id = ts_params->valid_devs[0];
1081         struct rte_crypto_asym_op *asym_op = NULL;
1082         struct rte_crypto_op *op = NULL, *result_op = NULL;
1083         struct rte_cryptodev_asym_session *sess = NULL;
1084         int status = TEST_SUCCESS;
1085         uint8_t output[TEST_DH_MOD_LEN];
1086         struct rte_crypto_asym_xform xform = *xfrm;
1087
1088         sess = rte_cryptodev_asym_session_create(sess_mpool);
1089         if (sess == NULL) {
1090                 RTE_LOG(ERR, USER1,
1091                                  "line %u FAILED: %s", __LINE__,
1092                                 "Session creation failed");
1093                 status = TEST_FAILED;
1094                 goto error_exit;
1095         }
1096         /* set up crypto op data structure */
1097         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1098         if (!op) {
1099                 RTE_LOG(ERR, USER1,
1100                         "line %u FAILED: %s",
1101                         __LINE__, "Failed to allocate asymmetric crypto "
1102                         "operation struct");
1103                 status = TEST_FAILED;
1104                 goto error_exit;
1105         }
1106         asym_op = op->asym;
1107
1108         /* Setup a xform and op to generate private key only */
1109         xform.dh.type = RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE;
1110         xform.next = NULL;
1111         asym_op->dh.priv_key.data = output;
1112         asym_op->dh.priv_key.length = sizeof(output);
1113
1114         if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
1115                         sess_mpool) < 0) {
1116                 RTE_LOG(ERR, USER1,
1117                                 "line %u FAILED: %s",
1118                                 __LINE__, "unabled to config sym session");
1119                 status = TEST_FAILED;
1120                 goto error_exit;
1121         }
1122
1123         /* attach asymmetric crypto session to crypto operations */
1124         rte_crypto_op_attach_asym_session(op, sess);
1125
1126         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1127
1128         /* Process crypto operation */
1129         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1130                 RTE_LOG(ERR, USER1,
1131                         "line %u FAILED: %s",
1132                         __LINE__, "Error sending packet for operation");
1133                 status = TEST_FAILED;
1134                 goto error_exit;
1135         }
1136
1137         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1138                 rte_pause();
1139
1140         if (result_op == NULL) {
1141                 RTE_LOG(ERR, USER1,
1142                         "line %u FAILED: %s",
1143                         __LINE__, "Failed to process asym crypto op");
1144                 status = TEST_FAILED;
1145                 goto error_exit;
1146         }
1147
1148         debug_hexdump(stdout, "private key:",
1149                         asym_op->dh.priv_key.data,
1150                         asym_op->dh.priv_key.length);
1151
1152
1153 error_exit:
1154         if (sess != NULL) {
1155                 rte_cryptodev_asym_session_clear(dev_id, sess);
1156                 rte_cryptodev_asym_session_free(sess);
1157         }
1158         if (op != NULL)
1159                 rte_crypto_op_free(op);
1160
1161         return status;
1162 }
1163
1164
1165 static int
1166 test_dh_gen_pub_key(struct rte_crypto_asym_xform *xfrm)
1167 {
1168         struct crypto_testsuite_params *ts_params = &testsuite_params;
1169         struct rte_mempool *op_mpool = ts_params->op_mpool;
1170         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1171         uint8_t dev_id = ts_params->valid_devs[0];
1172         struct rte_crypto_asym_op *asym_op = NULL;
1173         struct rte_crypto_op *op = NULL, *result_op = NULL;
1174         struct rte_cryptodev_asym_session *sess = NULL;
1175         int status = TEST_SUCCESS;
1176         uint8_t output[TEST_DH_MOD_LEN];
1177         struct rte_crypto_asym_xform xform = *xfrm;
1178
1179         sess = rte_cryptodev_asym_session_create(sess_mpool);
1180         if (sess == NULL) {
1181                 RTE_LOG(ERR, USER1,
1182                                  "line %u FAILED: %s", __LINE__,
1183                                 "Session creation failed");
1184                 status = TEST_FAILED;
1185                 goto error_exit;
1186         }
1187         /* set up crypto op data structure */
1188         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1189         if (!op) {
1190                 RTE_LOG(ERR, USER1,
1191                         "line %u FAILED: %s",
1192                         __LINE__, "Failed to allocate asymmetric crypto "
1193                         "operation struct");
1194                 status = TEST_FAILED;
1195                 goto error_exit;
1196         }
1197         asym_op = op->asym;
1198         /* Setup a xform chain to generate public key
1199          * using test private key
1200          *
1201          */
1202         xform.dh.type = RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE;
1203         xform.next = NULL;
1204
1205         asym_op->dh.pub_key.data = output;
1206         asym_op->dh.pub_key.length = sizeof(output);
1207         /* load pre-defined private key */
1208         asym_op->dh.priv_key.data = rte_malloc(NULL,
1209                                         dh_test_params.priv_key.length,
1210                                         0);
1211         asym_op->dh.priv_key = dh_test_params.priv_key;
1212
1213         if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
1214                         sess_mpool) < 0) {
1215                 RTE_LOG(ERR, USER1,
1216                                 "line %u FAILED: %s",
1217                                 __LINE__, "unabled to config sym session");
1218                 status = TEST_FAILED;
1219                 goto error_exit;
1220         }
1221
1222         /* attach asymmetric crypto session to crypto operations */
1223         rte_crypto_op_attach_asym_session(op, sess);
1224
1225         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1226
1227         /* Process crypto operation */
1228         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1229                 RTE_LOG(ERR, USER1,
1230                         "line %u FAILED: %s",
1231                         __LINE__, "Error sending packet for operation");
1232                 status = TEST_FAILED;
1233                 goto error_exit;
1234         }
1235
1236         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1237                 rte_pause();
1238
1239         if (result_op == NULL) {
1240                 RTE_LOG(ERR, USER1,
1241                         "line %u FAILED: %s",
1242                         __LINE__, "Failed to process asym crypto op");
1243                 status = TEST_FAILED;
1244                 goto error_exit;
1245         }
1246
1247         debug_hexdump(stdout, "pub key:",
1248                         asym_op->dh.pub_key.data, asym_op->dh.pub_key.length);
1249
1250         debug_hexdump(stdout, "priv key:",
1251                         asym_op->dh.priv_key.data, asym_op->dh.priv_key.length);
1252
1253 error_exit:
1254         if (sess != NULL) {
1255                 rte_cryptodev_asym_session_clear(dev_id, sess);
1256                 rte_cryptodev_asym_session_free(sess);
1257         }
1258         if (op != NULL)
1259                 rte_crypto_op_free(op);
1260
1261         return status;
1262 }
1263
1264 static int
1265 test_dh_gen_kp(struct rte_crypto_asym_xform *xfrm)
1266 {
1267         struct crypto_testsuite_params *ts_params = &testsuite_params;
1268         struct rte_mempool *op_mpool = ts_params->op_mpool;
1269         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1270         uint8_t dev_id = ts_params->valid_devs[0];
1271         struct rte_crypto_asym_op *asym_op = NULL;
1272         struct rte_crypto_op *op = NULL, *result_op = NULL;
1273         struct rte_cryptodev_asym_session *sess = NULL;
1274         int status = TEST_SUCCESS;
1275         uint8_t out_pub_key[TEST_DH_MOD_LEN];
1276         uint8_t out_prv_key[TEST_DH_MOD_LEN];
1277         struct rte_crypto_asym_xform pub_key_xform;
1278         struct rte_crypto_asym_xform xform = *xfrm;
1279
1280         sess = rte_cryptodev_asym_session_create(sess_mpool);
1281         if (sess == NULL) {
1282                 RTE_LOG(ERR, USER1,
1283                                  "line %u FAILED: %s", __LINE__,
1284                                 "Session creation failed");
1285                 status = TEST_FAILED;
1286                 goto error_exit;
1287         }
1288
1289         /* set up crypto op data structure */
1290         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1291         if (!op) {
1292                 RTE_LOG(ERR, USER1,
1293                         "line %u FAILED: %s",
1294                         __LINE__, "Failed to allocate asymmetric crypto "
1295                         "operation struct");
1296                 status = TEST_FAILED;
1297                 goto error_exit;
1298         }
1299         asym_op = op->asym;
1300         /* Setup a xform chain to generate
1301          * private key first followed by
1302          * public key
1303          */xform.dh.type = RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE;
1304         pub_key_xform.xform_type = RTE_CRYPTO_ASYM_XFORM_DH;
1305         pub_key_xform.dh.type = RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE;
1306         xform.next = &pub_key_xform;
1307
1308         asym_op->dh.pub_key.data = out_pub_key;
1309         asym_op->dh.pub_key.length = sizeof(out_pub_key);
1310         asym_op->dh.priv_key.data = out_prv_key;
1311         asym_op->dh.priv_key.length = sizeof(out_prv_key);
1312         if (rte_cryptodev_asym_session_init(dev_id, sess, &xform,
1313                         sess_mpool) < 0) {
1314                 RTE_LOG(ERR, USER1,
1315                                 "line %u FAILED: %s",
1316                                 __LINE__, "unabled to config sym session");
1317                 status = TEST_FAILED;
1318                 goto error_exit;
1319         }
1320
1321         /* attach asymmetric crypto session to crypto operations */
1322         rte_crypto_op_attach_asym_session(op, sess);
1323
1324         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1325
1326         /* Process crypto operation */
1327         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1328                 RTE_LOG(ERR, USER1,
1329                         "line %u FAILED: %s",
1330                         __LINE__, "Error sending packet for operation");
1331                 status = TEST_FAILED;
1332                 goto error_exit;
1333         }
1334
1335         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1336                 rte_pause();
1337
1338         if (result_op == NULL) {
1339                 RTE_LOG(ERR, USER1,
1340                         "line %u FAILED: %s",
1341                         __LINE__, "Failed to process asym crypto op");
1342                 status = TEST_FAILED;
1343                 goto error_exit;
1344         }
1345         debug_hexdump(stdout, "priv key:",
1346                         out_prv_key, asym_op->dh.priv_key.length);
1347         debug_hexdump(stdout, "pub key:",
1348                         out_pub_key, asym_op->dh.pub_key.length);
1349
1350 error_exit:
1351         if (sess != NULL) {
1352                 rte_cryptodev_asym_session_clear(dev_id, sess);
1353                 rte_cryptodev_asym_session_free(sess);
1354         }
1355         if (op != NULL)
1356                 rte_crypto_op_free(op);
1357
1358         return status;
1359 }
1360
1361 static int
1362 test_mod_inv(void)
1363 {
1364         struct crypto_testsuite_params *ts_params = &testsuite_params;
1365         struct rte_mempool *op_mpool = ts_params->op_mpool;
1366         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1367         uint8_t dev_id = ts_params->valid_devs[0];
1368         struct rte_crypto_asym_op *asym_op = NULL;
1369         struct rte_crypto_op *op = NULL, *result_op = NULL;
1370         struct rte_cryptodev_asym_session *sess = NULL;
1371         int status = TEST_SUCCESS;
1372         struct rte_cryptodev_asym_capability_idx cap_idx;
1373         const struct rte_cryptodev_asymmetric_xform_capability *capability;
1374         uint8_t input[TEST_DATA_SIZE] = {0};
1375         int ret = 0;
1376         uint8_t result[sizeof(mod_p)] = { 0 };
1377
1378         if (rte_cryptodev_asym_get_xform_enum(
1379                 &modinv_xform.xform_type, "modinv") < 0) {
1380                 RTE_LOG(ERR, USER1,
1381                                  "Invalid ASYM algorithm specified\n");
1382                 return -1;
1383         }
1384
1385         cap_idx.type = modinv_xform.xform_type;
1386         capability = rte_cryptodev_asym_capability_get(dev_id,
1387                                         &cap_idx);
1388
1389         if (capability == NULL) {
1390                 RTE_LOG(INFO, USER1,
1391                         "Device doesn't support MOD INV. Test Skipped\n");
1392                 return -ENOTSUP;
1393         }
1394
1395         if (rte_cryptodev_asym_xform_capability_check_modlen(
1396                 capability,
1397                 modinv_xform.modinv.modulus.length)) {
1398                 RTE_LOG(ERR, USER1,
1399                                  "Invalid MODULUS length specified\n");
1400                                 return -ENOTSUP;
1401                 }
1402
1403         sess = rte_cryptodev_asym_session_create(sess_mpool);
1404         if (!sess) {
1405                 RTE_LOG(ERR, USER1, "line %u "
1406                                 "FAILED: %s", __LINE__,
1407                                 "Session creation failed");
1408                 status = TEST_FAILED;
1409                 goto error_exit;
1410         }
1411
1412         if (rte_cryptodev_asym_session_init(dev_id, sess, &modinv_xform,
1413                         sess_mpool) < 0) {
1414                 RTE_LOG(ERR, USER1,
1415                                 "line %u FAILED: %s",
1416                                 __LINE__, "unabled to config sym session");
1417                 status = TEST_FAILED;
1418                 goto error_exit;
1419         }
1420
1421         /* generate crypto op data structure */
1422         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1423         if (!op) {
1424                 RTE_LOG(ERR, USER1,
1425                         "line %u FAILED: %s",
1426                         __LINE__, "Failed to allocate asymmetric crypto "
1427                         "operation struct");
1428                 status = TEST_FAILED;
1429                 goto error_exit;
1430         }
1431
1432         asym_op = op->asym;
1433         memcpy(input, base, sizeof(base));
1434         asym_op->modinv.base.data = input;
1435         asym_op->modinv.base.length = sizeof(base);
1436         asym_op->modinv.result.data = result;
1437         asym_op->modinv.result.length = sizeof(result);
1438
1439         /* attach asymmetric crypto session to crypto operations */
1440         rte_crypto_op_attach_asym_session(op, sess);
1441
1442         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1443
1444         /* Process crypto operation */
1445         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1446                 RTE_LOG(ERR, USER1,
1447                         "line %u FAILED: %s",
1448                         __LINE__, "Error sending packet for operation");
1449                 status = TEST_FAILED;
1450                 goto error_exit;
1451         }
1452
1453         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1454                 rte_pause();
1455
1456         if (result_op == NULL) {
1457                 RTE_LOG(ERR, USER1,
1458                                 "line %u FAILED: %s",
1459                                 __LINE__, "Failed to process asym crypto op");
1460                 status = TEST_FAILED;
1461                 goto error_exit;
1462         }
1463
1464         ret = verify_modinv(mod_inv, result_op);
1465         if (ret) {
1466                 RTE_LOG(ERR, USER1,
1467                          "operation verification failed\n");
1468                 status = TEST_FAILED;
1469         }
1470
1471 error_exit:
1472         if (sess) {
1473                 rte_cryptodev_asym_session_clear(dev_id, sess);
1474                 rte_cryptodev_asym_session_free(sess);
1475         }
1476
1477         if (op)
1478                 rte_crypto_op_free(op);
1479
1480         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1481
1482         return status;
1483 }
1484
1485 static int
1486 test_mod_exp(void)
1487 {
1488         struct crypto_testsuite_params *ts_params = &testsuite_params;
1489         struct rte_mempool *op_mpool = ts_params->op_mpool;
1490         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1491         uint8_t dev_id = ts_params->valid_devs[0];
1492         struct rte_crypto_asym_op *asym_op = NULL;
1493         struct rte_crypto_op *op = NULL, *result_op = NULL;
1494         struct rte_cryptodev_asym_session *sess = NULL;
1495         int status = TEST_SUCCESS;
1496         struct rte_cryptodev_asym_capability_idx cap_idx;
1497         const struct rte_cryptodev_asymmetric_xform_capability *capability;
1498         uint8_t input[TEST_DATA_SIZE] = {0};
1499         int ret = 0;
1500         uint8_t result[sizeof(mod_p)] = { 0 };
1501
1502         if (rte_cryptodev_asym_get_xform_enum(&modex_xform.xform_type,
1503                 "modexp")
1504                 < 0) {
1505                 RTE_LOG(ERR, USER1,
1506                                 "Invalid ASYM algorithm specified\n");
1507                 return -1;
1508         }
1509
1510         /* check for modlen capability */
1511         cap_idx.type = modex_xform.xform_type;
1512         capability = rte_cryptodev_asym_capability_get(dev_id, &cap_idx);
1513
1514         if (capability == NULL) {
1515                 RTE_LOG(INFO, USER1,
1516                         "Device doesn't support MOD EXP. Test Skipped\n");
1517                 return -ENOTSUP;
1518         }
1519
1520         if (rte_cryptodev_asym_xform_capability_check_modlen(
1521                         capability, modex_xform.modex.modulus.length)) {
1522                 RTE_LOG(ERR, USER1,
1523                                 "Invalid MODULUS length specified\n");
1524                                 return -ENOTSUP;
1525                 }
1526
1527         /* generate crypto op data structure */
1528         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1529         if (!op) {
1530                 RTE_LOG(ERR, USER1,
1531                         "line %u FAILED: %s",
1532                         __LINE__, "Failed to allocate asymmetric crypto "
1533                         "operation struct");
1534                 status = TEST_FAILED;
1535                 goto error_exit;
1536         }
1537
1538         sess = rte_cryptodev_asym_session_create(sess_mpool);
1539         if (!sess) {
1540                 RTE_LOG(ERR, USER1,
1541                                  "line %u "
1542                                 "FAILED: %s", __LINE__,
1543                                 "Session creation failed");
1544                 status = TEST_FAILED;
1545                 goto error_exit;
1546         }
1547
1548         if (rte_cryptodev_asym_session_init(dev_id, sess, &modex_xform,
1549                         sess_mpool) < 0) {
1550                 RTE_LOG(ERR, USER1,
1551                                 "line %u FAILED: %s",
1552                                 __LINE__, "unabled to config sym session");
1553                 status = TEST_FAILED;
1554                 goto error_exit;
1555         }
1556
1557         asym_op = op->asym;
1558         memcpy(input, base, sizeof(base));
1559         asym_op->modex.base.data = input;
1560         asym_op->modex.base.length = sizeof(base);
1561         asym_op->modex.result.data = result;
1562         asym_op->modex.result.length = sizeof(result);
1563         /* attach asymmetric crypto session to crypto operations */
1564         rte_crypto_op_attach_asym_session(op, sess);
1565
1566         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1567         /* Process crypto operation */
1568         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1569                 RTE_LOG(ERR, USER1,
1570                                 "line %u FAILED: %s",
1571                                 __LINE__, "Error sending packet for operation");
1572                 status = TEST_FAILED;
1573                 goto error_exit;
1574         }
1575
1576         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1577                 rte_pause();
1578
1579         if (result_op == NULL) {
1580                 RTE_LOG(ERR, USER1,
1581                                 "line %u FAILED: %s",
1582                                 __LINE__, "Failed to process asym crypto op");
1583                 status = TEST_FAILED;
1584                 goto error_exit;
1585         }
1586
1587         ret = verify_modexp(mod_exp, result_op);
1588         if (ret) {
1589                 RTE_LOG(ERR, USER1,
1590                          "operation verification failed\n");
1591                 status = TEST_FAILED;
1592         }
1593
1594 error_exit:
1595         if (sess != NULL) {
1596                 rte_cryptodev_asym_session_clear(dev_id, sess);
1597                 rte_cryptodev_asym_session_free(sess);
1598         }
1599
1600         if (op != NULL)
1601                 rte_crypto_op_free(op);
1602
1603         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1604
1605         return status;
1606 }
1607
1608 static int
1609 test_dh_keygenration(void)
1610 {
1611         int status;
1612
1613         debug_hexdump(stdout, "p:", dh_xform.dh.p.data, dh_xform.dh.p.length);
1614         debug_hexdump(stdout, "g:", dh_xform.dh.g.data, dh_xform.dh.g.length);
1615         debug_hexdump(stdout, "priv_key:", dh_test_params.priv_key.data,
1616                         dh_test_params.priv_key.length);
1617
1618         RTE_LOG(INFO, USER1,
1619                 "Test Public and Private key pair generation\n");
1620
1621         status = test_dh_gen_kp(&dh_xform);
1622         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1623
1624         RTE_LOG(INFO, USER1,
1625                 "Test Public Key Generation using pre-defined priv key\n");
1626
1627         status = test_dh_gen_pub_key(&dh_xform);
1628         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1629
1630         RTE_LOG(INFO, USER1,
1631                 "Test Private Key Generation only\n");
1632
1633         status = test_dh_gen_priv_key(&dh_xform);
1634         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1635
1636         RTE_LOG(INFO, USER1,
1637                 "Test shared secret compute\n");
1638
1639         status = test_dh_gen_shared_sec(&dh_xform);
1640         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1641
1642         return status;
1643 }
1644
1645 static int
1646 test_dsa_sign(void)
1647 {
1648         struct crypto_testsuite_params *ts_params = &testsuite_params;
1649         struct rte_mempool *op_mpool = ts_params->op_mpool;
1650         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1651         uint8_t dev_id = ts_params->valid_devs[0];
1652         struct rte_crypto_asym_op *asym_op = NULL;
1653         struct rte_crypto_op *op = NULL, *result_op = NULL;
1654         struct rte_cryptodev_asym_session *sess = NULL;
1655         int status = TEST_SUCCESS;
1656         uint8_t r[TEST_DH_MOD_LEN];
1657         uint8_t s[TEST_DH_MOD_LEN];
1658         uint8_t dgst[] = "35d81554afaad2cf18f3a1770d5fedc4ea5be344";
1659
1660         sess = rte_cryptodev_asym_session_create(sess_mpool);
1661         if (sess == NULL) {
1662                 RTE_LOG(ERR, USER1,
1663                                  "line %u FAILED: %s", __LINE__,
1664                                 "Session creation failed");
1665                 status = TEST_FAILED;
1666                 goto error_exit;
1667         }
1668         /* set up crypto op data structure */
1669         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1670         if (!op) {
1671                 RTE_LOG(ERR, USER1,
1672                         "line %u FAILED: %s",
1673                         __LINE__, "Failed to allocate asymmetric crypto "
1674                         "operation struct");
1675                 status = TEST_FAILED;
1676                 goto error_exit;
1677         }
1678         asym_op = op->asym;
1679
1680         debug_hexdump(stdout, "p: ", dsa_xform.dsa.p.data,
1681                         dsa_xform.dsa.p.length);
1682         debug_hexdump(stdout, "q: ", dsa_xform.dsa.q.data,
1683                         dsa_xform.dsa.q.length);
1684         debug_hexdump(stdout, "g: ", dsa_xform.dsa.g.data,
1685                         dsa_xform.dsa.g.length);
1686         debug_hexdump(stdout, "priv_key: ", dsa_xform.dsa.x.data,
1687                         dsa_xform.dsa.x.length);
1688
1689         if (rte_cryptodev_asym_session_init(dev_id, sess, &dsa_xform,
1690                                 sess_mpool) < 0) {
1691                 RTE_LOG(ERR, USER1,
1692                                 "line %u FAILED: %s",
1693                                 __LINE__, "unabled to config sym session");
1694                 status = TEST_FAILED;
1695                 goto error_exit;
1696         }
1697
1698         /* attach asymmetric crypto session to crypto operations */
1699         rte_crypto_op_attach_asym_session(op, sess);
1700         asym_op->dsa.op_type = RTE_CRYPTO_ASYM_OP_SIGN;
1701         asym_op->dsa.message.data = dgst;
1702         asym_op->dsa.message.length = sizeof(dgst);
1703         asym_op->dsa.r.length = sizeof(r);
1704         asym_op->dsa.r.data = r;
1705         asym_op->dsa.s.length = sizeof(s);
1706         asym_op->dsa.s.data = s;
1707
1708         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1709
1710         /* Process crypto operation */
1711         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1712                 RTE_LOG(ERR, USER1,
1713                         "line %u FAILED: %s",
1714                         __LINE__, "Error sending packet for operation");
1715                 status = TEST_FAILED;
1716                 goto error_exit;
1717         }
1718
1719         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1720                 rte_pause();
1721
1722         if (result_op == NULL) {
1723                 RTE_LOG(ERR, USER1,
1724                         "line %u FAILED: %s",
1725                         __LINE__, "Failed to process asym crypto op");
1726                 status = TEST_FAILED;
1727                 goto error_exit;
1728         }
1729
1730         asym_op = result_op->asym;
1731
1732         debug_hexdump(stdout, "r:",
1733                         asym_op->dsa.r.data, asym_op->dsa.r.length);
1734         debug_hexdump(stdout, "s:",
1735                         asym_op->dsa.s.data, asym_op->dsa.s.length);
1736
1737         /* Test PMD DSA sign verification using signer public key */
1738         asym_op->dsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
1739
1740         /* copy signer public key */
1741         asym_op->dsa.y.data = dsa_test_params.y.data;
1742         asym_op->dsa.y.length = dsa_test_params.y.length;
1743
1744         /* Process crypto operation */
1745         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1746                 RTE_LOG(ERR, USER1,
1747                         "line %u FAILED: %s",
1748                         __LINE__, "Error sending packet for operation");
1749                 status = TEST_FAILED;
1750                 goto error_exit;
1751         }
1752
1753         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1754                 rte_pause();
1755
1756         if (result_op == NULL) {
1757                 RTE_LOG(ERR, USER1,
1758                         "line %u FAILED: %s",
1759                         __LINE__, "Failed to process asym crypto op");
1760                 status = TEST_FAILED;
1761                 goto error_exit;
1762         }
1763
1764         if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
1765                 RTE_LOG(ERR, USER1,
1766                                 "line %u FAILED: %s",
1767                                 __LINE__, "Failed to process asym crypto op");
1768                 status = TEST_FAILED;
1769         }
1770 error_exit:
1771         if (sess != NULL) {
1772                 rte_cryptodev_asym_session_clear(dev_id, sess);
1773                 rte_cryptodev_asym_session_free(sess);
1774         }
1775         if (op != NULL)
1776                 rte_crypto_op_free(op);
1777         return status;
1778 }
1779
1780 static int
1781 test_dsa(void)
1782 {
1783         int status;
1784         status = test_dsa_sign();
1785         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1786         return status;
1787 }
1788
1789
1790 static struct unit_test_suite cryptodev_openssl_asym_testsuite  = {
1791         .suite_name = "Crypto Device OPENSSL ASYM Unit Test Suite",
1792         .setup = testsuite_setup,
1793         .teardown = testsuite_teardown,
1794         .unit_test_cases = {
1795                 TEST_CASE_ST(ut_setup, ut_teardown, test_capability),
1796                 TEST_CASE_ST(ut_setup, ut_teardown, test_dsa),
1797                 TEST_CASE_ST(ut_setup, ut_teardown, test_dh_keygenration),
1798                 TEST_CASE_ST(ut_setup, ut_teardown, test_rsa_enc_dec),
1799                 TEST_CASE_ST(ut_setup, ut_teardown, test_rsa_sign_verify),
1800                 TEST_CASE_ST(ut_setup, ut_teardown, test_rsa_enc_dec_crt),
1801                 TEST_CASE_ST(ut_setup, ut_teardown, test_rsa_sign_verify_crt),
1802                 TEST_CASE_ST(ut_setup, ut_teardown, test_mod_inv),
1803                 TEST_CASE_ST(ut_setup, ut_teardown, test_mod_exp),
1804                 TEST_CASE_ST(ut_setup, ut_teardown, test_one_by_one),
1805                 TEST_CASES_END() /**< NULL terminate unit test array */
1806         }
1807 };
1808
1809 static struct unit_test_suite cryptodev_qat_asym_testsuite  = {
1810         .suite_name = "Crypto Device QAT ASYM Unit Test Suite",
1811         .setup = testsuite_setup,
1812         .teardown = testsuite_teardown,
1813         .unit_test_cases = {
1814                 TEST_CASE_ST(ut_setup, ut_teardown, test_one_by_one),
1815                 TEST_CASES_END() /**< NULL terminate unit test array */
1816         }
1817 };
1818
1819 static struct unit_test_suite cryptodev_octeontx_asym_testsuite  = {
1820         .suite_name = "Crypto Device OCTEONTX ASYM Unit Test Suite",
1821         .setup = testsuite_setup,
1822         .teardown = testsuite_teardown,
1823         .unit_test_cases = {
1824                 TEST_CASE_ST(ut_setup, ut_teardown, test_capability),
1825                 TEST_CASE_ST(ut_setup, ut_teardown, test_rsa_enc_dec_crt),
1826                 TEST_CASE_ST(ut_setup, ut_teardown, test_rsa_sign_verify_crt),
1827                 TEST_CASE_ST(ut_setup, ut_teardown, test_mod_exp),
1828                 TEST_CASES_END() /**< NULL terminate unit test array */
1829         }
1830 };
1831
1832 static int
1833 test_cryptodev_openssl_asym(void)
1834 {
1835         gbl_driver_id = rte_cryptodev_driver_id_get(
1836                         RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
1837
1838         if (gbl_driver_id == -1) {
1839                 RTE_LOG(ERR, USER1, "OPENSSL PMD must be loaded. Check if "
1840                                 "CONFIG_RTE_LIBRTE_PMD_OPENSSL is enabled "
1841                                 "in config file to run this testsuite.\n");
1842                 return TEST_FAILED;
1843         }
1844
1845         return unit_test_suite_runner(&cryptodev_openssl_asym_testsuite);
1846 }
1847
1848 static int
1849 test_cryptodev_qat_asym(void)
1850 {
1851         gbl_driver_id = rte_cryptodev_driver_id_get(
1852                         RTE_STR(CRYPTODEV_NAME_QAT_ASYM_PMD));
1853
1854         if (gbl_driver_id == -1) {
1855                 RTE_LOG(ERR, USER1, "QAT PMD must be loaded. Check if "
1856                                     "CONFIG_RTE_LIBRTE_PMD_QAT_ASYM is enabled "
1857                                     "in config file to run this testsuite.\n");
1858                 return TEST_FAILED;
1859         }
1860
1861         return unit_test_suite_runner(&cryptodev_qat_asym_testsuite);
1862 }
1863
1864 static int
1865 test_cryptodev_octeontx_asym(void)
1866 {
1867         gbl_driver_id = rte_cryptodev_driver_id_get(
1868                         RTE_STR(CRYPTODEV_NAME_OCTEONTX_SYM_PMD));
1869         if (gbl_driver_id == -1) {
1870                 RTE_LOG(ERR, USER1, "OCTEONTX PMD must be loaded. Check if "
1871                                 "CONFIG_RTE_LIBRTE_PMD_OCTEONTX_CRYPTO is "
1872                                 "enabled in config file to run this "
1873                                 "testsuite.\n");
1874                 return TEST_FAILED;
1875         }
1876         return unit_test_suite_runner(&cryptodev_octeontx_asym_testsuite);
1877 }
1878
1879 static int
1880 test_cryptodev_octeontx2_asym(void)
1881 {
1882         gbl_driver_id = rte_cryptodev_driver_id_get(
1883                         RTE_STR(CRYPTODEV_NAME_OCTEONTX2_PMD));
1884         if (gbl_driver_id == -1) {
1885                 RTE_LOG(ERR, USER1, "OCTEONTX2 PMD must be loaded. Check if "
1886                                 "CONFIG_RTE_LIBRTE_PMD_OCTEONTX2_CRYPTO is "
1887                                 "enabled in config file to run this "
1888                                 "testsuite.\n");
1889                 return TEST_FAILED;
1890         }
1891
1892         /* Use test suite registered for crypto_octeontx PMD */
1893         return unit_test_suite_runner(&cryptodev_octeontx_asym_testsuite);
1894 }
1895
1896 REGISTER_TEST_COMMAND(cryptodev_openssl_asym_autotest,
1897                                           test_cryptodev_openssl_asym);
1898
1899 REGISTER_TEST_COMMAND(cryptodev_qat_asym_autotest, test_cryptodev_qat_asym);
1900
1901 REGISTER_TEST_COMMAND(cryptodev_octeontx_asym_autotest,
1902                                           test_cryptodev_octeontx_asym);
1903
1904 REGISTER_TEST_COMMAND(cryptodev_octeontx2_asym_autotest,
1905                                           test_cryptodev_octeontx2_asym);