test/crypto: add encrypted digest case for AES-CTR-CMAC
[dpdk.git] / app / test / test_cryptodev.c
index 65d8083..35738a4 100644 (file)
@@ -38,6 +38,7 @@
 #include "test_cryptodev_zuc_test_vectors.h"
 #include "test_cryptodev_aead_test_vectors.h"
 #include "test_cryptodev_hmac_test_vectors.h"
+#include "test_cryptodev_mixed_test_vectors.h"
 #ifdef RTE_LIBRTE_SECURITY
 #include "test_cryptodev_security_pdcp_test_vectors.h"
 #include "test_cryptodev_security_pdcp_test_func.h"
@@ -3021,6 +3022,11 @@ create_wireless_algo_auth_cipher_operation(unsigned int auth_tag_len,
        struct crypto_testsuite_params *ts_params = &testsuite_params;
        struct crypto_unittest_params *ut_params = &unittest_params;
 
+       enum rte_crypto_cipher_algorithm cipher_algo =
+                       ut_params->cipher_xform.cipher.algo;
+       enum rte_crypto_auth_algorithm auth_algo =
+                       ut_params->auth_xform.auth.algo;
+
        /* Generate Crypto op data structure */
        ut_params->op = rte_crypto_op_alloc(ts_params->op_mpool,
                        RTE_CRYPTO_OP_TYPE_SYMMETRIC);
@@ -3076,11 +3082,25 @@ create_wireless_algo_auth_cipher_operation(unsigned int auth_tag_len,
        iv_ptr += cipher_iv_len;
        rte_memcpy(iv_ptr, auth_iv, auth_iv_len);
 
-       sym_op->cipher.data.length = cipher_len;
-       sym_op->cipher.data.offset = cipher_offset;
+       if (cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
+               cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
+               cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
+               sym_op->cipher.data.length = cipher_len;
+               sym_op->cipher.data.offset = cipher_offset;
+       } else {
+               sym_op->cipher.data.length = cipher_len >> 3;
+               sym_op->cipher.data.offset = cipher_offset >> 3;
+       }
 
-       sym_op->auth.data.length = auth_len;
-       sym_op->auth.data.offset = auth_offset;
+       if (auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
+               auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
+               auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
+               sym_op->auth.data.length = auth_len;
+               sym_op->auth.data.offset = auth_offset;
+       } else {
+               sym_op->auth.data.length = auth_len >> 3;
+               sym_op->auth.data.offset = auth_offset >> 3;
+       }
 
        return 0;
 }
@@ -6511,6 +6531,446 @@ test_zuc_auth_cipher_verify_test_case_1_oop_sgl(void)
                &zuc_auth_cipher_test_case_1, OUT_OF_PLACE, 1);
 }
 
