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