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], "GCM")) {
115 info.algo = FIPS_TEST_ALGO_AES_GCM;
116 ret = parse_test_gcm_init();
119 } else if (strstr(info.vec[i], "CMAC")) {
120 info.algo = FIPS_TEST_ALGO_AES_CMAC;
121 ret = parse_test_cmac_init();
124 } else if (strstr(info.vec[i], "CCM")) {
125 info.algo = FIPS_TEST_ALGO_AES_CCM;
126 ret = parse_test_ccm_init();
129 } else if (strstr(info.vec[i], "HMAC")) {
130 info.algo = FIPS_TEST_ALGO_HMAC;
131 ret = parse_test_hmac_init();
134 } else if (strstr(info.vec[i], "TDES")) {
135 info.algo = FIPS_TEST_ALGO_TDES;
136 ret = parse_test_tdes_init();
139 } else if (strstr(info.vec[i], "SHA-")) {
140 info.algo = FIPS_TEST_ALGO_SHA;
141 ret = parse_test_sha_init();
146 tmp = strstr(info.vec[i], "# Config info for ");
148 fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ",
153 tmp = strstr(info.vec[i], "# HMAC information for ");
155 fprintf(info.fp_wr, "%s%s\n", "# HMAC information for "
161 tmp = strstr(info.vec[i], "# Config Info for : ");
164 fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ",
169 tmp = strstr(info.vec[i], "# information for ");
172 char tmp_output[128] = {0};
174 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
176 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
177 "information for DPDK Cryptodev ",
182 tmp = strstr(info.vec[i], " test information for ");
184 char tmp_output[128] = {0};
186 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
188 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
189 "test information for DPDK Cryptodev ",
194 tmp = strstr(info.vec[i], "\" information for \"");
196 char tmp_output[128] = {0};
198 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
200 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
201 "\" information for DPDK Cryptodev ",
206 if (i == info.nb_vec_lines - 1) {
207 /** update the time as current time, write to file */
208 fprintf(info.fp_wr, "%s%s\n", "# Generated on ",
213 /* to this point, no field need to update,
214 * only copy to rsp file
216 fprintf(info.fp_wr, "%s\n", info.vec[i]);
223 parse_file_type(const char *path)
225 const char *tmp = path + strlen(path) - 3;
227 if (strstr(tmp, REQ_FILE_PERFIX))
228 info.file_type = FIPS_TYPE_REQ;
229 else if (strstr(tmp, RSP_FILE_PERFIX))
230 info.file_type = FIPS_TYPE_RSP;
231 else if (strstr(path, FAX_FILE_PERFIX))
232 info.file_type = FIPS_TYPE_FAX;
240 fips_test_init(const char *req_file_path, const char *rsp_file_path,
241 const char *device_name)
243 if (strcmp(req_file_path, rsp_file_path) == 0) {
244 RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
250 info.algo = FIPS_TEST_ALGO_MAX;
251 if (parse_file_type(req_file_path) < 0) {
252 RTE_LOG(ERR, USER1, "File %s type not supported\n",
257 info.fp_rd = fopen(req_file_path, "r");
259 RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
263 info.fp_wr = fopen(rsp_file_path, "w");
265 RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
269 info.one_line_text = calloc(1, MAX_LINE_CHAR);
270 if (!info.one_line_text) {
271 RTE_LOG(ERR, USER1, "Insufficient memory\n");
275 strlcpy(info.device_name, device_name, sizeof(info.device_name));
277 if (fips_test_parse_header() < 0) {
278 RTE_LOG(ERR, USER1, "Failed parsing header\n");
286 fips_test_clear(void)
292 if (info.one_line_text)
293 free(info.one_line_text);
294 if (info.nb_vec_lines) {
297 for (i = 0; i < info.nb_vec_lines; i++)
301 memset(&info, 0, sizeof(info));
305 fips_test_parse_one_case(void)
308 uint32_t is_interim = 0;
311 if (info.interim_callbacks) {
312 for (i = 0; i < info.nb_vec_lines; i++) {
313 for (j = 0; info.interim_callbacks[j].key != NULL; j++)
314 if (strstr(info.vec[i],
315 info.interim_callbacks[j].key)) {
318 ret = info.interim_callbacks[j].cb(
319 info.interim_callbacks[j].key,
321 info.interim_callbacks[j].val);
329 for (i = 0; i < info.nb_vec_lines; i++)
330 fprintf(info.fp_wr, "%s\n", info.vec[i]);
331 fprintf(info.fp_wr, "\n");
335 for (i = 0; i < info.nb_vec_lines; i++) {
336 for (j = 0; info.callbacks[j].key != NULL; j++)
337 if (strstr(info.vec[i], info.callbacks[j].key)) {
338 ret = info.callbacks[j].cb(
339 info.callbacks[j].key,
340 info.vec[i], info.callbacks[j].val);
351 fips_test_write_one_case(void)
355 for (i = 0; i < info.nb_vec_lines; i++)
356 fprintf(info.fp_wr, "%s\n", info.vec[i]);
360 parser_read_uint64_hex(uint64_t *value, const char *p)
365 p = skip_white_spaces(p);
367 val = strtoul(p, &next, 16);
371 p = skip_white_spaces(next);
380 parser_read_uint8_hex(uint8_t *value, const char *p)
383 int ret = parser_read_uint64_hex(&val, p);
396 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
398 struct fips_val tmp_val = {0};
399 uint32_t len = val->len;
403 if (val->val != NULL) {
411 ret = parse_uint8_hex_str(key, src, &tmp_val);
415 if (tmp_val.len == val->len) {
416 val->val = tmp_val.val;
420 if (tmp_val.len < val->len) {
421 rte_free(tmp_val.val);
425 val->val = rte_zmalloc(NULL, val->len, 0);
427 rte_free(tmp_val.val);
428 memset(val, 0, sizeof(*val));
432 memcpy(val->val, tmp_val.val, val->len);
433 rte_free(tmp_val.val);
439 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
445 len = strlen(src) / 2;
452 val->val = rte_zmalloc(NULL, len, 0);
456 for (j = 0; j < len; j++) {
457 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
459 if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
461 memset(val, 0, sizeof(*val));
472 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
474 char *data = src + strlen(key);
475 size_t data_len = strlen(data);
478 if (data[data_len - 1] == ']') {
479 char *tmp_data = calloc(1, data_len + 1);
481 if (tmp_data == NULL)
484 strlcpy(tmp_data, data, data_len);
486 ret = parser_read_uint32(&val->len, tmp_data);
490 ret = parser_read_uint32(&val->len, data);
496 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
500 ret = parser_read_uint32_val(key, src, val);
511 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
518 for (len = 0; len < val->len; len++)
519 snprintf(str + len * 2, 255, "%02x", val->val[len]);
525 parser_read_uint64(uint64_t *value, const char *p)
530 p = skip_white_spaces(p);
534 val = strtoul(p, &next, 10);
556 p = skip_white_spaces(p);
565 parser_read_uint32(uint32_t *value, char *p)
568 int ret = parser_read_uint64(&val, p);
573 if (val > UINT32_MAX)
581 parse_write_hex_str(struct fips_val *src)
583 writeback_hex_str("", info.one_line_text, src);
585 fprintf(info.fp_wr, "%s\n", info.one_line_text);
589 update_info_vec(uint32_t count)
591 const struct fips_test_callback *cb;
594 if (!info.writeback_callbacks)
597 cb = &info.writeback_callbacks[0];
599 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key, count);
601 for (i = 1; i < info.nb_vec_lines; i++) {
602 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
603 cb = &info.writeback_callbacks[j];
604 if (strstr(info.vec[i], cb->key)) {
605 cb->cb(cb->key, info.vec[i], cb->val);