+static int
+test_mixed_check_if_unsupported(const struct mixed_cipher_auth_test_data *tdata)
+{
+       uint8_t dev_id = testsuite_params.valid_devs[0];
+
+       struct rte_cryptodev_sym_capability_idx cap_idx;
+
+       /* Check if device supports particular cipher algorithm */
+       cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
+       cap_idx.algo.cipher = tdata->cipher_algo;
+       if (rte_cryptodev_sym_capability_get(dev_id, &cap_idx) == NULL)
+               return -ENOTSUP;
+
+       /* Check if device supports particular hash algorithm */
+       cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
+       cap_idx.algo.auth = tdata->auth_algo;
+       if (rte_cryptodev_sym_capability_get(dev_id, &cap_idx) == NULL)
+               return -ENOTSUP;
+
+       return 0;
+}
+
+static int
+test_mixed_auth_cipher(const struct mixed_cipher_auth_test_data *tdata,
+       uint8_t op_mode, uint8_t verify)
+{
+       struct crypto_testsuite_params *ts_params = &testsuite_params;
+       struct crypto_unittest_params *ut_params = &unittest_params;
+
+       int retval;
+
+       uint8_t *plaintext = NULL, *ciphertext = NULL;
+       unsigned int plaintext_pad_len;
+       unsigned int plaintext_len;
+       unsigned int ciphertext_pad_len;
+       unsigned int ciphertext_len;
+
+       struct rte_cryptodev_info dev_info;
+
+       /* Check if device supports particular algorithms */
+       if (test_mixed_check_if_unsupported(tdata))
+               return -ENOTSUP;
+
+       rte_cryptodev_info_get(ts_params->valid_devs[0], &dev_info);
+
+       uint64_t feat_flags = dev_info.feature_flags;
+
+       if (op_mode == OUT_OF_PLACE) {
+               if (!(feat_flags & RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED)) {
+                       printf("Device doesn't support digest encrypted.\n");
+                       return -ENOTSUP;
+               }
+       }
+
+       /* Create the session */
+       retval = create_wireless_algo_auth_cipher_session(
+                       ts_params->valid_devs[0],
+                       (verify ? RTE_CRYPTO_CIPHER_OP_DECRYPT
+                                       : RTE_CRYPTO_CIPHER_OP_ENCRYPT),
+                       (verify ? RTE_CRYPTO_AUTH_OP_VERIFY
+                                       : RTE_CRYPTO_AUTH_OP_GENERATE),
+                       tdata->auth_algo,
+                       tdata->cipher_algo,
+                       tdata->auth_key.data, tdata->auth_key.len,
+                       tdata->auth_iv.len, tdata->digest_enc.len,
+                       tdata->cipher_iv.len);
+
+       if (retval < 0)
+               return retval;
+
+       ut_params->ibuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
+       if (op_mode == OUT_OF_PLACE)
+               ut_params->obuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
+
+       /* clear mbuf payload */
+       memset(rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *), 0,
+               rte_pktmbuf_tailroom(ut_params->ibuf));
+       if (op_mode == OUT_OF_PLACE)
+               memset(rte_pktmbuf_mtod(ut_params->obuf, uint8_t *), 0,
+                               rte_pktmbuf_tailroom(ut_params->obuf));
+
+       ciphertext_len = ceil_byte_length(tdata->ciphertext.len_bits);
+       plaintext_len = ceil_byte_length(tdata->plaintext.len_bits);
+       ciphertext_pad_len = RTE_ALIGN_CEIL(ciphertext_len, 16);
+       plaintext_pad_len = RTE_ALIGN_CEIL(plaintext_len, 16);
+
+       if (verify) {
+               ciphertext = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
+                               ciphertext_pad_len);
+               memcpy(ciphertext, tdata->ciphertext.data, ciphertext_len);
+               if (op_mode == OUT_OF_PLACE)
+                       rte_pktmbuf_append(ut_params->obuf, ciphertext_pad_len);
+               debug_hexdump(stdout, "ciphertext:", ciphertext,
+                               ciphertext_len);
+       } else {
+               plaintext = (uint8_t *)rte_pktmbuf_append(ut_params->ibuf,
+                               plaintext_pad_len);
+               memcpy(plaintext, tdata->plaintext.data, plaintext_len);
+               if (op_mode == OUT_OF_PLACE)
+                       rte_pktmbuf_append(ut_params->obuf, plaintext_pad_len);
+               debug_hexdump(stdout, "plaintext:", plaintext, plaintext_len);
+       }
+
+       /* Create the operation */
+       retval = create_wireless_algo_auth_cipher_operation(
+                       tdata->digest_enc.len,
+                       tdata->cipher_iv.data, tdata->cipher_iv.len,
+                       tdata->auth_iv.data, tdata->auth_iv.len,
+                       (tdata->digest_enc.offset == 0 ?
+                       (verify ? ciphertext_pad_len : plaintext_pad_len)
+                               : tdata->digest_enc.offset),
+                       tdata->validCipherLen.len_bits,
+                       tdata->cipher.offset_bits,
+                       tdata->validAuthLen.len_bits,
+                       tdata->auth.offset_bits,
+                       op_mode, 0);
+
+       if (retval < 0)
+               return retval;
+
+       ut_params->op = process_crypto_request(ts_params->valid_devs[0],
+                       ut_params->op);
+
+       TEST_ASSERT_NOT_NULL(ut_params->op, "failed to retrieve obuf");
+
+       ut_params->obuf = (op_mode == IN_PLACE ?
+                       ut_params->op->sym->m_src : ut_params->op->sym->m_dst);
+
+       if (verify) {
+               if (ut_params->obuf)
+                       plaintext = rte_pktmbuf_mtod(ut_params->obuf,
+                                                       uint8_t *);
+               else
+                       plaintext = ciphertext +
+                                       (tdata->cipher.offset_bits >> 3);
+
+               debug_hexdump(stdout, "plaintext:", plaintext,
+                               (tdata->plaintext.len_bits >> 3) -
+                               tdata->digest_enc.len);
+               debug_hexdump(stdout, "plaintext expected:",
+                               tdata->plaintext.data,
+                               (tdata->plaintext.len_bits >> 3) -
+                               tdata->digest_enc.len);
+       } else {
+               if (ut_params->obuf)
+                       ciphertext = rte_pktmbuf_mtod(ut_params->obuf,
+                                       uint8_t *);
+               else
+                       ciphertext = plaintext;
+
+               debug_hexdump(stdout, "ciphertext:", ciphertext,
+                               ciphertext_len);
+               debug_hexdump(stdout, "ciphertext expected:",
+                               tdata->ciphertext.data,
+                               tdata->ciphertext.len_bits >> 3);
+
+               ut_params->digest = rte_pktmbuf_mtod(ut_params->obuf, uint8_t *)
+                               + (tdata->digest_enc.offset == 0 ?
+               plaintext_pad_len : tdata->digest_enc.offset);
+
+               debug_hexdump(stdout, "digest:", ut_params->digest,
+                               tdata->digest_enc.len);
+               debug_hexdump(stdout, "digest expected:",
+                               tdata->digest_enc.data,
+                               tdata->digest_enc.len);
+       }
+
+       /* Validate obuf */
+       if (verify) {
+               TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(
+                               plaintext,
+                               tdata->plaintext.data,
+                               tdata->plaintext.len_bits >> 3,
+                               "Plaintext data not as expected");
+       } else {
+               TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(
+                               ciphertext,
+                               tdata->ciphertext.data,
+                               tdata->validDataLen.len_bits,
+                               "Ciphertext data not as expected");
+
+               TEST_ASSERT_BUFFERS_ARE_EQUAL(
+                               ut_params->digest,
+                               tdata->digest_enc.data,
+                               DIGEST_BYTE_LENGTH_SNOW3G_UIA2,
+                               "Generated auth tag not as expected");
+       }
+       return 0;
+}
+
+static int
+test_mixed_auth_cipher_sgl(const struct mixed_cipher_auth_test_data *tdata,
+       uint8_t op_mode, uint8_t verify)
+{
+       struct crypto_testsuite_params *ts_params = &testsuite_params;
+       struct crypto_unittest_params *ut_params = &unittest_params;
+
+       int retval;
+
+       const uint8_t *plaintext = NULL;
+       const uint8_t *ciphertext = NULL;
+       const uint8_t *digest = NULL;
+       unsigned int plaintext_pad_len;
+       unsigned int plaintext_len;
+       unsigned int ciphertext_pad_len;
+       unsigned int ciphertext_len;
+       uint8_t buffer[10000];
+       uint8_t digest_buffer[10000];
+
+       struct rte_cryptodev_info dev_info;
+
+       /* Check if device supports particular algorithms */
+       if (test_mixed_check_if_unsupported(tdata))
+               return -ENOTSUP;
+
+       rte_cryptodev_info_get(ts_params->valid_devs[0], &dev_info);
+
+       uint64_t feat_flags = dev_info.feature_flags;
+
+       if (op_mode == IN_PLACE) {
+               if (!(feat_flags & RTE_CRYPTODEV_FF_IN_PLACE_SGL)) {
+                       printf("Device doesn't support in-place scatter-gather "
+                                       "in both input and output mbufs.\n");
+                       return -ENOTSUP;
+               }
+       } else {
+               if (!(feat_flags & RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT)) {
+                       printf("Device doesn't support out-of-place scatter-gather "
+                                       "in both input and output mbufs.\n");
+                       return -ENOTSUP;
+               }
+               if (!(feat_flags & RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED)) {
+                       printf("Device doesn't support digest encrypted.\n");
+                       return -ENOTSUP;
+               }
+       }
+
+       /* Create the session */
+       retval = create_wireless_algo_auth_cipher_session(
+                       ts_params->valid_devs[0],
+                       (verify ? RTE_CRYPTO_CIPHER_OP_DECRYPT
+                                       : RTE_CRYPTO_CIPHER_OP_ENCRYPT),
+                       (verify ? RTE_CRYPTO_AUTH_OP_VERIFY
+                                       : RTE_CRYPTO_AUTH_OP_GENERATE),
+                       tdata->auth_algo,
+                       tdata->cipher_algo,
+                       tdata->auth_key.data, tdata->auth_key.len,
+                       tdata->auth_iv.len, tdata->digest_enc.len,
+                       tdata->cipher_iv.len);
+
+       if (retval < 0)
+               return retval;
+
+       ciphertext_len = ceil_byte_length(tdata->ciphertext.len_bits);
+       plaintext_len = ceil_byte_length(tdata->plaintext.len_bits);
+       ciphertext_pad_len = RTE_ALIGN_CEIL(ciphertext_len, 16);
+       plaintext_pad_len = RTE_ALIGN_CEIL(plaintext_len, 16);
+
+       ut_params->ibuf = create_segmented_mbuf(ts_params->mbuf_pool,
+                       plaintext_pad_len, 15, 0);
+       TEST_ASSERT_NOT_NULL(ut_params->ibuf,
+                       "Failed to allocate input buffer in mempool");
+
+       if (op_mode == OUT_OF_PLACE) {
+               ut_params->obuf = create_segmented_mbuf(ts_params->mbuf_pool,
+                               plaintext_pad_len, 15, 0);
+               TEST_ASSERT_NOT_NULL(ut_params->obuf,
+                               "Failed to allocate output buffer in mempool");
+       }
+
+       if (verify) {
+               pktmbuf_write(ut_params->ibuf, 0, ciphertext_len,
+                       tdata->ciphertext.data);
+               ciphertext = rte_pktmbuf_read(ut_params->ibuf, 0,
+                                       ciphertext_len, buffer);
+               debug_hexdump(stdout, "ciphertext:", ciphertext,
+                       ciphertext_len);
+       } else {
+               pktmbuf_write(ut_params->ibuf, 0, plaintext_len,
+                       tdata->plaintext.data);
+               plaintext = rte_pktmbuf_read(ut_params->ibuf, 0,
+                                       plaintext_len, buffer);
+               debug_hexdump(stdout, "plaintext:", plaintext,
+                       plaintext_len);
+       }
+       memset(buffer, 0, sizeof(buffer));
+
+       /* Create the operation */
+       retval = create_wireless_algo_auth_cipher_operation(
+                       tdata->digest_enc.len,
+                       tdata->cipher_iv.data, tdata->cipher_iv.len,
+                       tdata->auth_iv.data, tdata->auth_iv.len,
+                       (tdata->digest_enc.offset == 0 ?
+                       (verify ? ciphertext_pad_len : plaintext_pad_len)
+                               : tdata->digest_enc.offset),
+                       tdata->validCipherLen.len_bits,
+                       tdata->cipher.offset_bits,
+                       tdata->validAuthLen.len_bits,
+                       tdata->auth.offset_bits,
+                       op_mode, 1);
+
+       if (retval < 0)
+               return retval;
+
+       ut_params->op = process_crypto_request(ts_params->valid_devs[0],
+                       ut_params->op);
+
+       TEST_ASSERT_NOT_NULL(ut_params->op, "failed to retrieve obuf");
+
+       ut_params->obuf = (op_mode == IN_PLACE ?
+                       ut_params->op->sym->m_src : ut_params->op->sym->m_dst);
+
+       if (verify) {
+               if (ut_params->obuf)
+                       plaintext = rte_pktmbuf_read(ut_params->obuf, 0,
+                                       plaintext_len, buffer);
+               else
+                       plaintext = rte_pktmbuf_read(ut_params->ibuf, 0,
+                                       plaintext_len, buffer);
+
+               debug_hexdump(stdout, "plaintext:", plaintext,
+                               (tdata->plaintext.len_bits >> 3) -
+                               tdata->digest_enc.len);
+               debug_hexdump(stdout, "plaintext expected:",
+                               tdata->plaintext.data,
+                               (tdata->plaintext.len_bits >> 3) -
+                               tdata->digest_enc.len);
+       } else {
+               if (ut_params->obuf)
+                       ciphertext = rte_pktmbuf_read(ut_params->obuf, 0,
+                                       ciphertext_len, buffer);
+               else
+                       ciphertext = rte_pktmbuf_read(ut_params->ibuf, 0,
+                                       ciphertext_len, buffer);
+
+               debug_hexdump(stdout, "ciphertext:", ciphertext,
+                       ciphertext_len);
+               debug_hexdump(stdout, "ciphertext expected:",
+                       tdata->ciphertext.data,
+                       tdata->ciphertext.len_bits >> 3);
+
+               if (ut_params->obuf)
+                       digest = rte_pktmbuf_read(ut_params->obuf,
+                                       (tdata->digest_enc.offset == 0 ?
+                                               plaintext_pad_len :
+                                               tdata->digest_enc.offset),
+                                       tdata->digest_enc.len, digest_buffer);
+               else
+                       digest = rte_pktmbuf_read(ut_params->ibuf,
+                                       (tdata->digest_enc.offset == 0 ?
+                                               plaintext_pad_len :
+                                               tdata->digest_enc.offset),
+                                       tdata->digest_enc.len, digest_buffer);
+
+               debug_hexdump(stdout, "digest:", digest,
+                               tdata->digest_enc.len);
+               debug_hexdump(stdout, "digest expected:",
+                               tdata->digest_enc.data, tdata->digest_enc.len);
+       }
+
+       /* Validate obuf */
+       if (verify) {
+               TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(
+                               plaintext,
+                               tdata->plaintext.data,
+                               tdata->plaintext.len_bits >> 3,
+                               "Plaintext data not as expected");
+       } else {
+               TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(
+                               ciphertext,
+                               tdata->ciphertext.data,
+                               tdata->validDataLen.len_bits,
+                               "Ciphertext data not as expected");
+               TEST_ASSERT_BUFFERS_ARE_EQUAL(
+                               digest,
+                               tdata->digest_enc.data,
+                               tdata->digest_enc.len,
+                               "Generated auth tag not as expected");
+       }
+       return 0;
+}
+
+/** AUTH AES CMAC + CIPHER AES CTR */
+
+static int
+test_aes_cmac_aes_ctr_digest_enc_test_case_1(void)
+{
+       return test_mixed_auth_cipher(
+               &auth_aes_cmac_cipher_aes_ctr_test_case_1, IN_PLACE, 0);
+}
+
+static int
+test_aes_cmac_aes_ctr_digest_enc_test_case_1_oop(void)
+{
+       return test_mixed_auth_cipher(
+               &auth_aes_cmac_cipher_aes_ctr_test_case_1, OUT_OF_PLACE, 0);
+}
+
+static int
+test_aes_cmac_aes_ctr_digest_enc_test_case_1_sgl(void)
+{
+       return test_mixed_auth_cipher_sgl(
+               &auth_aes_cmac_cipher_aes_ctr_test_case_1, IN_PLACE, 0);
+}
+
+static int
+test_aes_cmac_aes_ctr_digest_enc_test_case_1_oop_sgl(void)
+{
+       return test_mixed_auth_cipher_sgl(
+               &auth_aes_cmac_cipher_aes_ctr_test_case_1, OUT_OF_PLACE, 0);
+}
+
+static int
+test_verify_aes_cmac_aes_ctr_digest_enc_test_case_1(void)
+{
+       return test_mixed_auth_cipher(
+               &auth_aes_cmac_cipher_aes_ctr_test_case_1, IN_PLACE, 1);
+}
+
+static int
+test_verify_aes_cmac_aes_ctr_digest_enc_test_case_1_oop(void)
+{
+       return test_mixed_auth_cipher(
+               &auth_aes_cmac_cipher_aes_ctr_test_case_1, OUT_OF_PLACE, 1);
+}
+
+static int
+test_verify_aes_cmac_aes_ctr_digest_enc_test_case_1_sgl(void)
+{
+       return test_mixed_auth_cipher_sgl(
+               &auth_aes_cmac_cipher_aes_ctr_test_case_1, IN_PLACE, 1);
+}
+
+static int
+test_verify_aes_cmac_aes_ctr_digest_enc_test_case_1_oop_sgl(void)
+{
+       return test_mixed_auth_cipher_sgl(
+               &auth_aes_cmac_cipher_aes_ctr_test_case_1, OUT_OF_PLACE, 1);
+}
+
 static int
 test_3DES_chain_qat_all(void)
 {
@@ -11802,6 +12262,25 @@ static struct unit_test_suite cryptodev_qat_testsuite  = {
                TEST_CASE_ST(ut_setup, ut_teardown,
                        auth_decryption_AES128CBC_HMAC_SHA1_fail_tag_corrupt),
 
+               /** Mixed CIPHER + HASH algorithms */
+               /** AUTH AES CMAC + CIPHER AES CTR */
+               TEST_CASE_ST(ut_setup, ut_teardown,
+                       test_aes_cmac_aes_ctr_digest_enc_test_case_1),
+               TEST_CASE_ST(ut_setup, ut_teardown,
+                       test_aes_cmac_aes_ctr_digest_enc_test_case_1_oop),
+               TEST_CASE_ST(ut_setup, ut_teardown,
+                       test_aes_cmac_aes_ctr_digest_enc_test_case_1_sgl),
+               TEST_CASE_ST(ut_setup, ut_teardown,
+                       test_aes_cmac_aes_ctr_digest_enc_test_case_1_oop_sgl),
+               TEST_CASE_ST(ut_setup, ut_teardown,
+                       test_verify_aes_cmac_aes_ctr_digest_enc_test_case_1),
+               TEST_CASE_ST(ut_setup, ut_teardown,
+                      test_verify_aes_cmac_aes_ctr_digest_enc_test_case_1_oop),
+               TEST_CASE_ST(ut_setup, ut_teardown,
+                      test_verify_aes_cmac_aes_ctr_digest_enc_test_case_1_sgl),
+               TEST_CASE_ST(ut_setup, ut_teardown,
+                  test_verify_aes_cmac_aes_ctr_digest_enc_test_case_1_oop_sgl),
+
                TEST_CASES_END() /**< NULL terminate unit test array */
        }
 };