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)
102 time_t t = time(NULL);
103 struct tm *tm_now = localtime(&t);
105 ret = fips_test_fetch_one_block();
109 for (i = 0; i < info.nb_vec_lines; i++) {
111 if (strstr(info.vec[i], "AESVS")) {
113 info.algo = FIPS_TEST_ALGO_AES;
114 ret = parse_test_aes_init();
117 } else if (strstr(info.vec[i], "GCM")) {
119 info.algo = FIPS_TEST_ALGO_AES_GCM;
120 ret = parse_test_gcm_init();
123 } else if (strstr(info.vec[i], "CMAC")) {
125 info.algo = FIPS_TEST_ALGO_AES_CMAC;
126 ret = parse_test_cmac_init();
129 } else if (strstr(info.vec[i], "CCM")) {
131 info.algo = FIPS_TEST_ALGO_AES_CCM;
132 ret = parse_test_ccm_init();
135 } else if (strstr(info.vec[i], "HMAC")) {
137 info.algo = FIPS_TEST_ALGO_HMAC;
138 ret = parse_test_hmac_init();
141 } else if (strstr(info.vec[i], "TDES")) {
143 info.algo = FIPS_TEST_ALGO_TDES;
144 ret = parse_test_tdes_init();
147 } else if (strstr(info.vec[i], "PERMUTATION")) {
149 info.algo = FIPS_TEST_ALGO_TDES;
150 ret = parse_test_tdes_init();
153 } else if (strstr(info.vec[i], "VARIABLE")) {
155 info.algo = FIPS_TEST_ALGO_TDES;
156 ret = parse_test_tdes_init();
159 } else if (strstr(info.vec[i], "SUBSTITUTION")) {
161 info.algo = FIPS_TEST_ALGO_TDES;
162 ret = parse_test_tdes_init();
165 } else if (strstr(info.vec[i], "SHA-")) {
167 info.algo = FIPS_TEST_ALGO_SHA;
168 ret = parse_test_sha_init();
171 } else if (strstr(info.vec[i], "XTS")) {
173 info.algo = FIPS_TEST_ALGO_AES_XTS;
174 ret = parse_test_xts_init();
180 tmp = strstr(info.vec[i], "# Config info for ");
182 fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ",
187 tmp = strstr(info.vec[i], "# HMAC information for ");
189 fprintf(info.fp_wr, "%s%s\n", "# HMAC information for "
195 tmp = strstr(info.vec[i], "# Config Info for : ");
198 fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ",
203 tmp = strstr(info.vec[i], "# information for ");
206 char tmp_output[128] = {0};
208 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
210 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
211 "information for DPDK Cryptodev ",
216 tmp = strstr(info.vec[i], " test 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 "test information for DPDK Cryptodev ",
228 tmp = strstr(info.vec[i], "\" 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 "\" information for DPDK Cryptodev ",
240 if (i == info.nb_vec_lines - 1) {
241 /** update the time as current time, write to file */
242 fprintf(info.fp_wr, "%s%s\n", "# Generated on ",
247 /* to this point, no field need to update,
248 * only copy to rsp file
250 fprintf(info.fp_wr, "%s\n", info.vec[i]);
257 parse_file_type(const char *path)
259 const char *tmp = path + strlen(path) - 3;
261 if (strstr(tmp, REQ_FILE_PERFIX))
262 info.file_type = FIPS_TYPE_REQ;
263 else if (strstr(tmp, RSP_FILE_PERFIX))
264 info.file_type = FIPS_TYPE_RSP;
265 else if (strstr(path, FAX_FILE_PERFIX))
266 info.file_type = FIPS_TYPE_FAX;
274 fips_test_init(const char *req_file_path, const char *rsp_file_path,
275 const char *device_name)
277 if (strcmp(req_file_path, rsp_file_path) == 0) {
278 RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
284 strcpy(info.file_name, req_file_path);
285 info.algo = FIPS_TEST_ALGO_MAX;
286 if (parse_file_type(req_file_path) < 0) {
287 RTE_LOG(ERR, USER1, "File %s type not supported\n",
292 info.fp_rd = fopen(req_file_path, "r");
294 RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
298 info.fp_wr = fopen(rsp_file_path, "w");
300 RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
304 info.one_line_text = calloc(1, MAX_LINE_CHAR);
305 if (!info.one_line_text) {
306 RTE_LOG(ERR, USER1, "Insufficient memory\n");
310 strlcpy(info.device_name, device_name, sizeof(info.device_name));
312 if (fips_test_parse_header() < 0) {
313 RTE_LOG(ERR, USER1, "Failed parsing header\n");
321 fips_test_clear(void)
327 if (info.one_line_text)
328 free(info.one_line_text);
329 if (info.nb_vec_lines) {
332 for (i = 0; i < info.nb_vec_lines; i++)
336 memset(&info, 0, sizeof(info));
340 fips_test_parse_one_case(void)
344 uint32_t interim_cnt = 0;
347 if (info.interim_callbacks) {
348 for (i = 0; i < info.nb_vec_lines; i++) {
350 for (j = 0; info.interim_callbacks[j].key != NULL; j++)
351 if (strstr(info.vec[i],
352 info.interim_callbacks[j].key)) {
355 ret = info.interim_callbacks[j].cb(
356 info.interim_callbacks[j].key,
358 info.interim_callbacks[j].val);
368 info.vec_start_off = interim_cnt;
371 for (i = 0; i < interim_cnt; i++)
372 fprintf(info.fp_wr, "%s\n", info.vec[i]);
373 fprintf(info.fp_wr, "\n");
375 if (info.nb_vec_lines == interim_cnt)
379 for (i = info.vec_start_off; i < info.nb_vec_lines; i++) {
380 for (j = 0; info.callbacks[j].key != NULL; j++)
381 if (strstr(info.vec[i], info.callbacks[j].key)) {
382 ret = info.callbacks[j].cb(
383 info.callbacks[j].key,
384 info.vec[i], info.callbacks[j].val);
395 fips_test_write_one_case(void)
399 for (i = info.vec_start_off; i < info.nb_vec_lines; i++)
400 fprintf(info.fp_wr, "%s\n", info.vec[i]);
404 parser_read_uint64_hex(uint64_t *value, const char *p)
409 p = skip_white_spaces(p);
411 val = strtoul(p, &next, 16);
415 p = skip_white_spaces(next);
424 parser_read_uint8_hex(uint8_t *value, const char *p)
427 int ret = parser_read_uint64_hex(&val, p);
440 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
442 struct fips_val tmp_val = {0};
443 uint32_t len = val->len;
447 if (val->val != NULL) {
455 ret = parse_uint8_hex_str(key, src, &tmp_val);
459 if (tmp_val.len == val->len) {
460 val->val = tmp_val.val;
464 if (tmp_val.len < val->len) {
465 rte_free(tmp_val.val);
469 val->val = rte_zmalloc(NULL, val->len, 0);
471 rte_free(tmp_val.val);
472 memset(val, 0, sizeof(*val));
476 memcpy(val->val, tmp_val.val, val->len);
477 rte_free(tmp_val.val);
483 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
489 len = strlen(src) / 2;
496 val->val = rte_zmalloc(NULL, len, 0);
500 for (j = 0; j < len; j++) {
501 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
503 if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
505 memset(val, 0, sizeof(*val));
516 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
518 char *data = src + strlen(key);
519 size_t data_len = strlen(data);
522 if (data[data_len - 1] == ']') {
523 char *tmp_data = calloc(1, data_len + 1);
525 if (tmp_data == NULL)
528 strlcpy(tmp_data, data, data_len);
530 ret = parser_read_uint32(&val->len, tmp_data);
534 ret = parser_read_uint32(&val->len, data);
540 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
544 ret = parser_read_uint32_val(key, src, val);
555 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
562 for (len = 0; len < val->len; len++)
563 snprintf(str + len * 2, 255, "%02x", val->val[len]);
569 parser_read_uint64(uint64_t *value, const char *p)
574 p = skip_white_spaces(p);
578 val = strtoul(p, &next, 10);
600 p = skip_white_spaces(p);
609 parser_read_uint32(uint32_t *value, char *p)
612 int ret = parser_read_uint64(&val, p);
617 if (val > UINT32_MAX)
625 parse_write_hex_str(struct fips_val *src)
627 writeback_hex_str("", info.one_line_text, src);
629 fprintf(info.fp_wr, "%s\n", info.one_line_text);
633 update_info_vec(uint32_t count)
635 const struct fips_test_callback *cb;
638 if (!info.writeback_callbacks)
641 cb = &info.writeback_callbacks[0];
643 if (!(strstr(info.vec[0], cb->key))) {
644 fprintf(info.fp_wr, "%s%u\n", cb->key, count);
647 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key,
651 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key, count);
653 for (i = 1; i < info.nb_vec_lines; i++) {
654 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
655 cb = &info.writeback_callbacks[j];
656 if (strstr(info.vec[i], cb->key)) {
657 cb->cb(cb->key, info.vec[i], cb->val);