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