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