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_header(void)
101 time_t t = time(NULL);
102 struct tm *tm_now = localtime(&t);
104 ret = fips_test_fetch_one_block();
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();
114 } else if (strstr(info.vec[i], "HMAC")) {
115 info.algo = FIPS_TEST_ALGO_HMAC;
116 ret = parse_test_hmac_init();
119 } else if (strstr(info.vec[i], "TDES")) {
120 info.algo = FIPS_TEST_ALGO_TDES;
121 ret = parse_test_tdes_init();
126 tmp = strstr(info.vec[i], "# Config info for ");
128 fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ",
133 tmp = strstr(info.vec[i], "# HMAC information for ");
135 fprintf(info.fp_wr, "%s%s\n", "# HMAC information for "
141 tmp = strstr(info.vec[i], "# Config Info for : ");
144 fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ",
149 tmp = strstr(info.vec[i], "# information for ");
152 char tmp_output[128] = {0};
154 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
156 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
157 "information for DPDK Cryptodev ",
162 tmp = strstr(info.vec[i], " test information for ");
164 char tmp_output[128] = {0};
166 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
168 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
169 "test information for DPDK Cryptodev ",
174 if (i == info.nb_vec_lines - 1) {
175 /** update the time as current time, write to file */
176 fprintf(info.fp_wr, "%s%s\n", "# Generated on ",
181 /* to this point, no field need to update,
182 * only copy to rsp file
184 fprintf(info.fp_wr, "%s\n", info.vec[i]);
191 parse_file_type(const char *path)
193 const char *tmp = path + strlen(path) - 3;
195 if (strstr(tmp, REQ_FILE_PERFIX))
196 info.file_type = FIPS_TYPE_REQ;
197 else if (strstr(tmp, RSP_FILE_PERFIX))
198 info.file_type = FIPS_TYPE_RSP;
199 else if (strstr(path, FAX_FILE_PERFIX))
200 info.file_type = FIPS_TYPE_FAX;
208 fips_test_init(const char *req_file_path, const char *rsp_file_path,
209 const char *device_name)
211 if (strcmp(req_file_path, rsp_file_path) == 0) {
212 RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
218 info.algo = FIPS_TEST_ALGO_MAX;
219 if (parse_file_type(req_file_path) < 0) {
220 RTE_LOG(ERR, USER1, "File %s type not supported\n",
225 info.fp_rd = fopen(req_file_path, "r");
227 RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
231 info.fp_wr = fopen(rsp_file_path, "w");
233 RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
237 info.one_line_text = calloc(1, MAX_LINE_CHAR);
238 if (!info.one_line_text) {
239 RTE_LOG(ERR, USER1, "Insufficient memory\n");
243 strlcpy(info.device_name, device_name, sizeof(info.device_name));
245 if (fips_test_parse_header() < 0) {
246 RTE_LOG(ERR, USER1, "Failed parsing header\n");
254 fips_test_clear(void)
260 if (info.one_line_text)
261 free(info.one_line_text);
262 if (info.nb_vec_lines) {
265 for (i = 0; i < info.nb_vec_lines; i++)
269 memset(&info, 0, sizeof(info));
273 fips_test_parse_one_case(void)
276 uint32_t is_interim = 0;
279 if (info.interim_callbacks) {
280 for (i = 0; i < info.nb_vec_lines; i++) {
281 for (j = 0; info.interim_callbacks[j].key != NULL; j++)
282 if (strstr(info.vec[i],
283 info.interim_callbacks[j].key)) {
286 ret = info.interim_callbacks[j].cb(
287 info.interim_callbacks[j].key,
289 info.interim_callbacks[j].val);
297 for (i = 0; i < info.nb_vec_lines; i++)
298 fprintf(info.fp_wr, "%s\n", info.vec[i]);
299 fprintf(info.fp_wr, "\n");
303 for (i = 0; i < info.nb_vec_lines; i++) {
304 for (j = 0; info.callbacks[j].key != NULL; j++)
305 if (strstr(info.vec[i], info.callbacks[j].key)) {
306 ret = info.callbacks[j].cb(
307 info.callbacks[j].key,
308 info.vec[i], info.callbacks[j].val);
319 fips_test_write_one_case(void)
323 for (i = 0; i < info.nb_vec_lines; i++)
324 fprintf(info.fp_wr, "%s\n", info.vec[i]);
328 parser_read_uint64_hex(uint64_t *value, const char *p)
333 p = skip_white_spaces(p);
335 val = strtoul(p, &next, 16);
339 p = skip_white_spaces(next);
348 parser_read_uint8_hex(uint8_t *value, const char *p)
351 int ret = parser_read_uint64_hex(&val, p);
364 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
366 struct fips_val tmp_val = {0};
367 uint32_t len = val->len;
371 if (val->val != NULL) {
379 ret = parse_uint8_hex_str(key, src, &tmp_val);
383 if (tmp_val.len == val->len) {
384 val->val = tmp_val.val;
388 if (tmp_val.len < val->len) {
389 rte_free(tmp_val.val);
393 val->val = rte_zmalloc(NULL, val->len, 0);
395 rte_free(tmp_val.val);
396 memset(val, 0, sizeof(*val));
400 memcpy(val->val, tmp_val.val, val->len);
401 rte_free(tmp_val.val);
407 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
413 len = strlen(src) / 2;
420 val->val = rte_zmalloc(NULL, len, 0);
424 for (j = 0; j < len; j++) {
425 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
427 if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
429 memset(val, 0, sizeof(*val));
440 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
442 char *data = src + strlen(key);
443 size_t data_len = strlen(data);
446 if (data[data_len - 1] == ']') {
447 char *tmp_data = calloc(1, data_len + 1);
449 if (tmp_data == NULL)
452 strlcpy(tmp_data, data, data_len);
454 ret = parser_read_uint32(&val->len, tmp_data);
458 ret = parser_read_uint32(&val->len, data);
464 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
468 ret = parser_read_uint32_val(key, src, val);
479 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
486 for (len = 0; len < val->len; len++)
487 snprintf(str + len * 2, 255, "%02x", val->val[len]);
493 parser_read_uint64(uint64_t *value, const char *p)
498 p = skip_white_spaces(p);
502 val = strtoul(p, &next, 10);
524 p = skip_white_spaces(p);
533 parser_read_uint32(uint32_t *value, char *p)
536 int ret = parser_read_uint64(&val, p);
541 if (val > UINT32_MAX)
549 parse_write_hex_str(struct fips_val *src)
551 writeback_hex_str("", info.one_line_text, src);
553 fprintf(info.fp_wr, "%s\n", info.one_line_text);
557 update_info_vec(uint32_t count)
559 const struct fips_test_callback *cb;
562 if (!info.writeback_callbacks)
565 cb = &info.writeback_callbacks[0];
567 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key, count);
569 for (i = 1; i < info.nb_vec_lines; i++) {
570 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
571 cb = &info.writeback_callbacks[j];
572 if (strstr(info.vec[i], cb->key)) {
573 cb->cb(cb->key, info.vec[i], cb->val);