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