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], "SHA-")) {
149 info.algo = FIPS_TEST_ALGO_SHA;
150 ret = parse_test_sha_init();
153 } else if (strstr(info.vec[i], "XTS")) {
155 info.algo = FIPS_TEST_ALGO_AES_XTS;
156 ret = parse_test_xts_init();
162 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], "# HMAC information for ");
171 fprintf(info.fp_wr, "%s%s\n", "# HMAC information for "
177 tmp = strstr(info.vec[i], "# Config Info for : ");
180 fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ",
185 tmp = strstr(info.vec[i], "# information for ");
188 char tmp_output[128] = {0};
190 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
192 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
193 "information for DPDK Cryptodev ",
198 tmp = strstr(info.vec[i], " test information for ");
200 char tmp_output[128] = {0};
202 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
204 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
205 "test information for DPDK Cryptodev ",
210 tmp = strstr(info.vec[i], "\" information for \"");
212 char tmp_output[128] = {0};
214 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
216 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
217 "\" information for DPDK Cryptodev ",
222 if (i == info.nb_vec_lines - 1) {
223 /** update the time as current time, write to file */
224 fprintf(info.fp_wr, "%s%s\n", "# Generated on ",
229 /* to this point, no field need to update,
230 * only copy to rsp file
232 fprintf(info.fp_wr, "%s\n", info.vec[i]);
239 parse_file_type(const char *path)
241 const char *tmp = path + strlen(path) - 3;
243 if (strstr(tmp, REQ_FILE_PERFIX))
244 info.file_type = FIPS_TYPE_REQ;
245 else if (strstr(tmp, RSP_FILE_PERFIX))
246 info.file_type = FIPS_TYPE_RSP;
247 else if (strstr(path, FAX_FILE_PERFIX))
248 info.file_type = FIPS_TYPE_FAX;
256 fips_test_init(const char *req_file_path, const char *rsp_file_path,
257 const char *device_name)
259 if (strcmp(req_file_path, rsp_file_path) == 0) {
260 RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
266 strcpy(info.file_name, req_file_path);
267 info.algo = FIPS_TEST_ALGO_MAX;
268 if (parse_file_type(req_file_path) < 0) {
269 RTE_LOG(ERR, USER1, "File %s type not supported\n",
274 info.fp_rd = fopen(req_file_path, "r");
276 RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
280 info.fp_wr = fopen(rsp_file_path, "w");
282 RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
286 info.one_line_text = calloc(1, MAX_LINE_CHAR);
287 if (!info.one_line_text) {
288 RTE_LOG(ERR, USER1, "Insufficient memory\n");
292 strlcpy(info.device_name, device_name, sizeof(info.device_name));
294 if (fips_test_parse_header() < 0) {
295 RTE_LOG(ERR, USER1, "Failed parsing header\n");
303 fips_test_clear(void)
309 if (info.one_line_text)
310 free(info.one_line_text);
311 if (info.nb_vec_lines) {
314 for (i = 0; i < info.nb_vec_lines; i++)
318 memset(&info, 0, sizeof(info));
322 fips_test_parse_one_case(void)
325 uint32_t is_interim = 0;
328 if (info.interim_callbacks) {
329 for (i = 0; i < info.nb_vec_lines; i++) {
330 for (j = 0; info.interim_callbacks[j].key != NULL; j++)
331 if (strstr(info.vec[i],
332 info.interim_callbacks[j].key)) {
335 ret = info.interim_callbacks[j].cb(
336 info.interim_callbacks[j].key,
338 info.interim_callbacks[j].val);
346 for (i = 0; i < info.nb_vec_lines; i++)
347 fprintf(info.fp_wr, "%s\n", info.vec[i]);
348 fprintf(info.fp_wr, "\n");
352 for (i = 0; i < info.nb_vec_lines; i++) {
353 for (j = 0; info.callbacks[j].key != NULL; j++)
354 if (strstr(info.vec[i], info.callbacks[j].key)) {
355 ret = info.callbacks[j].cb(
356 info.callbacks[j].key,
357 info.vec[i], info.callbacks[j].val);
368 fips_test_write_one_case(void)
372 for (i = 0; i < info.nb_vec_lines; i++)
373 fprintf(info.fp_wr, "%s\n", info.vec[i]);
377 parser_read_uint64_hex(uint64_t *value, const char *p)
382 p = skip_white_spaces(p);
384 val = strtoul(p, &next, 16);
388 p = skip_white_spaces(next);
397 parser_read_uint8_hex(uint8_t *value, const char *p)
400 int ret = parser_read_uint64_hex(&val, p);
413 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
415 struct fips_val tmp_val = {0};
416 uint32_t len = val->len;
420 if (val->val != NULL) {
428 ret = parse_uint8_hex_str(key, src, &tmp_val);
432 if (tmp_val.len == val->len) {
433 val->val = tmp_val.val;
437 if (tmp_val.len < val->len) {
438 rte_free(tmp_val.val);
442 val->val = rte_zmalloc(NULL, val->len, 0);
444 rte_free(tmp_val.val);
445 memset(val, 0, sizeof(*val));
449 memcpy(val->val, tmp_val.val, val->len);
450 rte_free(tmp_val.val);
456 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
462 len = strlen(src) / 2;
469 val->val = rte_zmalloc(NULL, len, 0);
473 for (j = 0; j < len; j++) {
474 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
476 if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
478 memset(val, 0, sizeof(*val));
489 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
491 char *data = src + strlen(key);
492 size_t data_len = strlen(data);
495 if (data[data_len - 1] == ']') {
496 char *tmp_data = calloc(1, data_len + 1);
498 if (tmp_data == NULL)
501 strlcpy(tmp_data, data, data_len);
503 ret = parser_read_uint32(&val->len, tmp_data);
507 ret = parser_read_uint32(&val->len, data);
513 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
517 ret = parser_read_uint32_val(key, src, val);
528 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
535 for (len = 0; len < val->len; len++)
536 snprintf(str + len * 2, 255, "%02x", val->val[len]);
542 parser_read_uint64(uint64_t *value, const char *p)
547 p = skip_white_spaces(p);
551 val = strtoul(p, &next, 10);
573 p = skip_white_spaces(p);
582 parser_read_uint32(uint32_t *value, char *p)
585 int ret = parser_read_uint64(&val, p);
590 if (val > UINT32_MAX)
598 parse_write_hex_str(struct fips_val *src)
600 writeback_hex_str("", info.one_line_text, src);
602 fprintf(info.fp_wr, "%s\n", info.one_line_text);
606 update_info_vec(uint32_t count)
608 const struct fips_test_callback *cb;
611 if (!info.writeback_callbacks)
614 cb = &info.writeback_callbacks[0];
616 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key, count);
618 for (i = 1; i < info.nb_vec_lines; i++) {
619 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
620 cb = &info.writeback_callbacks[j];
621 if (strstr(info.vec[i], cb->key)) {
622 cb->cb(cb->key, info.vec[i], cb->val);