1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2018 Intel Corporation 3 */ 4 5 #ifndef _FIPS_VALIDATION_H_ 6 #define _FIPS_VALIDATION_H_ 7 8 #ifdef USE_JANSSON 9 #include <jansson.h> 10 #endif /* USE_JANSSON */ 11 12 #define FIPS_PARSE_ERR(fmt, args) \ 13 RTE_LOG(ERR, USER1, "FIPS parse error" ## fmt ## "\n", ## args) 14 15 #define ERR_MSG_SIZE 128 16 #define MAX_CASE_LINE 15 17 #define MAX_LINE_CHAR 204800 /*< max number of characters per line */ 18 #define MAX_NB_TESTS 10240 19 #define DEF_MBUF_SEG_SIZE (UINT16_MAX - sizeof(struct rte_mbuf) - \ 20 RTE_PKTMBUF_HEADROOM) 21 #define MAX_STRING_SIZE 64 22 #define MAX_FILE_NAME_SIZE 256 23 #define MAX_DIGEST_SIZE 64 24 25 #define POSITIVE_TEST 0 26 #define NEGATIVE_TEST -1 27 28 #define REQ_FILE_PREFIX "req" 29 #define RSP_FILE_PREFIX "rsp" 30 #define FAX_FILE_PREFIX "fax" 31 #define JSON_FILE_PREFIX "json" 32 33 #define ACVVERSION "1.0" 34 35 enum fips_test_algorithms { 36 FIPS_TEST_ALGO_AES = 0, 37 FIPS_TEST_ALGO_AES_CBC, 38 FIPS_TEST_ALGO_AES_GCM, 39 FIPS_TEST_ALGO_AES_GMAC, 40 FIPS_TEST_ALGO_AES_CMAC, 41 FIPS_TEST_ALGO_AES_CCM, 42 FIPS_TEST_ALGO_AES_XTS, 43 FIPS_TEST_ALGO_HMAC, 44 FIPS_TEST_ALGO_TDES, 45 FIPS_TEST_ALGO_SHA, 46 FIPS_TEST_ALGO_MAX 47 }; 48 49 enum file_types { 50 FIPS_TYPE_REQ = 1, 51 FIPS_TYPE_FAX, 52 FIPS_TYPE_RSP, 53 FIPS_TYPE_JSON, 54 }; 55 56 enum fips_test_op { 57 FIPS_TEST_ENC_AUTH_GEN = 1, 58 FIPS_TEST_DEC_AUTH_VERIF, 59 }; 60 61 #define MAX_LINE_PER_VECTOR 16 62 63 struct fips_val { 64 uint8_t *val; 65 uint32_t len; 66 }; 67 68 struct fips_test_vector { 69 union { 70 struct { 71 struct fips_val key; 72 struct fips_val digest; 73 struct fips_val auth_aad; 74 struct fips_val aad; 75 } cipher_auth; 76 struct { 77 struct fips_val key; 78 struct fips_val digest; 79 struct fips_val aad; 80 } aead; 81 }; 82 83 struct fips_val pt; 84 struct fips_val ct; 85 struct fips_val iv; 86 87 enum rte_crypto_op_status status; 88 }; 89 90 typedef int (*post_prcess_t)(struct fips_val *val); 91 92 typedef int (*parse_callback_t)(const char *key, char *text, 93 struct fips_val *val); 94 95 struct fips_test_callback { 96 const char *key; 97 parse_callback_t cb; 98 struct fips_val *val; 99 }; 100 101 enum fips_aesavs_test_types { 102 AESAVS_TYPE_GFXBOX = 1, 103 AESAVS_TYPE_KEYSBOX, 104 AESAVS_TYPE_VARKEY, 105 AESAVS_TYPE_VARTXT, 106 AESAVS_TYPE_MMT, 107 AESAVS_TYPE_MCT, 108 AESAVS_TYPE_AFT, 109 }; 110 111 enum fips_tdes_test_types { 112 TDES_INVERSE_PERMUTATION = 0, 113 TDES_PERMUTATION, 114 TDES_SUBSTITUTION_TABLE, 115 TDES_VARIABLE_KEY, 116 TDES_VARIABLE_TEXT, 117 TDES_KAT, 118 TDES_AFT, /* Functional Test */ 119 TDES_MCT, /* Monte Carlo (Modes) Test */ 120 TDES_MMT /* Multi block Message Test */ 121 }; 122 123 enum fips_tdes_test_mode { 124 TDES_MODE_CBC = 0, 125 TDES_MODE_ECB 126 }; 127 128 enum fips_ccm_test_types { 129 CCM_VADT = 1, /* Variable Associated Data Test */ 130 CCM_VPT, /* Variable Payload Test */ 131 CCM_VNT, /* Variable Nonce Test */ 132 CCM_VTT, /* Variable Tag Test */ 133 CCM_DVPT, /* Decryption-Verification Process Test */ 134 }; 135 136 enum fips_sha_test_types { 137 SHA_KAT = 0, 138 SHA_AFT, 139 SHA_MCT 140 }; 141 142 struct aesavs_interim_data { 143 enum fips_aesavs_test_types test_type; 144 uint32_t cipher_algo; 145 uint32_t key_len; 146 }; 147 148 struct hmac_interim_data { 149 enum rte_crypto_auth_algorithm algo; 150 }; 151 152 struct tdes_interim_data { 153 enum fips_tdes_test_types test_type; 154 enum fips_tdes_test_mode test_mode; 155 uint32_t nb_keys; 156 }; 157 158 struct ccm_interim_data { 159 enum fips_ccm_test_types test_type; 160 uint32_t aad_len; 161 uint32_t pt_len; 162 uint32_t digest_len; 163 uint32_t key_len; 164 uint32_t iv_len; 165 }; 166 167 struct sha_interim_data { 168 enum fips_sha_test_types test_type; 169 enum rte_crypto_auth_algorithm algo; 170 }; 171 172 struct gcm_interim_data { 173 uint8_t is_gmac; 174 uint8_t gen_iv; 175 }; 176 177 enum xts_tweak_modes { 178 XTS_TWEAK_MODE_HEX = 0, 179 XTS_TWEAK_MODE_NUMBER 180 }; 181 182 struct xts_interim_data { 183 enum xts_tweak_modes tweak_mode; 184 }; 185 186 #ifdef USE_JANSSON 187 /* 188 * Maximum length of buffer to hold any json string. 189 * Esp, in asym op, modulo bits decide char buffer size. 190 * max = (modulo / 4) 191 */ 192 #define FIPS_TEST_JSON_BUF_LEN (4096 / 4) 193 194 struct fips_test_json_info { 195 /* Information used for reading from json */ 196 json_t *json_root; 197 json_t *json_vector_set; 198 json_t *json_test_group; 199 json_t *json_test_case; 200 /* Location of json write output */ 201 json_t *json_write_root; 202 json_t *json_write_group; 203 json_t *json_write_set; 204 json_t *json_write_case; 205 /* Other info */ 206 uint8_t is_sample; 207 }; 208 #endif /* USE_JANSSON */ 209 210 struct fips_test_interim_info { 211 FILE *fp_rd; 212 FILE *fp_wr; 213 enum file_types file_type; 214 enum fips_test_algorithms algo; 215 char *one_line_text; 216 char *vec[MAX_LINE_PER_VECTOR]; 217 uint32_t vec_start_off; 218 uint32_t nb_vec_lines; 219 char device_name[MAX_STRING_SIZE]; 220 char file_name[MAX_FILE_NAME_SIZE]; 221 float version; 222 223 union { 224 struct aesavs_interim_data aes_data; 225 struct hmac_interim_data hmac_data; 226 struct tdes_interim_data tdes_data; 227 struct ccm_interim_data ccm_data; 228 struct sha_interim_data sha_data; 229 struct gcm_interim_data gcm_data; 230 struct xts_interim_data xts_data; 231 } interim_info; 232 233 enum fips_test_op op; 234 235 const struct fips_test_callback *callbacks; 236 const struct fips_test_callback *interim_callbacks; 237 const struct fips_test_callback *writeback_callbacks; 238 239 post_prcess_t parse_interim_writeback; 240 post_prcess_t parse_writeback; 241 post_prcess_t kat_check; 242 }; 243 244 extern struct fips_test_vector vec; 245 extern struct fips_test_interim_info info; 246 247 #ifdef USE_JANSSON 248 extern struct fips_test_json_info json_info; 249 #endif /* USE_JANSSON */ 250 251 int 252 fips_test_init(const char *req_file_path, const char *rsp_file_path, 253 const char *device_name); 254 255 void 256 fips_test_clear(void); 257 258 int 259 fips_test_fetch_one_block(void); 260 261 int 262 fips_test_parse_one_case(void); 263 264 void 265 fips_test_write_one_case(void); 266 267 #ifdef USE_JANSSON 268 int 269 fips_test_parse_one_json_vector_set(void); 270 271 int 272 fips_test_parse_one_json_group(void); 273 274 int 275 fips_test_parse_one_json_case(void); 276 277 int 278 parse_test_gcm_json_init(void); 279 280 int 281 parse_test_hmac_json_init(void); 282 283 int 284 parse_test_hmac_json_algorithm(void); 285 286 int 287 parse_test_cmac_json_init(void); 288 289 int 290 parse_test_aes_json_init(void); 291 292 int 293 parse_test_xts_json_init(void); 294 295 int 296 parse_test_sha_json_init(void); 297 298 int 299 parse_test_sha_json_algorithm(void); 300 301 int 302 parse_test_sha_json_test_type(void); 303 304 int 305 parse_test_tdes_json_init(void); 306 #endif /* USE_JANSSON */ 307 308 int 309 parse_test_aes_init(void); 310 311 int 312 parse_test_tdes_init(void); 313 314 int 315 parse_test_hmac_init(void); 316 317 int 318 parse_test_gcm_init(void); 319 320 int 321 parse_test_cmac_init(void); 322 323 int 324 parse_test_ccm_init(void); 325 326 int 327 parse_test_sha_init(void); 328 329 int 330 parse_test_xts_init(void); 331 332 int 333 parser_read_uint8_hex(uint8_t *value, const char *p); 334 335 int 336 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val); 337 338 int 339 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val); 340 341 int 342 parser_read_uint16(uint16_t *value, const char *p); 343 344 int 345 parser_read_uint32_val(const char *key, char *src, struct fips_val *val); 346 347 int 348 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val); 349 350 int 351 parser_read_uint32(uint32_t *value, char *p); 352 353 int 354 parser_read_uint32_val(const char *key, char *src, struct fips_val *val); 355 356 int 357 writeback_hex_str(const char *key, char *dst, struct fips_val *val); 358 359 void 360 parse_write_hex_str(struct fips_val *src); 361 362 int 363 update_info_vec(uint32_t count); 364 365 typedef int (*fips_test_one_case_t)(void); 366 typedef int (*fips_prepare_op_t)(void); 367 typedef int (*fips_prepare_xform_t)(struct rte_crypto_sym_xform *); 368 369 struct fips_test_ops { 370 fips_prepare_xform_t prepare_xform; 371 fips_prepare_op_t prepare_op; 372 fips_test_one_case_t test; 373 }; 374 375 extern struct fips_test_ops test_ops; 376 377 int prepare_aead_op(void); 378 379 int prepare_auth_op(void); 380 381 int prepare_gcm_xform(struct rte_crypto_sym_xform *xform); 382 383 int prepare_gmac_xform(struct rte_crypto_sym_xform *xform); 384 385 int parse_test_sha_hash_size(enum rte_crypto_auth_algorithm algo); 386 387 #endif 388