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