1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2018 Intel Corporation 3 */ 4 5 #include <stdio.h> 6 #include <string.h> 7 8 #include <rte_string_fns.h> 9 #include <rte_cryptodev.h> 10 #include <rte_malloc.h> 11 12 #include "fips_validation.h" 13 14 #define skip_white_spaces(pos) \ 15 ({ \ 16 __typeof__(pos) _p = (pos); \ 17 for ( ; isspace(*_p); _p++) \ 18 ; \ 19 _p; \ 20 }) 21 22 static int 23 get_file_line(void) 24 { 25 FILE *fp = info.fp_rd; 26 char *line = info.one_line_text; 27 int ret; 28 uint32_t loc = 0; 29 30 memset(line, 0, MAX_LINE_CHAR); 31 while ((ret = fgetc(fp)) != EOF) { 32 char c = (char)ret; 33 34 if (loc >= MAX_LINE_CHAR - 1) 35 return -ENOMEM; 36 if (c == '\n') 37 break; 38 line[loc++] = c; 39 } 40 41 if (ret == EOF) 42 return -EOF; 43 44 return 0; 45 } 46 47 int 48 fips_test_fetch_one_block(void) 49 { 50 size_t size; 51 int ret = 0; 52 uint32_t i; 53 54 for (i = 0; i < info.nb_vec_lines; i++) { 55 free(info.vec[i]); 56 info.vec[i] = NULL; 57 } 58 59 i = 0; 60 do { 61 if (i >= MAX_LINE_PER_VECTOR) { 62 ret = -ENOMEM; 63 goto error_exit; 64 } 65 66 ret = get_file_line(); 67 size = strlen(info.one_line_text); 68 if (size == 0) 69 break; 70 71 info.vec[i] = calloc(1, size + 5); 72 if (info.vec[i] == NULL) 73 goto error_exit; 74 75 strlcpy(info.vec[i], info.one_line_text, size + 1); 76 i++; 77 } while (ret == 0); 78 79 info.nb_vec_lines = i; 80 81 return ret; 82 83 error_exit: 84 for (i = 0; i < MAX_LINE_PER_VECTOR; i++) 85 if (info.vec[i] != NULL) { 86 free(info.vec[i]); 87 info.vec[i] = NULL; 88 } 89 90 info.nb_vec_lines = 0; 91 92 return -ENOMEM; 93 } 94 95 static void 96 fips_test_parse_version(void) 97 { 98 int len = strlen(info.vec[0]); 99 char *ptr = info.vec[0]; 100 101 info.version = strtof(ptr + len - 4, NULL); 102 } 103 104 static int 105 fips_test_parse_header(void) 106 { 107 uint32_t i; 108 char *tmp; 109 int ret; 110 int algo_parsed = 0; 111 time_t t = time(NULL); 112 struct tm *tm_now = localtime(&t); 113 114 ret = fips_test_fetch_one_block(); 115 if (ret < 0) 116 return ret; 117 118 if (info.nb_vec_lines) 119 fips_test_parse_version(); 120 121 for (i = 0; i < info.nb_vec_lines; i++) { 122 if (!algo_parsed) { 123 if (strstr(info.vec[i], "AESVS")) { 124 algo_parsed = 1; 125 info.algo = FIPS_TEST_ALGO_AES; 126 ret = parse_test_aes_init(); 127 if (ret < 0) 128 return ret; 129 } else if (strstr(info.vec[i], "GCM")) { 130 algo_parsed = 1; 131 info.algo = FIPS_TEST_ALGO_AES_GCM; 132 ret = parse_test_gcm_init(); 133 if (ret < 0) 134 return ret; 135 } else if (strstr(info.vec[i], "CMAC")) { 136 algo_parsed = 1; 137 info.algo = FIPS_TEST_ALGO_AES_CMAC; 138 ret = parse_test_cmac_init(); 139 if (ret < 0) 140 return 0; 141 } else if (strstr(info.vec[i], "CCM")) { 142 algo_parsed = 1; 143 info.algo = FIPS_TEST_ALGO_AES_CCM; 144 ret = parse_test_ccm_init(); 145 if (ret < 0) 146 return 0; 147 } else if (strstr(info.vec[i], "HMAC")) { 148 algo_parsed = 1; 149 info.algo = FIPS_TEST_ALGO_HMAC; 150 ret = parse_test_hmac_init(); 151 if (ret < 0) 152 return ret; 153 } else if (strstr(info.vec[i], "TDES")) { 154 algo_parsed = 1; 155 info.algo = FIPS_TEST_ALGO_TDES; 156 ret = parse_test_tdes_init(); 157 if (ret < 0) 158 return 0; 159 } else if (strstr(info.vec[i], "PERMUTATION")) { 160 algo_parsed = 1; 161 info.algo = FIPS_TEST_ALGO_TDES; 162 ret = parse_test_tdes_init(); 163 if (ret < 0) 164 return 0; 165 } else if (strstr(info.vec[i], "VARIABLE")) { 166 algo_parsed = 1; 167 info.algo = FIPS_TEST_ALGO_TDES; 168 ret = parse_test_tdes_init(); 169 if (ret < 0) 170 return 0; 171 } else if (strstr(info.vec[i], "SUBSTITUTION")) { 172 algo_parsed = 1; 173 info.algo = FIPS_TEST_ALGO_TDES; 174 ret = parse_test_tdes_init(); 175 if (ret < 0) 176 return 0; 177 } else if (strstr(info.vec[i], "SHA-")) { 178 algo_parsed = 1; 179 info.algo = FIPS_TEST_ALGO_SHA; 180 ret = parse_test_sha_init(); 181 if (ret < 0) 182 return ret; 183 } 184 } 185 186 tmp = strstr(info.vec[i], "# Config info for "); 187 if (tmp != NULL) { 188 fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ", 189 info.device_name); 190 continue; 191 } 192 193 tmp = strstr(info.vec[i], "# HMAC information for "); 194 if (tmp != NULL) { 195 fprintf(info.fp_wr, "%s%s\n", "# HMAC information for " 196 "DPDK Cryptodev ", 197 info.device_name); 198 continue; 199 } 200 201 tmp = strstr(info.vec[i], "# Config Info for : "); 202 if (tmp != NULL) { 203 204 fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ", 205 info.device_name); 206 continue; 207 } 208 209 tmp = strstr(info.vec[i], "# information for "); 210 if (tmp != NULL) { 211 212 char tmp_output[128] = {0}; 213 214 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1); 215 216 fprintf(info.fp_wr, "%s%s%s\n", tmp_output, 217 "information for DPDK Cryptodev ", 218 info.device_name); 219 continue; 220 } 221 222 tmp = strstr(info.vec[i], " test information for "); 223 if (tmp != NULL) { 224 char tmp_output[128] = {0}; 225 226 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1); 227 228 fprintf(info.fp_wr, "%s%s%s\n", tmp_output, 229 "test information for DPDK Cryptodev ", 230 info.device_name); 231 continue; 232 } 233 234 tmp = strstr(info.vec[i], "\" information for \""); 235 if (tmp != NULL) { 236 char tmp_output[128] = {0}; 237 238 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1); 239 240 fprintf(info.fp_wr, "%s%s%s\n", tmp_output, 241 "\" information for DPDK Cryptodev ", 242 info.device_name); 243 continue; 244 } 245 246 if (i == info.nb_vec_lines - 1) { 247 /** update the time as current time, write to file */ 248 fprintf(info.fp_wr, "%s%s\n", "# Generated on ", 249 asctime(tm_now)); 250 continue; 251 } 252 253 /* to this point, no field need to update, 254 * only copy to rsp file 255 */ 256 fprintf(info.fp_wr, "%s\n", info.vec[i]); 257 } 258 259 return 0; 260 } 261 262 static int 263 parse_file_type(const char *path) 264 { 265 const char *tmp = path + strlen(path) - 3; 266 267 if (strstr(tmp, REQ_FILE_PERFIX)) 268 info.file_type = FIPS_TYPE_REQ; 269 else if (strstr(tmp, RSP_FILE_PERFIX)) 270 info.file_type = FIPS_TYPE_RSP; 271 else if (strstr(path, FAX_FILE_PERFIX)) 272 info.file_type = FIPS_TYPE_FAX; 273 else 274 return -EINVAL; 275 276 return 0; 277 } 278 279 int 280 fips_test_init(const char *req_file_path, const char *rsp_file_path, 281 const char *device_name) 282 { 283 if (strcmp(req_file_path, rsp_file_path) == 0) { 284 RTE_LOG(ERR, USER1, "File paths cannot be the same\n"); 285 return -EINVAL; 286 } 287 288 fips_test_clear(); 289 290 if (rte_strscpy(info.file_name, req_file_path, 291 sizeof(info.file_name)) < 0) { 292 RTE_LOG(ERR, USER1, "Path %s too long\n", req_file_path); 293 return -EINVAL; 294 } 295 info.algo = FIPS_TEST_ALGO_MAX; 296 if (parse_file_type(req_file_path) < 0) { 297 RTE_LOG(ERR, USER1, "File %s type not supported\n", 298 req_file_path); 299 return -EINVAL; 300 } 301 302 info.fp_rd = fopen(req_file_path, "r"); 303 if (!info.fp_rd) { 304 RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path); 305 return -EINVAL; 306 } 307 308 info.fp_wr = fopen(rsp_file_path, "w"); 309 if (!info.fp_wr) { 310 RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path); 311 return -EINVAL; 312 } 313 314 info.one_line_text = calloc(1, MAX_LINE_CHAR); 315 if (!info.one_line_text) { 316 RTE_LOG(ERR, USER1, "Insufficient memory\n"); 317 return -ENOMEM; 318 } 319 320 if (rte_strscpy(info.device_name, device_name, 321 sizeof(info.device_name)) < 0) { 322 RTE_LOG(ERR, USER1, "Device name %s too long\n", device_name); 323 return -EINVAL; 324 } 325 326 if (fips_test_parse_header() < 0) { 327 RTE_LOG(ERR, USER1, "Failed parsing header\n"); 328 return -1; 329 } 330 331 return 0; 332 } 333 334 void 335 fips_test_clear(void) 336 { 337 if (info.fp_rd) 338 fclose(info.fp_rd); 339 if (info.fp_wr) 340 fclose(info.fp_wr); 341 if (info.one_line_text) 342 free(info.one_line_text); 343 if (info.nb_vec_lines) { 344 uint32_t i; 345 346 for (i = 0; i < info.nb_vec_lines; i++) 347 free(info.vec[i]); 348 } 349 350 memset(&info, 0, sizeof(info)); 351 } 352 353 int 354 fips_test_parse_one_case(void) 355 { 356 uint32_t i, j = 0; 357 uint32_t is_interim; 358 uint32_t interim_cnt = 0; 359 int ret; 360 361 info.vec_start_off = 0; 362 363 if (info.interim_callbacks) { 364 for (i = 0; i < info.nb_vec_lines; i++) { 365 is_interim = 0; 366 for (j = 0; info.interim_callbacks[j].key != NULL; j++) 367 if (strstr(info.vec[i], 368 info.interim_callbacks[j].key)) { 369 is_interim = 1; 370 371 ret = info.interim_callbacks[j].cb( 372 info.interim_callbacks[j].key, 373 info.vec[i], 374 info.interim_callbacks[j].val); 375 if (ret < 0) 376 return ret; 377 } 378 379 if (is_interim) 380 interim_cnt += 1; 381 } 382 } 383 384 if (interim_cnt) { 385 if (info.version == 21.4f) { 386 for (i = 0; i < interim_cnt; i++) 387 fprintf(info.fp_wr, "%s\n", info.vec[i]); 388 fprintf(info.fp_wr, "\n"); 389 390 if (info.nb_vec_lines == interim_cnt) 391 return 1; 392 } else { 393 for (i = 0; i < info.nb_vec_lines; i++) 394 fprintf(info.fp_wr, "%s\n", info.vec[i]); 395 fprintf(info.fp_wr, "\n"); 396 return 1; 397 } 398 } 399 400 info.vec_start_off = interim_cnt; 401 402 for (i = info.vec_start_off; i < info.nb_vec_lines; i++) { 403 for (j = 0; info.callbacks[j].key != NULL; j++) 404 if (strstr(info.vec[i], info.callbacks[j].key)) { 405 ret = info.callbacks[j].cb( 406 info.callbacks[j].key, 407 info.vec[i], info.callbacks[j].val); 408 if (ret < 0) 409 return ret; 410 break; 411 } 412 } 413 414 return 0; 415 } 416 417 void 418 fips_test_write_one_case(void) 419 { 420 uint32_t i; 421 422 for (i = info.vec_start_off; i < info.nb_vec_lines; i++) 423 fprintf(info.fp_wr, "%s\n", info.vec[i]); 424 } 425 426 static int 427 parser_read_uint64_hex(uint64_t *value, const char *p) 428 { 429 char *next; 430 uint64_t val; 431 432 p = skip_white_spaces(p); 433 434 val = strtoul(p, &next, 16); 435 if (p == next) 436 return -EINVAL; 437 438 p = skip_white_spaces(next); 439 if (*p != '\0') 440 return -EINVAL; 441 442 *value = val; 443 return 0; 444 } 445 446 int 447 parser_read_uint8_hex(uint8_t *value, const char *p) 448 { 449 uint64_t val = 0; 450 int ret = parser_read_uint64_hex(&val, p); 451 452 if (ret < 0) 453 return ret; 454 455 if (val > UINT8_MAX) 456 return -ERANGE; 457 458 *value = val; 459 return 0; 460 } 461 462 int 463 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val) 464 { 465 struct fips_val tmp_val = {0}; 466 uint32_t len = val->len; 467 int ret; 468 469 if (len == 0) { 470 if (val->val != NULL) { 471 rte_free(val->val); 472 val->val = NULL; 473 } 474 475 return 0; 476 } 477 478 ret = parse_uint8_hex_str(key, src, &tmp_val); 479 if (ret < 0) 480 return ret; 481 482 if (tmp_val.len == val->len) { 483 val->val = tmp_val.val; 484 return 0; 485 } 486 487 if (tmp_val.len < val->len) { 488 rte_free(tmp_val.val); 489 return -EINVAL; 490 } 491 492 val->val = rte_zmalloc(NULL, val->len, 0); 493 if (!val->val) { 494 rte_free(tmp_val.val); 495 memset(val, 0, sizeof(*val)); 496 return -ENOMEM; 497 } 498 499 memcpy(val->val, tmp_val.val, val->len); 500 rte_free(tmp_val.val); 501 502 return 0; 503 } 504 505 int 506 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val) 507 { 508 uint32_t len, j; 509 510 src += strlen(key); 511 512 len = strlen(src) / 2; 513 514 if (val->val) { 515 rte_free(val->val); 516 val->val = NULL; 517 } 518 519 val->val = rte_zmalloc(NULL, len, 0); 520 if (!val->val) 521 return -ENOMEM; 522 523 for (j = 0; j < len; j++) { 524 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'}; 525 526 if (parser_read_uint8_hex(&val->val[j], byte) < 0) { 527 rte_free(val->val); 528 memset(val, 0, sizeof(*val)); 529 return -EINVAL; 530 } 531 } 532 533 val->len = len; 534 535 return 0; 536 } 537 538 int 539 parser_read_uint32_val(const char *key, char *src, struct fips_val *val) 540 { 541 char *data = src + strlen(key); 542 size_t data_len = strlen(data); 543 int ret; 544 545 if (data[data_len - 1] == ']') { 546 char *tmp_data = calloc(1, data_len + 1); 547 548 if (tmp_data == NULL) 549 return -ENOMEM; 550 551 strlcpy(tmp_data, data, data_len); 552 553 ret = parser_read_uint32(&val->len, tmp_data); 554 555 free(tmp_data); 556 } else 557 ret = parser_read_uint32(&val->len, data); 558 559 return ret; 560 } 561 562 int 563 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val) 564 { 565 int ret; 566 567 ret = parser_read_uint32_val(key, src, val); 568 569 if (ret < 0) 570 return ret; 571 572 val->len /= 8; 573 574 return 0; 575 } 576 577 int 578 writeback_hex_str(const char *key, char *dst, struct fips_val *val) 579 { 580 char *str = dst; 581 uint32_t len; 582 583 str += strlen(key); 584 585 for (len = 0; len < val->len; len++) 586 snprintf(str + len * 2, 255, "%02x", val->val[len]); 587 588 return 0; 589 } 590 591 static int 592 parser_read_uint64(uint64_t *value, const char *p) 593 { 594 char *next; 595 uint64_t val; 596 597 p = skip_white_spaces(p); 598 if (!isdigit(*p)) 599 return -EINVAL; 600 601 val = strtoul(p, &next, 10); 602 if (p == next) 603 return -EINVAL; 604 605 p = next; 606 switch (*p) { 607 case 'T': 608 val *= 1024ULL; 609 /* fall through */ 610 case 'G': 611 val *= 1024ULL; 612 /* fall through */ 613 case 'M': 614 val *= 1024ULL; 615 /* fall through */ 616 case 'k': 617 case 'K': 618 val *= 1024ULL; 619 p++; 620 break; 621 } 622 623 p = skip_white_spaces(p); 624 if (*p != '\0') 625 return -EINVAL; 626 627 *value = val; 628 return 0; 629 } 630 631 int 632 parser_read_uint32(uint32_t *value, char *p) 633 { 634 uint64_t val = 0; 635 int ret = parser_read_uint64(&val, p); 636 637 if (ret < 0) 638 return ret; 639 640 if (val > UINT32_MAX) 641 return -EINVAL; 642 643 *value = val; 644 return 0; 645 } 646 647 void 648 parse_write_hex_str(struct fips_val *src) 649 { 650 writeback_hex_str("", info.one_line_text, src); 651 652 fprintf(info.fp_wr, "%s\n", info.one_line_text); 653 } 654 655 int 656 update_info_vec(uint32_t count) 657 { 658 const struct fips_test_callback *cb; 659 uint32_t i, j; 660 661 if (!info.writeback_callbacks) 662 return -1; 663 664 cb = &info.writeback_callbacks[0]; 665 666 if ((info.version == 21.4f) && (!(strstr(info.vec[0], cb->key)))) { 667 fprintf(info.fp_wr, "%s%u\n", cb->key, count); 668 i = 0; 669 } else { 670 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key, 671 count); 672 i = 1; 673 } 674 675 for (; i < info.nb_vec_lines; i++) { 676 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) { 677 cb = &info.writeback_callbacks[j]; 678 if (strstr(info.vec[i], cb->key)) { 679 cb->cb(cb->key, info.vec[i], cb->val); 680 break; 681 } 682 } 683 } 684 685 return 0; 686 } 687