1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
9 #include <rte_cryptodev.h>
11 #include "fips_validation.h"
13 #define ALGO_PREFIX "[L="
14 #define KEYLEN_STR "Klen = "
15 #define TAGLEN_STR "Tlen = "
17 #define COUNT_STR "Count = "
18 #define KEY_STR "Key = "
19 #define PT_STR "Msg = "
20 #define TAG_STR "Mac = "
22 #define ALGO_JSON_STR "algorithm"
24 #define KEYLEN_JSON_STR "keyLen"
25 #define TAGLEN_JSON_STR "macLen"
27 #define KEY_JSON_STR "key"
28 #define PT_JSON_STR "msg"
29 #define TAG_JSON_STR "mac"
31 struct hash_size_conversion {
33 enum rte_crypto_auth_algorithm algo;
35 {"20", RTE_CRYPTO_AUTH_SHA1_HMAC},
36 {"28", RTE_CRYPTO_AUTH_SHA224_HMAC},
37 {"32", RTE_CRYPTO_AUTH_SHA256_HMAC},
38 {"48", RTE_CRYPTO_AUTH_SHA384_HMAC},
39 {"64", RTE_CRYPTO_AUTH_SHA512_HMAC},
43 parse_interim_algo(__rte_unused const char *key,
45 __rte_unused struct fips_val *val)
50 for (i = 0; i < RTE_DIM(hsc); i++) {
51 if (strstr(text, hsc[i].str)) {
52 info.interim_info.hmac_data.algo = hsc[i].algo;
57 if (i == RTE_DIM(hsc))
63 struct fips_test_callback hmac_tests_vectors[] = {
64 {KEYLEN_STR, parser_read_uint32_val, &vec.cipher_auth.key},
65 {TAGLEN_STR, parser_read_uint32_val, &vec.cipher_auth.digest},
66 {KEY_STR, parse_uint8_hex_str, &vec.cipher_auth.key},
67 {PT_STR, parse_uint8_hex_str, &vec.pt},
68 {TAG_STR, parse_uint8_hex_str, &vec.cipher_auth.digest},
69 {NULL, NULL, NULL} /**< end pointer */
72 struct fips_test_callback hmac_tests_interim_vectors[] = {
73 {ALGO_PREFIX, parse_interim_algo, NULL},
74 {NULL, NULL, NULL} /**< end pointer */
77 #ifdef RTE_HAS_JANSSON
78 struct hash_size_conversion json_algorithms[] = {
79 {"HMAC-SHA-1", RTE_CRYPTO_AUTH_SHA1_HMAC},
80 {"HMAC-SHA2-224", RTE_CRYPTO_AUTH_SHA224_HMAC},
81 {"HMAC-SHA2-256", RTE_CRYPTO_AUTH_SHA256_HMAC},
82 {"HMAC-SHA2-384", RTE_CRYPTO_AUTH_SHA384_HMAC},
83 {"HMAC-SHA2-512", RTE_CRYPTO_AUTH_SHA512_HMAC},
86 struct fips_test_callback hmac_tests_json_vectors[] = {
87 {KEY_JSON_STR, parse_uint8_hex_str, &vec.cipher_auth.key},
88 {PT_JSON_STR, parse_uint8_hex_str, &vec.pt},
89 {TAG_JSON_STR, parse_uint8_hex_str, &vec.cipher_auth.digest},
90 {NULL, NULL, NULL} /**< end pointer */
93 struct fips_test_callback hmac_tests_interim_json_vectors[] = {
94 {KEYLEN_JSON_STR, parser_read_uint32_val, &vec.cipher_auth.key},
95 {TAGLEN_JSON_STR, parser_read_uint32_bit_val, &vec.cipher_auth.digest},
96 {NULL, NULL, NULL} /**< end pointer */
98 #endif /* RTE_HAS_JANSSON */
101 parse_test_hmac_writeback(struct fips_val *val)
103 struct fips_val val_local;
105 fprintf(info.fp_wr, "%s", TAG_STR);
107 val_local.val = val->val + vec.pt.len;
108 val_local.len = vec.cipher_auth.digest.len;
110 parse_write_hex_str(&val_local);
115 rsp_test_hmac_check(struct fips_val *val)
117 if (memcmp(val->val + vec.pt.len, vec.cipher_auth.digest.val,
118 vec.cipher_auth.digest.len) == 0)
119 fprintf(info.fp_wr, "Success\n");
121 fprintf(info.fp_wr, "Failed\n");
127 parse_test_hmac_init(void)
129 info.op = FIPS_TEST_ENC_AUTH_GEN;
130 info.parse_writeback = parse_test_hmac_writeback;
131 info.callbacks = hmac_tests_vectors;
132 info.interim_callbacks = hmac_tests_interim_vectors;
133 info.writeback_callbacks = NULL;
134 info.kat_check = rsp_test_hmac_check;
139 #ifdef RTE_HAS_JANSSON
141 parse_test_hmac_json_writeback(struct fips_val *val)
143 struct fips_val val_local;
146 tcId = json_object_get(json_info.json_test_case, "tcId");
148 json_info.json_write_case = json_object();
149 json_object_set(json_info.json_write_case, "tcId", tcId);
152 val_local.val = val->val + vec.pt.len;
153 val_local.len = vec.cipher_auth.digest.len;
155 writeback_hex_str("", info.one_line_text, &val_local);
157 mac = json_string(info.one_line_text);
158 json_object_set_new(json_info.json_write_case, TAG_JSON_STR, mac);
164 parse_test_hmac_json_algorithm(void)
166 json_t *algorithm_object;
167 const char *algorithm_str;
170 algorithm_object = json_object_get(json_info.json_vector_set, "algorithm");
171 algorithm_str = json_string_value(algorithm_object);
173 for (i = 0; i < RTE_DIM(json_algorithms); i++) {
174 if (strstr(algorithm_str, json_algorithms[i].str)) {
175 info.interim_info.hmac_data.algo = json_algorithms[i].algo;
184 parse_test_hmac_json_init(void)
186 info.op = FIPS_TEST_ENC_AUTH_GEN;
187 info.parse_writeback = parse_test_hmac_json_writeback;
188 info.callbacks = hmac_tests_json_vectors;
189 info.writeback_callbacks = NULL;
190 info.kat_check = rsp_test_hmac_check;
191 info.interim_callbacks = hmac_tests_interim_json_vectors;
193 if (parse_test_hmac_json_algorithm() < 0)
198 #endif /* RTE_HAS_JANSSON */