xref: /dpdk/examples/fips_validation/fips_validation_xts.c (revision 09442498ef736d0a96632cf8b8c15d8ca78a6468)
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 #define ALGO_JSON_STR		"algorithm"
28 #define TESTTYPE_JSON_STR	"testType"
29 #define DIR_JSON_STR		"direction"
30 #define KEYLEN_JSON_STR		"keyLen"
31 #define TWEAKMODE_JSON_STR	"tweakMode"
32 
33 #define KEY_JSON_STR		"key"
34 #define DATAUNITLEN_JSON_STR	"dataUnitLen"
35 #define PAYLOADLEN_JSON_STR	"payloadLen"
36 #define TWEAKVALUE_JSON_STR	"tweakValue"
37 #define PT_JSON_STR	"pt"
38 #define CT_JSON_STR	"ct"
39 
40 #define OP_ENC_JSON_STR	"encrypt"
41 #define OP_DEC_JSON_STR	"decrypt"
42 
43 static int
44 parse_interim_xts_enc_dec(const char *key,
45 		__rte_unused char *text,
46 		__rte_unused struct fips_val *val)
47 {
48 	if (strcmp(key, OP_ENC_STR) == 0)
49 		info.op = FIPS_TEST_ENC_AUTH_GEN;
50 	else if (strcmp(key, OP_DEC_STR) == 0)
51 		info.op = FIPS_TEST_DEC_AUTH_VERIF;
52 	else
53 		return -1;
54 	return 0;
55 }
56 
57 struct fips_test_callback xts_tests_vectors[] = {
58 		{KEY_STR, parse_uint8_hex_str, &vec.cipher_auth.key},
59 		{IV_STR, parse_uint8_hex_str, &vec.iv},
60 		{PT_STR, parse_uint8_hex_str, &vec.pt},
61 		{CT_STR, parse_uint8_hex_str, &vec.ct},
62 		{NULL, NULL, NULL} /**< end pointer */
63 };
64 
65 struct fips_test_callback xts_tests_interim_vectors[] = {
66 		{OP_ENC_STR, parse_interim_xts_enc_dec, NULL},
67 		{OP_DEC_STR, parse_interim_xts_enc_dec, NULL},
68 		{NULL, NULL, NULL} /**< end pointer */
69 };
70 
71 struct fips_test_callback xts_writeback_callbacks[] = {
72 		/** First element is used to pass COUNT string */
73 		{COUNT_STR, NULL, NULL},
74 		{IV_STR, writeback_hex_str, &vec.iv},
75 		{KEY_STR, writeback_hex_str, &vec.cipher_auth.key},
76 		{PT_STR, writeback_hex_str, &vec.pt},
77 		{CT_STR, writeback_hex_str, &vec.ct},
78 		{NULL, NULL, NULL} /**< end pointer */
79 };
80 
81 #ifdef USE_JANSSON
82 static int
83 parser_xts_read_keylen(const char *key, char *src, struct fips_val *val)
84 {
85 	int ret;
86 
87 	ret = parser_read_uint32_bit_val(key, src, val);
88 	if (ret < 0)
89 		return ret;
90 
91 	val->len *= 2;
92 	return 0;
93 }
94 
95 static int
96 parser_xts_read_tweakval(const char *key, char *src, struct fips_val *val)
97 {
98 	int ret;
99 
100 	if (info.interim_info.xts_data.tweak_mode == XTS_TWEAK_MODE_HEX)
101 		ret = parse_uint8_hex_str(key, src, val);
102 	else if (info.interim_info.xts_data.tweak_mode == XTS_TWEAK_MODE_NUMBER)
103 		ret = parser_read_uint32_bit_val(key, src, val);
104 	else
105 		ret = -1;
106 
107 	return ret;
108 }
109 
110 struct fips_test_callback xts_dec_json_vectors[] = {
111 		{KEY_JSON_STR, parse_uint8_known_len_hex_str, &vec.cipher_auth.key},
112 		{TWEAKVALUE_JSON_STR, parser_xts_read_tweakval, &vec.iv},
113 		{CT_JSON_STR, parse_uint8_hex_str, &vec.ct},
114 		{NULL, NULL, NULL} /**< end pointer */
115 };
116 
117 struct fips_test_callback xts_interim_json_vectors[] = {
118 		{KEYLEN_JSON_STR, parser_xts_read_keylen, &vec.cipher_auth.key},
119 		{NULL, NULL, NULL} /**< end pointer */
120 };
121 
122 struct fips_test_callback xts_enc_json_vectors[] = {
123 		{KEY_JSON_STR, parse_uint8_known_len_hex_str, &vec.cipher_auth.key},
124 		{TWEAKVALUE_JSON_STR, parser_xts_read_tweakval, &vec.iv},
125 		{PT_JSON_STR, parse_uint8_hex_str, &vec.pt},
126 		{NULL, NULL, NULL} /**< end pointer */
127 };
128 
129 static int
130 parse_test_xts_json_writeback(struct fips_val *val)
131 {
132 	struct fips_val tmp_val;
133 	json_t *tcId;
134 
135 	tcId = json_object_get(json_info.json_test_case, "tcId");
136 
137 	json_info.json_write_case = json_object();
138 	json_object_set(json_info.json_write_case, "tcId", tcId);
139 
140 	if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
141 		json_t *ct;
142 
143 		tmp_val.val = val->val;
144 		tmp_val.len = vec.pt.len;
145 
146 		writeback_hex_str("", info.one_line_text, &tmp_val);
147 		ct = json_string(info.one_line_text);
148 		json_object_set_new(json_info.json_write_case, CT_JSON_STR, ct);
149 
150 		tmp_val.val = val->val + vec.pt.len;
151 		tmp_val.len = val->len - vec.pt.len;
152 
153 		writeback_hex_str("", info.one_line_text, &tmp_val);
154 	} else {
155 		if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
156 			tmp_val.val = val->val;
157 			tmp_val.len = vec.ct.len;
158 
159 			writeback_hex_str("", info.one_line_text, &tmp_val);
160 			json_object_set_new(json_info.json_write_case, PT_JSON_STR,
161 								json_string(info.one_line_text));
162 		} else {
163 			json_object_set_new(json_info.json_write_case, "testPassed", json_false());
164 		}
165 	}
166 
167 	return 0;
168 }
169 
170 int
171 parse_test_xts_json_init(void)
172 {
173 	if (json_info.json_test_group) {
174 		json_t *direction_obj, *tweakmode_obj;
175 		const char *direction_str, *tweakmode_str;
176 
177 		direction_obj = json_object_get(json_info.json_test_group, DIR_JSON_STR);
178 		direction_str = json_string_value(direction_obj);
179 
180 		if (strcmp(direction_str, OP_ENC_JSON_STR) == 0) {
181 			info.op = FIPS_TEST_ENC_AUTH_GEN;
182 			info.callbacks = xts_enc_json_vectors;
183 
184 		} else if (strcmp(direction_str, OP_DEC_JSON_STR) == 0) {
185 			info.op = FIPS_TEST_DEC_AUTH_VERIF;
186 			info.callbacks = xts_dec_json_vectors;
187 		} else {
188 			return -EINVAL;
189 		}
190 
191 		tweakmode_obj = json_object_get(json_info.json_test_group, TWEAKMODE_JSON_STR);
192 		tweakmode_str = json_string_value(tweakmode_obj);
193 		if (strcmp(tweakmode_str, "hex") == 0)
194 			info.interim_info.xts_data.tweak_mode = XTS_TWEAK_MODE_HEX;
195 		else
196 			info.interim_info.xts_data.tweak_mode = XTS_TWEAK_MODE_NUMBER;
197 
198 		info.interim_callbacks = xts_interim_json_vectors;
199 	}
200 
201 	info.parse_writeback = parse_test_xts_json_writeback;
202 	return 0;
203 }
204 #endif /* USE_JANSSON */
205 
206 static int
207 parse_test_xts_writeback(struct fips_val *val)
208 {
209 	if (info.op == FIPS_TEST_ENC_AUTH_GEN)
210 		fprintf(info.fp_wr, "%s", CT_STR);
211 	else
212 		fprintf(info.fp_wr, "%s", PT_STR);
213 
214 	parse_write_hex_str(val);
215 	return 0;
216 }
217 
218 static int
219 rsp_test_xts_check(struct fips_val *val)
220 {
221 	struct fips_val *data;
222 	if (info.op == FIPS_TEST_ENC_AUTH_GEN)
223 		data = &vec.ct;
224 	else
225 		data = &vec.pt;
226 
227 	if (memcmp(val->val, data->val, val->len) == 0)
228 		fprintf(info.fp_wr, "Success\n");
229 	else
230 		fprintf(info.fp_wr, "Failed\n");
231 	return 0;
232 }
233 
234 int parse_test_xts_init(void)
235 {
236 	char *tmp;
237 	uint32_t i;
238 	for (i = 0; i < info.nb_vec_lines; i++) {
239 		char *line = info.vec[i];
240 		tmp = strstr(line, KEY_SIZE_STR);
241 		if (tmp) {
242 			tmp += (strlen(KEY_SIZE_STR) + strlen("AES"));
243 			if (parser_read_uint32(
244 				&info.interim_info.aes_data.key_len,
245 					tmp) < 0)
246 				return -EINVAL;
247 			info.interim_info.aes_data.key_len =
248 			(info.interim_info.aes_data.key_len*2) / 8;
249 			continue;
250 		}
251 
252 	}
253 	info.parse_writeback = parse_test_xts_writeback;
254 	info.callbacks = xts_tests_vectors;
255 	info.interim_callbacks = xts_tests_interim_vectors;
256 	info.writeback_callbacks = xts_writeback_callbacks;
257 	info.kat_check = rsp_test_xts_check;
258 
259 	return 0;
260 }
261