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