xref: /dpdk/examples/fips_validation/fips_validation_sha.c (revision 8c76e2f6937730782baa210bf456bd19da2a9600)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4 
5 #include <string.h>
6 #include <time.h>
7 #include <stdio.h>
8 
9 #include <rte_cryptodev.h>
10 
11 #include "fips_validation.h"
12 
13 #define ALGO_PREFIX	"[L = "
14 #define MSGLEN_STR	"Len = "
15 #define MSG_STR		"Msg = "
16 #define MD_STR		"MD = "
17 #define SEED_STR	"Seed = "
18 #define MCT_STR		"Monte"
19 
20 #define ALGO_JSON_STR	"algorithm"
21 #define TESTTYPE_JSON_STR	"testType"
22 
23 #define PT_JSON_STR		"msg"
24 
25 struct plain_hash_size_conversion {
26 	const char *str;
27 	enum rte_crypto_auth_algorithm algo;
28 } phsc[] = {
29 		{"20", RTE_CRYPTO_AUTH_SHA1},
30 		{"28", RTE_CRYPTO_AUTH_SHA224},
31 		{"32", RTE_CRYPTO_AUTH_SHA256},
32 		{"48", RTE_CRYPTO_AUTH_SHA384},
33 		{"64", RTE_CRYPTO_AUTH_SHA512},
34 };
35 
36 static int
37 parse_interim_algo(__rte_unused const char *key,
38 		char *text,
39 		__rte_unused struct fips_val *val)
40 {
41 	uint32_t i;
42 
43 	for (i = 0; i < RTE_DIM(phsc); i++) {
44 		if (strstr(text, phsc[i].str)) {
45 			info.interim_info.sha_data.algo = phsc[i].algo;
46 			parser_read_uint32_val(ALGO_PREFIX,
47 				text, &vec.cipher_auth.digest);
48 			break;
49 		}
50 	}
51 
52 	if (i == RTE_DIM(phsc))
53 		return -1;
54 
55 	return 0;
56 }
57 
58 struct fips_test_callback sha_tests_vectors[] = {
59 		{MSGLEN_STR, parser_read_uint32_bit_val, &vec.pt},
60 		{MSG_STR, parse_uint8_known_len_hex_str, &vec.pt},
61 		{SEED_STR, parse_uint8_hex_str, &vec.cipher_auth.digest},
62 		{NULL, NULL, NULL} /**< end pointer */
63 };
64 
65 struct fips_test_callback sha_tests_interim_vectors[] = {
66 		{ALGO_PREFIX, parse_interim_algo, NULL},
67 		{NULL, NULL, NULL} /**< end pointer */
68 };
69 
70 #ifdef USE_JANSSON
71 static struct {
72 	uint32_t type;
73 	const char *desc;
74 } sha_test_types[] = {
75 		{SHA_MCT, "MCT"},
76 		{SHA_AFT, "AFT"},
77 };
78 
79 static struct plain_hash_algorithms {
80 	const char *str;
81 	enum rte_crypto_auth_algorithm algo;
82 } json_algorithms[] = {
83 		{"SHA-1", RTE_CRYPTO_AUTH_SHA1},
84 		{"SHA2-224", RTE_CRYPTO_AUTH_SHA224},
85 		{"SHA2-256", RTE_CRYPTO_AUTH_SHA256},
86 		{"SHA2-384", RTE_CRYPTO_AUTH_SHA384},
87 		{"SHA2-512", RTE_CRYPTO_AUTH_SHA512},
88 };
89 
90 struct fips_test_callback sha_tests_json_vectors[] = {
91 		{PT_JSON_STR, parse_uint8_hex_str, &vec.pt},
92 		{NULL, NULL, NULL} /**< end pointer */
93 };
94 #endif /* USE_JANSSON */
95 
96 static int
97 parse_test_sha_writeback(struct fips_val *val) // !
98 {
99 	struct fips_val val_local;
100 
101 	fprintf(info.fp_wr, "%s", MD_STR);
102 
103 	val_local.val = val->val + vec.pt.len;
104 	val_local.len = vec.cipher_auth.digest.len;
105 
106 	parse_write_hex_str(&val_local);
107 	return 0;
108 }
109 
110 static int
111 rsp_test_sha_check(struct fips_val *val)
112 {
113 	if (memcmp(val->val + vec.pt.len, vec.cipher_auth.digest.val,
114 			vec.cipher_auth.digest.len) == 0)
115 		fprintf(info.fp_wr, "Success\n");
116 	else
117 		fprintf(info.fp_wr, "Failed\n");
118 
119 	return 0;
120 }
121 
122 int
123 parse_test_sha_init(void)
124 {
125 	uint32_t i;
126 
127 	info.interim_info.sha_data.test_type = SHA_KAT;
128 	for (i = 0; i < info.nb_vec_lines; i++) {
129 		char *line = info.vec[i];
130 		if (strstr(line, MCT_STR))
131 			info.interim_info.sha_data.test_type = SHA_MCT;
132 	}
133 
134 	info.op = FIPS_TEST_ENC_AUTH_GEN;
135 	info.parse_writeback = parse_test_sha_writeback;
136 	info.callbacks = sha_tests_vectors;
137 	info.interim_callbacks = sha_tests_interim_vectors;
138 	info.writeback_callbacks = NULL;
139 	info.kat_check = rsp_test_sha_check;
140 	return 0;
141 }
142 
143 #ifdef USE_JANSSON
144 static int
145 parse_test_sha_json_writeback(struct fips_val *val)
146 {
147 	struct fips_val val_local;
148 	json_t *tcId, *md;
149 
150 	tcId = json_object_get(json_info.json_test_case, "tcId");
151 
152 	json_info.json_write_case = json_object();
153 	json_object_set_new(json_info.json_write_case, "tcId", tcId);
154 
155 	val_local.val = val->val + vec.pt.len;
156 	val_local.len = vec.cipher_auth.digest.len;
157 
158 	writeback_hex_str("", info.one_line_text, &val_local);
159 	md = json_string(info.one_line_text);
160 	json_object_set_new(json_info.json_write_case, "md", md);
161 
162 	return 0;
163 }
164 
165 static int
166 parse_test_sha_mct_json_writeback(struct fips_val *val)
167 {
168 	json_t *tcId, *msg, *md, *resArr, *res;
169 	struct fips_val val_local;
170 
171 	tcId = json_object_get(json_info.json_test_case, "tcId");
172 	if (json_info.json_write_case) {
173 		json_t *wcId;
174 
175 		wcId = json_object_get(json_info.json_write_case, "tcId");
176 		if (!json_equal(tcId, wcId)) {
177 			json_info.json_write_case = json_object();
178 			json_object_set_new(json_info.json_write_case, "tcId", tcId);
179 			json_object_set_new(json_info.json_write_case, "resultsArray",
180 								json_array());
181 		}
182 	} else {
183 		json_info.json_write_case = json_object();
184 		json_object_set_new(json_info.json_write_case, "tcId", tcId);
185 		json_object_set_new(json_info.json_write_case, "resultsArray", json_array());
186 	}
187 
188 	resArr = json_object_get(json_info.json_write_case, "resultsArray");
189 	if (!json_is_array(resArr))
190 		return -EINVAL;
191 
192 	res = json_object();
193 
194 	writeback_hex_str("", info.one_line_text, &val[1]);
195 	msg = json_string(info.one_line_text);
196 	json_object_set_new(res, "msg", msg);
197 
198 	val_local.val = val[0].val + vec.pt.len;
199 	val_local.len = vec.cipher_auth.digest.len;
200 
201 	writeback_hex_str("", info.one_line_text, &val_local);
202 	md = json_string(info.one_line_text);
203 	json_object_set_new(res, "md", md);
204 
205 	json_array_append_new(resArr, res);
206 	return 0;
207 }
208 
209 int
210 parse_test_sha_json_algorithm(void)
211 {
212 	json_t *algorithm_object;
213 	const char *algorithm_str;
214 	uint32_t i;
215 
216 	algorithm_object = json_object_get(json_info.json_vector_set, "algorithm");
217 	algorithm_str = json_string_value(algorithm_object);
218 
219 	for (i = 0; i < RTE_DIM(json_algorithms); i++) {
220 		if (strstr(algorithm_str, json_algorithms[i].str)) {
221 			info.interim_info.sha_data.algo = json_algorithms[i].algo;
222 			break;
223 		}
224 	}
225 
226 	if (i == RTE_DIM(json_algorithms))
227 		return -1;
228 
229 	for (i = 0; i < RTE_DIM(phsc); i++) {
230 		if (info.interim_info.sha_data.algo == phsc[i].algo) {
231 			vec.cipher_auth.digest.len = atoi(phsc[i].str);
232 			free(vec.cipher_auth.digest.val);
233 			vec.cipher_auth.digest.val = calloc(1, vec.cipher_auth.digest.len);
234 			if (vec.cipher_auth.digest.val == NULL)
235 				return -1;
236 
237 			break;
238 		}
239 	}
240 
241 	if (i == RTE_DIM(phsc)) {
242 		free(vec.cipher_auth.digest.val);
243 		vec.cipher_auth.digest.val = NULL;
244 		return -1;
245 	}
246 
247 	return 0;
248 }
249 
250 int
251 parse_test_sha_json_test_type(void)
252 {
253 	json_t *type_object;
254 	const char *type_str;
255 	uint32_t i;
256 
257 	type_object = json_object_get(json_info.json_test_group, TESTTYPE_JSON_STR);
258 	type_str = json_string_value(type_object);
259 
260 	for (i = 0; i < RTE_DIM(sha_test_types); i++)
261 		if (strstr(type_str, sha_test_types[i].desc)) {
262 			info.interim_info.aes_data.test_type =
263 				sha_test_types[i].type;
264 			break;
265 		}
266 
267 	if (i == RTE_DIM(sha_test_types))
268 		return -1;
269 
270 	switch (info.interim_info.sha_data.test_type) {
271 	case SHA_MCT:
272 		info.parse_writeback = parse_test_sha_mct_json_writeback;
273 		break;
274 	case SHA_AFT:
275 		info.parse_writeback = parse_test_sha_json_writeback;
276 		break;
277 	default:
278 		info.parse_writeback = NULL;
279 	}
280 
281 	if (!info.parse_writeback)
282 		return -1;
283 
284 	return 0;
285 }
286 
287 int
288 parse_test_sha_json_init(void)
289 {
290 	info.op = FIPS_TEST_ENC_AUTH_GEN;
291 	info.parse_writeback = parse_test_sha_json_writeback;
292 	info.callbacks = sha_tests_json_vectors;
293 	info.writeback_callbacks = NULL;
294 	info.kat_check = rsp_test_sha_check;
295 	info.interim_callbacks = NULL;
296 
297 	if (parse_test_sha_json_algorithm() < 0)
298 		return -1;
299 
300 	if (parse_test_sha_json_test_type() < 0)
301 		return -1;
302 
303 	return 0;
304 }
305 #endif /* USE_JANSSON */
306