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