6089af4efc93d9b2bf9322297101162f1b36a332
[deb_dpdk.git] / test / test / test_cryptodev_blockcipher.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2017 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_pause.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 #include "test_cryptodev_blockcipher.h"
47 #include "test_cryptodev_aes_test_vectors.h"
48 #include "test_cryptodev_des_test_vectors.h"
49 #include "test_cryptodev_hash_test_vectors.h"
50
51 static int
52 test_blockcipher_one_case(const struct blockcipher_test_case *t,
53         struct rte_mempool *mbuf_pool,
54         struct rte_mempool *op_mpool,
55         struct rte_mempool *sess_mpool,
56         uint8_t dev_id,
57         int driver_id,
58         char *test_msg)
59 {
60         struct rte_mbuf *ibuf = NULL;
61         struct rte_mbuf *obuf = NULL;
62         struct rte_mbuf *iobuf;
63         struct rte_crypto_sym_xform *cipher_xform = NULL;
64         struct rte_crypto_sym_xform *auth_xform = NULL;
65         struct rte_crypto_sym_xform *init_xform = NULL;
66         struct rte_crypto_sym_op *sym_op = NULL;
67         struct rte_crypto_op *op = NULL;
68         struct rte_cryptodev_info dev_info;
69         struct rte_cryptodev_sym_session *sess = NULL;
70
71         int status = TEST_SUCCESS;
72         const struct blockcipher_test_data *tdata = t->test_data;
73         uint8_t cipher_key[tdata->cipher_key.len];
74         uint8_t auth_key[tdata->auth_key.len];
75         uint32_t buf_len = tdata->ciphertext.len;
76         uint32_t digest_len = 0;
77         char *buf_p = NULL;
78         uint8_t src_pattern = 0xa5;
79         uint8_t dst_pattern = 0xb6;
80         uint8_t tmp_src_buf[MBUF_SIZE];
81         uint8_t tmp_dst_buf[MBUF_SIZE];
82
83         int openssl_pmd = rte_cryptodev_driver_id_get(
84                         RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
85         int scheduler_pmd = rte_cryptodev_driver_id_get(
86                         RTE_STR(CRYPTODEV_NAME_SCHEDULER_PMD));
87         int armv8_pmd = rte_cryptodev_driver_id_get(
88                         RTE_STR(CRYPTODEV_NAME_ARMV8_PMD));
89         int aesni_mb_pmd = rte_cryptodev_driver_id_get(
90                         RTE_STR(CRYPTODEV_NAME_AESNI_MB_PMD));
91         int qat_pmd = rte_cryptodev_driver_id_get(
92                         RTE_STR(CRYPTODEV_NAME_QAT_SYM_PMD));
93         int dpaa2_sec_pmd = rte_cryptodev_driver_id_get(
94                         RTE_STR(CRYPTODEV_NAME_DPAA2_SEC_PMD));
95
96         int nb_segs = 1;
97
98         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SG) {
99                 rte_cryptodev_info_get(dev_id, &dev_info);
100                 if (!(dev_info.feature_flags &
101                                 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER)) {
102                         printf("Device doesn't support scatter-gather. "
103                                         "Test Skipped.\n");
104                         return 0;
105                 }
106                 nb_segs = 3;
107         }
108
109         if (tdata->cipher_key.len)
110                 memcpy(cipher_key, tdata->cipher_key.data,
111                         tdata->cipher_key.len);
112         if (tdata->auth_key.len)
113                 memcpy(auth_key, tdata->auth_key.data,
114                         tdata->auth_key.len);
115
116         if (driver_id == dpaa2_sec_pmd ||
117                         driver_id == qat_pmd ||
118                         driver_id == openssl_pmd ||
119                         driver_id == armv8_pmd) { /* Fall through */
120                 digest_len = tdata->digest.len;
121         } else if (driver_id == aesni_mb_pmd ||
122                         driver_id == scheduler_pmd) {
123                 digest_len = tdata->digest.truncated_len;
124         } else {
125                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
126                         "line %u FAILED: %s",
127                         __LINE__, "Unsupported PMD type");
128                 status = TEST_FAILED;
129                 goto error_exit;
130         }
131
132         /* preparing data */
133         if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH)
134                 buf_len += digest_len;
135
136         /* for contiguous mbuf, nb_segs is 1 */
137         ibuf = create_segmented_mbuf(mbuf_pool,
138                         tdata->ciphertext.len, nb_segs, src_pattern);
139         if (ibuf == NULL) {
140                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
141                         "line %u FAILED: %s",
142                         __LINE__, "Cannot create source mbuf");
143                 status = TEST_FAILED;
144                 goto error_exit;
145         }
146
147         /* only encryption requires plaintext.data input,
148          * decryption/(digest gen)/(digest verify) use ciphertext.data
149          * to be computed
150          */
151         if (t->op_mask & BLOCKCIPHER_TEST_OP_ENCRYPT)
152                 pktmbuf_write(ibuf, 0, tdata->plaintext.len,
153                                 tdata->plaintext.data);
154         else
155                 pktmbuf_write(ibuf, 0, tdata->ciphertext.len,
156                                 tdata->ciphertext.data);
157
158         buf_p = rte_pktmbuf_append(ibuf, digest_len);
159         if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_VERIFY)
160                 rte_memcpy(buf_p, tdata->digest.data, digest_len);
161         else
162                 memset(buf_p, 0, digest_len);
163
164         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP) {
165                 obuf = rte_pktmbuf_alloc(mbuf_pool);
166                 if (!obuf) {
167                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
168                                 "FAILED: %s", __LINE__,
169                                 "Allocation of rte_mbuf failed");
170                         status = TEST_FAILED;
171                         goto error_exit;
172                 }
173                 memset(obuf->buf_addr, dst_pattern, obuf->buf_len);
174
175                 buf_p = rte_pktmbuf_append(obuf, buf_len);
176                 if (!buf_p) {
177                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
178                                 "FAILED: %s", __LINE__,
179                                 "No room to append mbuf");
180                         status = TEST_FAILED;
181                         goto error_exit;
182                 }
183                 memset(buf_p, 0, buf_len);
184         }
185
186         /* Generate Crypto op data structure */
187         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
188         if (!op) {
189                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
190                         "line %u FAILED: %s",
191                         __LINE__, "Failed to allocate symmetric crypto "
192                         "operation struct");
193                 status = TEST_FAILED;
194                 goto error_exit;
195         }
196
197         sym_op = op->sym;
198
199         sym_op->m_src = ibuf;
200
201         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP) {
202                 sym_op->m_dst = obuf;
203                 iobuf = obuf;
204         } else {
205                 sym_op->m_dst = NULL;
206                 iobuf = ibuf;
207         }
208
209         /* sessionless op requires allocate xform using
210          * rte_crypto_op_sym_xforms_alloc(), otherwise rte_zmalloc()
211          * is used
212          */
213         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS) {
214                 uint32_t n_xforms = 0;
215
216                 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER)
217                         n_xforms++;
218                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH)
219                         n_xforms++;
220
221                 if (rte_crypto_op_sym_xforms_alloc(op, n_xforms)
222                         == NULL) {
223                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
224                                 "FAILED: %s", __LINE__, "Failed to "
225                                 "allocate space for crypto transforms");
226                         status = TEST_FAILED;
227                         goto error_exit;
228                 }
229         } else {
230                 cipher_xform = rte_zmalloc(NULL,
231                         sizeof(struct rte_crypto_sym_xform), 0);
232
233                 auth_xform = rte_zmalloc(NULL,
234                         sizeof(struct rte_crypto_sym_xform), 0);
235
236                 if (!cipher_xform || !auth_xform) {
237                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
238                                 "FAILED: %s", __LINE__, "Failed to "
239                                 "allocate memory for crypto transforms");
240                         status = TEST_FAILED;
241                         goto error_exit;
242                 }
243         }
244
245         /* preparing xform, for sessioned op, init_xform is initialized
246          * here and later as param in rte_cryptodev_sym_session_create() call
247          */
248         if (t->op_mask == BLOCKCIPHER_TEST_OP_ENC_AUTH_GEN) {
249                 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS) {
250                         cipher_xform = op->sym->xform;
251                         auth_xform = cipher_xform->next;
252                         auth_xform->next = NULL;
253                 } else {
254                         cipher_xform->next = auth_xform;
255                         auth_xform->next = NULL;
256                         init_xform = cipher_xform;
257                 }
258         } else if (t->op_mask == BLOCKCIPHER_TEST_OP_AUTH_VERIFY_DEC) {
259                 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS) {
260                         auth_xform = op->sym->xform;
261                         cipher_xform = auth_xform->next;
262                         cipher_xform->next = NULL;
263                 } else {
264                         auth_xform->next = cipher_xform;
265                         cipher_xform->next = NULL;
266                         init_xform = auth_xform;
267                 }
268         } else if ((t->op_mask == BLOCKCIPHER_TEST_OP_ENCRYPT) ||
269                         (t->op_mask == BLOCKCIPHER_TEST_OP_DECRYPT)) {
270                 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS)
271                         cipher_xform = op->sym->xform;
272                 else
273                         init_xform = cipher_xform;
274                 cipher_xform->next = NULL;
275         } else if ((t->op_mask == BLOCKCIPHER_TEST_OP_AUTH_GEN) ||
276                         (t->op_mask == BLOCKCIPHER_TEST_OP_AUTH_VERIFY)) {
277                 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS)
278                         auth_xform = op->sym->xform;
279                 else
280                         init_xform = auth_xform;
281                 auth_xform->next = NULL;
282         } else {
283                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
284                         "line %u FAILED: %s",
285                         __LINE__, "Unrecognized operation");
286                 status = TEST_FAILED;
287                 goto error_exit;
288         }
289
290         /*configure xforms & sym_op cipher and auth data*/
291         if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
292                 cipher_xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
293                 cipher_xform->cipher.algo = tdata->crypto_algo;
294                 if (t->op_mask & BLOCKCIPHER_TEST_OP_ENCRYPT)
295                         cipher_xform->cipher.op =
296                                 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
297                 else
298                         cipher_xform->cipher.op =
299                                 RTE_CRYPTO_CIPHER_OP_DECRYPT;
300                 cipher_xform->cipher.key.data = cipher_key;
301                 cipher_xform->cipher.key.length = tdata->cipher_key.len;
302                 cipher_xform->cipher.iv.offset = IV_OFFSET;
303                 cipher_xform->cipher.iv.length = tdata->iv.len;
304
305                 sym_op->cipher.data.offset = 0;
306                 sym_op->cipher.data.length = tdata->ciphertext.len;
307                 rte_memcpy(rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET),
308                                 tdata->iv.data,
309                                 tdata->iv.len);
310         }
311
312         if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH) {
313                 uint32_t digest_offset = tdata->ciphertext.len;
314
315                 auth_xform->type = RTE_CRYPTO_SYM_XFORM_AUTH;
316                 auth_xform->auth.algo = tdata->auth_algo;
317                 auth_xform->auth.key.length = tdata->auth_key.len;
318                 auth_xform->auth.key.data = auth_key;
319                 auth_xform->auth.digest_length = digest_len;
320
321                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_GEN) {
322                         auth_xform->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
323                         sym_op->auth.digest.data = pktmbuf_mtod_offset
324                                 (iobuf, digest_offset);
325                         sym_op->auth.digest.phys_addr =
326                                 pktmbuf_mtophys_offset(iobuf,
327                                         digest_offset);
328                 } else {
329                         auth_xform->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
330                         sym_op->auth.digest.data = pktmbuf_mtod_offset
331                                 (sym_op->m_src, digest_offset);
332                         sym_op->auth.digest.phys_addr =
333                                 pktmbuf_mtophys_offset(sym_op->m_src,
334                                         digest_offset);
335                 }
336
337                 sym_op->auth.data.offset = 0;
338                 sym_op->auth.data.length = tdata->ciphertext.len;
339         }
340
341         /* create session for sessioned op */
342         if (!(t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS)) {
343                 sess = rte_cryptodev_sym_session_create(sess_mpool);
344
345                 rte_cryptodev_sym_session_init(dev_id, sess, init_xform,
346                                 sess_mpool);
347                 if (!sess) {
348                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
349                                 "FAILED: %s", __LINE__,
350                                 "Session creation failed");
351                         status = TEST_FAILED;
352                         goto error_exit;
353                 }
354
355                 /* attach symmetric crypto session to crypto operations */
356                 rte_crypto_op_attach_sym_session(op, sess);
357         }
358
359         TEST_HEXDUMP(stdout, "m_src(before):",
360                         sym_op->m_src->buf_addr, sym_op->m_src->buf_len);
361         rte_memcpy(tmp_src_buf, sym_op->m_src->buf_addr,
362                                                 sym_op->m_src->buf_len);
363         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP) {
364                 TEST_HEXDUMP(stdout, "m_dst(before):",
365                         sym_op->m_dst->buf_addr, sym_op->m_dst->buf_len);
366                 rte_memcpy(tmp_dst_buf, sym_op->m_dst->buf_addr,
367                                                 sym_op->m_dst->buf_len);
368         }
369
370         /* Process crypto operation */
371         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
372                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
373                         "line %u FAILED: %s",
374                         __LINE__, "Error sending packet for encryption");
375                 status = TEST_FAILED;
376                 goto error_exit;
377         }
378
379         op = NULL;
380
381         while (rte_cryptodev_dequeue_burst(dev_id, 0, &op, 1) == 0)
382                 rte_pause();
383
384         if (!op) {
385                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
386                         "line %u FAILED: %s",
387                         __LINE__, "Failed to process sym crypto op");
388                 status = TEST_FAILED;
389                 goto error_exit;
390         }
391
392         TEST_HEXDUMP(stdout, "m_src(after):",
393                         sym_op->m_src->buf_addr, sym_op->m_src->buf_len);
394         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP)
395                 TEST_HEXDUMP(stdout, "m_dst(after):",
396                         sym_op->m_dst->buf_addr, sym_op->m_dst->buf_len);
397
398         /* Verify results */
399         if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
400                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_VERIFY)
401                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
402                                 "FAILED: Digest verification failed "
403                                 "(0x%X)", __LINE__, op->status);
404                 else
405                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
406                                 "FAILED: Digest verification failed "
407                                 "(0x%X)", __LINE__, op->status);
408                 status = TEST_FAILED;
409                 goto error_exit;
410         }
411
412         if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
413                 uint8_t buffer[2048];
414                 const uint8_t *compare_ref;
415                 uint32_t compare_len;
416
417                 if (t->op_mask & BLOCKCIPHER_TEST_OP_ENCRYPT) {
418                         compare_ref = tdata->ciphertext.data;
419                         compare_len = tdata->ciphertext.len;
420                 } else {
421                         compare_ref = tdata->plaintext.data;
422                         compare_len = tdata->plaintext.len;
423                 }
424
425                 if (memcmp(rte_pktmbuf_read(iobuf, 0, compare_len,
426                                 buffer), compare_ref, compare_len)) {
427                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
428                                 "FAILED: %s", __LINE__,
429                                 "Crypto data not as expected");
430                         status = TEST_FAILED;
431                         goto error_exit;
432                 }
433         }
434
435         if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_GEN) {
436                 uint8_t *auth_res = pktmbuf_mtod_offset(iobuf,
437                                         tdata->ciphertext.len);
438
439                 if (memcmp(auth_res, tdata->digest.data, digest_len)) {
440                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
441                                 "FAILED: %s", __LINE__, "Generated "
442                                 "digest data not as expected");
443                         status = TEST_FAILED;
444                         goto error_exit;
445                 }
446         }
447
448         /* The only parts that should have changed in the buffer are
449          * plaintext/ciphertext and digest.
450          * In OOP only the dest buffer should change.
451          */
452         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP) {
453                 struct rte_mbuf *mbuf;
454                 uint8_t value;
455                 uint32_t head_unchanged_len = 0, changed_len = 0;
456                 uint32_t i;
457
458                 mbuf = sym_op->m_src;
459                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_VERIFY) {
460                         /* white-box test: PMDs use some of the
461                          * tailroom as temp storage in verify case
462                          */
463                         head_unchanged_len = rte_pktmbuf_headroom(mbuf)
464                                         + rte_pktmbuf_data_len(mbuf);
465                         changed_len = digest_len;
466                 } else {
467                         head_unchanged_len = mbuf->buf_len;
468                         changed_len = 0;
469                 }
470
471                 for (i = 0; i < mbuf->buf_len; i++) {
472                         if (i == head_unchanged_len)
473                                 i += changed_len;
474                         value = *((uint8_t *)(mbuf->buf_addr)+i);
475                         if (value != tmp_src_buf[i]) {
476                                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
477         "line %u FAILED: OOP src outer mbuf data (0x%x) not as expected (0x%x)",
478                                         __LINE__, value, tmp_src_buf[i]);
479                                 status = TEST_FAILED;
480                                 goto error_exit;
481                         }
482                 }
483
484                 mbuf = sym_op->m_dst;
485                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH) {
486                         head_unchanged_len = rte_pktmbuf_headroom(mbuf) +
487                                                 sym_op->auth.data.offset;
488                         changed_len = sym_op->auth.data.length;
489                         if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_GEN)
490                                 changed_len += digest_len;
491                 } else {
492                         /* cipher-only */
493                         head_unchanged_len = rte_pktmbuf_headroom(mbuf) +
494                                         sym_op->cipher.data.offset;
495                         changed_len = sym_op->cipher.data.length;
496                 }
497
498                 for (i = 0; i < mbuf->buf_len; i++) {
499                         if (i == head_unchanged_len)
500                                 i += changed_len;
501                         value = *((uint8_t *)(mbuf->buf_addr)+i);
502                         if (value != tmp_dst_buf[i]) {
503                                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
504                                 "line %u FAILED: OOP dst outer mbuf data "
505                                 "(0x%x) not as expected (0x%x)",
506                                 __LINE__, value, tmp_dst_buf[i]);
507                                 status = TEST_FAILED;
508                                 goto error_exit;
509                         }
510                 }
511         } else {
512                 /* In-place operation */
513                 struct rte_mbuf *mbuf;
514                 uint8_t value;
515                 uint32_t head_unchanged_len = 0, changed_len = 0;
516                 uint32_t i;
517
518                 mbuf = sym_op->m_src;
519                 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
520                         head_unchanged_len = rte_pktmbuf_headroom(mbuf) +
521                                         sym_op->cipher.data.offset;
522                         changed_len = sym_op->cipher.data.length;
523                 } else {
524                         /* auth-only */
525                         head_unchanged_len = rte_pktmbuf_headroom(mbuf) +
526                                         sym_op->auth.data.offset +
527                                         sym_op->auth.data.length;
528                         changed_len = 0;
529                 }
530
531                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_GEN)
532                         changed_len += digest_len;
533
534                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_VERIFY) {
535                         /* white-box test: PMDs use some of the
536                          * tailroom as temp storage in verify case
537                          */
538                         if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
539                                 /* This is simplified, not checking digest*/
540                                 changed_len += digest_len*2;
541                         } else {
542                                 head_unchanged_len += digest_len;
543                                 changed_len += digest_len;
544                         }
545                 }
546
547                 for (i = 0; i < mbuf->buf_len; i++) {
548                         if (i == head_unchanged_len)
549                                 i += changed_len;
550                         value = *((uint8_t *)(mbuf->buf_addr)+i);
551                         if (value != tmp_src_buf[i]) {
552                                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
553                                 "line %u FAILED: outer mbuf data (0x%x) "
554                                 "not as expected (0x%x)",
555                                 __LINE__, value, tmp_src_buf[i]);
556                                 status = TEST_FAILED;
557                                 goto error_exit;
558                         }
559                 }
560         }
561
562         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "PASS");
563
564 error_exit:
565         if (!(t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS)) {
566                 if (sess) {
567                         rte_cryptodev_sym_session_clear(dev_id, sess);
568                         rte_cryptodev_sym_session_free(sess);
569                 }
570                 if (cipher_xform)
571                         rte_free(cipher_xform);
572                 if (auth_xform)
573                         rte_free(auth_xform);
574         }
575
576         if (op)
577                 rte_crypto_op_free(op);
578
579         if (obuf)
580                 rte_pktmbuf_free(obuf);
581
582         if (ibuf)
583                 rte_pktmbuf_free(ibuf);
584
585         return status;
586 }
587
588 int
589 test_blockcipher_all_tests(struct rte_mempool *mbuf_pool,
590         struct rte_mempool *op_mpool,
591         struct rte_mempool *sess_mpool,
592         uint8_t dev_id,
593         int driver_id,
594         enum blockcipher_test_type test_type)
595 {
596         int status, overall_status = TEST_SUCCESS;
597         uint32_t i, test_index = 0;
598         char test_msg[BLOCKCIPHER_TEST_MSG_LEN + 1];
599         uint32_t n_test_cases = 0;
600         uint32_t target_pmd_mask = 0;
601         const struct blockcipher_test_case *tcs = NULL;
602
603         int openssl_pmd = rte_cryptodev_driver_id_get(
604                         RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
605         int dpaa2_pmd = rte_cryptodev_driver_id_get(
606                         RTE_STR(CRYPTODEV_NAME_DPAA2_SEC_PMD));
607         int scheduler_pmd = rte_cryptodev_driver_id_get(
608                         RTE_STR(CRYPTODEV_NAME_SCHEDULER_PMD));
609         int armv8_pmd = rte_cryptodev_driver_id_get(
610                         RTE_STR(CRYPTODEV_NAME_ARMV8_PMD));
611         int aesni_mb_pmd = rte_cryptodev_driver_id_get(
612                         RTE_STR(CRYPTODEV_NAME_AESNI_MB_PMD));
613         int qat_pmd = rte_cryptodev_driver_id_get(
614                         RTE_STR(CRYPTODEV_NAME_QAT_SYM_PMD));
615
616         switch (test_type) {
617         case BLKCIPHER_AES_CHAIN_TYPE:
618                 n_test_cases = sizeof(aes_chain_test_cases) /
619                 sizeof(aes_chain_test_cases[0]);
620                 tcs = aes_chain_test_cases;
621                 break;
622         case BLKCIPHER_AES_CIPHERONLY_TYPE:
623                 n_test_cases = sizeof(aes_cipheronly_test_cases) /
624                 sizeof(aes_cipheronly_test_cases[0]);
625                 tcs = aes_cipheronly_test_cases;
626                 break;
627         case BLKCIPHER_AES_DOCSIS_TYPE:
628                 n_test_cases = sizeof(aes_docsis_test_cases) /
629                 sizeof(aes_docsis_test_cases[0]);
630                 tcs = aes_docsis_test_cases;
631                 break;
632         case BLKCIPHER_3DES_CHAIN_TYPE:
633                 n_test_cases = sizeof(triple_des_chain_test_cases) /
634                 sizeof(triple_des_chain_test_cases[0]);
635                 tcs = triple_des_chain_test_cases;
636                 break;
637         case BLKCIPHER_3DES_CIPHERONLY_TYPE:
638                 n_test_cases = sizeof(triple_des_cipheronly_test_cases) /
639                 sizeof(triple_des_cipheronly_test_cases[0]);
640                 tcs = triple_des_cipheronly_test_cases;
641                 break;
642         case BLKCIPHER_DES_CIPHERONLY_TYPE:
643                 n_test_cases = sizeof(des_cipheronly_test_cases) /
644                 sizeof(des_cipheronly_test_cases[0]);
645                 tcs = des_cipheronly_test_cases;
646                 break;
647         case BLKCIPHER_DES_DOCSIS_TYPE:
648                 n_test_cases = sizeof(des_docsis_test_cases) /
649                 sizeof(des_docsis_test_cases[0]);
650                 tcs = des_docsis_test_cases;
651                 break;
652         case BLKCIPHER_AUTHONLY_TYPE:
653                 n_test_cases = sizeof(hash_test_cases) /
654                 sizeof(hash_test_cases[0]);
655                 tcs = hash_test_cases;
656                 break;
657         default:
658                 break;
659         }
660
661         if (driver_id == aesni_mb_pmd)
662                 target_pmd_mask = BLOCKCIPHER_TEST_TARGET_PMD_MB;
663         else if (driver_id == qat_pmd)
664                 target_pmd_mask = BLOCKCIPHER_TEST_TARGET_PMD_QAT;
665         else if (driver_id == openssl_pmd)
666                 target_pmd_mask = BLOCKCIPHER_TEST_TARGET_PMD_OPENSSL;
667         else if (driver_id == armv8_pmd)
668                 target_pmd_mask = BLOCKCIPHER_TEST_TARGET_PMD_ARMV8;
669         else if (driver_id == scheduler_pmd)
670                 target_pmd_mask = BLOCKCIPHER_TEST_TARGET_PMD_SCHEDULER;
671         else if (driver_id == dpaa2_pmd)
672                 target_pmd_mask = BLOCKCIPHER_TEST_TARGET_PMD_DPAA2_SEC;
673         else
674                 TEST_ASSERT(0, "Unrecognized cryptodev type");
675
676         for (i = 0; i < n_test_cases; i++) {
677                 const struct blockcipher_test_case *tc = &tcs[i];
678
679                 if (!(tc->pmd_mask & target_pmd_mask))
680                         continue;
681
682                 status = test_blockcipher_one_case(tc, mbuf_pool, op_mpool,
683                         sess_mpool, dev_id, driver_id, test_msg);
684
685                 printf("  %u) TestCase %s %s\n", test_index ++,
686                         tc->test_descr, test_msg);
687
688                 if (status != TEST_SUCCESS) {
689                         if (overall_status == TEST_SUCCESS)
690                                 overall_status = status;
691
692                         if (tc->feature_mask & BLOCKCIPHER_TEST_FEATURE_STOPPER)
693                                 break;
694                 }
695         }
696
697         return overall_status;
698 }