4 * Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
33 #include <rte_common.h>
34 #include <rte_hexdump.h>
36 #include <rte_malloc.h>
37 #include <rte_memcpy.h>
39 #include <rte_crypto.h>
40 #include <rte_cryptodev.h>
41 #include <rte_cryptodev_pmd.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"
50 test_blockcipher_one_case(const struct blockcipher_test_case *t,
51 struct rte_mempool *mbuf_pool,
52 struct rte_mempool *op_mpool,
54 enum rte_cryptodev_type cryptodev_type,
57 struct rte_mbuf *ibuf = NULL;
58 struct rte_mbuf *obuf = NULL;
59 struct rte_mbuf *iobuf;
60 struct rte_crypto_sym_xform *cipher_xform = NULL;
61 struct rte_crypto_sym_xform *auth_xform = NULL;
62 struct rte_crypto_sym_xform *init_xform = NULL;
63 struct rte_crypto_sym_op *sym_op = NULL;
64 struct rte_crypto_op *op = NULL;
65 struct rte_cryptodev_sym_session *sess = NULL;
67 int status = TEST_SUCCESS;
68 const struct blockcipher_test_data *tdata = t->test_data;
69 uint8_t cipher_key[tdata->cipher_key.len];
70 uint8_t auth_key[tdata->auth_key.len];
71 uint32_t buf_len = tdata->ciphertext.len;
72 uint32_t digest_len = 0;
75 if (tdata->cipher_key.len)
76 memcpy(cipher_key, tdata->cipher_key.data,
77 tdata->cipher_key.len);
78 if (tdata->auth_key.len)
79 memcpy(auth_key, tdata->auth_key.data,
82 switch (cryptodev_type) {
83 case RTE_CRYPTODEV_QAT_SYM_PMD:
84 case RTE_CRYPTODEV_OPENSSL_PMD:
85 digest_len = tdata->digest.len;
87 case RTE_CRYPTODEV_AESNI_MB_PMD:
88 digest_len = tdata->digest.truncated_len;
91 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
93 __LINE__, "Unsupported PMD type");
99 ibuf = rte_pktmbuf_alloc(mbuf_pool);
101 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
102 "line %u FAILED: %s",
103 __LINE__, "Allocation of rte_mbuf failed");
104 status = TEST_FAILED;
108 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER)
109 buf_len += tdata->iv.len;
110 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH)
111 buf_len += digest_len;
113 buf_p = rte_pktmbuf_append(ibuf, buf_len);
115 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
116 "line %u FAILED: %s",
117 __LINE__, "No room to append mbuf");
118 status = TEST_FAILED;
122 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
123 rte_memcpy(buf_p, tdata->iv.data, tdata->iv.len);
124 buf_p += tdata->iv.len;
127 /* only encryption requires plaintext.data input,
128 * decryption/(digest gen)/(digest verify) use ciphertext.data
131 if (t->op_mask & BLOCKCIPHER_TEST_OP_ENCRYPT) {
132 rte_memcpy(buf_p, tdata->plaintext.data,
133 tdata->plaintext.len);
134 buf_p += tdata->plaintext.len;
136 rte_memcpy(buf_p, tdata->ciphertext.data,
137 tdata->ciphertext.len);
138 buf_p += tdata->ciphertext.len;
141 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_VERIFY)
142 rte_memcpy(buf_p, tdata->digest.data, digest_len);
144 memset(buf_p, 0, digest_len);
146 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP) {
147 obuf = rte_pktmbuf_alloc(mbuf_pool);
149 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
150 "FAILED: %s", __LINE__,
151 "Allocation of rte_mbuf failed");
152 status = TEST_FAILED;
156 buf_p = rte_pktmbuf_append(obuf, buf_len);
158 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
159 "FAILED: %s", __LINE__,
160 "No room to append mbuf");
161 status = TEST_FAILED;
164 memset(buf_p, 0, buf_len);
167 /* Generate Crypto op data structure */
168 op = rte_crypto_op_alloc(op_mpool, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
170 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
171 "line %u FAILED: %s",
172 __LINE__, "Failed to allocate symmetric crypto "
174 status = TEST_FAILED;
180 sym_op->m_src = ibuf;
182 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP) {
183 sym_op->m_dst = obuf;
186 sym_op->m_dst = NULL;
190 /* sessionless op requires allocate xform using
191 * rte_crypto_op_sym_xforms_alloc(), otherwise rte_zmalloc()
194 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS) {
195 uint32_t n_xforms = 0;
197 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER)
199 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH)
202 if (rte_crypto_op_sym_xforms_alloc(op, n_xforms)
204 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
205 "FAILED: %s", __LINE__, "Failed to "
206 "allocate space for crypto transforms");
207 status = TEST_FAILED;
211 cipher_xform = rte_zmalloc(NULL,
212 sizeof(struct rte_crypto_sym_xform), 0);
214 auth_xform = rte_zmalloc(NULL,
215 sizeof(struct rte_crypto_sym_xform), 0);
217 if (!cipher_xform || !auth_xform) {
218 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
219 "FAILED: %s", __LINE__, "Failed to "
220 "allocate memory for crypto transforms");
221 status = TEST_FAILED;
226 /* preparing xform, for sessioned op, init_xform is initialized
227 * here and later as param in rte_cryptodev_sym_session_create() call
229 if (t->op_mask == BLOCKCIPHER_TEST_OP_ENC_AUTH_GEN) {
230 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS) {
231 cipher_xform = op->sym->xform;
232 auth_xform = cipher_xform->next;
233 auth_xform->next = NULL;
235 cipher_xform->next = auth_xform;
236 auth_xform->next = NULL;
237 init_xform = cipher_xform;
239 } else if (t->op_mask == BLOCKCIPHER_TEST_OP_AUTH_VERIFY_DEC) {
240 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS) {
241 auth_xform = op->sym->xform;
242 cipher_xform = auth_xform->next;
243 cipher_xform->next = NULL;
245 auth_xform->next = cipher_xform;
246 cipher_xform->next = NULL;
247 init_xform = auth_xform;
249 } else if ((t->op_mask == BLOCKCIPHER_TEST_OP_ENCRYPT) ||
250 (t->op_mask == BLOCKCIPHER_TEST_OP_DECRYPT)) {
251 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS)
252 cipher_xform = op->sym->xform;
254 init_xform = cipher_xform;
255 cipher_xform->next = NULL;
256 } else if ((t->op_mask == BLOCKCIPHER_TEST_OP_AUTH_GEN) ||
257 (t->op_mask == BLOCKCIPHER_TEST_OP_AUTH_VERIFY)) {
258 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS)
259 auth_xform = op->sym->xform;
261 init_xform = auth_xform;
262 auth_xform->next = NULL;
264 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
265 "line %u FAILED: %s",
266 __LINE__, "Unrecognized operation");
267 status = TEST_FAILED;
271 /*configure xforms & sym_op cipher and auth data*/
272 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
273 cipher_xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
274 cipher_xform->cipher.algo = tdata->crypto_algo;
275 if (t->op_mask & BLOCKCIPHER_TEST_OP_ENCRYPT)
276 cipher_xform->cipher.op =
277 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
279 cipher_xform->cipher.op =
280 RTE_CRYPTO_CIPHER_OP_DECRYPT;
281 cipher_xform->cipher.key.data = cipher_key;
282 cipher_xform->cipher.key.length = tdata->cipher_key.len;
284 sym_op->cipher.data.offset = tdata->iv.len;
285 sym_op->cipher.data.length = tdata->ciphertext.len;
286 sym_op->cipher.iv.data = rte_pktmbuf_mtod(sym_op->m_src,
288 sym_op->cipher.iv.length = tdata->iv.len;
289 sym_op->cipher.iv.phys_addr = rte_pktmbuf_mtophys(
293 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH) {
294 uint32_t auth_data_offset = 0;
295 uint32_t digest_offset = tdata->ciphertext.len;
297 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
298 digest_offset += tdata->iv.len;
299 auth_data_offset += tdata->iv.len;
302 auth_xform->type = RTE_CRYPTO_SYM_XFORM_AUTH;
303 auth_xform->auth.algo = tdata->auth_algo;
304 auth_xform->auth.key.length = tdata->auth_key.len;
305 auth_xform->auth.key.data = auth_key;
306 auth_xform->auth.digest_length = digest_len;
308 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_GEN) {
309 auth_xform->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
310 sym_op->auth.digest.data = rte_pktmbuf_mtod_offset
311 (iobuf, uint8_t *, digest_offset);
312 sym_op->auth.digest.phys_addr =
313 rte_pktmbuf_mtophys_offset(iobuf,
316 auth_xform->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
317 sym_op->auth.digest.data = rte_pktmbuf_mtod_offset
318 (sym_op->m_src, uint8_t *, digest_offset);
319 sym_op->auth.digest.phys_addr =
320 rte_pktmbuf_mtophys_offset(sym_op->m_src,
324 sym_op->auth.data.offset = auth_data_offset;
325 sym_op->auth.data.length = tdata->ciphertext.len;
326 sym_op->auth.digest.length = digest_len;
329 /* create session for sessioned op */
330 if (!(t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS)) {
331 sess = rte_cryptodev_sym_session_create(dev_id,
334 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
335 "FAILED: %s", __LINE__,
336 "Session creation failed");
337 status = TEST_FAILED;
341 /* attach symmetric crypto session to crypto operations */
342 rte_crypto_op_attach_sym_session(op, sess);
345 /* Process crypto operation */
346 if (rte_cryptodev_enqueue_burst(dev_id, 0, &op, 1) != 1) {
347 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
348 "line %u FAILED: %s",
349 __LINE__, "Error sending packet for encryption");
350 status = TEST_FAILED;
356 while (rte_cryptodev_dequeue_burst(dev_id, 0, &op, 1) == 0)
360 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN,
361 "line %u FAILED: %s",
362 __LINE__, "Failed to process sym crypto op");
363 status = TEST_FAILED;
367 TEST_HEXDUMP(stdout, "m_src:",
368 rte_pktmbuf_mtod(sym_op->m_src, uint8_t *), buf_len);
369 if (t->feature_mask & BLOCKCIPHER_TEST_FEATURE_OOP)
370 TEST_HEXDUMP(stdout, "m_dst:",
371 rte_pktmbuf_mtod(sym_op->m_dst, uint8_t *),
375 if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
376 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_VERIFY)
377 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
378 "FAILED: Digest verification failed "
379 "(0x%X)", __LINE__, op->status);
381 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
382 "FAILED: Digest verification failed "
383 "(0x%X)", __LINE__, op->status);
384 status = TEST_FAILED;
388 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER) {
390 const uint8_t *compare_ref;
391 uint32_t compare_len;
393 crypto_res = rte_pktmbuf_mtod_offset(iobuf, uint8_t *,
396 if (t->op_mask & BLOCKCIPHER_TEST_OP_ENCRYPT) {
397 compare_ref = tdata->ciphertext.data;
398 compare_len = tdata->ciphertext.len;
400 compare_ref = tdata->plaintext.data;
401 compare_len = tdata->plaintext.len;
404 if (memcmp(crypto_res, compare_ref, compare_len)) {
405 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
406 "FAILED: %s", __LINE__,
407 "Crypto data not as expected");
408 status = TEST_FAILED;
413 if (t->op_mask & BLOCKCIPHER_TEST_OP_AUTH_GEN) {
416 if (t->op_mask & BLOCKCIPHER_TEST_OP_CIPHER)
417 auth_res = rte_pktmbuf_mtod_offset(iobuf,
419 tdata->iv.len + tdata->ciphertext.len);
421 auth_res = rte_pktmbuf_mtod_offset(iobuf,
422 uint8_t *, tdata->ciphertext.len);
424 if (memcmp(auth_res, tdata->digest.data, digest_len)) {
425 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "line %u "
426 "FAILED: %s", __LINE__, "Generated "
427 "digest data not as expected");
428 status = TEST_FAILED;
433 snprintf(test_msg, BLOCKCIPHER_TEST_MSG_LEN, "PASS");
436 if (!(t->feature_mask & BLOCKCIPHER_TEST_FEATURE_SESSIONLESS)) {
438 rte_cryptodev_sym_session_free(dev_id, sess);
440 rte_free(cipher_xform);
442 rte_free(auth_xform);
446 rte_crypto_op_free(op);
449 rte_pktmbuf_free(obuf);
452 rte_pktmbuf_free(ibuf);
458 test_blockcipher_all_tests(struct rte_mempool *mbuf_pool,
459 struct rte_mempool *op_mpool,
461 enum rte_cryptodev_type cryptodev_type,
462 enum blockcipher_test_type test_type)
464 int status, overall_status = TEST_SUCCESS;
465 uint32_t i, test_index = 0;
466 char test_msg[BLOCKCIPHER_TEST_MSG_LEN + 1];
467 uint32_t n_test_cases = 0;
468 uint32_t target_pmd_mask = 0;
469 const struct blockcipher_test_case *tcs = NULL;
472 case BLKCIPHER_AES_CHAIN_TYPE:
473 n_test_cases = sizeof(aes_chain_test_cases) /
474 sizeof(aes_chain_test_cases[0]);
475 tcs = aes_chain_test_cases;
477 case BLKCIPHER_AES_CIPHERONLY_TYPE:
478 n_test_cases = sizeof(aes_cipheronly_test_cases) /
479 sizeof(aes_cipheronly_test_cases[0]);
480 tcs = aes_cipheronly_test_cases;
482 case BLKCIPHER_3DES_CHAIN_TYPE:
483 n_test_cases = sizeof(triple_des_chain_test_cases) /
484 sizeof(triple_des_chain_test_cases[0]);
485 tcs = triple_des_chain_test_cases;
487 case BLKCIPHER_3DES_CIPHERONLY_TYPE:
488 n_test_cases = sizeof(triple_des_cipheronly_test_cases) /
489 sizeof(triple_des_cipheronly_test_cases[0]);
490 tcs = triple_des_cipheronly_test_cases;
492 case BLKCIPHER_AUTHONLY_TYPE:
493 n_test_cases = sizeof(hash_test_cases) /
494 sizeof(hash_test_cases[0]);
495 tcs = hash_test_cases;
501 switch (cryptodev_type) {
502 case RTE_CRYPTODEV_AESNI_MB_PMD:
503 target_pmd_mask = BLOCKCIPHER_TEST_TARGET_PMD_MB;
505 case RTE_CRYPTODEV_QAT_SYM_PMD:
506 target_pmd_mask = BLOCKCIPHER_TEST_TARGET_PMD_QAT;
508 case RTE_CRYPTODEV_OPENSSL_PMD:
509 target_pmd_mask = BLOCKCIPHER_TEST_TARGET_PMD_OPENSSL;
512 TEST_ASSERT(0, "Unrecognized cryptodev type");
516 for (i = 0; i < n_test_cases; i++) {
517 const struct blockcipher_test_case *tc = &tcs[i];
519 if (!(tc->pmd_mask & target_pmd_mask))
522 status = test_blockcipher_one_case(tc, mbuf_pool, op_mpool,
523 dev_id, cryptodev_type, test_msg);
525 printf(" %u) TestCase %s %s\n", test_index ++,
526 tc->test_descr, test_msg);
528 if (status != TEST_SUCCESS) {
529 if (overall_status == TEST_SUCCESS)
530 overall_status = status;
532 if (tc->feature_mask & BLOCKCIPHER_TEST_FEATURE_STOPPER)
537 return overall_status;