1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2018 Intel Corporation 3 */ 4 5 #include <sys/stat.h> 6 #include <getopt.h> 7 #include <dirent.h> 8 9 #include <rte_cryptodev.h> 10 #include <rte_cryptodev_pmd.h> 11 #include <rte_mempool.h> 12 #include <rte_mbuf.h> 13 #include <rte_string_fns.h> 14 15 #include "fips_validation.h" 16 #include "fips_dev_self_test.h" 17 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" 28 29 struct fips_test_vector vec; 30 struct fips_test_interim_info info; 31 32 struct cryptodev_fips_validate_env { 33 const char *req_path; 34 const char *rsp_path; 35 uint32_t is_path_folder; 36 uint32_t dev_id; 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; 44 uint32_t self_test; 45 struct fips_dev_broken_test_config *broken_test_config; 46 } env; 47 48 static int 49 cryptodev_fips_validate_app_int(void) 50 { 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( 54 env.dev_id); 55 int ret; 56 57 if (env.self_test) { 58 ret = fips_dev_self_test(env.dev_id, env.broken_test_config); 59 if (ret < 0) { 60 struct rte_cryptodev *cryptodev = 61 rte_cryptodev_pmd_get_dev(env.dev_id); 62 63 rte_cryptodev_pmd_destroy(cryptodev); 64 65 return ret; 66 } 67 } 68 69 ret = rte_cryptodev_configure(env.dev_id, &conf); 70 if (ret < 0) 71 return ret; 72 73 env.mpool = rte_pktmbuf_pool_create("FIPS_MEMPOOL", 128, 0, 0, 74 UINT16_MAX, rte_socket_id()); 75 if (!env.mpool) 76 return ret; 77 78 ret = rte_cryptodev_queue_pair_setup(env.dev_id, 0, &qp_conf, 79 rte_socket_id()); 80 if (ret < 0) 81 return ret; 82 83 ret = -ENOMEM; 84 85 env.sess_mpool = rte_cryptodev_sym_session_pool_create( 86 "FIPS_SESS_MEMPOOL", 16, 0, 0, 0, rte_socket_id()); 87 if (!env.sess_mpool) 88 goto error_exit; 89 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) 94 goto error_exit; 95 96 env.op_pool = rte_crypto_op_pool_create( 97 "FIPS_OP_POOL", 98 RTE_CRYPTO_OP_TYPE_SYMMETRIC, 99 1, 0, 100 16, 101 rte_socket_id()); 102 if (!env.op_pool) 103 goto error_exit; 104 105 env.mbuf = rte_pktmbuf_alloc(env.mpool); 106 if (!env.mbuf) 107 goto error_exit; 108 109 env.op = rte_crypto_op_alloc(env.op_pool, RTE_CRYPTO_OP_TYPE_SYMMETRIC); 110 if (!env.op) 111 goto error_exit; 112 113 qp_conf.mp_session = env.sess_mpool; 114 qp_conf.mp_session_private = env.sess_priv_mpool; 115 116 ret = rte_cryptodev_queue_pair_setup(env.dev_id, 0, &qp_conf, 117 rte_socket_id()); 118 if (ret < 0) 119 goto error_exit; 120 121 return 0; 122 123 error_exit: 124 125 rte_mempool_free(env.mpool); 126 if (env.sess_mpool) 127 rte_mempool_free(env.sess_mpool); 128 if (env.sess_priv_mpool) 129 rte_mempool_free(env.sess_priv_mpool); 130 if (env.op_pool) 131 rte_mempool_free(env.op_pool); 132 133 return ret; 134 } 135 136 static void 137 cryptodev_fips_validate_app_uninit(void) 138 { 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); 147 } 148 149 static int 150 fips_test_one_file(void); 151 152 static int 153 parse_cryptodev_arg(char *arg) 154 { 155 int id = rte_cryptodev_get_dev_id(arg); 156 157 if (id < 0) { 158 RTE_LOG(ERR, USER1, "Error %i: invalid cryptodev name %s\n", 159 id, arg); 160 return id; 161 } 162 163 env.dev_id = (uint32_t)id; 164 165 return 0; 166 } 167 168 static int 169 parse_cryptodev_id_arg(char *arg) 170 { 171 uint32_t cryptodev_id; 172 173 if (parser_read_uint32(&cryptodev_id, arg) < 0) { 174 RTE_LOG(ERR, USER1, "Error %i: invalid cryptodev id %s\n", 175 -EINVAL, arg); 176 return -1; 177 } 178 179 180 if (!rte_cryptodev_pmd_is_valid_dev(cryptodev_id)) { 181 RTE_LOG(ERR, USER1, "Error %i: invalid cryptodev id %s\n", 182 cryptodev_id, arg); 183 return -1; 184 } 185 186 env.dev_id = (uint32_t)cryptodev_id; 187 188 return 0; 189 } 190 191 static void 192 cryptodev_fips_validate_usage(const char *prgname) 193 { 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); 207 } 208 209 static int 210 cryptodev_fips_validate_parse_args(int argc, char **argv) 211 { 212 int opt, ret; 213 char *prgname = argv[0]; 214 char **argvopt; 215 int option_index; 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}, 225 {NULL, 0, 0, 0} 226 }; 227 228 argvopt = argv; 229 230 while ((opt = getopt_long(argc, argvopt, "s:", 231 lgopts, &option_index)) != EOF) { 232 233 switch (opt) { 234 case 0: 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); 247 if (ret < 0) { 248 cryptodev_fips_validate_usage(prgname); 249 return -EINVAL; 250 } 251 } else if (strcmp(lgopts[option_index].name, 252 CRYPTODEV_ID_KEYWORD) == 0) { 253 ret = parse_cryptodev_id_arg(optarg); 254 if (ret < 0) { 255 cryptodev_fips_validate_usage(prgname); 256 return -EINVAL; 257 } 258 } else if (strcmp(lgopts[option_index].name, 259 CRYPTODEV_ST_KEYWORD) == 0) { 260 env.self_test = 1; 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( 265 NULL, 266 sizeof(*env.broken_test_config), 267 0); 268 if (!env.broken_test_config) 269 return -ENOMEM; 270 271 env.broken_test_config->expect_fail_dir = 272 self_test_dir_enc_auth_gen; 273 } 274 275 if (parser_read_uint32( 276 &env.broken_test_config->expect_fail_test_idx, 277 optarg) < 0) { 278 rte_free(env.broken_test_config); 279 cryptodev_fips_validate_usage(prgname); 280 return -EINVAL; 281 } 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( 286 NULL, 287 sizeof(*env.broken_test_config), 288 0); 289 if (!env.broken_test_config) 290 return -ENOMEM; 291 292 env.broken_test_config-> 293 expect_fail_test_idx = 0; 294 } 295 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) 300 == 0) 301 env.broken_test_config->expect_fail_dir = 302 self_test_dir_dec_auth_verify; 303 else { 304 rte_free(env.broken_test_config); 305 cryptodev_fips_validate_usage(prgname); 306 return -EINVAL; 307 } 308 } else { 309 cryptodev_fips_validate_usage(prgname); 310 return -EINVAL; 311 } 312 break; 313 default: 314 return -1; 315 } 316 } 317 318 if (env.dev_id == UINT32_MAX) { 319 RTE_LOG(ERR, USER1, "No device specified\n"); 320 cryptodev_fips_validate_usage(prgname); 321 return -EINVAL; 322 } 323 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); 328 return -EINVAL; 329 } 330 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); 334 return -EINVAL; 335 } 336 337 return 0; 338 } 339 340 int 341 main(int argc, char *argv[]) 342 { 343 int ret; 344 345 ret = rte_eal_init(argc, argv); 346 if (ret < 0) { 347 RTE_LOG(ERR, USER1, "Error %i: Failed init\n", ret); 348 return -1; 349 } 350 351 argc -= ret; 352 argv += ret; 353 354 ret = cryptodev_fips_validate_parse_args(argc, argv); 355 if (ret < 0) 356 rte_exit(EXIT_FAILURE, "Failed to parse arguments!\n"); 357 358 ret = cryptodev_fips_validate_app_int(); 359 if (ret < 0) { 360 RTE_LOG(ERR, USER1, "Error %i: Failed init\n", ret); 361 return -1; 362 } 363 364 if (env.req_path == NULL || env.rsp_path == NULL) { 365 printf("No request, exit.\n"); 366 goto exit; 367 } 368 369 if (!env.is_path_folder) { 370 printf("Processing file %s... ", env.req_path); 371 372 ret = fips_test_init(env.req_path, env.rsp_path, 373 rte_cryptodev_name_get(env.dev_id)); 374 if (ret < 0) { 375 RTE_LOG(ERR, USER1, "Error %i: Failed test %s\n", 376 ret, env.req_path); 377 goto exit; 378 } 379 380 381 ret = fips_test_one_file(); 382 if (ret < 0) { 383 RTE_LOG(ERR, USER1, "Error %i: Failed test %s\n", 384 ret, env.req_path); 385 goto exit; 386 } 387 388 printf("Done\n"); 389 390 } else { 391 struct dirent *dir; 392 DIR *d_req, *d_rsp; 393 char req_path[1024]; 394 char rsp_path[1024]; 395 396 d_req = opendir(env.req_path); 397 if (!d_req) { 398 RTE_LOG(ERR, USER1, "Error %i: Path %s not exist\n", 399 -EINVAL, env.req_path); 400 goto exit; 401 } 402 403 d_rsp = opendir(env.rsp_path); 404 if (!d_rsp) { 405 ret = mkdir(env.rsp_path, 0700); 406 if (ret == 0) 407 d_rsp = opendir(env.rsp_path); 408 else { 409 RTE_LOG(ERR, USER1, "Error %i: Invalid %s\n", 410 -EINVAL, env.rsp_path); 411 goto exit; 412 } 413 } 414 closedir(d_rsp); 415 416 while ((dir = readdir(d_req)) != NULL) { 417 if (strstr(dir->d_name, "req") == NULL) 418 continue; 419 420 snprintf(req_path, 1023, "%s/%s", env.req_path, 421 dir->d_name); 422 snprintf(rsp_path, 1023, "%s/%s", env.rsp_path, 423 dir->d_name); 424 strlcpy(strstr(rsp_path, "req"), "rsp", 4); 425 426 printf("Processing file %s... ", req_path); 427 428 ret = fips_test_init(req_path, rsp_path, 429 rte_cryptodev_name_get(env.dev_id)); 430 if (ret < 0) { 431 RTE_LOG(ERR, USER1, "Error %i: Failed test %s\n", 432 ret, req_path); 433 break; 434 } 435 436 ret = fips_test_one_file(); 437 if (ret < 0) { 438 RTE_LOG(ERR, USER1, "Error %i: Failed test %s\n", 439 ret, req_path); 440 break; 441 } 442 443 printf("Done\n"); 444 } 445 446 closedir(d_req); 447 } 448 449 450 exit: 451 fips_test_clear(); 452 cryptodev_fips_validate_app_uninit(); 453 454 return ret; 455 456 } 457 458 #define IV_OFF (sizeof(struct rte_crypto_op) + sizeof(struct rte_crypto_sym_op)) 459 #define CRYPTODEV_FIPS_MAX_RETRIES 16 460 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 *); 464 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; 469 } test_ops; 470 471 static int 472 prepare_cipher_op(void) 473 { 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); 476 477 __rte_crypto_op_reset(env.op, RTE_CRYPTO_OP_TYPE_SYMMETRIC); 478 rte_pktmbuf_reset(env.mbuf); 479 480 sym->m_src = env.mbuf; 481 sym->cipher.data.offset = 0; 482 483 memcpy(iv, vec.iv.val, vec.iv.len); 484 485 if (info.op == FIPS_TEST_ENC_AUTH_GEN) { 486 uint8_t *pt; 487 488 if (vec.pt.len > RTE_MBUF_MAX_NB_SEGS) { 489 RTE_LOG(ERR, USER1, "PT len %u\n", vec.pt.len); 490 return -EPERM; 491 } 492 493 pt = (uint8_t *)rte_pktmbuf_append(env.mbuf, vec.pt.len); 494 495 if (!pt) { 496 RTE_LOG(ERR, USER1, "Error %i: MBUF too small\n", 497 -ENOMEM); 498 return -ENOMEM; 499 } 500 501 memcpy(pt, vec.pt.val, vec.pt.len); 502 sym->cipher.data.length = vec.pt.len; 503 504 } else { 505 uint8_t *ct; 506 507 if (vec.ct.len > RTE_MBUF_MAX_NB_SEGS) { 508 RTE_LOG(ERR, USER1, "CT len %u\n", vec.ct.len); 509 return -EPERM; 510 } 511 512 ct = (uint8_t *)rte_pktmbuf_append(env.mbuf, vec.ct.len); 513 514 if (!ct) { 515 RTE_LOG(ERR, USER1, "Error %i: MBUF too small\n", 516 -ENOMEM); 517 return -ENOMEM; 518 } 519 520 memcpy(ct, vec.ct.val, vec.ct.len); 521 sym->cipher.data.length = vec.ct.len; 522 } 523 524 rte_crypto_op_attach_sym_session(env.op, env.sess); 525 526 return 0; 527 } 528 529 static int 530 prepare_auth_op(void) 531 { 532 struct rte_crypto_sym_op *sym = env.op->sym; 533 uint8_t *pt; 534 535 __rte_crypto_op_reset(env.op, RTE_CRYPTO_OP_TYPE_SYMMETRIC); 536 rte_pktmbuf_reset(env.mbuf); 537 538 sym->m_src = env.mbuf; 539 sym->auth.data.offset = 0; 540 541 pt = (uint8_t *)rte_pktmbuf_append(env.mbuf, vec.pt.len + 542 vec.cipher_auth.digest.len); 543 544 if (!pt) { 545 RTE_LOG(ERR, USER1, "Error %i: MBUF too small\n", 546 -ENOMEM); 547 return -ENOMEM; 548 } 549 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); 554 555 memcpy(pt, vec.pt.val, vec.pt.len); 556 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); 560 561 rte_crypto_op_attach_sym_session(env.op, env.sess); 562 563 return 0; 564 } 565 566 static int 567 prepare_aead_op(void) 568 { 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); 571 572 __rte_crypto_op_reset(env.op, RTE_CRYPTO_OP_TYPE_SYMMETRIC); 573 rte_pktmbuf_reset(env.mbuf); 574 575 if (info.algo == FIPS_TEST_ALGO_AES_CCM) 576 memcpy(iv + 1, vec.iv.val, vec.iv.len); 577 else 578 memcpy(iv, vec.iv.val, vec.iv.len); 579 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); 584 585 if (info.op == FIPS_TEST_ENC_AUTH_GEN) { 586 uint8_t *pt; 587 588 if (vec.pt.len > RTE_MBUF_MAX_NB_SEGS) { 589 RTE_LOG(ERR, USER1, "PT len %u\n", vec.pt.len); 590 return -EPERM; 591 } 592 593 pt = (uint8_t *)rte_pktmbuf_append(env.mbuf, 594 vec.pt.len + vec.aead.digest.len); 595 596 if (!pt) { 597 RTE_LOG(ERR, USER1, "Error %i: MBUF too small\n", 598 -ENOMEM); 599 return -ENOMEM; 600 } 601 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); 607 } else { 608 uint8_t *ct; 609 610 if (vec.ct.len > RTE_MBUF_MAX_NB_SEGS) { 611 RTE_LOG(ERR, USER1, "CT len %u\n", vec.ct.len); 612 return -EPERM; 613 } 614 615 ct = (uint8_t *)rte_pktmbuf_append(env.mbuf, vec.ct.len); 616 617 if (!ct) { 618 RTE_LOG(ERR, USER1, "Error %i: MBUF too small\n", 619 -ENOMEM); 620 return -ENOMEM; 621 } 622 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); 628 } 629 630 rte_crypto_op_attach_sym_session(env.op, env.sess); 631 632 return 0; 633 } 634 635 static int 636 prepare_aes_xform(struct rte_crypto_sym_xform *xform) 637 { 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; 641 642 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER; 643 644 if (info.interim_info.aes_data.cipher_algo == RTE_CRYPTO_CIPHER_AES_CBC) 645 cipher_xform->algo = RTE_CRYPTO_CIPHER_AES_CBC; 646 else 647 cipher_xform->algo = RTE_CRYPTO_CIPHER_AES_ECB; 648 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; 657 } else { 658 cipher_xform->iv.length = 0; 659 cipher_xform->iv.offset = 0; 660 } 661 cap_idx.algo.cipher = cipher_xform->algo; 662 cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER; 663 664 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx); 665 if (!cap) { 666 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n", 667 env.dev_id); 668 return -EINVAL; 669 } 670 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); 677 return -EPERM; 678 } 679 680 return 0; 681 } 682 683 static int 684 prepare_tdes_xform(struct rte_crypto_sym_xform *xform) 685 { 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; 689 690 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER; 691 692 if (info.interim_info.tdes_data.test_mode == TDES_MODE_CBC) 693 cipher_xform->algo = RTE_CRYPTO_CIPHER_3DES_CBC; 694 else 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; 701 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; 705 } else { 706 cipher_xform->iv.length = 0; 707 cipher_xform->iv.offset = 0; 708 } 709 cap_idx.algo.cipher = cipher_xform->algo; 710 cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER; 711 712 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx); 713 if (!cap) { 714 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n", 715 env.dev_id); 716 return -EINVAL; 717 } 718 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); 725 return -EPERM; 726 } 727 728 return 0; 729 } 730 731 static int 732 prepare_hmac_xform(struct rte_crypto_sym_xform *xform) 733 { 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; 737 738 xform->type = RTE_CRYPTO_SYM_XFORM_AUTH; 739 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; 745 746 cap_idx.algo.auth = auth_xform->algo; 747 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH; 748 749 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx); 750 if (!cap) { 751 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n", 752 env.dev_id); 753 return -EINVAL; 754 } 755 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); 762 return -EPERM; 763 } 764 765 return 0; 766 } 767 768 static int 769 prepare_gcm_xform(struct rte_crypto_sym_xform *xform) 770 { 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; 774 775 xform->type = RTE_CRYPTO_SYM_XFORM_AEAD; 776 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; 787 788 cap_idx.algo.aead = aead_xform->algo; 789 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD; 790 791 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx); 792 if (!cap) { 793 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n", 794 env.dev_id); 795 return -EINVAL; 796 } 797 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) { 802 RTE_LOG(ERR, USER1, 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); 808 return -EPERM; 809 } 810 811 return 0; 812 } 813 814 static int 815 prepare_cmac_xform(struct rte_crypto_sym_xform *xform) 816 { 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; 820 821 xform->type = RTE_CRYPTO_SYM_XFORM_AUTH; 822 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; 829 830 cap_idx.algo.auth = auth_xform->algo; 831 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH; 832 833 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx); 834 if (!cap) { 835 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n", 836 env.dev_id); 837 return -EINVAL; 838 } 839 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); 846 return -EPERM; 847 } 848 849 return 0; 850 } 851 852 static int 853 prepare_ccm_xform(struct rte_crypto_sym_xform *xform) 854 { 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; 858 859 xform->type = RTE_CRYPTO_SYM_XFORM_AEAD; 860 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; 871 872 cap_idx.algo.aead = aead_xform->algo; 873 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD; 874 875 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx); 876 if (!cap) { 877 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n", 878 env.dev_id); 879 return -EINVAL; 880 } 881 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) { 886 RTE_LOG(ERR, USER1, 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); 892 return -EPERM; 893 } 894 895 return 0; 896 } 897 898 static int 899 prepare_sha_xform(struct rte_crypto_sym_xform *xform) 900 { 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; 904 905 xform->type = RTE_CRYPTO_SYM_XFORM_AUTH; 906 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; 910 911 cap_idx.algo.auth = auth_xform->algo; 912 cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH; 913 914 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx); 915 if (!cap) { 916 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n", 917 env.dev_id); 918 return -EINVAL; 919 } 920 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); 927 return -EPERM; 928 } 929 930 return 0; 931 } 932 933 static int 934 prepare_xts_xform(struct rte_crypto_sym_xform *xform) 935 { 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; 939 940 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER; 941 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; 950 951 cap_idx.algo.cipher = RTE_CRYPTO_CIPHER_AES_XTS; 952 cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER; 953 954 cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx); 955 if (!cap) { 956 RTE_LOG(ERR, USER1, "Failed to get capability for cdev %u\n", 957 env.dev_id); 958 return -EINVAL; 959 } 960 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); 967 return -EPERM; 968 } 969 970 return 0; 971 } 972 973 static void 974 get_writeback_data(struct fips_val *val) 975 { 976 val->val = rte_pktmbuf_mtod(env.mbuf, uint8_t *); 977 val->len = rte_pktmbuf_pkt_len(env.mbuf); 978 } 979 980 static int 981 fips_run_test(void) 982 { 983 struct rte_crypto_sym_xform xform = {0}; 984 uint16_t n_deqd; 985 int ret; 986 987 ret = test_ops.prepare_xform(&xform); 988 if (ret < 0) 989 return ret; 990 991 env.sess = rte_cryptodev_sym_session_create(env.sess_mpool); 992 if (!env.sess) 993 return -ENOMEM; 994 995 ret = rte_cryptodev_sym_session_init(env.dev_id, 996 env.sess, &xform, env.sess_priv_mpool); 997 if (ret < 0) { 998 RTE_LOG(ERR, USER1, "Error %i: Init session\n", 999 ret); 1000 goto exit; 1001 } 1002 1003 ret = test_ops.prepare_op(); 1004 if (ret < 0) { 1005 RTE_LOG(ERR, USER1, "Error %i: Prepare op\n", 1006 ret); 1007 goto exit; 1008 } 1009 1010 if (rte_cryptodev_enqueue_burst(env.dev_id, 0, &env.op, 1) < 1) { 1011 RTE_LOG(ERR, USER1, "Error: Failed enqueue\n"); 1012 ret = -1; 1013 goto exit; 1014 } 1015 1016 do { 1017 struct rte_crypto_op *deqd_op; 1018 1019 n_deqd = rte_cryptodev_dequeue_burst(env.dev_id, 0, &deqd_op, 1020 1); 1021 } while (n_deqd == 0); 1022 1023 vec.status = env.op->status; 1024 1025 exit: 1026 rte_cryptodev_sym_session_clear(env.dev_id, env.sess); 1027 rte_cryptodev_sym_session_free(env.sess); 1028 env.sess = NULL; 1029 1030 return ret; 1031 } 1032 1033 static int 1034 fips_generic_test(void) 1035 { 1036 struct fips_val val; 1037 int ret; 1038 1039 fips_test_write_one_case(); 1040 1041 ret = fips_run_test(); 1042 if (ret < 0) { 1043 if (ret == -EPERM || ret == -ENOTSUP) { 1044 fprintf(info.fp_wr, "Bypass\n\n"); 1045 return 0; 1046 } 1047 1048 return ret; 1049 } 1050 1051 get_writeback_data(&val); 1052 1053 switch (info.file_type) { 1054 case FIPS_TYPE_REQ: 1055 case FIPS_TYPE_RSP: 1056 if (info.parse_writeback == NULL) 1057 return -EPERM; 1058 ret = info.parse_writeback(&val); 1059 if (ret < 0) 1060 return ret; 1061 break; 1062 case FIPS_TYPE_FAX: 1063 if (info.kat_check == NULL) 1064 return -EPERM; 1065 ret = info.kat_check(&val); 1066 if (ret < 0) 1067 return ret; 1068 break; 1069 } 1070 1071 fprintf(info.fp_wr, "\n"); 1072 1073 return 0; 1074 } 1075 1076 static int 1077 fips_mct_tdes_test(void) 1078 { 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}; 1086 uint32_t i, j, k; 1087 int ret; 1088 int test_mode = info.interim_info.tdes_data.test_mode; 1089 1090 for (i = 0; i < TDES_EXTERN_ITER; i++) { 1091 if (i == 0) { 1092 if (!(strstr(info.vec[0], "COUNT"))) 1093 fprintf(info.fp_wr, "%s%u\n", "COUNT = ", 0); 1094 } else 1095 update_info_vec(i); 1096 1097 fips_test_write_one_case(); 1098 1099 for (j = 0; j < TDES_INTERN_ITER; j++) { 1100 ret = fips_run_test(); 1101 if (ret < 0) { 1102 if (ret == -EPERM) { 1103 fprintf(info.fp_wr, "Bypass\n"); 1104 return 0; 1105 } 1106 return ret; 1107 } 1108 1109 get_writeback_data(&val); 1110 1111 if (info.op == FIPS_TEST_DEC_AUTH_VERIF) 1112 memcpy(prev_in, vec.ct.val, TDES_BLOCK_SIZE); 1113 1114 if (j == 0) { 1115 memcpy(prev_out, val.val, TDES_BLOCK_SIZE); 1116 1117 if (info.op == FIPS_TEST_ENC_AUTH_GEN) { 1118 if (test_mode == TDES_MODE_ECB) { 1119 memcpy(vec.pt.val, val.val, 1120 TDES_BLOCK_SIZE); 1121 } else { 1122 memcpy(vec.pt.val, vec.iv.val, 1123 TDES_BLOCK_SIZE); 1124 memcpy(vec.iv.val, val.val, 1125 TDES_BLOCK_SIZE); 1126 } 1127 1128 } else { 1129 if (test_mode == TDES_MODE_ECB) { 1130 memcpy(vec.ct.val, val.val, 1131 TDES_BLOCK_SIZE); 1132 } else { 1133 memcpy(vec.iv.val, vec.ct.val, 1134 TDES_BLOCK_SIZE); 1135 memcpy(vec.ct.val, val.val, 1136 TDES_BLOCK_SIZE); 1137 } 1138 } 1139 continue; 1140 } 1141 1142 if (info.op == FIPS_TEST_ENC_AUTH_GEN) { 1143 if (test_mode == TDES_MODE_ECB) { 1144 memcpy(vec.pt.val, val.val, 1145 TDES_BLOCK_SIZE); 1146 } else { 1147 memcpy(vec.iv.val, val.val, 1148 TDES_BLOCK_SIZE); 1149 memcpy(vec.pt.val, prev_out, 1150 TDES_BLOCK_SIZE); 1151 } 1152 } else { 1153 if (test_mode == TDES_MODE_ECB) { 1154 memcpy(vec.ct.val, val.val, 1155 TDES_BLOCK_SIZE); 1156 } else { 1157 memcpy(vec.iv.val, vec.ct.val, 1158 TDES_BLOCK_SIZE); 1159 memcpy(vec.ct.val, val.val, 1160 TDES_BLOCK_SIZE); 1161 } 1162 } 1163 1164 if (j == TDES_INTERN_ITER - 1) 1165 continue; 1166 1167 memcpy(prev_out, val.val, TDES_BLOCK_SIZE); 1168 1169 if (j == TDES_INTERN_ITER - 3) 1170 memcpy(prev_prev_out, val.val, TDES_BLOCK_SIZE); 1171 } 1172 1173 info.parse_writeback(&val); 1174 fprintf(info.fp_wr, "\n"); 1175 1176 if (i == TDES_EXTERN_ITER - 1) 1177 continue; 1178 1179 /** update key */ 1180 memcpy(&val_key, &vec.cipher_auth.key, sizeof(val_key)); 1181 1182 if (info.interim_info.tdes_data.nb_keys == 0) { 1183 if (memcmp(val_key.val, val_key.val + 8, 8) == 0) 1184 info.interim_info.tdes_data.nb_keys = 1; 1185 else if (memcmp(val_key.val, val_key.val + 16, 8) == 0) 1186 info.interim_info.tdes_data.nb_keys = 2; 1187 else 1188 info.interim_info.tdes_data.nb_keys = 3; 1189 1190 } 1191 1192 for (k = 0; k < TDES_BLOCK_SIZE; k++) { 1193 1194 switch (info.interim_info.tdes_data.nb_keys) { 1195 case 3: 1196 val_key.val[k] ^= val.val[k]; 1197 val_key.val[k + 8] ^= prev_out[k]; 1198 val_key.val[k + 16] ^= prev_prev_out[k]; 1199 break; 1200 case 2: 1201 val_key.val[k] ^= val.val[k]; 1202 val_key.val[k + 8] ^= prev_out[k]; 1203 val_key.val[k + 16] ^= val.val[k]; 1204 break; 1205 default: /* case 1 */ 1206 val_key.val[k] ^= val.val[k]; 1207 val_key.val[k + 8] ^= val.val[k]; 1208 val_key.val[k + 16] ^= val.val[k]; 1209 break; 1210 } 1211 1212 } 1213 1214 for (k = 0; k < 24; k++) 1215 val_key.val[k] = (__builtin_popcount(val_key.val[k]) & 1216 0x1) ? 1217 val_key.val[k] : (val_key.val[k] ^ 0x1); 1218 1219 if (info.op == FIPS_TEST_ENC_AUTH_GEN) { 1220 if (test_mode == TDES_MODE_ECB) { 1221 memcpy(vec.pt.val, val.val, TDES_BLOCK_SIZE); 1222 } else { 1223 memcpy(vec.iv.val, val.val, TDES_BLOCK_SIZE); 1224 memcpy(vec.pt.val, prev_out, TDES_BLOCK_SIZE); 1225 } 1226 } else { 1227 if (test_mode == TDES_MODE_ECB) { 1228 memcpy(vec.ct.val, val.val, TDES_BLOCK_SIZE); 1229 } else { 1230 memcpy(vec.iv.val, prev_out, TDES_BLOCK_SIZE); 1231 memcpy(vec.ct.val, val.val, TDES_BLOCK_SIZE); 1232 } 1233 } 1234 } 1235 1236 return 0; 1237 } 1238 1239 static int 1240 fips_mct_aes_ecb_test(void) 1241 { 1242 #define AES_BLOCK_SIZE 16 1243 #define AES_EXTERN_ITER 100 1244 #define AES_INTERN_ITER 1000 1245 struct fips_val val, val_key; 1246 uint8_t prev_out[AES_BLOCK_SIZE] = {0}; 1247 uint32_t i, j, k; 1248 int ret; 1249 1250 for (i = 0; i < AES_EXTERN_ITER; i++) { 1251 if (i != 0) 1252 update_info_vec(i); 1253 1254 fips_test_write_one_case(); 1255 1256 for (j = 0; j < AES_INTERN_ITER; j++) { 1257 ret = fips_run_test(); 1258 if (ret < 0) { 1259 if (ret == -EPERM) { 1260 fprintf(info.fp_wr, "Bypass\n"); 1261 return 0; 1262 } 1263 1264 return ret; 1265 } 1266 1267 get_writeback_data(&val); 1268 1269 if (info.op == FIPS_TEST_ENC_AUTH_GEN) 1270 memcpy(vec.pt.val, val.val, AES_BLOCK_SIZE); 1271 else 1272 memcpy(vec.ct.val, val.val, AES_BLOCK_SIZE); 1273 1274 if (j == AES_INTERN_ITER - 1) 1275 continue; 1276 1277 memcpy(prev_out, val.val, AES_BLOCK_SIZE); 1278 } 1279 1280 info.parse_writeback(&val); 1281 fprintf(info.fp_wr, "\n"); 1282 1283 if (i == AES_EXTERN_ITER - 1) 1284 continue; 1285 1286 /** update key */ 1287 memcpy(&val_key, &vec.cipher_auth.key, sizeof(val_key)); 1288 for (k = 0; k < vec.cipher_auth.key.len; k++) { 1289 switch (vec.cipher_auth.key.len) { 1290 case 16: 1291 val_key.val[k] ^= val.val[k]; 1292 break; 1293 case 24: 1294 if (k < 8) 1295 val_key.val[k] ^= prev_out[k + 8]; 1296 else 1297 val_key.val[k] ^= val.val[k - 8]; 1298 break; 1299 case 32: 1300 if (k < 16) 1301 val_key.val[k] ^= prev_out[k]; 1302 else 1303 val_key.val[k] ^= val.val[k - 16]; 1304 break; 1305 default: 1306 return -1; 1307 } 1308 } 1309 } 1310 1311 return 0; 1312 } 1313 static int 1314 fips_mct_aes_test(void) 1315 { 1316 #define AES_BLOCK_SIZE 16 1317 #define AES_EXTERN_ITER 100 1318 #define AES_INTERN_ITER 1000 1319 struct fips_val val, val_key; 1320 uint8_t prev_out[AES_BLOCK_SIZE] = {0}; 1321 uint8_t prev_in[AES_BLOCK_SIZE] = {0}; 1322 uint32_t i, j, k; 1323 int ret; 1324 1325 if (info.interim_info.aes_data.cipher_algo == RTE_CRYPTO_CIPHER_AES_ECB) 1326 return fips_mct_aes_ecb_test(); 1327 1328 for (i = 0; i < AES_EXTERN_ITER; i++) { 1329 if (i != 0) 1330 update_info_vec(i); 1331 1332 fips_test_write_one_case(); 1333 1334 for (j = 0; j < AES_INTERN_ITER; j++) { 1335 ret = fips_run_test(); 1336 if (ret < 0) { 1337 if (ret == -EPERM) { 1338 fprintf(info.fp_wr, "Bypass\n"); 1339 return 0; 1340 } 1341 1342 return ret; 1343 } 1344 1345 get_writeback_data(&val); 1346 1347 if (info.op == FIPS_TEST_DEC_AUTH_VERIF) 1348 memcpy(prev_in, vec.ct.val, AES_BLOCK_SIZE); 1349 1350 if (j == 0) { 1351 memcpy(prev_out, val.val, AES_BLOCK_SIZE); 1352 1353 if (info.op == FIPS_TEST_ENC_AUTH_GEN) { 1354 memcpy(vec.pt.val, vec.iv.val, 1355 AES_BLOCK_SIZE); 1356 memcpy(vec.iv.val, val.val, 1357 AES_BLOCK_SIZE); 1358 } else { 1359 memcpy(vec.ct.val, vec.iv.val, 1360 AES_BLOCK_SIZE); 1361 memcpy(vec.iv.val, prev_in, 1362 AES_BLOCK_SIZE); 1363 } 1364 continue; 1365 } 1366 1367 if (info.op == FIPS_TEST_ENC_AUTH_GEN) { 1368 memcpy(vec.iv.val, val.val, AES_BLOCK_SIZE); 1369 memcpy(vec.pt.val, prev_out, AES_BLOCK_SIZE); 1370 } else { 1371 memcpy(vec.iv.val, prev_in, AES_BLOCK_SIZE); 1372 memcpy(vec.ct.val, prev_out, AES_BLOCK_SIZE); 1373 } 1374 1375 if (j == AES_INTERN_ITER - 1) 1376 continue; 1377 1378 memcpy(prev_out, val.val, AES_BLOCK_SIZE); 1379 } 1380 1381 info.parse_writeback(&val); 1382 fprintf(info.fp_wr, "\n"); 1383 1384 if (i == AES_EXTERN_ITER - 1) 1385 continue; 1386 1387 /** update key */ 1388 memcpy(&val_key, &vec.cipher_auth.key, sizeof(val_key)); 1389 for (k = 0; k < vec.cipher_auth.key.len; k++) { 1390 switch (vec.cipher_auth.key.len) { 1391 case 16: 1392 val_key.val[k] ^= val.val[k]; 1393 break; 1394 case 24: 1395 if (k < 8) 1396 val_key.val[k] ^= prev_out[k + 8]; 1397 else 1398 val_key.val[k] ^= val.val[k - 8]; 1399 break; 1400 case 32: 1401 if (k < 16) 1402 val_key.val[k] ^= prev_out[k]; 1403 else 1404 val_key.val[k] ^= val.val[k - 16]; 1405 break; 1406 default: 1407 return -1; 1408 } 1409 } 1410 1411 if (info.op == FIPS_TEST_DEC_AUTH_VERIF) 1412 memcpy(vec.iv.val, val.val, AES_BLOCK_SIZE); 1413 } 1414 1415 return 0; 1416 } 1417 1418 static int 1419 fips_mct_sha_test(void) 1420 { 1421 #define SHA_EXTERN_ITER 100 1422 #define SHA_INTERN_ITER 1000 1423 #define SHA_MD_BLOCK 3 1424 struct fips_val val, md[SHA_MD_BLOCK]; 1425 char temp[MAX_DIGEST_SIZE*2]; 1426 int ret; 1427 uint32_t i, j; 1428 1429 val.val = rte_malloc(NULL, (MAX_DIGEST_SIZE*SHA_MD_BLOCK), 0); 1430 for (i = 0; i < SHA_MD_BLOCK; i++) 1431 md[i].val = rte_malloc(NULL, (MAX_DIGEST_SIZE*2), 0); 1432 1433 rte_free(vec.pt.val); 1434 vec.pt.val = rte_malloc(NULL, (MAX_DIGEST_SIZE*SHA_MD_BLOCK), 0); 1435 1436 fips_test_write_one_case(); 1437 fprintf(info.fp_wr, "\n"); 1438 1439 for (j = 0; j < SHA_EXTERN_ITER; j++) { 1440 1441 memcpy(md[0].val, vec.cipher_auth.digest.val, 1442 vec.cipher_auth.digest.len); 1443 md[0].len = vec.cipher_auth.digest.len; 1444 memcpy(md[1].val, vec.cipher_auth.digest.val, 1445 vec.cipher_auth.digest.len); 1446 md[1].len = vec.cipher_auth.digest.len; 1447 memcpy(md[2].val, vec.cipher_auth.digest.val, 1448 vec.cipher_auth.digest.len); 1449 md[2].len = vec.cipher_auth.digest.len; 1450 1451 for (i = 0; i < (SHA_INTERN_ITER); i++) { 1452 1453 memcpy(vec.pt.val, md[0].val, 1454 (size_t)md[0].len); 1455 memcpy((vec.pt.val + md[0].len), md[1].val, 1456 (size_t)md[1].len); 1457 memcpy((vec.pt.val + md[0].len + md[1].len), 1458 md[2].val, 1459 (size_t)md[2].len); 1460 vec.pt.len = md[0].len + md[1].len + md[2].len; 1461 1462 ret = fips_run_test(); 1463 if (ret < 0) { 1464 if (ret == -EPERM || ret == -ENOTSUP) { 1465 fprintf(info.fp_wr, "Bypass\n\n"); 1466 return 0; 1467 } 1468 return ret; 1469 } 1470 1471 get_writeback_data(&val); 1472 1473 memcpy(md[0].val, md[1].val, md[1].len); 1474 md[0].len = md[1].len; 1475 memcpy(md[1].val, md[2].val, md[2].len); 1476 md[1].len = md[2].len; 1477 1478 memcpy(md[2].val, (val.val + vec.pt.len), 1479 vec.cipher_auth.digest.len); 1480 md[2].len = vec.cipher_auth.digest.len; 1481 } 1482 1483 memcpy(vec.cipher_auth.digest.val, md[2].val, md[2].len); 1484 vec.cipher_auth.digest.len = md[2].len; 1485 1486 fprintf(info.fp_wr, "COUNT = %u\n", j); 1487 1488 writeback_hex_str("", temp, &vec.cipher_auth.digest); 1489 1490 fprintf(info.fp_wr, "MD = %s\n\n", temp); 1491 } 1492 1493 for (i = 0; i < (SHA_MD_BLOCK); i++) 1494 rte_free(md[i].val); 1495 1496 rte_free(vec.pt.val); 1497 1498 return 0; 1499 } 1500 1501 1502 static int 1503 init_test_ops(void) 1504 { 1505 switch (info.algo) { 1506 case FIPS_TEST_ALGO_AES: 1507 test_ops.prepare_op = prepare_cipher_op; 1508 test_ops.prepare_xform = prepare_aes_xform; 1509 if (info.interim_info.aes_data.test_type == AESAVS_TYPE_MCT) 1510 test_ops.test = fips_mct_aes_test; 1511 else 1512 test_ops.test = fips_generic_test; 1513 break; 1514 case FIPS_TEST_ALGO_HMAC: 1515 test_ops.prepare_op = prepare_auth_op; 1516 test_ops.prepare_xform = prepare_hmac_xform; 1517 test_ops.test = fips_generic_test; 1518 break; 1519 case FIPS_TEST_ALGO_TDES: 1520 test_ops.prepare_op = prepare_cipher_op; 1521 test_ops.prepare_xform = prepare_tdes_xform; 1522 if (info.interim_info.tdes_data.test_type == TDES_MCT) 1523 test_ops.test = fips_mct_tdes_test; 1524 else 1525 test_ops.test = fips_generic_test; 1526 break; 1527 case FIPS_TEST_ALGO_AES_GCM: 1528 test_ops.prepare_op = prepare_aead_op; 1529 test_ops.prepare_xform = prepare_gcm_xform; 1530 test_ops.test = fips_generic_test; 1531 break; 1532 case FIPS_TEST_ALGO_AES_CMAC: 1533 test_ops.prepare_op = prepare_auth_op; 1534 test_ops.prepare_xform = prepare_cmac_xform; 1535 test_ops.test = fips_generic_test; 1536 break; 1537 case FIPS_TEST_ALGO_AES_CCM: 1538 test_ops.prepare_op = prepare_aead_op; 1539 test_ops.prepare_xform = prepare_ccm_xform; 1540 test_ops.test = fips_generic_test; 1541 break; 1542 case FIPS_TEST_ALGO_SHA: 1543 test_ops.prepare_op = prepare_auth_op; 1544 test_ops.prepare_xform = prepare_sha_xform; 1545 if (info.interim_info.sha_data.test_type == SHA_MCT) 1546 test_ops.test = fips_mct_sha_test; 1547 else 1548 test_ops.test = fips_generic_test; 1549 break; 1550 case FIPS_TEST_ALGO_AES_XTS: 1551 test_ops.prepare_op = prepare_cipher_op; 1552 test_ops.prepare_xform = prepare_xts_xform; 1553 test_ops.test = fips_generic_test; 1554 break; 1555 default: 1556 if (strstr(info.file_name, "TECB") || 1557 strstr(info.file_name, "TCBC")) { 1558 info.algo = FIPS_TEST_ALGO_TDES; 1559 test_ops.prepare_op = prepare_cipher_op; 1560 test_ops.prepare_xform = prepare_tdes_xform; 1561 if (info.interim_info.tdes_data.test_type == TDES_MCT) 1562 test_ops.test = fips_mct_tdes_test; 1563 else 1564 test_ops.test = fips_generic_test; 1565 break; 1566 } 1567 return -1; 1568 } 1569 1570 return 0; 1571 } 1572 1573 static void 1574 print_test_block(void) 1575 { 1576 uint32_t i; 1577 1578 for (i = 0; i < info.nb_vec_lines; i++) 1579 printf("%s\n", info.vec[i]); 1580 1581 printf("\n"); 1582 } 1583 1584 static int 1585 fips_test_one_file(void) 1586 { 1587 int fetch_ret = 0, ret; 1588 1589 1590 ret = init_test_ops(); 1591 if (ret < 0) { 1592 RTE_LOG(ERR, USER1, "Error %i: Init test op\n", ret); 1593 return ret; 1594 } 1595 1596 while (ret >= 0 && fetch_ret == 0) { 1597 fetch_ret = fips_test_fetch_one_block(); 1598 if (fetch_ret < 0) { 1599 RTE_LOG(ERR, USER1, "Error %i: Fetch block\n", 1600 fetch_ret); 1601 ret = fetch_ret; 1602 goto error_one_case; 1603 } 1604 1605 if (info.nb_vec_lines == 0) { 1606 if (fetch_ret == -EOF) 1607 break; 1608 1609 fprintf(info.fp_wr, "\n"); 1610 continue; 1611 } 1612 1613 ret = fips_test_parse_one_case(); 1614 switch (ret) { 1615 case 0: 1616 ret = test_ops.test(); 1617 if (ret == 0) 1618 break; 1619 RTE_LOG(ERR, USER1, "Error %i: test block\n", 1620 ret); 1621 goto error_one_case; 1622 case 1: 1623 break; 1624 default: 1625 RTE_LOG(ERR, USER1, "Error %i: Parse block\n", 1626 ret); 1627 goto error_one_case; 1628 } 1629 1630 continue; 1631 error_one_case: 1632 print_test_block(); 1633 } 1634 1635 fips_test_clear(); 1636 1637 return ret; 1638 1639 } 1640