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