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