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