1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
9 #include <rte_cryptodev.h>
10 #include <rte_cryptodev_pmd.h>
11 #include <rte_mempool.h>
13 #include <rte_string_fns.h>
15 #include "fips_validation.h"
16 #include "fips_dev_self_test.h"
18 #define REQ_FILE_PATH_KEYWORD "req-file"
19 #define RSP_FILE_PATH_KEYWORD "rsp-file"
20 #define FOLDER_KEYWORD "path-is-folder"
21 #define CRYPTODEV_KEYWORD "cryptodev"
22 #define CRYPTODEV_ID_KEYWORD "cryptodev-id"
23 #define CRYPTODEV_ST_KEYWORD "self-test"
24 #define CRYPTODEV_BK_ID_KEYWORD "broken-test-id"
25 #define CRYPTODEV_BK_DIR_KEY "broken-test-dir"
26 #define CRYPTODEV_ENC_KEYWORD "enc"
27 #define CRYPTODEV_DEC_KEYWORD "dec"
29 struct fips_test_vector vec;
30 struct fips_test_interim_info info;
32 struct cryptodev_fips_validate_env {
35 uint32_t is_path_folder;
37 struct rte_mempool *mpool;
38 struct rte_mempool *sess_mpool;
39 struct rte_mempool *sess_priv_mpool;
40 struct rte_mempool *op_pool;
41 struct rte_mbuf *mbuf;
42 struct rte_crypto_op *op;
43 struct rte_cryptodev_sym_session *sess;
45 struct fips_dev_broken_test_config *broken_test_config;
49 cryptodev_fips_validate_app_int(void)
51 struct rte_cryptodev_config conf = {rte_socket_id(), 1, 0};
52 struct rte_cryptodev_qp_conf qp_conf = {128, NULL, NULL};
53 uint32_t sess_sz = rte_cryptodev_sym_get_private_session_size(
58 ret = fips_dev_self_test(env.dev_id, env.broken_test_config);
60 struct rte_cryptodev *cryptodev =
61 rte_cryptodev_pmd_get_dev(env.dev_id);
63 rte_cryptodev_pmd_destroy(cryptodev);
69 ret = rte_cryptodev_configure(env.dev_id, &conf);
73 env.mpool = rte_pktmbuf_pool_create("FIPS_MEMPOOL", 128, 0, 0,
74 UINT16_MAX, rte_socket_id());
78 ret = rte_cryptodev_queue_pair_setup(env.dev_id, 0, &qp_conf,
85 env.sess_mpool = rte_cryptodev_sym_session_pool_create(
86 "FIPS_SESS_MEMPOOL", 16, 0, 0, 0, rte_socket_id());
90 env.sess_priv_mpool = rte_mempool_create("FIPS_SESS_PRIV_MEMPOOL",
91 16, sess_sz, 0, 0, NULL, NULL, NULL,
92 NULL, rte_socket_id(), 0);
93 if (!env.sess_priv_mpool)
96 env.op_pool = rte_crypto_op_pool_create(
98 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
105 env.mbuf = rte_pktmbuf_alloc(env.mpool);
109 env.op = rte_crypto_op_alloc(env.op_pool, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
113 qp_conf.mp_session = env.sess_mpool;
114 qp_conf.mp_session_private = env.sess_priv_mpool;
116 ret = rte_cryptodev_queue_pair_setup(env.dev_id, 0, &qp_conf,
125 rte_mempool_free(env.mpool);
127 rte_mempool_free(env.sess_mpool);
128 if (env.sess_priv_mpool)
129 rte_mempool_free(env.sess_priv_mpool);
131 rte_mempool_free(env.op_pool);
137 cryptodev_fips_validate_app_uninit(void)
139 rte_pktmbuf_free(env.mbuf);
140 rte_crypto_op_free(env.op);
141 rte_cryptodev_sym_session_clear(env.dev_id, env.sess);
142 rte_cryptodev_sym_session_free(env.sess);
143 rte_mempool_free(env.mpool);
144 rte_mempool_free(env.sess_mpool);
145 rte_mempool_free(env.sess_priv_mpool);
146 rte_mempool_free(env.op_pool);
150 fips_test_one_file(void);
153 parse_cryptodev_arg(char *arg)
155 int id = rte_cryptodev_get_dev_id(arg);
158 RTE_LOG(ERR, USER1, "Error %i: invalid cryptodev name %s\n",
163 env.dev_id = (uint32_t)id;
169 parse_cryptodev_id_arg(char *arg)
171 uint32_t cryptodev_id;
173 if (parser_read_uint32(&cryptodev_id, arg) < 0) {
174 RTE_LOG(ERR, USER1, "Error %i: invalid cryptodev id %s\n",
180 if (!rte_cryptodev_pmd_is_valid_dev(cryptodev_id)) {
181 RTE_LOG(ERR, USER1, "Error %i: invalid cryptodev id %s\n",
186 env.dev_id = (uint32_t)cryptodev_id;
192 cryptodev_fips_validate_usage(const char *prgname)
194 printf("%s [EAL options] --\n"
195 " --%s: REQUEST-FILE-PATH\n"
196 " --%s: RESPONSE-FILE-PATH\n"
197 " --%s: indicating both paths are folders\n"
198 " --%s: CRYPTODEV-NAME\n"
199 " --%s: CRYPTODEV-ID-NAME\n"
200 " --%s: self test indicator\n"
201 " --%s: self broken test ID\n"
202 " --%s: self broken test direction\n",
203 prgname, REQ_FILE_PATH_KEYWORD, RSP_FILE_PATH_KEYWORD,
204 FOLDER_KEYWORD, CRYPTODEV_KEYWORD, CRYPTODEV_ID_KEYWORD,
205 CRYPTODEV_ST_KEYWORD, CRYPTODEV_BK_ID_KEYWORD,
206 CRYPTODEV_BK_DIR_KEY);
210 cryptodev_fips_validate_parse_args(int argc, char **argv)
213 char *prgname = argv[0];
216 struct option lgopts[] = {
217 {REQ_FILE_PATH_KEYWORD, required_argument, 0, 0},
218 {RSP_FILE_PATH_KEYWORD, required_argument, 0, 0},
219 {FOLDER_KEYWORD, no_argument, 0, 0},
220 {CRYPTODEV_KEYWORD, required_argument, 0, 0},
221 {CRYPTODEV_ID_KEYWORD, required_argument, 0, 0},
222 {CRYPTODEV_ST_KEYWORD, no_argument, 0, 0},
223 {CRYPTODEV_BK_ID_KEYWORD, required_argument, 0, 0},
224 {CRYPTODEV_BK_DIR_KEY, required_argument, 0, 0},
230 while ((opt = getopt_long(argc, argvopt, "s:",
231 lgopts, &option_index)) != EOF) {
235 if (strcmp(lgopts[option_index].name,
236 REQ_FILE_PATH_KEYWORD) == 0)
237 env.req_path = optarg;
238 else if (strcmp(lgopts[option_index].name,
239 RSP_FILE_PATH_KEYWORD) == 0)
240 env.rsp_path = optarg;
241 else if (strcmp(lgopts[option_index].name,
242 FOLDER_KEYWORD) == 0)
243 env.is_path_folder = 1;
244 else if (strcmp(lgopts[option_index].name,
245 CRYPTODEV_KEYWORD) == 0) {
246 ret = parse_cryptodev_arg(optarg);
248 cryptodev_fips_validate_usage(prgname);
251 } else if (strcmp(lgopts[option_index].name,
252 CRYPTODEV_ID_KEYWORD) == 0) {
253 ret = parse_cryptodev_id_arg(optarg);
255 cryptodev_fips_validate_usage(prgname);
258 } else if (strcmp(lgopts[option_index].name,
259 CRYPTODEV_ST_KEYWORD) == 0) {
261 } else if (strcmp(lgopts[option_index].name,
262 CRYPTODEV_BK_ID_KEYWORD) == 0) {
263 if (!env.broken_test_config) {
264 env.broken_test_config = rte_malloc(
266 sizeof(*env.broken_test_config),
268 if (!env.broken_test_config)
271 env.broken_test_config->expect_fail_dir =
272 self_test_dir_enc_auth_gen;
275 if (parser_read_uint32(
276 &env.broken_test_config->expect_fail_test_idx,
278 rte_free(env.broken_test_config);
279 cryptodev_fips_validate_usage(prgname);
282 } else if (strcmp(lgopts[option_index].name,
283 CRYPTODEV_BK_DIR_KEY) == 0) {
284 if (!env.broken_test_config) {
285 env.broken_test_config = rte_malloc(
287 sizeof(*env.broken_test_config),
289 if (!env.broken_test_config)
292 env.broken_test_config->
293 expect_fail_test_idx = 0;
296 if (strcmp(optarg, CRYPTODEV_ENC_KEYWORD) == 0)
297 env.broken_test_config->expect_fail_dir =
298 self_test_dir_enc_auth_gen;
299 else if (strcmp(optarg, CRYPTODEV_DEC_KEYWORD)
301 env.broken_test_config->expect_fail_dir =
302 self_test_dir_dec_auth_verify;
304 rte_free(env.broken_test_config);
305 cryptodev_fips_validate_usage(prgname);
309 cryptodev_fips_validate_usage(prgname);
318 if (env.dev_id == UINT32_MAX) {
319 RTE_LOG(ERR, USER1, "No device specified\n");
320 cryptodev_fips_validate_usage(prgname);
324 if ((env.req_path == NULL && env.rsp_path != NULL) ||
325 (env.req_path != NULL && env.rsp_path == NULL)) {
326 RTE_LOG(ERR, USER1, "Missing req path or rsp path\n");
327 cryptodev_fips_validate_usage(prgname);
331 if (env.req_path == NULL && env.self_test == 0) {
332 RTE_LOG(ERR, USER1, "--self-test must be set if req path is missing\n");
333 cryptodev_fips_validate_usage(prgname);
341 main(int argc, char *argv[])
345 ret = rte_eal_init(argc, argv);
347 RTE_LOG(ERR, USER1, "Error %i: Failed init\n", ret);
354 ret = cryptodev_fips_validate_parse_args(argc, argv);
356 rte_exit(EXIT_FAILURE, "Failed to parse arguments!\n");
358 ret = cryptodev_fips_validate_app_int();
360 RTE_LOG(ERR, USER1, "Error %i: Failed init\n", ret);
364 if (env.req_path == NULL || env.rsp_path == NULL) {
365 printf("No request, exit.\n");
369 if (!env.is_path_folder) {
370 printf("Processing file %s... ", env.req_path);
372 ret = fips_test_init(env.req_path, env.rsp_path,
373 rte_cryptodev_name_get(env.dev_id));
375 RTE_LOG(ERR, USER1, "Error %i: Failed test %s\n",
381 ret = fips_test_one_file();
383 RTE_LOG(ERR, USER1, "Error %i: Failed test %s\n",
396 d_req = opendir(env.req_path);
398 RTE_LOG(ERR, USER1, "Error %i: Path %s not exist\n",
399 -EINVAL, env.req_path);
403 d_rsp = opendir(env.rsp_path);
405 ret = mkdir(env.rsp_path, 0700);
407 d_rsp = opendir(env.rsp_path);
409 RTE_LOG(ERR, USER1, "Error %i: Invalid %s\n",
410 -EINVAL, env.rsp_path);
416 while ((dir = readdir(d_req)) != NULL) {
417 if (strstr(dir->d_name, "req") == NULL)
420 snprintf(req_path, 1023, "%s/%s", env.req_path,
422 snprintf(rsp_path, 1023, "%s/%s", env.rsp_path,
424 strlcpy(strstr(rsp_path, "req"), "rsp", 4);
426 printf("Processing file %s... ", req_path);
428 ret = fips_test_init(req_path, rsp_path,
429 rte_cryptodev_name_get(env.dev_id));
431 RTE_LOG(ERR, USER1, "Error %i: Failed test %s\n",
436 ret = fips_test_one_file();
438 RTE_LOG(ERR, USER1, "Error %i: Failed test %s\n",
452 cryptodev_fips_validate_app_uninit();
458 #define IV_OFF (sizeof(struct rte_crypto_op) + sizeof(struct rte_crypto_sym_op))
459 #define CRYPTODEV_FIPS_MAX_RETRIES 16
461 typedef int (*fips_test_one_case_t)(void);
462 typedef int (*fips_prepare_op_t)(void);
463 typedef int (*fips_prepare_xform_t)(struct rte_crypto_sym_xform *);
465 struct fips_test_ops {
466 fips_prepare_xform_t prepare_xform;
467 fips_prepare_op_t prepare_op;
468 fips_test_one_case_t test;
472 prepare_cipher_op(void)
474 struct rte_crypto_sym_op *sym = env.op->sym;
475 uint8_t *iv = rte_crypto_op_ctod_offset(env.op, uint8_t *, IV_OFF);
477 __rte_crypto_op_reset(env.op, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
478 rte_pktmbuf_reset(env.mbuf);
480 sym->m_src = env.mbuf;
481 sym->cipher.data.offset = 0;
483 memcpy(iv, vec.iv.val, vec.iv.len);
485 if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
488 if (vec.pt.len > RTE_MBUF_MAX_NB_SEGS) {
489 RTE_LOG(ERR, USER1, "PT len %u\n", vec.pt.len);
493 pt = (uint8_t *)rte_pktmbuf_append(env.mbuf, vec.pt.len);
496 RTE_LOG(ERR, USER1, "Error %i: MBUF too small\n",
501 memcpy(pt, vec.pt.val, vec.pt.len);
502 sym->cipher.data.length = vec.pt.len;
507 if (vec.ct.len > RTE_MBUF_MAX_NB_SEGS) {
508 RTE_LOG(ERR, USER1, "CT len %u\n", vec.ct.len);
512 ct = (uint8_t *)rte_pktmbuf_append(env.mbuf, vec.ct.len);
515 RTE_LOG(ERR, USER1, "Error %i: MBUF too small\n",
520 memcpy(ct, vec.ct.val, vec.ct.len);
521 sym->cipher.data.length = vec.ct.len;
524 rte_crypto_op_attach_sym_session(env.op, env.sess);
530 prepare_auth_op(void)
532 struct rte_crypto_sym_op *sym = env.op->sym;
535 __rte_crypto_op_reset(env.op, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
536 rte_pktmbuf_reset(env.mbuf);
538 sym->m_src = env.mbuf;
539 sym->auth.data.offset = 0;
541 pt = (uint8_t *)rte_pktmbuf_append(env.mbuf, vec.pt.len +
542 vec.cipher_auth.digest.len);
545 RTE_LOG(ERR, USER1, "Error %i: MBUF too small\n",
550 sym->auth.data.length = vec.pt.len;
551 sym->auth.digest.data = pt + vec.pt.len;
552 sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(
553 env.mbuf, vec.pt.len);
555 memcpy(pt, vec.pt.val, vec.pt.len);
557 if (info.op == FIPS_TEST_DEC_AUTH_VERIF)
558 memcpy(pt + vec.pt.len, vec.cipher_auth.digest.val,
559 vec.cipher_auth.digest.len);
561 rte_crypto_op_attach_sym_session(env.op, env.sess);
567 prepare_aead_op(void)
569 struct rte_crypto_sym_op *sym = env.op->sym;
570 uint8_t *iv = rte_crypto_op_ctod_offset(env.op, uint8_t *, IV_OFF);
572 __rte_crypto_op_reset(env.op, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
573 rte_pktmbuf_reset(env.mbuf);
575 if (info.algo == FIPS_TEST_ALGO_AES_CCM)
576 memcpy(iv + 1, vec.iv.val, vec.iv.len);
578 memcpy(iv, vec.iv.val, vec.iv.len);
580 sym->m_src = env.mbuf;
581 sym->aead.data.offset = 0;
582 sym->aead.aad.data = vec.aead.aad.val;
583 sym->aead.aad.phys_addr = rte_malloc_virt2iova(sym->aead.aad.data);
585 if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
588 if (vec.pt.len > RTE_MBUF_MAX_NB_SEGS) {
589 RTE_LOG(ERR, USER1, "PT len %u\n", vec.pt.len);
593 pt = (uint8_t *)rte_pktmbuf_append(env.mbuf,
594 vec.pt.len + vec.aead.digest.len);
597 RTE_LOG(ERR, USER1, "Error %i: MBUF too small\n",
602 memcpy(pt, vec.pt.val, vec.pt.len);
603 sym->aead.data.length = vec.pt.len;
604 sym->aead.digest.data = pt + vec.pt.len;
605 sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(
606 env.mbuf, vec.pt.len);
610 if (vec.ct.len > RTE_MBUF_MAX_NB_SEGS) {
611 RTE_LOG(ERR, USER1, "CT len %u\n", vec.ct.len);
615 ct = (uint8_t *)rte_pktmbuf_append(env.mbuf, vec.ct.len);
618 RTE_LOG(ERR, USER1, "Error %i: MBUF too small\n",
623 memcpy(ct, vec.ct.val, vec.ct.len);
624 sym->aead.data.length = vec.ct.len;
625 sym->aead.digest.data = vec.aead.digest.val;
626 sym->aead.digest.phys_addr = rte_malloc_virt2iova(
627 sym->aead.digest.data);
630 rte_crypto_op_attach_sym_session(env.op, env.sess);
636 prepare_aes_xform(struct rte_crypto_sym_xform *xform)
638 const struct rte_cryptodev_symmetric_capability *cap;
639 struct rte_cryptodev_sym_capability_idx cap_idx;
640 struct rte_crypto_cipher_xform *cipher_xform = &xform->cipher;
642 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
644 if (info.interim_info.aes_data.cipher_algo == RTE_CRYPTO_CIPHER_AES_CBC)
645 cipher_xform->algo = RTE_CRYPTO_CIPHER_AES_CBC;
647 cipher_xform->algo = RTE_CRYPTO_CIPHER_AES_ECB;
649 cipher_xform->op = (info.op == FIPS_TEST_ENC_AUTH_GEN) ?
650 RTE_CRYPTO_CIPHER_OP_ENCRYPT :
651 RTE_CRYPTO_CIPHER_OP_DECRYPT;
652 cipher_xform->key.data = vec.cipher_auth.key.val;
653 cipher_xform->key.length = vec.cipher_auth.key.len;
654 if (cipher_xform->algo == RTE_CRYPTO_CIPHER_AES_CBC) {
655 cipher_xform->iv.length = vec.iv.len;
656 cipher_xform->iv.offset = IV_OFF;
658 cipher_xform->iv.length = 0;
659 cipher_xform->iv.offset = 0;
661 cap_idx.algo.cipher = cipher_xform->algo;
662 cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
664 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx);
666 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n",
671 if (rte_cryptodev_sym_capability_check_cipher(cap,
672 cipher_xform->key.length,
673 cipher_xform->iv.length) != 0) {
674 RTE_LOG(ERR, USER1, "PMD %s key length %u IV length %u\n",
675 info.device_name, cipher_xform->key.length,
676 cipher_xform->iv.length);
684 prepare_tdes_xform(struct rte_crypto_sym_xform *xform)
686 const struct rte_cryptodev_symmetric_capability *cap;
687 struct rte_cryptodev_sym_capability_idx cap_idx;
688 struct rte_crypto_cipher_xform *cipher_xform = &xform->cipher;
690 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
692 if (info.interim_info.tdes_data.test_mode == TDES_MODE_CBC)
693 cipher_xform->algo = RTE_CRYPTO_CIPHER_3DES_CBC;
695 cipher_xform->algo = RTE_CRYPTO_CIPHER_3DES_ECB;
696 cipher_xform->op = (info.op == FIPS_TEST_ENC_AUTH_GEN) ?
697 RTE_CRYPTO_CIPHER_OP_ENCRYPT :
698 RTE_CRYPTO_CIPHER_OP_DECRYPT;
699 cipher_xform->key.data = vec.cipher_auth.key.val;
700 cipher_xform->key.length = vec.cipher_auth.key.len;
702 if (cipher_xform->algo == RTE_CRYPTO_CIPHER_3DES_CBC) {
703 cipher_xform->iv.length = vec.iv.len;
704 cipher_xform->iv.offset = IV_OFF;
706 cipher_xform->iv.length = 0;
707 cipher_xform->iv.offset = 0;
709 cap_idx.algo.cipher = cipher_xform->algo;
710 cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
712 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx);
714 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n",
719 if (rte_cryptodev_sym_capability_check_cipher(cap,
720 cipher_xform->key.length,
721 cipher_xform->iv.length) != 0) {
722 RTE_LOG(ERR, USER1, "PMD %s key length %u IV length %u\n",
723 info.device_name, cipher_xform->key.length,
724 cipher_xform->iv.length);
732 prepare_hmac_xform(struct rte_crypto_sym_xform *xform)
734 const struct rte_cryptodev_symmetric_capability *cap;
735 struct rte_cryptodev_sym_capability_idx cap_idx;
736 struct rte_crypto_auth_xform *auth_xform = &xform->auth;
738 xform->type = RTE_CRYPTO_SYM_XFORM_AUTH;
740 auth_xform->algo = info.interim_info.hmac_data.algo;
741 auth_xform->op = RTE_CRYPTO_AUTH_OP_GENERATE;
742 auth_xform->digest_length = vec.cipher_auth.digest.len;
743 auth_xform->key.data = vec.cipher_auth.key.val;
744 auth_xform->key.length = vec.cipher_auth.key.len;
746 cap_idx.algo.auth = auth_xform->algo;
747 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
749 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx);
751 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n",
756 if (rte_cryptodev_sym_capability_check_auth(cap,
757 auth_xform->key.length,
758 auth_xform->digest_length, 0) != 0) {
759 RTE_LOG(ERR, USER1, "PMD %s key length %u IV length %u\n",
760 info.device_name, auth_xform->key.length,
761 auth_xform->digest_length);
769 prepare_gcm_xform(struct rte_crypto_sym_xform *xform)
771 const struct rte_cryptodev_symmetric_capability *cap;
772 struct rte_cryptodev_sym_capability_idx cap_idx;
773 struct rte_crypto_aead_xform *aead_xform = &xform->aead;
775 xform->type = RTE_CRYPTO_SYM_XFORM_AEAD;
777 aead_xform->algo = RTE_CRYPTO_AEAD_AES_GCM;
778 aead_xform->aad_length = vec.aead.aad.len;
779 aead_xform->digest_length = vec.aead.digest.len;
780 aead_xform->iv.offset = IV_OFF;
781 aead_xform->iv.length = vec.iv.len;
782 aead_xform->key.data = vec.aead.key.val;
783 aead_xform->key.length = vec.aead.key.len;
784 aead_xform->op = (info.op == FIPS_TEST_ENC_AUTH_GEN) ?
785 RTE_CRYPTO_AEAD_OP_ENCRYPT :
786 RTE_CRYPTO_AEAD_OP_DECRYPT;
788 cap_idx.algo.aead = aead_xform->algo;
789 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD;
791 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx);
793 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n",
798 if (rte_cryptodev_sym_capability_check_aead(cap,
799 aead_xform->key.length,
800 aead_xform->digest_length, aead_xform->aad_length,
801 aead_xform->iv.length) != 0) {
803 "PMD %s key_len %u tag_len %u aad_len %u iv_len %u\n",
804 info.device_name, aead_xform->key.length,
805 aead_xform->digest_length,
806 aead_xform->aad_length,
807 aead_xform->iv.length);
815 prepare_cmac_xform(struct rte_crypto_sym_xform *xform)
817 const struct rte_cryptodev_symmetric_capability *cap;
818 struct rte_cryptodev_sym_capability_idx cap_idx;
819 struct rte_crypto_auth_xform *auth_xform = &xform->auth;
821 xform->type = RTE_CRYPTO_SYM_XFORM_AUTH;
823 auth_xform->algo = RTE_CRYPTO_AUTH_AES_CMAC;
824 auth_xform->op = (info.op == FIPS_TEST_ENC_AUTH_GEN) ?
825 RTE_CRYPTO_AUTH_OP_GENERATE : RTE_CRYPTO_AUTH_OP_VERIFY;
826 auth_xform->digest_length = vec.cipher_auth.digest.len;
827 auth_xform->key.data = vec.cipher_auth.key.val;
828 auth_xform->key.length = vec.cipher_auth.key.len;
830 cap_idx.algo.auth = auth_xform->algo;
831 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
833 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx);
835 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n",
840 if (rte_cryptodev_sym_capability_check_auth(cap,
841 auth_xform->key.length,
842 auth_xform->digest_length, 0) != 0) {
843 RTE_LOG(ERR, USER1, "PMD %s key length %u IV length %u\n",
844 info.device_name, auth_xform->key.length,
845 auth_xform->digest_length);
853 prepare_ccm_xform(struct rte_crypto_sym_xform *xform)
855 const struct rte_cryptodev_symmetric_capability *cap;
856 struct rte_cryptodev_sym_capability_idx cap_idx;
857 struct rte_crypto_aead_xform *aead_xform = &xform->aead;
859 xform->type = RTE_CRYPTO_SYM_XFORM_AEAD;
861 aead_xform->algo = RTE_CRYPTO_AEAD_AES_CCM;
862 aead_xform->aad_length = vec.aead.aad.len;
863 aead_xform->digest_length = vec.aead.digest.len;
864 aead_xform->iv.offset = IV_OFF;
865 aead_xform->iv.length = vec.iv.len;
866 aead_xform->key.data = vec.aead.key.val;
867 aead_xform->key.length = vec.aead.key.len;
868 aead_xform->op = (info.op == FIPS_TEST_ENC_AUTH_GEN) ?
869 RTE_CRYPTO_AEAD_OP_ENCRYPT :
870 RTE_CRYPTO_AEAD_OP_DECRYPT;
872 cap_idx.algo.aead = aead_xform->algo;
873 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD;
875 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx);
877 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n",
882 if (rte_cryptodev_sym_capability_check_aead(cap,
883 aead_xform->key.length,
884 aead_xform->digest_length, aead_xform->aad_length,
885 aead_xform->iv.length) != 0) {
887 "PMD %s key_len %u tag_len %u aad_len %u iv_len %u\n",
888 info.device_name, aead_xform->key.length,
889 aead_xform->digest_length,
890 aead_xform->aad_length,
891 aead_xform->iv.length);
899 prepare_sha_xform(struct rte_crypto_sym_xform *xform)
901 const struct rte_cryptodev_symmetric_capability *cap;
902 struct rte_cryptodev_sym_capability_idx cap_idx;
903 struct rte_crypto_auth_xform *auth_xform = &xform->auth;
905 xform->type = RTE_CRYPTO_SYM_XFORM_AUTH;
907 auth_xform->algo = info.interim_info.sha_data.algo;
908 auth_xform->op = RTE_CRYPTO_AUTH_OP_GENERATE;
909 auth_xform->digest_length = vec.cipher_auth.digest.len;
911 cap_idx.algo.auth = auth_xform->algo;
912 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
914 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx);
916 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n",
921 if (rte_cryptodev_sym_capability_check_auth(cap,
922 auth_xform->key.length,
923 auth_xform->digest_length, 0) != 0) {
924 RTE_LOG(ERR, USER1, "PMD %s key length %u digest length %u\n",
925 info.device_name, auth_xform->key.length,
926 auth_xform->digest_length);
934 prepare_xts_xform(struct rte_crypto_sym_xform *xform)
936 const struct rte_cryptodev_symmetric_capability *cap;
937 struct rte_cryptodev_sym_capability_idx cap_idx;
938 struct rte_crypto_cipher_xform *cipher_xform = &xform->cipher;
940 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
942 cipher_xform->algo = RTE_CRYPTO_CIPHER_AES_XTS;
943 cipher_xform->op = (info.op == FIPS_TEST_ENC_AUTH_GEN) ?
944 RTE_CRYPTO_CIPHER_OP_ENCRYPT :
945 RTE_CRYPTO_CIPHER_OP_DECRYPT;
946 cipher_xform->key.data = vec.cipher_auth.key.val;
947 cipher_xform->key.length = vec.cipher_auth.key.len;
948 cipher_xform->iv.length = vec.iv.len;
949 cipher_xform->iv.offset = IV_OFF;
951 cap_idx.algo.cipher = RTE_CRYPTO_CIPHER_AES_XTS;
952 cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
954 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx);
956 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n",
961 if (rte_cryptodev_sym_capability_check_cipher(cap,
962 cipher_xform->key.length,
963 cipher_xform->iv.length) != 0) {
964 RTE_LOG(ERR, USER1, "PMD %s key length %u IV length %u\n",
965 info.device_name, cipher_xform->key.length,
966 cipher_xform->iv.length);
974 get_writeback_data(struct fips_val *val)
976 val->val = rte_pktmbuf_mtod(env.mbuf, uint8_t *);
977 val->len = rte_pktmbuf_pkt_len(env.mbuf);
983 struct rte_crypto_sym_xform xform = {0};
987 ret = test_ops.prepare_xform(&xform);
991 env.sess = rte_cryptodev_sym_session_create(env.sess_mpool);
995 ret = rte_cryptodev_sym_session_init(env.dev_id,
996 env.sess, &xform, env.sess_priv_mpool);
998 RTE_LOG(ERR, USER1, "Error %i: Init session\n",
1003 ret = test_ops.prepare_op();
1005 RTE_LOG(ERR, USER1, "Error %i: Prepare op\n",
1010 if (rte_cryptodev_enqueue_burst(env.dev_id, 0, &env.op, 1) < 1) {
1011 RTE_LOG(ERR, USER1, "Error: Failed enqueue\n");
1017 struct rte_crypto_op *deqd_op;
1019 n_deqd = rte_cryptodev_dequeue_burst(env.dev_id, 0, &deqd_op,
1021 } while (n_deqd == 0);
1023 vec.status = env.op->status;
1026 rte_cryptodev_sym_session_clear(env.dev_id, env.sess);
1027 rte_cryptodev_sym_session_free(env.sess);
1034 fips_generic_test(void)
1036 struct fips_val val;
1039 fips_test_write_one_case();
1041 ret = fips_run_test();
1043 if (ret == -EPERM || ret == -ENOTSUP) {
1044 fprintf(info.fp_wr, "Bypass\n\n");
1051 get_writeback_data(&val);
1053 switch (info.file_type) {
1056 if (info.parse_writeback == NULL)
1058 ret = info.parse_writeback(&val);
1063 if (info.kat_check == NULL)
1065 ret = info.kat_check(&val);
1071 fprintf(info.fp_wr, "\n");
1077 fips_mct_tdes_test(void)
1079 #define TDES_BLOCK_SIZE 8
1080 #define TDES_EXTERN_ITER 400
1081 #define TDES_INTERN_ITER 10000
1082 struct fips_val val, val_key;
1083 uint8_t prev_out[TDES_BLOCK_SIZE] = {0};
1084 uint8_t prev_prev_out[TDES_BLOCK_SIZE] = {0};
1085 uint8_t prev_in[TDES_BLOCK_SIZE] = {0};
1088 int test_mode = info.interim_info.tdes_data.test_mode;
1090 for (i = 0; i < TDES_EXTERN_ITER; i++) {
1091 if ((i == 0) && (info.version == 21.4f)) {
1092 if (!(strstr(info.vec[0], "COUNT")))
1093 fprintf(info.fp_wr, "%s%u\n", "COUNT = ", 0);
1099 fips_test_write_one_case();
1101 for (j = 0; j < TDES_INTERN_ITER; j++) {
1102 ret = fips_run_test();
1104 if (ret == -EPERM) {
1105 fprintf(info.fp_wr, "Bypass\n");
1111 get_writeback_data(&val);
1113 if (info.op == FIPS_TEST_DEC_AUTH_VERIF)
1114 memcpy(prev_in, vec.ct.val, TDES_BLOCK_SIZE);
1117 memcpy(prev_out, val.val, TDES_BLOCK_SIZE);
1119 if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
1120 if (test_mode == TDES_MODE_ECB) {
1121 memcpy(vec.pt.val, val.val,
1124 memcpy(vec.pt.val, vec.iv.val,
1126 memcpy(vec.iv.val, val.val,
1131 if (test_mode == TDES_MODE_ECB) {
1132 memcpy(vec.ct.val, val.val,
1135 memcpy(vec.iv.val, vec.ct.val,
1137 memcpy(vec.ct.val, val.val,
1144 if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
1145 if (test_mode == TDES_MODE_ECB) {
1146 memcpy(vec.pt.val, val.val,
1149 memcpy(vec.iv.val, val.val,
1151 memcpy(vec.pt.val, prev_out,
1155 if (test_mode == TDES_MODE_ECB) {
1156 memcpy(vec.ct.val, val.val,
1159 memcpy(vec.iv.val, vec.ct.val,
1161 memcpy(vec.ct.val, val.val,
1166 if (j == TDES_INTERN_ITER - 1)
1169 memcpy(prev_out, val.val, TDES_BLOCK_SIZE);
1171 if (j == TDES_INTERN_ITER - 3)
1172 memcpy(prev_prev_out, val.val, TDES_BLOCK_SIZE);
1175 info.parse_writeback(&val);
1176 fprintf(info.fp_wr, "\n");
1178 if (i == TDES_EXTERN_ITER - 1)
1182 memcpy(&val_key, &vec.cipher_auth.key, sizeof(val_key));
1184 if (info.interim_info.tdes_data.nb_keys == 0) {
1185 if (memcmp(val_key.val, val_key.val + 8, 8) == 0)
1186 info.interim_info.tdes_data.nb_keys = 1;
1187 else if (memcmp(val_key.val, val_key.val + 16, 8) == 0)
1188 info.interim_info.tdes_data.nb_keys = 2;
1190 info.interim_info.tdes_data.nb_keys = 3;
1194 for (k = 0; k < TDES_BLOCK_SIZE; k++) {
1196 switch (info.interim_info.tdes_data.nb_keys) {
1198 val_key.val[k] ^= val.val[k];
1199 val_key.val[k + 8] ^= prev_out[k];
1200 val_key.val[k + 16] ^= prev_prev_out[k];
1203 val_key.val[k] ^= val.val[k];
1204 val_key.val[k + 8] ^= prev_out[k];
1205 val_key.val[k + 16] ^= val.val[k];
1207 default: /* case 1 */
1208 val_key.val[k] ^= val.val[k];
1209 val_key.val[k + 8] ^= val.val[k];
1210 val_key.val[k + 16] ^= val.val[k];
1216 for (k = 0; k < 24; k++)
1217 val_key.val[k] = (__builtin_popcount(val_key.val[k]) &
1219 val_key.val[k] : (val_key.val[k] ^ 0x1);
1221 if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
1222 if (test_mode == TDES_MODE_ECB) {
1223 memcpy(vec.pt.val, val.val, TDES_BLOCK_SIZE);
1225 memcpy(vec.iv.val, val.val, TDES_BLOCK_SIZE);
1226 memcpy(vec.pt.val, prev_out, TDES_BLOCK_SIZE);
1229 if (test_mode == TDES_MODE_ECB) {
1230 memcpy(vec.ct.val, val.val, TDES_BLOCK_SIZE);
1232 memcpy(vec.iv.val, prev_out, TDES_BLOCK_SIZE);
1233 memcpy(vec.ct.val, val.val, TDES_BLOCK_SIZE);
1242 fips_mct_aes_ecb_test(void)
1244 #define AES_BLOCK_SIZE 16
1245 #define AES_EXTERN_ITER 100
1246 #define AES_INTERN_ITER 1000
1247 struct fips_val val, val_key;
1248 uint8_t prev_out[AES_BLOCK_SIZE] = {0};
1252 for (i = 0; i < AES_EXTERN_ITER; i++) {
1256 fips_test_write_one_case();
1258 for (j = 0; j < AES_INTERN_ITER; j++) {
1259 ret = fips_run_test();
1261 if (ret == -EPERM) {
1262 fprintf(info.fp_wr, "Bypass\n");
1269 get_writeback_data(&val);
1271 if (info.op == FIPS_TEST_ENC_AUTH_GEN)
1272 memcpy(vec.pt.val, val.val, AES_BLOCK_SIZE);
1274 memcpy(vec.ct.val, val.val, AES_BLOCK_SIZE);
1276 if (j == AES_INTERN_ITER - 1)
1279 memcpy(prev_out, val.val, AES_BLOCK_SIZE);
1282 info.parse_writeback(&val);
1283 fprintf(info.fp_wr, "\n");
1285 if (i == AES_EXTERN_ITER - 1)
1289 memcpy(&val_key, &vec.cipher_auth.key, sizeof(val_key));
1290 for (k = 0; k < vec.cipher_auth.key.len; k++) {
1291 switch (vec.cipher_auth.key.len) {
1293 val_key.val[k] ^= val.val[k];
1297 val_key.val[k] ^= prev_out[k + 8];
1299 val_key.val[k] ^= val.val[k - 8];
1303 val_key.val[k] ^= prev_out[k];
1305 val_key.val[k] ^= val.val[k - 16];
1316 fips_mct_aes_test(void)
1318 #define AES_BLOCK_SIZE 16
1319 #define AES_EXTERN_ITER 100
1320 #define AES_INTERN_ITER 1000
1321 struct fips_val val, val_key;
1322 uint8_t prev_out[AES_BLOCK_SIZE] = {0};
1323 uint8_t prev_in[AES_BLOCK_SIZE] = {0};
1327 if (info.interim_info.aes_data.cipher_algo == RTE_CRYPTO_CIPHER_AES_ECB)
1328 return fips_mct_aes_ecb_test();
1330 for (i = 0; i < AES_EXTERN_ITER; i++) {
1334 fips_test_write_one_case();
1336 for (j = 0; j < AES_INTERN_ITER; j++) {
1337 ret = fips_run_test();
1339 if (ret == -EPERM) {
1340 fprintf(info.fp_wr, "Bypass\n");
1347 get_writeback_data(&val);
1349 if (info.op == FIPS_TEST_DEC_AUTH_VERIF)
1350 memcpy(prev_in, vec.ct.val, AES_BLOCK_SIZE);
1353 memcpy(prev_out, val.val, AES_BLOCK_SIZE);
1355 if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
1356 memcpy(vec.pt.val, vec.iv.val,
1358 memcpy(vec.iv.val, val.val,
1361 memcpy(vec.ct.val, vec.iv.val,
1363 memcpy(vec.iv.val, prev_in,
1369 if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
1370 memcpy(vec.iv.val, val.val, AES_BLOCK_SIZE);
1371 memcpy(vec.pt.val, prev_out, AES_BLOCK_SIZE);
1373 memcpy(vec.iv.val, prev_in, AES_BLOCK_SIZE);
1374 memcpy(vec.ct.val, prev_out, AES_BLOCK_SIZE);
1377 if (j == AES_INTERN_ITER - 1)
1380 memcpy(prev_out, val.val, AES_BLOCK_SIZE);
1383 info.parse_writeback(&val);
1384 fprintf(info.fp_wr, "\n");
1386 if (i == AES_EXTERN_ITER - 1)
1390 memcpy(&val_key, &vec.cipher_auth.key, sizeof(val_key));
1391 for (k = 0; k < vec.cipher_auth.key.len; k++) {
1392 switch (vec.cipher_auth.key.len) {
1394 val_key.val[k] ^= val.val[k];
1398 val_key.val[k] ^= prev_out[k + 8];
1400 val_key.val[k] ^= val.val[k - 8];
1404 val_key.val[k] ^= prev_out[k];
1406 val_key.val[k] ^= val.val[k - 16];
1413 if (info.op == FIPS_TEST_DEC_AUTH_VERIF)
1414 memcpy(vec.iv.val, val.val, AES_BLOCK_SIZE);
1421 fips_mct_sha_test(void)
1423 #define SHA_EXTERN_ITER 100
1424 #define SHA_INTERN_ITER 1000
1425 #define SHA_MD_BLOCK 3
1426 struct fips_val val, md[SHA_MD_BLOCK];
1427 char temp[MAX_DIGEST_SIZE*2];
1431 val.val = rte_malloc(NULL, (MAX_DIGEST_SIZE*SHA_MD_BLOCK), 0);
1432 for (i = 0; i < SHA_MD_BLOCK; i++)
1433 md[i].val = rte_malloc(NULL, (MAX_DIGEST_SIZE*2), 0);
1435 rte_free(vec.pt.val);
1436 vec.pt.val = rte_malloc(NULL, (MAX_DIGEST_SIZE*SHA_MD_BLOCK), 0);
1438 fips_test_write_one_case();
1439 fprintf(info.fp_wr, "\n");
1441 for (j = 0; j < SHA_EXTERN_ITER; j++) {
1443 memcpy(md[0].val, vec.cipher_auth.digest.val,
1444 vec.cipher_auth.digest.len);
1445 md[0].len = vec.cipher_auth.digest.len;
1446 memcpy(md[1].val, vec.cipher_auth.digest.val,
1447 vec.cipher_auth.digest.len);
1448 md[1].len = vec.cipher_auth.digest.len;
1449 memcpy(md[2].val, vec.cipher_auth.digest.val,
1450 vec.cipher_auth.digest.len);
1451 md[2].len = vec.cipher_auth.digest.len;
1453 for (i = 0; i < (SHA_INTERN_ITER); i++) {
1455 memcpy(vec.pt.val, md[0].val,
1457 memcpy((vec.pt.val + md[0].len), md[1].val,
1459 memcpy((vec.pt.val + md[0].len + md[1].len),
1462 vec.pt.len = md[0].len + md[1].len + md[2].len;
1464 ret = fips_run_test();
1466 if (ret == -EPERM || ret == -ENOTSUP) {
1467 fprintf(info.fp_wr, "Bypass\n\n");
1473 get_writeback_data(&val);
1475 memcpy(md[0].val, md[1].val, md[1].len);
1476 md[0].len = md[1].len;
1477 memcpy(md[1].val, md[2].val, md[2].len);
1478 md[1].len = md[2].len;
1480 memcpy(md[2].val, (val.val + vec.pt.len),
1481 vec.cipher_auth.digest.len);
1482 md[2].len = vec.cipher_auth.digest.len;
1485 memcpy(vec.cipher_auth.digest.val, md[2].val, md[2].len);
1486 vec.cipher_auth.digest.len = md[2].len;
1488 fprintf(info.fp_wr, "COUNT = %u\n", j);
1490 writeback_hex_str("", temp, &vec.cipher_auth.digest);
1492 fprintf(info.fp_wr, "MD = %s\n\n", temp);
1495 for (i = 0; i < (SHA_MD_BLOCK); i++)
1496 rte_free(md[i].val);
1498 rte_free(vec.pt.val);
1507 switch (info.algo) {
1508 case FIPS_TEST_ALGO_AES:
1509 test_ops.prepare_op = prepare_cipher_op;
1510 test_ops.prepare_xform = prepare_aes_xform;
1511 if (info.interim_info.aes_data.test_type == AESAVS_TYPE_MCT)
1512 test_ops.test = fips_mct_aes_test;
1514 test_ops.test = fips_generic_test;
1516 case FIPS_TEST_ALGO_HMAC:
1517 test_ops.prepare_op = prepare_auth_op;
1518 test_ops.prepare_xform = prepare_hmac_xform;
1519 test_ops.test = fips_generic_test;
1521 case FIPS_TEST_ALGO_TDES:
1522 test_ops.prepare_op = prepare_cipher_op;
1523 test_ops.prepare_xform = prepare_tdes_xform;
1524 if (info.interim_info.tdes_data.test_type == TDES_MCT)
1525 test_ops.test = fips_mct_tdes_test;
1527 test_ops.test = fips_generic_test;
1529 case FIPS_TEST_ALGO_AES_GCM:
1530 test_ops.prepare_op = prepare_aead_op;
1531 test_ops.prepare_xform = prepare_gcm_xform;
1532 test_ops.test = fips_generic_test;
1534 case FIPS_TEST_ALGO_AES_CMAC:
1535 test_ops.prepare_op = prepare_auth_op;
1536 test_ops.prepare_xform = prepare_cmac_xform;
1537 test_ops.test = fips_generic_test;
1539 case FIPS_TEST_ALGO_AES_CCM:
1540 test_ops.prepare_op = prepare_aead_op;
1541 test_ops.prepare_xform = prepare_ccm_xform;
1542 test_ops.test = fips_generic_test;
1544 case FIPS_TEST_ALGO_SHA:
1545 test_ops.prepare_op = prepare_auth_op;
1546 test_ops.prepare_xform = prepare_sha_xform;
1547 if (info.interim_info.sha_data.test_type == SHA_MCT)
1548 test_ops.test = fips_mct_sha_test;
1550 test_ops.test = fips_generic_test;
1552 case FIPS_TEST_ALGO_AES_XTS:
1553 test_ops.prepare_op = prepare_cipher_op;
1554 test_ops.prepare_xform = prepare_xts_xform;
1555 test_ops.test = fips_generic_test;
1558 if (strstr(info.file_name, "TECB") ||
1559 strstr(info.file_name, "TCBC")) {
1560 info.algo = FIPS_TEST_ALGO_TDES;
1561 test_ops.prepare_op = prepare_cipher_op;
1562 test_ops.prepare_xform = prepare_tdes_xform;
1563 if (info.interim_info.tdes_data.test_type == TDES_MCT)
1564 test_ops.test = fips_mct_tdes_test;
1566 test_ops.test = fips_generic_test;
1576 print_test_block(void)
1580 for (i = 0; i < info.nb_vec_lines; i++)
1581 printf("%s\n", info.vec[i]);
1587 fips_test_one_file(void)
1589 int fetch_ret = 0, ret;
1592 ret = init_test_ops();
1594 RTE_LOG(ERR, USER1, "Error %i: Init test op\n", ret);
1598 while (ret >= 0 && fetch_ret == 0) {
1599 fetch_ret = fips_test_fetch_one_block();
1600 if (fetch_ret < 0) {
1601 RTE_LOG(ERR, USER1, "Error %i: Fetch block\n",
1604 goto error_one_case;
1607 if (info.nb_vec_lines == 0) {
1608 if (fetch_ret == -EOF)
1611 fprintf(info.fp_wr, "\n");
1615 ret = fips_test_parse_one_case();
1618 ret = test_ops.test();
1621 RTE_LOG(ERR, USER1, "Error %i: test block\n",
1623 goto error_one_case;
1627 RTE_LOG(ERR, USER1, "Error %i: Parse block\n",
1629 goto error_one_case;