1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2018 Intel Corporation 3 */ 4 5 #include <string.h> 6 #include <time.h> 7 #include <stdio.h> 8 9 #include <rte_cryptodev.h> 10 11 #include "fips_validation.h" 12 13 #define ALGO_PREFIX "[L=" 14 #define KEYLEN_STR "Klen = " 15 #define TAGLEN_STR "Tlen = " 16 17 #define COUNT_STR "Count = " 18 #define KEY_STR "Key = " 19 #define PT_STR "Msg = " 20 #define TAG_STR "Mac = " 21 22 #define ALGO_JSON_STR "algorithm" 23 24 #define KEYLEN_JSON_STR "keyLen" 25 #define TAGLEN_JSON_STR "macLen" 26 27 #define KEY_JSON_STR "key" 28 #define PT_JSON_STR "msg" 29 #define TAG_JSON_STR "mac" 30 31 struct hash_size_conversion { 32 const char *str; 33 enum rte_crypto_auth_algorithm algo; 34 } hsc[] = { 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}, 40 {"28", RTE_CRYPTO_AUTH_SHA3_224_HMAC}, 41 {"32", RTE_CRYPTO_AUTH_SHA3_256_HMAC}, 42 {"48", RTE_CRYPTO_AUTH_SHA3_384_HMAC}, 43 {"64", RTE_CRYPTO_AUTH_SHA3_512_HMAC}, 44 }; 45 46 static int 47 parse_interim_algo(__rte_unused const char *key, 48 char *text, 49 __rte_unused struct fips_val *val) 50 { 51 52 uint32_t i; 53 54 for (i = 0; i < RTE_DIM(hsc); i++) { 55 if (strstr(text, hsc[i].str)) { 56 info.interim_info.hmac_data.algo = hsc[i].algo; 57 break; 58 } 59 } 60 61 if (i == RTE_DIM(hsc)) 62 return -1; 63 64 return 0; 65 } 66 67 struct fips_test_callback hmac_tests_vectors[] = { 68 {KEYLEN_STR, parser_read_uint32_val, &vec.cipher_auth.key}, 69 {TAGLEN_STR, parser_read_uint32_val, &vec.cipher_auth.digest}, 70 {KEY_STR, parse_uint8_hex_str, &vec.cipher_auth.key}, 71 {PT_STR, parse_uint8_hex_str, &vec.pt}, 72 {TAG_STR, parse_uint8_hex_str, &vec.cipher_auth.digest}, 73 {NULL, NULL, NULL} /**< end pointer */ 74 }; 75 76 struct fips_test_callback hmac_tests_interim_vectors[] = { 77 {ALGO_PREFIX, parse_interim_algo, NULL}, 78 {NULL, NULL, NULL} /**< end pointer */ 79 }; 80 81 #ifdef USE_JANSSON 82 struct hash_size_conversion json_algorithms[] = { 83 {"HMAC-SHA-1", RTE_CRYPTO_AUTH_SHA1_HMAC}, 84 {"HMAC-SHA2-224", RTE_CRYPTO_AUTH_SHA224_HMAC}, 85 {"HMAC-SHA2-256", RTE_CRYPTO_AUTH_SHA256_HMAC}, 86 {"HMAC-SHA2-384", RTE_CRYPTO_AUTH_SHA384_HMAC}, 87 {"HMAC-SHA2-512", RTE_CRYPTO_AUTH_SHA512_HMAC}, 88 {"HMAC-SHA3-224", RTE_CRYPTO_AUTH_SHA3_224_HMAC}, 89 {"HMAC-SHA3-256", RTE_CRYPTO_AUTH_SHA3_256_HMAC}, 90 {"HMAC-SHA3-384", RTE_CRYPTO_AUTH_SHA3_384_HMAC}, 91 {"HMAC-SHA3-512", RTE_CRYPTO_AUTH_SHA3_512_HMAC}, 92 }; 93 94 struct fips_test_callback hmac_tests_json_vectors[] = { 95 {KEY_JSON_STR, parse_uint8_hex_str, &vec.cipher_auth.key}, 96 {PT_JSON_STR, parse_uint8_hex_str, &vec.pt}, 97 {TAG_JSON_STR, parse_uint8_hex_str, &vec.cipher_auth.digest}, 98 {NULL, NULL, NULL} /**< end pointer */ 99 }; 100 101 struct fips_test_callback hmac_tests_interim_json_vectors[] = { 102 {KEYLEN_JSON_STR, parser_read_uint32_val, &vec.cipher_auth.key}, 103 {TAGLEN_JSON_STR, parser_read_uint32_bit_val, &vec.cipher_auth.digest}, 104 {NULL, NULL, NULL} /**< end pointer */ 105 }; 106 #endif /* USE_JANSSON */ 107 108 static int 109 parse_test_hmac_writeback(struct fips_val *val) 110 { 111 struct fips_val val_local; 112 113 fprintf(info.fp_wr, "%s", TAG_STR); 114 115 val_local.val = val->val + vec.pt.len; 116 val_local.len = vec.cipher_auth.digest.len; 117 118 parse_write_hex_str(&val_local); 119 return 0; 120 } 121 122 static int 123 rsp_test_hmac_check(struct fips_val *val) 124 { 125 if (memcmp(val->val + vec.pt.len, vec.cipher_auth.digest.val, 126 vec.cipher_auth.digest.len) == 0) 127 fprintf(info.fp_wr, "Success\n"); 128 else 129 fprintf(info.fp_wr, "Failed\n"); 130 131 return 0; 132 } 133 134 int 135 parse_test_hmac_init(void) 136 { 137 info.op = FIPS_TEST_ENC_AUTH_GEN; 138 info.parse_writeback = parse_test_hmac_writeback; 139 info.callbacks = hmac_tests_vectors; 140 info.interim_callbacks = hmac_tests_interim_vectors; 141 info.writeback_callbacks = NULL; 142 info.kat_check = rsp_test_hmac_check; 143 144 return 0; 145 } 146 147 #ifdef USE_JANSSON 148 static int 149 parse_test_hmac_json_writeback(struct fips_val *val) 150 { 151 struct fips_val val_local; 152 json_t *tcId, *mac; 153 154 tcId = json_object_get(json_info.json_test_case, "tcId"); 155 156 json_info.json_write_case = json_object(); 157 json_object_set(json_info.json_write_case, "tcId", tcId); 158 159 160 val_local.val = val->val + vec.pt.len; 161 val_local.len = vec.cipher_auth.digest.len; 162 163 writeback_hex_str("", info.one_line_text, &val_local); 164 165 mac = json_string(info.one_line_text); 166 json_object_set_new(json_info.json_write_case, TAG_JSON_STR, mac); 167 168 return 0; 169 } 170 171 int 172 parse_test_hmac_json_algorithm(void) 173 { 174 json_t *algorithm_object; 175 const char *algorithm_str; 176 uint32_t i; 177 178 algorithm_object = json_object_get(json_info.json_vector_set, "algorithm"); 179 algorithm_str = json_string_value(algorithm_object); 180 181 for (i = 0; i < RTE_DIM(json_algorithms); i++) { 182 if (strstr(algorithm_str, json_algorithms[i].str)) { 183 info.interim_info.hmac_data.algo = json_algorithms[i].algo; 184 return 0; 185 } 186 } 187 188 return -1; 189 } 190 191 int 192 parse_test_hmac_json_init(void) 193 { 194 info.op = FIPS_TEST_ENC_AUTH_GEN; 195 info.parse_writeback = parse_test_hmac_json_writeback; 196 info.callbacks = hmac_tests_json_vectors; 197 info.writeback_callbacks = NULL; 198 info.kat_check = rsp_test_hmac_check; 199 info.interim_callbacks = hmac_tests_interim_json_vectors; 200 201 if (parse_test_hmac_json_algorithm() < 0) 202 return -1; 203 204 return 0; 205 } 206 #endif /* USE_JANSSON */ 207