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