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