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 }; 41 42 static int 43 parse_interim_algo(__rte_unused const char *key, 44 char *text, 45 __rte_unused struct fips_val *val) 46 { 47 48 uint32_t i; 49 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; 53 break; 54 } 55 } 56 57 if (i == RTE_DIM(hsc)) 58 return -1; 59 60 return 0; 61 } 62 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 */ 70 }; 71 72 struct fips_test_callback hmac_tests_interim_vectors[] = { 73 {ALGO_PREFIX, parse_interim_algo, NULL}, 74 {NULL, NULL, NULL} /**< end pointer */ 75 }; 76 77 #ifdef USE_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}, 84 }; 85 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 */ 91 }; 92 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 */ 97 }; 98 #endif /* USE_JANSSON */ 99 100 static int 101 parse_test_hmac_writeback(struct fips_val *val) 102 { 103 struct fips_val val_local; 104 105 fprintf(info.fp_wr, "%s", TAG_STR); 106 107 val_local.val = val->val + vec.pt.len; 108 val_local.len = vec.cipher_auth.digest.len; 109 110 parse_write_hex_str(&val_local); 111 return 0; 112 } 113 114 static int 115 rsp_test_hmac_check(struct fips_val *val) 116 { 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"); 120 else 121 fprintf(info.fp_wr, "Failed\n"); 122 123 return 0; 124 } 125 126 int 127 parse_test_hmac_init(void) 128 { 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; 135 136 return 0; 137 } 138 139 #ifdef USE_JANSSON 140 static int 141 parse_test_hmac_json_writeback(struct fips_val *val) 142 { 143 struct fips_val val_local; 144 json_t *tcId, *mac; 145 146 tcId = json_object_get(json_info.json_test_case, "tcId"); 147 148 json_info.json_write_case = json_object(); 149 json_object_set(json_info.json_write_case, "tcId", tcId); 150 151 152 val_local.val = val->val + vec.pt.len; 153 val_local.len = vec.cipher_auth.digest.len; 154 155 writeback_hex_str("", info.one_line_text, &val_local); 156 157 mac = json_string(info.one_line_text); 158 json_object_set_new(json_info.json_write_case, TAG_JSON_STR, mac); 159 160 return 0; 161 } 162 163 int 164 parse_test_hmac_json_algorithm(void) 165 { 166 json_t *algorithm_object; 167 const char *algorithm_str; 168 uint32_t i; 169 170 algorithm_object = json_object_get(json_info.json_vector_set, "algorithm"); 171 algorithm_str = json_string_value(algorithm_object); 172 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; 176 return 0; 177 } 178 } 179 180 return -1; 181 } 182 183 int 184 parse_test_hmac_json_init(void) 185 { 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; 192 193 if (parse_test_hmac_json_algorithm() < 0) 194 return -1; 195 196 return 0; 197 } 198 #endif /* USE_JANSSON */ 199