xref: /dpdk/examples/fips_validation/fips_validation_cmac.c (revision 8d70a19417ad70accc3e138190875ff8f2baae8c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4 
5 #include <string.h>
6 #include <time.h>
7 #include <stdio.h>
8 #include <rte_string_fns.h>
9 
10 #include <rte_cryptodev.h>
11 
12 #include "fips_validation.h"
13 
14 #define NEW_LINE_STR	"#"
15 #define OP_STR		"CMAC"
16 
17 #define ALGO_STR	"Alg = "
18 #define MODE_STR	"Mode = "
19 
20 #define COUNT_STR	"Count = "
21 #define KLEN_STR	"Klen = "
22 #define PTLEN_STR	"Mlen = "
23 #define TAGLEN_STR	"Tlen = "
24 #define KEY_STR		"Key = "
25 #define PT_STR		"Msg = "
26 #define TAG_STR		"Mac = "
27 
28 #define GEN_STR		"Generate"
29 #define VERIF_STR	"Verify"
30 
31 #define POS_NEG_STR	"Result = "
32 #define PASS_STR	"P"
33 #define FAIL_STR	"F"
34 
35 #define KLEN_JSON_STR		"keyLen"
36 #define PTLEN_JSON_STR		"msgLen"
37 #define TAGLEN_JSON_STR		"macLen"
38 #define KEY_JSON_STR		"key"
39 #define PT_JSON_STR			"message"
40 #define TAG_JSON_STR		"mac"
41 #define DIRECTION_JSON_STR	"direction"
42 #define POS_NEG_JSON_STR	"testPassed"
43 
44 #define GEN_JSON_STR	"gen"
45 #define VERIF_JSON_STR	"ver"
46 
47 struct hash_algo_conversion {
48 	const char *str;
49 	enum fips_test_algorithms algo;
50 } cmac_algo[] = {
51 		{"AES", FIPS_TEST_ALGO_AES_CMAC},
52 };
53 
54 #ifdef USE_JANSSON
55 static int
parser_read_cmac_direction_str(__rte_unused const char * key,char * src,__rte_unused struct fips_val * val)56 parser_read_cmac_direction_str(__rte_unused const char *key, char *src,
57 		__rte_unused struct fips_val *val)
58 {
59 	if (strcmp(src, "gen") == 0)
60 		info.op = FIPS_TEST_ENC_AUTH_GEN;
61 	else if (strcmp(src, "ver") == 0)
62 		info.op = FIPS_TEST_DEC_AUTH_VERIF;
63 
64 	return 0;
65 }
66 
67 struct fips_test_callback cmac_tests_interim_json_vectors[] = {
68 		{KLEN_JSON_STR, parser_read_uint32_bit_val, &vec.cipher_auth.key},
69 		{PTLEN_JSON_STR, parser_read_uint32_bit_val, &vec.pt},
70 		{TAGLEN_JSON_STR, parser_read_uint32_bit_val, &vec.cipher_auth.digest},
71 		{DIRECTION_JSON_STR, parser_read_cmac_direction_str, NULL},
72 		{NULL, NULL, NULL} /**< end pointer */
73 };
74 
75 struct fips_test_callback cmac_tests_json_vectors[] = {
76 		{KEY_JSON_STR, parse_uint8_hex_str, &vec.cipher_auth.key},
77 		{PT_JSON_STR, parse_uint8_known_len_hex_str, &vec.pt},
78 		{TAG_JSON_STR, parse_uint8_known_len_hex_str,
79 				&vec.cipher_auth.digest},
80 		{NULL, NULL, NULL} /**< end pointer */
81 };
82 
83 static int
parse_test_cmac_json_writeback(struct fips_val * val)84 parse_test_cmac_json_writeback(struct fips_val *val)
85 {
86 	json_info.json_write_case = json_object();
87 	json_object_set(json_info.json_write_case, "tcId",
88 		json_object_get(json_info.json_test_case, "tcId"));
89 
90 	if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
91 		struct fips_val tmp_val = {val->val + vec.pt.len,
92 				vec.cipher_auth.digest.len};
93 
94 		writeback_hex_str("", info.one_line_text, &tmp_val);
95 		json_object_set_new(json_info.json_write_case, TAG_JSON_STR,
96 			json_string(info.one_line_text));
97 	} else {
98 		if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS)
99 			json_object_set_new(json_info.json_write_case, POS_NEG_JSON_STR,
100 				json_boolean(true));
101 		else if (vec.status == RTE_CRYPTO_OP_STATUS_AUTH_FAILED)
102 			json_object_set_new(json_info.json_write_case, POS_NEG_JSON_STR,
103 				json_boolean(false));
104 	}
105 
106 	return 0;
107 }
108 
109 int
parse_test_cmac_json_init(void)110 parse_test_cmac_json_init(void)
111 {
112 	info.algo = FIPS_TEST_ALGO_AES_CMAC;
113 
114 	info.parse_writeback = parse_test_cmac_json_writeback;
115 	info.callbacks = cmac_tests_json_vectors;
116 	info.interim_callbacks = cmac_tests_interim_json_vectors;
117 
118 	return 0;
119 }
120 #endif /* USE_JANSSON */
121 
122 static int
parse_test_cmac_writeback(struct fips_val * val)123 parse_test_cmac_writeback(struct fips_val *val)
124 {
125 	if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
126 		struct fips_val tmp_val = {val->val + vec.pt.len,
127 				vec.cipher_auth.digest.len};
128 
129 		fprintf(info.fp_wr, "%s", TAG_STR);
130 		parse_write_hex_str(&tmp_val);
131 	} else {
132 		fprintf(info.fp_wr, "%s", POS_NEG_STR);
133 
134 		if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS)
135 			fprintf(info.fp_wr, "%s\n", PASS_STR);
136 		else if (vec.status == RTE_CRYPTO_OP_STATUS_AUTH_FAILED)
137 			fprintf(info.fp_wr, "%s\n", FAIL_STR);
138 		else
139 			fprintf(info.fp_wr, "Error\n");
140 	}
141 
142 	return 0;
143 }
144 
145 struct fips_test_callback cmac_tests_vectors[] = {
146 		{KLEN_STR, parser_read_uint32_val, &vec.cipher_auth.key},
147 		{PTLEN_STR, parser_read_uint32_val, &vec.pt},
148 		{TAGLEN_STR, parser_read_uint32_val, &vec.cipher_auth.digest},
149 		{KEY_STR, parse_uint8_hex_str, &vec.cipher_auth.key},
150 		{PT_STR, parse_uint8_known_len_hex_str, &vec.pt},
151 		{TAG_STR, parse_uint8_known_len_hex_str,
152 				&vec.cipher_auth.digest},
153 		{NULL, NULL, NULL} /**< end pointer */
154 };
155 
156 int
parse_test_cmac_init(void)157 parse_test_cmac_init(void)
158 {
159 	char *tmp;
160 	uint32_t i, j;
161 
162 	for (i = 0; i < info.nb_vec_lines; i++) {
163 		char *line = info.vec[i];
164 
165 		tmp = strstr(line, ALGO_STR);
166 		if (!tmp)
167 			continue;
168 
169 		for (j = 0; j < RTE_DIM(cmac_algo); j++) {
170 			if (!strstr(line, cmac_algo[j].str))
171 				continue;
172 
173 			info.algo = cmac_algo[j].algo;
174 			break;
175 		}
176 
177 		if (j == RTE_DIM(cmac_algo))
178 			return -EINVAL;
179 
180 		tmp = strstr(line, MODE_STR);
181 		if (!tmp)
182 			return -1;
183 
184 		if (strstr(tmp, GEN_STR))
185 			info.op = FIPS_TEST_ENC_AUTH_GEN;
186 		else if (strstr(tmp, VERIF_STR))
187 			info.op = FIPS_TEST_DEC_AUTH_VERIF;
188 		else
189 			return -EINVAL;
190 	}
191 
192 	info.parse_writeback = parse_test_cmac_writeback;
193 	info.callbacks = cmac_tests_vectors;
194 
195 	return 0;
196 }
197