xref: /dpdk/examples/fips_validation/fips_validation_xts.c (revision 10b71caecbe1cddcbb65c050ca775fba575e88db)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (C) 2020 Marvell International Ltd.
3  */
4 
5 #include <string.h>
6 #include <stdio.h>
7 #include <time.h>
8 
9 #include <rte_cryptodev.h>
10 
11 #include "fips_validation.h"
12 
13 #define MODE_STR	"XTS"
14 #define ALGO_STR	"test data for "
15 #define OP_STR		"State"
16 #define KEY_SIZE_STR	"Key Length : "
17 
18 #define COUNT_STR	"COUNT = "
19 #define KEY_STR		"Key = "
20 #define IV_STR		"i = "
21 #define PT_STR		"PT = "
22 #define CT_STR		"CT = "
23 
24 #define OP_ENC_STR	"ENCRYPT"
25 #define OP_DEC_STR	"DECRYPT"
26 
27 static int
28 parse_interim_xts_enc_dec(const char *key,
29 		__rte_unused char *text,
30 		__rte_unused struct fips_val *val)
31 {
32 	if (strcmp(key, OP_ENC_STR) == 0)
33 		info.op = FIPS_TEST_ENC_AUTH_GEN;
34 	else if (strcmp(key, OP_DEC_STR) == 0)
35 		info.op = FIPS_TEST_DEC_AUTH_VERIF;
36 	else
37 		return -1;
38 	return 0;
39 }
40 
41 struct fips_test_callback xts_tests_vectors[] = {
42 		{KEY_STR, parse_uint8_hex_str, &vec.cipher_auth.key},
43 		{IV_STR, parse_uint8_hex_str, &vec.iv},
44 		{PT_STR, parse_uint8_hex_str, &vec.pt},
45 		{CT_STR, parse_uint8_hex_str, &vec.ct},
46 		{NULL, NULL, NULL} /**< end pointer */
47 };
48 
49 struct fips_test_callback xts_tests_interim_vectors[] = {
50 		{OP_ENC_STR, parse_interim_xts_enc_dec, NULL},
51 		{OP_DEC_STR, parse_interim_xts_enc_dec, NULL},
52 		{NULL, NULL, NULL} /**< end pointer */
53 };
54 
55 struct fips_test_callback xts_writeback_callbacks[] = {
56 		/** First element is used to pass COUNT string */
57 		{COUNT_STR, NULL, NULL},
58 		{IV_STR, writeback_hex_str, &vec.iv},
59 		{KEY_STR, writeback_hex_str, &vec.cipher_auth.key},
60 		{PT_STR, writeback_hex_str, &vec.pt},
61 		{CT_STR, writeback_hex_str, &vec.ct},
62 		{NULL, NULL, NULL} /**< end pointer */
63 };
64 
65 static int
66 parse_test_xts_writeback(struct fips_val *val)
67 {
68 	if (info.op == FIPS_TEST_ENC_AUTH_GEN)
69 		fprintf(info.fp_wr, "%s", CT_STR);
70 	else
71 		fprintf(info.fp_wr, "%s", PT_STR);
72 
73 	parse_write_hex_str(val);
74 	return 0;
75 }
76 
77 static int
78 rsp_test_xts_check(struct fips_val *val)
79 {
80 	struct fips_val *data;
81 	if (info.op == FIPS_TEST_ENC_AUTH_GEN)
82 		data = &vec.ct;
83 	else
84 		data = &vec.pt;
85 
86 	if (memcmp(val->val, data->val, val->len) == 0)
87 		fprintf(info.fp_wr, "Success\n");
88 	else
89 		fprintf(info.fp_wr, "Failed\n");
90 	return 0;
91 }
92 
93 int parse_test_xts_init(void)
94 {
95 	char *tmp;
96 	uint32_t i;
97 	for (i = 0; i < info.nb_vec_lines; i++) {
98 		char *line = info.vec[i];
99 		tmp = strstr(line, KEY_SIZE_STR);
100 		if (tmp) {
101 			tmp += (strlen(KEY_SIZE_STR) + strlen("AES"));
102 			if (parser_read_uint32(
103 				&info.interim_info.aes_data.key_len,
104 					tmp) < 0)
105 				return -EINVAL;
106 			info.interim_info.aes_data.key_len =
107 			(info.interim_info.aes_data.key_len*2) / 8;
108 			continue;
109 		}
110 
111 	}
112 	info.parse_writeback = parse_test_xts_writeback;
113 	info.callbacks = xts_tests_vectors;
114 	info.interim_callbacks = xts_tests_interim_vectors;
115 	info.writeback_callbacks = xts_writeback_callbacks;
116 	info.kat_check = rsp_test_xts_check;
117 
118 	return 0;
119 }
120