xref: /dpdk/examples/fips_validation/fips_validation.c (revision f4eac3a09c51a1a2dab1f2fd3a10fe0619286a0d)
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 void
96 fips_test_parse_version(void)
97 {
98 	int len = strlen(info.vec[0]);
99 	char *ptr = info.vec[0];
100 
101 	info.version = strtof(ptr + len - 4, NULL);
102 }
103 
104 static int
105 fips_test_parse_header(void)
106 {
107 	uint32_t i;
108 	char *tmp;
109 	int ret;
110 	int algo_parsed = 0;
111 	time_t t = time(NULL);
112 	struct tm *tm_now = localtime(&t);
113 
114 	ret = fips_test_fetch_one_block();
115 	if (ret < 0)
116 		return ret;
117 
118 	if (info.nb_vec_lines)
119 		fips_test_parse_version();
120 
121 	for (i = 0; i < info.nb_vec_lines; i++) {
122 		if (!algo_parsed) {
123 			if (strstr(info.vec[i], "AESVS")) {
124 				algo_parsed = 1;
125 				info.algo = FIPS_TEST_ALGO_AES;
126 				ret = parse_test_aes_init();
127 				if (ret < 0)
128 					return ret;
129 			} else if (strstr(info.vec[i], "GCM")) {
130 				algo_parsed = 1;
131 				info.algo = FIPS_TEST_ALGO_AES_GCM;
132 				ret = parse_test_gcm_init();
133 				if (ret < 0)
134 					return ret;
135 			} else if (strstr(info.vec[i], "CMAC")) {
136 				algo_parsed = 1;
137 				info.algo = FIPS_TEST_ALGO_AES_CMAC;
138 				ret = parse_test_cmac_init();
139 				if (ret < 0)
140 					return 0;
141 			} else if (strstr(info.vec[i], "CCM")) {
142 				algo_parsed = 1;
143 				info.algo = FIPS_TEST_ALGO_AES_CCM;
144 				ret = parse_test_ccm_init();
145 				if (ret < 0)
146 					return 0;
147 			} else if (strstr(info.vec[i], "HMAC")) {
148 				algo_parsed = 1;
149 				info.algo = FIPS_TEST_ALGO_HMAC;
150 				ret = parse_test_hmac_init();
151 				if (ret < 0)
152 					return ret;
153 			} else if (strstr(info.vec[i], "TDES")) {
154 				algo_parsed = 1;
155 				info.algo = FIPS_TEST_ALGO_TDES;
156 				ret = parse_test_tdes_init();
157 				if (ret < 0)
158 					return 0;
159 			} else if (strstr(info.vec[i], "PERMUTATION")) {
160 				algo_parsed = 1;
161 				info.algo = FIPS_TEST_ALGO_TDES;
162 				ret = parse_test_tdes_init();
163 				if (ret < 0)
164 					return 0;
165 			} else if (strstr(info.vec[i], "VARIABLE")) {
166 				algo_parsed = 1;
167 				info.algo = FIPS_TEST_ALGO_TDES;
168 				ret = parse_test_tdes_init();
169 				if (ret < 0)
170 					return 0;
171 			} else if (strstr(info.vec[i], "SUBSTITUTION")) {
172 				algo_parsed = 1;
173 				info.algo = FIPS_TEST_ALGO_TDES;
174 				ret = parse_test_tdes_init();
175 				if (ret < 0)
176 					return 0;
177 			} else if (strstr(info.vec[i], "SHA-")) {
178 				algo_parsed = 1;
179 				info.algo = FIPS_TEST_ALGO_SHA;
180 				ret = parse_test_sha_init();
181 				if (ret < 0)
182 					return ret;
183 			} else if (strstr(info.vec[i], "XTS")) {
184 				algo_parsed = 1;
185 				info.algo = FIPS_TEST_ALGO_AES_XTS;
186 				ret = parse_test_xts_init();
187 				if (ret < 0)
188 					return ret;
189 			}
190 		}
191 
192 		tmp = strstr(info.vec[i], "# Config info for ");
193 		if (tmp != NULL) {
194 			fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ",
195 					info.device_name);
196 			continue;
197 		}
198 
199 		tmp = strstr(info.vec[i], "#  HMAC information for ");
200 		if (tmp != NULL) {
201 			fprintf(info.fp_wr, "%s%s\n", "#  HMAC information for "
202 				"DPDK Cryptodev ",
203 				info.device_name);
204 			continue;
205 		}
206 
207 		tmp = strstr(info.vec[i], "# Config Info for : ");
208 		if (tmp != NULL) {
209 
210 			fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ",
211 					info.device_name);
212 			continue;
213 		}
214 
215 		tmp = strstr(info.vec[i], "# information for ");
216 		if (tmp != NULL) {
217 
218 			char tmp_output[128] = {0};
219 
220 			strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
221 
222 			fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
223 					"information for DPDK Cryptodev ",
224 					info.device_name);
225 			continue;
226 		}
227 
228 		tmp = strstr(info.vec[i], " test information for ");
229 		if (tmp != NULL) {
230 			char tmp_output[128] = {0};
231 
232 			strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
233 
234 			fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
235 					"test information for DPDK Cryptodev ",
236 					info.device_name);
237 			continue;
238 		}
239 
240 		tmp = strstr(info.vec[i], "\" information for \"");
241 		if (tmp != NULL) {
242 			char tmp_output[128] = {0};
243 
244 			strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
245 
246 			fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
247 					"\" information for DPDK Cryptodev ",
248 					info.device_name);
249 			continue;
250 		}
251 
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 ",
255 					asctime(tm_now));
256 			continue;
257 		}
258 
259 		/* to this point, no field need to update,
260 		 *  only copy to rsp file
261 		 */
262 		fprintf(info.fp_wr, "%s\n", info.vec[i]);
263 	}
264 
265 	return 0;
266 }
267 
268 static int
269 parse_file_type(const char *path)
270 {
271 	const char *tmp = path + strlen(path) - 3;
272 
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;
279 	else
280 		return -EINVAL;
281 
282 	return 0;
283 }
284 
285 int
286 fips_test_init(const char *req_file_path, const char *rsp_file_path,
287 		const char *device_name)
288 {
289 	if (strcmp(req_file_path, rsp_file_path) == 0) {
290 		RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
291 		return -EINVAL;
292 	}
293 
294 	fips_test_clear();
295 
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);
299 		return -EINVAL;
300 	}
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",
304 				req_file_path);
305 		return -EINVAL;
306 	}
307 
308 	info.fp_rd = fopen(req_file_path, "r");
309 	if (!info.fp_rd) {
310 		RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
311 		return -EINVAL;
312 	}
313 
314 	info.fp_wr = fopen(rsp_file_path, "w");
315 	if (!info.fp_wr) {
316 		RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
317 		return -EINVAL;
318 	}
319 
320 	info.one_line_text = calloc(1, MAX_LINE_CHAR);
321 	if (!info.one_line_text) {
322 		RTE_LOG(ERR, USER1, "Insufficient memory\n");
323 		return -ENOMEM;
324 	}
325 
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);
329 		return -EINVAL;
330 	}
331 
332 	if (fips_test_parse_header() < 0) {
333 		RTE_LOG(ERR, USER1, "Failed parsing header\n");
334 		return -1;
335 	}
336 
337 	return 0;
338 }
339 
340 void
341 fips_test_clear(void)
342 {
343 	if (info.fp_rd)
344 		fclose(info.fp_rd);
345 	if (info.fp_wr)
346 		fclose(info.fp_wr);
347 	free(info.one_line_text);
348 	if (info.nb_vec_lines) {
349 		uint32_t i;
350 
351 		for (i = 0; i < info.nb_vec_lines; i++)
352 			free(info.vec[i]);
353 	}
354 
355 	memset(&info, 0, sizeof(info));
356 }
357 
358 int
359 fips_test_parse_one_case(void)
360 {
361 	uint32_t i, j = 0;
362 	uint32_t is_interim;
363 	uint32_t interim_cnt = 0;
364 	int ret;
365 
366 	info.vec_start_off = 0;
367 
368 	if (info.interim_callbacks) {
369 		for (i = 0; i < info.nb_vec_lines; i++) {
370 			is_interim = 0;
371 			for (j = 0; info.interim_callbacks[j].key != NULL; j++)
372 				if (strstr(info.vec[i],
373 					info.interim_callbacks[j].key)) {
374 					is_interim = 1;
375 
376 					ret = info.interim_callbacks[j].cb(
377 						info.interim_callbacks[j].key,
378 						info.vec[i],
379 						info.interim_callbacks[j].val);
380 					if (ret < 0)
381 						return ret;
382 				}
383 
384 			if (is_interim)
385 				interim_cnt += 1;
386 		}
387 	}
388 
389 	if (interim_cnt) {
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");
394 
395 			if (info.nb_vec_lines == interim_cnt)
396 				return 1;
397 		} else {
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");
401 			return 1;
402 		}
403 	}
404 
405 	info.vec_start_off = interim_cnt;
406 
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);
413 				if (ret < 0)
414 					return ret;
415 				break;
416 			}
417 	}
418 
419 	return 0;
420 }
421 
422 void
423 fips_test_write_one_case(void)
424 {
425 	uint32_t i;
426 
427 	for (i = info.vec_start_off; i < info.nb_vec_lines; i++)
428 		fprintf(info.fp_wr, "%s\n", info.vec[i]);
429 }
430 
431 static int
432 parser_read_uint64_hex(uint64_t *value, const char *p)
433 {
434 	char *next;
435 	uint64_t val;
436 
437 	p = skip_white_spaces(p);
438 
439 	val = strtoul(p, &next, 16);
440 	if (p == next)
441 		return -EINVAL;
442 
443 	p = skip_white_spaces(next);
444 	if (*p != '\0')
445 		return -EINVAL;
446 
447 	*value = val;
448 	return 0;
449 }
450 
451 int
452 parser_read_uint8_hex(uint8_t *value, const char *p)
453 {
454 	uint64_t val = 0;
455 	int ret = parser_read_uint64_hex(&val, p);
456 
457 	if (ret < 0)
458 		return ret;
459 
460 	if (val > UINT8_MAX)
461 		return -ERANGE;
462 
463 	*value = val;
464 	return 0;
465 }
466 
467 int
468 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
469 {
470 	struct fips_val tmp_val = {0};
471 	uint32_t len = val->len;
472 	int ret;
473 
474 	if (len == 0) {
475 		if (val->val != NULL) {
476 			rte_free(val->val);
477 			val->val = NULL;
478 		}
479 
480 		return 0;
481 	}
482 
483 	ret = parse_uint8_hex_str(key, src, &tmp_val);
484 	if (ret < 0)
485 		return ret;
486 
487 	if (tmp_val.len == val->len) {
488 		val->val = tmp_val.val;
489 		return 0;
490 	}
491 
492 	if (tmp_val.len < val->len) {
493 		rte_free(tmp_val.val);
494 		return -EINVAL;
495 	}
496 
497 	val->val = rte_zmalloc(NULL, val->len, 0);
498 	if (!val->val) {
499 		rte_free(tmp_val.val);
500 		memset(val, 0, sizeof(*val));
501 		return -ENOMEM;
502 	}
503 
504 	memcpy(val->val, tmp_val.val, val->len);
505 	rte_free(tmp_val.val);
506 
507 	return 0;
508 }
509 
510 int
511 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
512 {
513 	uint32_t len, j;
514 
515 	src += strlen(key);
516 
517 	len = strlen(src) / 2;
518 
519 	if (val->val) {
520 		rte_free(val->val);
521 		val->val = NULL;
522 	}
523 
524 	val->val = rte_zmalloc(NULL, len, 0);
525 	if (!val->val)
526 		return -ENOMEM;
527 
528 	for (j = 0; j < len; j++) {
529 		char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
530 
531 		if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
532 			rte_free(val->val);
533 			memset(val, 0, sizeof(*val));
534 			return -EINVAL;
535 		}
536 	}
537 
538 	val->len = len;
539 
540 	return 0;
541 }
542 
543 int
544 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
545 {
546 	char *data = src + strlen(key);
547 	size_t data_len = strlen(data);
548 	int ret;
549 
550 	if (data[data_len - 1] == ']') {
551 		char *tmp_data = calloc(1, data_len + 1);
552 
553 		if (tmp_data == NULL)
554 			return -ENOMEM;
555 
556 		strlcpy(tmp_data, data, data_len);
557 
558 		ret = parser_read_uint32(&val->len, tmp_data);
559 
560 		free(tmp_data);
561 	} else
562 		ret = parser_read_uint32(&val->len, data);
563 
564 	return ret;
565 }
566 
567 int
568 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
569 {
570 	int ret;
571 
572 	ret = parser_read_uint32_val(key, src, val);
573 
574 	if (ret < 0)
575 		return ret;
576 
577 	val->len /= 8;
578 
579 	return 0;
580 }
581 
582 int
583 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
584 {
585 	char *str = dst;
586 	uint32_t len;
587 
588 	str += strlen(key);
589 
590 	for (len = 0; len < val->len; len++)
591 		snprintf(str + len * 2, 255, "%02x", val->val[len]);
592 
593 	return 0;
594 }
595 
596 static int
597 parser_read_uint64(uint64_t *value, const char *p)
598 {
599 	char *next;
600 	uint64_t val;
601 
602 	p = skip_white_spaces(p);
603 	if (!isdigit(*p))
604 		return -EINVAL;
605 
606 	val = strtoul(p, &next, 10);
607 	if (p == next)
608 		return -EINVAL;
609 
610 	p = next;
611 	switch (*p) {
612 	case 'T':
613 		val *= 1024ULL;
614 		/* fall through */
615 	case 'G':
616 		val *= 1024ULL;
617 		/* fall through */
618 	case 'M':
619 		val *= 1024ULL;
620 		/* fall through */
621 	case 'k':
622 	case 'K':
623 		val *= 1024ULL;
624 		p++;
625 		break;
626 	}
627 
628 	p = skip_white_spaces(p);
629 	if (*p != '\0')
630 		return -EINVAL;
631 
632 	*value = val;
633 	return 0;
634 }
635 
636 int
637 parser_read_uint32(uint32_t *value, char *p)
638 {
639 	uint64_t val = 0;
640 	int ret = parser_read_uint64(&val, p);
641 
642 	if (ret < 0)
643 		return ret;
644 
645 	if (val > UINT32_MAX)
646 		return -EINVAL;
647 
648 	*value = val;
649 	return 0;
650 }
651 
652 int
653 parser_read_uint16(uint16_t *value, const char *p)
654 {
655 	uint64_t val = 0;
656 	int ret = parser_read_uint64(&val, p);
657 
658 	if (ret < 0)
659 		return ret;
660 
661 	if (val > UINT16_MAX)
662 		return -ERANGE;
663 
664 	*value = val;
665 	return 0;
666 }
667 
668 void
669 parse_write_hex_str(struct fips_val *src)
670 {
671 	writeback_hex_str("", info.one_line_text, src);
672 
673 	fprintf(info.fp_wr, "%s\n", info.one_line_text);
674 }
675 
676 int
677 update_info_vec(uint32_t count)
678 {
679 	const struct fips_test_callback *cb;
680 	uint32_t i, j;
681 
682 	if (!info.writeback_callbacks)
683 		return -1;
684 
685 	cb = &info.writeback_callbacks[0];
686 
687 	if ((info.version == 21.4f) && (!(strstr(info.vec[0], cb->key)))) {
688 		fprintf(info.fp_wr, "%s%u\n", cb->key, count);
689 		i = 0;
690 	} else {
691 		snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key,
692 				count);
693 		i = 1;
694 	}
695 
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);
701 				break;
702 			}
703 		}
704 	}
705 
706 	return 0;
707 }
708