xref: /dpdk/examples/fips_validation/fips_validation_ccm.c (revision 55a7050e212fe9366c5e0409cfc0892ddee27b3e)
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 DVPT_STR	"CCM-DVPT"
15 #define VADT_STR	"CCM-VADT"
16 #define VPT_STR		"CCM-VPT"
17 #define VNT_STR		"CCM-VNT"
18 #define VTT_STR		"CCM-VTT"
19 
20 #define PARAM_PREFIX	"["
21 #define ALEN_PREFIX	"Alen = "
22 #define PLEN_PREFIX	"Plen = "
23 #define IVLEN_PREFIX	"Nlen = "
24 #define DIGESTL_PREFIX	"Tlen = "
25 
26 #define COUNT_STR	"Count = "
27 #define KEY_STR		"Key = "
28 #define IV_STR		"Nonce = "
29 #define PT_STR		"Payload = "
30 #define CT_STR		"CT = "
31 #define AAD_STR		"Adata = "
32 #define POS_NEG_STR	"Result = "
33 
34 #define POS_KEYWORD	"Pass"
35 #define NEG_KEYWORD	"Fail"
36 
37 #define DIR_JSON_STR	"direction"
38 #define IVLEN_JSON_STR		"ivLen"
39 #define PTLEN_JSON_STR	"payloadLen"
40 #define AADLEN_JSON_STR		"aadLen"
41 #define TAGLEN_JSON_STR		"tagLen"
42 #define KEYLEN_JSON_STR		"keyLen"
43 #define PT_JSON_STR		"pt"
44 #define CT_JSON_STR		"ct"
45 #define KEY_JSON_STR		"key"
46 #define IV_JSON_STR		"iv"
47 #define AAD_JSON_STR		"aad"
48 
49 static int
parser_dvpt_interim(const char * key,char * src,struct fips_val * val)50 parser_dvpt_interim(const char *key, char *src, struct fips_val *val)
51 {
52 	char *tmp, c, value[10];
53 	char num_pattern[] = "0123456789";
54 	int i = 0;
55 
56 	memset(value, 0, 10);
57 
58 	tmp = strstr(src, key);
59 	if (!tmp)
60 		return -1;
61 
62 	tmp += strlen(key);
63 
64 	c = tmp[0];
65 
66 	while (strchr(num_pattern, c) && i < 10) {
67 		value[i++] = c;
68 		c = tmp[i];
69 	}
70 
71 	return parser_read_uint32_val("", value, val);
72 }
73 
74 static int
parse_dvpt_ct_hex_str(const char * key,char * src,struct fips_val * val)75 parse_dvpt_ct_hex_str(const char *key, char *src, struct fips_val *val)
76 {
77 	int ret;
78 
79 	val->len = vec.pt.len;
80 
81 	ret = parse_uint8_known_len_hex_str(key, src, val);
82 	if (ret < 0)
83 		return ret;
84 
85 	src += strlen(key) + val->len * 2;
86 
87 	ret = parse_uint8_known_len_hex_str("", src, &vec.aead.digest);
88 	if (ret < 0) {
89 		rte_free(val->val);
90 		memset(val, 0, sizeof(*val));
91 		return ret;
92 	}
93 
94 	return 0;
95 }
96 
97 static int
parse_uint8_ccm_aad_str(const char * key,char * src,struct fips_val * val)98 parse_uint8_ccm_aad_str(const char *key, char *src, struct fips_val *val)
99 {
100 	uint32_t len = val->len, j;
101 
102 	src += strlen(key);
103 
104 	/* CCM aad requires 18 bytes padding before the real content */
105 	val->val = rte_zmalloc(NULL, len + 18, 0);
106 	if (!val->val)
107 		return -1;
108 
109 	for (j = 0; j < len; j++) {
110 		char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
111 
112 		if (parser_read_uint8_hex(&val->val[j + 18], byte) < 0) {
113 			rte_free(val->val);
114 			memset(val, 0, sizeof(*val));
115 			return -EINVAL;
116 		}
117 	}
118 
119 	return 0;
120 }
121 
122 struct fips_test_callback ccm_vnt_vec[] = {
123 		{IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
124 		{AAD_STR, parse_uint8_ccm_aad_str, &vec.aead.aad},
125 		{PT_STR, parse_uint8_known_len_hex_str, &vec.pt},
126 		{NULL, NULL, NULL} /**< end pointer */
127 };
128 
129 struct fips_test_callback ccm_vnt_interim_vec[] = {
130 		{ALEN_PREFIX, parser_read_uint32_val, &vec.aead.aad},
131 		{PLEN_PREFIX, parser_read_uint32_val, &vec.pt},
132 		{DIGESTL_PREFIX, parser_read_uint32_val, &vec.aead.digest},
133 		{IVLEN_PREFIX, parser_read_uint32_val, &vec.iv},
134 		{KEY_STR, parse_uint8_hex_str, &vec.aead.key},
135 		{NULL, NULL, NULL} /**< end pointer */
136 };
137 
138 struct fips_test_callback ccm_vtt_vec[] = {
139 		{AAD_STR, parse_uint8_ccm_aad_str, &vec.aead.aad},
140 		{PT_STR, parse_uint8_known_len_hex_str, &vec.pt},
141 		{NULL, NULL, NULL} /**< end pointer */
142 };
143 
144 struct fips_test_callback ccm_vtt_interim_vec[] = {
145 		{ALEN_PREFIX, parser_read_uint32_val, &vec.aead.aad},
146 		{PLEN_PREFIX, parser_read_uint32_val, &vec.pt},
147 		{IVLEN_PREFIX, parser_read_uint32_val, &vec.iv},
148 		{DIGESTL_PREFIX, parser_read_uint32_val, &vec.aead.digest},
149 		{KEY_STR, parse_uint8_hex_str, &vec.aead.key},
150 		{IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
151 		{NULL, NULL, NULL} /**< end pointer */
152 };
153 
154 struct fips_test_callback ccm_vadt_vec[] = {
155 		{AAD_STR, parse_uint8_ccm_aad_str, &vec.aead.aad},
156 		{PT_STR, parse_uint8_known_len_hex_str, &vec.pt},
157 		{NULL, NULL, NULL} /**< end pointer */
158 };
159 
160 struct fips_test_callback ccm_vadt_interim_vec[] = {
161 		{PLEN_PREFIX, parser_read_uint32_val, &vec.pt},
162 		{IVLEN_PREFIX, parser_read_uint32_val, &vec.iv},
163 		{ALEN_PREFIX, parser_read_uint32_val, &vec.aead.aad},
164 		{DIGESTL_PREFIX, parser_read_uint32_val, &vec.aead.digest},
165 		{KEY_STR, parse_uint8_hex_str, &vec.aead.key},
166 		{IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
167 		{NULL, NULL, NULL} /**< end pointer */
168 };
169 
170 struct fips_test_callback ccm_vpt_vec[] = {
171 		{AAD_STR, parse_uint8_ccm_aad_str, &vec.aead.aad},
172 		{PT_STR, parse_uint8_known_len_hex_str, &vec.pt},
173 		{NULL, NULL, NULL} /**< end pointer */
174 };
175 
176 struct fips_test_callback ccm_vpt_interim_vec[] = {
177 		{ALEN_PREFIX, parser_read_uint32_val, &vec.aead.aad},
178 		{IVLEN_PREFIX, parser_read_uint32_val, &vec.iv},
179 		{DIGESTL_PREFIX, parser_read_uint32_val, &vec.aead.digest},
180 		{PLEN_PREFIX, parser_read_uint32_val, &vec.pt},
181 		{KEY_STR, parse_uint8_hex_str, &vec.aead.key},
182 		{IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
183 		{NULL, NULL, NULL} /**< end pointer */
184 };
185 
186 struct fips_test_callback ccm_dvpt_vec[] = {
187 		{IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
188 		{AAD_STR, parse_uint8_ccm_aad_str, &vec.aead.aad},
189 		{CT_STR, parse_dvpt_ct_hex_str, &vec.ct},
190 		{NULL, NULL, NULL} /**< end pointer */
191 };
192 
193 struct fips_test_callback ccm_dvpt_interim_vec[] = {
194 		{ALEN_PREFIX, parser_dvpt_interim, &vec.aead.aad},
195 		{PLEN_PREFIX, parser_dvpt_interim, &vec.pt},
196 		{IVLEN_PREFIX, parser_dvpt_interim, &vec.iv},
197 		{DIGESTL_PREFIX, parser_dvpt_interim, &vec.aead.digest},
198 		{KEY_STR, parse_uint8_hex_str, &vec.aead.key},
199 		{NULL, NULL, NULL} /**< end pointer */
200 };
201 
202 struct ccm_test_types {
203 	const char *str;
204 	uint32_t type;
205 	const struct fips_test_callback *cb;
206 	const struct fips_test_callback *cb_interim;
207 	enum fips_test_op op;
208 } ctt[] = {
209 		{DVPT_STR, CCM_DVPT, ccm_dvpt_vec, ccm_dvpt_interim_vec,
210 			FIPS_TEST_DEC_AUTH_VERIF},
211 		{VPT_STR, CCM_VPT, ccm_vpt_vec, ccm_vpt_interim_vec,
212 			FIPS_TEST_ENC_AUTH_GEN},
213 		{VADT_STR, CCM_VADT, ccm_vadt_vec, ccm_vadt_interim_vec,
214 			FIPS_TEST_ENC_AUTH_GEN},
215 		{VNT_STR, CCM_VNT, ccm_vnt_vec, ccm_vnt_interim_vec,
216 			FIPS_TEST_ENC_AUTH_GEN},
217 		{VTT_STR, CCM_VTT, ccm_vtt_vec, ccm_vtt_interim_vec,
218 			FIPS_TEST_ENC_AUTH_GEN},
219 };
220 
221 #ifdef USE_JANSSON
222 static int
parser_read_ccm_direction_str(__rte_unused const char * key,char * src,__rte_unused struct fips_val * val)223 parser_read_ccm_direction_str(__rte_unused const char *key, char *src,
224 	__rte_unused struct fips_val *val)
225 {
226 	if (strcmp(src, "encrypt") == 0)
227 		info.op = FIPS_TEST_ENC_AUTH_GEN;
228 	else if (strcmp(src, "decrypt") == 0)
229 		info.op = FIPS_TEST_DEC_AUTH_VERIF;
230 
231 	return 0;
232 }
233 
234 static int
parser_read_ccm_aad_str(const char * key,char * src,struct fips_val * val)235 parser_read_ccm_aad_str(const char *key, char *src, struct fips_val *val)
236 {
237 	struct fips_val tmp_val = {0};
238 	uint32_t len = val->len;
239 
240 	/* CCM aad requires 18 bytes padding before the real content */
241 	val->val = rte_zmalloc(NULL, len + 18, 0);
242 	if (!val->val)
243 		return -1;
244 
245 	if (parse_uint8_hex_str(key, src, &tmp_val) < 0)
246 		return -1;
247 
248 	memcpy(val->val + 18, tmp_val.val, val->len);
249 	rte_free(tmp_val.val);
250 
251 	return 0;
252 }
253 
254 static int
parse_read_ccm_ct_str(const char * key,char * src,struct fips_val * val)255 parse_read_ccm_ct_str(const char *key, char *src, struct fips_val *val)
256 {
257 	int ret;
258 
259 	val->len = vec.pt.len;
260 
261 	ret = parse_uint8_known_len_hex_str(key, src, val);
262 	if (ret < 0)
263 		return ret;
264 
265 	src += val->len * 2;
266 
267 	ret = parse_uint8_known_len_hex_str("", src, &vec.aead.digest);
268 	if (ret < 0) {
269 		rte_free(val->val);
270 		memset(val, 0, sizeof(*val));
271 		return ret;
272 	}
273 
274 	return 0;
275 }
276 
277 struct fips_test_callback ccm_tests_interim_json_vectors[] = {
278 	{DIR_JSON_STR, parser_read_ccm_direction_str, NULL},
279 	{IVLEN_JSON_STR, parser_read_uint32_bit_val, &vec.iv},
280 	{PTLEN_JSON_STR, parser_read_uint32_bit_val, &vec.pt},
281 	{AADLEN_JSON_STR, parser_read_uint32_bit_val, &vec.aead.aad},
282 	{TAGLEN_JSON_STR, parser_read_uint32_bit_val, &vec.aead.digest},
283 	{KEYLEN_JSON_STR, parser_read_uint32_bit_val, &vec.aead.key},
284 	{NULL, NULL, NULL} /**< end pointer */
285 };
286 
287 struct fips_test_callback ccm_tests_json_vectors[] = {
288 	{PT_JSON_STR, parse_uint8_known_len_hex_str, &vec.pt},
289 	{CT_JSON_STR, parse_read_ccm_ct_str, &vec.ct},
290 	{KEY_JSON_STR, parse_uint8_known_len_hex_str, &vec.aead.key},
291 	{IV_JSON_STR, parse_uint8_known_len_hex_str, &vec.iv},
292 	{AAD_JSON_STR, parser_read_ccm_aad_str, &vec.aead.aad},
293 	{NULL, NULL, NULL} /**< end pointer */
294 };
295 
296 static int
parse_test_ccm_json_writeback(struct fips_val * val)297 parse_test_ccm_json_writeback(struct fips_val *val)
298 {
299 	struct fips_val tmp_val;
300 	json_t *tcId;
301 
302 	tcId = json_object_get(json_info.json_test_case, "tcId");
303 	json_info.json_write_case = json_object();
304 	json_object_set(json_info.json_write_case, "tcId", tcId);
305 
306 	if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
307 		json_t *ct;
308 
309 		info.one_line_text[0] = '\0';
310 		writeback_hex_str("", info.one_line_text, val);
311 		ct = json_string(info.one_line_text);
312 		json_object_set_new(json_info.json_write_case, CT_JSON_STR, ct);
313 	} else {
314 		if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
315 			tmp_val.val = val->val;
316 			tmp_val.len = vec.pt.len;
317 
318 			info.one_line_text[0] = '\0';
319 			writeback_hex_str("", info.one_line_text, &tmp_val);
320 			json_object_set_new(json_info.json_write_case, PT_JSON_STR,
321 				json_string(info.one_line_text));
322 		}  else {
323 			json_object_set_new(json_info.json_write_case, "testPassed",
324 				json_false());
325 		}
326 	}
327 
328 	return 0;
329 }
330 
331 int
parse_test_ccm_json_init(void)332 parse_test_ccm_json_init(void)
333 {
334 	info.interim_callbacks = ccm_tests_interim_json_vectors;
335 	info.parse_writeback = parse_test_ccm_json_writeback;
336 	info.callbacks = ccm_tests_json_vectors;
337 	return 0;
338 }
339 #endif /* USE_JANSSON */
340 
341 static int
parse_test_ccm_writeback(struct fips_val * val)342 parse_test_ccm_writeback(struct fips_val *val)
343 {
344 	struct fips_val tmp_val;
345 
346 	switch (info.interim_info.ccm_data.test_type) {
347 	case CCM_DVPT:
348 		fprintf(info.fp_wr, "%s", POS_NEG_STR);
349 		if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
350 			fprintf(info.fp_wr, "%s\n", POS_KEYWORD);
351 			fprintf(info.fp_wr, "%s", PT_STR);
352 
353 			tmp_val.val = val->val;
354 			tmp_val.len = vec.pt.len;
355 
356 			if (tmp_val.len == 0)
357 				fprintf(info.fp_wr, "00\n");
358 			else
359 				parse_write_hex_str(&tmp_val);
360 		} else
361 			fprintf(info.fp_wr, "%s\n", NEG_KEYWORD);
362 
363 		break;
364 
365 	case CCM_VADT:
366 	case CCM_VNT:
367 	case CCM_VPT:
368 	case CCM_VTT:
369 		fprintf(info.fp_wr, "%s", CT_STR);
370 
371 		parse_write_hex_str(val);
372 
373 		break;
374 
375 	}
376 
377 	return 0;
378 }
379 
380 int
parse_test_ccm_init(void)381 parse_test_ccm_init(void)
382 {
383 
384 	uint32_t i;
385 
386 	for (i = 0; i < info.nb_vec_lines; i++) {
387 		char *line = info.vec[i];
388 		uint32_t j;
389 
390 		for (j = 0; j < RTE_DIM(ctt); j++)
391 			if (strstr(line, ctt[j].str)) {
392 				info.interim_info.ccm_data.test_type =
393 						ctt[j].type;
394 				info.callbacks = ctt[j].cb;
395 				info.interim_callbacks = ctt[j].cb_interim;
396 				info.op = ctt[j].op;
397 				break;
398 		}
399 	}
400 
401 	info.parse_writeback = parse_test_ccm_writeback;
402 
403 	return 0;
404 }
405