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();
116 tmp = strstr(info.vec[i], "# Config info for ");
118 fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ",
123 tmp = strstr(info.vec[i], "# HMAC information for ");
125 fprintf(info.fp_wr, "%s%s\n", "# HMAC information for "
131 tmp = strstr(info.vec[i], "# Config Info for : ");
134 fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ",
139 tmp = strstr(info.vec[i], "# information for ");
142 char tmp_output[128] = {0};
144 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
146 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
147 "information for DPDK Cryptodev ",
152 tmp = strstr(info.vec[i], " test information for ");
154 char tmp_output[128] = {0};
156 strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
158 fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
159 "test information for DPDK Cryptodev ",
164 if (i == info.nb_vec_lines - 1) {
165 /** update the time as current time, write to file */
166 fprintf(info.fp_wr, "%s%s\n", "# Generated on ",
171 /* to this point, no field need to update,
172 * only copy to rsp file
174 fprintf(info.fp_wr, "%s\n", info.vec[i]);
181 parse_file_type(const char *path)
183 const char *tmp = path + strlen(path) - 3;
185 if (strstr(tmp, REQ_FILE_PERFIX))
186 info.file_type = FIPS_TYPE_REQ;
187 else if (strstr(tmp, RSP_FILE_PERFIX))
188 info.file_type = FIPS_TYPE_RSP;
189 else if (strstr(path, FAX_FILE_PERFIX))
190 info.file_type = FIPS_TYPE_FAX;
198 fips_test_init(const char *req_file_path, const char *rsp_file_path,
199 const char *device_name)
201 if (strcmp(req_file_path, rsp_file_path) == 0) {
202 RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
208 info.algo = FIPS_TEST_ALGO_MAX;
209 if (parse_file_type(req_file_path) < 0) {
210 RTE_LOG(ERR, USER1, "File %s type not supported\n",
215 info.fp_rd = fopen(req_file_path, "r");
217 RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
221 info.fp_wr = fopen(rsp_file_path, "w");
223 RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
227 info.one_line_text = calloc(1, MAX_LINE_CHAR);
228 if (!info.one_line_text) {
229 RTE_LOG(ERR, USER1, "Insufficient memory\n");
233 strlcpy(info.device_name, device_name, sizeof(info.device_name));
235 if (fips_test_parse_header() < 0) {
236 RTE_LOG(ERR, USER1, "Failed parsing header\n");
244 fips_test_clear(void)
250 if (info.one_line_text)
251 free(info.one_line_text);
252 if (info.nb_vec_lines) {
255 for (i = 0; i < info.nb_vec_lines; i++)
259 memset(&info, 0, sizeof(info));
263 fips_test_parse_one_case(void)
266 uint32_t is_interim = 0;
269 if (info.interim_callbacks) {
270 for (i = 0; i < info.nb_vec_lines; i++) {
271 for (j = 0; info.interim_callbacks[j].key != NULL; j++)
272 if (strstr(info.vec[i],
273 info.interim_callbacks[j].key)) {
276 ret = info.interim_callbacks[j].cb(
277 info.interim_callbacks[j].key,
279 info.interim_callbacks[j].val);
287 for (i = 0; i < info.nb_vec_lines; i++)
288 fprintf(info.fp_wr, "%s\n", info.vec[i]);
289 fprintf(info.fp_wr, "\n");
293 for (i = 0; i < info.nb_vec_lines; i++) {
294 for (j = 0; info.callbacks[j].key != NULL; j++)
295 if (strstr(info.vec[i], info.callbacks[j].key)) {
296 ret = info.callbacks[j].cb(
297 info.callbacks[j].key,
298 info.vec[i], info.callbacks[j].val);
309 fips_test_write_one_case(void)
313 for (i = 0; i < info.nb_vec_lines; i++)
314 fprintf(info.fp_wr, "%s\n", info.vec[i]);
318 parser_read_uint64_hex(uint64_t *value, const char *p)
323 p = skip_white_spaces(p);
325 val = strtoul(p, &next, 16);
329 p = skip_white_spaces(next);
338 parser_read_uint8_hex(uint8_t *value, const char *p)
341 int ret = parser_read_uint64_hex(&val, p);
354 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
356 struct fips_val tmp_val = {0};
357 uint32_t len = val->len;
361 if (val->val != NULL) {
369 ret = parse_uint8_hex_str(key, src, &tmp_val);
373 if (tmp_val.len == val->len) {
374 val->val = tmp_val.val;
378 if (tmp_val.len < val->len) {
379 rte_free(tmp_val.val);
383 val->val = rte_zmalloc(NULL, val->len, 0);
385 rte_free(tmp_val.val);
386 memset(val, 0, sizeof(*val));
390 memcpy(val->val, tmp_val.val, val->len);
391 rte_free(tmp_val.val);
397 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
403 len = strlen(src) / 2;
410 val->val = rte_zmalloc(NULL, len, 0);
414 for (j = 0; j < len; j++) {
415 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
417 if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
419 memset(val, 0, sizeof(*val));
430 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
432 char *data = src + strlen(key);
433 size_t data_len = strlen(data);
436 if (data[data_len - 1] == ']') {
437 char *tmp_data = calloc(1, data_len + 1);
439 if (tmp_data == NULL)
442 strlcpy(tmp_data, data, data_len);
444 ret = parser_read_uint32(&val->len, tmp_data);
448 ret = parser_read_uint32(&val->len, data);
454 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
458 ret = parser_read_uint32_val(key, src, val);
469 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
476 for (len = 0; len < val->len; len++)
477 snprintf(str + len * 2, 255, "%02x", val->val[len]);
483 parser_read_uint64(uint64_t *value, const char *p)
488 p = skip_white_spaces(p);
492 val = strtoul(p, &next, 10);
514 p = skip_white_spaces(p);
523 parser_read_uint32(uint32_t *value, char *p)
526 int ret = parser_read_uint64(&val, p);
531 if (val > UINT32_MAX)
539 parse_write_hex_str(struct fips_val *src)
541 writeback_hex_str("", info.one_line_text, src);
543 fprintf(info.fp_wr, "%s\n", info.one_line_text);
547 update_info_vec(uint32_t count)
549 const struct fips_test_callback *cb;
552 if (!info.writeback_callbacks)
555 cb = &info.writeback_callbacks[0];
557 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key, count);
559 for (i = 1; i < info.nb_vec_lines; i++) {
560 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
561 cb = &info.writeback_callbacks[j];
562 if (strstr(info.vec[i], cb->key)) {
563 cb->cb(cb->key, info.vec[i], cb->val);