xref: /dpdk/examples/fips_validation/fips_validation.c (revision cd255ccf57647830bfdda8d760dad833f019b76a)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <string.h>
7 
8 #include <rte_string_fns.h>
9 #include <rte_cryptodev.h>
10 #include <rte_malloc.h>
11 
12 #include "fips_validation.h"
13 
14 #define skip_white_spaces(pos)			\
15 ({						\
16 	__typeof__(pos) _p = (pos);		\
17 	for ( ; isspace(*_p); _p++)		\
18 		;				\
19 	_p;					\
20 })
21 
22 static int
23 get_file_line(void)
24 {
25 	FILE *fp = info.fp_rd;
26 	char *line = info.one_line_text;
27 	int ret;
28 	uint32_t loc = 0;
29 
30 	memset(line, 0, MAX_LINE_CHAR);
31 	while ((ret = fgetc(fp)) != EOF) {
32 		char c = (char)ret;
33 
34 		if (loc >= MAX_LINE_CHAR - 1)
35 			return -ENOMEM;
36 		if (c == '\n')
37 			break;
38 		line[loc++] = c;
39 	}
40 
41 	if (ret == EOF)
42 		return -EOF;
43 
44 	return 0;
45 }
46 
47 int
48 fips_test_fetch_one_block(void)
49 {
50 	size_t size;
51 	int ret = 0;
52 	uint32_t i;
53 
54 	for (i = 0; i < info.nb_vec_lines; i++) {
55 		free(info.vec[i]);
56 		info.vec[i] = NULL;
57 	}
58 
59 	i = 0;
60 	do {
61 		if (i >= MAX_LINE_PER_VECTOR) {
62 			ret = -ENOMEM;
63 			goto error_exit;
64 		}
65 
66 		ret = get_file_line();
67 		size = strlen(info.one_line_text);
68 		if (size == 0)
69 			break;
70 
71 		info.vec[i] = calloc(1, size + 5);
72 		if (info.vec[i] == NULL)
73 			goto error_exit;
74 
75 		strlcpy(info.vec[i], info.one_line_text, size + 1);
76 		i++;
77 	} while (ret == 0);
78 
79 	info.nb_vec_lines = i;
80 
81 	return ret;
82 
83 error_exit:
84 	for (i = 0; i < MAX_LINE_PER_VECTOR; i++)
85 		if (info.vec[i] != NULL) {
86 			free(info.vec[i]);
87 			info.vec[i] = NULL;
88 		}
89 
90 	info.nb_vec_lines = 0;
91 
92 	return -ENOMEM;
93 }
94 
95 static int
96 fips_test_parse_header(void)
97 {
98 	uint32_t i;
99 	char *tmp;
100 	int ret;
101 	time_t t = time(NULL);
102 	struct tm *tm_now = localtime(&t);
103 
104 	ret = fips_test_fetch_one_block();
105 	if (ret < 0)
106 		return ret;
107 
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();
112 			if (ret < 0)
113 				return ret;
114 		}
115 
116 		tmp = strstr(info.vec[i], "# Config info for ");
117 		if (tmp != NULL) {
118 			fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ",
119 					info.device_name);
120 			continue;
121 		}
122 
123 		tmp = strstr(info.vec[i], "#  HMAC information for ");
124 		if (tmp != NULL) {
125 			fprintf(info.fp_wr, "%s%s\n", "#  HMAC information for "
126 				"DPDK Cryptodev ",
127 				info.device_name);
128 			continue;
129 		}
130 
131 		tmp = strstr(info.vec[i], "# Config Info for : ");
132 		if (tmp != NULL) {
133 
134 			fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ",
135 					info.device_name);
136 			continue;
137 		}
138 
139 		tmp = strstr(info.vec[i], "# information for ");
140 		if (tmp != NULL) {
141 
142 			char tmp_output[128] = {0};
143 
144 			strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
145 
146 			fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
147 					"information for DPDK Cryptodev ",
148 					info.device_name);
149 			continue;
150 		}
151 
152 		tmp = strstr(info.vec[i], " test information for ");
153 		if (tmp != NULL) {
154 			char tmp_output[128] = {0};
155 
156 			strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
157 
158 			fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
159 					"test information for DPDK Cryptodev ",
160 					info.device_name);
161 			continue;
162 		}
163 
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 ",
167 					asctime(tm_now));
168 			continue;
169 		}
170 
171 		/* to this point, no field need to update,
172 		 *  only copy to rsp file
173 		 */
174 		fprintf(info.fp_wr, "%s\n", info.vec[i]);
175 	}
176 
177 	return 0;
178 }
179 
180 static int
181 parse_file_type(const char *path)
182 {
183 	const char *tmp = path + strlen(path) - 3;
184 
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;
191 	else
192 		return -EINVAL;
193 
194 	return 0;
195 }
196 
197 int
198 fips_test_init(const char *req_file_path, const char *rsp_file_path,
199 		const char *device_name)
200 {
201 	if (strcmp(req_file_path, rsp_file_path) == 0) {
202 		RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
203 		return -EINVAL;
204 	}
205 
206 	fips_test_clear();
207 
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",
211 				req_file_path);
212 		return -EINVAL;
213 	}
214 
215 	info.fp_rd = fopen(req_file_path, "r");
216 	if (!info.fp_rd) {
217 		RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
218 		return -EINVAL;
219 	}
220 
221 	info.fp_wr = fopen(rsp_file_path, "w");
222 	if (!info.fp_wr) {
223 		RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
224 		return -EINVAL;
225 	}
226 
227 	info.one_line_text = calloc(1, MAX_LINE_CHAR);
228 	if (!info.one_line_text) {
229 		RTE_LOG(ERR, USER1, "Insufficient memory\n");
230 		return -ENOMEM;
231 	}
232 
233 	strlcpy(info.device_name, device_name, sizeof(info.device_name));
234 
235 	if (fips_test_parse_header() < 0) {
236 		RTE_LOG(ERR, USER1, "Failed parsing header\n");
237 		return -1;
238 	}
239 
240 	return 0;
241 }
242 
243 void
244 fips_test_clear(void)
245 {
246 	if (info.fp_rd)
247 		fclose(info.fp_rd);
248 	if (info.fp_wr)
249 		fclose(info.fp_wr);
250 	if (info.one_line_text)
251 		free(info.one_line_text);
252 	if (info.nb_vec_lines) {
253 		uint32_t i;
254 
255 		for (i = 0; i < info.nb_vec_lines; i++)
256 			free(info.vec[i]);
257 	}
258 
259 	memset(&info, 0, sizeof(info));
260 }
261 
262 int
263 fips_test_parse_one_case(void)
264 {
265 	uint32_t i, j = 0;
266 	uint32_t is_interim = 0;
267 	int ret;
268 
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)) {
274 					is_interim = 1;
275 
276 					ret = info.interim_callbacks[j].cb(
277 						info.interim_callbacks[j].key,
278 						info.vec[i],
279 						info.interim_callbacks[j].val);
280 					if (ret < 0)
281 						return ret;
282 				}
283 		}
284 	}
285 
286 	if (is_interim) {
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");
290 		return 1;
291 	}
292 
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);
299 				if (ret < 0)
300 					return ret;
301 				break;
302 			}
303 	}
304 
305 	return 0;
306 }
307 
308 void
309 fips_test_write_one_case(void)
310 {
311 	uint32_t i;
312 
313 	for (i = 0; i < info.nb_vec_lines; i++)
314 		fprintf(info.fp_wr, "%s\n", info.vec[i]);
315 }
316 
317 static int
318 parser_read_uint64_hex(uint64_t *value, const char *p)
319 {
320 	char *next;
321 	uint64_t val;
322 
323 	p = skip_white_spaces(p);
324 
325 	val = strtoul(p, &next, 16);
326 	if (p == next)
327 		return -EINVAL;
328 
329 	p = skip_white_spaces(next);
330 	if (*p != '\0')
331 		return -EINVAL;
332 
333 	*value = val;
334 	return 0;
335 }
336 
337 int
338 parser_read_uint8_hex(uint8_t *value, const char *p)
339 {
340 	uint64_t val = 0;
341 	int ret = parser_read_uint64_hex(&val, p);
342 
343 	if (ret < 0)
344 		return ret;
345 
346 	if (val > UINT8_MAX)
347 		return -ERANGE;
348 
349 	*value = val;
350 	return 0;
351 }
352 
353 int
354 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
355 {
356 	struct fips_val tmp_val = {0};
357 	uint32_t len = val->len;
358 	int ret;
359 
360 	if (len == 0) {
361 		if (val->val != NULL) {
362 			rte_free(val->val);
363 			val->val = NULL;
364 		}
365 
366 		return 0;
367 	}
368 
369 	ret = parse_uint8_hex_str(key, src, &tmp_val);
370 	if (ret < 0)
371 		return ret;
372 
373 	if (tmp_val.len == val->len) {
374 		val->val = tmp_val.val;
375 		return 0;
376 	}
377 
378 	if (tmp_val.len < val->len) {
379 		rte_free(tmp_val.val);
380 		return -EINVAL;
381 	}
382 
383 	val->val = rte_zmalloc(NULL, val->len, 0);
384 	if (!val->val) {
385 		rte_free(tmp_val.val);
386 		memset(val, 0, sizeof(*val));
387 		return -ENOMEM;
388 	}
389 
390 	memcpy(val->val, tmp_val.val, val->len);
391 	rte_free(tmp_val.val);
392 
393 	return 0;
394 }
395 
396 int
397 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
398 {
399 	uint32_t len, j;
400 
401 	src += strlen(key);
402 
403 	len = strlen(src) / 2;
404 
405 	if (val->val) {
406 		rte_free(val->val);
407 		val->val = NULL;
408 	}
409 
410 	val->val = rte_zmalloc(NULL, len, 0);
411 	if (!val->val)
412 		return -ENOMEM;
413 
414 	for (j = 0; j < len; j++) {
415 		char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
416 
417 		if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
418 			rte_free(val->val);
419 			memset(val, 0, sizeof(*val));
420 			return -EINVAL;
421 		}
422 	}
423 
424 	val->len = len;
425 
426 	return 0;
427 }
428 
429 int
430 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
431 {
432 	char *data = src + strlen(key);
433 	size_t data_len = strlen(data);
434 	int ret;
435 
436 	if (data[data_len - 1] == ']') {
437 		char *tmp_data = calloc(1, data_len + 1);
438 
439 		if (tmp_data == NULL)
440 			return -ENOMEM;
441 
442 		strlcpy(tmp_data, data, data_len);
443 
444 		ret = parser_read_uint32(&val->len, tmp_data);
445 
446 		free(tmp_data);
447 	} else
448 		ret = parser_read_uint32(&val->len, data);
449 
450 	return ret;
451 }
452 
453 int
454 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
455 {
456 	int ret;
457 
458 	ret = parser_read_uint32_val(key, src, val);
459 
460 	if (ret < 0)
461 		return ret;
462 
463 	val->len /= 8;
464 
465 	return 0;
466 }
467 
468 int
469 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
470 {
471 	char *str = dst;
472 	uint32_t len;
473 
474 	str += strlen(key);
475 
476 	for (len = 0; len < val->len; len++)
477 		snprintf(str + len * 2, 255, "%02x", val->val[len]);
478 
479 	return 0;
480 }
481 
482 static int
483 parser_read_uint64(uint64_t *value, const char *p)
484 {
485 	char *next;
486 	uint64_t val;
487 
488 	p = skip_white_spaces(p);
489 	if (!isdigit(*p))
490 		return -EINVAL;
491 
492 	val = strtoul(p, &next, 10);
493 	if (p == next)
494 		return -EINVAL;
495 
496 	p = next;
497 	switch (*p) {
498 	case 'T':
499 		val *= 1024ULL;
500 		/* fall through */
501 	case 'G':
502 		val *= 1024ULL;
503 		/* fall through */
504 	case 'M':
505 		val *= 1024ULL;
506 		/* fall through */
507 	case 'k':
508 	case 'K':
509 		val *= 1024ULL;
510 		p++;
511 		break;
512 	}
513 
514 	p = skip_white_spaces(p);
515 	if (*p != '\0')
516 		return -EINVAL;
517 
518 	*value = val;
519 	return 0;
520 }
521 
522 int
523 parser_read_uint32(uint32_t *value, char *p)
524 {
525 	uint64_t val = 0;
526 	int ret = parser_read_uint64(&val, p);
527 
528 	if (ret < 0)
529 		return ret;
530 
531 	if (val > UINT32_MAX)
532 		return -EINVAL;
533 
534 	*value = val;
535 	return 0;
536 }
537 
538 void
539 parse_write_hex_str(struct fips_val *src)
540 {
541 	writeback_hex_str("", info.one_line_text, src);
542 
543 	fprintf(info.fp_wr, "%s\n", info.one_line_text);
544 }
545 
546 int
547 update_info_vec(uint32_t count)
548 {
549 	const struct fips_test_callback *cb;
550 	uint32_t i, j;
551 
552 	if (!info.writeback_callbacks)
553 		return -1;
554 
555 	cb = &info.writeback_callbacks[0];
556 
557 	snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key, count);
558 
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);
564 				break;
565 			}
566 		}
567 	}
568 
569 	return 0;
570 }
571