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_CTR, 39 FIPS_TEST_ALGO_AES_GCM, 40 FIPS_TEST_ALGO_AES_GMAC, 41 FIPS_TEST_ALGO_AES_CMAC, 42 FIPS_TEST_ALGO_AES_CCM, 43 FIPS_TEST_ALGO_AES_XTS, 44 FIPS_TEST_ALGO_HMAC, 45 FIPS_TEST_ALGO_TDES, 46 FIPS_TEST_ALGO_SHA, 47 FIPS_TEST_ALGO_RSA, 48 FIPS_TEST_ALGO_ECDSA, 49 FIPS_TEST_ALGO_MAX 50 }; 51 52 enum file_types { 53 FIPS_TYPE_REQ = 1, 54 FIPS_TYPE_FAX, 55 FIPS_TYPE_RSP, 56 FIPS_TYPE_JSON, 57 }; 58 59 enum fips_test_op { 60 FIPS_TEST_ENC_AUTH_GEN = 1, 61 FIPS_TEST_DEC_AUTH_VERIF, 62 FIPS_TEST_ASYM_KEYGEN, 63 FIPS_TEST_ASYM_SIGGEN, 64 FIPS_TEST_ASYM_SIGVER 65 }; 66 67 #define MAX_LINE_PER_VECTOR 16 68 69 struct fips_val { 70 uint8_t *val; 71 uint32_t len; 72 }; 73 74 struct fips_test_vector { 75 union { 76 struct { 77 struct fips_val key; 78 struct fips_val digest; 79 struct fips_val auth_aad; 80 struct fips_val aad; 81 } cipher_auth; 82 struct { 83 struct fips_val key; 84 struct fips_val digest; 85 struct fips_val aad; 86 } aead; 87 }; 88 struct { 89 struct fips_val seed; 90 struct fips_val signature; 91 struct fips_val e; 92 struct fips_val n; 93 struct fips_val d; 94 struct fips_val p; 95 struct fips_val q; 96 struct fips_val dp; 97 struct fips_val dq; 98 struct fips_val qinv; 99 } rsa; 100 struct { 101 struct fips_val seed; 102 struct fips_val pkey; 103 struct fips_val qx; 104 struct fips_val qy; 105 struct fips_val r; 106 struct fips_val s; 107 struct fips_val k; 108 } ecdsa; 109 110 struct fips_val pt; 111 struct fips_val ct; 112 struct fips_val iv; 113 enum rte_crypto_op_status status; 114 }; 115 116 typedef int (*post_prcess_t)(struct fips_val *val); 117 118 typedef int (*parse_callback_t)(const char *key, char *text, 119 struct fips_val *val); 120 121 struct fips_test_callback { 122 const char *key; 123 parse_callback_t cb; 124 struct fips_val *val; 125 }; 126 127 enum fips_aesavs_test_types { 128 AESAVS_TYPE_GFXBOX = 1, 129 AESAVS_TYPE_KEYSBOX, 130 AESAVS_TYPE_VARKEY, 131 AESAVS_TYPE_VARTXT, 132 AESAVS_TYPE_MMT, 133 AESAVS_TYPE_MCT, 134 AESAVS_TYPE_AFT, 135 AESAVS_TYPE_CTR, 136 }; 137 138 enum fips_tdes_test_types { 139 TDES_INVERSE_PERMUTATION = 0, 140 TDES_PERMUTATION, 141 TDES_SUBSTITUTION_TABLE, 142 TDES_VARIABLE_KEY, 143 TDES_VARIABLE_TEXT, 144 TDES_KAT, 145 TDES_AFT, /* Functional Test */ 146 TDES_MCT, /* Monte Carlo (Modes) Test */ 147 TDES_MMT /* Multi block Message Test */ 148 }; 149 150 enum fips_tdes_test_mode { 151 TDES_MODE_CBC = 0, 152 TDES_MODE_ECB 153 }; 154 155 enum fips_ccm_test_types { 156 CCM_VADT = 1, /* Variable Associated Data Test */ 157 CCM_VPT, /* Variable Payload Test */ 158 CCM_VNT, /* Variable Nonce Test */ 159 CCM_VTT, /* Variable Tag Test */ 160 CCM_DVPT, /* Decryption-Verification Process Test */ 161 }; 162 163 enum fips_sha_test_types { 164 SHA_KAT = 0, 165 SHA_AFT, 166 SHA_MCT 167 }; 168 169 enum fips_rsa_test_types { 170 RSA_AFT = 0, 171 RSA_GDT, 172 RSA_KAT 173 }; 174 175 enum fips_ecdsa_test_types { 176 ECDSA_AFT = 0, 177 }; 178 179 struct aesavs_interim_data { 180 enum fips_aesavs_test_types test_type; 181 uint32_t cipher_algo; 182 uint32_t key_len; 183 }; 184 185 struct hmac_interim_data { 186 enum rte_crypto_auth_algorithm algo; 187 }; 188 189 struct tdes_interim_data { 190 enum fips_tdes_test_types test_type; 191 enum fips_tdes_test_mode test_mode; 192 uint32_t nb_keys; 193 }; 194 195 struct ccm_interim_data { 196 enum fips_ccm_test_types test_type; 197 uint32_t aad_len; 198 uint32_t pt_len; 199 uint32_t digest_len; 200 uint32_t key_len; 201 uint32_t iv_len; 202 }; 203 204 struct sha_interim_data { 205 /* keep algo always on top as it is also used in asym digest */ 206 enum rte_crypto_auth_algorithm algo; 207 enum fips_sha_test_types test_type; 208 uint8_t md_blocks; 209 }; 210 211 struct gcm_interim_data { 212 uint8_t is_gmac; 213 uint8_t gen_iv; 214 }; 215 216 enum xts_tweak_modes { 217 XTS_TWEAK_MODE_HEX = 0, 218 XTS_TWEAK_MODE_NUMBER 219 }; 220 221 struct xts_interim_data { 222 enum xts_tweak_modes tweak_mode; 223 }; 224 225 struct rsa_interim_data { 226 enum rte_crypto_auth_algorithm auth; 227 uint16_t modulo; 228 uint16_t saltlen; 229 enum rte_crypto_rsa_padding_type padding; 230 enum rte_crypto_rsa_priv_key_type privkey; 231 uint8_t random_msg; 232 }; 233 234 struct ecdsa_interim_data { 235 enum rte_crypto_auth_algorithm auth; 236 enum rte_crypto_curve_id curve_id; 237 uint8_t curve_len; 238 uint8_t random_msg; 239 uint8_t pubkey_gen; 240 }; 241 242 #ifdef USE_JANSSON 243 /* 244 * Maximum length of buffer to hold any json string. 245 * Esp, in asym op, modulo bits decide char buffer size. 246 * max = (modulo / 4) 247 */ 248 #define FIPS_TEST_JSON_BUF_LEN (4096 / 4) 249 250 struct fips_test_json_info { 251 /* Information used for reading from json */ 252 json_t *json_root; 253 json_t *json_vector_set; 254 json_t *json_test_group; 255 json_t *json_test_case; 256 /* Location of json write output */ 257 json_t *json_write_root; 258 json_t *json_write_group; 259 json_t *json_write_set; 260 json_t *json_write_case; 261 /* Other info */ 262 uint8_t is_sample; 263 }; 264 #endif /* USE_JANSSON */ 265 266 struct fips_test_interim_info { 267 FILE *fp_rd; 268 FILE *fp_wr; 269 enum file_types file_type; 270 enum fips_test_algorithms algo; 271 char *one_line_text; 272 char *vec[MAX_LINE_PER_VECTOR]; 273 uint32_t vec_start_off; 274 uint32_t nb_vec_lines; 275 char device_name[MAX_STRING_SIZE]; 276 char file_name[MAX_FILE_NAME_SIZE]; 277 float version; 278 279 union { 280 struct aesavs_interim_data aes_data; 281 struct hmac_interim_data hmac_data; 282 struct tdes_interim_data tdes_data; 283 struct ccm_interim_data ccm_data; 284 struct sha_interim_data sha_data; 285 struct gcm_interim_data gcm_data; 286 struct xts_interim_data xts_data; 287 struct rsa_interim_data rsa_data; 288 struct ecdsa_interim_data ecdsa_data; 289 } interim_info; 290 291 enum fips_test_op op; 292 293 const struct fips_test_callback *callbacks; 294 const struct fips_test_callback *interim_callbacks; 295 const struct fips_test_callback *writeback_callbacks; 296 297 post_prcess_t parse_interim_writeback; 298 post_prcess_t post_interim_writeback; 299 post_prcess_t parse_writeback; 300 post_prcess_t kat_check; 301 }; 302 303 extern struct fips_test_vector vec; 304 extern struct fips_test_interim_info info; 305 306 #ifdef USE_JANSSON 307 extern struct fips_test_json_info json_info; 308 #endif /* USE_JANSSON */ 309 310 int 311 fips_test_init(const char *req_file_path, const char *rsp_file_path, 312 const char *device_name); 313 314 void 315 fips_test_clear(void); 316 317 int 318 fips_test_fetch_one_block(void); 319 320 int 321 fips_test_parse_one_case(void); 322 323 void 324 fips_test_write_one_case(void); 325 326 #ifdef USE_JANSSON 327 int 328 fips_test_parse_one_json_vector_set(void); 329 330 int 331 fips_test_parse_one_json_group(void); 332 333 int 334 fips_test_parse_one_json_case(void); 335 336 int 337 parse_test_gcm_json_init(void); 338 339 int 340 parse_test_hmac_json_init(void); 341 342 int 343 parse_test_hmac_json_algorithm(void); 344 345 int 346 parse_test_cmac_json_init(void); 347 348 int 349 parse_test_aes_json_init(void); 350 351 int 352 parse_test_xts_json_init(void); 353 354 int 355 parse_test_sha_json_init(void); 356 357 int 358 parse_test_sha_json_algorithm(void); 359 360 int 361 parse_test_sha_json_test_type(void); 362 363 int 364 parse_test_tdes_json_init(void); 365 366 int 367 parse_test_rsa_json_init(void); 368 369 int 370 parse_test_ecdsa_json_init(void); 371 372 int 373 fips_test_randomize_message(struct fips_val *msg, struct fips_val *rand); 374 #endif /* USE_JANSSON */ 375 376 int 377 parse_test_aes_init(void); 378 379 int 380 parse_test_tdes_init(void); 381 382 int 383 parse_test_hmac_init(void); 384 385 int 386 parse_test_gcm_init(void); 387 388 int 389 parse_test_cmac_init(void); 390 391 int 392 parse_test_ccm_init(void); 393 394 int 395 parse_test_sha_init(void); 396 397 int 398 parse_test_xts_init(void); 399 400 int 401 parser_read_uint8_hex(uint8_t *value, const char *p); 402 403 int 404 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val); 405 406 int 407 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val); 408 409 int 410 parser_read_uint16(uint16_t *value, const char *p); 411 412 int 413 parser_read_uint32_val(const char *key, char *src, struct fips_val *val); 414 415 int 416 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val); 417 418 int 419 parser_read_uint32(uint32_t *value, char *p); 420 421 int 422 parser_read_uint32_val(const char *key, char *src, struct fips_val *val); 423 424 int 425 writeback_hex_str(const char *key, char *dst, struct fips_val *val); 426 427 void 428 parse_write_hex_str(struct fips_val *src); 429 430 int 431 update_info_vec(uint32_t count); 432 433 typedef int (*fips_test_one_case_t)(void); 434 typedef int (*fips_prepare_op_t)(void); 435 typedef int (*fips_prepare_sym_xform_t)(struct rte_crypto_sym_xform *); 436 typedef int (*fips_prepare_asym_xform_t)(struct rte_crypto_asym_xform *); 437 438 struct fips_test_ops { 439 fips_prepare_sym_xform_t prepare_sym_xform; 440 fips_prepare_asym_xform_t prepare_asym_xform; 441 fips_prepare_op_t prepare_sym_op; 442 fips_prepare_op_t prepare_asym_op; 443 fips_test_one_case_t test; 444 }; 445 446 extern struct fips_test_ops test_ops; 447 448 int prepare_aead_op(void); 449 450 int prepare_auth_op(void); 451 452 int prepare_gcm_xform(struct rte_crypto_sym_xform *xform); 453 454 int prepare_gmac_xform(struct rte_crypto_sym_xform *xform); 455 456 int parse_test_sha_hash_size(enum rte_crypto_auth_algorithm algo); 457 458 #endif 459