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