app/test: fix debug build
[dpdk.git] / app / test / test_cryptodev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *       * Redistributions of source code must retain the above copyright
11  *         notice, this list of conditions and the following disclaimer.
12  *       * Redistributions in binary form must reproduce the above copyright
13  *         notice, this list of conditions and the following disclaimer in
14  *         the documentation and/or other materials provided with the
15  *         distribution.
16  *       * Neither the name of Intel Corporation nor the names of its
17  *         contributors may be used to endorse or promote products derived
18  *         from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <rte_common.h>
34 #include <rte_hexdump.h>
35 #include <rte_mbuf.h>
36 #include <rte_malloc.h>
37 #include <rte_memcpy.h>
38
39 #include <rte_crypto.h>
40 #include <rte_cryptodev.h>
41 #include <rte_cryptodev_pmd.h>
42
43 #include "test.h"
44 #include "test_cryptodev.h"
45
46 #include "test_cryptodev_aes.h"
47 #include "test_cryptodev_snow3g_test_vectors.h"
48 #include "test_cryptodev_snow3g_hash_test_vectors.h"
49 #include "test_cryptodev_gcm_test_vectors.h"
50
51 static enum rte_cryptodev_type gbl_cryptodev_type;
52
53 struct crypto_testsuite_params {
54         struct rte_mempool *mbuf_pool;
55         struct rte_mempool *op_mpool;
56         struct rte_cryptodev_config conf;
57         struct rte_cryptodev_qp_conf qp_conf;
58
59         uint8_t valid_devs[RTE_CRYPTO_MAX_DEVS];
60         uint8_t valid_dev_count;
61 };
62
63 struct crypto_unittest_params {
64         struct rte_crypto_sym_xform cipher_xform;
65         struct rte_crypto_sym_xform auth_xform;
66
67         struct rte_cryptodev_sym_session *sess;
68
69         struct rte_crypto_op *op;
70
71         struct rte_mbuf *obuf, *ibuf;
72
73         uint8_t *digest;
74 };
75
76 #define ALIGN_POW2_ROUNDUP(num, align) \
77         (((num) + (align) - 1) & ~((align) - 1))
78
79 /*
80  * Forward declarations.
81  */
82 static int
83 test_AES_CBC_HMAC_SHA512_decrypt_create_session_params(
84                 struct crypto_unittest_params *ut_params);
85
86 static int
87 test_AES_CBC_HMAC_SHA512_decrypt_perform(struct rte_cryptodev_sym_session *sess,
88                 struct crypto_unittest_params *ut_params,
89                 struct crypto_testsuite_params *ts_param);
90
91 static struct rte_mbuf *
92 setup_test_string(struct rte_mempool *mpool,
93                 const char *string, size_t len, uint8_t blocksize)
94 {
95         struct rte_mbuf *m = rte_pktmbuf_alloc(mpool);
96         size_t t_len = len - (blocksize ? (len % blocksize) : 0);
97
98         memset(m->buf_addr, 0, m->buf_len);
99         if (m) {
100                 char *dst = rte_pktmbuf_append(m, t_len);
101
102                 if (!dst) {
103                         rte_pktmbuf_free(m);
104                         return NULL;
105                 }
106                 if (string != NULL)
107                         rte_memcpy(dst, string, t_len);
108                 else
109                         memset(dst, 0, t_len);
110         }
111
112         return m;
113 }
114
115 #if HEX_DUMP
116 static void
117 hexdump_mbuf_data(FILE *f, const char *title, struct rte_mbuf *m)
118 {
119         rte_hexdump(f, title, rte_pktmbuf_mtod(m, const void *), m->data_len);
120 }
121 #endif
122
123 static struct rte_crypto_op *
124 process_crypto_request(uint8_t dev_id, struct rte_crypto_op *op)
125 {
126 #if HEX_DUMP
127         hexdump_mbuf_data(stdout, "Enqueued Packet", ibuf);
128 #endif
129
130         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
131                 printf("Error sending packet for encryption");
132                 return NULL;
133         }
134
135         op = NULL;
136
137         while (rte_cryptodev_dequeue_burst(dev_id, 0, &op, 1) == 0)
138                 rte_pause();
139
140 #if HEX_DUMP
141         if (obuf)
142                 hexdump_mbuf_data(stdout, "Dequeued Packet", obuf);
143 #endif
144
145         return op;
146 }
147
148 static struct crypto_testsuite_params testsuite_params = { NULL };
149 static struct crypto_unittest_params unittest_params;
150
151 static int
152 testsuite_setup(void)
153 {
154         struct crypto_testsuite_params *ts_params = &testsuite_params;
155         struct rte_cryptodev_info info;
156         unsigned i, nb_devs, dev_id;
157         int ret;
158         uint16_t qp_id;
159
160         memset(ts_params, 0, sizeof(*ts_params));
161
162         ts_params->mbuf_pool = rte_mempool_lookup("CRYPTO_MBUFPOOL");
163         if (ts_params->mbuf_pool == NULL) {
164                 /* Not already created so create */
165                 ts_params->mbuf_pool = rte_pktmbuf_pool_create(
166                                 "CRYPTO_MBUFPOOL",
167                                 NUM_MBUFS, MBUF_CACHE_SIZE, 0, MBUF_SIZE,
168                                 rte_socket_id());
169                 if (ts_params->mbuf_pool == NULL) {
170                         RTE_LOG(ERR, USER1, "Can't create CRYPTO_MBUFPOOL\n");
171                         return TEST_FAILED;
172                 }
173         }
174
175         ts_params->op_mpool = rte_crypto_op_pool_create(
176                         "MBUF_CRYPTO_SYM_OP_POOL",
177                         RTE_CRYPTO_OP_TYPE_SYMMETRIC,
178                         NUM_MBUFS, MBUF_CACHE_SIZE,
179                         DEFAULT_NUM_XFORMS *
180                         sizeof(struct rte_crypto_sym_xform),
181                         rte_socket_id());
182         if (ts_params->op_mpool == NULL) {
183                 RTE_LOG(ERR, USER1, "Can't create CRYPTO_OP_POOL\n");
184                 return TEST_FAILED;
185         }
186
187         /* Create 2 AESNI MB devices if required */
188         if (gbl_cryptodev_type == RTE_CRYPTODEV_AESNI_MB_PMD) {
189                 nb_devs = rte_cryptodev_count_devtype(
190                                 RTE_CRYPTODEV_AESNI_MB_PMD);
191                 if (nb_devs < 2) {
192                         for (i = nb_devs; i < 2; i++) {
193                                 ret = rte_eal_vdev_init(
194                                         CRYPTODEV_NAME_AESNI_MB_PMD, NULL);
195
196                                 TEST_ASSERT(ret == 0,
197                                         "Failed to create instance %u of"
198                                         " pmd : %s",
199                                         i, CRYPTODEV_NAME_AESNI_MB_PMD);
200                         }
201                 }
202         }
203
204         /* Create 2 AESNI GCM devices if required */
205         if (gbl_cryptodev_type == RTE_CRYPTODEV_AESNI_GCM_PMD) {
206                 nb_devs = rte_cryptodev_count_devtype(
207                                 RTE_CRYPTODEV_AESNI_GCM_PMD);
208                 if (nb_devs < 2) {
209                         for (i = nb_devs; i < 2; i++) {
210                                 TEST_ASSERT_SUCCESS(rte_eal_vdev_init(
211                                         CRYPTODEV_NAME_AESNI_GCM_PMD, NULL),
212                                         "Failed to create instance %u of"
213                                         " pmd : %s",
214                                         i, CRYPTODEV_NAME_AESNI_GCM_PMD);
215                         }
216                 }
217         }
218
219         /* Create 2 Snow3G devices if required */
220         if (gbl_cryptodev_type == RTE_CRYPTODEV_SNOW3G_PMD) {
221                 nb_devs = rte_cryptodev_count_devtype(RTE_CRYPTODEV_SNOW3G_PMD);
222                 if (nb_devs < 2) {
223                         for (i = nb_devs; i < 2; i++) {
224                                 TEST_ASSERT_SUCCESS(rte_eal_vdev_init(
225                                         CRYPTODEV_NAME_SNOW3G_PMD, NULL),
226                                         "Failed to create instance %u of"
227                                         " pmd : %s",
228                                         i, CRYPTODEV_NAME_SNOW3G_PMD);
229                         }
230                 }
231         }
232
233         /* Create 2 NULL devices if required */
234         if (gbl_cryptodev_type == RTE_CRYPTODEV_NULL_PMD) {
235                 nb_devs = rte_cryptodev_count_devtype(
236                                 RTE_CRYPTODEV_NULL_PMD);
237                 if (nb_devs < 2) {
238                         for (i = nb_devs; i < 2; i++) {
239                                 int dev_id = rte_eal_vdev_init(
240                                         CRYPTODEV_NAME_NULL_PMD, NULL);
241
242                                 TEST_ASSERT(dev_id >= 0,
243                                         "Failed to create instance %u of"
244                                         " pmd : %s",
245                                         i, CRYPTODEV_NAME_NULL_PMD);
246                         }
247                 }
248         }
249
250         nb_devs = rte_cryptodev_count();
251         if (nb_devs < 1) {
252                 RTE_LOG(ERR, USER1, "No crypto devices found?");
253                 return TEST_FAILED;
254         }
255
256         /* Create list of valid crypto devs */
257         for (i = 0; i < nb_devs; i++) {
258                 rte_cryptodev_info_get(i, &info);
259                 if (info.dev_type == gbl_cryptodev_type)
260                         ts_params->valid_devs[ts_params->valid_dev_count++] = i;
261         }
262
263         if (ts_params->valid_dev_count < 1)
264                 return TEST_FAILED;
265
266         /* Set up all the qps on the first of the valid devices found */
267         for (i = 0; i < 1; i++) {
268                 dev_id = ts_params->valid_devs[i];
269
270                 rte_cryptodev_info_get(dev_id, &info);
271
272                 /*
273                  * Since we can't free and re-allocate queue memory always set
274                  * the queues on this device up to max size first so enough
275                  * memory is allocated for any later re-configures needed by
276                  * other tests
277                  */
278
279                 ts_params->conf.nb_queue_pairs = info.max_nb_queue_pairs;
280                 ts_params->conf.socket_id = SOCKET_ID_ANY;
281                 ts_params->conf.session_mp.nb_objs = info.sym.max_nb_sessions;
282
283                 TEST_ASSERT_SUCCESS(rte_cryptodev_configure(dev_id,
284                                 &ts_params->conf),
285                                 "Failed to configure cryptodev %u with %u qps",
286                                 dev_id, ts_params->conf.nb_queue_pairs);
287
288                 ts_params->qp_conf.nb_descriptors = MAX_NUM_OPS_INFLIGHT;
289
290                 for (qp_id = 0; qp_id < info.max_nb_queue_pairs; qp_id++) {
291                         TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
292                                         dev_id, qp_id, &ts_params->qp_conf,
293                                         rte_cryptodev_socket_id(dev_id)),
294                                         "Failed to setup queue pair %u on "
295                                         "cryptodev %u",
296                                         qp_id, dev_id);
297                 }
298         }
299
300         return TEST_SUCCESS;
301 }
302
303 static void
304 testsuite_teardown(void)
305 {
306         struct crypto_testsuite_params *ts_params = &testsuite_params;
307
308         if (ts_params->mbuf_pool != NULL) {
309                 RTE_LOG(DEBUG, USER1, "CRYPTO_MBUFPOOL count %u\n",
310                 rte_mempool_count(ts_params->mbuf_pool));
311         }
312
313         if (ts_params->op_mpool != NULL) {
314                 RTE_LOG(DEBUG, USER1, "CRYPTO_OP_POOL count %u\n",
315                 rte_mempool_count(ts_params->op_mpool));
316         }
317
318 }
319
320 static int
321 ut_setup(void)
322 {
323         struct crypto_testsuite_params *ts_params = &testsuite_params;
324         struct crypto_unittest_params *ut_params = &unittest_params;
325
326         uint16_t qp_id;
327
328         /* Clear unit test parameters before running test */
329         memset(ut_params, 0, sizeof(*ut_params));
330
331         /* Reconfigure device to default parameters */
332         ts_params->conf.nb_queue_pairs = DEFAULT_NUM_QPS_PER_QAT_DEVICE;
333         ts_params->conf.socket_id = SOCKET_ID_ANY;
334         ts_params->conf.session_mp.nb_objs =
335                         (gbl_cryptodev_type == RTE_CRYPTODEV_QAT_SYM_PMD) ?
336                                         DEFAULT_NUM_OPS_INFLIGHT :
337                                         DEFAULT_NUM_OPS_INFLIGHT;
338
339         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(ts_params->valid_devs[0],
340                         &ts_params->conf),
341                         "Failed to configure cryptodev %u",
342                         ts_params->valid_devs[0]);
343
344         /*
345          * Now reconfigure queues to size we actually want to use in this
346          * test suite.
347          */
348         ts_params->qp_conf.nb_descriptors = DEFAULT_NUM_OPS_INFLIGHT;
349
350         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs ; qp_id++) {
351                 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
352                         ts_params->valid_devs[0], qp_id,
353                         &ts_params->qp_conf,
354                         rte_cryptodev_socket_id(ts_params->valid_devs[0])),
355                         "Failed to setup queue pair %u on cryptodev %u",
356                         qp_id, ts_params->valid_devs[0]);
357         }
358
359
360         rte_cryptodev_stats_reset(ts_params->valid_devs[0]);
361
362         /* Start the device */
363         TEST_ASSERT_SUCCESS(rte_cryptodev_start(ts_params->valid_devs[0]),
364                         "Failed to start cryptodev %u",
365                         ts_params->valid_devs[0]);
366
367         return TEST_SUCCESS;
368 }
369
370 static void
371 ut_teardown(void)
372 {
373         struct crypto_testsuite_params *ts_params = &testsuite_params;
374         struct crypto_unittest_params *ut_params = &unittest_params;
375         struct rte_cryptodev_stats stats;
376
377         /* free crypto session structure */
378         if (ut_params->sess) {
379                 rte_cryptodev_sym_session_free(ts_params->valid_devs[0],
380                                 ut_params->sess);
381                 ut_params->sess = NULL;
382         }
383
384         /* free crypto operation structure */
385         if (ut_params->op)
386                 rte_crypto_op_free(ut_params->op);
387
388         /*
389          * free mbuf - both obuf and ibuf are usually the same,
390          * but rte copes even if we call free twice
391          */
392         if (ut_params->obuf) {
393                 rte_pktmbuf_free(ut_params->obuf);
394                 ut_params->obuf = 0;
395         }
396         if (ut_params->ibuf) {
397                 rte_pktmbuf_free(ut_params->ibuf);
398                 ut_params->ibuf = 0;
399         }
400
401         if (ts_params->mbuf_pool != NULL)
402                 RTE_LOG(DEBUG, USER1, "CRYPTO_MBUFPOOL count %u\n",
403                                 rte_mempool_count(ts_params->mbuf_pool));
404
405         rte_cryptodev_stats_get(ts_params->valid_devs[0], &stats);
406
407         /* Stop the device */
408         rte_cryptodev_stop(ts_params->valid_devs[0]);
409 }
410
411 static int
412 test_device_configure_invalid_dev_id(void)
413 {
414         struct crypto_testsuite_params *ts_params = &testsuite_params;
415         uint16_t dev_id, num_devs = 0;
416
417         TEST_ASSERT((num_devs = rte_cryptodev_count()) >= 1,
418                         "Need at least %d devices for test", 1);
419
420         /* valid dev_id values */
421         dev_id = ts_params->valid_devs[ts_params->valid_dev_count - 1];
422
423         /* Stop the device in case it's started so it can be configured */
424         rte_cryptodev_stop(ts_params->valid_devs[dev_id]);
425
426         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(dev_id, &ts_params->conf),
427                         "Failed test for rte_cryptodev_configure: "
428                         "invalid dev_num %u", dev_id);
429
430         /* invalid dev_id values */
431         dev_id = num_devs;
432
433         TEST_ASSERT_FAIL(rte_cryptodev_configure(dev_id, &ts_params->conf),
434                         "Failed test for rte_cryptodev_configure: "
435                         "invalid dev_num %u", dev_id);
436
437         dev_id = 0xff;
438
439         TEST_ASSERT_FAIL(rte_cryptodev_configure(dev_id, &ts_params->conf),
440                         "Failed test for rte_cryptodev_configure:"
441                         "invalid dev_num %u", dev_id);
442
443         return TEST_SUCCESS;
444 }
445
446 static int
447 test_device_configure_invalid_queue_pair_ids(void)
448 {
449         struct crypto_testsuite_params *ts_params = &testsuite_params;
450
451         /* Stop the device in case it's started so it can be configured */
452         rte_cryptodev_stop(ts_params->valid_devs[0]);
453
454         /* valid - one queue pairs */
455         ts_params->conf.nb_queue_pairs = 1;
456
457         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(ts_params->valid_devs[0],
458                         &ts_params->conf),
459                         "Failed to configure cryptodev: dev_id %u, qp_id %u",
460                         ts_params->valid_devs[0], ts_params->conf.nb_queue_pairs);
461
462
463         /* valid - max value queue pairs */
464         ts_params->conf.nb_queue_pairs = MAX_NUM_QPS_PER_QAT_DEVICE;
465
466         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(ts_params->valid_devs[0],
467                         &ts_params->conf),
468                         "Failed to configure cryptodev: dev_id %u, qp_id %u",
469                         ts_params->valid_devs[0], ts_params->conf.nb_queue_pairs);
470
471
472         /* invalid - zero queue pairs */
473         ts_params->conf.nb_queue_pairs = 0;
474
475         TEST_ASSERT_FAIL(rte_cryptodev_configure(ts_params->valid_devs[0],
476                         &ts_params->conf),
477                         "Failed test for rte_cryptodev_configure, dev_id %u,"
478                         " invalid qps: %u",
479                         ts_params->valid_devs[0],
480                         ts_params->conf.nb_queue_pairs);
481
482
483         /* invalid - max value supported by field queue pairs */
484         ts_params->conf.nb_queue_pairs = UINT16_MAX;
485
486         TEST_ASSERT_FAIL(rte_cryptodev_configure(ts_params->valid_devs[0],
487                         &ts_params->conf),
488                         "Failed test for rte_cryptodev_configure, dev_id %u,"
489                         " invalid qps: %u",
490                         ts_params->valid_devs[0],
491                         ts_params->conf.nb_queue_pairs);
492
493
494         /* invalid - max value + 1 queue pairs */
495         ts_params->conf.nb_queue_pairs = MAX_NUM_QPS_PER_QAT_DEVICE + 1;
496
497         TEST_ASSERT_FAIL(rte_cryptodev_configure(ts_params->valid_devs[0],
498                         &ts_params->conf),
499                         "Failed test for rte_cryptodev_configure, dev_id %u,"
500                         " invalid qps: %u",
501                         ts_params->valid_devs[0],
502                         ts_params->conf.nb_queue_pairs);
503
504         return TEST_SUCCESS;
505 }
506
507 static int
508 test_queue_pair_descriptor_setup(void)
509 {
510         struct crypto_testsuite_params *ts_params = &testsuite_params;
511         struct rte_cryptodev_info dev_info;
512         struct rte_cryptodev_qp_conf qp_conf = {
513                 .nb_descriptors = MAX_NUM_OPS_INFLIGHT
514         };
515
516         uint16_t qp_id;
517
518         /* Stop the device in case it's started so it can be configured */
519         rte_cryptodev_stop(ts_params->valid_devs[0]);
520
521
522         rte_cryptodev_info_get(ts_params->valid_devs[0], &dev_info);
523
524         ts_params->conf.session_mp.nb_objs = dev_info.sym.max_nb_sessions;
525
526         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(ts_params->valid_devs[0],
527                         &ts_params->conf), "Failed to configure cryptodev %u",
528                         ts_params->valid_devs[0]);
529
530
531         /*
532          * Test various ring sizes on this device. memzones can't be
533          * freed so are re-used if ring is released and re-created.
534          */
535         qp_conf.nb_descriptors = MIN_NUM_OPS_INFLIGHT; /* min size*/
536
537         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
538                 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
539                                 ts_params->valid_devs[0], qp_id, &qp_conf,
540                                 rte_cryptodev_socket_id(
541                                                 ts_params->valid_devs[0])),
542                                 "Failed test for "
543                                 "rte_cryptodev_queue_pair_setup: num_inflights "
544                                 "%u on qp %u on cryptodev %u",
545                                 qp_conf.nb_descriptors, qp_id,
546                                 ts_params->valid_devs[0]);
547         }
548
549         qp_conf.nb_descriptors = (uint32_t)(MAX_NUM_OPS_INFLIGHT / 2);
550
551         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
552                 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
553                                 ts_params->valid_devs[0], qp_id, &qp_conf,
554                                 rte_cryptodev_socket_id(
555                                                 ts_params->valid_devs[0])),
556                                 "Failed test for"
557                                 " rte_cryptodev_queue_pair_setup: num_inflights"
558                                 " %u on qp %u on cryptodev %u",
559                                 qp_conf.nb_descriptors, qp_id,
560                                 ts_params->valid_devs[0]);
561         }
562
563         qp_conf.nb_descriptors = MAX_NUM_OPS_INFLIGHT; /* valid */
564
565         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
566                 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
567                                 ts_params->valid_devs[0], qp_id, &qp_conf,
568                                 rte_cryptodev_socket_id(
569                                                 ts_params->valid_devs[0])),
570                                 "Failed test for "
571                                 "rte_cryptodev_queue_pair_setup: num_inflights"
572                                 " %u on qp %u on cryptodev %u",
573                                 qp_conf.nb_descriptors, qp_id,
574                                 ts_params->valid_devs[0]);
575         }
576
577         /* invalid number of descriptors - max supported + 2 */
578         qp_conf.nb_descriptors = MAX_NUM_OPS_INFLIGHT + 2;
579
580         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
581                 TEST_ASSERT_FAIL(rte_cryptodev_queue_pair_setup(
582                                 ts_params->valid_devs[0], qp_id, &qp_conf,
583                                 rte_cryptodev_socket_id(
584                                                 ts_params->valid_devs[0])),
585                                 "Unexpectedly passed test for "
586                                 "rte_cryptodev_queue_pair_setup:"
587                                 "num_inflights %u on qp %u on cryptodev %u",
588                                 qp_conf.nb_descriptors, qp_id,
589                                 ts_params->valid_devs[0]);
590         }
591
592         /* invalid number of descriptors - max value of parameter */
593         qp_conf.nb_descriptors = UINT32_MAX-1;
594
595         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
596                 TEST_ASSERT_FAIL(rte_cryptodev_queue_pair_setup(
597                                 ts_params->valid_devs[0], qp_id, &qp_conf,
598                                 rte_cryptodev_socket_id(
599                                                 ts_params->valid_devs[0])),
600                                 "Unexpectedly passed test for "
601                                 "rte_cryptodev_queue_pair_setup:"
602                                 "num_inflights %u on qp %u on cryptodev %u",
603                                 qp_conf.nb_descriptors, qp_id,
604                                 ts_params->valid_devs[0]);
605         }
606
607         qp_conf.nb_descriptors = DEFAULT_NUM_OPS_INFLIGHT;
608
609         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
610                 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
611                                 ts_params->valid_devs[0], qp_id, &qp_conf,
612                                 rte_cryptodev_socket_id(
613                                                 ts_params->valid_devs[0])),
614                                 "Failed test for"
615                                 " rte_cryptodev_queue_pair_setup:"
616                                 "num_inflights %u on qp %u on cryptodev %u",
617                                 qp_conf.nb_descriptors, qp_id,
618                                 ts_params->valid_devs[0]);
619         }
620
621         /* invalid number of descriptors - max supported + 1 */
622         qp_conf.nb_descriptors = DEFAULT_NUM_OPS_INFLIGHT + 1;
623
624         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
625                 TEST_ASSERT_FAIL(rte_cryptodev_queue_pair_setup(
626                                 ts_params->valid_devs[0], qp_id, &qp_conf,
627                                 rte_cryptodev_socket_id(
628                                                 ts_params->valid_devs[0])),
629                                 "Unexpectedly passed test for "
630                                 "rte_cryptodev_queue_pair_setup:"
631                                 "num_inflights %u on qp %u on cryptodev %u",
632                                 qp_conf.nb_descriptors, qp_id,
633                                 ts_params->valid_devs[0]);
634         }
635
636         /* test invalid queue pair id */
637         qp_conf.nb_descriptors = DEFAULT_NUM_OPS_INFLIGHT;      /*valid */
638
639         qp_id = DEFAULT_NUM_QPS_PER_QAT_DEVICE;         /*invalid */
640
641         TEST_ASSERT_FAIL(rte_cryptodev_queue_pair_setup(
642                         ts_params->valid_devs[0],
643                         qp_id, &qp_conf,
644                         rte_cryptodev_socket_id(ts_params->valid_devs[0])),
645                         "Failed test for rte_cryptodev_queue_pair_setup:"
646                         "invalid qp %u on cryptodev %u",
647                         qp_id, ts_params->valid_devs[0]);
648
649         qp_id = 0xffff; /*invalid*/
650
651         TEST_ASSERT_FAIL(rte_cryptodev_queue_pair_setup(
652                         ts_params->valid_devs[0],
653                         qp_id, &qp_conf,
654                         rte_cryptodev_socket_id(ts_params->valid_devs[0])),
655                         "Failed test for rte_cryptodev_queue_pair_setup:"
656                         "invalid qp %u on cryptodev %u",
657                         qp_id, ts_params->valid_devs[0]);
658
659         return TEST_SUCCESS;
660 }
661
662 /* ***** Plaintext data for tests ***** */
663
664 const char catch_22_quote_1[] =
665                 "There was only one catch and that was Catch-22, which "
666                 "specified that a concern for one's safety in the face of "
667                 "dangers that were real and immediate was the process of a "
668                 "rational mind. Orr was crazy and could be grounded. All he "
669                 "had to do was ask; and as soon as he did, he would no longer "
670                 "be crazy and would have to fly more missions. Orr would be "
671                 "crazy to fly more missions and sane if he didn't, but if he "
672                 "was sane he had to fly them. If he flew them he was crazy "
673                 "and didn't have to; but if he didn't want to he was sane and "
674                 "had to. Yossarian was moved very deeply by the absolute "
675                 "simplicity of this clause of Catch-22 and let out a "
676                 "respectful whistle. \"That's some catch, that Catch-22\", he "
677                 "observed. \"It's the best there is,\" Doc Daneeka agreed.";
678
679 const char catch_22_quote[] =
680                 "What a lousy earth! He wondered how many people were "
681                 "destitute that same night even in his own prosperous country, "
682                 "how many homes were shanties, how many husbands were drunk "
683                 "and wives socked, and how many children were bullied, abused, "
684                 "or abandoned. How many families hungered for food they could "
685                 "not afford to buy? How many hearts were broken? How many "
686                 "suicides would take place that same night, how many people "
687                 "would go insane? How many cockroaches and landlords would "
688                 "triumph? How many winners were losers, successes failures, "
689                 "and rich men poor men? How many wise guys were stupid? How "
690                 "many happy endings were unhappy endings? How many honest men "
691                 "were liars, brave men cowards, loyal men traitors, how many "
692                 "sainted men were corrupt, how many people in positions of "
693                 "trust had sold their souls to bodyguards, how many had never "
694                 "had souls? How many straight-and-narrow paths were crooked "
695                 "paths? How many best families were worst families and how "
696                 "many good people were bad people? When you added them all up "
697                 "and then subtracted, you might be left with only the children, "
698                 "and perhaps with Albert Einstein and an old violinist or "
699                 "sculptor somewhere.";
700
701 #define QUOTE_480_BYTES         (480)
702 #define QUOTE_512_BYTES         (512)
703 #define QUOTE_768_BYTES         (768)
704 #define QUOTE_1024_BYTES        (1024)
705
706
707
708 /* ***** SHA1 Hash Tests ***** */
709
710 #define HMAC_KEY_LENGTH_SHA1    (DIGEST_BYTE_LENGTH_SHA1)
711
712 static uint8_t hmac_sha1_key[] = {
713         0xF8, 0x2A, 0xC7, 0x54, 0xDB, 0x96, 0x18, 0xAA,
714         0xC3, 0xA1, 0x53, 0xF6, 0x1F, 0x17, 0x60, 0xBD,
715         0xDE, 0xF4, 0xDE, 0xAD };
716
717 /* ***** SHA224 Hash Tests ***** */
718
719 #define HMAC_KEY_LENGTH_SHA224  (DIGEST_BYTE_LENGTH_SHA224)
720
721
722 /* ***** AES-CBC Cipher Tests ***** */
723
724 #define CIPHER_KEY_LENGTH_AES_CBC       (16)
725 #define CIPHER_IV_LENGTH_AES_CBC        (CIPHER_KEY_LENGTH_AES_CBC)
726
727 static uint8_t aes_cbc_key[] = {
728         0xE4, 0x23, 0x33, 0x8A, 0x35, 0x64, 0x61, 0xE2,
729         0x49, 0x03, 0xDD, 0xC6, 0xB8, 0xCA, 0x55, 0x7A };
730
731 static uint8_t aes_cbc_iv[] = {
732         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
733         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
734
735
736 /* ***** AES-CBC / HMAC-SHA1 Hash Tests ***** */
737
738 static const uint8_t catch_22_quote_2_512_bytes_AES_CBC_ciphertext[] = {
739         0x8B, 0x4D, 0xDA, 0x1B, 0xCF, 0x04, 0xA0, 0x31,
740         0xB4, 0xBF, 0xBD, 0x68, 0x43, 0x20, 0x7E, 0x76,
741         0xB1, 0x96, 0x8B, 0xA2, 0x7C, 0xA2, 0x83, 0x9E,
742         0x39, 0x5A, 0x2F, 0x7E, 0x92, 0xB4, 0x48, 0x1A,
743         0x3F, 0x6B, 0x5D, 0xDF, 0x52, 0x85, 0x5F, 0x8E,
744         0x42, 0x3C, 0xFB, 0xE9, 0x1A, 0x24, 0xD6, 0x08,
745         0xDD, 0xFD, 0x16, 0xFB, 0xE9, 0x55, 0xEF, 0xF0,
746         0xA0, 0x8D, 0x13, 0xAB, 0x81, 0xC6, 0x90, 0x01,
747         0xB5, 0x18, 0x84, 0xB3, 0xF6, 0xE6, 0x11, 0x57,
748         0xD6, 0x71, 0xC6, 0x3C, 0x3F, 0x2F, 0x33, 0xEE,
749         0x24, 0x42, 0x6E, 0xAC, 0x0B, 0xCA, 0xEC, 0xF9,
750         0x84, 0xF8, 0x22, 0xAA, 0x60, 0xF0, 0x32, 0xA9,
751         0x75, 0x75, 0x3B, 0xCB, 0x70, 0x21, 0x0A, 0x8D,
752         0x0F, 0xE0, 0xC4, 0x78, 0x2B, 0xF8, 0x97, 0xE3,
753         0xE4, 0x26, 0x4B, 0x29, 0xDA, 0x88, 0xCD, 0x46,
754         0xEC, 0xAA, 0xF9, 0x7F, 0xF1, 0x15, 0xEA, 0xC3,
755         0x87, 0xE6, 0x31, 0xF2, 0xCF, 0xDE, 0x4D, 0x80,
756         0x70, 0x91, 0x7E, 0x0C, 0xF7, 0x26, 0x3A, 0x92,
757         0x4F, 0x18, 0x83, 0xC0, 0x8F, 0x59, 0x01, 0xA5,
758         0x88, 0xD1, 0xDB, 0x26, 0x71, 0x27, 0x16, 0xF5,
759         0xEE, 0x10, 0x82, 0xAC, 0x68, 0x26, 0x9B, 0xE2,
760         0x6D, 0xD8, 0x9A, 0x80, 0xDF, 0x04, 0x31, 0xD5,
761         0xF1, 0x35, 0x5C, 0x3B, 0xDD, 0x9A, 0x65, 0xBA,
762         0x58, 0x34, 0x85, 0x61, 0x1C, 0x42, 0x10, 0x76,
763         0x73, 0x02, 0x42, 0xC9, 0x23, 0x18, 0x8E, 0xB4,
764         0x6F, 0xB4, 0xA3, 0x54, 0x6E, 0x88, 0x3B, 0x62,
765         0x7C, 0x02, 0x8D, 0x4C, 0x9F, 0xC8, 0x45, 0xF4,
766         0xC9, 0xDE, 0x4F, 0xEB, 0x22, 0x83, 0x1B, 0xE4,
767         0x49, 0x37, 0xE4, 0xAD, 0xE7, 0xCD, 0x21, 0x54,
768         0xBC, 0x1C, 0xC2, 0x04, 0x97, 0xB4, 0x10, 0x61,
769         0xF0, 0xE4, 0xEF, 0x27, 0x63, 0x3A, 0xDA, 0x91,
770         0x41, 0x25, 0x62, 0x1C, 0x5C, 0xB6, 0x38, 0x4A,
771         0x88, 0x71, 0x59, 0x5A, 0x8D, 0xA0, 0x09, 0xAF,
772         0x72, 0x94, 0xD7, 0x79, 0x5C, 0x60, 0x7C, 0x8F,
773         0x4C, 0xF5, 0xD9, 0xA1, 0x39, 0x6D, 0x81, 0x28,
774         0xEF, 0x13, 0x28, 0xDF, 0xF5, 0x3E, 0xF7, 0x8E,
775         0x09, 0x9C, 0x78, 0x18, 0x79, 0xB8, 0x68, 0xD7,
776         0xA8, 0x29, 0x62, 0xAD, 0xDE, 0xE1, 0x61, 0x76,
777         0x1B, 0x05, 0x16, 0xCD, 0xBF, 0x02, 0x8E, 0xA6,
778         0x43, 0x6E, 0x92, 0x55, 0x4F, 0x60, 0x9C, 0x03,
779         0xB8, 0x4F, 0xA3, 0x02, 0xAC, 0xA8, 0xA7, 0x0C,
780         0x1E, 0xB5, 0x6B, 0xF8, 0xC8, 0x4D, 0xDE, 0xD2,
781         0xB0, 0x29, 0x6E, 0x40, 0xE6, 0xD6, 0xC9, 0xE6,
782         0xB9, 0x0F, 0xB6, 0x63, 0xF5, 0xAA, 0x2B, 0x96,
783         0xA7, 0x16, 0xAC, 0x4E, 0x0A, 0x33, 0x1C, 0xA6,
784         0xE6, 0xBD, 0x8A, 0xCF, 0x40, 0xA9, 0xB2, 0xFA,
785         0x63, 0x27, 0xFD, 0x9B, 0xD9, 0xFC, 0xD5, 0x87,
786         0x8D, 0x4C, 0xB6, 0xA4, 0xCB, 0xE7, 0x74, 0x55,
787         0xF4, 0xFB, 0x41, 0x25, 0xB5, 0x4B, 0x0A, 0x1B,
788         0xB1, 0xD6, 0xB7, 0xD9, 0x47, 0x2A, 0xC3, 0x98,
789         0x6A, 0xC4, 0x03, 0x73, 0x1F, 0x93, 0x6E, 0x53,
790         0x19, 0x25, 0x64, 0x15, 0x83, 0xF9, 0x73, 0x2A,
791         0x74, 0xB4, 0x93, 0x69, 0xC4, 0x72, 0xFC, 0x26,
792         0xA2, 0x9F, 0x43, 0x45, 0xDD, 0xB9, 0xEF, 0x36,
793         0xC8, 0x3A, 0xCD, 0x99, 0x9B, 0x54, 0x1A, 0x36,
794         0xC1, 0x59, 0xF8, 0x98, 0xA8, 0xCC, 0x28, 0x0D,
795         0x73, 0x4C, 0xEE, 0x98, 0xCB, 0x7C, 0x58, 0x7E,
796         0x20, 0x75, 0x1E, 0xB7, 0xC9, 0xF8, 0xF2, 0x0E,
797         0x63, 0x9E, 0x05, 0x78, 0x1A, 0xB6, 0xA8, 0x7A,
798         0xF9, 0x98, 0x6A, 0xA6, 0x46, 0x84, 0x2E, 0xF6,
799         0x4B, 0xDC, 0x9B, 0x8F, 0x9B, 0x8F, 0xEE, 0xB4,
800         0xAA, 0x3F, 0xEE, 0xC0, 0x37, 0x27, 0x76, 0xC7,
801         0x95, 0xBB, 0x26, 0x74, 0x69, 0x12, 0x7F, 0xF1,
802         0xBB, 0xFF, 0xAE, 0xB5, 0x99, 0x6E, 0xCB, 0x0C
803 };
804
805 static const uint8_t catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA1_digest[] = {
806         0x9a, 0x4f, 0x88, 0x1b, 0xb6, 0x8f, 0xd8, 0x60,
807         0x42, 0x1a, 0x7d, 0x3d, 0xf5, 0x82, 0x80, 0xf1,
808         0x18, 0x8c, 0x1d, 0x32
809 };
810
811
812 static int
813 test_AES_CBC_HMAC_SHA1_encrypt_digest(void)
814 {
815         struct crypto_testsuite_params *ts_params = &testsuite_params;
816         struct crypto_unittest_params *ut_params = &unittest_params;
817
818         /* Generate test mbuf data and space for digest */
819         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
820                         catch_22_quote, QUOTE_512_BYTES, 0);
821
822         ut_params->digest = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
823                         DIGEST_BYTE_LENGTH_SHA1);
824         TEST_ASSERT_NOT_NULL(ut_params->digest, "no room to append digest");
825
826         /* Setup Cipher Parameters */
827         ut_params->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
828         ut_params->cipher_xform.next = &ut_params->auth_xform;
829
830         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
831         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
832         ut_params->cipher_xform.cipher.key.data = aes_cbc_key;
833         ut_params->cipher_xform.cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
834
835         /* Setup HMAC Parameters */
836         ut_params->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
837
838         ut_params->auth_xform.next = NULL;
839
840         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
841         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
842         ut_params->auth_xform.auth.key.length = HMAC_KEY_LENGTH_SHA1;
843         ut_params->auth_xform.auth.key.data = hmac_sha1_key;
844         ut_params->auth_xform.auth.digest_length = DIGEST_BYTE_LENGTH_SHA1;
845
846         /* Create crypto session*/
847         ut_params->sess = rte_cryptodev_sym_session_create(
848                         ts_params->valid_devs[0],
849                         &ut_params->cipher_xform);
850         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
851
852         /* Generate crypto op data structure */
853         ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
854                         RTE_CRYPTO_OP_TYPE_SYMMETRIC);
855         TEST_ASSERT_NOT_NULL(ut_params->op,
856                         "Failed to allocate symmetric crypto operation struct");
857
858         rte_crypto_op_attach_sym_session(ut_params->op, ut_params->sess);
859
860         struct rte_crypto_sym_op *sym_op = ut_params->op->sym;
861
862         /* set crypto operation source mbuf */
863         sym_op->m_src = ut_params->ibuf;
864
865         /* Set crypto operation authentication parameters */
866         sym_op->auth.digest.data = ut_params->digest;
867         sym_op->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(
868                         ut_params->ibuf, QUOTE_512_BYTES);
869         sym_op->auth.digest.length = DIGEST_BYTE_LENGTH_SHA1;
870
871         sym_op->auth.data.offset = CIPHER_IV_LENGTH_AES_CBC;
872         sym_op->auth.data.length = QUOTE_512_BYTES;
873
874         /* Set crypto operation cipher parameters */
875         sym_op->cipher.iv.data = (uint8_t *)rte_pktmbuf_prepend(ut_params->ibuf,
876                         CIPHER_IV_LENGTH_AES_CBC);
877         sym_op->cipher.iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
878         sym_op->cipher.iv.length = CIPHER_IV_LENGTH_AES_CBC;
879
880         rte_memcpy(sym_op->cipher.iv.data, aes_cbc_iv,
881                         CIPHER_IV_LENGTH_AES_CBC);
882
883         sym_op->cipher.data.offset = CIPHER_IV_LENGTH_AES_CBC;
884         sym_op->cipher.data.length = QUOTE_512_BYTES;
885
886         /* Process crypto operation */
887         TEST_ASSERT_NOT_NULL(process_crypto_request(ts_params->valid_devs[0],
888                         ut_params->op), "failed to process sym crypto op");
889
890         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
891                         "crypto op processing failed");
892
893         /* Validate obuf */
894         uint8_t *ciphertext = rte_pktmbuf_mtod_offset(ut_params->op->sym->m_src,
895                         uint8_t *, CIPHER_IV_LENGTH_AES_CBC);
896
897         TEST_ASSERT_BUFFERS_ARE_EQUAL(ciphertext,
898                         catch_22_quote_2_512_bytes_AES_CBC_ciphertext,
899                         QUOTE_512_BYTES,
900                         "ciphertext data not as expected");
901
902         uint8_t *digest = ciphertext + QUOTE_512_BYTES;
903
904         TEST_ASSERT_BUFFERS_ARE_EQUAL(digest,
905                         catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA1_digest,
906                         gbl_cryptodev_type == RTE_CRYPTODEV_AESNI_MB_PMD ?
907                                         TRUNCATED_DIGEST_BYTE_LENGTH_SHA1 :
908                                         DIGEST_BYTE_LENGTH_SHA1,
909                         "Generated digest data not as expected");
910
911         return TEST_SUCCESS;
912 }
913
914 /* ***** AES-CBC / HMAC-SHA512 Hash Tests ***** */
915
916 #define HMAC_KEY_LENGTH_SHA512  (DIGEST_BYTE_LENGTH_SHA512)
917
918 static uint8_t hmac_sha512_key[] = {
919         0x42, 0x1a, 0x7d, 0x3d, 0xf5, 0x82, 0x80, 0xf1,
920         0xF1, 0x35, 0x5C, 0x3B, 0xDD, 0x9A, 0x65, 0xBA,
921         0x58, 0x34, 0x85, 0x65, 0x1C, 0x42, 0x50, 0x76,
922         0x9a, 0xaf, 0x88, 0x1b, 0xb6, 0x8f, 0xf8, 0x60,
923         0xa2, 0x5a, 0x7f, 0x3f, 0xf4, 0x72, 0x70, 0xf1,
924         0xF5, 0x35, 0x4C, 0x3B, 0xDD, 0x90, 0x65, 0xB0,
925         0x47, 0x3a, 0x75, 0x61, 0x5C, 0xa2, 0x10, 0x76,
926         0x9a, 0xaf, 0x77, 0x5b, 0xb6, 0x7f, 0xf7, 0x60 };
927
928 static const uint8_t catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA512_digest[] = {
929         0x5D, 0x54, 0x66, 0xC1, 0x6E, 0xBC, 0x04, 0xB8,
930         0x46, 0xB8, 0x08, 0x6E, 0xE0, 0xF0, 0x43, 0x48,
931         0x37, 0x96, 0x9C, 0xC6, 0x9C, 0xC2, 0x1E, 0xE8,
932         0xF2, 0x0C, 0x0B, 0xEF, 0x86, 0xA2, 0xE3, 0x70,
933         0x95, 0xC8, 0xB3, 0x06, 0x47, 0xA9, 0x90, 0xE8,
934         0xA0, 0xC6, 0x72, 0x69, 0x05, 0xC0, 0x0D, 0x0E,
935         0x21, 0x96, 0x65, 0x93, 0x74, 0x43, 0x2A, 0x1D,
936         0x2E, 0xBF, 0xC2, 0xC2, 0xEE, 0xCC, 0x2F, 0x0A };
937
938
939
940 static int
941 test_AES_CBC_HMAC_SHA512_decrypt_create_session_params(
942                 struct crypto_unittest_params *ut_params);
943
944 static int
945 test_AES_CBC_HMAC_SHA512_decrypt_perform(struct rte_cryptodev_sym_session *sess,
946                 struct crypto_unittest_params *ut_params,
947                 struct crypto_testsuite_params *ts_params);
948
949
950 static int
951 test_AES_CBC_HMAC_SHA512_decrypt_create_session_params(
952                 struct crypto_unittest_params *ut_params)
953 {
954
955         /* Setup Cipher Parameters */
956         ut_params->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
957         ut_params->cipher_xform.next = NULL;
958
959         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
960         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
961         ut_params->cipher_xform.cipher.key.data = aes_cbc_key;
962         ut_params->cipher_xform.cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
963
964         /* Setup HMAC Parameters */
965         ut_params->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
966         ut_params->auth_xform.next = &ut_params->cipher_xform;
967
968         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
969         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
970         ut_params->auth_xform.auth.key.data = hmac_sha512_key;
971         ut_params->auth_xform.auth.key.length = HMAC_KEY_LENGTH_SHA512;
972         ut_params->auth_xform.auth.digest_length = DIGEST_BYTE_LENGTH_SHA512;
973
974         return TEST_SUCCESS;
975 }
976
977
978 static int
979 test_AES_CBC_HMAC_SHA512_decrypt_perform(struct rte_cryptodev_sym_session *sess,
980                 struct crypto_unittest_params *ut_params,
981                 struct crypto_testsuite_params *ts_params)
982 {
983         /* Generate test mbuf data and digest */
984         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
985                         (const char *)
986                         catch_22_quote_2_512_bytes_AES_CBC_ciphertext,
987                         QUOTE_512_BYTES, 0);
988
989         ut_params->digest = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
990                         DIGEST_BYTE_LENGTH_SHA512);
991         TEST_ASSERT_NOT_NULL(ut_params->digest, "no room to append digest");
992
993         rte_memcpy(ut_params->digest,
994                         catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA512_digest,
995                         DIGEST_BYTE_LENGTH_SHA512);
996
997         /* Generate Crypto op data structure */
998         ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
999                         RTE_CRYPTO_OP_TYPE_SYMMETRIC);
1000         TEST_ASSERT_NOT_NULL(ut_params->op,
1001                         "Failed to allocate symmetric crypto operation struct");
1002
1003         rte_crypto_op_attach_sym_session(ut_params->op, sess);
1004
1005         struct rte_crypto_sym_op *sym_op = ut_params->op->sym;
1006
1007         /* set crypto operation source mbuf */
1008         sym_op->m_src = ut_params->ibuf;
1009
1010         sym_op->auth.digest.data = ut_params->digest;
1011         sym_op->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(
1012                         ut_params->ibuf, QUOTE_512_BYTES);
1013         sym_op->auth.digest.length = DIGEST_BYTE_LENGTH_SHA512;
1014
1015         sym_op->auth.data.offset = CIPHER_IV_LENGTH_AES_CBC;
1016         sym_op->auth.data.length = QUOTE_512_BYTES;
1017
1018         sym_op->cipher.iv.data = (uint8_t *)rte_pktmbuf_prepend(
1019                         ut_params->ibuf, CIPHER_IV_LENGTH_AES_CBC);
1020         sym_op->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(
1021                         ut_params->ibuf, 0);
1022         sym_op->cipher.iv.length = CIPHER_IV_LENGTH_AES_CBC;
1023
1024         rte_memcpy(sym_op->cipher.iv.data, aes_cbc_iv,
1025                         CIPHER_IV_LENGTH_AES_CBC);
1026
1027         sym_op->cipher.data.offset = CIPHER_IV_LENGTH_AES_CBC;
1028         sym_op->cipher.data.length = QUOTE_512_BYTES;
1029
1030         /* Process crypto operation */
1031         TEST_ASSERT_NOT_NULL(process_crypto_request(ts_params->valid_devs[0],
1032                         ut_params->op), "failed to process sym crypto op");
1033
1034         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
1035                         "crypto op processing failed");
1036
1037         ut_params->obuf = ut_params->op->sym->m_src;
1038
1039         /* Validate obuf */
1040         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1041                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
1042                         CIPHER_IV_LENGTH_AES_CBC, catch_22_quote,
1043                         QUOTE_512_BYTES,
1044                         "Plaintext data not as expected");
1045
1046         /* Validate obuf */
1047         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
1048                         "Digest verification failed");
1049
1050         return TEST_SUCCESS;
1051 }
1052
1053 static int
1054 test_AES_mb_all(void)
1055 {
1056         struct crypto_testsuite_params *ts_params = &testsuite_params;
1057         int status;
1058
1059         status = test_AES_all_tests(ts_params->mbuf_pool,
1060                 ts_params->op_mpool, ts_params->valid_devs[0],
1061                 RTE_CRYPTODEV_AESNI_MB_PMD);
1062
1063         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1064
1065         return TEST_SUCCESS;
1066 }
1067
1068 static int
1069 test_AES_qat_all(void)
1070 {
1071         struct crypto_testsuite_params *ts_params = &testsuite_params;
1072         int status;
1073
1074         status = test_AES_all_tests(ts_params->mbuf_pool,
1075                 ts_params->op_mpool, ts_params->valid_devs[0],
1076                 RTE_CRYPTODEV_QAT_SYM_PMD);
1077
1078         TEST_ASSERT_EQUAL(status, 0, "Test failed");
1079
1080         return TEST_SUCCESS;
1081 }
1082
1083 /* ***** Snow3G Tests ***** */
1084 static int
1085 create_snow3g_hash_session(uint8_t dev_id,
1086         const uint8_t *key, const uint8_t key_len,
1087         const uint8_t aad_len, const uint8_t auth_len,
1088         enum rte_crypto_auth_operation op)
1089 {
1090         uint8_t hash_key[key_len];
1091
1092         struct crypto_unittest_params *ut_params = &unittest_params;
1093
1094         memcpy(hash_key, key, key_len);
1095 #ifdef RTE_APP_TEST_DEBUG
1096         rte_hexdump(stdout, "key:", key, key_len);
1097 #endif
1098         /* Setup Authentication Parameters */
1099         ut_params->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1100         ut_params->auth_xform.next = NULL;
1101
1102         ut_params->auth_xform.auth.op = op;
1103         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
1104         ut_params->auth_xform.auth.key.length = key_len;
1105         ut_params->auth_xform.auth.key.data = hash_key;
1106         ut_params->auth_xform.auth.digest_length = auth_len;
1107         ut_params->auth_xform.auth.add_auth_data_length = aad_len;
1108         ut_params->sess = rte_cryptodev_sym_session_create(dev_id,
1109                                 &ut_params->auth_xform);
1110         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
1111         return 0;
1112 }
1113 static int
1114 create_snow3g_cipher_session(uint8_t dev_id,
1115                         enum rte_crypto_cipher_operation op,
1116                         const uint8_t *key, const uint8_t key_len)
1117 {
1118         uint8_t cipher_key[key_len];
1119
1120         struct crypto_unittest_params *ut_params = &unittest_params;
1121
1122         memcpy(cipher_key, key, key_len);
1123
1124         /* Setup Cipher Parameters */
1125         ut_params->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1126         ut_params->cipher_xform.next = NULL;
1127
1128         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
1129         ut_params->cipher_xform.cipher.op = op;
1130         ut_params->cipher_xform.cipher.key.data = cipher_key;
1131         ut_params->cipher_xform.cipher.key.length = key_len;
1132
1133 #ifdef RTE_APP_TEST_DEBUG
1134         rte_hexdump(stdout, "key:", key, key_len);
1135 #endif
1136         /* Create Crypto session */
1137         ut_params->sess = rte_cryptodev_sym_session_create(dev_id,
1138                                                 &ut_params->
1139                                                 cipher_xform);
1140         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
1141         return 0;
1142 }
1143
1144 static int
1145 create_snow3g_cipher_operation(const uint8_t *iv, const unsigned iv_len,
1146                         const unsigned cipher_len,
1147                         const unsigned cipher_offset)
1148 {
1149         struct crypto_testsuite_params *ts_params = &testsuite_params;
1150         struct crypto_unittest_params *ut_params = &unittest_params;
1151         unsigned iv_pad_len = 0;
1152
1153         /* Generate Crypto op data structure */
1154         ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
1155                                 RTE_CRYPTO_OP_TYPE_SYMMETRIC);
1156         TEST_ASSERT_NOT_NULL(ut_params->op,
1157                                 "Failed to allocate pktmbuf offload");
1158
1159         /* Set crypto operation data parameters */
1160         rte_crypto_op_attach_sym_session(ut_params->op, ut_params->sess);
1161
1162         struct rte_crypto_sym_op *sym_op = ut_params->op->sym;
1163
1164         /* set crypto operation source mbuf */
1165         sym_op->m_src = ut_params->ibuf;
1166
1167         /* iv */
1168         iv_pad_len = RTE_ALIGN_CEIL(iv_len, 16);
1169         sym_op->cipher.iv.data = (uint8_t *)rte_pktmbuf_prepend(ut_params->ibuf
1170                         , iv_pad_len);
1171
1172         TEST_ASSERT_NOT_NULL(sym_op->cipher.iv.data, "no room to prepend iv");
1173
1174         memset(sym_op->cipher.iv.data, 0, iv_pad_len);
1175         sym_op->cipher.iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
1176         sym_op->cipher.iv.length = iv_pad_len;
1177
1178         rte_memcpy(sym_op->cipher.iv.data, iv, iv_len);
1179         sym_op->cipher.data.length = cipher_len;
1180         sym_op->cipher.data.offset = cipher_offset;
1181         return 0;
1182 }
1183
1184 static int
1185 create_snow3g_cipher_operation_oop(const uint8_t *iv, const uint8_t iv_len,
1186                         const unsigned cipher_len,
1187                         const unsigned cipher_offset)
1188 {
1189         struct crypto_testsuite_params *ts_params = &testsuite_params;
1190         struct crypto_unittest_params *ut_params = &unittest_params;
1191         unsigned iv_pad_len = 0;
1192
1193         /* Generate Crypto op data structure */
1194         ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
1195                                 RTE_CRYPTO_OP_TYPE_SYMMETRIC);
1196         TEST_ASSERT_NOT_NULL(ut_params->op,
1197                                 "Failed to allocate pktmbuf offload");
1198
1199         /* Set crypto operation data parameters */
1200         rte_crypto_op_attach_sym_session(ut_params->op, ut_params->sess);
1201
1202         struct rte_crypto_sym_op *sym_op = ut_params->op->sym;
1203
1204         /* set crypto operation source mbuf */
1205         sym_op->m_src = ut_params->ibuf;
1206         sym_op->m_dst = ut_params->obuf;
1207
1208         /* iv */
1209         iv_pad_len = RTE_ALIGN_CEIL(iv_len, 16);
1210         sym_op->cipher.iv.data = (uint8_t *)rte_pktmbuf_prepend(ut_params->ibuf
1211                         , iv_pad_len);
1212
1213         TEST_ASSERT_NOT_NULL(sym_op->cipher.iv.data, "no room to prepend iv");
1214
1215         memset(sym_op->cipher.iv.data, 0, iv_pad_len);
1216         sym_op->cipher.iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
1217         sym_op->cipher.iv.length = iv_pad_len;
1218
1219         rte_memcpy(sym_op->cipher.iv.data, iv, iv_len);
1220         sym_op->cipher.data.length = cipher_len;
1221         sym_op->cipher.data.offset = cipher_offset;
1222         return 0;
1223 }
1224
1225 static int
1226 create_snow3g_cipher_auth_session(uint8_t dev_id,
1227                 enum rte_crypto_cipher_operation cipher_op,
1228                 enum rte_crypto_auth_operation auth_op,
1229                 const uint8_t *key, const uint8_t key_len,
1230                 const uint8_t aad_len, const uint8_t auth_len)
1231 {
1232         uint8_t cipher_auth_key[key_len];
1233
1234         struct crypto_unittest_params *ut_params = &unittest_params;
1235
1236         memcpy(cipher_auth_key, key, key_len);
1237
1238         /* Setup Authentication Parameters */
1239         ut_params->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1240         ut_params->auth_xform.next = NULL;
1241
1242         ut_params->auth_xform.auth.op = auth_op;
1243         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
1244         ut_params->auth_xform.auth.key.length = key_len;
1245         /* Hash key = cipher key */
1246         ut_params->auth_xform.auth.key.data = cipher_auth_key;
1247         ut_params->auth_xform.auth.digest_length = auth_len;
1248         ut_params->auth_xform.auth.add_auth_data_length = aad_len;
1249
1250         /* Setup Cipher Parameters */
1251         ut_params->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1252         ut_params->cipher_xform.next = &ut_params->auth_xform;
1253
1254         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
1255         ut_params->cipher_xform.cipher.op = cipher_op;
1256         ut_params->cipher_xform.cipher.key.data = cipher_auth_key;
1257         ut_params->cipher_xform.cipher.key.length = key_len;
1258
1259 #ifdef RTE_APP_TEST_DEBUG
1260         rte_hexdump(stdout, "key:", key, key_len);
1261 #endif
1262         /* Create Crypto session*/
1263         ut_params->sess = rte_cryptodev_sym_session_create(dev_id,
1264                                 &ut_params->cipher_xform);
1265
1266         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
1267         return 0;
1268 }
1269
1270 static int
1271 create_snow3g_auth_cipher_session(uint8_t dev_id,
1272                 enum rte_crypto_cipher_operation cipher_op,
1273                 enum rte_crypto_auth_operation auth_op,
1274                 const uint8_t *key, const uint8_t key_len,
1275                 const uint8_t aad_len, const uint8_t auth_len)
1276         {
1277         uint8_t auth_cipher_key[key_len];
1278
1279         struct crypto_unittest_params *ut_params = &unittest_params;
1280
1281         memcpy(auth_cipher_key, key, key_len);
1282
1283         /* Setup Authentication Parameters */
1284         ut_params->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1285         ut_params->auth_xform.auth.op = auth_op;
1286         ut_params->auth_xform.next = &ut_params->cipher_xform;
1287         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
1288         ut_params->auth_xform.auth.key.length = key_len;
1289         ut_params->auth_xform.auth.key.data = auth_cipher_key;
1290         ut_params->auth_xform.auth.digest_length = auth_len;
1291         ut_params->auth_xform.auth.add_auth_data_length = aad_len;
1292
1293         /* Setup Cipher Parameters */
1294         ut_params->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1295         ut_params->cipher_xform.next = NULL;
1296         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
1297         ut_params->cipher_xform.cipher.op = cipher_op;
1298         ut_params->cipher_xform.cipher.key.data = auth_cipher_key;
1299         ut_params->cipher_xform.cipher.key.length = key_len;
1300
1301 #ifdef RTE_APP_TEST_DEBUG
1302         rte_hexdump(stdout, "key:", key, key_len);
1303 #endif
1304         /* Create Crypto session*/
1305         ut_params->sess = rte_cryptodev_sym_session_create(dev_id,
1306                                 &ut_params->auth_xform);
1307
1308         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
1309
1310         return 0;
1311 }
1312
1313 static int
1314 create_snow3g_hash_operation(const uint8_t *auth_tag,
1315                 const unsigned auth_tag_len,
1316                 const uint8_t *aad, const unsigned aad_len,
1317                 unsigned data_pad_len,
1318                 enum rte_crypto_auth_operation op,
1319                 const unsigned auth_len, const unsigned auth_offset)
1320 {
1321         struct crypto_testsuite_params *ts_params = &testsuite_params;
1322
1323         struct crypto_unittest_params *ut_params = &unittest_params;
1324
1325         unsigned aad_buffer_len;
1326
1327         /* Generate Crypto op data structure */
1328         ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
1329                         RTE_CRYPTO_OP_TYPE_SYMMETRIC);
1330         TEST_ASSERT_NOT_NULL(ut_params->op,
1331                 "Failed to allocate pktmbuf offload");
1332
1333         /* Set crypto operation data parameters */
1334         rte_crypto_op_attach_sym_session(ut_params->op, ut_params->sess);
1335
1336         struct rte_crypto_sym_op *sym_op = ut_params->op->sym;
1337
1338         /* set crypto operation source mbuf */
1339         sym_op->m_src = ut_params->ibuf;
1340
1341         /* aad */
1342         /*
1343         * Always allocate the aad up to the block size.
1344         * The cryptodev API calls out -
1345         *  - the array must be big enough to hold the AAD, plus any
1346         *   space to round this up to the nearest multiple of the
1347         *   block size (16 bytes).
1348         */
1349         aad_buffer_len = ALIGN_POW2_ROUNDUP(aad_len, 16);
1350         sym_op->auth.aad.data = (uint8_t *)rte_pktmbuf_prepend(
1351                         ut_params->ibuf, aad_buffer_len);
1352         TEST_ASSERT_NOT_NULL(sym_op->auth.aad.data,
1353                                         "no room to prepend aad");
1354         sym_op->auth.aad.phys_addr = rte_pktmbuf_mtophys(
1355                         ut_params->ibuf);
1356         sym_op->auth.aad.length = aad_len;
1357
1358         memset(sym_op->auth.aad.data, 0, aad_buffer_len);
1359         rte_memcpy(sym_op->auth.aad.data, aad, aad_len);
1360
1361 #ifdef RTE_APP_TEST_DEBUG
1362         rte_hexdump(stdout, "aad:",
1363                         sym_op->auth.aad.data, aad_len);
1364 #endif
1365
1366         /* digest */
1367         sym_op->auth.digest.data = (uint8_t *)rte_pktmbuf_append(
1368                                         ut_params->ibuf, auth_tag_len);
1369
1370         TEST_ASSERT_NOT_NULL(sym_op->auth.digest.data,
1371                                 "no room to append auth tag");
1372         ut_params->digest = sym_op->auth.digest.data;
1373         sym_op->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(
1374                         ut_params->ibuf, data_pad_len + aad_len);
1375         sym_op->auth.digest.length = auth_tag_len;
1376         if (op == RTE_CRYPTO_AUTH_OP_GENERATE)
1377                 memset(sym_op->auth.digest.data, 0, auth_tag_len);
1378         else
1379                 rte_memcpy(sym_op->auth.digest.data, auth_tag, auth_tag_len);
1380
1381 #ifdef RTE_APP_TEST_DEBUG
1382         rte_hexdump(stdout, "digest:",
1383                 sym_op->auth.digest.data,
1384                 sym_op->auth.digest.length);
1385 #endif
1386
1387         sym_op->auth.data.length = auth_len;
1388         sym_op->auth.data.offset = auth_offset;
1389
1390         return 0;
1391 }
1392
1393 static int
1394 create_snow3g_cipher_hash_operation(const uint8_t *auth_tag,
1395                 const unsigned auth_tag_len,
1396                 const uint8_t *aad, const uint8_t aad_len,
1397                 unsigned data_pad_len,
1398                 enum rte_crypto_auth_operation op,
1399                 const uint8_t *iv, const uint8_t iv_len,
1400                 const unsigned cipher_len, const unsigned cipher_offset,
1401                 const unsigned auth_len, const unsigned auth_offset)
1402 {
1403         struct crypto_testsuite_params *ts_params = &testsuite_params;
1404         struct crypto_unittest_params *ut_params = &unittest_params;
1405
1406         unsigned iv_pad_len = 0;
1407         unsigned aad_buffer_len;
1408
1409         /* Generate Crypto op data structure */
1410         ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
1411                         RTE_CRYPTO_OP_TYPE_SYMMETRIC);
1412         TEST_ASSERT_NOT_NULL(ut_params->op,
1413                         "Failed to allocate pktmbuf offload");
1414         /* Set crypto operation data parameters */
1415         rte_crypto_op_attach_sym_session(ut_params->op, ut_params->sess);
1416
1417         struct rte_crypto_sym_op *sym_op = ut_params->op->sym;
1418
1419         /* set crypto operation source mbuf */
1420         sym_op->m_src = ut_params->ibuf;
1421
1422
1423         /* iv */
1424         iv_pad_len = RTE_ALIGN_CEIL(iv_len, 16);
1425
1426         sym_op->cipher.iv.data = (uint8_t *)rte_pktmbuf_prepend(
1427                 ut_params->ibuf, iv_pad_len);
1428         TEST_ASSERT_NOT_NULL(sym_op->cipher.iv.data, "no room to prepend iv");
1429
1430         memset(sym_op->cipher.iv.data, 0, iv_pad_len);
1431         sym_op->cipher.iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
1432         sym_op->cipher.iv.length = iv_pad_len;
1433
1434         rte_memcpy(sym_op->cipher.iv.data, iv, iv_len);
1435
1436         sym_op->cipher.data.length = cipher_len;
1437         sym_op->cipher.data.offset = cipher_offset;
1438
1439         /* aad */
1440         /*
1441         * Always allocate the aad up to the block size.
1442         * The cryptodev API calls out -
1443         *  - the array must be big enough to hold the AAD, plus any
1444         *   space to round this up to the nearest multiple of the
1445         *   block size (16 bytes).
1446         */
1447         aad_buffer_len = ALIGN_POW2_ROUNDUP(aad_len, 16);
1448
1449         sym_op->auth.aad.data =
1450                         (uint8_t *)rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *);
1451         TEST_ASSERT_NOT_NULL(sym_op->auth.aad.data,
1452                         "no room to prepend aad");
1453         sym_op->auth.aad.phys_addr = rte_pktmbuf_mtophys(
1454                         ut_params->ibuf);
1455         sym_op->auth.aad.length = aad_len;
1456
1457         memset(sym_op->auth.aad.data, 0, aad_buffer_len);
1458         rte_memcpy(sym_op->auth.aad.data, aad, aad_len);
1459
1460 #ifdef RTE_APP_TEST_DEBUG
1461         rte_hexdump(stdout, "aad:",
1462                         sym_op->auth.aad.data, aad_len);
1463 #endif
1464
1465         /* digest */
1466         sym_op->auth.digest.data = (uint8_t *)rte_pktmbuf_append(
1467                         ut_params->ibuf, auth_tag_len);
1468
1469         TEST_ASSERT_NOT_NULL(sym_op->auth.digest.data,
1470                         "no room to append auth tag");
1471         ut_params->digest = sym_op->auth.digest.data;
1472         sym_op->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(
1473                         ut_params->ibuf, data_pad_len + aad_len);
1474         sym_op->auth.digest.length = auth_tag_len;
1475         if (op == RTE_CRYPTO_AUTH_OP_GENERATE)
1476                 memset(sym_op->auth.digest.data, 0, auth_tag_len);
1477         else
1478                 rte_memcpy(sym_op->auth.digest.data, auth_tag, auth_tag_len);
1479
1480         #ifdef RTE_APP_TEST_DEBUG
1481         rte_hexdump(stdout, "digest:",
1482                 sym_op->auth.digest.data,
1483                 sym_op->auth.digest.length);
1484         #endif
1485
1486         sym_op->auth.data.length = auth_len;
1487         sym_op->auth.data.offset = auth_offset;
1488
1489         return 0;
1490 }
1491
1492 static int
1493 create_snow3g_auth_cipher_operation(const unsigned auth_tag_len,
1494                 const uint8_t *iv, const uint8_t iv_len,
1495                 const uint8_t *aad, const uint8_t aad_len,
1496                 unsigned data_pad_len,
1497                 const unsigned cipher_len, const unsigned cipher_offset,
1498                 const unsigned auth_len, const unsigned auth_offset)
1499 {
1500         struct crypto_testsuite_params *ts_params = &testsuite_params;
1501         struct crypto_unittest_params *ut_params = &unittest_params;
1502
1503         unsigned iv_pad_len = 0;
1504         unsigned aad_buffer_len = 0;
1505
1506         /* Generate Crypto op data structure */
1507         ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
1508                         RTE_CRYPTO_OP_TYPE_SYMMETRIC);
1509         TEST_ASSERT_NOT_NULL(ut_params->op,
1510                         "Failed to allocate pktmbuf offload");
1511
1512         /* Set crypto operation data parameters */
1513         rte_crypto_op_attach_sym_session(ut_params->op, ut_params->sess);
1514
1515         struct rte_crypto_sym_op *sym_op = ut_params->op->sym;
1516
1517         /* set crypto operation source mbuf */
1518         sym_op->m_src = ut_params->ibuf;
1519
1520         /* digest */
1521         sym_op->auth.digest.data = (uint8_t *)rte_pktmbuf_append(
1522                         ut_params->ibuf, auth_tag_len);
1523
1524         TEST_ASSERT_NOT_NULL(sym_op->auth.digest.data,
1525                         "no room to append auth tag");
1526
1527         sym_op->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(
1528                         ut_params->ibuf, data_pad_len);
1529         sym_op->auth.digest.length = auth_tag_len;
1530
1531         memset(sym_op->auth.digest.data, 0, auth_tag_len);
1532
1533         #ifdef RTE_APP_TEST_DEBUG
1534                 rte_hexdump(stdout, "digest:",
1535                         sym_op->auth.digest.data,
1536                         sym_op->auth.digest.length);
1537         #endif
1538         /* iv */
1539         iv_pad_len = RTE_ALIGN_CEIL(iv_len, 16);
1540
1541         sym_op->cipher.iv.data = (uint8_t *)rte_pktmbuf_prepend(
1542                 ut_params->ibuf, iv_pad_len);
1543         TEST_ASSERT_NOT_NULL(sym_op->cipher.iv.data, "no room to prepend iv");
1544
1545         memset(sym_op->cipher.iv.data, 0, iv_pad_len);
1546         sym_op->cipher.iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
1547         sym_op->cipher.iv.length = iv_pad_len;
1548
1549         rte_memcpy(sym_op->cipher.iv.data, iv, iv_len);
1550
1551         /* aad */
1552         /*
1553         * Always allocate the aad up to the block size.
1554         * The cryptodev API calls out -
1555         *  - the array must be big enough to hold the AAD, plus any
1556         *   space to round this up to the nearest multiple of the
1557         *   block size (16 bytes).
1558         */
1559         aad_buffer_len = ALIGN_POW2_ROUNDUP(aad_len, 16);
1560
1561         sym_op->auth.aad.data = (uint8_t *)rte_pktmbuf_prepend(
1562         ut_params->ibuf, aad_buffer_len);
1563         TEST_ASSERT_NOT_NULL(sym_op->auth.aad.data,
1564                                 "no room to prepend aad");
1565         sym_op->auth.aad.phys_addr = rte_pktmbuf_mtophys(
1566                                 ut_params->ibuf);
1567         sym_op->auth.aad.length = aad_len;
1568
1569         memset(sym_op->auth.aad.data, 0, aad_buffer_len);
1570         rte_memcpy(sym_op->auth.aad.data, aad, aad_len);
1571
1572 #ifdef RTE_APP_TEST_DEBUG
1573         rte_hexdump(stdout, "aad:",
1574                         sym_op->auth.aad.data, aad_len);
1575 #endif
1576
1577         sym_op->cipher.data.length = cipher_len;
1578         sym_op->cipher.data.offset = auth_offset + cipher_offset;
1579
1580         sym_op->auth.data.length = auth_len;
1581         sym_op->auth.data.offset = auth_offset + cipher_offset;
1582
1583         return 0;
1584 }
1585
1586 static int
1587 test_snow3g_authentication(const struct snow3g_hash_test_data *tdata)
1588 {
1589         struct crypto_testsuite_params *ts_params = &testsuite_params;
1590         struct crypto_unittest_params *ut_params = &unittest_params;
1591
1592         int retval;
1593         unsigned plaintext_pad_len;
1594         uint8_t *plaintext;
1595
1596         /* Create SNOW3G session */
1597         retval = create_snow3g_hash_session(ts_params->valid_devs[0],
1598                         tdata->key.data, tdata->key.len,
1599                         tdata->aad.len, tdata->digest.len,
1600                         RTE_CRYPTO_AUTH_OP_GENERATE);
1601         if (retval < 0)
1602                 return retval;
1603
1604         /* alloc mbuf and set payload */
1605         ut_params->ibuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
1606
1607         memset(rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *), 0,
1608         rte_pktmbuf_tailroom(ut_params->ibuf));
1609
1610         /* Append data which is padded to a multiple of */
1611         /* the algorithms block size */
1612         plaintext_pad_len = tdata->plaintext.len >> 3;
1613         plaintext = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
1614                                 plaintext_pad_len);
1615         memcpy(plaintext, tdata->plaintext.data, tdata->plaintext.len >> 3);
1616
1617         /* Create SNOW3G opertaion */
1618         retval = create_snow3g_hash_operation(NULL, tdata->digest.len,
1619                         tdata->aad.data, tdata->aad.len,
1620                         plaintext_pad_len, RTE_CRYPTO_AUTH_OP_GENERATE,
1621                         tdata->validAuthLenInBits.len,
1622                         tdata->validAuthOffsetLenInBits.len);
1623         if (retval < 0)
1624                 return retval;
1625
1626         ut_params->op = process_crypto_request(ts_params->valid_devs[0],
1627                                 ut_params->op);
1628         ut_params->obuf = ut_params->op->sym->m_src;
1629         TEST_ASSERT_NOT_NULL(ut_params->op, "failed to retrieve obuf");
1630         ut_params->digest = rte_pktmbuf_mtod(ut_params->obuf, uint8_t *)
1631                         + plaintext_pad_len + tdata->aad.len;
1632
1633         /* Validate obuf */
1634         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1635         ut_params->digest,
1636         tdata->digest.data,
1637         DIGEST_BYTE_LENGTH_SNOW3G_UIA2,
1638         "Snow3G Generated auth tag not as expected");
1639
1640         return 0;
1641 }
1642
1643 static int
1644 test_snow3g_authentication_verify(const struct snow3g_hash_test_data *tdata)
1645 {
1646         struct crypto_testsuite_params *ts_params = &testsuite_params;
1647         struct crypto_unittest_params *ut_params = &unittest_params;
1648
1649         int retval;
1650         unsigned plaintext_pad_len;
1651         uint8_t *plaintext;
1652
1653         /* Create SNOW3G session */
1654         retval = create_snow3g_hash_session(ts_params->valid_devs[0],
1655                                 tdata->key.data, tdata->key.len,
1656                                 tdata->aad.len, tdata->digest.len,
1657                                 RTE_CRYPTO_AUTH_OP_VERIFY);
1658         if (retval < 0)
1659                 return retval;
1660         /* alloc mbuf and set payload */
1661         ut_params->ibuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
1662
1663         memset(rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *), 0,
1664         rte_pktmbuf_tailroom(ut_params->ibuf));
1665
1666         /* Append data which is padded to a multiple */
1667         /* of the algorithms block size */
1668         plaintext_pad_len = tdata->plaintext.len >> 3;
1669         plaintext = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
1670                                         plaintext_pad_len);
1671         memcpy(plaintext, tdata->plaintext.data, tdata->plaintext.len >> 3);
1672
1673         /* Create SNOW3G operation */
1674         retval = create_snow3g_hash_operation(tdata->digest.data,
1675                         tdata->digest.len,
1676                         tdata->aad.data, tdata->aad.len,
1677                         plaintext_pad_len,
1678                         RTE_CRYPTO_AUTH_OP_VERIFY,
1679                         tdata->validAuthLenInBits.len,
1680                         tdata->validAuthOffsetLenInBits.len);
1681         if (retval < 0)
1682                 return retval;
1683
1684         ut_params->op = process_crypto_request(ts_params->valid_devs[0],
1685                                 ut_params->op);
1686         TEST_ASSERT_NOT_NULL(ut_params->op, "failed to retrieve obuf");
1687         ut_params->obuf = ut_params->op->sym->m_src;
1688         ut_params->digest = rte_pktmbuf_mtod(ut_params->obuf, uint8_t *)
1689                                 + plaintext_pad_len + tdata->aad.len;
1690
1691         /* Validate obuf */
1692         if (ut_params->op->status == RTE_CRYPTO_OP_STATUS_SUCCESS)
1693                 return 0;
1694         else
1695                 return -1;
1696
1697         return 0;
1698 }
1699
1700
1701 static int
1702 test_snow3g_hash_generate_test_case_1(void)
1703 {
1704         return test_snow3g_authentication(&snow3g_hash_test_case_1);
1705 }
1706
1707 static int
1708 test_snow3g_hash_generate_test_case_2(void)
1709 {
1710         return test_snow3g_authentication(&snow3g_hash_test_case_2);
1711 }
1712
1713 static int
1714 test_snow3g_hash_generate_test_case_3(void)
1715 {
1716         return test_snow3g_authentication(&snow3g_hash_test_case_3);
1717 }
1718
1719 static int
1720 test_snow3g_hash_verify_test_case_1(void)
1721 {
1722         return test_snow3g_authentication_verify(&snow3g_hash_test_case_1);
1723
1724 }
1725
1726 static int
1727 test_snow3g_hash_verify_test_case_2(void)
1728 {
1729         return test_snow3g_authentication_verify(&snow3g_hash_test_case_2);
1730 }
1731
1732 static int
1733 test_snow3g_hash_verify_test_case_3(void)
1734 {
1735         return test_snow3g_authentication_verify(&snow3g_hash_test_case_3);
1736 }
1737
1738 static int
1739 test_snow3g_encryption(const struct snow3g_test_data *tdata)
1740 {
1741         struct crypto_testsuite_params *ts_params = &testsuite_params;
1742         struct crypto_unittest_params *ut_params = &unittest_params;
1743
1744         int retval;
1745         uint8_t *plaintext, *ciphertext;
1746         uint8_t plaintext_pad_len;
1747         uint8_t lastByteValidBits = 8;
1748         uint8_t lastByteMask = 0xFF;
1749
1750         /* Create SNOW3G session */
1751         retval = create_snow3g_cipher_session(ts_params->valid_devs[0],
1752                                         RTE_CRYPTO_CIPHER_OP_ENCRYPT,
1753                                         tdata->key.data, tdata->key.len);
1754         if (retval < 0)
1755                 return retval;
1756
1757         ut_params->ibuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
1758
1759         /* Clear mbuf payload */
1760         memset(rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *), 0,
1761                rte_pktmbuf_tailroom(ut_params->ibuf));
1762
1763         /*
1764          * Append data which is padded to a
1765          * multiple of the algorithms block size
1766          */
1767         /*tdata->plaintext.len = tdata->plaintext.len >> 3;*/
1768         plaintext_pad_len = RTE_ALIGN_CEIL((tdata->plaintext.len >> 3), 16);
1769
1770         plaintext = (uint8_t *) rte_pktmbuf_append(ut_params->ibuf,
1771                                                 plaintext_pad_len);
1772         memcpy(plaintext, tdata->plaintext.data, (tdata->plaintext.len >> 3));
1773
1774 #ifdef RTE_APP_TEST_DEBUG
1775         rte_hexdump(stdout, "plaintext:", plaintext, tdata->plaintext.len);
1776 #endif
1777         /* Create SNOW3G operation */
1778         retval = create_snow3g_cipher_operation(tdata->iv.data, tdata->iv.len,
1779                                         tdata->validCipherLenInBits.len,
1780                                         tdata->validCipherOffsetLenInBits.len);
1781         if (retval < 0)
1782                 return retval;
1783
1784         ut_params->op = process_crypto_request(ts_params->valid_devs[0],
1785                                                 ut_params->op);
1786         TEST_ASSERT_NOT_NULL(ut_params->op, "failed to retrieve obuf");
1787
1788         ut_params->obuf = ut_params->op->sym->m_dst;
1789         if (ut_params->obuf)
1790                 ciphertext = rte_pktmbuf_mtod(ut_params->obuf, uint8_t *)
1791                                 + tdata->iv.len;
1792         else
1793                 ciphertext = plaintext;
1794
1795         lastByteValidBits = (tdata->validDataLenInBits.len % 8);
1796         if (lastByteValidBits == 0)
1797                 lastByteValidBits = 8;
1798         lastByteMask = lastByteMask << (8 - lastByteValidBits);
1799         (*(ciphertext + (tdata->ciphertext.len >> 3) - 1)) &= lastByteMask;
1800
1801 #ifdef RTE_APP_TEST_DEBUG
1802         rte_hexdump(stdout, "ciphertext:", ciphertext, tdata->ciphertext.len);
1803 #endif
1804         /* Validate obuf */
1805         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1806                 ciphertext,
1807                 tdata->ciphertext.data,
1808                 tdata->ciphertext.len >> 3,
1809                 "Snow3G Ciphertext data not as expected");
1810         return 0;
1811 }
1812
1813
1814 static int
1815 test_snow3g_encryption_oop(const struct snow3g_test_data *tdata)
1816 {
1817         struct crypto_testsuite_params *ts_params = &testsuite_params;
1818         struct crypto_unittest_params *ut_params = &unittest_params;
1819         uint8_t *plaintext, *ciphertext;
1820
1821         int retval;
1822         uint8_t plaintext_pad_len;
1823         uint8_t lastByteValidBits = 8;
1824         uint8_t lastByteMask = 0xFF;
1825
1826         /* Create SNOW3G session */
1827         retval = create_snow3g_cipher_session(ts_params->valid_devs[0],
1828                                         RTE_CRYPTO_CIPHER_OP_ENCRYPT,
1829                                         tdata->key.data, tdata->key.len);
1830         if (retval < 0)
1831                 return retval;
1832
1833         ut_params->ibuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
1834         ut_params->obuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
1835
1836         TEST_ASSERT_NOT_NULL(ut_params->ibuf,
1837                         "Failed to allocate input buffer in mempool");
1838         TEST_ASSERT_NOT_NULL(ut_params->obuf,
1839                         "Failed to allocate output buffer in mempool");
1840
1841         /* Clear mbuf payload */
1842         memset(rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *), 0,
1843                rte_pktmbuf_tailroom(ut_params->ibuf));
1844
1845         /*
1846          * Append data which is padded to a
1847          * multiple of the algorithms block size
1848          */
1849         /*tdata->plaintext.len = tdata->plaintext.len >> 3;*/
1850         plaintext_pad_len = RTE_ALIGN_CEIL((tdata->plaintext.len >> 3), 16);
1851
1852         plaintext = (uint8_t *) rte_pktmbuf_append(ut_params->ibuf,
1853                                                 plaintext_pad_len);
1854
1855         rte_pktmbuf_append(ut_params->obuf,
1856                                                 plaintext_pad_len);
1857
1858         memcpy(plaintext, tdata->plaintext.data, (tdata->plaintext.len >> 3));
1859
1860 #ifdef RTE_APP_TEST_DEBUG
1861         rte_hexdump(stdout, "plaintext:", plaintext, tdata->plaintext.len);
1862 #endif
1863         /* Create SNOW3G operation */
1864         retval = create_snow3g_cipher_operation_oop(tdata->iv.data,
1865                                         tdata->iv.len,
1866                                         tdata->validCipherLenInBits.len,
1867                                         tdata->validCipherOffsetLenInBits.len);
1868         if (retval < 0)
1869                 return retval;
1870
1871         ut_params->op = process_crypto_request(ts_params->valid_devs[0],
1872                                                 ut_params->op);
1873         TEST_ASSERT_NOT_NULL(ut_params->op, "failed to retrieve obuf");
1874
1875         ut_params->obuf = ut_params->op->sym->m_dst;
1876         if (ut_params->obuf)
1877                 ciphertext = rte_pktmbuf_mtod(ut_params->obuf, uint8_t *)
1878                                 + tdata->iv.len;
1879         else
1880                 ciphertext = plaintext;
1881
1882         lastByteValidBits = (tdata->validDataLenInBits.len % 8);
1883         if (lastByteValidBits == 0)
1884                 lastByteValidBits = 8;
1885         lastByteMask = lastByteMask << (8 - lastByteValidBits);
1886         (*(ciphertext + (tdata->ciphertext.len >> 3) - 1)) &= lastByteMask;
1887
1888 #ifdef RTE_APP_TEST_DEBUG
1889         rte_hexdump(stdout, "ciphertext:", ciphertext, tdata->ciphertext.len);
1890 #endif
1891         /* Validate obuf */
1892         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1893                 ciphertext,
1894                 tdata->ciphertext.data,
1895                 tdata->ciphertext.len >> 3,
1896                 "Snow3G Ciphertext data not as expected");
1897         return 0;
1898 }
1899
1900
1901 static int test_snow3g_decryption(const struct snow3g_test_data *tdata)
1902 {
1903         struct crypto_testsuite_params *ts_params = &testsuite_params;
1904         struct crypto_unittest_params *ut_params = &unittest_params;
1905
1906         int retval;
1907
1908         uint8_t *plaintext, *ciphertext;
1909         uint8_t ciphertext_pad_len;
1910         uint8_t lastByteValidBits = 8;
1911         uint8_t lastByteMask = 0xFF;
1912
1913         /* Create SNOW3G session */
1914         retval = create_snow3g_cipher_session(ts_params->valid_devs[0],
1915                                         RTE_CRYPTO_CIPHER_OP_DECRYPT,
1916                                         tdata->key.data, tdata->key.len);
1917         if (retval < 0)
1918                 return retval;
1919
1920         ut_params->ibuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
1921
1922         /* Clear mbuf payload */
1923         memset(rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *), 0,
1924                rte_pktmbuf_tailroom(ut_params->ibuf));
1925
1926         /*
1927          * Append data which is padded to a
1928          * multiple of the algorithms block size
1929          */
1930         ciphertext_pad_len = RTE_ALIGN_CEIL((tdata->ciphertext.len >> 3), 16);
1931
1932         ciphertext = (uint8_t *) rte_pktmbuf_append(ut_params->ibuf,
1933                                                 ciphertext_pad_len);
1934         memcpy(ciphertext, tdata->ciphertext.data, tdata->ciphertext.len >> 3);
1935
1936 #ifdef RTE_APP_TEST_DEBUG
1937         rte_hexdump(stdout, "ciphertext:", ciphertext, tdata->ciphertext.len);
1938 #endif
1939         /* Create SNOW3G operation */
1940         retval = create_snow3g_cipher_operation(tdata->iv.data, tdata->iv.len,
1941                                         tdata->validCipherLenInBits.len,
1942                                         tdata->validCipherOffsetLenInBits.len);
1943         if (retval < 0)
1944                 return retval;
1945
1946         ut_params->op = process_crypto_request(ts_params->valid_devs[0],
1947                                                 ut_params->op);
1948         TEST_ASSERT_NOT_NULL(ut_params->op, "failed to retrieve obuf");
1949         ut_params->obuf = ut_params->op->sym->m_src;
1950         if (ut_params->obuf)
1951                 plaintext = rte_pktmbuf_mtod(ut_params->obuf, uint8_t *)
1952                                 + tdata->iv.len;
1953         else
1954                 plaintext = ciphertext;
1955         lastByteValidBits = (tdata->validDataLenInBits.len  % 8);
1956         if (lastByteValidBits == 0)
1957                 lastByteValidBits = 8;
1958         lastByteMask = lastByteMask << (8 - lastByteValidBits);
1959         (*(ciphertext + (tdata->ciphertext.len >> 3) - 1)) &= lastByteMask;
1960
1961 #ifdef RTE_APP_TEST_DEBUG
1962         rte_hexdump(stdout, "plaintext:", plaintext, tdata->plaintext.len);
1963 #endif
1964         /* Validate obuf */
1965         TEST_ASSERT_BUFFERS_ARE_EQUAL(plaintext,
1966                                 tdata->plaintext.data,
1967                                 tdata->plaintext.len >> 3,
1968                                 "Snow3G Plaintext data not as expected");
1969         return 0;
1970 }
1971
1972 static int test_snow3g_decryption_oop(const struct snow3g_test_data *tdata)
1973 {
1974         struct crypto_testsuite_params *ts_params = &testsuite_params;
1975         struct crypto_unittest_params *ut_params = &unittest_params;
1976
1977         int retval;
1978
1979         uint8_t *plaintext, *ciphertext;
1980         uint8_t ciphertext_pad_len;
1981         uint8_t lastByteValidBits = 8;
1982         uint8_t lastByteMask = 0xFF;
1983
1984         /* Create SNOW3G session */
1985         retval = create_snow3g_cipher_session(ts_params->valid_devs[0],
1986                                         RTE_CRYPTO_CIPHER_OP_DECRYPT,
1987                                         tdata->key.data, tdata->key.len);
1988         if (retval < 0)
1989                 return retval;
1990
1991         ut_params->ibuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
1992         ut_params->obuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
1993
1994         TEST_ASSERT_NOT_NULL(ut_params->ibuf,
1995                         "Failed to allocate input buffer");
1996         TEST_ASSERT_NOT_NULL(ut_params->obuf,
1997                         "Failed to allocate output buffer");
1998
1999         /* Clear mbuf payload */
2000         memset(rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *), 0,
2001                rte_pktmbuf_tailroom(ut_params->ibuf));
2002
2003         memset(rte_pktmbuf_mtod(ut_params->obuf, uint8_t *), 0,
2004                        rte_pktmbuf_tailroom(ut_params->obuf));
2005
2006         /*
2007          * Append data which is padded to a
2008          * multiple of the algorithms block size
2009          */
2010         ciphertext_pad_len = RTE_ALIGN_CEIL((tdata->ciphertext.len >> 3), 16);
2011
2012         ciphertext = (uint8_t *) rte_pktmbuf_append(ut_params->ibuf,
2013                                                 ciphertext_pad_len);
2014
2015         rte_pktmbuf_append(ut_params->obuf,
2016                                                 ciphertext_pad_len);
2017
2018         memcpy(ciphertext, tdata->ciphertext.data, tdata->ciphertext.len >> 3);
2019
2020 #ifdef RTE_APP_TEST_DEBUG
2021         rte_hexdump(stdout, "ciphertext:", ciphertext, tdata->ciphertext.len);
2022 #endif
2023         /* Create SNOW3G operation */
2024         retval = create_snow3g_cipher_operation_oop(tdata->iv.data,
2025                                         tdata->iv.len,
2026                                         tdata->validCipherLenInBits.len,
2027                                         tdata->validCipherOffsetLenInBits.len);
2028         if (retval < 0)
2029                 return retval;
2030
2031         ut_params->op = process_crypto_request(ts_params->valid_devs[0],
2032                                                 ut_params->op);
2033         TEST_ASSERT_NOT_NULL(ut_params->op, "failed to retrieve obuf");
2034         ut_params->obuf = ut_params->op->sym->m_dst;
2035         if (ut_params->obuf)
2036                 plaintext = rte_pktmbuf_mtod(ut_params->obuf, uint8_t *)
2037                                 + tdata->iv.len;
2038         else
2039                 plaintext = ciphertext;
2040         lastByteValidBits = (tdata->validDataLenInBits.len  % 8);
2041         if (lastByteValidBits == 0)
2042                 lastByteValidBits = 8;
2043         lastByteMask = lastByteMask << (8 - lastByteValidBits);
2044         (*(plaintext + (tdata->ciphertext.len >> 3) - 1)) &= lastByteMask;
2045
2046 #ifdef RTE_APP_TEST_DEBUG
2047         rte_hexdump(stdout, "plaintext:", plaintext, tdata->plaintext.len);
2048 #endif
2049         /* Validate obuf */
2050         TEST_ASSERT_BUFFERS_ARE_EQUAL(plaintext,
2051                                 tdata->plaintext.data,
2052                                 tdata->plaintext.len >> 3,
2053                                 "Snow3G Plaintext data not as expected");
2054         return 0;
2055 }
2056
2057 static int
2058 test_snow3g_authenticated_encryption(const struct snow3g_test_data *tdata)
2059 {
2060         struct crypto_testsuite_params *ts_params = &testsuite_params;
2061         struct crypto_unittest_params *ut_params = &unittest_params;
2062
2063         int retval;
2064
2065         uint8_t *plaintext, *ciphertext;
2066         uint8_t plaintext_pad_len;
2067         uint8_t lastByteValidBits = 8;
2068         uint8_t lastByteMask = 0xFF;
2069
2070         /* Create SNOW3G session */
2071         retval = create_snow3g_cipher_auth_session(ts_params->valid_devs[0],
2072                         RTE_CRYPTO_CIPHER_OP_ENCRYPT,
2073                         RTE_CRYPTO_AUTH_OP_GENERATE,
2074                         tdata->key.data, tdata->key.len,
2075                         tdata->aad.len, tdata->digest.len);
2076         if (retval < 0)
2077                 return retval;
2078         ut_params->ibuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
2079
2080         /* clear mbuf payload */
2081         memset(rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *), 0,
2082                         rte_pktmbuf_tailroom(ut_params->ibuf));
2083
2084         /* Append data which is padded to a multiple */
2085         /*  of the algorithms block size */
2086         plaintext_pad_len = tdata->plaintext.len >> 3;
2087
2088         plaintext = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
2089                         plaintext_pad_len);
2090         memcpy(plaintext, tdata->plaintext.data, tdata->plaintext.len >> 3);
2091
2092 #ifdef RTE_APP_TEST_DEBUG
2093         rte_hexdump(stdout, "plaintext:", plaintext, tdata->plaintext.len);
2094 #endif
2095
2096         /* Create SNOW3G operation */
2097         retval = create_snow3g_cipher_hash_operation(tdata->digest.data,
2098                         tdata->digest.len, tdata->aad.data,
2099                         tdata->aad.len, /*tdata->plaintext.len,*/
2100                         plaintext_pad_len, RTE_CRYPTO_AUTH_OP_GENERATE,
2101                         tdata->iv.data, tdata->iv.len,
2102                         tdata->validCipherLenInBits.len,
2103                         tdata->validCipherOffsetLenInBits.len,
2104                         tdata->validAuthLenInBits.len,
2105                         tdata->validAuthOffsetLenInBits.len);
2106         if (retval < 0)
2107                 return retval;
2108
2109         ut_params->op = process_crypto_request(ts_params->valid_devs[0],
2110                         ut_params->op);
2111         TEST_ASSERT_NOT_NULL(ut_params->op, "failed to retrieve obuf");
2112         ut_params->obuf = ut_params->op->sym->m_src;
2113         if (ut_params->obuf)
2114                 ciphertext = rte_pktmbuf_mtod(ut_params->obuf, uint8_t *)
2115                                 + tdata->iv.len;
2116         else
2117                 ciphertext = plaintext;
2118         lastByteValidBits = (tdata->validDataLenInBits.len % 8);
2119         if (lastByteValidBits == 0)
2120                 lastByteValidBits = 8;
2121         lastByteMask = lastByteMask << (8-lastByteValidBits);
2122         (*(ciphertext + (tdata->ciphertext.len >> 3) - 1)) &= lastByteMask;
2123
2124 #ifdef RTE_APP_TEST_DEBUG
2125         rte_hexdump(stdout, "ciphertext:", ciphertext, tdata->ciphertext.len);
2126 #endif
2127         /* Validate obuf */
2128         TEST_ASSERT_BUFFERS_ARE_EQUAL(
2129                         ciphertext,
2130                         tdata->ciphertext.data,
2131                         tdata->ciphertext.len >> 3,
2132                         "Snow3G Ciphertext data not as expected");
2133
2134         ut_params->digest = rte_pktmbuf_mtod(ut_params->obuf, uint8_t *)
2135             + plaintext_pad_len + tdata->aad.len;
2136
2137         /* Validate obuf */
2138         TEST_ASSERT_BUFFERS_ARE_EQUAL(
2139                         ut_params->digest,
2140                         tdata->digest.data,
2141                         DIGEST_BYTE_LENGTH_SNOW3G_UIA2,
2142                         "Snow3G Generated auth tag not as expected");
2143         return 0;
2144 }
2145 static int
2146 test_snow3g_encrypted_authentication(const struct snow3g_test_data *tdata)
2147 {
2148         struct crypto_testsuite_params *ts_params = &testsuite_params;
2149         struct crypto_unittest_params *ut_params = &unittest_params;
2150
2151         int retval;
2152
2153         uint8_t *plaintext, *ciphertext;
2154         uint8_t plaintext_pad_len;
2155         uint8_t lastByteValidBits = 8;
2156         uint8_t lastByteMask = 0xFF;
2157
2158         /* Create SNOW3G session */
2159         retval = create_snow3g_auth_cipher_session(ts_params->valid_devs[0],
2160                         RTE_CRYPTO_CIPHER_OP_ENCRYPT,
2161                         RTE_CRYPTO_AUTH_OP_GENERATE,
2162                         tdata->key.data, tdata->key.len,
2163                         tdata->aad.len, tdata->digest.len);
2164         if (retval < 0)
2165                 return retval;
2166
2167         ut_params->ibuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
2168
2169         /* clear mbuf payload */
2170         memset(rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *), 0,
2171                         rte_pktmbuf_tailroom(ut_params->ibuf));
2172
2173         /* Append data which is padded to a multiple */
2174         /* of the algorithms block size */
2175         plaintext_pad_len = RTE_ALIGN_CEIL((tdata->plaintext.len >> 3), 8);
2176
2177         plaintext = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
2178                         plaintext_pad_len);
2179         memcpy(plaintext, tdata->plaintext.data, tdata->plaintext.len >> 3);
2180
2181 #ifdef RTE_APP_TEST_DEBUG
2182         rte_hexdump(stdout, "plaintext:", plaintext, tdata->plaintext.len);
2183 #endif
2184
2185         /* Create SNOW3G operation */
2186         retval = create_snow3g_auth_cipher_operation(
2187                 tdata->digest.len,
2188                 tdata->iv.data, tdata->iv.len,
2189                 tdata->aad.data, tdata->aad.len,
2190                 plaintext_pad_len,
2191                 tdata->validCipherLenInBits.len,
2192                 tdata->validCipherOffsetLenInBits.len,
2193                 tdata->validAuthLenInBits.len,
2194                 tdata->validAuthOffsetLenInBits.len
2195         );
2196
2197         if (retval < 0)
2198                 return retval;
2199
2200         ut_params->op = process_crypto_request(ts_params->valid_devs[0],
2201                         ut_params->op);
2202         TEST_ASSERT_NOT_NULL(ut_params->op, "failed to retrieve obuf");
2203         ut_params->obuf = ut_params->op->sym->m_src;
2204         if (ut_params->obuf)
2205                 ciphertext = rte_pktmbuf_mtod(ut_params->obuf, uint8_t *)
2206                                 + tdata->aad.len + tdata->iv.len;
2207         else
2208                 ciphertext = plaintext;
2209
2210         lastByteValidBits = (tdata->validDataLenInBits.len % 8);
2211         if (lastByteValidBits == 0)
2212                 lastByteValidBits = 8;
2213         lastByteMask = lastByteMask << (8-lastByteValidBits);
2214         (*(ciphertext + (tdata->ciphertext.len >> 3) - 1)) &= lastByteMask;
2215         ut_params->digest = rte_pktmbuf_mtod(ut_params->obuf, uint8_t *)
2216                         + plaintext_pad_len + tdata->aad.len + tdata->iv.len;
2217         #ifdef RTE_APP_TEST_DEBUG
2218         rte_hexdump(stdout, "ciphertext:", ciphertext, tdata->ciphertext.len);
2219 #endif
2220         /* Validate obuf */
2221         TEST_ASSERT_BUFFERS_ARE_EQUAL(
2222                 ciphertext,
2223                 tdata->ciphertext.data,
2224                 tdata->ciphertext.len >> 3,
2225                 "Snow3G Ciphertext data not as expected");
2226
2227         /* Validate obuf */
2228         TEST_ASSERT_BUFFERS_ARE_EQUAL(
2229                 ut_params->digest,
2230                 tdata->digest.data,
2231                 DIGEST_BYTE_LENGTH_SNOW3G_UIA2,
2232                 "Snow3G Generated auth tag not as expected");
2233         return 0;
2234 }
2235
2236 static int
2237 test_snow3g_encryption_test_case_1(void)
2238 {
2239         return test_snow3g_encryption(&snow3g_test_case_1);
2240 }
2241
2242 static int
2243 test_snow3g_encryption_test_case_1_oop(void)
2244 {
2245         return test_snow3g_encryption_oop(&snow3g_test_case_1);
2246 }
2247
2248 static int
2249 test_snow3g_encryption_test_case_2(void)
2250 {
2251         return test_snow3g_encryption(&snow3g_test_case_2);
2252 }
2253
2254 static int
2255 test_snow3g_encryption_test_case_3(void)
2256 {
2257         return test_snow3g_encryption(&snow3g_test_case_3);
2258 }
2259
2260 static int
2261 test_snow3g_encryption_test_case_4(void)
2262 {
2263         return test_snow3g_encryption(&snow3g_test_case_4);
2264 }
2265
2266 static int
2267 test_snow3g_encryption_test_case_5(void)
2268 {
2269         return test_snow3g_encryption(&snow3g_test_case_5);
2270 }
2271
2272 static int
2273 test_snow3g_decryption_test_case_1(void)
2274 {
2275         return test_snow3g_decryption(&snow3g_test_case_1);
2276 }
2277
2278 static int
2279 test_snow3g_decryption_test_case_1_oop(void)
2280 {
2281         return test_snow3g_decryption_oop(&snow3g_test_case_1);
2282 }
2283
2284 static int
2285 test_snow3g_decryption_test_case_2(void)
2286 {
2287         return test_snow3g_decryption(&snow3g_test_case_2);
2288 }
2289
2290 static int
2291 test_snow3g_decryption_test_case_3(void)
2292 {
2293         return test_snow3g_decryption(&snow3g_test_case_3);
2294 }
2295
2296 static int
2297 test_snow3g_decryption_test_case_4(void)
2298 {
2299         return test_snow3g_decryption(&snow3g_test_case_4);
2300 }
2301
2302 static int
2303 test_snow3g_decryption_test_case_5(void)
2304 {
2305         return test_snow3g_decryption(&snow3g_test_case_5);
2306 }
2307 static int
2308 test_snow3g_authenticated_encryption_test_case_1(void)
2309 {
2310         return test_snow3g_authenticated_encryption(&snow3g_test_case_3);
2311 }
2312
2313 static int
2314 test_snow3g_encrypted_authentication_test_case_1(void)
2315 {
2316         return test_snow3g_encrypted_authentication(&snow3g_test_case_6);
2317 }
2318
2319 /* ***** AES-GCM Tests ***** */
2320
2321 static int
2322 create_gcm_session(uint8_t dev_id, enum rte_crypto_cipher_operation op,
2323                 const uint8_t *key, const uint8_t key_len,
2324                 const uint8_t aad_len, const uint8_t auth_len)
2325 {
2326         uint8_t cipher_key[key_len];
2327
2328         struct crypto_unittest_params *ut_params = &unittest_params;
2329
2330
2331         memcpy(cipher_key, key, key_len);
2332
2333         /* Setup Cipher Parameters */
2334         ut_params->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
2335         ut_params->cipher_xform.next = NULL;
2336
2337         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_GCM;
2338         ut_params->cipher_xform.cipher.op = op;
2339         ut_params->cipher_xform.cipher.key.data = cipher_key;
2340         ut_params->cipher_xform.cipher.key.length = key_len;
2341
2342 #ifdef RTE_APP_TEST_DEBUG
2343         rte_hexdump(stdout, "key:", key, key_len);
2344 #endif
2345         /* Setup Authentication Parameters */
2346         ut_params->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
2347         ut_params->auth_xform.next = NULL;
2348
2349         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_AES_GCM;
2350
2351         ut_params->auth_xform.auth.digest_length = auth_len;
2352         ut_params->auth_xform.auth.add_auth_data_length = aad_len;
2353         ut_params->auth_xform.auth.key.length = 0;
2354         ut_params->auth_xform.auth.key.data = NULL;
2355
2356         if (op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2357                 ut_params->cipher_xform.next = &ut_params->auth_xform;
2358
2359                 /* Create Crypto session*/
2360                 ut_params->sess = rte_cryptodev_sym_session_create(dev_id,
2361                                 &ut_params->cipher_xform);
2362         } else {/* Create Crypto session*/
2363                 ut_params->auth_xform.next = &ut_params->cipher_xform;
2364                 ut_params->sess = rte_cryptodev_sym_session_create(dev_id,
2365                                 &ut_params->auth_xform);
2366         }
2367
2368         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
2369
2370         return 0;
2371 }
2372
2373 static int
2374 create_gcm_operation(enum rte_crypto_cipher_operation op,
2375                 const uint8_t *auth_tag, const unsigned auth_tag_len,
2376                 const uint8_t *iv, const unsigned iv_len,
2377                 const uint8_t *aad, const unsigned aad_len,
2378                 const unsigned data_len, unsigned data_pad_len)
2379 {
2380         struct crypto_testsuite_params *ts_params = &testsuite_params;
2381         struct crypto_unittest_params *ut_params = &unittest_params;
2382
2383         unsigned iv_pad_len = 0, aad_buffer_len;
2384
2385         /* Generate Crypto op data structure */
2386         ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
2387                         RTE_CRYPTO_OP_TYPE_SYMMETRIC);
2388         TEST_ASSERT_NOT_NULL(ut_params->op,
2389                         "Failed to allocate symmetric crypto operation struct");
2390
2391         struct rte_crypto_sym_op *sym_op = ut_params->op->sym;
2392
2393
2394
2395         sym_op->auth.digest.data = (uint8_t *)rte_pktmbuf_append(
2396                         ut_params->ibuf, auth_tag_len);
2397         TEST_ASSERT_NOT_NULL(sym_op->auth.digest.data,
2398                         "no room to append digest");
2399         sym_op->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(
2400                         ut_params->ibuf, data_pad_len);
2401         sym_op->auth.digest.length = auth_tag_len;
2402
2403         if (op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2404                 rte_memcpy(sym_op->auth.digest.data, auth_tag, auth_tag_len);
2405 #ifdef RTE_APP_TEST_DEBUG
2406                 rte_hexdump(stdout, "digest:",
2407                                 sym_op->auth.digest.data,
2408                                 sym_op->auth.digest.length);
2409 #endif
2410         }
2411
2412         /* iv */
2413         iv_pad_len = RTE_ALIGN_CEIL(iv_len, 16);
2414
2415         sym_op->cipher.iv.data = (uint8_t *)rte_pktmbuf_prepend(
2416                         ut_params->ibuf, iv_pad_len);
2417         TEST_ASSERT_NOT_NULL(sym_op->cipher.iv.data, "no room to prepend iv");
2418
2419         memset(sym_op->cipher.iv.data, 0, iv_pad_len);
2420         sym_op->cipher.iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
2421         sym_op->cipher.iv.length = iv_pad_len;
2422
2423         rte_memcpy(sym_op->cipher.iv.data, iv, iv_len);
2424
2425         /* CalcY0 */
2426         if (iv_len != 16)
2427                 sym_op->cipher.iv.data[15] = 1;
2428
2429         /*
2430          * Always allocate the aad up to the block size.
2431          * The cryptodev API calls out -
2432          *  - the array must be big enough to hold the AAD, plus any
2433          *   space to round this up to the nearest multiple of the
2434          *   block size (16 bytes).
2435          */
2436         aad_buffer_len = ALIGN_POW2_ROUNDUP(aad_len, 16);
2437
2438         sym_op->auth.aad.data = (uint8_t *)rte_pktmbuf_prepend(
2439                         ut_params->ibuf, aad_buffer_len);
2440         TEST_ASSERT_NOT_NULL(sym_op->auth.aad.data,
2441                         "no room to prepend aad");
2442         sym_op->auth.aad.phys_addr = rte_pktmbuf_mtophys(
2443                         ut_params->ibuf);
2444         sym_op->auth.aad.length = aad_len;
2445
2446         memset(sym_op->auth.aad.data, 0, aad_buffer_len);
2447         rte_memcpy(sym_op->auth.aad.data, aad, aad_len);
2448
2449 #ifdef RTE_APP_TEST_DEBUG
2450         rte_hexdump(stdout, "iv:", sym_op->cipher.iv.data, iv_pad_len);
2451         rte_hexdump(stdout, "aad:",
2452                         sym_op->auth.aad.data, aad_len);
2453 #endif
2454         sym_op->cipher.data.length = data_len;
2455         sym_op->cipher.data.offset = aad_buffer_len + iv_pad_len;
2456
2457         sym_op->auth.data.offset = aad_buffer_len + iv_pad_len;
2458         sym_op->auth.data.length = data_len;
2459
2460         return 0;
2461 }
2462
2463 static int
2464 test_mb_AES_GCM_authenticated_encryption(const struct gcm_test_data *tdata)
2465 {
2466         struct crypto_testsuite_params *ts_params = &testsuite_params;
2467         struct crypto_unittest_params *ut_params = &unittest_params;
2468
2469         int retval;
2470
2471         uint8_t *plaintext, *ciphertext, *auth_tag;
2472         uint16_t plaintext_pad_len;
2473
2474         /* Create GCM session */
2475         retval = create_gcm_session(ts_params->valid_devs[0],
2476                         RTE_CRYPTO_CIPHER_OP_ENCRYPT,
2477                         tdata->key.data, tdata->key.len,
2478                         tdata->aad.len, tdata->auth_tag.len);
2479         if (retval < 0)
2480                 return retval;
2481
2482
2483         ut_params->ibuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
2484
2485         /* clear mbuf payload */
2486         memset(rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *), 0,
2487                         rte_pktmbuf_tailroom(ut_params->ibuf));
2488
2489         /*
2490          * Append data which is padded to a multiple
2491          * of the algorithms block size
2492          */
2493         plaintext_pad_len = RTE_ALIGN_CEIL(tdata->plaintext.len, 16);
2494
2495         plaintext = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
2496                         plaintext_pad_len);
2497         memcpy(plaintext, tdata->plaintext.data, tdata->plaintext.len);
2498
2499 #ifdef RTE_APP_TEST_DEBUG
2500         rte_hexdump(stdout, "plaintext:", plaintext, tdata->plaintext.len);
2501 #endif
2502         /* Create GCM opertaion */
2503         retval = create_gcm_operation(RTE_CRYPTO_CIPHER_OP_ENCRYPT,
2504                         tdata->auth_tag.data, tdata->auth_tag.len,
2505                         tdata->iv.data, tdata->iv.len,
2506                         tdata->aad.data, tdata->aad.len,
2507                         tdata->plaintext.len, plaintext_pad_len);
2508         if (retval < 0)
2509                 return retval;
2510
2511         rte_crypto_op_attach_sym_session(ut_params->op, ut_params->sess);
2512
2513         ut_params->op->sym->m_src = ut_params->ibuf;
2514
2515         /* Process crypto operation */
2516         TEST_ASSERT_NOT_NULL(process_crypto_request(ts_params->valid_devs[0],
2517                         ut_params->op), "failed to process sym crypto op");
2518
2519         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
2520                         "crypto op processing failed");
2521
2522         if (ut_params->op->sym->m_dst) {
2523                 ciphertext = rte_pktmbuf_mtod(ut_params->op->sym->m_dst,
2524                                 uint8_t *);
2525                 auth_tag = rte_pktmbuf_mtod_offset(ut_params->op->sym->m_dst,
2526                                 uint8_t *, plaintext_pad_len);
2527         } else {
2528                 ciphertext = plaintext;
2529                 auth_tag = plaintext + plaintext_pad_len;
2530         }
2531
2532 #ifdef RTE_APP_TEST_DEBUG
2533         rte_hexdump(stdout, "ciphertext:", ciphertext, tdata->ciphertext.len);
2534         rte_hexdump(stdout, "auth tag:", auth_tag, tdata->auth_tag.len);
2535 #endif
2536         /* Validate obuf */
2537         TEST_ASSERT_BUFFERS_ARE_EQUAL(
2538                         ciphertext,
2539                         tdata->ciphertext.data,
2540                         tdata->ciphertext.len,
2541                         "GCM Ciphertext data not as expected");
2542
2543         TEST_ASSERT_BUFFERS_ARE_EQUAL(
2544                         auth_tag,
2545                         tdata->auth_tag.data,
2546                         tdata->auth_tag.len,
2547                         "GCM Generated auth tag not as expected");
2548
2549         return 0;
2550
2551 }
2552
2553 static int
2554 test_mb_AES_GCM_authenticated_encryption_test_case_1(void)
2555 {
2556         return test_mb_AES_GCM_authenticated_encryption(&gcm_test_case_1);
2557 }
2558
2559 static int
2560 test_mb_AES_GCM_authenticated_encryption_test_case_2(void)
2561 {
2562         return test_mb_AES_GCM_authenticated_encryption(&gcm_test_case_2);
2563 }
2564
2565 static int
2566 test_mb_AES_GCM_authenticated_encryption_test_case_3(void)
2567 {
2568         return test_mb_AES_GCM_authenticated_encryption(&gcm_test_case_3);
2569 }
2570
2571 static int
2572 test_mb_AES_GCM_authenticated_encryption_test_case_4(void)
2573 {
2574         return test_mb_AES_GCM_authenticated_encryption(&gcm_test_case_4);
2575 }
2576
2577 static int
2578 test_mb_AES_GCM_authenticated_encryption_test_case_5(void)
2579 {
2580         return test_mb_AES_GCM_authenticated_encryption(&gcm_test_case_5);
2581 }
2582
2583 static int
2584 test_mb_AES_GCM_authenticated_encryption_test_case_6(void)
2585 {
2586         return test_mb_AES_GCM_authenticated_encryption(&gcm_test_case_6);
2587 }
2588
2589 static int
2590 test_mb_AES_GCM_authenticated_encryption_test_case_7(void)
2591 {
2592         return test_mb_AES_GCM_authenticated_encryption(&gcm_test_case_7);
2593 }
2594
2595 static int
2596 test_mb_AES_GCM_authenticated_decryption(const struct gcm_test_data *tdata)
2597 {
2598         struct crypto_testsuite_params *ts_params = &testsuite_params;
2599         struct crypto_unittest_params *ut_params = &unittest_params;
2600
2601         int retval;
2602
2603         uint8_t *plaintext, *ciphertext;
2604         uint16_t ciphertext_pad_len;
2605
2606         /* Create GCM session */
2607         retval = create_gcm_session(ts_params->valid_devs[0],
2608                         RTE_CRYPTO_CIPHER_OP_DECRYPT,
2609                         tdata->key.data, tdata->key.len,
2610                         tdata->aad.len, tdata->auth_tag.len);
2611         if (retval < 0)
2612                 return retval;
2613
2614
2615         /* alloc mbuf and set payload */
2616         ut_params->ibuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
2617
2618         memset(rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *), 0,
2619                         rte_pktmbuf_tailroom(ut_params->ibuf));
2620
2621         ciphertext_pad_len = RTE_ALIGN_CEIL(tdata->ciphertext.len, 16);
2622
2623         ciphertext = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
2624                         ciphertext_pad_len);
2625         memcpy(ciphertext, tdata->ciphertext.data, tdata->ciphertext.len);
2626
2627 #ifdef RTE_APP_TEST_DEBUG
2628         rte_hexdump(stdout, "ciphertext:", ciphertext, tdata->ciphertext.len);
2629 #endif
2630         /* Create GCM opertaion */
2631         retval = create_gcm_operation(RTE_CRYPTO_CIPHER_OP_DECRYPT,
2632                         tdata->auth_tag.data, tdata->auth_tag.len,
2633                         tdata->iv.data, tdata->iv.len,
2634                         tdata->aad.data, tdata->aad.len,
2635                         tdata->ciphertext.len, ciphertext_pad_len);
2636         if (retval < 0)
2637                 return retval;
2638
2639
2640         rte_crypto_op_attach_sym_session(ut_params->op, ut_params->sess);
2641
2642         ut_params->op->sym->m_src = ut_params->ibuf;
2643
2644         /* Process crypto operation */
2645         TEST_ASSERT_NOT_NULL(process_crypto_request(ts_params->valid_devs[0],
2646                         ut_params->op), "failed to process sym crypto op");
2647
2648         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
2649                         "crypto op processing failed");
2650
2651         if (ut_params->op->sym->m_dst)
2652                 plaintext = rte_pktmbuf_mtod(ut_params->op->sym->m_dst,
2653                                 uint8_t *);
2654         else
2655                 plaintext = ciphertext;
2656
2657 #ifdef RTE_APP_TEST_DEBUG
2658         rte_hexdump(stdout, "plaintext:", plaintext, tdata->ciphertext.len);
2659 #endif
2660         /* Validate obuf */
2661         TEST_ASSERT_BUFFERS_ARE_EQUAL(
2662                         plaintext,
2663                         tdata->plaintext.data,
2664                         tdata->plaintext.len,
2665                         "GCM plaintext data not as expected");
2666
2667         TEST_ASSERT_EQUAL(ut_params->op->status,
2668                         RTE_CRYPTO_OP_STATUS_SUCCESS,
2669                         "GCM authentication failed");
2670         return 0;
2671 }
2672
2673 static int
2674 test_mb_AES_GCM_authenticated_decryption_test_case_1(void)
2675 {
2676         return test_mb_AES_GCM_authenticated_decryption(&gcm_test_case_1);
2677 }
2678
2679 static int
2680 test_mb_AES_GCM_authenticated_decryption_test_case_2(void)
2681 {
2682         return test_mb_AES_GCM_authenticated_decryption(&gcm_test_case_2);
2683 }
2684
2685 static int
2686 test_mb_AES_GCM_authenticated_decryption_test_case_3(void)
2687 {
2688         return test_mb_AES_GCM_authenticated_decryption(&gcm_test_case_3);
2689 }
2690
2691 static int
2692 test_mb_AES_GCM_authenticated_decryption_test_case_4(void)
2693 {
2694         return test_mb_AES_GCM_authenticated_decryption(&gcm_test_case_4);
2695 }
2696
2697 static int
2698 test_mb_AES_GCM_authenticated_decryption_test_case_5(void)
2699 {
2700         return test_mb_AES_GCM_authenticated_decryption(&gcm_test_case_5);
2701 }
2702
2703 static int
2704 test_mb_AES_GCM_authenticated_decryption_test_case_6(void)
2705 {
2706         return test_mb_AES_GCM_authenticated_decryption(&gcm_test_case_6);
2707 }
2708
2709 static int
2710 test_mb_AES_GCM_authenticated_decryption_test_case_7(void)
2711 {
2712         return test_mb_AES_GCM_authenticated_decryption(&gcm_test_case_7);
2713 }
2714
2715 static int
2716 test_stats(void)
2717 {
2718         struct crypto_testsuite_params *ts_params = &testsuite_params;
2719         struct rte_cryptodev_stats stats;
2720         struct rte_cryptodev *dev;
2721         cryptodev_stats_get_t temp_pfn;
2722
2723         rte_cryptodev_stats_reset(ts_params->valid_devs[0]);
2724         TEST_ASSERT((rte_cryptodev_stats_get(ts_params->valid_devs[0] + 600,
2725                         &stats) == -ENODEV),
2726                 "rte_cryptodev_stats_get invalid dev failed");
2727         TEST_ASSERT((rte_cryptodev_stats_get(ts_params->valid_devs[0], 0) != 0),
2728                 "rte_cryptodev_stats_get invalid Param failed");
2729         dev = &rte_cryptodevs[ts_params->valid_devs[0]];
2730         temp_pfn = dev->dev_ops->stats_get;
2731         dev->dev_ops->stats_get = (cryptodev_stats_get_t)0;
2732         TEST_ASSERT((rte_cryptodev_stats_get(ts_params->valid_devs[0], &stats)
2733                         == -ENOTSUP),
2734                 "rte_cryptodev_stats_get invalid Param failed");
2735         dev->dev_ops->stats_get = temp_pfn;
2736
2737         /* Test expected values */
2738         ut_setup();
2739         test_AES_CBC_HMAC_SHA1_encrypt_digest();
2740         ut_teardown();
2741         TEST_ASSERT_SUCCESS(rte_cryptodev_stats_get(ts_params->valid_devs[0],
2742                         &stats),
2743                 "rte_cryptodev_stats_get failed");
2744         TEST_ASSERT((stats.enqueued_count == 1),
2745                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
2746         TEST_ASSERT((stats.dequeued_count == 1),
2747                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
2748         TEST_ASSERT((stats.enqueue_err_count == 0),
2749                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
2750         TEST_ASSERT((stats.dequeue_err_count == 0),
2751                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
2752
2753         /* invalid device but should ignore and not reset device stats*/
2754         rte_cryptodev_stats_reset(ts_params->valid_devs[0] + 300);
2755         TEST_ASSERT_SUCCESS(rte_cryptodev_stats_get(ts_params->valid_devs[0],
2756                         &stats),
2757                 "rte_cryptodev_stats_get failed");
2758         TEST_ASSERT((stats.enqueued_count == 1),
2759                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
2760
2761         /* check that a valid reset clears stats */
2762         rte_cryptodev_stats_reset(ts_params->valid_devs[0]);
2763         TEST_ASSERT_SUCCESS(rte_cryptodev_stats_get(ts_params->valid_devs[0],
2764                         &stats),
2765                                           "rte_cryptodev_stats_get failed");
2766         TEST_ASSERT((stats.enqueued_count == 0),
2767                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
2768         TEST_ASSERT((stats.dequeued_count == 0),
2769                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
2770
2771         return TEST_SUCCESS;
2772 }
2773
2774
2775 static int
2776 test_multi_session(void)
2777 {
2778         struct crypto_testsuite_params *ts_params = &testsuite_params;
2779         struct crypto_unittest_params *ut_params = &unittest_params;
2780
2781         struct rte_cryptodev_info dev_info;
2782         struct rte_cryptodev_sym_session **sessions;
2783
2784         uint16_t i;
2785
2786         test_AES_CBC_HMAC_SHA512_decrypt_create_session_params(ut_params);
2787
2788
2789         rte_cryptodev_info_get(ts_params->valid_devs[0], &dev_info);
2790
2791         sessions = rte_malloc(NULL,
2792                         (sizeof(struct rte_cryptodev_sym_session *) *
2793                         dev_info.sym.max_nb_sessions) + 1, 0);
2794
2795         /* Create multiple crypto sessions*/
2796         for (i = 0; i < dev_info.sym.max_nb_sessions; i++) {
2797                 sessions[i] = rte_cryptodev_sym_session_create(
2798                                 ts_params->valid_devs[0],
2799                         &ut_params->auth_xform);
2800                 TEST_ASSERT_NOT_NULL(sessions[i],
2801                                 "Session creation failed at session number %u",
2802                                 i);
2803
2804                 /* Attempt to send a request on each session */
2805                 TEST_ASSERT_SUCCESS(test_AES_CBC_HMAC_SHA512_decrypt_perform(
2806                                 sessions[i], ut_params, ts_params),
2807                                 "Failed to perform decrypt on request "
2808                                 "number %u.", i);
2809                 /* free crypto operation structure */
2810                 if (ut_params->op)
2811                         rte_crypto_op_free(ut_params->op);
2812
2813                 /*
2814                  * free mbuf - both obuf and ibuf are usually the same,
2815                  * but rte copes even if we call free twice
2816                  */
2817                 if (ut_params->obuf) {
2818                         rte_pktmbuf_free(ut_params->obuf);
2819                         ut_params->obuf = 0;
2820                 }
2821         }
2822
2823         /* Next session create should fail */
2824         sessions[i] = rte_cryptodev_sym_session_create(ts_params->valid_devs[0],
2825                         &ut_params->auth_xform);
2826         TEST_ASSERT_NULL(sessions[i],
2827                         "Session creation succeeded unexpectedly!");
2828
2829         for (i = 0; i < dev_info.sym.max_nb_sessions; i++)
2830                 rte_cryptodev_sym_session_free(ts_params->valid_devs[0],
2831                                 sessions[i]);
2832
2833         rte_free(sessions);
2834
2835         return TEST_SUCCESS;
2836 }
2837
2838 static int
2839 test_null_cipher_only_operation(void)
2840 {
2841         struct crypto_testsuite_params *ts_params = &testsuite_params;
2842         struct crypto_unittest_params *ut_params = &unittest_params;
2843
2844         /* Generate test mbuf data and space for digest */
2845         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
2846                         catch_22_quote, QUOTE_512_BYTES, 0);
2847
2848         /* Setup Cipher Parameters */
2849         ut_params->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
2850         ut_params->cipher_xform.next = NULL;
2851
2852         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_NULL;
2853         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
2854
2855         /* Create Crypto session*/
2856         ut_params->sess = rte_cryptodev_sym_session_create(
2857                         ts_params->valid_devs[0], &ut_params->cipher_xform);
2858         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
2859
2860         /* Generate Crypto op data structure */
2861         ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
2862                         RTE_CRYPTO_OP_TYPE_SYMMETRIC);
2863         TEST_ASSERT_NOT_NULL(ut_params->op,
2864                         "Failed to allocate symmetric crypto operation struct");
2865
2866         /* Set crypto operation data parameters */
2867         rte_crypto_op_attach_sym_session(ut_params->op, ut_params->sess);
2868
2869         struct rte_crypto_sym_op *sym_op = ut_params->op->sym;
2870
2871         /* set crypto operation source mbuf */
2872         sym_op->m_src = ut_params->ibuf;
2873
2874         sym_op->cipher.data.offset = 0;
2875         sym_op->cipher.data.length = QUOTE_512_BYTES;
2876
2877         /* Process crypto operation */
2878         ut_params->op = process_crypto_request(ts_params->valid_devs[0],
2879                         ut_params->op);
2880         TEST_ASSERT_NOT_NULL(ut_params->op, "no crypto operation returned");
2881
2882         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
2883                         "crypto operation processing failed");
2884
2885         /* Validate obuf */
2886         TEST_ASSERT_BUFFERS_ARE_EQUAL(
2887                         rte_pktmbuf_mtod(ut_params->op->sym->m_src, uint8_t *),
2888                         catch_22_quote,
2889                         QUOTE_512_BYTES,
2890                         "Ciphertext data not as expected");
2891
2892         return TEST_SUCCESS;
2893 }
2894
2895 static int
2896 test_null_auth_only_operation(void)
2897 {
2898         struct crypto_testsuite_params *ts_params = &testsuite_params;
2899         struct crypto_unittest_params *ut_params = &unittest_params;
2900
2901         /* Generate test mbuf data and space for digest */
2902         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
2903                         catch_22_quote, QUOTE_512_BYTES, 0);
2904
2905         /* Setup HMAC Parameters */
2906         ut_params->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
2907         ut_params->auth_xform.next = NULL;
2908
2909         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_NULL;
2910         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
2911
2912         /* Create Crypto session*/
2913         ut_params->sess = rte_cryptodev_sym_session_create(
2914                         ts_params->valid_devs[0], &ut_params->auth_xform);
2915         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
2916
2917         /* Generate Crypto op data structure */
2918         ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
2919                         RTE_CRYPTO_OP_TYPE_SYMMETRIC);
2920         TEST_ASSERT_NOT_NULL(ut_params->op,
2921                         "Failed to allocate symmetric crypto operation struct");
2922
2923         /* Set crypto operation data parameters */
2924         rte_crypto_op_attach_sym_session(ut_params->op, ut_params->sess);
2925
2926         struct rte_crypto_sym_op *sym_op = ut_params->op->sym;
2927
2928         sym_op->m_src = ut_params->ibuf;
2929
2930         sym_op->auth.data.offset = 0;
2931         sym_op->auth.data.length = QUOTE_512_BYTES;
2932
2933         /* Process crypto operation */
2934         ut_params->op = process_crypto_request(ts_params->valid_devs[0],
2935                         ut_params->op);
2936         TEST_ASSERT_NOT_NULL(ut_params->op, "no crypto operation returned");
2937
2938         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
2939                         "crypto operation processing failed");
2940
2941         return TEST_SUCCESS;
2942 }
2943
2944 static int
2945 test_null_cipher_auth_operation(void)
2946 {
2947         struct crypto_testsuite_params *ts_params = &testsuite_params;
2948         struct crypto_unittest_params *ut_params = &unittest_params;
2949
2950         /* Generate test mbuf data and space for digest */
2951         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
2952                         catch_22_quote, QUOTE_512_BYTES, 0);
2953
2954         /* Setup Cipher Parameters */
2955         ut_params->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
2956         ut_params->cipher_xform.next = &ut_params->auth_xform;
2957
2958         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_NULL;
2959         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
2960
2961         /* Setup HMAC Parameters */
2962         ut_params->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
2963         ut_params->auth_xform.next = NULL;
2964
2965         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_NULL;
2966         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
2967
2968         /* Create Crypto session*/
2969         ut_params->sess = rte_cryptodev_sym_session_create(
2970                         ts_params->valid_devs[0], &ut_params->cipher_xform);
2971         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
2972
2973         /* Generate Crypto op data structure */
2974         ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
2975                         RTE_CRYPTO_OP_TYPE_SYMMETRIC);
2976         TEST_ASSERT_NOT_NULL(ut_params->op,
2977                         "Failed to allocate symmetric crypto operation struct");
2978
2979         /* Set crypto operation data parameters */
2980         rte_crypto_op_attach_sym_session(ut_params->op, ut_params->sess);
2981
2982         struct rte_crypto_sym_op *sym_op = ut_params->op->sym;
2983
2984         sym_op->m_src = ut_params->ibuf;
2985
2986         sym_op->cipher.data.offset = 0;
2987         sym_op->cipher.data.length = QUOTE_512_BYTES;
2988
2989         sym_op->auth.data.offset = 0;
2990         sym_op->auth.data.length = QUOTE_512_BYTES;
2991
2992         /* Process crypto operation */
2993         ut_params->op = process_crypto_request(ts_params->valid_devs[0],
2994                         ut_params->op);
2995         TEST_ASSERT_NOT_NULL(ut_params->op, "no crypto operation returned");
2996
2997         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
2998                         "crypto operation processing failed");
2999
3000         /* Validate obuf */
3001         TEST_ASSERT_BUFFERS_ARE_EQUAL(
3002                         rte_pktmbuf_mtod(ut_params->op->sym->m_src, uint8_t *),
3003                         catch_22_quote,
3004                         QUOTE_512_BYTES,
3005                         "Ciphertext data not as expected");
3006
3007         return TEST_SUCCESS;
3008 }
3009
3010 static int
3011 test_null_auth_cipher_operation(void)
3012 {
3013         struct crypto_testsuite_params *ts_params = &testsuite_params;
3014         struct crypto_unittest_params *ut_params = &unittest_params;
3015
3016         /* Generate test mbuf data and space for digest */
3017         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
3018                         catch_22_quote, QUOTE_512_BYTES, 0);
3019
3020         /* Setup Cipher Parameters */
3021         ut_params->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
3022         ut_params->cipher_xform.next = NULL;
3023
3024         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_NULL;
3025         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
3026
3027         /* Setup HMAC Parameters */
3028         ut_params->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
3029         ut_params->auth_xform.next = &ut_params->cipher_xform;
3030
3031         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_NULL;
3032         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
3033
3034         /* Create Crypto session*/
3035         ut_params->sess = rte_cryptodev_sym_session_create(
3036                         ts_params->valid_devs[0], &ut_params->cipher_xform);
3037         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
3038
3039         /* Generate Crypto op data structure */
3040         ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
3041                         RTE_CRYPTO_OP_TYPE_SYMMETRIC);
3042         TEST_ASSERT_NOT_NULL(ut_params->op,
3043                         "Failed to allocate symmetric crypto operation struct");
3044
3045         /* Set crypto operation data parameters */
3046         rte_crypto_op_attach_sym_session(ut_params->op, ut_params->sess);
3047
3048         struct rte_crypto_sym_op *sym_op = ut_params->op->sym;
3049
3050         sym_op->m_src = ut_params->ibuf;
3051
3052         sym_op->cipher.data.offset = 0;
3053         sym_op->cipher.data.length = QUOTE_512_BYTES;
3054
3055         sym_op->auth.data.offset = 0;
3056         sym_op->auth.data.length = QUOTE_512_BYTES;
3057
3058         /* Process crypto operation */
3059         ut_params->op = process_crypto_request(ts_params->valid_devs[0],
3060                         ut_params->op);
3061         TEST_ASSERT_NOT_NULL(ut_params->op, "no crypto operation returned");
3062
3063         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
3064                         "crypto operation processing failed");
3065
3066         /* Validate obuf */
3067         TEST_ASSERT_BUFFERS_ARE_EQUAL(
3068                         rte_pktmbuf_mtod(ut_params->op->sym->m_src, uint8_t *),
3069                         catch_22_quote,
3070                         QUOTE_512_BYTES,
3071                         "Ciphertext data not as expected");
3072
3073         return TEST_SUCCESS;
3074 }
3075
3076
3077 static int
3078 test_null_invalid_operation(void)
3079 {
3080         struct crypto_testsuite_params *ts_params = &testsuite_params;
3081         struct crypto_unittest_params *ut_params = &unittest_params;
3082
3083         /* Setup Cipher Parameters */
3084         ut_params->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
3085         ut_params->cipher_xform.next = NULL;
3086
3087         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
3088         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
3089
3090         /* Create Crypto session*/
3091         ut_params->sess = rte_cryptodev_sym_session_create(
3092                         ts_params->valid_devs[0], &ut_params->cipher_xform);
3093         TEST_ASSERT_NULL(ut_params->sess,
3094                         "Session creation succeeded unexpectedly");
3095
3096
3097         /* Setup HMAC Parameters */
3098         ut_params->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
3099         ut_params->auth_xform.next = NULL;
3100
3101         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
3102         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
3103
3104         /* Create Crypto session*/
3105         ut_params->sess = rte_cryptodev_sym_session_create(
3106                         ts_params->valid_devs[0], &ut_params->auth_xform);
3107         TEST_ASSERT_NULL(ut_params->sess,
3108                         "Session creation succeeded unexpectedly");
3109
3110         return TEST_SUCCESS;
3111 }
3112
3113
3114 #define NULL_BURST_LENGTH (32)
3115
3116 static int
3117 test_null_burst_operation(void)
3118 {
3119         struct crypto_testsuite_params *ts_params = &testsuite_params;
3120         struct crypto_unittest_params *ut_params = &unittest_params;
3121
3122         unsigned i, burst_len = NULL_BURST_LENGTH;
3123
3124         struct rte_crypto_op *burst[NULL_BURST_LENGTH] = { NULL };
3125         struct rte_crypto_op *burst_dequeued[NULL_BURST_LENGTH] = { NULL };
3126
3127         /* Setup Cipher Parameters */
3128         ut_params->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
3129         ut_params->cipher_xform.next = &ut_params->auth_xform;
3130
3131         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_NULL;
3132         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
3133
3134         /* Setup HMAC Parameters */
3135         ut_params->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
3136         ut_params->auth_xform.next = NULL;
3137
3138         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_NULL;
3139         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
3140
3141         /* Create Crypto session*/
3142         ut_params->sess = rte_cryptodev_sym_session_create(
3143                         ts_params->valid_devs[0], &ut_params->cipher_xform);
3144         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
3145
3146         TEST_ASSERT_EQUAL(rte_crypto_op_bulk_alloc(ts_params->op_mpool,
3147                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, burst, burst_len),
3148                         burst_len, "failed to generate burst of crypto ops");
3149
3150         /* Generate an operation for each mbuf in burst */
3151         for (i = 0; i < burst_len; i++) {
3152                 struct rte_mbuf *m = rte_pktmbuf_alloc(ts_params->mbuf_pool);
3153
3154                 TEST_ASSERT_NOT_NULL(m, "Failed to allocate mbuf");
3155
3156                 unsigned *data = (unsigned *)rte_pktmbuf_append(m,
3157                                 sizeof(unsigned));
3158                 *data = i;
3159
3160                 rte_crypto_op_attach_sym_session(burst[i], ut_params->sess);
3161
3162                 burst[i]->sym->m_src = m;
3163         }
3164
3165         /* Process crypto operation */
3166         TEST_ASSERT_EQUAL(rte_cryptodev_enqueue_burst(ts_params->valid_devs[0],
3167                         0, burst, burst_len),
3168                         burst_len,
3169                         "Error enqueuing burst");
3170
3171         TEST_ASSERT_EQUAL(rte_cryptodev_dequeue_burst(ts_params->valid_devs[0],
3172                         0, burst_dequeued, burst_len),
3173                         burst_len,
3174                         "Error dequeuing burst");
3175
3176
3177         for (i = 0; i < burst_len; i++) {
3178                 TEST_ASSERT_EQUAL(
3179                         *rte_pktmbuf_mtod(burst[i]->sym->m_src, uint32_t *),
3180                         *rte_pktmbuf_mtod(burst_dequeued[i]->sym->m_src,
3181                                         uint32_t *),
3182                         "data not as expected");
3183
3184                 rte_pktmbuf_free(burst[i]->sym->m_src);
3185                 rte_crypto_op_free(burst[i]);
3186         }
3187
3188         return TEST_SUCCESS;
3189 }
3190
3191
3192
3193
3194 static struct unit_test_suite cryptodev_qat_testsuite  = {
3195         .suite_name = "Crypto QAT Unit Test Suite",
3196         .setup = testsuite_setup,
3197         .teardown = testsuite_teardown,
3198         .unit_test_cases = {
3199                 TEST_CASE_ST(ut_setup, ut_teardown,
3200                                 test_device_configure_invalid_dev_id),
3201                 TEST_CASE_ST(ut_setup, ut_teardown,
3202                                 test_device_configure_invalid_queue_pair_ids),
3203                 TEST_CASE_ST(ut_setup, ut_teardown,
3204                                 test_queue_pair_descriptor_setup),
3205                 TEST_CASE_ST(ut_setup, ut_teardown,
3206                                 test_multi_session),
3207
3208                 TEST_CASE_ST(ut_setup, ut_teardown, test_AES_qat_all),
3209                 TEST_CASE_ST(ut_setup, ut_teardown, test_stats),
3210
3211                 /** AES GCM Authenticated Encryption */
3212                 TEST_CASE_ST(ut_setup, ut_teardown,
3213                         test_mb_AES_GCM_authenticated_encryption_test_case_1),
3214                 TEST_CASE_ST(ut_setup, ut_teardown,
3215                         test_mb_AES_GCM_authenticated_encryption_test_case_2),
3216                 TEST_CASE_ST(ut_setup, ut_teardown,
3217                         test_mb_AES_GCM_authenticated_encryption_test_case_3),
3218                 TEST_CASE_ST(ut_setup, ut_teardown,
3219                         test_mb_AES_GCM_authenticated_encryption_test_case_4),
3220                 TEST_CASE_ST(ut_setup, ut_teardown,
3221                         test_mb_AES_GCM_authenticated_encryption_test_case_5),
3222                 TEST_CASE_ST(ut_setup, ut_teardown,
3223                         test_mb_AES_GCM_authenticated_encryption_test_case_6),
3224                 TEST_CASE_ST(ut_setup, ut_teardown,
3225                         test_mb_AES_GCM_authenticated_encryption_test_case_7),
3226
3227                 /** AES GCM Authenticated Decryption */
3228                 TEST_CASE_ST(ut_setup, ut_teardown,
3229                         test_mb_AES_GCM_authenticated_decryption_test_case_1),
3230                 TEST_CASE_ST(ut_setup, ut_teardown,
3231                         test_mb_AES_GCM_authenticated_decryption_test_case_2),
3232                 TEST_CASE_ST(ut_setup, ut_teardown,
3233                         test_mb_AES_GCM_authenticated_decryption_test_case_3),
3234                 TEST_CASE_ST(ut_setup, ut_teardown,
3235                         test_mb_AES_GCM_authenticated_decryption_test_case_4),
3236                 TEST_CASE_ST(ut_setup, ut_teardown,
3237                         test_mb_AES_GCM_authenticated_decryption_test_case_5),
3238                 TEST_CASE_ST(ut_setup, ut_teardown,
3239                         test_mb_AES_GCM_authenticated_decryption_test_case_6),
3240                 TEST_CASE_ST(ut_setup, ut_teardown,
3241                         test_mb_AES_GCM_authenticated_decryption_test_case_7),
3242
3243                 /** Snow3G encrypt only (UEA2) */
3244                 TEST_CASE_ST(ut_setup, ut_teardown,
3245                         test_snow3g_encryption_test_case_1),
3246                 TEST_CASE_ST(ut_setup, ut_teardown,
3247                         test_snow3g_encryption_test_case_2),
3248                 TEST_CASE_ST(ut_setup, ut_teardown,
3249                         test_snow3g_encryption_test_case_3),
3250                 TEST_CASE_ST(ut_setup, ut_teardown,
3251                         test_snow3g_encryption_test_case_4),
3252                 TEST_CASE_ST(ut_setup, ut_teardown,
3253                         test_snow3g_encryption_test_case_5),
3254
3255                 TEST_CASE_ST(ut_setup, ut_teardown,
3256                         test_snow3g_encryption_test_case_1_oop),
3257                 TEST_CASE_ST(ut_setup, ut_teardown,
3258                         test_snow3g_decryption_test_case_1_oop),
3259
3260                 /** Snow3G decrypt only (UEA2) */
3261                 TEST_CASE_ST(ut_setup, ut_teardown,
3262                         test_snow3g_decryption_test_case_1),
3263                 TEST_CASE_ST(ut_setup, ut_teardown,
3264                         test_snow3g_decryption_test_case_2),
3265                 TEST_CASE_ST(ut_setup, ut_teardown,
3266                         test_snow3g_decryption_test_case_3),
3267                 TEST_CASE_ST(ut_setup, ut_teardown,
3268                         test_snow3g_decryption_test_case_4),
3269                 TEST_CASE_ST(ut_setup, ut_teardown,
3270                         test_snow3g_decryption_test_case_5),
3271                 TEST_CASE_ST(ut_setup, ut_teardown,
3272                         test_snow3g_hash_generate_test_case_1),
3273                 TEST_CASE_ST(ut_setup, ut_teardown,
3274                         test_snow3g_hash_generate_test_case_2),
3275                 TEST_CASE_ST(ut_setup, ut_teardown,
3276                         test_snow3g_hash_generate_test_case_3),
3277                 TEST_CASE_ST(ut_setup, ut_teardown,
3278                         test_snow3g_hash_verify_test_case_1),
3279                 TEST_CASE_ST(ut_setup, ut_teardown,
3280                         test_snow3g_hash_verify_test_case_2),
3281                 TEST_CASE_ST(ut_setup, ut_teardown,
3282                         test_snow3g_hash_verify_test_case_3),
3283                 TEST_CASE_ST(ut_setup, ut_teardown,
3284                         test_snow3g_authenticated_encryption_test_case_1),
3285                 TEST_CASE_ST(ut_setup, ut_teardown,
3286                         test_snow3g_encrypted_authentication_test_case_1),
3287                 TEST_CASES_END() /**< NULL terminate unit test array */
3288         }
3289 };
3290
3291 static struct unit_test_suite cryptodev_aesni_mb_testsuite  = {
3292         .suite_name = "Crypto Device AESNI MB Unit Test Suite",
3293         .setup = testsuite_setup,
3294         .teardown = testsuite_teardown,
3295         .unit_test_cases = {
3296                 TEST_CASE_ST(ut_setup, ut_teardown, test_AES_mb_all),
3297
3298                 TEST_CASES_END() /**< NULL terminate unit test array */
3299         }
3300 };
3301
3302 static struct unit_test_suite cryptodev_aesni_gcm_testsuite  = {
3303         .suite_name = "Crypto Device AESNI GCM Unit Test Suite",
3304         .setup = testsuite_setup,
3305         .teardown = testsuite_teardown,
3306         .unit_test_cases = {
3307                 /** AES GCM Authenticated Encryption */
3308                 TEST_CASE_ST(ut_setup, ut_teardown,
3309                         test_mb_AES_GCM_authenticated_encryption_test_case_1),
3310                 TEST_CASE_ST(ut_setup, ut_teardown,
3311                         test_mb_AES_GCM_authenticated_encryption_test_case_2),
3312                 TEST_CASE_ST(ut_setup, ut_teardown,
3313                         test_mb_AES_GCM_authenticated_encryption_test_case_3),
3314                 TEST_CASE_ST(ut_setup, ut_teardown,
3315                         test_mb_AES_GCM_authenticated_encryption_test_case_4),
3316                 TEST_CASE_ST(ut_setup, ut_teardown,
3317                         test_mb_AES_GCM_authenticated_encryption_test_case_5),
3318                 TEST_CASE_ST(ut_setup, ut_teardown,
3319                         test_mb_AES_GCM_authenticated_encryption_test_case_6),
3320                 TEST_CASE_ST(ut_setup, ut_teardown,
3321                         test_mb_AES_GCM_authenticated_encryption_test_case_7),
3322
3323                 /** AES GCM Authenticated Decryption */
3324                 TEST_CASE_ST(ut_setup, ut_teardown,
3325                         test_mb_AES_GCM_authenticated_decryption_test_case_1),
3326                 TEST_CASE_ST(ut_setup, ut_teardown,
3327                         test_mb_AES_GCM_authenticated_decryption_test_case_2),
3328                 TEST_CASE_ST(ut_setup, ut_teardown,
3329                         test_mb_AES_GCM_authenticated_decryption_test_case_3),
3330                 TEST_CASE_ST(ut_setup, ut_teardown,
3331                         test_mb_AES_GCM_authenticated_decryption_test_case_4),
3332                 TEST_CASE_ST(ut_setup, ut_teardown,
3333                         test_mb_AES_GCM_authenticated_decryption_test_case_5),
3334                 TEST_CASE_ST(ut_setup, ut_teardown,
3335                         test_mb_AES_GCM_authenticated_decryption_test_case_6),
3336                 TEST_CASE_ST(ut_setup, ut_teardown,
3337                         test_mb_AES_GCM_authenticated_decryption_test_case_7),
3338
3339                 TEST_CASES_END() /**< NULL terminate unit test array */
3340         }
3341 };
3342
3343 static struct unit_test_suite cryptodev_sw_snow3g_testsuite  = {
3344         .suite_name = "Crypto Device SW Snow3G Unit Test Suite",
3345         .setup = testsuite_setup,
3346         .teardown = testsuite_teardown,
3347         .unit_test_cases = {
3348                 /** Snow3G encrypt only (UEA2) */
3349                 TEST_CASE_ST(ut_setup, ut_teardown,
3350                         test_snow3g_encryption_test_case_1),
3351                 TEST_CASE_ST(ut_setup, ut_teardown,
3352                         test_snow3g_encryption_test_case_2),
3353                 TEST_CASE_ST(ut_setup, ut_teardown,
3354                         test_snow3g_encryption_test_case_3),
3355                 TEST_CASE_ST(ut_setup, ut_teardown,
3356                         test_snow3g_encryption_test_case_4),
3357                 TEST_CASE_ST(ut_setup, ut_teardown,
3358                         test_snow3g_encryption_test_case_5),
3359
3360
3361                 /** Snow3G decrypt only (UEA2) */
3362                 TEST_CASE_ST(ut_setup, ut_teardown,
3363                         test_snow3g_decryption_test_case_1),
3364                 TEST_CASE_ST(ut_setup, ut_teardown,
3365                         test_snow3g_decryption_test_case_2),
3366                 TEST_CASE_ST(ut_setup, ut_teardown,
3367                         test_snow3g_decryption_test_case_3),
3368                 TEST_CASE_ST(ut_setup, ut_teardown,
3369                         test_snow3g_decryption_test_case_4),
3370                 TEST_CASE_ST(ut_setup, ut_teardown,
3371                         test_snow3g_decryption_test_case_5),
3372                 TEST_CASE_ST(ut_setup, ut_teardown,
3373                         test_snow3g_hash_generate_test_case_1),
3374                 TEST_CASE_ST(ut_setup, ut_teardown,
3375                         test_snow3g_hash_generate_test_case_2),
3376                 TEST_CASE_ST(ut_setup, ut_teardown,
3377                         test_snow3g_hash_generate_test_case_3),
3378                 TEST_CASE_ST(ut_setup, ut_teardown,
3379                         test_snow3g_hash_verify_test_case_1),
3380                 TEST_CASE_ST(ut_setup, ut_teardown,
3381                         test_snow3g_hash_verify_test_case_2),
3382                 TEST_CASE_ST(ut_setup, ut_teardown,
3383                         test_snow3g_hash_verify_test_case_3),
3384                 TEST_CASE_ST(ut_setup, ut_teardown,
3385                         test_snow3g_authenticated_encryption_test_case_1),
3386                 TEST_CASE_ST(ut_setup, ut_teardown,
3387                         test_snow3g_encrypted_authentication_test_case_1),
3388
3389                 TEST_CASES_END() /**< NULL terminate unit test array */
3390         }
3391 };
3392
3393 static struct unit_test_suite cryptodev_null_testsuite  = {
3394         .suite_name = "Crypto Device NULL Unit Test Suite",
3395         .setup = testsuite_setup,
3396         .teardown = testsuite_teardown,
3397         .unit_test_cases = {
3398                 TEST_CASE_ST(ut_setup, ut_teardown,
3399                         test_null_auth_only_operation),
3400                 TEST_CASE_ST(ut_setup, ut_teardown,
3401                         test_null_cipher_only_operation),
3402                 TEST_CASE_ST(ut_setup, ut_teardown,
3403                         test_null_cipher_auth_operation),
3404                 TEST_CASE_ST(ut_setup, ut_teardown,
3405                         test_null_auth_cipher_operation),
3406                 TEST_CASE_ST(ut_setup, ut_teardown,
3407                         test_null_invalid_operation),
3408                 TEST_CASE_ST(ut_setup, ut_teardown,
3409                         test_null_burst_operation),
3410
3411                 TEST_CASES_END() /**< NULL terminate unit test array */
3412         }
3413 };
3414
3415 static int
3416 test_cryptodev_qat(void /*argv __rte_unused, int argc __rte_unused*/)
3417 {
3418         gbl_cryptodev_type = RTE_CRYPTODEV_QAT_SYM_PMD;
3419         return unit_test_suite_runner(&cryptodev_qat_testsuite);
3420 }
3421 static struct test_command cryptodev_qat_cmd = {
3422         .command = "cryptodev_qat_autotest",
3423         .callback = test_cryptodev_qat,
3424 };
3425
3426 static int
3427 test_cryptodev_aesni_mb(void /*argv __rte_unused, int argc __rte_unused*/)
3428 {
3429         gbl_cryptodev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
3430
3431         return unit_test_suite_runner(&cryptodev_aesni_mb_testsuite);
3432 }
3433
3434 static struct test_command cryptodev_aesni_mb_cmd = {
3435         .command = "cryptodev_aesni_mb_autotest",
3436         .callback = test_cryptodev_aesni_mb,
3437 };
3438
3439 static int
3440 test_cryptodev_aesni_gcm(void)
3441 {
3442         gbl_cryptodev_type = RTE_CRYPTODEV_AESNI_GCM_PMD;
3443
3444         return unit_test_suite_runner(&cryptodev_aesni_gcm_testsuite);
3445 }
3446
3447 static struct test_command cryptodev_aesni_gcm_cmd = {
3448         .command = "cryptodev_aesni_gcm_autotest",
3449         .callback = test_cryptodev_aesni_gcm,
3450 };
3451
3452 static int
3453 test_cryptodev_null(void)
3454 {
3455         gbl_cryptodev_type = RTE_CRYPTODEV_NULL_PMD;
3456
3457         return unit_test_suite_runner(&cryptodev_null_testsuite);
3458 }
3459
3460 static struct test_command cryptodev_null_cmd = {
3461         .command = "cryptodev_null_autotest",
3462         .callback = test_cryptodev_null,
3463 };
3464
3465 static int
3466 test_cryptodev_sw_snow3g(void /*argv __rte_unused, int argc __rte_unused*/)
3467 {
3468         gbl_cryptodev_type = RTE_CRYPTODEV_SNOW3G_PMD;
3469
3470         return unit_test_suite_runner(&cryptodev_sw_snow3g_testsuite);
3471 }
3472
3473 static struct test_command cryptodev_sw_snow3g_cmd = {
3474         .command = "cryptodev_sw_snow3g_autotest",
3475         .callback = test_cryptodev_sw_snow3g,
3476 };
3477
3478 REGISTER_TEST_COMMAND(cryptodev_qat_cmd);
3479 REGISTER_TEST_COMMAND(cryptodev_aesni_mb_cmd);
3480 REGISTER_TEST_COMMAND(cryptodev_aesni_gcm_cmd);
3481 REGISTER_TEST_COMMAND(cryptodev_null_cmd);
3482 REGISTER_TEST_COMMAND(cryptodev_sw_snow3g_cmd);