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