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], "AES")) {
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;
461 else if (strstr(algo_str, "HMAC"))
462 info.algo = FIPS_TEST_ALGO_HMAC;
463 else if (strstr(algo_str, "CMAC"))
464 info.algo = FIPS_TEST_ALGO_AES_CMAC;
465 else if (strstr(algo_str, "AES-CBC"))
466 info.algo = FIPS_TEST_ALGO_AES;
474 fips_test_parse_one_json_group(void)
479 if (info.interim_callbacks) {
480 char json_value[256];
481 for (i = 0; info.interim_callbacks[i].key != NULL; i++) {
482 param = json_object_get(json_info.json_test_group,
483 info.interim_callbacks[i].key);
484 switch (json_typeof(param)) {
486 snprintf(json_value, 256, "%s", json_string_value(param));
490 snprintf(json_value, 255, "%"JSON_INTEGER_FORMAT,
491 json_integer_value(param));
498 /* First argument is blank because the key
499 * is not included in the string being parsed.
501 ret = info.interim_callbacks[i].cb(
503 info.interim_callbacks[i].val
514 fips_test_parse_one_json_case(void)
520 for (i = 0; info.callbacks[i].key != NULL; i++) {
521 param = json_object_get(json_info.json_test_case, info.callbacks[i].key);
523 strcpy(info.one_line_text, json_string_value(param));
524 /* First argument is blank because the key
525 * is not included in the string being parsed.
527 ret = info.callbacks[i].cb(
528 "", info.one_line_text,
529 info.callbacks[i].val
538 #endif /* RTE_HAS_JANSSON */
541 parser_read_uint64_hex(uint64_t *value, const char *p)
546 p = skip_white_spaces(p);
548 val = strtoul(p, &next, 16);
552 p = skip_white_spaces(next);
561 parser_read_uint8_hex(uint8_t *value, const char *p)
564 int ret = parser_read_uint64_hex(&val, p);
577 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
579 struct fips_val tmp_val = {0};
580 uint32_t len = val->len;
584 if (val->val != NULL) {
592 ret = parse_uint8_hex_str(key, src, &tmp_val);
596 if (tmp_val.len == val->len) {
597 val->val = tmp_val.val;
601 if (tmp_val.len < val->len) {
602 rte_free(tmp_val.val);
606 val->val = rte_zmalloc(NULL, val->len, 0);
608 rte_free(tmp_val.val);
609 memset(val, 0, sizeof(*val));
613 memcpy(val->val, tmp_val.val, val->len);
614 rte_free(tmp_val.val);
620 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
626 len = strlen(src) / 2;
633 val->val = rte_zmalloc(NULL, len, 0);
637 for (j = 0; j < len; j++) {
638 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
640 if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
642 memset(val, 0, sizeof(*val));
653 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
655 char *data = src + strlen(key);
656 size_t data_len = strlen(data);
659 if (data[data_len - 1] == ']') {
660 char *tmp_data = calloc(1, data_len + 1);
662 if (tmp_data == NULL)
665 strlcpy(tmp_data, data, data_len);
667 ret = parser_read_uint32(&val->len, tmp_data);
671 ret = parser_read_uint32(&val->len, data);
677 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
681 ret = parser_read_uint32_val(key, src, val);
692 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
699 for (len = 0; len < val->len; len++)
700 snprintf(str + len * 2, 255, "%02x", val->val[len]);
706 parser_read_uint64(uint64_t *value, const char *p)
711 p = skip_white_spaces(p);
715 val = strtoul(p, &next, 10);
737 p = skip_white_spaces(p);
746 parser_read_uint32(uint32_t *value, char *p)
749 int ret = parser_read_uint64(&val, p);
754 if (val > UINT32_MAX)
762 parser_read_uint16(uint16_t *value, const char *p)
765 int ret = parser_read_uint64(&val, p);
770 if (val > UINT16_MAX)
778 parse_write_hex_str(struct fips_val *src)
780 writeback_hex_str("", info.one_line_text, src);
782 fprintf(info.fp_wr, "%s\n", info.one_line_text);
786 update_info_vec(uint32_t count)
788 const struct fips_test_callback *cb;
791 if (!info.writeback_callbacks)
794 cb = &info.writeback_callbacks[0];
796 if ((info.version == 21.4f) && (!(strstr(info.vec[0], cb->key)))) {
797 fprintf(info.fp_wr, "%s%u\n", cb->key, count);
800 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key,
805 for (; i < info.nb_vec_lines; i++) {
806 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
807 cb = &info.writeback_callbacks[j];
808 if (strstr(info.vec[i], cb->key)) {
809 cb->cb(cb->key, info.vec[i], cb->val);