replace zero-length arrays with flexible ones
[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.padding.type = 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.padding.type = 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.padding.type = 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.padding.type = 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.padding.type = 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_KEY_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                         if (capa->xform_type == RTE_CRYPTO_ASYM_XFORM_DH)
981                                 printf(" %s", rte_crypto_asym_ke_strings[i]);
982                         else
983                                 printf(" %s", rte_crypto_asym_op_strings[i]);
984                 }
985         }
986         switch (capa->xform_type) {
987         case RTE_CRYPTO_ASYM_XFORM_RSA:
988         case RTE_CRYPTO_ASYM_XFORM_MODINV:
989         case RTE_CRYPTO_ASYM_XFORM_MODEX:
990         case RTE_CRYPTO_ASYM_XFORM_DH:
991         case RTE_CRYPTO_ASYM_XFORM_DSA:
992                 printf(" modlen: min %d max %d increment %d",
993                                 capa->modlen.min,
994                                 capa->modlen.max,
995                                 capa->modlen.increment);
996         break;
997         case RTE_CRYPTO_ASYM_XFORM_ECDSA:
998         case RTE_CRYPTO_ASYM_XFORM_ECPM:
999         default:
1000                 break;
1001         }
1002         printf("\n");
1003 }
1004
1005 static int
1006 test_capability(void)
1007 {
1008         struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
1009         uint8_t dev_id = ts_params->valid_devs[0];
1010         struct rte_cryptodev_info dev_info;
1011         const struct rte_cryptodev_capabilities *dev_capa;
1012         int i = 0;
1013         struct rte_cryptodev_asym_capability_idx idx;
1014         const struct rte_cryptodev_asymmetric_xform_capability *capa;
1015
1016         rte_cryptodev_info_get(dev_id, &dev_info);
1017         if (!(dev_info.feature_flags &
1018                                 RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)) {
1019                 RTE_LOG(INFO, USER1,
1020                                 "Device doesn't support asymmetric. Test Skipped\n");
1021                 return TEST_SUCCESS;
1022         }
1023
1024         /* print xform capability */
1025         for (i = 0;
1026                 dev_info.capabilities[i].op != RTE_CRYPTO_OP_TYPE_UNDEFINED;
1027                 i++) {
1028                 dev_capa = &(dev_info.capabilities[i]);
1029                 if (dev_info.capabilities[i].op ==
1030                                 RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
1031                         idx.type = dev_capa->asym.xform_capa.xform_type;
1032
1033                         capa = rte_cryptodev_asym_capability_get(dev_id,
1034                                 (const struct
1035                                 rte_cryptodev_asym_capability_idx *) &idx);
1036                         print_asym_capa(capa);
1037                         }
1038         }
1039         return TEST_SUCCESS;
1040 }
1041
1042 static int
1043 test_dh_gen_shared_sec(struct rte_crypto_asym_xform *xfrm)
1044 {
1045         struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
1046         struct rte_mempool *op_mpool = ts_params->op_mpool;
1047         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1048         uint8_t dev_id = ts_params->valid_devs[0];
1049         struct rte_crypto_asym_op *asym_op = NULL;
1050         struct rte_crypto_op *op = NULL, *result_op = NULL;
1051         void *sess = NULL;
1052         int ret, status = TEST_SUCCESS;
1053         uint8_t output[TEST_DH_MOD_LEN];
1054         struct rte_crypto_asym_xform xform = *xfrm;
1055         uint8_t peer[] = "01234567890123456789012345678901234567890123456789";
1056
1057         /* set up crypto op data structure */
1058         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1059         if (!op) {
1060                 RTE_LOG(ERR, USER1,
1061                         "line %u FAILED: %s",
1062                         __LINE__, "Failed to allocate asymmetric crypto "
1063                         "operation struct");
1064                 status = TEST_FAILED;
1065                 goto error_exit;
1066         }
1067         asym_op = op->asym;
1068
1069         /* Setup a xform and op to generate private key only */
1070         xform.next = NULL;
1071         asym_op->dh.ke_type = RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
1072         asym_op->dh.priv_key.data = dh_test_params.priv_key.data;
1073         asym_op->dh.priv_key.length = dh_test_params.priv_key.length;
1074         asym_op->dh.pub_key.data = (uint8_t *)peer;
1075         asym_op->dh.pub_key.length = sizeof(peer);
1076         asym_op->dh.shared_secret.data = output;
1077         asym_op->dh.shared_secret.length = sizeof(output);
1078
1079         ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
1080         if (ret < 0) {
1081                 RTE_LOG(ERR, USER1,
1082                                 "line %u FAILED: %s", __LINE__,
1083                                 "Session creation failed");
1084                 status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
1085                 goto error_exit;
1086         }
1087
1088         /* attach asymmetric crypto session to crypto operations */
1089         rte_crypto_op_attach_asym_session(op, sess);
1090
1091         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1092
1093         /* Process crypto operation */
1094         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1095                 RTE_LOG(ERR, USER1,
1096                         "line %u FAILED: %s",
1097                         __LINE__, "Error sending packet for operation");
1098                 status = TEST_FAILED;
1099                 goto error_exit;
1100         }
1101
1102         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1103                 rte_pause();
1104
1105         if (result_op == NULL) {
1106                 RTE_LOG(ERR, USER1,
1107                         "line %u FAILED: %s",
1108                         __LINE__, "Failed to process asym crypto op");
1109                 status = TEST_FAILED;
1110                 goto error_exit;
1111         }
1112
1113         debug_hexdump(stdout, "shared secret:",
1114                         asym_op->dh.shared_secret.data,
1115                         asym_op->dh.shared_secret.length);
1116
1117 error_exit:
1118         if (sess != NULL)
1119                 rte_cryptodev_asym_session_free(dev_id, sess);
1120         if (op != NULL)
1121                 rte_crypto_op_free(op);
1122         return status;
1123 }
1124
1125 static int
1126 test_dh_gen_priv_key(struct rte_crypto_asym_xform *xfrm)
1127 {
1128         struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
1129         struct rte_mempool *op_mpool = ts_params->op_mpool;
1130         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1131         uint8_t dev_id = ts_params->valid_devs[0];
1132         struct rte_crypto_asym_op *asym_op = NULL;
1133         struct rte_crypto_op *op = NULL, *result_op = NULL;
1134         void *sess = NULL;
1135         int ret, status = TEST_SUCCESS;
1136         uint8_t output[TEST_DH_MOD_LEN];
1137         struct rte_crypto_asym_xform xform = *xfrm;
1138
1139         /* set up crypto op data structure */
1140         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1141         if (!op) {
1142                 RTE_LOG(ERR, USER1,
1143                         "line %u FAILED: %s",
1144                         __LINE__, "Failed to allocate asymmetric crypto "
1145                         "operation struct");
1146                 status = TEST_FAILED;
1147                 goto error_exit;
1148         }
1149         asym_op = op->asym;
1150
1151         /* Setup a xform and op to generate private key only */
1152         xform.next = NULL;
1153         asym_op->dh.ke_type = RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE;
1154         asym_op->dh.priv_key.data = output;
1155         asym_op->dh.priv_key.length = sizeof(output);
1156
1157         ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
1158         if (ret < 0) {
1159                 RTE_LOG(ERR, USER1,
1160                                 "line %u FAILED: %s", __LINE__,
1161                                 "Session creation failed");
1162                 status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
1163                 goto error_exit;
1164         }
1165
1166         /* attach asymmetric crypto session to crypto operations */
1167         rte_crypto_op_attach_asym_session(op, sess);
1168
1169         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1170
1171         /* Process crypto operation */
1172         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1173                 RTE_LOG(ERR, USER1,
1174                         "line %u FAILED: %s",
1175                         __LINE__, "Error sending packet for operation");
1176                 status = TEST_FAILED;
1177                 goto error_exit;
1178         }
1179
1180         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1181                 rte_pause();
1182
1183         if (result_op == NULL) {
1184                 RTE_LOG(ERR, USER1,
1185                         "line %u FAILED: %s",
1186                         __LINE__, "Failed to process asym crypto op");
1187                 status = TEST_FAILED;
1188                 goto error_exit;
1189         }
1190
1191         debug_hexdump(stdout, "private key:",
1192                         asym_op->dh.priv_key.data,
1193                         asym_op->dh.priv_key.length);
1194
1195
1196 error_exit:
1197         if (sess != NULL)
1198                 rte_cryptodev_asym_session_free(dev_id, sess);
1199         if (op != NULL)
1200                 rte_crypto_op_free(op);
1201
1202         return status;
1203 }
1204
1205
1206 static int
1207 test_dh_gen_pub_key(struct rte_crypto_asym_xform *xfrm)
1208 {
1209         struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
1210         struct rte_mempool *op_mpool = ts_params->op_mpool;
1211         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1212         uint8_t dev_id = ts_params->valid_devs[0];
1213         struct rte_crypto_asym_op *asym_op = NULL;
1214         struct rte_crypto_op *op = NULL, *result_op = NULL;
1215         void *sess = NULL;
1216         int ret, status = TEST_SUCCESS;
1217         uint8_t output[TEST_DH_MOD_LEN];
1218         struct rte_crypto_asym_xform xform = *xfrm;
1219
1220         /* set up crypto op data structure */
1221         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1222         if (!op) {
1223                 RTE_LOG(ERR, USER1,
1224                         "line %u FAILED: %s",
1225                         __LINE__, "Failed to allocate asymmetric crypto "
1226                         "operation struct");
1227                 status = TEST_FAILED;
1228                 goto error_exit;
1229         }
1230         asym_op = op->asym;
1231         /* Setup a xform chain to generate public key
1232          * using test private key
1233          *
1234          */
1235         xform.next = NULL;
1236
1237         asym_op->dh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE;
1238         asym_op->dh.pub_key.data = output;
1239         asym_op->dh.pub_key.length = sizeof(output);
1240         /* load pre-defined private key */
1241         asym_op->dh.priv_key.data = rte_malloc(NULL,
1242                                         dh_test_params.priv_key.length,
1243                                         0);
1244         asym_op->dh.priv_key = dh_test_params.priv_key;
1245
1246         ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
1247         if (ret < 0) {
1248                 RTE_LOG(ERR, USER1,
1249                                 "line %u FAILED: %s", __LINE__,
1250                                 "Session creation failed");
1251                 status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
1252                 goto error_exit;
1253         }
1254
1255         /* attach asymmetric crypto session to crypto operations */
1256         rte_crypto_op_attach_asym_session(op, sess);
1257
1258         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1259
1260         /* Process crypto operation */
1261         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1262                 RTE_LOG(ERR, USER1,
1263                         "line %u FAILED: %s",
1264                         __LINE__, "Error sending packet for operation");
1265                 status = TEST_FAILED;
1266                 goto error_exit;
1267         }
1268
1269         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1270                 rte_pause();
1271
1272         if (result_op == NULL) {
1273                 RTE_LOG(ERR, USER1,
1274                         "line %u FAILED: %s",
1275                         __LINE__, "Failed to process asym crypto op");
1276                 status = TEST_FAILED;
1277                 goto error_exit;
1278         }
1279
1280         debug_hexdump(stdout, "pub key:",
1281                         asym_op->dh.pub_key.data, asym_op->dh.pub_key.length);
1282
1283         debug_hexdump(stdout, "priv key:",
1284                         asym_op->dh.priv_key.data, asym_op->dh.priv_key.length);
1285
1286 error_exit:
1287         if (sess != NULL)
1288                 rte_cryptodev_asym_session_free(dev_id, sess);
1289         if (op != NULL)
1290                 rte_crypto_op_free(op);
1291
1292         return status;
1293 }
1294
1295 static int
1296 test_dh_gen_kp(struct rte_crypto_asym_xform *xfrm)
1297 {
1298         struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
1299         struct rte_mempool *op_mpool = ts_params->op_mpool;
1300         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1301         uint8_t dev_id = ts_params->valid_devs[0];
1302         struct rte_crypto_asym_op *asym_op = NULL;
1303         struct rte_crypto_op *op = NULL, *result_op = NULL;
1304         void *sess = NULL;
1305         int ret, status = TEST_SUCCESS;
1306         uint8_t out_pub_key[TEST_DH_MOD_LEN];
1307         uint8_t out_prv_key[TEST_DH_MOD_LEN];
1308         struct rte_crypto_asym_xform pub_key_xform;
1309         struct rte_crypto_asym_xform xform = *xfrm;
1310
1311         /* set up crypto op data structure */
1312         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1313         if (!op) {
1314                 RTE_LOG(ERR, USER1,
1315                         "line %u FAILED: %s",
1316                         __LINE__, "Failed to allocate asymmetric crypto "
1317                         "operation struct");
1318                 status = TEST_FAILED;
1319                 goto error_exit;
1320         }
1321         asym_op = op->asym;
1322         /* Setup a xform chain to generate
1323          * private key first followed by
1324          * public key
1325          */
1326         pub_key_xform.xform_type = RTE_CRYPTO_ASYM_XFORM_DH;
1327         xform.next = &pub_key_xform;
1328
1329         asym_op->dh.ke_type = RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE;
1330         asym_op->dh.pub_key.data = out_pub_key;
1331         asym_op->dh.pub_key.length = sizeof(out_pub_key);
1332         asym_op->dh.priv_key.data = out_prv_key;
1333         asym_op->dh.priv_key.length = 0;
1334
1335         ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
1336         if (ret < 0) {
1337                 RTE_LOG(ERR, USER1,
1338                                 "line %u FAILED: %s", __LINE__,
1339                                 "Session creation failed");
1340                 status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
1341                 goto error_exit;
1342         }
1343
1344         /* attach asymmetric crypto session to crypto operations */
1345         rte_crypto_op_attach_asym_session(op, sess);
1346
1347         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1348
1349         /* Process crypto operation */
1350         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1351                 RTE_LOG(ERR, USER1,
1352                         "line %u FAILED: %s",
1353                         __LINE__, "Error sending packet for operation");
1354                 status = TEST_FAILED;
1355                 goto error_exit;
1356         }
1357
1358         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1359                 rte_pause();
1360
1361         if (result_op == NULL) {
1362                 RTE_LOG(ERR, USER1,
1363                         "line %u FAILED: %s",
1364                         __LINE__, "Failed to process asym crypto op");
1365                 status = TEST_FAILED;
1366                 goto error_exit;
1367         }
1368         debug_hexdump(stdout, "priv key:",
1369                         out_prv_key, asym_op->dh.priv_key.length);
1370         debug_hexdump(stdout, "pub key:",
1371                         out_pub_key, asym_op->dh.pub_key.length);
1372
1373 error_exit:
1374         if (sess != NULL)
1375                 rte_cryptodev_asym_session_free(dev_id, sess);
1376         if (op != NULL)
1377                 rte_crypto_op_free(op);
1378
1379         return status;
1380 }
1381
1382 static int
1383 test_mod_inv(void)
1384 {
1385         struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
1386         struct rte_mempool *op_mpool = ts_params->op_mpool;
1387         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1388         uint8_t dev_id = ts_params->valid_devs[0];
1389         struct rte_crypto_asym_op *asym_op = NULL;
1390         struct rte_crypto_op *op = NULL, *result_op = NULL;
1391         void *sess = NULL;
1392         int status = TEST_SUCCESS;
1393         struct rte_cryptodev_asym_capability_idx cap_idx;
1394         const struct rte_cryptodev_asymmetric_xform_capability *capability;
1395         uint8_t input[TEST_DATA_SIZE] = {0};
1396         int ret = 0;
1397         uint8_t result[sizeof(mod_p)] = { 0 };
1398
1399         if (rte_cryptodev_asym_get_xform_enum(
1400                 &modinv_xform.xform_type, "modinv") < 0) {
1401                 RTE_LOG(ERR, USER1,
1402                                  "Invalid ASYM algorithm specified\n");
1403                 return -1;
1404         }
1405
1406         cap_idx.type = modinv_xform.xform_type;
1407         capability = rte_cryptodev_asym_capability_get(dev_id,
1408                                         &cap_idx);
1409
1410         if (capability == NULL) {
1411                 RTE_LOG(INFO, USER1,
1412                         "Device doesn't support MOD INV. Test Skipped\n");
1413                 return TEST_SKIPPED;
1414         }
1415
1416         if (rte_cryptodev_asym_xform_capability_check_modlen(
1417                 capability,
1418                 modinv_xform.modinv.modulus.length)) {
1419                 RTE_LOG(ERR, USER1,
1420                                  "Invalid MODULUS length specified\n");
1421                                 return TEST_SKIPPED;
1422                 }
1423
1424         ret = rte_cryptodev_asym_session_create(dev_id, &modinv_xform, sess_mpool, &sess);
1425         if (ret < 0) {
1426                 RTE_LOG(ERR, USER1, "line %u "
1427                                 "FAILED: %s", __LINE__,
1428                                 "Session creation failed");
1429                 status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
1430                 goto error_exit;
1431         }
1432
1433         /* generate crypto op data structure */
1434         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1435         if (!op) {
1436                 RTE_LOG(ERR, USER1,
1437                         "line %u FAILED: %s",
1438                         __LINE__, "Failed to allocate asymmetric crypto "
1439                         "operation struct");
1440                 status = TEST_FAILED;
1441                 goto error_exit;
1442         }
1443
1444         asym_op = op->asym;
1445         memcpy(input, base, sizeof(base));
1446         asym_op->modinv.base.data = input;
1447         asym_op->modinv.base.length = sizeof(base);
1448         asym_op->modinv.result.data = result;
1449         asym_op->modinv.result.length = sizeof(result);
1450
1451         /* attach asymmetric crypto session to crypto operations */
1452         rte_crypto_op_attach_asym_session(op, sess);
1453
1454         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1455
1456         /* Process crypto operation */
1457         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1458                 RTE_LOG(ERR, USER1,
1459                         "line %u FAILED: %s",
1460                         __LINE__, "Error sending packet for operation");
1461                 status = TEST_FAILED;
1462                 goto error_exit;
1463         }
1464
1465         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1466                 rte_pause();
1467
1468         if (result_op == NULL) {
1469                 RTE_LOG(ERR, USER1,
1470                                 "line %u FAILED: %s",
1471                                 __LINE__, "Failed to process asym crypto op");
1472                 status = TEST_FAILED;
1473                 goto error_exit;
1474         }
1475
1476         ret = verify_modinv(mod_inv, result_op);
1477         if (ret) {
1478                 RTE_LOG(ERR, USER1,
1479                          "operation verification failed\n");
1480                 status = TEST_FAILED;
1481         }
1482
1483 error_exit:
1484         if (sess)
1485                 rte_cryptodev_asym_session_free(dev_id, sess);
1486
1487         if (op)
1488                 rte_crypto_op_free(op);
1489
1490         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1491
1492         return status;
1493 }
1494
1495 static int
1496 test_mod_exp(void)
1497 {
1498         struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
1499         struct rte_mempool *op_mpool = ts_params->op_mpool;
1500         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1501         uint8_t dev_id = ts_params->valid_devs[0];
1502         struct rte_crypto_asym_op *asym_op = NULL;
1503         struct rte_crypto_op *op = NULL, *result_op = NULL;
1504         void *sess = NULL;
1505         int status = TEST_SUCCESS;
1506         struct rte_cryptodev_asym_capability_idx cap_idx;
1507         const struct rte_cryptodev_asymmetric_xform_capability *capability;
1508         uint8_t input[TEST_DATA_SIZE] = {0};
1509         int ret = 0;
1510         uint8_t result[sizeof(mod_p)] = { 0 };
1511
1512         if (rte_cryptodev_asym_get_xform_enum(&modex_xform.xform_type,
1513                 "modexp")
1514                 < 0) {
1515                 RTE_LOG(ERR, USER1,
1516                                 "Invalid ASYM algorithm specified\n");
1517                 return -1;
1518         }
1519
1520         /* check for modlen capability */
1521         cap_idx.type = modex_xform.xform_type;
1522         capability = rte_cryptodev_asym_capability_get(dev_id, &cap_idx);
1523
1524         if (capability == NULL) {
1525                 RTE_LOG(INFO, USER1,
1526                         "Device doesn't support MOD EXP. Test Skipped\n");
1527                 return TEST_SKIPPED;
1528         }
1529
1530         if (rte_cryptodev_asym_xform_capability_check_modlen(
1531                         capability, modex_xform.modex.modulus.length)) {
1532                 RTE_LOG(ERR, USER1,
1533                                 "Invalid MODULUS length specified\n");
1534                                 return TEST_SKIPPED;
1535                 }
1536
1537         /* Create op, create session, and process packets. 8< */
1538         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1539         if (!op) {
1540                 RTE_LOG(ERR, USER1,
1541                         "line %u FAILED: %s",
1542                         __LINE__, "Failed to allocate asymmetric crypto "
1543                         "operation struct");
1544                 status = TEST_FAILED;
1545                 goto error_exit;
1546         }
1547
1548         ret = rte_cryptodev_asym_session_create(dev_id, &modex_xform, sess_mpool, &sess);
1549         if (ret < 0) {
1550                 RTE_LOG(ERR, USER1,
1551                                  "line %u "
1552                                 "FAILED: %s", __LINE__,
1553                                 "Session creation failed");
1554                 status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
1555                 goto error_exit;
1556         }
1557
1558         asym_op = op->asym;
1559         memcpy(input, base, sizeof(base));
1560         asym_op->modex.base.data = input;
1561         asym_op->modex.base.length = sizeof(base);
1562         asym_op->modex.result.data = result;
1563         asym_op->modex.result.length = sizeof(result);
1564         /* attach asymmetric crypto session to crypto operations */
1565         rte_crypto_op_attach_asym_session(op, sess);
1566
1567         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1568         /* Process crypto operation */
1569         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1570                 RTE_LOG(ERR, USER1,
1571                                 "line %u FAILED: %s",
1572                                 __LINE__, "Error sending packet for operation");
1573                 status = TEST_FAILED;
1574                 goto error_exit;
1575         }
1576
1577         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1578                 rte_pause();
1579
1580         if (result_op == NULL) {
1581                 RTE_LOG(ERR, USER1,
1582                                 "line %u FAILED: %s",
1583                                 __LINE__, "Failed to process asym crypto op");
1584                 status = TEST_FAILED;
1585                 goto error_exit;
1586         }
1587         /* >8 End of create op, create session, and process packets section. */
1588         ret = verify_modexp(mod_exp, result_op);
1589         if (ret) {
1590                 RTE_LOG(ERR, USER1,
1591                          "operation verification failed\n");
1592                 status = TEST_FAILED;
1593         }
1594
1595 error_exit:
1596         if (sess != NULL)
1597                 rte_cryptodev_asym_session_free(dev_id, sess);
1598
1599         if (op != NULL)
1600                 rte_crypto_op_free(op);
1601
1602         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1603
1604         return status;
1605 }
1606
1607 static int
1608 test_dh_keygenration(void)
1609 {
1610         int status;
1611
1612         debug_hexdump(stdout, "p:", dh_xform.dh.p.data, dh_xform.dh.p.length);
1613         debug_hexdump(stdout, "g:", dh_xform.dh.g.data, dh_xform.dh.g.length);
1614         debug_hexdump(stdout, "priv_key:", dh_test_params.priv_key.data,
1615                         dh_test_params.priv_key.length);
1616
1617         RTE_LOG(INFO, USER1,
1618                 "Test Public and Private key pair generation\n");
1619
1620         status = test_dh_gen_kp(&dh_xform);
1621         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1622
1623         RTE_LOG(INFO, USER1,
1624                 "Test Public Key Generation using pre-defined priv key\n");
1625
1626         status = test_dh_gen_pub_key(&dh_xform);
1627         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1628
1629         RTE_LOG(INFO, USER1,
1630                 "Test Private Key Generation only\n");
1631
1632         status = test_dh_gen_priv_key(&dh_xform);
1633         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1634
1635         RTE_LOG(INFO, USER1,
1636                 "Test shared secret compute\n");
1637
1638         status = test_dh_gen_shared_sec(&dh_xform);
1639         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1640
1641         return status;
1642 }
1643
1644 static int
1645 test_dsa_sign(void)
1646 {
1647         struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
1648         struct rte_mempool *op_mpool = ts_params->op_mpool;
1649         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1650         uint8_t dev_id = ts_params->valid_devs[0];
1651         struct rte_crypto_asym_op *asym_op = NULL;
1652         struct rte_crypto_op *op = NULL, *result_op = NULL;
1653         void *sess = NULL;
1654         int status = TEST_SUCCESS;
1655         uint8_t r[TEST_DH_MOD_LEN];
1656         uint8_t s[TEST_DH_MOD_LEN];
1657         uint8_t dgst[] = "35d81554afaad2cf18f3a1770d5fedc4ea5be344";
1658         int ret;
1659
1660         ret = rte_cryptodev_asym_session_create(dev_id, &dsa_xform, sess_mpool, &sess);
1661         if (ret < 0) {
1662                 RTE_LOG(ERR, USER1,
1663                                  "line %u FAILED: %s", __LINE__,
1664                                 "Session creation failed");
1665                 status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
1666                 goto error_exit;
1667         }
1668         /* set up crypto op data structure */
1669         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1670         if (!op) {
1671                 RTE_LOG(ERR, USER1,
1672                         "line %u FAILED: %s",
1673                         __LINE__, "Failed to allocate asymmetric crypto "
1674                         "operation struct");
1675                 status = TEST_FAILED;
1676                 goto error_exit;
1677         }
1678         asym_op = op->asym;
1679
1680         debug_hexdump(stdout, "p: ", dsa_xform.dsa.p.data,
1681                         dsa_xform.dsa.p.length);
1682         debug_hexdump(stdout, "q: ", dsa_xform.dsa.q.data,
1683                         dsa_xform.dsa.q.length);
1684         debug_hexdump(stdout, "g: ", dsa_xform.dsa.g.data,
1685                         dsa_xform.dsa.g.length);
1686         debug_hexdump(stdout, "priv_key: ", dsa_xform.dsa.x.data,
1687                         dsa_xform.dsa.x.length);
1688
1689         /* attach asymmetric crypto session to crypto operations */
1690         rte_crypto_op_attach_asym_session(op, sess);
1691         asym_op->dsa.op_type = RTE_CRYPTO_ASYM_OP_SIGN;
1692         asym_op->dsa.message.data = dgst;
1693         asym_op->dsa.message.length = sizeof(dgst);
1694         asym_op->dsa.r.length = sizeof(r);
1695         asym_op->dsa.r.data = r;
1696         asym_op->dsa.s.length = sizeof(s);
1697         asym_op->dsa.s.data = s;
1698
1699         RTE_LOG(DEBUG, USER1, "Process ASYM operation");
1700
1701         /* Process crypto operation */
1702         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1703                 RTE_LOG(ERR, USER1,
1704                         "line %u FAILED: %s",
1705                         __LINE__, "Error sending packet for operation");
1706                 status = TEST_FAILED;
1707                 goto error_exit;
1708         }
1709
1710         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1711                 rte_pause();
1712
1713         if (result_op == NULL) {
1714                 RTE_LOG(ERR, USER1,
1715                         "line %u FAILED: %s",
1716                         __LINE__, "Failed to process asym crypto op");
1717                 status = TEST_FAILED;
1718                 goto error_exit;
1719         }
1720
1721         asym_op = result_op->asym;
1722
1723         debug_hexdump(stdout, "r:",
1724                         asym_op->dsa.r.data, asym_op->dsa.r.length);
1725         debug_hexdump(stdout, "s:",
1726                         asym_op->dsa.s.data, asym_op->dsa.s.length);
1727
1728         /* Test PMD DSA sign verification using signer public key */
1729         asym_op->dsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
1730
1731         /* copy signer public key */
1732         asym_op->dsa.y.data = dsa_test_params.y.data;
1733         asym_op->dsa.y.length = dsa_test_params.y.length;
1734
1735         /* Process crypto operation */
1736         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1737                 RTE_LOG(ERR, USER1,
1738                         "line %u FAILED: %s",
1739                         __LINE__, "Error sending packet for operation");
1740                 status = TEST_FAILED;
1741                 goto error_exit;
1742         }
1743
1744         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1745                 rte_pause();
1746
1747         if (result_op == NULL) {
1748                 RTE_LOG(ERR, USER1,
1749                         "line %u FAILED: %s",
1750                         __LINE__, "Failed to process asym crypto op");
1751                 status = TEST_FAILED;
1752                 goto error_exit;
1753         }
1754
1755         if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
1756                 RTE_LOG(ERR, USER1,
1757                                 "line %u FAILED: %s",
1758                                 __LINE__, "Failed to process asym crypto op");
1759                 status = TEST_FAILED;
1760         }
1761 error_exit:
1762         if (sess != NULL)
1763                 rte_cryptodev_asym_session_free(dev_id, sess);
1764         if (op != NULL)
1765                 rte_crypto_op_free(op);
1766         return status;
1767 }
1768
1769 static int
1770 test_dsa(void)
1771 {
1772         int status;
1773         status = test_dsa_sign();
1774         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1775         return status;
1776 }
1777
1778 static int
1779 test_ecdsa_sign_verify(enum curve curve_id)
1780 {
1781         struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
1782         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1783         struct rte_mempool *op_mpool = ts_params->op_mpool;
1784         struct crypto_testsuite_ecdsa_params input_params;
1785         void *sess = NULL;
1786         uint8_t dev_id = ts_params->valid_devs[0];
1787         struct rte_crypto_op *result_op = NULL;
1788         uint8_t output_buf_r[TEST_DATA_SIZE];
1789         uint8_t output_buf_s[TEST_DATA_SIZE];
1790         struct rte_crypto_asym_xform xform;
1791         struct rte_crypto_asym_op *asym_op;
1792         struct rte_cryptodev_info dev_info;
1793         struct rte_crypto_op *op = NULL;
1794         int ret, status = TEST_SUCCESS;
1795
1796         switch (curve_id) {
1797         case SECP192R1:
1798                 input_params = ecdsa_param_secp192r1;
1799                 break;
1800         case SECP224R1:
1801                 input_params = ecdsa_param_secp224r1;
1802                 break;
1803         case SECP256R1:
1804                 input_params = ecdsa_param_secp256r1;
1805                 break;
1806         case SECP384R1:
1807                 input_params = ecdsa_param_secp384r1;
1808                 break;
1809         case SECP521R1:
1810                 input_params = ecdsa_param_secp521r1;
1811                 break;
1812         default:
1813                 RTE_LOG(ERR, USER1,
1814                                 "line %u FAILED: %s", __LINE__,
1815                                 "Unsupported curve id\n");
1816                 status = TEST_FAILED;
1817                 goto exit;
1818         }
1819
1820         rte_cryptodev_info_get(dev_id, &dev_info);
1821
1822         /* Setup crypto op data structure */
1823         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1824         if (op == NULL) {
1825                 RTE_LOG(ERR, USER1,
1826                                 "line %u FAILED: %s", __LINE__,
1827                                 "Failed to allocate asymmetric crypto "
1828                                 "operation struct\n");
1829                 status = TEST_FAILED;
1830                 goto exit;
1831         }
1832         asym_op = op->asym;
1833
1834         /* Setup asym xform */
1835         xform.next = NULL;
1836         xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDSA;
1837         xform.ec.curve_id = input_params.curve;
1838
1839         ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
1840         if (ret < 0) {
1841                 RTE_LOG(ERR, USER1,
1842                                 "line %u FAILED: %s", __LINE__,
1843                                 "Session creation failed\n");
1844                 status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
1845                 goto exit;
1846         }
1847
1848         /* Attach asymmetric crypto session to crypto operations */
1849         rte_crypto_op_attach_asym_session(op, sess);
1850
1851         /* Compute sign */
1852
1853         /* Populate op with operational details */
1854         op->asym->ecdsa.op_type = RTE_CRYPTO_ASYM_OP_SIGN;
1855         op->asym->ecdsa.message.data = input_params.digest.data;
1856         op->asym->ecdsa.message.length = input_params.digest.length;
1857         op->asym->ecdsa.k.data = input_params.scalar.data;
1858         op->asym->ecdsa.k.length = input_params.scalar.length;
1859         op->asym->ecdsa.pkey.data = input_params.pkey.data;
1860         op->asym->ecdsa.pkey.length = input_params.pkey.length;
1861
1862         /* Init out buf */
1863         op->asym->ecdsa.r.data = output_buf_r;
1864         op->asym->ecdsa.s.data = output_buf_s;
1865
1866         RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
1867
1868         /* Process crypto operation */
1869         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1870                 RTE_LOG(ERR, USER1,
1871                                 "line %u FAILED: %s", __LINE__,
1872                                 "Error sending packet for operation\n");
1873                 status = TEST_FAILED;
1874                 goto exit;
1875         }
1876
1877         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1878                 rte_pause();
1879
1880         if (result_op == NULL) {
1881                 RTE_LOG(ERR, USER1,
1882                                 "line %u FAILED: %s", __LINE__,
1883                                 "Failed to process asym crypto op\n");
1884                 status = TEST_FAILED;
1885                 goto exit;
1886         }
1887
1888         if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
1889                 RTE_LOG(ERR, USER1,
1890                                 "line %u FAILED: %s", __LINE__,
1891                                 "Failed to process asym crypto op\n");
1892                 status = TEST_FAILED;
1893                 goto exit;
1894         }
1895
1896         asym_op = result_op->asym;
1897
1898         debug_hexdump(stdout, "r:",
1899                         asym_op->ecdsa.r.data, asym_op->ecdsa.r.length);
1900         debug_hexdump(stdout, "s:",
1901                         asym_op->ecdsa.s.data, asym_op->ecdsa.s.length);
1902
1903         ret = verify_ecdsa_sign(input_params.sign_r.data,
1904                                 input_params.sign_s.data, result_op);
1905         if (ret) {
1906                 status = TEST_FAILED;
1907                 RTE_LOG(ERR, USER1,
1908                                 "line %u FAILED: %s", __LINE__,
1909                                 "ECDSA sign failed.\n");
1910                 goto exit;
1911         }
1912
1913         /* Verify sign */
1914
1915         /* Populate op with operational details */
1916         op->asym->ecdsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
1917         op->asym->ecdsa.q.x.data = input_params.pubkey_qx.data;
1918         op->asym->ecdsa.q.x.length = input_params.pubkey_qx.length;
1919         op->asym->ecdsa.q.y.data = input_params.pubkey_qy.data;
1920         op->asym->ecdsa.q.y.length = input_params.pubkey_qx.length;
1921         op->asym->ecdsa.r.data = asym_op->ecdsa.r.data;
1922         op->asym->ecdsa.r.length = asym_op->ecdsa.r.length;
1923         op->asym->ecdsa.s.data = asym_op->ecdsa.s.data;
1924         op->asym->ecdsa.s.length = asym_op->ecdsa.s.length;
1925
1926         /* Enqueue sign result for verify */
1927         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
1928                 status = TEST_FAILED;
1929                 RTE_LOG(ERR, USER1,
1930                                 "line %u FAILED: %s", __LINE__,
1931                                 "Error sending packet for operation\n");
1932                 goto exit;
1933         }
1934
1935         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
1936                 rte_pause();
1937
1938         if (result_op == NULL) {
1939                 status = TEST_FAILED;
1940                 goto exit;
1941         }
1942         if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
1943                 status = TEST_FAILED;
1944                 RTE_LOG(ERR, USER1,
1945                                 "line %u FAILED: %s", __LINE__,
1946                                 "ECDSA verify failed.\n");
1947                 goto exit;
1948         }
1949
1950 exit:
1951         if (sess != NULL)
1952                 rte_cryptodev_asym_session_free(dev_id, sess);
1953         if (op != NULL)
1954                 rte_crypto_op_free(op);
1955         return status;
1956 };
1957
1958 static int
1959 test_ecdsa_sign_verify_all_curve(void)
1960 {
1961         int status, overall_status = TEST_SUCCESS;
1962         enum curve curve_id;
1963         int test_index = 0;
1964         const char *msg;
1965
1966         for (curve_id = SECP192R1; curve_id < END_OF_CURVE_LIST; curve_id++) {
1967                 status = test_ecdsa_sign_verify(curve_id);
1968                 if (status == TEST_SUCCESS) {
1969                         msg = "succeeded";
1970                 } else {
1971                         msg = "failed";
1972                         overall_status = status;
1973                 }
1974                 printf("  %u) TestCase Sign/Veriy Curve %s  %s\n",
1975                        test_index ++, curve[curve_id], msg);
1976         }
1977         return overall_status;
1978 }
1979
1980 static int
1981 test_ecpm(enum curve curve_id)
1982 {
1983         struct crypto_testsuite_params_asym *ts_params = &testsuite_params;
1984         struct rte_mempool *sess_mpool = ts_params->session_mpool;
1985         struct rte_mempool *op_mpool = ts_params->op_mpool;
1986         struct crypto_testsuite_ecpm_params input_params;
1987         void *sess = NULL;
1988         uint8_t dev_id = ts_params->valid_devs[0];
1989         struct rte_crypto_op *result_op = NULL;
1990         uint8_t output_buf_x[TEST_DATA_SIZE];
1991         uint8_t output_buf_y[TEST_DATA_SIZE];
1992         struct rte_crypto_asym_xform xform;
1993         struct rte_crypto_asym_op *asym_op;
1994         struct rte_cryptodev_info dev_info;
1995         struct rte_crypto_op *op = NULL;
1996         int ret, status = TEST_SUCCESS;
1997
1998         switch (curve_id) {
1999         case SECP192R1:
2000                 input_params = ecpm_param_secp192r1;
2001                 break;
2002         case SECP224R1:
2003                 input_params = ecpm_param_secp224r1;
2004                 break;
2005         case SECP256R1:
2006                 input_params = ecpm_param_secp256r1;
2007                 break;
2008         case SECP384R1:
2009                 input_params = ecpm_param_secp384r1;
2010                 break;
2011         case SECP521R1:
2012                 input_params = ecpm_param_secp521r1;
2013                 break;
2014         default:
2015                 RTE_LOG(ERR, USER1,
2016                                 "line %u FAILED: %s", __LINE__,
2017                                 "Unsupported curve id\n");
2018                 status = TEST_FAILED;
2019                 goto exit;
2020         }
2021
2022         rte_cryptodev_info_get(dev_id, &dev_info);
2023
2024         /* Setup crypto op data structure */
2025         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
2026         if (op == NULL) {
2027                 RTE_LOG(ERR, USER1,
2028                                 "line %u FAILED: %s", __LINE__,
2029                                 "Failed to allocate asymmetric crypto "
2030                                 "operation struct\n");
2031                 status = TEST_FAILED;
2032                 goto exit;
2033         }
2034         asym_op = op->asym;
2035
2036         /* Setup asym xform */
2037         xform.next = NULL;
2038         xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECPM;
2039         xform.ec.curve_id = input_params.curve;
2040
2041         ret = rte_cryptodev_asym_session_create(dev_id, &xform, sess_mpool, &sess);
2042         if (ret < 0) {
2043                 RTE_LOG(ERR, USER1,
2044                                 "line %u FAILED: %s", __LINE__,
2045                                 "Session creation failed\n");
2046                 status = (ret == -ENOTSUP) ? TEST_SKIPPED : TEST_FAILED;
2047                 goto exit;
2048         }
2049
2050         /* Attach asymmetric crypto session to crypto operations */
2051         rte_crypto_op_attach_asym_session(op, sess);
2052
2053         /* Populate op with operational details */
2054         op->asym->ecpm.p.x.data = input_params.gen_x.data;
2055         op->asym->ecpm.p.x.length = input_params.gen_x.length;
2056         op->asym->ecpm.p.y.data = input_params.gen_y.data;
2057         op->asym->ecpm.p.y.length = input_params.gen_y.length;
2058         op->asym->ecpm.scalar.data = input_params.privkey.data;
2059         op->asym->ecpm.scalar.length = input_params.privkey.length;
2060
2061         /* Init out buf */
2062         op->asym->ecpm.r.x.data = output_buf_x;
2063         op->asym->ecpm.r.y.data = output_buf_y;
2064
2065         RTE_LOG(DEBUG, USER1, "Process ASYM operation\n");
2066
2067         /* Process crypto operation */
2068         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
2069                 RTE_LOG(ERR, USER1,
2070                                 "line %u FAILED: %s", __LINE__,
2071                                 "Error sending packet for operation\n");
2072                 status = TEST_FAILED;
2073                 goto exit;
2074         }
2075
2076         while (rte_cryptodev_dequeue_burst(dev_id, 0, &result_op, 1) == 0)
2077                 rte_pause();
2078
2079         if (result_op == NULL) {
2080                 RTE_LOG(ERR, USER1,
2081                                 "line %u FAILED: %s", __LINE__,
2082                                 "Failed to process asym crypto op\n");
2083                 status = TEST_FAILED;
2084                 goto exit;
2085         }
2086
2087         if (result_op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
2088                 RTE_LOG(ERR, USER1,
2089                                 "line %u FAILED: %s", __LINE__,
2090                                 "Failed to process asym crypto op\n");
2091                 status = TEST_FAILED;
2092                 goto exit;
2093         }
2094
2095         asym_op = result_op->asym;
2096
2097         debug_hexdump(stdout, "r x:",
2098                         asym_op->ecpm.r.x.data, asym_op->ecpm.r.x.length);
2099         debug_hexdump(stdout, "r y:",
2100                         asym_op->ecpm.r.y.data, asym_op->ecpm.r.y.length);
2101
2102         ret = verify_ecpm(input_params.pubkey_x.data,
2103                                 input_params.pubkey_y.data, result_op);
2104         if (ret) {
2105                 status = TEST_FAILED;
2106                 RTE_LOG(ERR, USER1,
2107                                 "line %u FAILED: %s", __LINE__,
2108                                 "EC Point Multiplication failed.\n");
2109                 goto exit;
2110         }
2111
2112 exit:
2113         if (sess != NULL)
2114                 rte_cryptodev_asym_session_free(dev_id, sess);
2115         if (op != NULL)
2116                 rte_crypto_op_free(op);
2117         return status;
2118 }
2119
2120 static int
2121 test_ecpm_all_curve(void)
2122 {
2123         int status, overall_status = TEST_SUCCESS;
2124         enum curve curve_id;
2125         int test_index = 0;
2126         const char *msg;
2127
2128         for (curve_id = SECP192R1; curve_id < END_OF_CURVE_LIST; curve_id++) {
2129                 status = test_ecpm(curve_id);
2130                 if (status == TEST_SUCCESS) {
2131                         msg = "succeeded";
2132                 } else {
2133                         msg = "failed";
2134                         overall_status = status;
2135                 }
2136                 printf("  %u) TestCase EC Point Mul Curve %s  %s\n",
2137                        test_index ++, curve[curve_id], msg);
2138         }
2139         return overall_status;
2140 }
2141
2142 static struct unit_test_suite cryptodev_openssl_asym_testsuite  = {
2143         .suite_name = "Crypto Device OPENSSL ASYM Unit Test Suite",
2144         .setup = testsuite_setup,
2145         .teardown = testsuite_teardown,
2146         .unit_test_cases = {
2147                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_capability),
2148                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_dsa),
2149                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
2150                                 test_dh_keygenration),
2151                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_rsa_enc_dec),
2152                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
2153                                 test_rsa_sign_verify),
2154                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
2155                                 test_rsa_enc_dec_crt),
2156                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
2157                                 test_rsa_sign_verify_crt),
2158                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_mod_inv),
2159                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_mod_exp),
2160                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_one_by_one),
2161                 TEST_CASES_END() /**< NULL terminate unit test array */
2162         }
2163 };
2164
2165 static struct unit_test_suite cryptodev_qat_asym_testsuite  = {
2166         .suite_name = "Crypto Device QAT ASYM Unit Test Suite",
2167         .setup = testsuite_setup,
2168         .teardown = testsuite_teardown,
2169         .unit_test_cases = {
2170                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_one_by_one),
2171                 TEST_CASES_END() /**< NULL terminate unit test array */
2172         }
2173 };
2174
2175 static struct unit_test_suite cryptodev_octeontx_asym_testsuite  = {
2176         .suite_name = "Crypto Device OCTEONTX ASYM Unit Test Suite",
2177         .setup = testsuite_setup,
2178         .teardown = testsuite_teardown,
2179         .unit_test_cases = {
2180                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_capability),
2181                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
2182                                 test_rsa_enc_dec_crt),
2183                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
2184                                 test_rsa_sign_verify_crt),
2185                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym, test_mod_exp),
2186                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
2187                              test_ecdsa_sign_verify_all_curve),
2188                 TEST_CASE_ST(ut_setup_asym, ut_teardown_asym,
2189                                 test_ecpm_all_curve),
2190                 TEST_CASES_END() /**< NULL terminate unit test array */
2191         }
2192 };
2193
2194 static int
2195 test_cryptodev_openssl_asym(void)
2196 {
2197         gbl_driver_id = rte_cryptodev_driver_id_get(
2198                         RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
2199
2200         if (gbl_driver_id == -1) {
2201                 RTE_LOG(ERR, USER1, "OPENSSL PMD must be loaded.\n");
2202                 return TEST_FAILED;
2203         }
2204
2205         return unit_test_suite_runner(&cryptodev_openssl_asym_testsuite);
2206 }
2207
2208 static int
2209 test_cryptodev_qat_asym(void)
2210 {
2211         gbl_driver_id = rte_cryptodev_driver_id_get(
2212                         RTE_STR(CRYPTODEV_NAME_QAT_ASYM_PMD));
2213
2214         if (gbl_driver_id == -1) {
2215                 RTE_LOG(ERR, USER1, "QAT PMD must be loaded.\n");
2216                 return TEST_FAILED;
2217         }
2218
2219         return unit_test_suite_runner(&cryptodev_qat_asym_testsuite);
2220 }
2221
2222 static int
2223 test_cryptodev_octeontx_asym(void)
2224 {
2225         gbl_driver_id = rte_cryptodev_driver_id_get(
2226                         RTE_STR(CRYPTODEV_NAME_OCTEONTX_SYM_PMD));
2227         if (gbl_driver_id == -1) {
2228                 RTE_LOG(ERR, USER1, "OCTEONTX PMD must be loaded.\n");
2229                 return TEST_FAILED;
2230         }
2231         return unit_test_suite_runner(&cryptodev_octeontx_asym_testsuite);
2232 }
2233
2234 static int
2235 test_cryptodev_cn9k_asym(void)
2236 {
2237         gbl_driver_id = rte_cryptodev_driver_id_get(
2238                         RTE_STR(CRYPTODEV_NAME_CN9K_PMD));
2239         if (gbl_driver_id == -1) {
2240                 RTE_LOG(ERR, USER1, "CN9K PMD must be loaded.\n");
2241                 return TEST_FAILED;
2242         }
2243
2244         /* Use test suite registered for crypto_octeontx PMD */
2245         return unit_test_suite_runner(&cryptodev_octeontx_asym_testsuite);
2246 }
2247
2248 static int
2249 test_cryptodev_cn10k_asym(void)
2250 {
2251         gbl_driver_id = rte_cryptodev_driver_id_get(
2252                         RTE_STR(CRYPTODEV_NAME_CN10K_PMD));
2253         if (gbl_driver_id == -1) {
2254                 RTE_LOG(ERR, USER1, "CN10K PMD must be loaded.\n");
2255                 return TEST_FAILED;
2256         }
2257
2258         /* Use test suite registered for crypto_octeontx PMD */
2259         return unit_test_suite_runner(&cryptodev_octeontx_asym_testsuite);
2260 }
2261
2262 REGISTER_TEST_COMMAND(cryptodev_openssl_asym_autotest,
2263                                           test_cryptodev_openssl_asym);
2264
2265 REGISTER_TEST_COMMAND(cryptodev_qat_asym_autotest, test_cryptodev_qat_asym);
2266
2267 REGISTER_TEST_COMMAND(cryptodev_octeontx_asym_autotest,
2268                                           test_cryptodev_octeontx_asym);
2269 REGISTER_TEST_COMMAND(cryptodev_cn9k_asym_autotest, test_cryptodev_cn9k_asym);
2270 REGISTER_TEST_COMMAND(cryptodev_cn10k_asym_autotest, test_cryptodev_cn10k_asym);
2271
2272 #endif /* !RTE_EXEC_ENV_WINDOWS */