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], "AESVS")) {
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_PERFIX))
274 info.file_type = FIPS_TYPE_REQ;
275 else if (strstr(tmp, RSP_FILE_PERFIX))
276 info.file_type = FIPS_TYPE_RSP;
277 else if (strstr(path, FAX_FILE_PERFIX))
278 info.file_type = FIPS_TYPE_FAX;
286 fips_test_init(const char *req_file_path, const char *rsp_file_path,
287 const char *device_name)
289 if (strcmp(req_file_path, rsp_file_path) == 0) {
290 RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
296 if (rte_strscpy(info.file_name, req_file_path,
297 sizeof(info.file_name)) < 0) {
298 RTE_LOG(ERR, USER1, "Path %s too long\n", req_file_path);
301 info.algo = FIPS_TEST_ALGO_MAX;
302 if (parse_file_type(req_file_path) < 0) {
303 RTE_LOG(ERR, USER1, "File %s type not supported\n",
308 info.fp_rd = fopen(req_file_path, "r");
310 RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
314 info.fp_wr = fopen(rsp_file_path, "w");
316 RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
320 info.one_line_text = calloc(1, MAX_LINE_CHAR);
321 if (!info.one_line_text) {
322 RTE_LOG(ERR, USER1, "Insufficient memory\n");
326 if (rte_strscpy(info.device_name, device_name,
327 sizeof(info.device_name)) < 0) {
328 RTE_LOG(ERR, USER1, "Device name %s too long\n", device_name);
332 if (fips_test_parse_header() < 0) {
333 RTE_LOG(ERR, USER1, "Failed parsing header\n");
341 fips_test_clear(void)
347 free(info.one_line_text);
348 if (info.nb_vec_lines) {
351 for (i = 0; i < info.nb_vec_lines; i++)
355 memset(&info, 0, sizeof(info));
359 fips_test_parse_one_case(void)
363 uint32_t interim_cnt = 0;
366 info.vec_start_off = 0;
368 if (info.interim_callbacks) {
369 for (i = 0; i < info.nb_vec_lines; i++) {
371 for (j = 0; info.interim_callbacks[j].key != NULL; j++)
372 if (strstr(info.vec[i],
373 info.interim_callbacks[j].key)) {
376 ret = info.interim_callbacks[j].cb(
377 info.interim_callbacks[j].key,
379 info.interim_callbacks[j].val);
390 if (info.version == 21.4f) {
391 for (i = 0; i < interim_cnt; i++)
392 fprintf(info.fp_wr, "%s\n", info.vec[i]);
393 fprintf(info.fp_wr, "\n");
395 if (info.nb_vec_lines == interim_cnt)
398 for (i = 0; i < info.nb_vec_lines; i++)
399 fprintf(info.fp_wr, "%s\n", info.vec[i]);
400 fprintf(info.fp_wr, "\n");
405 info.vec_start_off = interim_cnt;
407 for (i = info.vec_start_off; i < info.nb_vec_lines; i++) {
408 for (j = 0; info.callbacks[j].key != NULL; j++)
409 if (strstr(info.vec[i], info.callbacks[j].key)) {
410 ret = info.callbacks[j].cb(
411 info.callbacks[j].key,
412 info.vec[i], info.callbacks[j].val);
423 fips_test_write_one_case(void)
427 for (i = info.vec_start_off; i < info.nb_vec_lines; i++)
428 fprintf(info.fp_wr, "%s\n", info.vec[i]);
432 parser_read_uint64_hex(uint64_t *value, const char *p)
437 p = skip_white_spaces(p);
439 val = strtoul(p, &next, 16);
443 p = skip_white_spaces(next);
452 parser_read_uint8_hex(uint8_t *value, const char *p)
455 int ret = parser_read_uint64_hex(&val, p);
468 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
470 struct fips_val tmp_val = {0};
471 uint32_t len = val->len;
475 if (val->val != NULL) {
483 ret = parse_uint8_hex_str(key, src, &tmp_val);
487 if (tmp_val.len == val->len) {
488 val->val = tmp_val.val;
492 if (tmp_val.len < val->len) {
493 rte_free(tmp_val.val);
497 val->val = rte_zmalloc(NULL, val->len, 0);
499 rte_free(tmp_val.val);
500 memset(val, 0, sizeof(*val));
504 memcpy(val->val, tmp_val.val, val->len);
505 rte_free(tmp_val.val);
511 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
517 len = strlen(src) / 2;
524 val->val = rte_zmalloc(NULL, len, 0);
528 for (j = 0; j < len; j++) {
529 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
531 if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
533 memset(val, 0, sizeof(*val));
544 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
546 char *data = src + strlen(key);
547 size_t data_len = strlen(data);
550 if (data[data_len - 1] == ']') {
551 char *tmp_data = calloc(1, data_len + 1);
553 if (tmp_data == NULL)
556 strlcpy(tmp_data, data, data_len);
558 ret = parser_read_uint32(&val->len, tmp_data);
562 ret = parser_read_uint32(&val->len, data);
568 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
572 ret = parser_read_uint32_val(key, src, val);
583 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
590 for (len = 0; len < val->len; len++)
591 snprintf(str + len * 2, 255, "%02x", val->val[len]);
597 parser_read_uint64(uint64_t *value, const char *p)
602 p = skip_white_spaces(p);
606 val = strtoul(p, &next, 10);
628 p = skip_white_spaces(p);
637 parser_read_uint32(uint32_t *value, char *p)
640 int ret = parser_read_uint64(&val, p);
645 if (val > UINT32_MAX)
653 parser_read_uint16(uint16_t *value, const char *p)
656 int ret = parser_read_uint64(&val, p);
661 if (val > UINT16_MAX)
669 parse_write_hex_str(struct fips_val *src)
671 writeback_hex_str("", info.one_line_text, src);
673 fprintf(info.fp_wr, "%s\n", info.one_line_text);
677 update_info_vec(uint32_t count)
679 const struct fips_test_callback *cb;
682 if (!info.writeback_callbacks)
685 cb = &info.writeback_callbacks[0];
687 if ((info.version == 21.4f) && (!(strstr(info.vec[0], cb->key)))) {
688 fprintf(info.fp_wr, "%s%u\n", cb->key, count);
691 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key,
696 for (; i < info.nb_vec_lines; i++) {
697 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
698 cb = &info.writeback_callbacks[j];
699 if (strstr(info.vec[i], cb->key)) {
700 cb->cb(cb->key, info.vec[i], cb->val);