summaryrefslogtreecommitdiff
path: root/examples/fips_validation/fips_validation.c
blob: a835cc3fa6260ec8dc48870ed3e7541fab89b093 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018 Intel Corporation
 */

#include <stdio.h>
#include <string.h>

#include <rte_string_fns.h>
#include <rte_cryptodev.h>
#include <rte_malloc.h>

#include "fips_validation.h"

#define skip_white_spaces(pos)			\
({						\
	__typeof__(pos) _p = (pos);		\
	for ( ; isspace(*_p); _p++)		\
		;				\
	_p;					\
})

static int
get_file_line(void)
{
	FILE *fp = info.fp_rd;
	char *line = info.one_line_text;
	int ret;
	uint32_t loc = 0;

	memset(line, 0, MAX_LINE_CHAR);
	while ((ret = fgetc(fp)) != EOF) {
		char c = (char)ret;

		if (loc >= MAX_LINE_CHAR - 1)
			return -ENOMEM;
		if (c == '\n')
			break;
		line[loc++] = c;
	}

	if (ret == EOF)
		return -EOF;

	return 0;
}

int
fips_test_fetch_one_block(void)
{
	size_t size;
	int ret = 0;
	uint32_t i;

	for (i = 0; i < info.nb_vec_lines; i++) {
		free(info.vec[i]);
		info.vec[i] = NULL;
	}

	i = 0;
	do {
		if (i >= MAX_LINE_PER_VECTOR) {
			ret = -ENOMEM;
			goto error_exit;
		}

		ret = get_file_line();
		size = strlen(info.one_line_text);
		if (size == 0)
			break;

		info.vec[i] = calloc(1, size + 5);
		if (info.vec[i] == NULL)
			goto error_exit;

		strlcpy(info.vec[i], info.one_line_text, size + 1);
		i++;
	} while (ret == 0);

	info.nb_vec_lines = i;

	return ret;

error_exit:
	for (i = 0; i < MAX_LINE_PER_VECTOR; i++)
		if (info.vec[i] != NULL) {
			free(info.vec[i]);
			info.vec[i] = NULL;
		}

	info.nb_vec_lines = 0;

	return -ENOMEM;
}

static int
fips_test_parse_header(void)
{
	uint32_t i;
	char *tmp;
	int ret;
	time_t t = time(NULL);
	struct tm *tm_now = localtime(&t);

	ret = fips_test_fetch_one_block();
	if (ret < 0)
		return ret;

	for (i = 0; i < info.nb_vec_lines; i++) {
		if (strstr(info.vec[i], "AESVS")) {
			info.algo = FIPS_TEST_ALGO_AES;
			ret = parse_test_aes_init();
			if (ret < 0)
				return ret;
		} else if (strstr(info.vec[i], "GCM")) {
			info.algo = FIPS_TEST_ALGO_AES_GCM;
			ret = parse_test_gcm_init();
			if (ret < 0)
				return ret;
		} else if (strstr(info.vec[i], "CMAC")) {
			info.algo = FIPS_TEST_ALGO_AES_CMAC;
			ret = parse_test_cmac_init();
			if (ret < 0)
				return 0;
		} else if (strstr(info.vec[i], "CCM")) {
			info.algo = FIPS_TEST_ALGO_AES_CCM;
			ret = parse_test_ccm_init();
			if (ret < 0)
				return 0;
		} else if (strstr(info.vec[i], "HMAC")) {
			info.algo = FIPS_TEST_ALGO_HMAC;
			ret = parse_test_hmac_init();
			if (ret < 0)
				return ret;
		} else if (strstr(info.vec[i], "TDES")) {
			info.algo = FIPS_TEST_ALGO_TDES;
			ret = parse_test_tdes_init();
			if (ret < 0)
				return 0;
		}

		tmp = strstr(info.vec[i], "# Config info for ");
		if (tmp != NULL) {
			fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ",
					info.device_name);
			continue;
		}

		tmp = strstr(info.vec[i], "#  HMAC information for ");
		if (tmp != NULL) {
			fprintf(info.fp_wr, "%s%s\n", "#  HMAC information for "
				"DPDK Cryptodev ",
				info.device_name);
			continue;
		}

		tmp = strstr(info.vec[i], "# Config Info for : ");
		if (tmp != NULL) {

			fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ",
					info.device_name);
			continue;
		}

		tmp = strstr(info.vec[i], "# information for ");
		if (tmp != NULL) {

			char tmp_output[128] = {0};

			strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);

			fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
					"information for DPDK Cryptodev ",
					info.device_name);
			continue;
		}

		tmp = strstr(info.vec[i], " test information for ");
		if (tmp != NULL) {
			char tmp_output[128] = {0};

			strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);

			fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
					"test information for DPDK Cryptodev ",
					info.device_name);
			continue;
		}

		if (i == info.nb_vec_lines - 1) {
			/** update the time as current time, write to file */
			fprintf(info.fp_wr, "%s%s\n", "# Generated on ",
					asctime(tm_now));
			continue;
		}

		/* to this point, no field need to update,
		 *  only copy to rsp file
		 */
		fprintf(info.fp_wr, "%s\n", info.vec[i]);
	}

	return 0;
}

