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