1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
9 #include <rte_cryptodev.h>
11 #include "fips_validation.h"
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"
20 #define ALGO_JSON_STR "algorithm"
21 #define TESTTYPE_JSON_STR "testType"
23 #define PT_JSON_STR "msg"
25 struct plain_hash_size_conversion {
27 enum rte_crypto_auth_algorithm algo;
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},
37 parse_interim_algo(__rte_unused const char *key,
39 __rte_unused struct fips_val *val)
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);
52 if (i == RTE_DIM(phsc))
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 */
65 struct fips_test_callback sha_tests_interim_vectors[] = {
66 {ALGO_PREFIX, parse_interim_algo, NULL},
67 {NULL, NULL, NULL} /**< end pointer */
74 } sha_test_types[] = {
79 static struct plain_hash_algorithms {
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},
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 */
94 #endif /* USE_JANSSON */
97 parse_test_sha_writeback(struct fips_val *val) // !
99 struct fips_val val_local;
101 fprintf(info.fp_wr, "%s", MD_STR);
103 val_local.val = val->val + vec.pt.len;
104 val_local.len = vec.cipher_auth.digest.len;
106 parse_write_hex_str(&val_local);
111 rsp_test_sha_check(struct fips_val *val)
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");
117 fprintf(info.fp_wr, "Failed\n");
123 parse_test_sha_init(void)
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;
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;
145 parse_test_sha_json_writeback(struct fips_val *val)
147 struct fips_val val_local;
150 tcId = json_object_get(json_info.json_test_case, "tcId");
152 json_info.json_write_case = json_object();
153 json_object_set_new(json_info.json_write_case, "tcId", tcId);
155 val_local.val = val->val + vec.pt.len;
156 val_local.len = vec.cipher_auth.digest.len;
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);
166 parse_test_sha_mct_json_writeback(struct fips_val *val)
168 json_t *tcId, *msg, *md, *resArr, *res;
169 struct fips_val val_local;
171 tcId = json_object_get(json_info.json_test_case, "tcId");
172 if (json_info.json_write_case) {
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",
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());
188 resArr = json_object_get(json_info.json_write_case, "resultsArray");
189 if (!json_is_array(resArr))
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);
198 val_local.val = val[0].val + vec.pt.len;
199 val_local.len = vec.cipher_auth.digest.len;
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);
205 json_array_append_new(resArr, res);
210 parse_test_sha_json_algorithm(void)
212 json_t *algorithm_object;
213 const char *algorithm_str;
216 algorithm_object = json_object_get(json_info.json_vector_set, "algorithm");
217 algorithm_str = json_string_value(algorithm_object);
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;
226 if (i == RTE_DIM(json_algorithms))
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)
241 if (i == RTE_DIM(phsc)) {
242 free(vec.cipher_auth.digest.val);
243 vec.cipher_auth.digest.val = NULL;
251 parse_test_sha_json_test_type(void)
254 const char *type_str;
257 type_object = json_object_get(json_info.json_test_group, TESTTYPE_JSON_STR);
258 type_str = json_string_value(type_object);
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;
267 if (i == RTE_DIM(sha_test_types))
270 switch (info.interim_info.sha_data.test_type) {
272 info.parse_writeback = parse_test_sha_mct_json_writeback;
275 info.parse_writeback = parse_test_sha_json_writeback;
278 info.parse_writeback = NULL;
281 if (!info.parse_writeback)
288 parse_test_sha_json_init(void)
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;
297 if (parse_test_sha_json_algorithm() < 0)
300 if (parse_test_sha_json_test_type() < 0)
305 #endif /* USE_JANSSON */