static int
parse_file_type(const char *path)
{
	const char *tmp = path + strlen(path) - 3;

	if (strstr(tmp, REQ_FILE_PERFIX))
		info.file_type = FIPS_TYPE_REQ;
	else if (strstr(tmp, RSP_FILE_PERFIX))
		info.file_type = FIPS_TYPE_RSP;
	else if (strstr(path, FAX_FILE_PERFIX))
		info.file_type = FIPS_TYPE_FAX;
	else
		return -EINVAL;

	return 0;
}

int
fips_test_init(const char *req_file_path, const char *rsp_file_path,
		const char *device_name)
{
	if (strcmp(req_file_path, rsp_file_path) == 0) {
		RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
		return -EINVAL;
	}

	fips_test_clear();

	info.algo = FIPS_TEST_ALGO_MAX;
	if (parse_file_type(req_file_path) < 0) {
		RTE_LOG(ERR, USER1, "File %s type not supported\n",
				req_file_path);
		return -EINVAL;
	}

	info.fp_rd = fopen(req_file_path, "r");
	if (!info.fp_rd) {
		RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
		return -EINVAL;
	}

	info.fp_wr = fopen(rsp_file_path, "w");
	if (!info.fp_wr) {
		RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
		return -EINVAL;
	}

	info.one_line_text = calloc(1, MAX_LINE_CHAR);
	if (!info.one_line_text) {
		RTE_LOG(ERR, USER1, "Insufficient memory\n");
		return -ENOMEM;
	}

	strlcpy(info.device_name, device_name, sizeof(info.device_name));

	if (fips_test_parse_header() < 0) {
		RTE_LOG(ERR, USER1, "Failed parsing header\n");
		return -1;
	}

	return 0;
}

void
fips_test_clear(void)
{
	if (info.fp_rd)
		fclose(info.fp_rd);
	if (info.fp_wr)
		fclose(info.fp_wr);
	if (info.one_line_text)
		free(info.one_line_text);
	if (info.nb_vec_lines) {
		uint32_t i;

		for (i = 0; i < info.nb_vec_lines; i++)
			free(info.vec[i]);
	}

	memset(&info, 0, sizeof(info));
}

int
fips_test_parse_one_case(void)
{
	uint32_t i, j = 0;
	uint32_t is_interim = 0;
	int ret;

	if (info.interim_callbacks) {
		for (i = 0; i < info.nb_vec_lines; i++) {
			for (j = 0; info.interim_callbacks[j].key != NULL; j++)
				if (strstr(info.vec[i],
					info.interim_callbacks[j].key)) {
					is_interim = 1;

					ret = info.interim_callbacks[j].cb(
						info.interim_callbacks[j].key,
						info.vec[i],
						info.interim_callbacks[j].val);
					if (ret < 0)
						return ret;
				}
		}
	}

	if (is_interim) {
		for (i = 0; i < info.nb_vec_lines; i++)
			fprintf(info.fp_wr, "%s\n", info.vec[i]);
		fprintf(info.fp_wr, "\n");
		return 1;
	}

	for (i = 0; i < info.nb_vec_lines; i++) {
		for (j = 0; info.callbacks[j].key != NULL; j++)
			if (strstr(info.vec[i], info.callbacks[j].key)) {
				ret = info.callbacks[j].cb(
					info.callbacks[j].key,
					info.vec[i], info.callbacks[j].val);
				if (ret < 0)
					return ret;
				break;
			}
	}

	return 0;
}

void
fips_test_write_one_case(void)
{
	uint32_t i;

	for (i = 0; i < info.nb_vec_lines; i++)
		fprintf(info.fp_wr, "%s\n", info.vec[i]);
}

static int
parser_read_uint64_hex(uint64_t *value, const char *p)
{
	char *next;
	uint64_t val;

	p = skip_white_spaces(p);

	val = strtoul(p, &next, 16);
	if (p == next)
		return -EINVAL;

	p = skip_white_spaces(next);
	if (*p != '\0')
		return -EINVAL;

	*value = val;
	return 0;
}

