aesni_mb: fix wrong return value
[dpdk.git] / app / test / test_cryptodev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 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 #include <rte_mbuf_offload.h>
39
40 #include <rte_crypto.h>
41 #include <rte_cryptodev.h>
42 #include <rte_cryptodev_pmd.h>
43
44 #include "test.h"
45 #include "test_cryptodev.h"
46
47 static enum rte_cryptodev_type gbl_cryptodev_type;
48
49 struct crypto_testsuite_params {
50         struct rte_mempool *mbuf_pool;
51         struct rte_mempool *mbuf_ol_pool;
52         struct rte_cryptodev_config conf;
53         struct rte_cryptodev_qp_conf qp_conf;
54
55         uint8_t valid_devs[RTE_CRYPTO_MAX_DEVS];
56         uint8_t valid_dev_count;
57 };
58
59 struct crypto_unittest_params {
60         struct rte_crypto_xform cipher_xform;
61         struct rte_crypto_xform auth_xform;
62
63         struct rte_cryptodev_session *sess;
64
65         struct rte_mbuf_offload *ol;
66         struct rte_crypto_op *op;
67
68         struct rte_mbuf *obuf, *ibuf;
69
70         uint8_t *digest;
71 };
72
73 /*
74  * Forward declarations.
75  */
76 static int
77 test_AES_CBC_HMAC_SHA512_decrypt_create_session_params(
78                 struct crypto_unittest_params *ut_params);
79
80 static int
81 test_AES_CBC_HMAC_SHA512_decrypt_perform(struct rte_cryptodev_session *sess,
82                 struct crypto_unittest_params *ut_params,
83                 struct crypto_testsuite_params *ts_param);
84
85 static struct rte_mbuf *
86 setup_test_string(struct rte_mempool *mpool,
87                 const char *string, size_t len, uint8_t blocksize)
88 {
89         struct rte_mbuf *m = rte_pktmbuf_alloc(mpool);
90         size_t t_len = len - (blocksize ? (len % blocksize) : 0);
91
92         memset(m->buf_addr, 0, m->buf_len);
93         if (m) {
94                 char *dst = rte_pktmbuf_append(m, t_len);
95
96                 if (!dst) {
97                         rte_pktmbuf_free(m);
98                         return NULL;
99                 }
100
101                 rte_memcpy(dst, string, t_len);
102         }
103
104         return m;
105 }
106
107 #if HEX_DUMP
108 static void
109 hexdump_mbuf_data(FILE *f, const char *title, struct rte_mbuf *m)
110 {
111         rte_hexdump(f, title, rte_pktmbuf_mtod(m, const void *), m->data_len);
112 }
113 #endif
114
115 static struct rte_mbuf *
116 process_crypto_request(uint8_t dev_id, struct rte_mbuf *ibuf)
117 {
118         struct rte_mbuf *obuf = NULL;
119 #if HEX_DUMP
120         hexdump_mbuf_data(stdout, "Enqueued Packet", ibuf);
121 #endif
122
123         if (rte_cryptodev_enqueue_burst(dev_id, 0, &ibuf, 1) != 1) {
124                 printf("Error sending packet for encryption");
125                 return NULL;
126         }
127         while (rte_cryptodev_dequeue_burst(dev_id, 0, &obuf, 1) == 0)
128                 rte_pause();
129
130 #if HEX_DUMP
131         if (obuf)
132                 hexdump_mbuf_data(stdout, "Dequeued Packet", obuf);
133 #endif
134
135         return obuf;
136 }
137
138 static struct crypto_testsuite_params testsuite_params = { NULL };
139 static struct crypto_unittest_params unittest_params;
140
141 static int
142 testsuite_setup(void)
143 {
144         struct crypto_testsuite_params *ts_params = &testsuite_params;
145         struct rte_cryptodev_info info;
146         unsigned i, nb_devs, dev_id;
147         int ret;
148         uint16_t qp_id;
149
150         memset(ts_params, 0, sizeof(*ts_params));
151
152         ts_params->mbuf_pool = rte_mempool_lookup("CRYPTO_MBUFPOOL");
153         if (ts_params->mbuf_pool == NULL) {
154                 /* Not already created so create */
155                 ts_params->mbuf_pool = rte_pktmbuf_pool_create(
156                                 "CRYPTO_MBUFPOOL",
157                                 NUM_MBUFS, MBUF_CACHE_SIZE, 0, MBUF_SIZE,
158                                 rte_socket_id());
159                 if (ts_params->mbuf_pool == NULL) {
160                         RTE_LOG(ERR, USER1, "Can't create CRYPTO_MBUFPOOL\n");
161                         return TEST_FAILED;
162                 }
163         }
164
165         ts_params->mbuf_ol_pool = rte_pktmbuf_offload_pool_create(
166                         "MBUF_OFFLOAD_POOL",
167                         NUM_MBUFS, MBUF_CACHE_SIZE,
168                         DEFAULT_NUM_XFORMS * sizeof(struct rte_crypto_xform),
169                         rte_socket_id());
170         if (ts_params->mbuf_ol_pool == NULL) {
171                 RTE_LOG(ERR, USER1, "Can't create CRYPTO_OP_POOL\n");
172                 return TEST_FAILED;
173         }
174
175         /* Create 2 AESNI MB devices if required */
176         if (gbl_cryptodev_type == RTE_CRYPTODEV_AESNI_MB_PMD) {
177                 nb_devs = rte_cryptodev_count_devtype(
178                                 RTE_CRYPTODEV_AESNI_MB_PMD);
179                 if (nb_devs < 2) {
180                         for (i = nb_devs; i < 2; i++) {
181                                 ret = rte_eal_vdev_init(
182                                         CRYPTODEV_NAME_AESNI_MB_PMD, NULL);
183
184                                 TEST_ASSERT(ret == 0,
185                                         "Failed to create instance %u of"
186                                         " pmd : %s",
187                                         i, CRYPTODEV_NAME_AESNI_MB_PMD);
188                         }
189                 }
190         }
191
192         nb_devs = rte_cryptodev_count();
193         if (nb_devs < 1) {
194                 RTE_LOG(ERR, USER1, "No crypto devices found?");
195                 return TEST_FAILED;
196         }
197
198         /* Create list of valid crypto devs */
199         for (i = 0; i < nb_devs; i++) {
200                 rte_cryptodev_info_get(i, &info);
201                 if (info.dev_type == gbl_cryptodev_type)
202                         ts_params->valid_devs[ts_params->valid_dev_count++] = i;
203         }
204
205         if (ts_params->valid_dev_count < 1)
206                 return TEST_FAILED;
207
208         /* Set up all the qps on the first of the valid devices found */
209         for (i = 0; i < 1; i++) {
210                 dev_id = ts_params->valid_devs[i];
211
212                 rte_cryptodev_info_get(dev_id, &info);
213
214                 /*
215                  * Since we can't free and re-allocate queue memory always set
216                  * the queues on this device up to max size first so enough
217                  * memory is allocated for any later re-configures needed by
218                  * other tests
219                  */
220
221                 ts_params->conf.nb_queue_pairs = info.max_nb_queue_pairs;
222                 ts_params->conf.socket_id = SOCKET_ID_ANY;
223                 ts_params->conf.session_mp.nb_objs = info.max_nb_sessions;
224
225                 TEST_ASSERT_SUCCESS(rte_cryptodev_configure(dev_id,
226                                 &ts_params->conf),
227                                 "Failed to configure cryptodev %u with %u qps",
228                                 dev_id, ts_params->conf.nb_queue_pairs);
229
230                 ts_params->qp_conf.nb_descriptors = MAX_NUM_OPS_INFLIGHT;
231
232                 for (qp_id = 0; qp_id < info.max_nb_queue_pairs; qp_id++) {
233                         TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
234                                         dev_id, qp_id, &ts_params->qp_conf,
235                                         rte_cryptodev_socket_id(dev_id)),
236                                         "Failed to setup queue pair %u on "
237                                         "cryptodev %u",
238                                         qp_id, dev_id);
239                 }
240         }
241
242         return TEST_SUCCESS;
243 }
244
245 static void
246 testsuite_teardown(void)
247 {
248         struct crypto_testsuite_params *ts_params = &testsuite_params;
249
250         if (ts_params->mbuf_pool != NULL) {
251                 RTE_LOG(DEBUG, USER1, "CRYPTO_MBUFPOOL count %u\n",
252                 rte_mempool_count(ts_params->mbuf_pool));
253         }
254
255
256         if (ts_params->mbuf_ol_pool != NULL) {
257                 RTE_LOG(DEBUG, USER1, "CRYPTO_OP_POOL count %u\n",
258                 rte_mempool_count(ts_params->mbuf_ol_pool));
259         }
260
261 }
262
263 static int
264 ut_setup(void)
265 {
266         struct crypto_testsuite_params *ts_params = &testsuite_params;
267         struct crypto_unittest_params *ut_params = &unittest_params;
268
269         uint16_t qp_id;
270
271         /* Clear unit test parameters before running test */
272         memset(ut_params, 0, sizeof(*ut_params));
273
274         /* Reconfigure device to default parameters */
275         ts_params->conf.nb_queue_pairs = DEFAULT_NUM_QPS_PER_QAT_DEVICE;
276         ts_params->conf.socket_id = SOCKET_ID_ANY;
277         ts_params->conf.session_mp.nb_objs =
278                         (gbl_cryptodev_type == RTE_CRYPTODEV_QAT_PMD) ?
279                                         DEFAULT_NUM_OPS_INFLIGHT :
280                                         DEFAULT_NUM_OPS_INFLIGHT;
281
282         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(ts_params->valid_devs[0],
283                         &ts_params->conf),
284                         "Failed to configure cryptodev %u",
285                         ts_params->valid_devs[0]);
286
287         /*
288          * Now reconfigure queues to size we actually want to use in this
289          * test suite.
290          */
291         ts_params->qp_conf.nb_descriptors = DEFAULT_NUM_OPS_INFLIGHT;
292
293         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs ; qp_id++) {
294                 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
295                         ts_params->valid_devs[0], qp_id,
296                         &ts_params->qp_conf,
297                         rte_cryptodev_socket_id(ts_params->valid_devs[0])),
298                         "Failed to setup queue pair %u on cryptodev %u",
299                         qp_id, ts_params->valid_devs[0]);
300         }
301
302
303         rte_cryptodev_stats_reset(ts_params->valid_devs[0]);
304
305         /* Start the device */
306         TEST_ASSERT_SUCCESS(rte_cryptodev_start(ts_params->valid_devs[0]),
307                         "Failed to start cryptodev %u",
308                         ts_params->valid_devs[0]);
309
310         return TEST_SUCCESS;
311 }
312
313 static void
314 ut_teardown(void)
315 {
316         struct crypto_testsuite_params *ts_params = &testsuite_params;
317         struct crypto_unittest_params *ut_params = &unittest_params;
318         struct rte_cryptodev_stats stats;
319
320         /* free crypto session structure */
321         if (ut_params->sess) {
322                 rte_cryptodev_session_free(ts_params->valid_devs[0],
323                                 ut_params->sess);
324                 ut_params->sess = NULL;
325         }
326
327         /* free crypto operation structure */
328         if (ut_params->ol)
329                 rte_pktmbuf_offload_free(ut_params->ol);
330
331         /*
332          * free mbuf - both obuf and ibuf are usually the same,
333          * but rte copes even if we call free twice
334          */
335         if (ut_params->obuf) {
336                 rte_pktmbuf_free(ut_params->obuf);
337                 ut_params->obuf = 0;
338         }
339         if (ut_params->ibuf) {
340                 rte_pktmbuf_free(ut_params->ibuf);
341                 ut_params->ibuf = 0;
342         }
343
344         if (ts_params->mbuf_pool != NULL)
345                 RTE_LOG(DEBUG, USER1, "CRYPTO_MBUFPOOL count %u\n",
346                                 rte_mempool_count(ts_params->mbuf_pool));
347
348         rte_cryptodev_stats_get(ts_params->valid_devs[0], &stats);
349
350         /* Stop the device */
351         rte_cryptodev_stop(ts_params->valid_devs[0]);
352 }
353
354 static int
355 test_device_configure_invalid_dev_id(void)
356 {
357         struct crypto_testsuite_params *ts_params = &testsuite_params;
358         uint16_t dev_id, num_devs = 0;
359
360         TEST_ASSERT((num_devs = rte_cryptodev_count()) >= 1,
361                         "Need at least %d devices for test", 1);
362
363         /* valid dev_id values */
364         dev_id = ts_params->valid_devs[ts_params->valid_dev_count - 1];
365
366         /* Stop the device in case it's started so it can be configured */
367         rte_cryptodev_stop(ts_params->valid_devs[dev_id]);
368
369         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(dev_id, &ts_params->conf),
370                         "Failed test for rte_cryptodev_configure: "
371                         "invalid dev_num %u", dev_id);
372
373         /* invalid dev_id values */
374         dev_id = num_devs;
375
376         TEST_ASSERT_FAIL(rte_cryptodev_configure(dev_id, &ts_params->conf),
377                         "Failed test for rte_cryptodev_configure: "
378                         "invalid dev_num %u", dev_id);
379
380         dev_id = 0xff;
381
382         TEST_ASSERT_FAIL(rte_cryptodev_configure(dev_id, &ts_params->conf),
383                         "Failed test for rte_cryptodev_configure:"
384                         "invalid dev_num %u", dev_id);
385
386         return TEST_SUCCESS;
387 }
388
389 static int
390 test_device_configure_invalid_queue_pair_ids(void)
391 {
392         struct crypto_testsuite_params *ts_params = &testsuite_params;
393
394         /* Stop the device in case it's started so it can be configured */
395         rte_cryptodev_stop(ts_params->valid_devs[0]);
396
397         /* valid - one queue pairs */
398         ts_params->conf.nb_queue_pairs = 1;
399
400         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(ts_params->valid_devs[0],
401                         &ts_params->conf),
402                         "Failed to configure cryptodev: dev_id %u, qp_id %u",
403                         ts_params->valid_devs[0], ts_params->conf.nb_queue_pairs);
404
405
406         /* valid - max value queue pairs */
407         ts_params->conf.nb_queue_pairs = MAX_NUM_QPS_PER_QAT_DEVICE;
408
409         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(ts_params->valid_devs[0],
410                         &ts_params->conf),
411                         "Failed to configure cryptodev: dev_id %u, qp_id %u",
412                         ts_params->valid_devs[0], ts_params->conf.nb_queue_pairs);
413
414
415         /* invalid - zero queue pairs */
416         ts_params->conf.nb_queue_pairs = 0;
417
418         TEST_ASSERT_FAIL(rte_cryptodev_configure(ts_params->valid_devs[0],
419                         &ts_params->conf),
420                         "Failed test for rte_cryptodev_configure, dev_id %u,"
421                         " invalid qps: %u",
422                         ts_params->valid_devs[0],
423                         ts_params->conf.nb_queue_pairs);
424
425
426         /* invalid - max value supported by field queue pairs */
427         ts_params->conf.nb_queue_pairs = UINT16_MAX;
428
429         TEST_ASSERT_FAIL(rte_cryptodev_configure(ts_params->valid_devs[0],
430                         &ts_params->conf),
431                         "Failed test for rte_cryptodev_configure, dev_id %u,"
432                         " invalid qps: %u",
433                         ts_params->valid_devs[0],
434                         ts_params->conf.nb_queue_pairs);
435
436
437         /* invalid - max value + 1 queue pairs */
438         ts_params->conf.nb_queue_pairs = MAX_NUM_QPS_PER_QAT_DEVICE + 1;
439
440         TEST_ASSERT_FAIL(rte_cryptodev_configure(ts_params->valid_devs[0],
441                         &ts_params->conf),
442                         "Failed test for rte_cryptodev_configure, dev_id %u,"
443                         " invalid qps: %u",
444                         ts_params->valid_devs[0],
445                         ts_params->conf.nb_queue_pairs);
446
447         return TEST_SUCCESS;
448 }
449
450 static int
451 test_queue_pair_descriptor_setup(void)
452 {
453         struct crypto_testsuite_params *ts_params = &testsuite_params;
454         struct rte_cryptodev_info dev_info;
455         struct rte_cryptodev_qp_conf qp_conf = {
456                 .nb_descriptors = MAX_NUM_OPS_INFLIGHT
457         };
458
459         uint16_t qp_id;
460
461         /* Stop the device in case it's started so it can be configured */
462         rte_cryptodev_stop(ts_params->valid_devs[0]);
463
464
465         rte_cryptodev_info_get(ts_params->valid_devs[0], &dev_info);
466
467         ts_params->conf.session_mp.nb_objs = dev_info.max_nb_sessions;
468
469         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(ts_params->valid_devs[0],
470                         &ts_params->conf), "Failed to configure cryptodev %u",
471                         ts_params->valid_devs[0]);
472
473
474         /*
475          * Test various ring sizes on this device. memzones can't be
476          * freed so are re-used if ring is released and re-created.
477          */
478         qp_conf.nb_descriptors = MIN_NUM_OPS_INFLIGHT; /* min size*/
479
480         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
481                 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
482                                 ts_params->valid_devs[0], qp_id, &qp_conf,
483                                 rte_cryptodev_socket_id(
484                                                 ts_params->valid_devs[0])),
485                                 "Failed test for "
486                                 "rte_cryptodev_queue_pair_setup: num_inflights "
487                                 "%u on qp %u on cryptodev %u",
488                                 qp_conf.nb_descriptors, qp_id,
489                                 ts_params->valid_devs[0]);
490         }
491
492         qp_conf.nb_descriptors = (uint32_t)(MAX_NUM_OPS_INFLIGHT / 2);
493
494         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
495                 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
496                                 ts_params->valid_devs[0], qp_id, &qp_conf,
497                                 rte_cryptodev_socket_id(
498                                                 ts_params->valid_devs[0])),
499                                 "Failed test for"
500                                 " rte_cryptodev_queue_pair_setup: num_inflights"
501                                 " %u on qp %u on cryptodev %u",
502                                 qp_conf.nb_descriptors, qp_id,
503                                 ts_params->valid_devs[0]);
504         }
505
506         qp_conf.nb_descriptors = MAX_NUM_OPS_INFLIGHT; /* valid */
507
508         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
509                 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
510                                 ts_params->valid_devs[0], qp_id, &qp_conf,
511                                 rte_cryptodev_socket_id(
512                                                 ts_params->valid_devs[0])),
513                                 "Failed test for "
514                                 "rte_cryptodev_queue_pair_setup: num_inflights"
515                                 " %u on qp %u on cryptodev %u",
516                                 qp_conf.nb_descriptors, qp_id,
517                                 ts_params->valid_devs[0]);
518         }
519
520         /* invalid number of descriptors - max supported + 2 */
521         qp_conf.nb_descriptors = MAX_NUM_OPS_INFLIGHT + 2;
522
523         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
524                 TEST_ASSERT_FAIL(rte_cryptodev_queue_pair_setup(
525                                 ts_params->valid_devs[0], qp_id, &qp_conf,
526                                 rte_cryptodev_socket_id(
527                                                 ts_params->valid_devs[0])),
528                                 "Unexpectedly passed test for "
529                                 "rte_cryptodev_queue_pair_setup:"
530                                 "num_inflights %u on qp %u on cryptodev %u",
531                                 qp_conf.nb_descriptors, qp_id,
532                                 ts_params->valid_devs[0]);
533         }
534
535         /* invalid number of descriptors - max value of parameter */
536         qp_conf.nb_descriptors = UINT32_MAX-1;
537
538         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
539                 TEST_ASSERT_FAIL(rte_cryptodev_queue_pair_setup(
540                                 ts_params->valid_devs[0], qp_id, &qp_conf,
541                                 rte_cryptodev_socket_id(
542                                                 ts_params->valid_devs[0])),
543                                 "Unexpectedly passed test for "
544                                 "rte_cryptodev_queue_pair_setup:"
545                                 "num_inflights %u on qp %u on cryptodev %u",
546                                 qp_conf.nb_descriptors, qp_id,
547                                 ts_params->valid_devs[0]);
548         }
549
550         qp_conf.nb_descriptors = DEFAULT_NUM_OPS_INFLIGHT;
551
552         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
553                 TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
554                                 ts_params->valid_devs[0], qp_id, &qp_conf,
555                                 rte_cryptodev_socket_id(
556                                                 ts_params->valid_devs[0])),
557                                 "Failed test for"
558                                 " rte_cryptodev_queue_pair_setup:"
559                                 "num_inflights %u on qp %u on cryptodev %u",
560                                 qp_conf.nb_descriptors, qp_id,
561                                 ts_params->valid_devs[0]);
562         }
563
564         /* invalid number of descriptors - max supported + 1 */
565         qp_conf.nb_descriptors = DEFAULT_NUM_OPS_INFLIGHT + 1;
566
567         for (qp_id = 0; qp_id < ts_params->conf.nb_queue_pairs; qp_id++) {
568                 TEST_ASSERT_FAIL(rte_cryptodev_queue_pair_setup(
569                                 ts_params->valid_devs[0], qp_id, &qp_conf,
570                                 rte_cryptodev_socket_id(
571                                                 ts_params->valid_devs[0])),
572                                 "Unexpectedly passed test for "
573                                 "rte_cryptodev_queue_pair_setup:"
574                                 "num_inflights %u on qp %u on cryptodev %u",
575                                 qp_conf.nb_descriptors, qp_id,
576                                 ts_params->valid_devs[0]);
577         }
578
579         /* test invalid queue pair id */
580         qp_conf.nb_descriptors = DEFAULT_NUM_OPS_INFLIGHT;      /*valid */
581
582         qp_id = DEFAULT_NUM_QPS_PER_QAT_DEVICE;         /*invalid */
583
584         TEST_ASSERT_FAIL(rte_cryptodev_queue_pair_setup(
585                         ts_params->valid_devs[0],
586                         qp_id, &qp_conf,
587                         rte_cryptodev_socket_id(ts_params->valid_devs[0])),
588                         "Failed test for rte_cryptodev_queue_pair_setup:"
589                         "invalid qp %u on cryptodev %u",
590                         qp_id, ts_params->valid_devs[0]);
591
592         qp_id = 0xffff; /*invalid*/
593
594         TEST_ASSERT_FAIL(rte_cryptodev_queue_pair_setup(
595                         ts_params->valid_devs[0],
596                         qp_id, &qp_conf,
597                         rte_cryptodev_socket_id(ts_params->valid_devs[0])),
598                         "Failed test for rte_cryptodev_queue_pair_setup:"
599                         "invalid qp %u on cryptodev %u",
600                         qp_id, ts_params->valid_devs[0]);
601
602         return TEST_SUCCESS;
603 }
604
605 /* ***** Plaintext data for tests ***** */
606
607 const char catch_22_quote_1[] =
608                 "There was only one catch and that was Catch-22, which "
609                 "specified that a concern for one's safety in the face of "
610                 "dangers that were real and immediate was the process of a "
611                 "rational mind. Orr was crazy and could be grounded. All he "
612                 "had to do was ask; and as soon as he did, he would no longer "
613                 "be crazy and would have to fly more missions. Orr would be "
614                 "crazy to fly more missions and sane if he didn't, but if he "
615                 "was sane he had to fly them. If he flew them he was crazy "
616                 "and didn't have to; but if he didn't want to he was sane and "
617                 "had to. Yossarian was moved very deeply by the absolute "
618                 "simplicity of this clause of Catch-22 and let out a "
619                 "respectful whistle. \"That's some catch, that Catch-22\", he "
620                 "observed. \"It's the best there is,\" Doc Daneeka agreed.";
621
622 const char catch_22_quote[] =
623                 "What a lousy earth! He wondered how many people were "
624                 "destitute that same night even in his own prosperous country, "
625                 "how many homes were shanties, how many husbands were drunk "
626                 "and wives socked, and how many children were bullied, abused, "
627                 "or abandoned. How many families hungered for food they could "
628                 "not afford to buy? How many hearts were broken? How many "
629                 "suicides would take place that same night, how many people "
630                 "would go insane? How many cockroaches and landlords would "
631                 "triumph? How many winners were losers, successes failures, "
632                 "and rich men poor men? How many wise guys were stupid? How "
633                 "many happy endings were unhappy endings? How many honest men "
634                 "were liars, brave men cowards, loyal men traitors, how many "
635                 "sainted men were corrupt, how many people in positions of "
636                 "trust had sold their souls to bodyguards, how many had never "
637                 "had souls? How many straight-and-narrow paths were crooked "
638                 "paths? How many best families were worst families and how "
639                 "many good people were bad people? When you added them all up "
640                 "and then subtracted, you might be left with only the children, "
641                 "and perhaps with Albert Einstein and an old violinist or "
642                 "sculptor somewhere.";
643
644 #define QUOTE_480_BYTES         (480)
645 #define QUOTE_512_BYTES         (512)
646 #define QUOTE_768_BYTES         (768)
647 #define QUOTE_1024_BYTES        (1024)
648
649
650
651 /* ***** SHA1 Hash Tests ***** */
652
653 #define HMAC_KEY_LENGTH_SHA1    (DIGEST_BYTE_LENGTH_SHA1)
654
655 static uint8_t hmac_sha1_key[] = {
656         0xF8, 0x2A, 0xC7, 0x54, 0xDB, 0x96, 0x18, 0xAA,
657         0xC3, 0xA1, 0x53, 0xF6, 0x1F, 0x17, 0x60, 0xBD,
658         0xDE, 0xF4, 0xDE, 0xAD };
659
660 /* ***** SHA224 Hash Tests ***** */
661
662 #define HMAC_KEY_LENGTH_SHA224  (DIGEST_BYTE_LENGTH_SHA224)
663
664
665 /* ***** AES-CBC Cipher Tests ***** */
666
667 #define CIPHER_KEY_LENGTH_AES_CBC       (16)
668 #define CIPHER_IV_LENGTH_AES_CBC        (CIPHER_KEY_LENGTH_AES_CBC)
669
670 static uint8_t aes_cbc_key[] = {
671         0xE4, 0x23, 0x33, 0x8A, 0x35, 0x64, 0x61, 0xE2,
672         0x49, 0x03, 0xDD, 0xC6, 0xB8, 0xCA, 0x55, 0x7A };
673
674 static uint8_t aes_cbc_iv[] = {
675         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
676         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
677
678
679 /* ***** AES-CBC / HMAC-SHA1 Hash Tests ***** */
680
681 static const uint8_t catch_22_quote_2_512_bytes_AES_CBC_ciphertext[] = {
682         0x8B, 0X4D, 0XDA, 0X1B, 0XCF, 0X04, 0XA0, 0X31,
683         0XB4, 0XBF, 0XBD, 0X68, 0X43, 0X20, 0X7E, 0X76,
684         0XB1, 0X96, 0X8B, 0XA2, 0X7C, 0XA2, 0X83, 0X9E,
685         0X39, 0X5A, 0X2F, 0X7E, 0X92, 0XB4, 0X48, 0X1A,
686         0X3F, 0X6B, 0X5D, 0XDF, 0X52, 0X85, 0X5F, 0X8E,
687         0X42, 0X3C, 0XFB, 0XE9, 0X1A, 0X24, 0XD6, 0X08,
688         0XDD, 0XFD, 0X16, 0XFB, 0XE9, 0X55, 0XEF, 0XF0,
689         0XA0, 0X8D, 0X13, 0XAB, 0X81, 0XC6, 0X90, 0X01,
690         0XB5, 0X18, 0X84, 0XB3, 0XF6, 0XE6, 0X11, 0X57,
691         0XD6, 0X71, 0XC6, 0X3C, 0X3F, 0X2F, 0X33, 0XEE,
692         0X24, 0X42, 0X6E, 0XAC, 0X0B, 0XCA, 0XEC, 0XF9,
693         0X84, 0XF8, 0X22, 0XAA, 0X60, 0XF0, 0X32, 0XA9,
694         0X75, 0X75, 0X3B, 0XCB, 0X70, 0X21, 0X0A, 0X8D,
695         0X0F, 0XE0, 0XC4, 0X78, 0X2B, 0XF8, 0X97, 0XE3,
696         0XE4, 0X26, 0X4B, 0X29, 0XDA, 0X88, 0XCD, 0X46,
697         0XEC, 0XAA, 0XF9, 0X7F, 0XF1, 0X15, 0XEA, 0XC3,
698         0X87, 0XE6, 0X31, 0XF2, 0XCF, 0XDE, 0X4D, 0X80,
699         0X70, 0X91, 0X7E, 0X0C, 0XF7, 0X26, 0X3A, 0X92,
700         0X4F, 0X18, 0X83, 0XC0, 0X8F, 0X59, 0X01, 0XA5,
701         0X88, 0XD1, 0XDB, 0X26, 0X71, 0X27, 0X16, 0XF5,
702         0XEE, 0X10, 0X82, 0XAC, 0X68, 0X26, 0X9B, 0XE2,
703         0X6D, 0XD8, 0X9A, 0X80, 0XDF, 0X04, 0X31, 0XD5,
704         0XF1, 0X35, 0X5C, 0X3B, 0XDD, 0X9A, 0X65, 0XBA,
705         0X58, 0X34, 0X85, 0X61, 0X1C, 0X42, 0X10, 0X76,
706         0X73, 0X02, 0X42, 0XC9, 0X23, 0X18, 0X8E, 0XB4,
707         0X6F, 0XB4, 0XA3, 0X54, 0X6E, 0X88, 0X3B, 0X62,
708         0X7C, 0X02, 0X8D, 0X4C, 0X9F, 0XC8, 0X45, 0XF4,
709         0XC9, 0XDE, 0X4F, 0XEB, 0X22, 0X83, 0X1B, 0XE4,
710         0X49, 0X37, 0XE4, 0XAD, 0XE7, 0XCD, 0X21, 0X54,
711         0XBC, 0X1C, 0XC2, 0X04, 0X97, 0XB4, 0X10, 0X61,
712         0XF0, 0XE4, 0XEF, 0X27, 0X63, 0X3A, 0XDA, 0X91,
713         0X41, 0X25, 0X62, 0X1C, 0X5C, 0XB6, 0X38, 0X4A,
714         0X88, 0X71, 0X59, 0X5A, 0X8D, 0XA0, 0X09, 0XAF,
715         0X72, 0X94, 0XD7, 0X79, 0X5C, 0X60, 0X7C, 0X8F,
716         0X4C, 0XF5, 0XD9, 0XA1, 0X39, 0X6D, 0X81, 0X28,
717         0XEF, 0X13, 0X28, 0XDF, 0XF5, 0X3E, 0XF7, 0X8E,
718         0X09, 0X9C, 0X78, 0X18, 0X79, 0XB8, 0X68, 0XD7,
719         0XA8, 0X29, 0X62, 0XAD, 0XDE, 0XE1, 0X61, 0X76,
720         0X1B, 0X05, 0X16, 0XCD, 0XBF, 0X02, 0X8E, 0XA6,
721         0X43, 0X6E, 0X92, 0X55, 0X4F, 0X60, 0X9C, 0X03,
722         0XB8, 0X4F, 0XA3, 0X02, 0XAC, 0XA8, 0XA7, 0X0C,
723         0X1E, 0XB5, 0X6B, 0XF8, 0XC8, 0X4D, 0XDE, 0XD2,
724         0XB0, 0X29, 0X6E, 0X40, 0XE6, 0XD6, 0XC9, 0XE6,
725         0XB9, 0X0F, 0XB6, 0X63, 0XF5, 0XAA, 0X2B, 0X96,
726         0XA7, 0X16, 0XAC, 0X4E, 0X0A, 0X33, 0X1C, 0XA6,
727         0XE6, 0XBD, 0X8A, 0XCF, 0X40, 0XA9, 0XB2, 0XFA,
728         0X63, 0X27, 0XFD, 0X9B, 0XD9, 0XFC, 0XD5, 0X87,
729         0X8D, 0X4C, 0XB6, 0XA4, 0XCB, 0XE7, 0X74, 0X55,
730         0XF4, 0XFB, 0X41, 0X25, 0XB5, 0X4B, 0X0A, 0X1B,
731         0XB1, 0XD6, 0XB7, 0XD9, 0X47, 0X2A, 0XC3, 0X98,
732         0X6A, 0XC4, 0X03, 0X73, 0X1F, 0X93, 0X6E, 0X53,
733         0X19, 0X25, 0X64, 0X15, 0X83, 0XF9, 0X73, 0X2A,
734         0X74, 0XB4, 0X93, 0X69, 0XC4, 0X72, 0XFC, 0X26,
735         0XA2, 0X9F, 0X43, 0X45, 0XDD, 0XB9, 0XEF, 0X36,
736         0XC8, 0X3A, 0XCD, 0X99, 0X9B, 0X54, 0X1A, 0X36,
737         0XC1, 0X59, 0XF8, 0X98, 0XA8, 0XCC, 0X28, 0X0D,
738         0X73, 0X4C, 0XEE, 0X98, 0XCB, 0X7C, 0X58, 0X7E,
739         0X20, 0X75, 0X1E, 0XB7, 0XC9, 0XF8, 0XF2, 0X0E,
740         0X63, 0X9E, 0X05, 0X78, 0X1A, 0XB6, 0XA8, 0X7A,
741         0XF9, 0X98, 0X6A, 0XA6, 0X46, 0X84, 0X2E, 0XF6,
742         0X4B, 0XDC, 0X9B, 0X8F, 0X9B, 0X8F, 0XEE, 0XB4,
743         0XAA, 0X3F, 0XEE, 0XC0, 0X37, 0X27, 0X76, 0XC7,
744         0X95, 0XBB, 0X26, 0X74, 0X69, 0X12, 0X7F, 0XF1,
745         0XBB, 0XFF, 0XAE, 0XB5, 0X99, 0X6E, 0XCB, 0X0C
746 };
747
748 static const uint8_t catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA1_digest[] = {
749         0x9a, 0X4f, 0X88, 0X1b, 0Xb6, 0X8f, 0Xd8, 0X60,
750         0X42, 0X1a, 0X7d, 0X3d, 0Xf5, 0X82, 0X80, 0Xf1,
751         0X18, 0X8c, 0X1d, 0X32 };
752
753
754 static int
755 test_AES_CBC_HMAC_SHA1_encrypt_digest(void)
756 {
757         struct crypto_testsuite_params *ts_params = &testsuite_params;
758         struct crypto_unittest_params *ut_params = &unittest_params;
759
760         /* Generate test mbuf data and space for digest */
761         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
762                         catch_22_quote, QUOTE_512_BYTES, 0);
763
764         ut_params->digest = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
765                         DIGEST_BYTE_LENGTH_SHA1);
766         TEST_ASSERT_NOT_NULL(ut_params->digest, "no room to append digest");
767
768         /* Setup Cipher Parameters */
769         ut_params->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
770         ut_params->cipher_xform.next = &ut_params->auth_xform;
771
772         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
773         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
774         ut_params->cipher_xform.cipher.key.data = aes_cbc_key;
775         ut_params->cipher_xform.cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
776
777         /* Setup HMAC Parameters */
778
779         ut_params->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
780         ut_params->auth_xform.next = NULL;
781
782         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
783         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
784         ut_params->auth_xform.auth.key.length = HMAC_KEY_LENGTH_SHA1;
785         ut_params->auth_xform.auth.key.data = hmac_sha1_key;
786         ut_params->auth_xform.auth.digest_length = DIGEST_BYTE_LENGTH_SHA1;
787
788         /* Create Crypto session*/
789         ut_params->sess = rte_cryptodev_session_create(ts_params->valid_devs[0],
790                         &ut_params->cipher_xform);
791         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
792
793         /* Generate Crypto op data structure */
794         ut_params->ol = rte_pktmbuf_offload_alloc(ts_params->mbuf_ol_pool,
795                                 RTE_PKTMBUF_OL_CRYPTO);
796         TEST_ASSERT_NOT_NULL(ut_params->ol,
797                         "Failed to allocate pktmbuf offload");
798
799         ut_params->op = &ut_params->ol->op.crypto;
800
801         /* Set crypto operation data parameters */
802         rte_crypto_op_attach_session(ut_params->op, ut_params->sess);
803
804         ut_params->op->digest.data = ut_params->digest;
805         ut_params->op->digest.phys_addr = rte_pktmbuf_mtophys_offset(
806                         ut_params->ibuf, QUOTE_512_BYTES);
807         ut_params->op->digest.length = DIGEST_BYTE_LENGTH_SHA1;
808
809         ut_params->op->iv.data = (uint8_t *)rte_pktmbuf_prepend(ut_params->ibuf,
810                         CIPHER_IV_LENGTH_AES_CBC);
811         ut_params->op->iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
812         ut_params->op->iv.length = CIPHER_IV_LENGTH_AES_CBC;
813
814         rte_memcpy(ut_params->op->iv.data, aes_cbc_iv,
815                         CIPHER_IV_LENGTH_AES_CBC);
816
817         ut_params->op->data.to_cipher.offset = CIPHER_IV_LENGTH_AES_CBC;
818         ut_params->op->data.to_cipher.length = QUOTE_512_BYTES;
819         ut_params->op->data.to_hash.offset = CIPHER_IV_LENGTH_AES_CBC;
820         ut_params->op->data.to_hash.length = QUOTE_512_BYTES;
821
822         rte_pktmbuf_offload_attach(ut_params->ibuf, ut_params->ol);
823
824         /* Process crypto operation */
825         ut_params->obuf = process_crypto_request(ts_params->valid_devs[0],
826                         ut_params->ibuf);
827         TEST_ASSERT_NOT_NULL(ut_params->obuf, "failed to retrieve obuf");
828
829         /* Validate obuf */
830         TEST_ASSERT_BUFFERS_ARE_EQUAL(
831                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
832                         CIPHER_IV_LENGTH_AES_CBC,
833                         catch_22_quote_2_512_bytes_AES_CBC_ciphertext,
834                         QUOTE_512_BYTES,
835                         "Ciphertext data not as expected");
836
837         TEST_ASSERT_BUFFERS_ARE_EQUAL(
838                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
839                         CIPHER_IV_LENGTH_AES_CBC + QUOTE_512_BYTES,
840                         catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA1_digest,
841                         gbl_cryptodev_type == RTE_CRYPTODEV_AESNI_MB_PMD ?
842                                         TRUNCATED_DIGEST_BYTE_LENGTH_SHA1 :
843                                         DIGEST_BYTE_LENGTH_SHA1,
844                         "Generated digest data not as expected");
845
846         return TEST_SUCCESS;
847 }
848
849 static int
850 test_AES_CBC_HMAC_SHA1_encrypt_digest_sessionless(void)
851 {
852         struct crypto_testsuite_params *ts_params = &testsuite_params;
853         struct crypto_unittest_params *ut_params = &unittest_params;
854
855         /* Generate test mbuf data and space for digest */
856         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
857                         catch_22_quote, QUOTE_512_BYTES, 0);
858
859         ut_params->digest = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
860                         DIGEST_BYTE_LENGTH_SHA1);
861         TEST_ASSERT_NOT_NULL(ut_params->digest, "no room to append digest");
862
863         /* Generate Crypto op data structure */
864         ut_params->ol = rte_pktmbuf_offload_alloc(ts_params->mbuf_ol_pool,
865                                 RTE_PKTMBUF_OL_CRYPTO);
866         TEST_ASSERT_NOT_NULL(ut_params->ol,
867                         "Failed to allocate pktmbuf offload");
868
869         ut_params->op = &ut_params->ol->op.crypto;
870
871         TEST_ASSERT_NOT_NULL(rte_pktmbuf_offload_alloc_crypto_xforms(
872                         ut_params->ol, 2),
873                         "failed to allocate space for crypto transforms");
874
875         /* Set crypto operation data parameters */
876         ut_params->op->xform->type = RTE_CRYPTO_XFORM_CIPHER;
877
878         /* cipher parameters */
879         ut_params->op->xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
880         ut_params->op->xform->cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
881         ut_params->op->xform->cipher.key.data = aes_cbc_key;
882         ut_params->op->xform->cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
883
884         /* hash parameters */
885         ut_params->op->xform->next->type = RTE_CRYPTO_XFORM_AUTH;
886
887         ut_params->op->xform->next->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
888         ut_params->op->xform->next->auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
889         ut_params->op->xform->next->auth.key.length = HMAC_KEY_LENGTH_SHA1;
890         ut_params->op->xform->next->auth.key.data = hmac_sha1_key;
891         ut_params->op->xform->next->auth.digest_length =
892                         DIGEST_BYTE_LENGTH_SHA1;
893
894         ut_params->op->digest.data = ut_params->digest;
895         ut_params->op->digest.phys_addr = rte_pktmbuf_mtophys_offset(
896                         ut_params->ibuf, QUOTE_512_BYTES);
897         ut_params->op->digest.length = DIGEST_BYTE_LENGTH_SHA1;
898
899         ut_params->op->iv.data = (uint8_t *)rte_pktmbuf_prepend(ut_params->ibuf,
900                         CIPHER_IV_LENGTH_AES_CBC);
901         ut_params->op->iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
902         ut_params->op->iv.length = CIPHER_IV_LENGTH_AES_CBC;
903
904         rte_memcpy(ut_params->op->iv.data, aes_cbc_iv,
905                         CIPHER_IV_LENGTH_AES_CBC);
906
907         ut_params->op->data.to_cipher.offset = CIPHER_IV_LENGTH_AES_CBC;
908         ut_params->op->data.to_cipher.length = QUOTE_512_BYTES;
909         ut_params->op->data.to_hash.offset = CIPHER_IV_LENGTH_AES_CBC;
910         ut_params->op->data.to_hash.length = QUOTE_512_BYTES;
911
912         rte_pktmbuf_offload_attach(ut_params->ibuf, ut_params->ol);
913
914         /* Process crypto operation */
915         ut_params->obuf = process_crypto_request(ts_params->valid_devs[0],
916                         ut_params->ibuf);
917         TEST_ASSERT_NOT_NULL(ut_params->obuf, "failed to retrieve obuf");
918
919         /* Validate obuf */
920         TEST_ASSERT_BUFFERS_ARE_EQUAL(
921                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
922                         CIPHER_IV_LENGTH_AES_CBC,
923                         catch_22_quote_2_512_bytes_AES_CBC_ciphertext,
924                         QUOTE_512_BYTES,
925                         "Ciphertext data not as expected");
926
927         TEST_ASSERT_BUFFERS_ARE_EQUAL(
928                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
929                         CIPHER_IV_LENGTH_AES_CBC + QUOTE_512_BYTES,
930                         catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA1_digest,
931                         gbl_cryptodev_type == RTE_CRYPTODEV_AESNI_MB_PMD ?
932                                         TRUNCATED_DIGEST_BYTE_LENGTH_SHA1 :
933                                         DIGEST_BYTE_LENGTH_SHA1,
934                         "Generated digest data not as expected");
935
936
937         return TEST_SUCCESS;
938 }
939
940 static int
941 test_AES_CBC_HMAC_SHA1_decrypt_digest_verify(void)
942 {
943         struct crypto_testsuite_params *ts_params = &testsuite_params;
944         struct crypto_unittest_params *ut_params = &unittest_params;
945
946         /* Generate test mbuf data and digest */
947         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
948                         (const char *)
949                         catch_22_quote_2_512_bytes_AES_CBC_ciphertext,
950                         QUOTE_512_BYTES, 0);
951
952         ut_params->digest = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
953                         DIGEST_BYTE_LENGTH_SHA1);
954         TEST_ASSERT_NOT_NULL(ut_params->digest, "no room to append digest");
955
956         rte_memcpy(ut_params->digest,
957                         catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA1_digest,
958                         DIGEST_BYTE_LENGTH_SHA1);
959
960         /* Setup Cipher Parameters */
961         ut_params->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
962         ut_params->cipher_xform.next = NULL;
963
964         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
965         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
966         ut_params->cipher_xform.cipher.key.data = aes_cbc_key;
967         ut_params->cipher_xform.cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
968
969         /* Setup HMAC Parameters */
970         ut_params->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
971         ut_params->auth_xform.next = &ut_params->cipher_xform;
972
973         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
974         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
975         ut_params->auth_xform.auth.key.length = HMAC_KEY_LENGTH_SHA1;
976         ut_params->auth_xform.auth.key.data = hmac_sha1_key;
977         ut_params->auth_xform.auth.digest_length = DIGEST_BYTE_LENGTH_SHA1;
978
979         /* Create Crypto session*/
980         ut_params->sess = rte_cryptodev_session_create(ts_params->valid_devs[0],
981                         &ut_params->auth_xform);
982         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
983
984         /* Generate Crypto op data structure */
985         ut_params->ol = rte_pktmbuf_offload_alloc(ts_params->mbuf_ol_pool,
986                                 RTE_PKTMBUF_OL_CRYPTO);
987         TEST_ASSERT_NOT_NULL(ut_params->ol,
988                         "Failed to allocate pktmbuf offload");
989
990         ut_params->op = &ut_params->ol->op.crypto;
991
992
993         /* Set crypto operation data parameters */
994         rte_crypto_op_attach_session(ut_params->op, ut_params->sess);
995
996         ut_params->op->digest.data = ut_params->digest;
997         ut_params->op->digest.phys_addr = rte_pktmbuf_mtophys_offset(
998                         ut_params->ibuf, QUOTE_512_BYTES);
999         ut_params->op->digest.length = DIGEST_BYTE_LENGTH_SHA1;
1000
1001         ut_params->op->iv.data = (uint8_t *)rte_pktmbuf_prepend(ut_params->ibuf,
1002                         CIPHER_IV_LENGTH_AES_CBC);
1003         ut_params->op->iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
1004         ut_params->op->iv.length = CIPHER_IV_LENGTH_AES_CBC;
1005
1006         rte_memcpy(ut_params->op->iv.data, aes_cbc_iv,
1007                         CIPHER_IV_LENGTH_AES_CBC);
1008
1009         ut_params->op->data.to_cipher.offset = CIPHER_IV_LENGTH_AES_CBC;
1010         ut_params->op->data.to_cipher.length = QUOTE_512_BYTES;
1011
1012         ut_params->op->data.to_hash.offset = CIPHER_IV_LENGTH_AES_CBC;
1013         ut_params->op->data.to_hash.length = QUOTE_512_BYTES;
1014
1015         rte_pktmbuf_offload_attach(ut_params->ibuf, ut_params->ol);
1016
1017         /* Process crypto operation */
1018         ut_params->obuf = process_crypto_request(ts_params->valid_devs[0],
1019                         ut_params->ibuf);
1020         TEST_ASSERT_NOT_NULL(ut_params->obuf, "failed to retrieve obuf");
1021
1022         /* Validate obuf */
1023         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1024                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
1025                         CIPHER_IV_LENGTH_AES_CBC,
1026                         catch_22_quote,
1027                         QUOTE_512_BYTES,
1028                         "Ciphertext data not as expected");
1029
1030         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
1031                         "Digest verification failed");
1032
1033
1034         return TEST_SUCCESS;
1035 }
1036
1037
1038 /* ***** AES-CBC / HMAC-SHA256 Hash Tests ***** */
1039
1040 #define HMAC_KEY_LENGTH_SHA256  (DIGEST_BYTE_LENGTH_SHA256)
1041
1042 static uint8_t hmac_sha256_key[] = {
1043         0x42, 0x1a, 0x7d, 0x3d, 0xf5, 0x82, 0x80, 0xf1,
1044         0xF1, 0x35, 0x5C, 0x3B, 0xDD, 0x9A, 0x65, 0xBA,
1045         0x58, 0x34, 0x85, 0x61, 0x1C, 0x42, 0x10, 0x76,
1046         0x9a, 0x4f, 0x88, 0x1b, 0xb6, 0x8f, 0xd8, 0x60 };
1047
1048 static const uint8_t catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA256_digest[] = {
1049         0xc8, 0x57, 0x57, 0x31, 0x03, 0xe0, 0x03, 0x55,
1050         0x07, 0xc8, 0x9e, 0x7f, 0x48, 0x9a, 0x61, 0x9a,
1051         0x68, 0xee, 0x03, 0x0e, 0x71, 0x75, 0xc7, 0xf4,
1052         0x2e, 0x45, 0x26, 0x32, 0x7c, 0x12, 0x15, 0x15 };
1053
1054 static int
1055 test_AES_CBC_HMAC_SHA256_encrypt_digest(void)
1056 {
1057         struct crypto_testsuite_params *ts_params = &testsuite_params;
1058         struct crypto_unittest_params *ut_params = &unittest_params;
1059
1060         /* Generate test mbuf data and space for digest */
1061         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
1062                         catch_22_quote, QUOTE_512_BYTES, 0);
1063
1064         ut_params->digest = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
1065                         DIGEST_BYTE_LENGTH_SHA256);
1066         TEST_ASSERT_NOT_NULL(ut_params->digest, "no room to append digest");
1067
1068         /* Setup Cipher Parameters */
1069         ut_params->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
1070         ut_params->cipher_xform.next = &ut_params->auth_xform;
1071
1072         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1073         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1074         ut_params->cipher_xform.cipher.key.data = aes_cbc_key;
1075         ut_params->cipher_xform.cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
1076
1077         /* Setup HMAC Parameters */
1078         ut_params->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
1079         ut_params->auth_xform.next = NULL;
1080
1081         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1082         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
1083         ut_params->auth_xform.auth.key.length = HMAC_KEY_LENGTH_SHA256;
1084         ut_params->auth_xform.auth.key.data = hmac_sha256_key;
1085         ut_params->auth_xform.auth.digest_length = DIGEST_BYTE_LENGTH_SHA256;
1086
1087         /* Create Crypto session*/
1088         ut_params->sess = rte_cryptodev_session_create(ts_params->valid_devs[0],
1089                         &ut_params->cipher_xform);
1090         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
1091
1092         /* Generate Crypto op data structure */
1093         ut_params->ol = rte_pktmbuf_offload_alloc(ts_params->mbuf_ol_pool,
1094                                 RTE_PKTMBUF_OL_CRYPTO);
1095         TEST_ASSERT_NOT_NULL(ut_params->ol,
1096                         "Failed to allocate pktmbuf offload");
1097
1098         ut_params->op = &ut_params->ol->op.crypto;
1099
1100
1101         /* Set crypto operation data parameters */
1102         rte_crypto_op_attach_session(ut_params->op, ut_params->sess);
1103
1104         ut_params->op->digest.data = ut_params->digest;
1105         ut_params->op->digest.phys_addr = rte_pktmbuf_mtophys_offset(
1106                         ut_params->ibuf, QUOTE_512_BYTES);
1107         ut_params->op->digest.length = DIGEST_BYTE_LENGTH_SHA256;
1108
1109         ut_params->op->iv.data = (uint8_t *)rte_pktmbuf_prepend(ut_params->ibuf,
1110                         CIPHER_IV_LENGTH_AES_CBC);
1111         ut_params->op->iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
1112         ut_params->op->iv.length = CIPHER_IV_LENGTH_AES_CBC;
1113
1114         rte_memcpy(ut_params->op->iv.data, aes_cbc_iv,
1115                         CIPHER_IV_LENGTH_AES_CBC);
1116
1117         ut_params->op->data.to_cipher.offset = CIPHER_IV_LENGTH_AES_CBC;
1118         ut_params->op->data.to_cipher.length = QUOTE_512_BYTES;
1119         ut_params->op->data.to_hash.offset = CIPHER_IV_LENGTH_AES_CBC;
1120         ut_params->op->data.to_hash.length = QUOTE_512_BYTES;
1121
1122         rte_pktmbuf_offload_attach(ut_params->ibuf, ut_params->ol);
1123
1124         /* Process crypto operation */
1125         ut_params->obuf = process_crypto_request(ts_params->valid_devs[0],
1126                         ut_params->ibuf);
1127         TEST_ASSERT_NOT_NULL(ut_params->obuf, "failed to retrieve obuf");
1128
1129         /* Validate obuf */
1130         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1131                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
1132                         CIPHER_IV_LENGTH_AES_CBC,
1133                         catch_22_quote_2_512_bytes_AES_CBC_ciphertext,
1134                         QUOTE_512_BYTES,
1135                         "Ciphertext data not as expected");
1136
1137         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1138                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
1139                         CIPHER_IV_LENGTH_AES_CBC + QUOTE_512_BYTES,
1140                         catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA256_digest,
1141                         gbl_cryptodev_type == RTE_CRYPTODEV_AESNI_MB_PMD ?
1142                                         TRUNCATED_DIGEST_BYTE_LENGTH_SHA256 :
1143                                         DIGEST_BYTE_LENGTH_SHA256,
1144                         "Generated digest data not as expected");
1145
1146
1147         return TEST_SUCCESS;
1148 }
1149
1150 static int
1151 test_AES_CBC_HMAC_SHA256_decrypt_digest_verify(void)
1152 {
1153         struct crypto_testsuite_params *ts_params = &testsuite_params;
1154         struct crypto_unittest_params *ut_params = &unittest_params;
1155
1156         /* Generate test mbuf data and digest */
1157         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
1158                         (const char *)
1159                         catch_22_quote_2_512_bytes_AES_CBC_ciphertext,
1160                         QUOTE_512_BYTES, 0);
1161
1162         ut_params->digest = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
1163                         DIGEST_BYTE_LENGTH_SHA256);
1164         TEST_ASSERT_NOT_NULL(ut_params->digest, "no room to append digest");
1165
1166         rte_memcpy(ut_params->digest,
1167                         catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA256_digest,
1168                         DIGEST_BYTE_LENGTH_SHA256);
1169
1170         /* Setup Cipher Parameters */
1171         ut_params->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
1172         ut_params->cipher_xform.next = NULL;
1173
1174         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1175         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1176         ut_params->cipher_xform.cipher.key.data = aes_cbc_key;
1177         ut_params->cipher_xform.cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
1178
1179         /* Setup HMAC Parameters */
1180         ut_params->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
1181         ut_params->auth_xform.next = &ut_params->cipher_xform;
1182
1183         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
1184         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
1185         ut_params->auth_xform.auth.key.data = hmac_sha256_key;
1186         ut_params->auth_xform.auth.key.length = HMAC_KEY_LENGTH_SHA256;
1187         ut_params->auth_xform.auth.digest_length = DIGEST_BYTE_LENGTH_SHA256;
1188
1189         /* Create Crypto session*/
1190         ut_params->sess = rte_cryptodev_session_create(ts_params->valid_devs[0],
1191                         &ut_params->auth_xform);
1192         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
1193
1194         /* Generate Crypto op data structure */
1195         ut_params->ol = rte_pktmbuf_offload_alloc(ts_params->mbuf_ol_pool,
1196                                 RTE_PKTMBUF_OL_CRYPTO);
1197         TEST_ASSERT_NOT_NULL(ut_params->ol,
1198                         "Failed to allocate pktmbuf offload");
1199
1200         ut_params->op = &ut_params->ol->op.crypto;
1201
1202
1203         /* Set crypto operation data parameters */
1204         rte_crypto_op_attach_session(ut_params->op, ut_params->sess);
1205
1206         ut_params->op->digest.data = ut_params->digest;
1207         ut_params->op->digest.phys_addr = rte_pktmbuf_mtophys_offset(
1208                         ut_params->ibuf, QUOTE_512_BYTES);
1209         ut_params->op->digest.length = DIGEST_BYTE_LENGTH_SHA256;
1210
1211         ut_params->op->iv.data = (uint8_t *)rte_pktmbuf_prepend(
1212                         ut_params->ibuf, CIPHER_IV_LENGTH_AES_CBC);
1213         ut_params->op->iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
1214         ut_params->op->iv.length = CIPHER_IV_LENGTH_AES_CBC;
1215
1216         rte_memcpy(ut_params->op->iv.data, aes_cbc_iv,
1217                         CIPHER_IV_LENGTH_AES_CBC);
1218
1219         ut_params->op->data.to_cipher.offset = CIPHER_IV_LENGTH_AES_CBC;
1220         ut_params->op->data.to_cipher.length = QUOTE_512_BYTES;
1221
1222         ut_params->op->data.to_hash.offset = CIPHER_IV_LENGTH_AES_CBC;
1223         ut_params->op->data.to_hash.length = QUOTE_512_BYTES;
1224
1225         rte_pktmbuf_offload_attach(ut_params->ibuf, ut_params->ol);
1226
1227         /* Process crypto operation */
1228         ut_params->obuf = process_crypto_request(ts_params->valid_devs[0],
1229                         ut_params->ibuf);
1230         TEST_ASSERT_NOT_NULL(ut_params->obuf, "failed to retrieve obuf");
1231
1232         /* Validate obuf */
1233         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1234                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
1235                         CIPHER_IV_LENGTH_AES_CBC, catch_22_quote,
1236                         QUOTE_512_BYTES,
1237                         "Plaintext data not as expected");
1238
1239         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
1240                         "Digest verification failed");
1241
1242
1243         return TEST_SUCCESS;
1244 }
1245
1246 /* ***** AES-CBC / HMAC-SHA512 Hash Tests ***** */
1247
1248 #define HMAC_KEY_LENGTH_SHA512  (DIGEST_BYTE_LENGTH_SHA512)
1249
1250 static uint8_t hmac_sha512_key[] = {
1251         0x42, 0x1a, 0x7d, 0x3d, 0xf5, 0x82, 0x80, 0xf1,
1252         0xF1, 0x35, 0x5C, 0x3B, 0xDD, 0x9A, 0x65, 0xBA,
1253         0x58, 0x34, 0x85, 0x65, 0x1C, 0x42, 0x50, 0x76,
1254         0x9a, 0xaf, 0x88, 0x1b, 0xb6, 0x8f, 0xf8, 0x60,
1255         0xa2, 0x5a, 0x7f, 0x3f, 0xf4, 0x72, 0x70, 0xf1,
1256         0xF5, 0x35, 0x4C, 0x3B, 0xDD, 0x90, 0x65, 0xB0,
1257         0x47, 0x3a, 0x75, 0x61, 0x5C, 0xa2, 0x10, 0x76,
1258         0x9a, 0xaf, 0x77, 0x5b, 0xb6, 0x7f, 0xf7, 0x60 };
1259
1260 static const uint8_t catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA512_digest[] = {
1261         0x5D, 0x54, 0x66, 0xC1, 0x6E, 0xBC, 0x04, 0xB8,
1262         0x46, 0xB8, 0x08, 0x6E, 0xE0, 0xF0, 0x43, 0x48,
1263         0x37, 0x96, 0x9C, 0xC6, 0x9C, 0xC2, 0x1E, 0xE8,
1264         0xF2, 0x0C, 0x0B, 0xEF, 0x86, 0xA2, 0xE3, 0x70,
1265         0x95, 0xC8, 0xB3, 0x06, 0x47, 0xA9, 0x90, 0xE8,
1266         0xA0, 0xC6, 0x72, 0x69, 0x05, 0xC0, 0x0D, 0x0E,
1267         0x21, 0x96, 0x65, 0x93, 0x74, 0x43, 0x2A, 0x1D,
1268         0x2E, 0xBF, 0xC2, 0xC2, 0xEE, 0xCC, 0x2F, 0x0A };
1269
1270 static int
1271 test_AES_CBC_HMAC_SHA512_encrypt_digest(void)
1272 {
1273         struct crypto_testsuite_params *ts_params = &testsuite_params;
1274         struct crypto_unittest_params *ut_params = &unittest_params;
1275
1276         /* Generate test mbuf data and space for digest */
1277         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
1278                         catch_22_quote, QUOTE_512_BYTES, 0);
1279
1280         ut_params->digest = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
1281                         DIGEST_BYTE_LENGTH_SHA512);
1282         TEST_ASSERT_NOT_NULL(ut_params->digest, "no room to append digest");
1283
1284         /* Setup Cipher Parameters */
1285         ut_params->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
1286         ut_params->cipher_xform.next = &ut_params->auth_xform;
1287
1288         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1289         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1290         ut_params->cipher_xform.cipher.key.data = aes_cbc_key;
1291         ut_params->cipher_xform.cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
1292
1293         /* Setup HMAC Parameters */
1294         ut_params->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
1295         ut_params->auth_xform.next = NULL;
1296
1297         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1298         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
1299         ut_params->auth_xform.auth.key.length = HMAC_KEY_LENGTH_SHA512;
1300         ut_params->auth_xform.auth.key.data = hmac_sha512_key;
1301         ut_params->auth_xform.auth.digest_length = DIGEST_BYTE_LENGTH_SHA512;
1302
1303         /* Create Crypto session*/
1304         ut_params->sess = rte_cryptodev_session_create(ts_params->valid_devs[0],
1305                         &ut_params->cipher_xform);
1306
1307         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
1308
1309
1310         /* Generate Crypto op data structure */
1311         ut_params->ol = rte_pktmbuf_offload_alloc(ts_params->mbuf_ol_pool,
1312                                 RTE_PKTMBUF_OL_CRYPTO);
1313         TEST_ASSERT_NOT_NULL(ut_params->ol,
1314                         "Failed to allocate pktmbuf offload");
1315
1316         ut_params->op = &ut_params->ol->op.crypto;
1317
1318
1319         /* Set crypto operation data parameters */
1320         rte_crypto_op_attach_session(ut_params->op, ut_params->sess);
1321
1322         ut_params->op->digest.data = ut_params->digest;
1323         ut_params->op->digest.phys_addr = rte_pktmbuf_mtophys_offset(
1324                         ut_params->ibuf, QUOTE_512_BYTES);
1325         ut_params->op->digest.length = DIGEST_BYTE_LENGTH_SHA512;
1326
1327         ut_params->op->iv.data = (uint8_t *)rte_pktmbuf_prepend(ut_params->ibuf,
1328                         CIPHER_IV_LENGTH_AES_CBC);
1329         ut_params->op->iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
1330         ut_params->op->iv.length = CIPHER_IV_LENGTH_AES_CBC;
1331
1332         rte_memcpy(ut_params->op->iv.data, aes_cbc_iv,
1333                         CIPHER_IV_LENGTH_AES_CBC);
1334
1335         ut_params->op->data.to_cipher.offset = CIPHER_IV_LENGTH_AES_CBC;
1336         ut_params->op->data.to_cipher.length = QUOTE_512_BYTES;
1337         ut_params->op->data.to_hash.offset = CIPHER_IV_LENGTH_AES_CBC;
1338         ut_params->op->data.to_hash.length = QUOTE_512_BYTES;
1339
1340         rte_pktmbuf_offload_attach(ut_params->ibuf, ut_params->ol);
1341
1342         /* Process crypto operation */
1343         ut_params->obuf = process_crypto_request(ts_params->valid_devs[0],
1344                         ut_params->ibuf);
1345         TEST_ASSERT_NOT_NULL(ut_params->obuf, "failed to retrieve obuf");
1346
1347         /* Validate obuf */
1348         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1349                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
1350                         CIPHER_IV_LENGTH_AES_CBC,
1351                         catch_22_quote_2_512_bytes_AES_CBC_ciphertext,
1352                         QUOTE_512_BYTES,
1353                         "Ciphertext data not as expected");
1354
1355         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1356                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
1357                         CIPHER_IV_LENGTH_AES_CBC + QUOTE_512_BYTES,
1358                         catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA512_digest,
1359                         gbl_cryptodev_type == RTE_CRYPTODEV_AESNI_MB_PMD ?
1360                                         TRUNCATED_DIGEST_BYTE_LENGTH_SHA512 :
1361                                         DIGEST_BYTE_LENGTH_SHA512,
1362                         "Generated digest data not as expected");
1363
1364
1365         return TEST_SUCCESS;
1366 }
1367
1368
1369 static int
1370 test_AES_CBC_HMAC_SHA512_decrypt_create_session_params(
1371                 struct crypto_unittest_params *ut_params);
1372
1373 static int
1374 test_AES_CBC_HMAC_SHA512_decrypt_perform(struct rte_cryptodev_session *sess,
1375                 struct crypto_unittest_params *ut_params,
1376                 struct crypto_testsuite_params *ts_params);
1377
1378 static int
1379 test_AES_CBC_HMAC_SHA512_decrypt_digest_verify(void)
1380 {
1381         struct crypto_unittest_params *ut_params = &unittest_params;
1382         struct crypto_testsuite_params *ts_params = &testsuite_params;
1383
1384         TEST_ASSERT(test_AES_CBC_HMAC_SHA512_decrypt_create_session_params(
1385                         ut_params) == TEST_SUCCESS,
1386                         "Failed to create session params");
1387
1388         /* Create Crypto session*/
1389         ut_params->sess = rte_cryptodev_session_create(ts_params->valid_devs[0],
1390                         &ut_params->auth_xform);
1391         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
1392
1393         return test_AES_CBC_HMAC_SHA512_decrypt_perform(ut_params->sess,
1394                         ut_params, ts_params);
1395 }
1396
1397 static int
1398 test_AES_CBC_HMAC_SHA512_decrypt_create_session_params(
1399                 struct crypto_unittest_params *ut_params)
1400 {
1401
1402         /* Setup Cipher Parameters */
1403         ut_params->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
1404         ut_params->cipher_xform.next = NULL;
1405
1406         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1407         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1408         ut_params->cipher_xform.cipher.key.data = aes_cbc_key;
1409         ut_params->cipher_xform.cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
1410
1411         /* Setup HMAC Parameters */
1412         ut_params->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
1413         ut_params->auth_xform.next = &ut_params->cipher_xform;
1414
1415         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
1416         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
1417         ut_params->auth_xform.auth.key.data = hmac_sha512_key;
1418         ut_params->auth_xform.auth.key.length = HMAC_KEY_LENGTH_SHA512;
1419         ut_params->auth_xform.auth.digest_length = DIGEST_BYTE_LENGTH_SHA512;
1420         return TEST_SUCCESS;
1421 }
1422
1423
1424 static int
1425 test_AES_CBC_HMAC_SHA512_decrypt_perform(struct rte_cryptodev_session *sess,
1426                 struct crypto_unittest_params *ut_params,
1427                 struct crypto_testsuite_params *ts_params)
1428 {
1429         /* Generate test mbuf data and digest */
1430         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
1431                         (const char *)
1432                         catch_22_quote_2_512_bytes_AES_CBC_ciphertext,
1433                         QUOTE_512_BYTES, 0);
1434
1435         ut_params->digest = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
1436                         DIGEST_BYTE_LENGTH_SHA512);
1437         TEST_ASSERT_NOT_NULL(ut_params->digest, "no room to append digest");
1438
1439         rte_memcpy(ut_params->digest,
1440                         catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA512_digest,
1441                         DIGEST_BYTE_LENGTH_SHA512);
1442
1443         /* Generate Crypto op data structure */
1444         ut_params->ol = rte_pktmbuf_offload_alloc(ts_params->mbuf_ol_pool,
1445                                 RTE_PKTMBUF_OL_CRYPTO);
1446         TEST_ASSERT_NOT_NULL(ut_params->ol,
1447                         "Failed to allocate pktmbuf offload");
1448
1449         ut_params->op = &ut_params->ol->op.crypto;
1450
1451
1452         /* Set crypto operation data parameters */
1453         rte_crypto_op_attach_session(ut_params->op, sess);
1454
1455         ut_params->op->digest.data = ut_params->digest;
1456         ut_params->op->digest.phys_addr = rte_pktmbuf_mtophys_offset(
1457                         ut_params->ibuf, QUOTE_512_BYTES);
1458         ut_params->op->digest.length = DIGEST_BYTE_LENGTH_SHA512;
1459
1460         ut_params->op->iv.data = (uint8_t *)rte_pktmbuf_prepend(
1461                         ut_params->ibuf, CIPHER_IV_LENGTH_AES_CBC);
1462         ut_params->op->iv.phys_addr = rte_pktmbuf_mtophys_offset(
1463                         ut_params->ibuf, 0);
1464         ut_params->op->iv.length = CIPHER_IV_LENGTH_AES_CBC;
1465
1466         rte_memcpy(ut_params->op->iv.data, aes_cbc_iv,
1467                         CIPHER_IV_LENGTH_AES_CBC);
1468
1469         ut_params->op->data.to_cipher.offset = CIPHER_IV_LENGTH_AES_CBC;
1470         ut_params->op->data.to_cipher.length = QUOTE_512_BYTES;
1471
1472         ut_params->op->data.to_hash.offset = CIPHER_IV_LENGTH_AES_CBC;
1473         ut_params->op->data.to_hash.length = QUOTE_512_BYTES;
1474
1475         rte_pktmbuf_offload_attach(ut_params->ibuf, ut_params->ol);
1476
1477         /* Process crypto operation */
1478         ut_params->obuf = process_crypto_request(ts_params->valid_devs[0],
1479                         ut_params->ibuf);
1480         TEST_ASSERT_NOT_NULL(ut_params->obuf, "failed to retrieve obuf");
1481
1482         /* Validate obuf */
1483         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1484                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
1485                         CIPHER_IV_LENGTH_AES_CBC, catch_22_quote,
1486                         QUOTE_512_BYTES,
1487                         "Plaintext data not as expected");
1488
1489         /* Validate obuf */
1490         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
1491                         "Digest verification failed");
1492
1493
1494
1495         return TEST_SUCCESS;
1496 }
1497
1498 /* ***** AES-CBC / HMAC-AES_XCBC Chain Tests ***** */
1499
1500 static uint8_t aes_cbc_hmac_aes_xcbc_key[] = {
1501         0x87, 0x61, 0x54, 0x53, 0xC4, 0x6D, 0xDD, 0x51,
1502         0xE1, 0x9F, 0x86, 0x64, 0x39, 0x0A, 0xE6, 0x59
1503         };
1504
1505 static const uint8_t  catch_22_quote_2_512_bytes_HMAC_AES_XCBC_digest[] = {
1506         0xE0, 0xAC, 0x9A, 0xC4, 0x22, 0x64, 0x35, 0x89,
1507         0x77, 0x1D, 0x8B, 0x75
1508         };
1509
1510 static int
1511 test_AES_CBC_HMAC_AES_XCBC_encrypt_digest(void)
1512 {
1513         struct crypto_testsuite_params *ts_params = &testsuite_params;
1514         struct crypto_unittest_params *ut_params = &unittest_params;
1515
1516         /* Generate test mbuf data and space for digest */
1517         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
1518                         catch_22_quote, QUOTE_512_BYTES, 0);
1519
1520         ut_params->digest = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
1521                         DIGEST_BYTE_LENGTH_AES_XCBC);
1522         TEST_ASSERT_NOT_NULL(ut_params->digest, "no room to append digest");
1523
1524         /* Setup Cipher Parameters */
1525         ut_params->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
1526         ut_params->cipher_xform.next = &ut_params->auth_xform;
1527
1528         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1529         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1530         ut_params->cipher_xform.cipher.key.data = aes_cbc_key;
1531         ut_params->cipher_xform.cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
1532
1533         /* Setup HMAC Parameters */
1534         ut_params->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
1535         ut_params->auth_xform.next = NULL;
1536
1537         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1538         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
1539         ut_params->auth_xform.auth.key.length = AES_XCBC_MAC_KEY_SZ;
1540         ut_params->auth_xform.auth.key.data = aes_cbc_hmac_aes_xcbc_key;
1541         ut_params->auth_xform.auth.digest_length = DIGEST_BYTE_LENGTH_AES_XCBC;
1542
1543         /* Create Crypto session*/
1544         ut_params->sess = rte_cryptodev_session_create(ts_params->valid_devs[0],
1545                         &ut_params->cipher_xform);
1546         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
1547
1548         /* Generate Crypto op data structure */
1549         ut_params->ol = rte_pktmbuf_offload_alloc(ts_params->mbuf_ol_pool,
1550                                 RTE_PKTMBUF_OL_CRYPTO);
1551         TEST_ASSERT_NOT_NULL(ut_params->ol,
1552                         "Failed to allocate pktmbuf offload");
1553
1554         ut_params->op = &ut_params->ol->op.crypto;
1555
1556
1557         /* Set crypto operation data parameters */
1558         rte_crypto_op_attach_session(ut_params->op, ut_params->sess);
1559
1560         ut_params->op->iv.data = (uint8_t *)
1561                 rte_pktmbuf_prepend(ut_params->ibuf,
1562                                 CIPHER_IV_LENGTH_AES_CBC);
1563         ut_params->op->iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
1564         ut_params->op->iv.length = CIPHER_IV_LENGTH_AES_CBC;
1565
1566         rte_memcpy(ut_params->op->iv.data, aes_cbc_iv,
1567                         CIPHER_IV_LENGTH_AES_CBC);
1568
1569         ut_params->op->data.to_cipher.offset = CIPHER_IV_LENGTH_AES_CBC;
1570         ut_params->op->data.to_cipher.length = QUOTE_512_BYTES;
1571         ut_params->op->data.to_hash.offset = CIPHER_IV_LENGTH_AES_CBC;
1572         ut_params->op->data.to_hash.length = QUOTE_512_BYTES;
1573
1574         rte_pktmbuf_offload_attach(ut_params->ibuf, ut_params->ol);
1575
1576         /* Process crypto operation */
1577         ut_params->obuf = process_crypto_request(ts_params->valid_devs[0],
1578                         ut_params->ibuf);
1579         TEST_ASSERT_NOT_NULL(ut_params->obuf, "failed to retrieve obuf");
1580
1581         /* Validate obuf */
1582         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1583                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
1584                         CIPHER_IV_LENGTH_AES_CBC,
1585                         catch_22_quote_2_512_bytes_AES_CBC_ciphertext,
1586                         QUOTE_512_BYTES,
1587                         "Ciphertext data not as expected");
1588         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1589                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
1590                         CIPHER_IV_LENGTH_AES_CBC + QUOTE_512_BYTES,
1591                         catch_22_quote_2_512_bytes_HMAC_AES_XCBC_digest,
1592                         DIGEST_BYTE_LENGTH_AES_XCBC,
1593                         "Generated digest data not as expected");
1594
1595         return TEST_SUCCESS;
1596 }
1597
1598 static int
1599 test_AES_CBC_HMAC_AES_XCBC_decrypt_digest_verify(void)
1600 {
1601         struct crypto_testsuite_params *ts_params = &testsuite_params;
1602         struct crypto_unittest_params *ut_params = &unittest_params;
1603
1604         /* Generate test mbuf data and space for digest */
1605         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
1606                 (const char *)catch_22_quote_2_512_bytes_AES_CBC_ciphertext,
1607                 QUOTE_512_BYTES, 0);
1608
1609         ut_params->digest = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
1610                         DIGEST_BYTE_LENGTH_AES_XCBC);
1611         TEST_ASSERT_NOT_NULL(ut_params->digest, "no room to append digest");
1612
1613         rte_memcpy(ut_params->digest,
1614                         catch_22_quote_2_512_bytes_HMAC_AES_XCBC_digest,
1615                         DIGEST_BYTE_LENGTH_AES_XCBC);
1616
1617         /* Setup Cipher Parameters */
1618         ut_params->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
1619         ut_params->cipher_xform.next = NULL;
1620
1621         ut_params->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1622         ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1623         ut_params->cipher_xform.cipher.key.data = aes_cbc_key;
1624         ut_params->cipher_xform.cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
1625
1626         /* Setup HMAC Parameters */
1627         ut_params->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
1628         ut_params->auth_xform.next = &ut_params->cipher_xform;
1629
1630         ut_params->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
1631         ut_params->auth_xform.auth.algo = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
1632         ut_params->auth_xform.auth.key.length = AES_XCBC_MAC_KEY_SZ;
1633         ut_params->auth_xform.auth.key.data = aes_cbc_hmac_aes_xcbc_key;
1634         ut_params->auth_xform.auth.digest_length = DIGEST_BYTE_LENGTH_AES_XCBC;
1635
1636         /* Create Crypto session*/
1637         ut_params->sess = rte_cryptodev_session_create(ts_params->valid_devs[0],
1638                         &ut_params->auth_xform);
1639         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
1640
1641         /* Generate Crypto op data structure */
1642         ut_params->ol = rte_pktmbuf_offload_alloc(ts_params->mbuf_ol_pool,
1643                                 RTE_PKTMBUF_OL_CRYPTO);
1644         TEST_ASSERT_NOT_NULL(ut_params->ol,
1645                         "Failed to allocate pktmbuf offload");
1646
1647         ut_params->op = &ut_params->ol->op.crypto;
1648
1649
1650         /* Set crypto operation data parameters */
1651         rte_crypto_op_attach_session(ut_params->op, ut_params->sess);
1652
1653         ut_params->op->iv.data = (uint8_t *)rte_pktmbuf_prepend(ut_params->ibuf,
1654                         CIPHER_IV_LENGTH_AES_CBC);
1655         ut_params->op->iv.phys_addr = rte_pktmbuf_mtophys(ut_params->ibuf);
1656         ut_params->op->iv.length = CIPHER_IV_LENGTH_AES_CBC;
1657
1658         rte_memcpy(ut_params->op->iv.data, aes_cbc_iv,
1659                         CIPHER_IV_LENGTH_AES_CBC);
1660
1661         ut_params->op->data.to_cipher.offset = CIPHER_IV_LENGTH_AES_CBC;
1662         ut_params->op->data.to_cipher.length = QUOTE_512_BYTES;
1663         ut_params->op->data.to_hash.offset = CIPHER_IV_LENGTH_AES_CBC;
1664         ut_params->op->data.to_hash.length = QUOTE_512_BYTES;
1665         rte_pktmbuf_offload_attach(ut_params->ibuf, ut_params->ol);
1666
1667         /* Process crypto operation */
1668         ut_params->obuf = process_crypto_request(ts_params->valid_devs[0],
1669                         ut_params->ibuf);
1670         TEST_ASSERT_NOT_NULL(ut_params->obuf, "failed to retrieve obuf");
1671
1672         /* Validate obuf */
1673         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1674                         rte_pktmbuf_mtod(ut_params->obuf, uint8_t *) +
1675                         CIPHER_IV_LENGTH_AES_CBC, catch_22_quote,
1676                         QUOTE_512_BYTES,
1677                         "Ciphertext data not as expected");
1678
1679         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
1680                         "Digest verification failed");
1681
1682         return TEST_SUCCESS;
1683 }
1684
1685
1686 /* ***** AES-GCM Tests ***** */
1687
1688 static int
1689 test_stats(void)
1690 {
1691         struct crypto_testsuite_params *ts_params = &testsuite_params;
1692         struct rte_cryptodev_stats stats;
1693         struct rte_cryptodev *dev;
1694         cryptodev_stats_get_t temp_pfn;
1695
1696         rte_cryptodev_stats_reset(ts_params->valid_devs[0]);
1697         TEST_ASSERT((rte_cryptodev_stats_get(ts_params->valid_devs[0] + 600,
1698                         &stats) == -ENODEV),
1699                 "rte_cryptodev_stats_get invalid dev failed");
1700         TEST_ASSERT((rte_cryptodev_stats_get(ts_params->valid_devs[0], 0) != 0),
1701                 "rte_cryptodev_stats_get invalid Param failed");
1702         dev = &rte_cryptodevs[ts_params->valid_devs[0]];
1703         temp_pfn = dev->dev_ops->stats_get;
1704         dev->dev_ops->stats_get = (cryptodev_stats_get_t)0;
1705         TEST_ASSERT((rte_cryptodev_stats_get(ts_params->valid_devs[0], &stats)
1706                         == -ENOTSUP),
1707                 "rte_cryptodev_stats_get invalid Param failed");
1708         dev->dev_ops->stats_get = temp_pfn;
1709
1710         /* Test expected values */
1711         ut_setup();
1712         test_AES_CBC_HMAC_SHA1_encrypt_digest();
1713         ut_teardown();
1714         TEST_ASSERT_SUCCESS(rte_cryptodev_stats_get(ts_params->valid_devs[0],
1715                         &stats),
1716                 "rte_cryptodev_stats_get failed");
1717         TEST_ASSERT((stats.enqueued_count == 1),
1718                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
1719         TEST_ASSERT((stats.dequeued_count == 1),
1720                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
1721         TEST_ASSERT((stats.enqueue_err_count == 0),
1722                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
1723         TEST_ASSERT((stats.dequeue_err_count == 0),
1724                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
1725
1726         /* invalid device but should ignore and not reset device stats*/
1727         rte_cryptodev_stats_reset(ts_params->valid_devs[0] + 300);
1728         TEST_ASSERT_SUCCESS(rte_cryptodev_stats_get(ts_params->valid_devs[0],
1729                         &stats),
1730                 "rte_cryptodev_stats_get failed");
1731         TEST_ASSERT((stats.enqueued_count == 1),
1732                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
1733
1734         /* check that a valid reset clears stats */
1735         rte_cryptodev_stats_reset(ts_params->valid_devs[0]);
1736         TEST_ASSERT_SUCCESS(rte_cryptodev_stats_get(ts_params->valid_devs[0],
1737                         &stats),
1738                                           "rte_cryptodev_stats_get failed");
1739         TEST_ASSERT((stats.enqueued_count == 0),
1740                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
1741         TEST_ASSERT((stats.dequeued_count == 0),
1742                 "rte_cryptodev_stats_get returned unexpected enqueued stat");
1743
1744         return TEST_SUCCESS;
1745 }
1746
1747
1748 static int
1749 test_multi_session(void)
1750 {
1751         struct crypto_testsuite_params *ts_params = &testsuite_params;
1752         struct crypto_unittest_params *ut_params = &unittest_params;
1753
1754         struct rte_cryptodev_info dev_info;
1755         struct rte_cryptodev_session **sessions;
1756
1757         uint16_t i;
1758
1759         test_AES_CBC_HMAC_SHA512_decrypt_create_session_params(ut_params);
1760
1761
1762         rte_cryptodev_info_get(ts_params->valid_devs[0], &dev_info);
1763
1764         sessions = rte_malloc(NULL, (sizeof(struct rte_cryptodev_session *) *
1765                         dev_info.max_nb_sessions) + 1, 0);
1766
1767         /* Create multiple crypto sessions*/
1768         for (i = 0; i < dev_info.max_nb_sessions; i++) {
1769                 sessions[i] = rte_cryptodev_session_create(
1770                                 ts_params->valid_devs[0],
1771                         &ut_params->auth_xform);
1772                 TEST_ASSERT_NOT_NULL(sessions[i],
1773                                 "Session creation failed at session number %u",
1774                                 i);
1775
1776                 /* Attempt to send a request on each session */
1777                 TEST_ASSERT_SUCCESS(test_AES_CBC_HMAC_SHA512_decrypt_perform(
1778                                 sessions[i], ut_params, ts_params),
1779                                 "Failed to perform decrypt on request "
1780                                 "number %u.", i);
1781         }
1782
1783         /* Next session create should fail */
1784         sessions[i] = rte_cryptodev_session_create(ts_params->valid_devs[0],
1785                         &ut_params->auth_xform);
1786         TEST_ASSERT_NULL(sessions[i],
1787                         "Session creation succeeded unexpectedly!");
1788
1789         for (i = 0; i < dev_info.max_nb_sessions; i++)
1790                 rte_cryptodev_session_free(ts_params->valid_devs[0],
1791                                 sessions[i]);
1792
1793         rte_free(sessions);
1794
1795         return TEST_SUCCESS;
1796 }
1797
1798 static int
1799 test_not_in_place_crypto(void)
1800 {
1801         struct crypto_testsuite_params *ts_params = &testsuite_params;
1802         struct crypto_unittest_params *ut_params = &unittest_params;
1803         struct rte_mbuf *dst_m = rte_pktmbuf_alloc(ts_params->mbuf_pool);
1804
1805         test_AES_CBC_HMAC_SHA512_decrypt_create_session_params(ut_params);
1806
1807         /* Create multiple crypto sessions*/
1808
1809         ut_params->sess = rte_cryptodev_session_create(
1810                         ts_params->valid_devs[0], &ut_params->auth_xform);
1811
1812         TEST_ASSERT_NOT_NULL(ut_params->sess, "Session creation failed");
1813
1814
1815         /* Generate test mbuf data and digest */
1816         ut_params->ibuf = setup_test_string(ts_params->mbuf_pool,
1817                         (const char *)
1818                         catch_22_quote_2_512_bytes_AES_CBC_ciphertext,
1819                         QUOTE_512_BYTES, 0);
1820
1821         ut_params->digest = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
1822                         DIGEST_BYTE_LENGTH_SHA512);
1823         TEST_ASSERT_NOT_NULL(ut_params->digest, "no room to append digest");
1824
1825         rte_memcpy(ut_params->digest,
1826                         catch_22_quote_2_512_bytes_AES_CBC_HMAC_SHA512_digest,
1827                         DIGEST_BYTE_LENGTH_SHA512);
1828
1829         /* Generate Crypto op data structure */
1830         ut_params->ol = rte_pktmbuf_offload_alloc(ts_params->mbuf_ol_pool,
1831                                 RTE_PKTMBUF_OL_CRYPTO);
1832         TEST_ASSERT_NOT_NULL(ut_params->ol,
1833                         "Failed to allocate pktmbuf offload");
1834
1835         ut_params->op = &ut_params->ol->op.crypto;
1836
1837
1838         /* Set crypto operation data parameters */
1839         rte_crypto_op_attach_session(ut_params->op, ut_params->sess);
1840
1841         ut_params->op->digest.data = ut_params->digest;
1842         ut_params->op->digest.phys_addr = rte_pktmbuf_mtophys_offset(
1843                         ut_params->ibuf, QUOTE_512_BYTES);
1844         ut_params->op->digest.length = DIGEST_BYTE_LENGTH_SHA512;
1845
1846         ut_params->op->iv.data = (uint8_t *)rte_pktmbuf_prepend(
1847                         ut_params->ibuf, CIPHER_IV_LENGTH_AES_CBC);
1848         ut_params->op->iv.phys_addr = rte_pktmbuf_mtophys_offset(
1849                         ut_params->ibuf, 0);
1850         ut_params->op->iv.length = CIPHER_IV_LENGTH_AES_CBC;
1851
1852         rte_memcpy(ut_params->op->iv.data, aes_cbc_iv,
1853                         CIPHER_IV_LENGTH_AES_CBC);
1854
1855         ut_params->op->data.to_cipher.offset = CIPHER_IV_LENGTH_AES_CBC;
1856         ut_params->op->data.to_cipher.length = QUOTE_512_BYTES;
1857
1858         ut_params->op->data.to_hash.offset = CIPHER_IV_LENGTH_AES_CBC;
1859         ut_params->op->data.to_hash.length = QUOTE_512_BYTES;
1860
1861         ut_params->op->dst.m = dst_m;
1862         ut_params->op->dst.offset = 0;
1863
1864         rte_pktmbuf_offload_attach(ut_params->ibuf, ut_params->ol);
1865
1866         /* Process crypto operation */
1867         ut_params->obuf = process_crypto_request(ts_params->valid_devs[0],
1868                         ut_params->ibuf);
1869         TEST_ASSERT_NOT_NULL(ut_params->obuf, "failed to retrieve obuf");
1870
1871         /* Validate obuf */
1872         TEST_ASSERT_BUFFERS_ARE_EQUAL(
1873                         rte_pktmbuf_mtod(ut_params->op->dst.m, char *),
1874                         catch_22_quote,
1875                         QUOTE_512_BYTES,
1876                         "Plaintext data not as expected");
1877
1878         /* Validate obuf */
1879
1880         TEST_ASSERT_EQUAL(ut_params->op->status, RTE_CRYPTO_OP_STATUS_SUCCESS,
1881                         "Digest verification failed");
1882
1883         return TEST_SUCCESS;
1884 }
1885
1886
1887 static struct unit_test_suite cryptodev_qat_testsuite  = {
1888         .suite_name = "Crypto QAT Unit Test Suite",
1889         .setup = testsuite_setup,
1890         .teardown = testsuite_teardown,
1891         .unit_test_cases = {
1892                 TEST_CASE_ST(ut_setup, ut_teardown,
1893                                 test_device_configure_invalid_dev_id),
1894                 TEST_CASE_ST(ut_setup, ut_teardown,
1895                                 test_device_configure_invalid_queue_pair_ids),
1896                 TEST_CASE_ST(ut_setup, ut_teardown,
1897                                 test_queue_pair_descriptor_setup),
1898                 TEST_CASE_ST(ut_setup, ut_teardown,
1899                                 test_multi_session),
1900
1901                 TEST_CASE_ST(ut_setup, ut_teardown,
1902                                 test_AES_CBC_HMAC_SHA1_encrypt_digest),
1903                 TEST_CASE_ST(ut_setup, ut_teardown,
1904                                 test_AES_CBC_HMAC_SHA1_decrypt_digest_verify),
1905
1906                 TEST_CASE_ST(ut_setup, ut_teardown,
1907                                 test_AES_CBC_HMAC_SHA256_encrypt_digest),
1908                 TEST_CASE_ST(ut_setup, ut_teardown,
1909                                 test_AES_CBC_HMAC_SHA256_decrypt_digest_verify),
1910
1911                 TEST_CASE_ST(ut_setup, ut_teardown,
1912                                 test_AES_CBC_HMAC_SHA512_encrypt_digest),
1913                 TEST_CASE_ST(ut_setup, ut_teardown,
1914                                 test_AES_CBC_HMAC_SHA512_decrypt_digest_verify),
1915
1916                 TEST_CASE_ST(ut_setup, ut_teardown,
1917                                 test_AES_CBC_HMAC_AES_XCBC_encrypt_digest),
1918                 TEST_CASE_ST(ut_setup, ut_teardown,
1919                                 test_AES_CBC_HMAC_AES_XCBC_decrypt_digest_verify),
1920
1921                 TEST_CASE_ST(ut_setup, ut_teardown, test_stats),
1922
1923                 TEST_CASES_END() /**< NULL terminate unit test array */
1924         }
1925 };
1926
1927 static struct unit_test_suite cryptodev_aesni_mb_testsuite  = {
1928         .suite_name = "Crypto Device AESNI MB Unit Test Suite",
1929         .setup = testsuite_setup,
1930         .teardown = testsuite_teardown,
1931         .unit_test_cases = {
1932                 TEST_CASE_ST(ut_setup, ut_teardown,
1933                         test_AES_CBC_HMAC_SHA1_encrypt_digest),
1934                 TEST_CASE_ST(ut_setup, ut_teardown,
1935                         test_AES_CBC_HMAC_SHA1_decrypt_digest_verify),
1936
1937                 TEST_CASE_ST(ut_setup, ut_teardown,
1938                         test_AES_CBC_HMAC_SHA256_encrypt_digest),
1939                 TEST_CASE_ST(ut_setup, ut_teardown,
1940                         test_AES_CBC_HMAC_SHA256_decrypt_digest_verify),
1941
1942                 TEST_CASE_ST(ut_setup, ut_teardown,
1943                         test_AES_CBC_HMAC_SHA512_encrypt_digest),
1944                 TEST_CASE_ST(ut_setup, ut_teardown,
1945                         test_AES_CBC_HMAC_SHA512_decrypt_digest_verify),
1946
1947                 TEST_CASE_ST(ut_setup, ut_teardown,
1948                         test_AES_CBC_HMAC_AES_XCBC_encrypt_digest),
1949                 TEST_CASE_ST(ut_setup, ut_teardown,
1950                         test_AES_CBC_HMAC_AES_XCBC_decrypt_digest_verify),
1951
1952                 TEST_CASE_ST(ut_setup, ut_teardown,
1953                         test_AES_CBC_HMAC_SHA1_encrypt_digest_sessionless),
1954
1955                 TEST_CASE_ST(ut_setup, ut_teardown,
1956                         test_not_in_place_crypto),
1957
1958                 TEST_CASES_END() /**< NULL terminate unit test array */
1959         }
1960 };
1961
1962 static int
1963 test_cryptodev_qat(void /*argv __rte_unused, int argc __rte_unused*/)
1964 {
1965         gbl_cryptodev_type = RTE_CRYPTODEV_QAT_PMD;
1966         return unit_test_suite_runner(&cryptodev_qat_testsuite);
1967 }
1968 static struct test_command cryptodev_qat_cmd = {
1969         .command = "cryptodev_qat_autotest",
1970         .callback = test_cryptodev_qat,
1971 };
1972
1973 static int
1974 test_cryptodev_aesni_mb(void /*argv __rte_unused, int argc __rte_unused*/)
1975 {
1976         gbl_cryptodev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
1977
1978         return unit_test_suite_runner(&cryptodev_aesni_mb_testsuite);
1979 }
1980
1981 static struct test_command cryptodev_aesni_mb_cmd = {
1982         .command = "cryptodev_aesni_mb_autotest",
1983         .callback = test_cryptodev_aesni_mb,
1984 };
1985
1986 REGISTER_TEST_COMMAND(cryptodev_qat_cmd);
1987 REGISTER_TEST_COMMAND(cryptodev_aesni_mb_cmd);