app/test: add integrity check for crypto mbuf data
[dpdk.git] / app / test / test_cryptodev_blockcipher.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *       * Redistributions of source code must retain the above copyright
11  *         notice, this list of conditions and the following disclaimer.
12  *       * Redistributions in binary form must reproduce the above copyright
13  *         notice, this list of conditions and the following disclaimer in
14  *         the documentation and/or other materials provided with the
15  *         distribution.
16  *       * Neither the name of Intel Corporation nor the names of its
17  *         contributors may be used to endorse or promote products derived
18  *         from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <rte_common.h>
34 #include <rte_hexdump.h>
35 #include <rte_mbuf.h>
36 #include <rte_malloc.h>
37 #include <rte_memcpy.h>
38
39 #include <rte_crypto.h>
40 #include <rte_cryptodev.h>
41 #include <rte_cryptodev_pmd.h>
42
43 #include "test.h"
44 #include "test_cryptodev_blockcipher.h"
45 #include "test_cryptodev_aes_test_vectors.h"
46 #include "test_cryptodev_des_test_vectors.h"
47 #include "test_cryptodev_hash_test_vectors.h"
48 #include "test_cryptodev.h"
49
50 static int
51 test_blockcipher_one_case(const struct blockcipher_test_case *t,
52         struct rte_mempool *mbuf_pool,
53         struct rte_mempool *op_mpool,
54         uint8_t dev_id,
55         enum rte_cryptodev_type cryptodev_type,
56         char *test_msg)
57 {
58         struct rte_mbuf *ibuf = NULL;
59         struct rte_mbuf *obuf = NULL;
60         struct rte_mbuf *iobuf;
61         struct rte_crypto_sym_xform *cipher_xform = NULL;
62         struct rte_crypto_sym_xform *auth_xform = NULL;
63         struct rte_crypto_sym_xform *init_xform = NULL;
64         struct rte_crypto_sym_op *sym_op = NULL;
65         struct rte_crypto_op *op = NULL;
66         struct rte_cryptodev_sym_session *sess = NULL;
67
68         int status = TEST_SUCCESS;
69         const struct blockcipher_test_data *tdata = t->test_data;
70         uint8_t cipher_key[tdata->cipher_key.len];
71         uint8_t auth_key[tdata->auth_key.len];
72         uint32_t buf_len = tdata->ciphertext.len;
73         uint32_t digest_len = 0;
74         char *buf_p = NULL;
75         uint8_t src_pattern = 0xa5;
76         uint8_t dst_pattern = 0xb6;
77         uint8_t tmp_src_buf[MBUF_SIZE];
78         uint8_t tmp_dst_buf[MBUF_SIZE];
79
80         if (tdata->cipher_key.len)
81                 memcpy(cipher_key, tdata->cipher_key.data,
82                         tdata->cipher_key.len);
83         if (tdata->auth_key.len)
84                 memcpy(auth_key, tdata->auth_key.data,
85                         tdata->auth_key.len);
86
87         switch (cryptodev_type) {
88         case RTE_CRYPTODEV_QAT_SYM_PMD:
89         case RTE_CRYPTODEV_OPENSSL_PMD:
90                 digest_len = tdata->digest.len;
91                 break;
92         case RTE_CRYPTODEV_AESNI_MB_PMD:
93                 digest_len = tdata->digest.truncated_len;
94                 break;
95         default:
96                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
97                         "line %u FAILED: %s",
98                         __LINE__, "Unsupported PMD type");
99                 status = TEST_FAILED;
100                 goto error_exit;
101         }
102
103         /* preparing data */
104         ibuf = rte_pktmbuf_alloc(mbuf_pool);
105         if (!ibuf) {
106                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
107                         "line %u FAILED: %s",
108                         __LINE__, "Allocation of rte_mbuf failed");
109                 status = TEST_FAILED;
110                 goto error_exit;
111         }
112         memset(ibuf->buf_addr, src_pattern, ibuf->buf_len);
113
114         if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER)
115                 buf_len += tdata->iv.len;
116         if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH)
117                 buf_len += digest_len;
118
119         buf_p = rte_pktmbuf_append(ibuf, buf_len);
120         if (!buf_p) {
121                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
122                         "line %u FAILED: %s",
123                         __LINE__, "No room to append mbuf");
124                 status = TEST_FAILED;
125                 goto error_exit;
126         }
127
128         if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
129                 rte_memcpy(buf_p, tdata->iv.data, tdata->iv.len);
130                 buf_p += tdata->iv.len;
131         }
132
133         /* only encryption requires plaintext.data input,
134          * decryption/(digest gen)/(digest verify) use ciphertext.data
135          * to be computed
136          */
137         if (t->op_mask & BLOCKCIPHER_TEST_OP_ENCRYPT) {
138                 rte_memcpy(buf_p, tdata->plaintext.data,
139                         tdata->plaintext.len);
140                 buf_p += tdata->plaintext.len;
141         } else {
142                 rte_memcpy(buf_p, tdata->ciphertext.data,
143                         tdata->ciphertext.len);
144                 buf_p += tdata->ciphertext.len;
145         }
146
147         if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_VERIFY)
148                 rte_memcpy(buf_p, tdata->digest.data, digest_len);
149         else
150                 memset(buf_p, 0, digest_len);
151
152         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP) {
153                 obuf = rte_pktmbuf_alloc(mbuf_pool);
154                 if (!obuf) {
155                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
156                                 "FAILED: %s", __LINE__,
157                                 "Allocation of rte_mbuf failed");
158                         status = TEST_FAILED;
159                         goto error_exit;
160                 }
161                 memset(obuf->buf_addr, dst_pattern, obuf->buf_len);
162
163                 buf_p = rte_pktmbuf_append(obuf, buf_len);
164                 if (!buf_p) {
165                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
166                                 "FAILED: %s", __LINE__,
167                                 "No room to append mbuf");
168                         status = TEST_FAILED;
169                         goto error_exit;
170                 }
171                 memset(buf_p, 0, buf_len);
172         }
173
174         /* Generate Crypto op data structure */
175         op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
176         if (!op) {
177                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
178                         "line %u FAILED: %s",
179                         __LINE__, "Failed to allocate symmetric crypto "
180                         "operation struct");
181                 status = TEST_FAILED;
182                 goto error_exit;
183         }
184
185         sym_op = op->sym;
186
187         sym_op->m_src = ibuf;
188
189         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP) {
190                 sym_op->m_dst = obuf;
191                 iobuf = obuf;
192         } else {
193                 sym_op->m_dst = NULL;
194                 iobuf = ibuf;
195         }
196
197         /* sessionless op requires allocate xform using
198          * rte_crypto_op_sym_xforms_alloc(), otherwise rte_zmalloc()
199          * is used
200          */
201         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS) {
202                 uint32_t n_xforms = 0;
203
204                 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER)
205                         n_xforms++;
206                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH)
207                         n_xforms++;
208
209                 if (rte_crypto_op_sym_xforms_alloc(op, n_xforms)
210                         == NULL) {
211                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
212                                 "FAILED: %s", __LINE__, "Failed to "
213                                 "allocate space for crypto transforms");
214                         status = TEST_FAILED;
215                         goto error_exit;
216                 }
217         } else {
218                 cipher_xform = rte_zmalloc(NULL,
219                         sizeof(struct rte_crypto_sym_xform), 0);
220
221                 auth_xform = rte_zmalloc(NULL,
222                         sizeof(struct rte_crypto_sym_xform), 0);
223
224                 if (!cipher_xform || !auth_xform) {
225                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
226                                 "FAILED: %s", __LINE__, "Failed to "
227                                 "allocate memory for crypto transforms");
228                         status = TEST_FAILED;
229                         goto error_exit;
230                 }
231         }
232
233         /* preparing xform, for sessioned op, init_xform is initialized
234          * here and later as param in rte_cryptodev_sym_session_create() call
235          */
236         if (t->op_mask == BLOCKCIPHER_TEST_OP_ENC_AUTH_GEN) {
237                 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS) {
238                         cipher_xform = op->sym->xform;
239                         auth_xform = cipher_xform->next;
240                         auth_xform->next = NULL;
241                 } else {
242                         cipher_xform->next = auth_xform;
243                         auth_xform->next = NULL;
244                         init_xform = cipher_xform;
245                 }
246         } else if (t->op_mask == BLOCKCIPHER_TEST_OP_AUTH_VERIFY_DEC) {
247                 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS) {
248                         auth_xform = op->sym->xform;
249                         cipher_xform = auth_xform->next;
250                         cipher_xform->next = NULL;
251                 } else {
252                         auth_xform->next = cipher_xform;
253                         cipher_xform->next = NULL;
254                         init_xform = auth_xform;
255                 }
256         } else if ((t->op_mask == BLOCKCIPHER_TEST_OP_ENCRYPT) ||
257                         (t->op_mask == BLOCKCIPHER_TEST_OP_DECRYPT)) {
258                 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS)
259                         cipher_xform = op->sym->xform;
260                 else
261                         init_xform = cipher_xform;
262                 cipher_xform->next = NULL;
263         } else if ((t->op_mask == BLOCKCIPHER_TEST_OP_AUTH_GEN) ||
264                         (t->op_mask == BLOCKCIPHER_TEST_OP_AUTH_VERIFY)) {
265                 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS)
266                         auth_xform = op->sym->xform;
267                 else
268                         init_xform = auth_xform;
269                 auth_xform->next = NULL;
270         } else {
271                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
272                         "line %u FAILED: %s",
273                         __LINE__, "Unrecognized operation");
274                 status = TEST_FAILED;
275                 goto error_exit;
276         }
277
278         /*configure xforms & sym_op cipher and auth data*/
279         if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
280                 cipher_xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
281                 cipher_xform->cipher.algo = tdata->crypto_algo;
282                 if (t->op_mask & BLOCKCIPHER_TEST_OP_ENCRYPT)
283                         cipher_xform->cipher.op =
284                                 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
285                 else
286                         cipher_xform->cipher.op =
287                                 RTE_CRYPTO_CIPHER_OP_DECRYPT;
288                 cipher_xform->cipher.key.data = cipher_key;
289                 cipher_xform->cipher.key.length = tdata->cipher_key.len;
290
291                 sym_op->cipher.data.offset = tdata->iv.len;
292                 sym_op->cipher.data.length = tdata->ciphertext.len;
293                 sym_op->cipher.iv.data = rte_pktmbuf_mtod(sym_op->m_src,
294                         uint8_t *);
295                 sym_op->cipher.iv.length = tdata->iv.len;
296                 sym_op->cipher.iv.phys_addr = rte_pktmbuf_mtophys(
297                         sym_op->m_src);
298         }
299
300         if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH) {
301                 uint32_t auth_data_offset = 0;
302                 uint32_t digest_offset = tdata->ciphertext.len;
303
304                 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
305                         digest_offset += tdata->iv.len;
306                         auth_data_offset += tdata->iv.len;
307                 }
308
309                 auth_xform->type = RTE_CRYPTO_SYM_XFORM_AUTH;
310                 auth_xform->auth.algo = tdata->auth_algo;
311                 auth_xform->auth.key.length = tdata->auth_key.len;
312                 auth_xform->auth.key.data = auth_key;
313                 auth_xform->auth.digest_length = digest_len;
314
315                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_GEN) {
316                         auth_xform->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
317                         sym_op->auth.digest.data = rte_pktmbuf_mtod_offset
318                                 (iobuf, uint8_t *, digest_offset);
319                         sym_op->auth.digest.phys_addr =
320                                 rte_pktmbuf_mtophys_offset(iobuf,
321                                         digest_offset);
322                 } else {
323                         auth_xform->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
324                         sym_op->auth.digest.data = rte_pktmbuf_mtod_offset
325                                 (sym_op->m_src, uint8_t *, digest_offset);
326                         sym_op->auth.digest.phys_addr =
327                                 rte_pktmbuf_mtophys_offset(sym_op->m_src,
328                                         digest_offset);
329                 }
330
331                 sym_op->auth.data.offset = auth_data_offset;
332                 sym_op->auth.data.length = tdata->ciphertext.len;
333                 sym_op->auth.digest.length = digest_len;
334         }
335
336         /* create session for sessioned op */
337         if (!(t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS)) {
338                 sess = rte_cryptodev_sym_session_create(dev_id,
339                         init_xform);
340                 if (!sess) {
341                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
342                                 "FAILED: %s", __LINE__,
343                                 "Session creation failed");
344                         status = TEST_FAILED;
345                         goto error_exit;
346                 }
347
348                 /* attach symmetric crypto session to crypto operations */
349                 rte_crypto_op_attach_sym_session(op, sess);
350         }
351
352         TEST_HEXDUMP(stdout, "m_src(before):",
353                         sym_op->m_src->buf_addr, sym_op->m_src->buf_len);
354         rte_memcpy(tmp_src_buf, sym_op->m_src->buf_addr,
355                                                 sym_op->m_src->buf_len);
356         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP) {
357                 TEST_HEXDUMP(stdout, "m_dst(before):",
358                         sym_op->m_dst->buf_addr, sym_op->m_dst->buf_len);
359                 rte_memcpy(tmp_dst_buf, sym_op->m_dst->buf_addr,
360                                                 sym_op->m_dst->buf_len);
361         }
362
363         /* Process crypto operation */
364         if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
365                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
366                         "line %u FAILED: %s",
367                         __LINE__, "Error sending packet for encryption");
368                 status = TEST_FAILED;
369                 goto error_exit;
370         }
371
372         op = NULL;
373
374         while (rte_cryptodev_dequeue_burst(dev_id, 0, &op, 1) == 0)
375                 rte_pause();
376
377         if (!op) {
378                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
379                         "line %u FAILED: %s",
380                         __LINE__, "Failed to process sym crypto op");
381                 status = TEST_FAILED;
382                 goto error_exit;
383         }
384
385         TEST_HEXDUMP(stdout, "m_src(after):",
386                         sym_op->m_src->buf_addr, sym_op->m_src->buf_len);
387         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP)
388                 TEST_HEXDUMP(stdout, "m_dst(after):",
389                         sym_op->m_dst->buf_addr, sym_op->m_dst->buf_len);
390
391         /* Verify results */
392         if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
393                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_VERIFY)
394                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
395                                 "FAILED: Digest verification failed "
396                                 "(0x%X)", __LINE__, op->status);
397                 else
398                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
399                                 "FAILED: Digest verification failed "
400                                 "(0x%X)", __LINE__, op->status);
401                 status = TEST_FAILED;
402                 goto error_exit;
403         }
404
405         if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
406                 uint8_t *crypto_res;
407                 const uint8_t *compare_ref;
408                 uint32_t compare_len;
409
410                 crypto_res = rte_pktmbuf_mtod_offset(iobuf, uint8_t *,
411                         tdata->iv.len);
412
413                 if (t->op_mask & BLOCKCIPHER_TEST_OP_ENCRYPT) {
414                         compare_ref = tdata->ciphertext.data;
415                         compare_len = tdata->ciphertext.len;
416                 } else {
417                         compare_ref = tdata->plaintext.data;
418                         compare_len = tdata->plaintext.len;
419                 }
420
421                 if (memcmp(crypto_res, compare_ref, compare_len)) {
422                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
423                                 "FAILED: %s", __LINE__,
424                                 "Crypto data not as expected");
425                         status = TEST_FAILED;
426                         goto error_exit;
427                 }
428         }
429
430         if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_GEN) {
431                 uint8_t *auth_res;
432
433                 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER)
434                         auth_res = rte_pktmbuf_mtod_offset(iobuf,
435                                 uint8_t *,
436                                 tdata->iv.len + tdata->ciphertext.len);
437                 else
438                         auth_res = rte_pktmbuf_mtod_offset(iobuf,
439                                 uint8_t *, tdata->ciphertext.len);
440
441                 if (memcmp(auth_res, tdata->digest.data, digest_len)) {
442                         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
443                                 "FAILED: %s", __LINE__, "Generated "
444                                 "digest data not as expected");
445                         status = TEST_FAILED;
446                         goto error_exit;
447                 }
448         }
449
450         /* The only parts that should have changed in the buffer are
451          * plaintext/ciphertext and digest.
452          * In OOP only the dest buffer should change.
453          */
454         if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP) {
455                 struct rte_mbuf *mbuf;
456                 uint8_t value;
457                 uint32_t head_unchanged_len = 0, changed_len = 0;
458                 uint32_t i;
459
460                 mbuf = sym_op->m_src;
461                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_VERIFY) {
462                         /* white-box test: PMDs use some of the
463                          * tailroom as temp storage in verify case
464                          */
465                         head_unchanged_len = rte_pktmbuf_headroom(mbuf)
466                                         + rte_pktmbuf_data_len(mbuf);
467                         changed_len = digest_len;
468                 } else {
469                         head_unchanged_len = mbuf->buf_len;
470                         changed_len = 0;
471                 }
472
473                 for (i = 0; i < mbuf->buf_len; i++) {
474                         if (i == head_unchanged_len)
475                                 i += changed_len;
476                         value = *((uint8_t *)(mbuf->buf_addr)+i);
477                         if (value != tmp_src_buf[i]) {
478                                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
479         "line %u FAILED: OOP src outer mbuf data (0x%x) not as expected (0x%x)",
480                                         __LINE__, value, tmp_src_buf[i]);
481                                 status = TEST_FAILED;
482                                 goto error_exit;
483                         }
484                 }
485
486                 mbuf = sym_op->m_dst;
487                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH) {
488                         head_unchanged_len = rte_pktmbuf_headroom(mbuf) +
489                                                 sym_op->auth.data.offset;
490                         changed_len = sym_op->auth.data.length;
491                         if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_GEN)
492                                 changed_len += sym_op->auth.digest.length;
493                 } else {
494                         /* cipher-only */
495                         head_unchanged_len = rte_pktmbuf_headroom(mbuf) +
496                                         sym_op->cipher.data.offset;
497                         changed_len = sym_op->cipher.data.length;
498                 }
499
500                 for (i = 0; i < mbuf->buf_len; i++) {
501                         if (i == head_unchanged_len)
502                                 i += changed_len;
503                         value = *((uint8_t *)(mbuf->buf_addr)+i);
504                         if (value != tmp_dst_buf[i]) {
505                                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
506                                 "line %u FAILED: OOP dst outer mbuf data "
507                                 "(0x%x) not as expected (0x%x)",
508                                 __LINE__, value, tmp_dst_buf[i]);
509                                 status = TEST_FAILED;
510                                 goto error_exit;
511                         }
512                 }
513         } else {
514                 /* In-place operation */
515                 struct rte_mbuf *mbuf;
516                 uint8_t value;
517                 uint32_t head_unchanged_len = 0, changed_len = 0;
518                 uint32_t i;
519
520                 mbuf = sym_op->m_src;
521                 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
522                         head_unchanged_len = rte_pktmbuf_headroom(mbuf) +
523                                         sym_op->cipher.data.offset;
524                         changed_len = sym_op->cipher.data.length;
525                 } else {
526                         /* auth-only */
527                         head_unchanged_len = rte_pktmbuf_headroom(mbuf) +
528                                         sym_op->auth.data.offset +
529                                         sym_op->auth.data.length;
530                         changed_len = 0;
531                 }
532
533                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_GEN)
534                         changed_len += sym_op->auth.digest.length;
535
536                 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_VERIFY) {
537                         /* white-box test: PMDs use some of the
538                          * tailroom as temp storage in verify case
539                          */
540                         if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
541                                 /* This is simplified, not checking digest*/
542                                 changed_len += digest_len*2;
543                         } else {
544                                 head_unchanged_len += digest_len;
545                                 changed_len += digest_len;
546                         }
547                 }
548
549                 for (i = 0; i < mbuf->buf_len; i++) {
550                         if (i == head_unchanged_len)
551                                 i += changed_len;
552                         value = *((uint8_t *)(mbuf->buf_addr)+i);
553                         if (value != tmp_src_buf[i]) {
554                                 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
555                                 "line %u FAILED: outer mbuf data (0x%x) "
556                                 "not as expected (0x%x)",
557                                 __LINE__, value, tmp_src_buf[i]);
558                                 status = TEST_FAILED;
559                                 goto error_exit;
560                         }
561                 }
562         }
563
564         snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "PASS");
565
566 error_exit:
567         if (!(t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS)) {
568                 if (sess)
569                         rte_cryptodev_sym_session_free(dev_id, sess);
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         uint8_t dev_id,
592         enum rte_cryptodev_type cryptodev_type,
593         enum blockcipher_test_type test_type)
594 {
595         int status, overall_status = TEST_SUCCESS;
596         uint32_t i, test_index = 0;
597         char test_msg[BLOCKCIPHER_TEST_MSG_LEN + 1];
598         uint32_t n_test_cases = 0;
599         uint32_t target_pmd_mask = 0;
600         const struct blockcipher_test_case *tcs = NULL;
601
602         switch (test_type) {
603         case BLKCIPHER_AES_CHAIN_TYPE:
604                 n_test_cases = sizeof(aes_chain_test_cases) /
605                 sizeof(aes_chain_test_cases[0]);
606                 tcs = aes_chain_test_cases;
607                 break;
608         case BLKCIPHER_AES_CIPHERONLY_TYPE:
609                 n_test_cases = sizeof(aes_cipheronly_test_cases) /
610                 sizeof(aes_cipheronly_test_cases[0]);
611                 tcs = aes_cipheronly_test_cases;
612                 break;
613         case BLKCIPHER_3DES_CHAIN_TYPE:
614                 n_test_cases = sizeof(triple_des_chain_test_cases) /
615                 sizeof(triple_des_chain_test_cases[0]);
616                 tcs = triple_des_chain_test_cases;
617                 break;
618         case BLKCIPHER_3DES_CIPHERONLY_TYPE:
619                 n_test_cases = sizeof(triple_des_cipheronly_test_cases) /
620                 sizeof(triple_des_cipheronly_test_cases[0]);
621                 tcs = triple_des_cipheronly_test_cases;
622                 break;
623         case BLKCIPHER_DES_CIPHERONLY_TYPE:
624                 n_test_cases = sizeof(des_cipheronly_test_cases) /
625                 sizeof(des_cipheronly_test_cases[0]);
626                 tcs = des_cipheronly_test_cases;
627                 break;
628         case BLKCIPHER_AUTHONLY_TYPE:
629                 n_test_cases = sizeof(hash_test_cases) /
630                 sizeof(hash_test_cases[0]);
631                 tcs = hash_test_cases;
632                 break;
633         default:
634                 break;
635         }
636
637         switch (cryptodev_type) {
638         case RTE_CRYPTODEV_AESNI_MB_PMD:
639                 target_pmd_mask = BLOCKCIPHER_TEST_TARGET_PMD_MB;
640                 break;
641         case RTE_CRYPTODEV_QAT_SYM_PMD:
642                 target_pmd_mask = BLOCKCIPHER_TEST_TARGET_PMD_QAT;
643                 break;
644         case RTE_CRYPTODEV_OPENSSL_PMD:
645                 target_pmd_mask = BLOCKCIPHER_TEST_TARGET_PMD_OPENSSL;
646                 break;
647         default:
648                 TEST_ASSERT(0, "Unrecognized cryptodev type");
649                 break;
650         }
651
652         for (i = 0; i < n_test_cases; i++) {
653                 const struct blockcipher_test_case *tc = &tcs[i];
654
655                 if (!(tc->pmd_mask & target_pmd_mask))
656                         continue;
657
658                 status = test_blockcipher_one_case(tc, mbuf_pool, op_mpool,
659                         dev_id, cryptodev_type, test_msg);
660
661                 printf("  %u) TestCase %s %s\n", test_index ++,
662                         tc->test_descr, test_msg);
663
664                 if (status != TEST_SUCCESS) {
665                         if (overall_status == TEST_SUCCESS)
666                                 overall_status = status;
667
668                         if (tc->feature_mask & BLOCKCIPHER_TEST_FEATURE_STOPPER)
669                                 break;
670                 }
671         }
672
673         return overall_status;
674 }