xref: /dpdk/examples/fips_validation/fips_validation_sha.c (revision a39f5e14560df310e7af3c135a11bb6f51eb92e6)
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, *msg, *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 	writeback_hex_str("", info.one_line_text, &val[1]);
212 	msg = json_string(info.one_line_text);
213 	json_object_set_new(res, "msg", msg);
214 
215 	val_local.val = val[0].val + vec.pt.len;
216 	val_local.len = vec.cipher_auth.digest.len;
217 
218 	writeback_hex_str("", info.one_line_text, &val_local);
219 	md = json_string(info.one_line_text);
220 	json_object_set_new(res, "md", md);
221 
222 	json_array_append_new(resArr, res);
223 	return 0;
224 }
225 
226 int
227 parse_test_sha_json_algorithm(void)
228 {
229 	json_t *algorithm_object;
230 	const char *algorithm_str;
231 	uint32_t i;
232 	int sz;
233 
234 	algorithm_object = json_object_get(json_info.json_vector_set, "algorithm");
235 	algorithm_str = json_string_value(algorithm_object);
236 
237 	for (i = 0; i < RTE_DIM(json_algorithms); i++) {
238 		if (strstr(algorithm_str, json_algorithms[i].str)) {
239 			info.interim_info.sha_data.algo = json_algorithms[i].algo;
240 			break;
241 		}
242 	}
243 
244 	if (i == RTE_DIM(json_algorithms))
245 		return -1;
246 
247 	sz = parse_test_sha_hash_size(info.interim_info.sha_data.algo);
248 	if (sz < 0)
249 		return -1;
250 
251 	free(vec.cipher_auth.digest.val);
252 	vec.cipher_auth.digest.len = sz;
253 	vec.cipher_auth.digest.val = calloc(1, sz);
254 	if (vec.cipher_auth.digest.val == NULL)
255 		return -1;
256 
257 	return 0;
258 }
259 
260 int
261 parse_test_sha_json_test_type(void)
262 {
263 	json_t *type_object;
264 	const char *type_str;
265 	uint32_t i;
266 
267 	type_object = json_object_get(json_info.json_test_group, TESTTYPE_JSON_STR);
268 	type_str = json_string_value(type_object);
269 
270 	for (i = 0; i < RTE_DIM(sha_test_types); i++)
271 		if (strstr(type_str, sha_test_types[i].desc)) {
272 			info.interim_info.sha_data.test_type =
273 				sha_test_types[i].type;
274 			break;
275 		}
276 
277 	if (i == RTE_DIM(sha_test_types))
278 		return -1;
279 
280 	switch (info.interim_info.sha_data.test_type) {
281 	case SHA_MCT:
282 		info.parse_writeback = parse_test_sha_mct_json_writeback;
283 		break;
284 	case SHA_AFT:
285 		info.parse_writeback = parse_test_sha_json_writeback;
286 		break;
287 	default:
288 		info.parse_writeback = NULL;
289 	}
290 
291 	if (!info.parse_writeback)
292 		return -1;
293 
294 	return 0;
295 }
296 
297 int
298 parse_test_sha_json_init(void)
299 {
300 	info.op = FIPS_TEST_ENC_AUTH_GEN;
301 	info.parse_writeback = parse_test_sha_json_writeback;
302 	info.callbacks = sha_tests_json_vectors;
303 	info.writeback_callbacks = NULL;
304 	info.kat_check = rsp_test_sha_check;
305 	info.interim_callbacks = NULL;
306 
307 	if (parse_test_sha_json_algorithm() < 0)
308 		return -1;
309 
310 	if (parse_test_sha_json_test_type() < 0)
311 		return -1;
312 
313 	return 0;
314 }
315 #endif /* USE_JANSSON */
316