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