1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
8 #include <rte_string_fns.h>
9 #include <rte_cryptodev.h>
10 #include <rte_malloc.h>
12 #include "fips_validation.h"
14 #define skip_white_spaces(pos) \
16 __typeof__(pos) _p = (pos); \
17 for ( ; isspace(*_p); _p++) \
25 FILE *fp = info.fp_rd;
26 char *line = info.one_line_text;
30 memset(line, 0, MAX_LINE_CHAR);
31 while ((ret = fgetc(fp)) != EOF) {
34 if (loc >= MAX_LINE_CHAR - 1)
48 fips_test_fetch_one_block(void)
54 for (i = 0; i < info.nb_vec_lines; i++) {
61 if (i >= MAX_LINE_PER_VECTOR) {
66 ret = get_file_line();
67 size = strlen(info.one_line_text);
71 info.vec[i] = calloc(1, size + 5);
72 if (info.vec[i] == NULL)
75 strlcpy(info.vec[i], info.one_line_text, size + 1);
79 info.nb_vec_lines = i;
84 for (i = 0; i < MAX_LINE_PER_VECTOR; i++)
85 if (info.vec[i] != NULL) {
90 info.nb_vec_lines = 0;
96 fips_test_parse_version(void)
98 int len = strlen(info.vec[0]);
99 char *ptr = info.vec[0];
101 info.version = strtof(ptr + len - 4, NULL);
105 fips_test_parse_header(void)
111 time_t t = time(NULL);
112 struct tm *tm_now = localtime(&t);
114 ret = fips_test_fetch_one_block();
118 if (info.nb_vec_lines)
119 fips_test_parse_version();
121 for (i = 0; i < info.nb_vec_lines; i++) {
123 if (strstr(info.vec[i], "AESVS")) {
125 info.algo = FIPS_TEST_ALGO_AES;
126 ret = parse_test_aes_init();
129 } else if (strstr(info.vec[i], "GCM")) {
131 info.algo = FIPS_TEST_ALGO_AES_GCM;
132 ret = parse_test_gcm_init();
135 } else if (strstr(info.vec[i], "CMAC")) {
137 info.algo = FIPS_TEST_ALGO_AES_CMAC;
138 ret = parse_test_cmac_init();
141 } else if (strstr(info.vec[i], "CCM")) {
143 info.algo = FIPS_TEST_ALGO_AES_CCM;
144 ret = parse_test_ccm_init();
147 } else if (strstr(info.vec[i], "HMAC")) {
149 info.algo = FIPS_TEST_ALGO_HMAC;
150 ret = parse_test_hmac_init();
153 } else if (strstr(info.vec[i], "TDES")) {
155 info.algo = FIPS_TEST_ALGO_TDES;
156 ret = parse_test_tdes_init();
159 } else if (strstr(info.vec[i], "PERMUTATION")) {
161 info.algo = FIPS_TEST_ALGO_TDES;
162 ret = parse_test_tdes_init();
165 } else if (strstr(info.vec[i], "VARIABLE")) {
167 info.algo = FIPS_TEST_ALGO_TDES;
168 ret = parse_test_tdes_init();
171 } else if (strstr(info.vec[i], "SUBSTITUTION")) {
173 info.algo = FIPS_TEST_ALGO_TDES;
174 ret = parse_test_tdes_init();
177 } else if (strstr(info.vec[i], "SHA-")) {
179 info.algo = FIPS_TEST_ALGO_SHA;
180 ret = parse_test_sha_init();
183 } else if (strstr(info.vec[i], "XTS")) {
185 info.algo = FIPS_TEST_ALGO_AES_XTS;
186 ret = parse_test_xts_init();
192 tmp = strstr(info.vec[i], "# Config info for ");
194 fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ",
199 tmp = strstr(info.vec[i], "# HMAC information for ");
201 fprintf(info.fp_wr, "%s%s\n", "# HMAC information for "
207 tmp = strstr(info.vec[i], "# Config Info for : ");
210 fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ",
215 tmp = strstr(info.vec[i], "# information for ");
218 char tmp_output[128] = {0};
220 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
222 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
223 "information for DPDK Cryptodev ",
228 tmp = strstr(info.vec[i], " test information for ");
230 char tmp_output[128] = {0};
232 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
234 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
235 "test information for DPDK Cryptodev ",
240 tmp = strstr(info.vec[i], "\" information for \"");
242 char tmp_output[128] = {0};
244 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
246 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
247 "\" information for DPDK Cryptodev ",
252 if (i == info.nb_vec_lines - 1) {
253 /** update the time as current time, write to file */
254 fprintf(info.fp_wr, "%s%s\n", "# Generated on ",
259 /* to this point, no field need to update,
260 * only copy to rsp file
262 fprintf(info.fp_wr, "%s\n", info.vec[i]);
269 parse_file_type(const char *path)
271 const char *tmp = path + strlen(path) - 3;
273 if (strstr(tmp, REQ_FILE_PREFIX))
274 info.file_type = FIPS_TYPE_REQ;
275 else if (strstr(tmp, RSP_FILE_PREFIX))
276 info.file_type = FIPS_TYPE_RSP;
277 else if (strstr(path, FAX_FILE_PREFIX))
278 info.file_type = FIPS_TYPE_FAX;
279 else if (strstr(path, JSON_FILE_PREFIX))
280 info.file_type = FIPS_TYPE_JSON;
288 fips_test_init(const char *req_file_path, const char *rsp_file_path,
289 const char *device_name)
291 if (strcmp(req_file_path, rsp_file_path) == 0) {
292 RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
298 if (rte_strscpy(info.file_name, req_file_path,
299 sizeof(info.file_name)) < 0) {
300 RTE_LOG(ERR, USER1, "Path %s too long\n", req_file_path);
303 info.algo = FIPS_TEST_ALGO_MAX;
304 if (parse_file_type(req_file_path) < 0) {
305 RTE_LOG(ERR, USER1, "File %s type not supported\n",
310 info.fp_rd = fopen(req_file_path, "r");
312 RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
316 if (info.file_type == FIPS_TYPE_JSON) {
317 #ifdef RTE_HAS_JANSSON
319 json_info.json_root = json_loadf(info.fp_rd, 0, &error);
320 if (!json_info.json_root) {
321 RTE_LOG(ERR, USER1, "Cannot parse json file %s (line %d, column %d)\n",
322 req_file_path, error.line, error.column);
325 #else /* RTE_HAS_JANSSON */
326 RTE_LOG(ERR, USER1, "No json library configured.\n");
328 #endif /* RTE_HAS_JANSSON */
331 info.fp_wr = fopen(rsp_file_path, "w");
333 RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
337 info.one_line_text = calloc(1, MAX_LINE_CHAR);
338 if (!info.one_line_text) {
339 RTE_LOG(ERR, USER1, "Insufficient memory\n");
343 if (rte_strscpy(info.device_name, device_name,
344 sizeof(info.device_name)) < 0) {
345 RTE_LOG(ERR, USER1, "Device name %s too long\n", device_name);
349 if (info.file_type == FIPS_TYPE_JSON)
352 if (fips_test_parse_header() < 0) {
353 RTE_LOG(ERR, USER1, "Failed parsing header\n");
361 fips_test_clear(void)
367 free(info.one_line_text);
368 if (info.nb_vec_lines) {
371 for (i = 0; i < info.nb_vec_lines; i++)
375 memset(&info, 0, sizeof(info));
379 fips_test_parse_one_case(void)
383 uint32_t interim_cnt = 0;
386 info.vec_start_off = 0;
388 if (info.interim_callbacks) {
389 for (i = 0; i < info.nb_vec_lines; i++) {
391 for (j = 0; info.interim_callbacks[j].key != NULL; j++)
392 if (strstr(info.vec[i],
393 info.interim_callbacks[j].key)) {
396 ret = info.interim_callbacks[j].cb(
397 info.interim_callbacks[j].key,
399 info.interim_callbacks[j].val);
410 if (info.version == 21.4f) {
411 for (i = 0; i < interim_cnt; i++)
412 fprintf(info.fp_wr, "%s\n", info.vec[i]);
413 fprintf(info.fp_wr, "\n");
415 if (info.nb_vec_lines == interim_cnt)
418 for (i = 0; i < info.nb_vec_lines; i++)
419 fprintf(info.fp_wr, "%s\n", info.vec[i]);
420 fprintf(info.fp_wr, "\n");
425 info.vec_start_off = interim_cnt;
427 for (i = info.vec_start_off; i < info.nb_vec_lines; i++) {
428 for (j = 0; info.callbacks[j].key != NULL; j++)
429 if (strstr(info.vec[i], info.callbacks[j].key)) {
430 ret = info.callbacks[j].cb(
431 info.callbacks[j].key,
432 info.vec[i], info.callbacks[j].val);
443 fips_test_write_one_case(void)
447 for (i = info.vec_start_off; i < info.nb_vec_lines; i++)
448 fprintf(info.fp_wr, "%s\n", info.vec[i]);
451 #ifdef RTE_HAS_JANSSON
453 fips_test_parse_one_json_vector_set(void)
455 json_t *algo_obj = json_object_get(json_info.json_vector_set, "algorithm");
456 const char *algo_str = json_string_value(algo_obj);
458 /* Vector sets contain the algorithm type, and nothing else we need. */
459 if (strstr(algo_str, "AES-GCM"))
460 info.algo = FIPS_TEST_ALGO_AES_GCM;
468 fips_test_parse_one_json_group(void)
474 if (info.interim_callbacks) {
475 char json_value[256];
476 for (i = 0; info.interim_callbacks[i].key != NULL; i++) {
477 param = json_object_get(json_info.json_test_group,
478 info.interim_callbacks[i].key);
479 val = json_integer_value(param);
480 snprintf(json_value, 255, "%"JSON_INTEGER_FORMAT, val);
481 /* First argument is blank because the key
482 * is not included in the string being parsed.
484 ret = info.interim_callbacks[i].cb(
486 info.interim_callbacks[i].val
497 fips_test_parse_one_json_case(void)
503 for (i = 0; info.callbacks[i].key != NULL; i++) {
504 param = json_object_get(json_info.json_test_case, info.callbacks[i].key);
506 strcpy(info.one_line_text, json_string_value(param));
507 /* First argument is blank because the key
508 * is not included in the string being parsed.
510 ret = info.callbacks[i].cb(
511 "", info.one_line_text,
512 info.callbacks[i].val
521 #endif /* RTE_HAS_JANSSON */
524 parser_read_uint64_hex(uint64_t *value, const char *p)
529 p = skip_white_spaces(p);
531 val = strtoul(p, &next, 16);
535 p = skip_white_spaces(next);
544 parser_read_uint8_hex(uint8_t *value, const char *p)
547 int ret = parser_read_uint64_hex(&val, p);
560 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
562 struct fips_val tmp_val = {0};
563 uint32_t len = val->len;
567 if (val->val != NULL) {
575 ret = parse_uint8_hex_str(key, src, &tmp_val);
579 if (tmp_val.len == val->len) {
580 val->val = tmp_val.val;
584 if (tmp_val.len < val->len) {
585 rte_free(tmp_val.val);
589 val->val = rte_zmalloc(NULL, val->len, 0);
591 rte_free(tmp_val.val);
592 memset(val, 0, sizeof(*val));
596 memcpy(val->val, tmp_val.val, val->len);
597 rte_free(tmp_val.val);
603 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
609 len = strlen(src) / 2;
616 val->val = rte_zmalloc(NULL, len, 0);
620 for (j = 0; j < len; j++) {
621 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
623 if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
625 memset(val, 0, sizeof(*val));
636 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
638 char *data = src + strlen(key);
639 size_t data_len = strlen(data);
642 if (data[data_len - 1] == ']') {
643 char *tmp_data = calloc(1, data_len + 1);
645 if (tmp_data == NULL)
648 strlcpy(tmp_data, data, data_len);
650 ret = parser_read_uint32(&val->len, tmp_data);
654 ret = parser_read_uint32(&val->len, data);
660 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
664 ret = parser_read_uint32_val(key, src, val);
675 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
682 for (len = 0; len < val->len; len++)
683 snprintf(str + len * 2, 255, "%02x", val->val[len]);
689 parser_read_uint64(uint64_t *value, const char *p)
694 p = skip_white_spaces(p);
698 val = strtoul(p, &next, 10);
720 p = skip_white_spaces(p);
729 parser_read_uint32(uint32_t *value, char *p)
732 int ret = parser_read_uint64(&val, p);
737 if (val > UINT32_MAX)
745 parser_read_uint16(uint16_t *value, const char *p)
748 int ret = parser_read_uint64(&val, p);
753 if (val > UINT16_MAX)
761 parse_write_hex_str(struct fips_val *src)
763 writeback_hex_str("", info.one_line_text, src);
765 fprintf(info.fp_wr, "%s\n", info.one_line_text);
769 update_info_vec(uint32_t count)
771 const struct fips_test_callback *cb;
774 if (!info.writeback_callbacks)
777 cb = &info.writeback_callbacks[0];
779 if ((info.version == 21.4f) && (!(strstr(info.vec[0], cb->key)))) {
780 fprintf(info.fp_wr, "%s%u\n", cb->key, count);
783 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key,
788 for (; i < info.nb_vec_lines; i++) {
789 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
790 cb = &info.writeback_callbacks[j];
791 if (strstr(info.vec[i], cb->key)) {
792 cb->cb(cb->key, info.vec[i], cb->val);