xref: /dpdk/examples/fips_validation/fips_validation.c (revision 8809f78c7dd9f33a44a4f89c58fc91ded34296ed)
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 = 1; 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 	if (info.one_line_text)
348 		free(info.one_line_text);
349 	if (info.nb_vec_lines) {
350 		uint32_t i;
351 
352 		for (i = 0; i < info.nb_vec_lines; i++)
353 			free(info.vec[i]);
354 	}
355 
356 	memset(&info, 0, sizeof(info));
357 }
358 
359 int
360 fips_test_parse_one_case(void)
361 {
362 	uint32_t i, j = 0;
363 	uint32_t is_interim;
364 	uint32_t interim_cnt = 0;
365 	int ret;
366 
367 	info.vec_start_off = 0;
368 
369 	if (info.interim_callbacks) {
370 		for (i = 0; i < info.nb_vec_lines; i++) {
371 			is_interim = 0;
372 			for (j = 0; info.interim_callbacks[j].key != NULL; j++)
373 				if (strstr(info.vec[i],
374 					info.interim_callbacks[j].key)) {
375 					is_interim = 1;
376 
377 					ret = info.interim_callbacks[j].cb(
378 						info.interim_callbacks[j].key,
379 						info.vec[i],
380 						info.interim_callbacks[j].val);
381 					if (ret < 0)
382 						return ret;
383 				}
384 
385 			if (is_interim)
386 				interim_cnt += 1;
387 		}
388 	}
389 
390 	if (interim_cnt) {
391 		if (info.version == 21.4f) {
392 			for (i = 0; i < interim_cnt; i++)
393 				fprintf(info.fp_wr, "%s\n", info.vec[i]);
394 			fprintf(info.fp_wr, "\n");
395 
396 			if (info.nb_vec_lines == interim_cnt)
397 				return 1;
398 		} else {
399 			for (i = 0; i < info.nb_vec_lines; i++)
400 				fprintf(info.fp_wr, "%s\n", info.vec[i]);
401 			fprintf(info.fp_wr, "\n");
402 			return 1;
403 		}
404 	}
405 
406 	info.vec_start_off = interim_cnt;
407 
408 	for (i = info.vec_start_off; i < info.nb_vec_lines; i++) {
409 		for (j = 0; info.callbacks[j].key != NULL; j++)
410 			if (strstr(info.vec[i], info.callbacks[j].key)) {
411 				ret = info.callbacks[j].cb(
412 					info.callbacks[j].key,
413 					info.vec[i], info.callbacks[j].val);
414 				if (ret < 0)
415 					return ret;
416 				break;
417 			}
418 	}
419 
420 	return 0;
421 }
422 
423 void
424 fips_test_write_one_case(void)
425 {
426 	uint32_t i;
427 
428 	for (i = info.vec_start_off; i < info.nb_vec_lines; i++)
429 		fprintf(info.fp_wr, "%s\n", info.vec[i]);
430 }
431 
432 static int
433 parser_read_uint64_hex(uint64_t *value, const char *p)
434 {
435 	char *next;
436 	uint64_t val;
437 
438 	p = skip_white_spaces(p);
439 
440 	val = strtoul(p, &next, 16);
441 	if (p == next)
442 		return -EINVAL;
443 
444 	p = skip_white_spaces(next);
445 	if (*p != '\0')
446 		return -EINVAL;
447 
448 	*value = val;
449 	return 0;
450 }
451 
452 int
453 parser_read_uint8_hex(uint8_t *value, const char *p)
454 {
455 	uint64_t val = 0;
456 	int ret = parser_read_uint64_hex(&val, p);
457 
458 	if (ret < 0)
459 		return ret;
460 
461 	if (val > UINT8_MAX)
462 		return -ERANGE;
463 
464 	*value = val;
465 	return 0;
466 }
467 
468 int
469 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
470 {
471 	struct fips_val tmp_val = {0};
472 	uint32_t len = val->len;
473 	int ret;
474 
475 	if (len == 0) {
476 		if (val->val != NULL) {
477 			rte_free(val->val);
478 			val->val = NULL;
479 		}
480 
481 		return 0;
482 	}
483 
484 	ret = parse_uint8_hex_str(key, src, &tmp_val);
485 	if (ret < 0)
486 		return ret;
487 
488 	if (tmp_val.len == val->len) {
489 		val->val = tmp_val.val;
490 		return 0;
491 	}
492 
493 	if (tmp_val.len < val->len) {
494 		rte_free(tmp_val.val);
495 		return -EINVAL;
496 	}
497 
498 	val->val = rte_zmalloc(NULL, val->len, 0);
499 	if (!val->val) {
500 		rte_free(tmp_val.val);
501 		memset(val, 0, sizeof(*val));
502 		return -ENOMEM;
503 	}
504 
505 	memcpy(val->val, tmp_val.val, val->len);
506 	rte_free(tmp_val.val);
507 
508 	return 0;
509 }
510 
511 int
512 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
513 {
514 	uint32_t len, j;
515 
516 	src += strlen(key);
517 
518 	len = strlen(src) / 2;
519 
520 	if (val->val) {
521 		rte_free(val->val);
522 		val->val = NULL;
523 	}
524 
525 	val->val = rte_zmalloc(NULL, len, 0);
526 	if (!val->val)
527 		return -ENOMEM;
528 
529 	for (j = 0; j < len; j++) {
530 		char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
531 
532 		if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
533 			rte_free(val->val);
534 			memset(val, 0, sizeof(*val));
535 			return -EINVAL;
536 		}
537 	}
538 
539 	val->len = len;
540 
541 	return 0;
542 }
543 
544 int
545 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
546 {
547 	char *data = src + strlen(key);
548 	size_t data_len = strlen(data);
549 	int ret;
550 
551 	if (data[data_len - 1] == ']') {
552 		char *tmp_data = calloc(1, data_len + 1);
553 
554 		if (tmp_data == NULL)
555 			return -ENOMEM;
556 
557 		strlcpy(tmp_data, data, data_len);
558 
559 		ret = parser_read_uint32(&val->len, tmp_data);
560 
561 		free(tmp_data);
562 	} else
563 		ret = parser_read_uint32(&val->len, data);
564 
565 	return ret;
566 }
567 
568 int
569 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
570 {
571 	int ret;
572 
573 	ret = parser_read_uint32_val(key, src, val);
574 
575 	if (ret < 0)
576 		return ret;
577 
578 	val->len /= 8;
579 
580 	return 0;
581 }
582 
583 int
584 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
585 {
586 	char *str = dst;
587 	uint32_t len;
588 
589 	str += strlen(key);
590 
591 	for (len = 0; len < val->len; len++)
592 		snprintf(str + len * 2, 255, "%02x", val->val[len]);
593 
594 	return 0;
595 }
596 
597 static int
598 parser_read_uint64(uint64_t *value, const char *p)
599 {
600 	char *next;
601 	uint64_t val;
602 
603 	p = skip_white_spaces(p);
604 	if (!isdigit(*p))
605 		return -EINVAL;
606 
607 	val = strtoul(p, &next, 10);
608 	if (p == next)
609 		return -EINVAL;
610 
611 	p = next;
612 	switch (*p) {
613 	case 'T':
614 		val *= 1024ULL;
615 		/* fall through */
616 	case 'G':
617 		val *= 1024ULL;
618 		/* fall through */
619 	case 'M':
620 		val *= 1024ULL;
621 		/* fall through */
622 	case 'k':
623 	case 'K':
624 		val *= 1024ULL;
625 		p++;
626 		break;
627 	}
628 
629 	p = skip_white_spaces(p);
630 	if (*p != '\0')
631 		return -EINVAL;
632 
633 	*value = val;
634 	return 0;
635 }
636 
637 int
638 parser_read_uint32(uint32_t *value, char *p)
639 {
640 	uint64_t val = 0;
641 	int ret = parser_read_uint64(&val, p);
642 
643 	if (ret < 0)
644 		return ret;
645 
646 	if (val > UINT32_MAX)
647 		return -EINVAL;
648 
649 	*value = val;
650 	return 0;
651 }
652 
653 void
654 parse_write_hex_str(struct fips_val *src)
655 {
656 	writeback_hex_str("", info.one_line_text, src);
657 
658 	fprintf(info.fp_wr, "%s\n", info.one_line_text);
659 }
660 
661 int
662 update_info_vec(uint32_t count)
663 {
664 	const struct fips_test_callback *cb;
665 	uint32_t i, j;
666 
667 	if (!info.writeback_callbacks)
668 		return -1;
669 
670 	cb = &info.writeback_callbacks[0];
671 
672 	if ((info.version == 21.4f) && (!(strstr(info.vec[0], cb->key)))) {
673 		fprintf(info.fp_wr, "%s%u\n", cb->key, count);
674 		i = 0;
675 	} else {
676 		snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key,
677 				count);
678 		i = 1;
679 	}
680 
681 	for (; i < info.nb_vec_lines; i++) {
682 		for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
683 			cb = &info.writeback_callbacks[j];
684 			if (strstr(info.vec[i], cb->key)) {
685 				cb->cb(cb->key, info.vec[i], cb->val);
686 				break;
687 			}
688 		}
689 	}
690 
691 	return 0;
692 }
693