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