int
parser_read_uint8_hex(uint8_t *value, const char *p)
{
	uint64_t val = 0;
	int ret = parser_read_uint64_hex(&val, p);

	if (ret < 0)
		return ret;

	if (val > UINT8_MAX)
		return -ERANGE;

	*value = val;
	return 0;
}

int
parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
{
	struct fips_val tmp_val = {0};
	uint32_t len = val->len;
	int ret;

	if (len == 0) {
		if (val->val != NULL) {
			rte_free(val->val);
			val->val = NULL;
		}

		return 0;
	}

	ret = parse_uint8_hex_str(key, src, &tmp_val);
	if (ret < 0)
		return ret;

	if (tmp_val.len == val->len) {
		val->val = tmp_val.val;
		return 0;
	}

	if (tmp_val.len < val->len) {
		rte_free(tmp_val.val);
		return -EINVAL;
	}

	val->val = rte_zmalloc(NULL, val->len, 0);
	if (!val->val) {
		rte_free(tmp_val.val);
		memset(val, 0, sizeof(*val));
		return -ENOMEM;
	}

	memcpy(val->val, tmp_val.val, val->len);
	rte_free(tmp_val.val);

	return 0;
}

int
parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
{
	uint32_t len, j;

	src += strlen(key);

	len = strlen(src) / 2;

	if (val->val) {
		rte_free(val->val);
		val->val = NULL;
	}

	val->val = rte_zmalloc(NULL, len, 0);
	if (!val->val)
		return -ENOMEM;

	for (j = 0; j < len; j++) {
		char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};

		if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
			rte_free(val->val);
			memset(val, 0, sizeof(*val));
			return -EINVAL;
		}
	}

	val->len = len;

	return 0;
}

int
parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
{
	char *data = src + strlen(key);
	size_t data_len = strlen(data);
	int ret;

	if (data[data_len - 1] == ']') {
		char *tmp_data = calloc(1, data_len + 1);

		if (tmp_data == NULL)
			return -ENOMEM;

		strlcpy(tmp_data, data, data_len);

		ret = parser_read_uint32(&val->len, tmp_data);

		free(tmp_data);
	} else
		ret = parser_read_uint32(&val->len, data);

	return ret;
}

int
parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
{
	int ret;

	ret = parser_read_uint32_val(key, src, val);

	if (ret < 0)
		return ret;

	val->len /= 8;

	return 0;
}

int
writeback_hex_str(const char *key, char *dst, struct fips_val *val)
{
	char *str = dst;
	uint32_t len;

	str += strlen(key);

	for (len = 0; len < val->len; len++)
		snprintf(str + len * 2, 255, "%02x", val->val[len]);

	return 0;
}

static int
parser_read_uint64(uint64_t *value, const char *p)
{
	char *next;
	uint64_t val;

	p = skip_white_spaces(p);
	if (!isdigit(*p))
		return -EINVAL;

	val = strtoul(p, &next, 10);
	if (p == next)
		return -EINVAL;

	p = next;
	switch (*p) {
	case 'T':
		val *= 1024ULL;
		/* fall through */
	case 'G':
		val *= 1024ULL;
		/* fall through */
	case 'M':
		val *= 1024ULL;
		/* fall through */
	case 'k':
	case 'K':
		val *= 1024ULL;
		p++;
		break;
	}

	p = skip_white_spaces(p);
	if (*p != '\0')
		return -EINVAL;

	*value = val;
	return 0;
}

int
parser_read_uint32(uint32_t *value, char *p)
{
	uint64_t val = 0;
	int ret = parser_read_uint64(&val, p);

	if (ret < 0)
		return ret;

	if (val > UINT32_MAX)
		return -EINVAL;

	*value = val;
	return 0;
}

void
parse_write_hex_str(struct fips_val *src)
{
	writeback_hex_str("", info.one_line_text, src);

	fprintf(info.fp_wr, "%s\n", info.one_line_text);
}

int
update_info_vec(uint32_t count)
{
	const struct fips_test_callback *cb;
	uint32_t i, j;

	if (!info.writeback_callbacks)
		return -1;

	cb = &info.writeback_callbacks[0];

	snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key, count);

	for (i = 1; i < info.nb_vec_lines; i++) {
		for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
			cb = &info.writeback_callbacks[j];
			if (strstr(info.vec[i], cb->key)) {
				cb->cb(cb->key, info.vec[i], cb->val);
				break;
			}
		}
	}

	return 0;
}