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 int 96 fips_test_parse_header(void) 97 { 98 uint32_t i; 99 char *tmp; 100 int ret; 101 int algo_parsed = 0; 102 time_t t = time(NULL); 103 struct tm *tm_now = localtime(&t); 104 105 ret = fips_test_fetch_one_block(); 106 if (ret < 0) 107 return ret; 108 109 for (i = 0; i < info.nb_vec_lines; i++) { 110 if (!algo_parsed) { 111 if (strstr(info.vec[i], "AESVS")) { 112 algo_parsed = 1; 113 info.algo = FIPS_TEST_ALGO_AES; 114 ret = parse_test_aes_init(); 115 if (ret < 0) 116 return ret; 117 } else if (strstr(info.vec[i], "GCM")) { 118 algo_parsed = 1; 119 info.algo = FIPS_TEST_ALGO_AES_GCM; 120 ret = parse_test_gcm_init(); 121 if (ret < 0) 122 return ret; 123 } else if (strstr(info.vec[i], "CMAC")) { 124 algo_parsed = 1; 125 info.algo = FIPS_TEST_ALGO_AES_CMAC; 126 ret = parse_test_cmac_init(); 127 if (ret < 0) 128 return 0; 129 } else if (strstr(info.vec[i], "CCM")) { 130 algo_parsed = 1; 131 info.algo = FIPS_TEST_ALGO_AES_CCM; 132 ret = parse_test_ccm_init(); 133 if (ret < 0) 134 return 0; 135 } else if (strstr(info.vec[i], "HMAC")) { 136 algo_parsed = 1; 137 info.algo = FIPS_TEST_ALGO_HMAC; 138 ret = parse_test_hmac_init(); 139 if (ret < 0) 140 return ret; 141 } else if (strstr(info.vec[i], "TDES")) { 142 algo_parsed = 1; 143 info.algo = FIPS_TEST_ALGO_TDES; 144 ret = parse_test_tdes_init(); 145 if (ret < 0) 146 return 0; 147 } else if (strstr(info.vec[i], "SHA-")) { 148 algo_parsed = 1; 149 info.algo = FIPS_TEST_ALGO_SHA; 150 ret = parse_test_sha_init(); 151 if (ret < 0) 152 return ret; 153 } 154 } 155 156 tmp = strstr(info.vec[i], "# Config info for "); 157 if (tmp != NULL) { 158 fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ", 159 info.device_name); 160 continue; 161 } 162 163 tmp = strstr(info.vec[i], "# HMAC information for "); 164 if (tmp != NULL) { 165 fprintf(info.fp_wr, "%s%s\n", "# HMAC information for " 166 "DPDK Cryptodev ", 167 info.device_name); 168 continue; 169 } 170 171 tmp = strstr(info.vec[i], "# Config Info for : "); 172 if (tmp != NULL) { 173 174 fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ", 175 info.device_name); 176 continue; 177 } 178 179 tmp = strstr(info.vec[i], "# information for "); 180 if (tmp != NULL) { 181 182 char tmp_output[128] = {0}; 183 184 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1); 185 186 fprintf(info.fp_wr, "%s%s%s\n", tmp_output, 187 "information for DPDK Cryptodev ", 188 info.device_name); 189 continue; 190 } 191 192 tmp = strstr(info.vec[i], " test information for "); 193 if (tmp != NULL) { 194 char tmp_output[128] = {0}; 195 196 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1); 197 198 fprintf(info.fp_wr, "%s%s%s\n", tmp_output, 199 "test information for DPDK Cryptodev ", 200 info.device_name); 201 continue; 202 } 203 204 tmp = strstr(info.vec[i], "\" information for \""); 205 if (tmp != NULL) { 206 char tmp_output[128] = {0}; 207 208 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1); 209 210 fprintf(info.fp_wr, "%s%s%s\n", tmp_output, 211 "\" information for DPDK Cryptodev ", 212 info.device_name); 213 continue; 214 } 215 216 if (i == info.nb_vec_lines - 1) { 217 /** update the time as current time, write to file */ 218 fprintf(info.fp_wr, "%s%s\n", "# Generated on ", 219 asctime(tm_now)); 220 continue; 221 } 222 223 /* to this point, no field need to update, 224 * only copy to rsp file 225 */ 226 fprintf(info.fp_wr, "%s\n", info.vec[i]); 227 } 228 229 return 0; 230 } 231 232 static int 233 parse_file_type(const char *path) 234 { 235 const char *tmp = path + strlen(path) - 3; 236 237 if (strstr(tmp, REQ_FILE_PERFIX)) 238 info.file_type = FIPS_TYPE_REQ; 239 else if (strstr(tmp, RSP_FILE_PERFIX)) 240 info.file_type = FIPS_TYPE_RSP; 241 else if (strstr(path, FAX_FILE_PERFIX)) 242 info.file_type = FIPS_TYPE_FAX; 243 else 244 return -EINVAL; 245 246 return 0; 247 } 248 249 int 250 fips_test_init(const char *req_file_path, const char *rsp_file_path, 251 const char *device_name) 252 { 253 if (strcmp(req_file_path, rsp_file_path) == 0) { 254 RTE_LOG(ERR, USER1, "File paths cannot be the same\n"); 255 return -EINVAL; 256 } 257 258 fips_test_clear(); 259 260 strcpy(info.file_name, req_file_path); 261 info.algo = FIPS_TEST_ALGO_MAX; 262 if (parse_file_type(req_file_path) < 0) { 263 RTE_LOG(ERR, USER1, "File %s type not supported\n", 264 req_file_path); 265 return -EINVAL; 266 } 267 268 info.fp_rd = fopen(req_file_path, "r"); 269 if (!info.fp_rd) { 270 RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path); 271 return -EINVAL; 272 } 273 274 info.fp_wr = fopen(rsp_file_path, "w"); 275 if (!info.fp_wr) { 276 RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path); 277 return -EINVAL; 278 } 279 280 info.one_line_text = calloc(1, MAX_LINE_CHAR); 281 if (!info.one_line_text) { 282 RTE_LOG(ERR, USER1, "Insufficient memory\n"); 283 return -ENOMEM; 284 } 285 286 strlcpy(info.device_name, device_name, sizeof(info.device_name)); 287 288 if (fips_test_parse_header() < 0) { 289 RTE_LOG(ERR, USER1, "Failed parsing header\n"); 290 return -1; 291 } 292 293 return 0; 294 } 295 296 void 297 fips_test_clear(void) 298 { 299 if (info.fp_rd) 300 fclose(info.fp_rd); 301 if (info.fp_wr) 302 fclose(info.fp_wr); 303 if (info.one_line_text) 304 free(info.one_line_text); 305 if (info.nb_vec_lines) { 306 uint32_t i; 307 308 for (i = 0; i < info.nb_vec_lines; i++) 309 free(info.vec[i]); 310 } 311 312 memset(&info, 0, sizeof(info)); 313 } 314 315 int 316 fips_test_parse_one_case(void) 317 { 318 uint32_t i, j = 0; 319 uint32_t is_interim = 0; 320 int ret; 321 322 if (info.interim_callbacks) { 323 for (i = 0; i < info.nb_vec_lines; i++) { 324 for (j = 0; info.interim_callbacks[j].key != NULL; j++) 325 if (strstr(info.vec[i], 326 info.interim_callbacks[j].key)) { 327 is_interim = 1; 328 329 ret = info.interim_callbacks[j].cb( 330 info.interim_callbacks[j].key, 331 info.vec[i], 332 info.interim_callbacks[j].val); 333 if (ret < 0) 334 return ret; 335 } 336 } 337 } 338 339 if (is_interim) { 340 for (i = 0; i < info.nb_vec_lines; i++) 341 fprintf(info.fp_wr, "%s\n", info.vec[i]); 342 fprintf(info.fp_wr, "\n"); 343 return 1; 344 } 345 346 for (i = 0; i < info.nb_vec_lines; i++) { 347 for (j = 0; info.callbacks[j].key != NULL; j++) 348 if (strstr(info.vec[i], info.callbacks[j].key)) { 349 ret = info.callbacks[j].cb( 350 info.callbacks[j].key, 351 info.vec[i], info.callbacks[j].val); 352 if (ret < 0) 353 return ret; 354 break; 355 } 356 } 357 358 return 0; 359 } 360 361 void 362 fips_test_write_one_case(void) 363 { 364 uint32_t i; 365 366 for (i = 0; i < info.nb_vec_lines; i++) 367 fprintf(info.fp_wr, "%s\n", info.vec[i]); 368 } 369 370 static int 371 parser_read_uint64_hex(uint64_t *value, const char *p) 372 { 373 char *next; 374 uint64_t val; 375 376 p = skip_white_spaces(p); 377 378 val = strtoul(p, &next, 16); 379 if (p == next) 380 return -EINVAL; 381 382 p = skip_white_spaces(next); 383 if (*p != '\0') 384 return -EINVAL; 385 386 *value = val; 387 return 0; 388 } 389 390 int 391 parser_read_uint8_hex(uint8_t *value, const char *p) 392 { 393 uint64_t val = 0; 394 int ret = parser_read_uint64_hex(&val, p); 395 396 if (ret < 0) 397 return ret; 398 399 if (val > UINT8_MAX) 400 return -ERANGE; 401 402 *value = val; 403 return 0; 404 } 405 406 int 407 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val) 408 { 409 struct fips_val tmp_val = {0}; 410 uint32_t len = val->len; 411 int ret; 412 413 if (len == 0) { 414 if (val->val != NULL) { 415 rte_free(val->val); 416 val->val = NULL; 417 } 418 419 return 0; 420 } 421 422 ret = parse_uint8_hex_str(key, src, &tmp_val); 423 if (ret < 0) 424 return ret; 425 426 if (tmp_val.len == val->len) { 427 val->val = tmp_val.val; 428 return 0; 429 } 430 431 if (tmp_val.len < val->len) { 432 rte_free(tmp_val.val); 433 return -EINVAL; 434 } 435 436 val->val = rte_zmalloc(NULL, val->len, 0); 437 if (!val->val) { 438 rte_free(tmp_val.val); 439 memset(val, 0, sizeof(*val)); 440 return -ENOMEM; 441 } 442 443 memcpy(val->val, tmp_val.val, val->len); 444 rte_free(tmp_val.val); 445 446 return 0; 447 } 448 449 int 450 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val) 451 { 452 uint32_t len, j; 453 454 src += strlen(key); 455 456 len = strlen(src) / 2; 457 458 if (val->val) { 459 rte_free(val->val); 460 val->val = NULL; 461 } 462 463 val->val = rte_zmalloc(NULL, len, 0); 464 if (!val->val) 465 return -ENOMEM; 466 467 for (j = 0; j < len; j++) { 468 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'}; 469 470 if (parser_read_uint8_hex(&val->val[j], byte) < 0) { 471 rte_free(val->val); 472 memset(val, 0, sizeof(*val)); 473 return -EINVAL; 474 } 475 } 476 477 val->len = len; 478 479 return 0; 480 } 481 482 int 483 parser_read_uint32_val(const char *key, char *src, struct fips_val *val) 484 { 485 char *data = src + strlen(key); 486 size_t data_len = strlen(data); 487 int ret; 488 489 if (data[data_len - 1] == ']') { 490 char *tmp_data = calloc(1, data_len + 1); 491 492 if (tmp_data == NULL) 493 return -ENOMEM; 494 495 strlcpy(tmp_data, data, data_len); 496 497 ret = parser_read_uint32(&val->len, tmp_data); 498 499 free(tmp_data); 500 } else 501 ret = parser_read_uint32(&val->len, data); 502 503 return ret; 504 } 505 506 int 507 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val) 508 { 509 int ret; 510 511 ret = parser_read_uint32_val(key, src, val); 512 513 if (ret < 0) 514 return ret; 515 516 val->len /= 8; 517 518 return 0; 519 } 520 521 int 522 writeback_hex_str(const char *key, char *dst, struct fips_val *val) 523 { 524 char *str = dst; 525 uint32_t len; 526 527 str += strlen(key); 528 529 for (len = 0; len < val->len; len++) 530 snprintf(str + len * 2, 255, "%02x", val->val[len]); 531 532 return 0; 533 } 534 535 static int 536 parser_read_uint64(uint64_t *value, const char *p) 537 { 538 char *next; 539 uint64_t val; 540 541 p = skip_white_spaces(p); 542 if (!isdigit(*p)) 543 return -EINVAL; 544 545 val = strtoul(p, &next, 10); 546 if (p == next) 547 return -EINVAL; 548 549 p = next; 550 switch (*p) { 551 case 'T': 552 val *= 1024ULL; 553 /* fall through */ 554 case 'G': 555 val *= 1024ULL; 556 /* fall through */ 557 case 'M': 558 val *= 1024ULL; 559 /* fall through */ 560 case 'k': 561 case 'K': 562 val *= 1024ULL; 563 p++; 564 break; 565 } 566 567 p = skip_white_spaces(p); 568 if (*p != '\0') 569 return -EINVAL; 570 571 *value = val; 572 return 0; 573 } 574 575 int 576 parser_read_uint32(uint32_t *value, char *p) 577 { 578 uint64_t val = 0; 579 int ret = parser_read_uint64(&val, p); 580 581 if (ret < 0) 582 return ret; 583 584 if (val > UINT32_MAX) 585 return -EINVAL; 586 587 *value = val; 588 return 0; 589 } 590 591 void 592 parse_write_hex_str(struct fips_val *src) 593 { 594 writeback_hex_str("", info.one_line_text, src); 595 596 fprintf(info.fp_wr, "%s\n", info.one_line_text); 597 } 598 599 int 600 update_info_vec(uint32_t count) 601 { 602 const struct fips_test_callback *cb; 603 uint32_t i, j; 604 605 if (!info.writeback_callbacks) 606 return -1; 607 608 cb = &info.writeback_callbacks[0]; 609 610 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key, count); 611 612 for (i = 1; i < info.nb_vec_lines; i++) { 613 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) { 614 cb = &info.writeback_callbacks[j]; 615 if (strstr(info.vec[i], cb->key)) { 616 cb->cb(cb->key, info.vec[i], cb->val); 617 break; 618 } 619 } 620 } 621 622 return 0; 623 } 624