xref: /dpdk/examples/fips_validation/fips_validation.h (revision 36128a67c27ecb83425a793b2eb837e0581692e7)
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 };
217 
218 #ifdef USE_JANSSON
219 /*
220  * Maximum length of buffer to hold any json string.
221  * Esp, in asym op, modulo bits decide char buffer size.
222  * max = (modulo / 4)
223  */
224 #define FIPS_TEST_JSON_BUF_LEN (4096 / 4)
225 
226 struct fips_test_json_info {
227 	/* Information used for reading from json */
228 	json_t *json_root;
229 	json_t *json_vector_set;
230 	json_t *json_test_group;
231 	json_t *json_test_case;
232 	/* Location of json write output */
233 	json_t *json_write_root;
234 	json_t *json_write_group;
235 	json_t *json_write_set;
236 	json_t *json_write_case;
237 	/* Other info */
238 	uint8_t is_sample;
239 };
240 #endif /* USE_JANSSON */
241 
242 struct fips_test_interim_info {
243 	FILE *fp_rd;
244 	FILE *fp_wr;
245 	enum file_types file_type;
246 	enum fips_test_algorithms algo;
247 	char *one_line_text;
248 	char *vec[MAX_LINE_PER_VECTOR];
249 	uint32_t vec_start_off;
250 	uint32_t nb_vec_lines;
251 	char device_name[MAX_STRING_SIZE];
252 	char file_name[MAX_FILE_NAME_SIZE];
253 	float version;
254 
255 	union {
256 		struct aesavs_interim_data aes_data;
257 		struct hmac_interim_data hmac_data;
258 		struct tdes_interim_data tdes_data;
259 		struct ccm_interim_data ccm_data;
260 		struct sha_interim_data sha_data;
261 		struct gcm_interim_data gcm_data;
262 		struct xts_interim_data xts_data;
263 		struct rsa_interim_data rsa_data;
264 	} interim_info;
265 
266 	enum fips_test_op op;
267 
268 	const struct fips_test_callback *callbacks;
269 	const struct fips_test_callback *interim_callbacks;
270 	const struct fips_test_callback *writeback_callbacks;
271 
272 	post_prcess_t parse_interim_writeback;
273 	post_prcess_t parse_writeback;
274 	post_prcess_t kat_check;
275 };
276 
277 extern struct fips_test_vector vec;
278 extern struct fips_test_interim_info info;
279 
280 #ifdef USE_JANSSON
281 extern struct fips_test_json_info json_info;
282 #endif /* USE_JANSSON */
283 
284 int
285 fips_test_init(const char *req_file_path, const char *rsp_file_path,
286 		const char *device_name);
287 
288 void
289 fips_test_clear(void);
290 
291 int
292 fips_test_fetch_one_block(void);
293 
294 int
295 fips_test_parse_one_case(void);
296 
297 void
298 fips_test_write_one_case(void);
299 
300 #ifdef USE_JANSSON
301 int
302 fips_test_parse_one_json_vector_set(void);
303 
304 int
305 fips_test_parse_one_json_group(void);
306 
307 int
308 fips_test_parse_one_json_case(void);
309 
310 int
311 parse_test_gcm_json_init(void);
312 
313 int
314 parse_test_hmac_json_init(void);
315 
316 int
317 parse_test_hmac_json_algorithm(void);
318 
319 int
320 parse_test_cmac_json_init(void);
321 
322 int
323 parse_test_aes_json_init(void);
324 
325 int
326 parse_test_xts_json_init(void);
327 
328 int
329 parse_test_sha_json_init(void);
330 
331 int
332 parse_test_sha_json_algorithm(void);
333 
334 int
335 parse_test_sha_json_test_type(void);
336 
337 int
338 parse_test_tdes_json_init(void);
339 
340 int
341 parse_test_rsa_json_init(void);
342 #endif /* USE_JANSSON */
343 
344 int
345 parse_test_aes_init(void);
346 
347 int
348 parse_test_tdes_init(void);
349 
350 int
351 parse_test_hmac_init(void);
352 
353 int
354 parse_test_gcm_init(void);
355 
356 int
357 parse_test_cmac_init(void);
358 
359 int
360 parse_test_ccm_init(void);
361 
362 int
363 parse_test_sha_init(void);
364 
365 int
366 parse_test_xts_init(void);
367 
368 int
369 parser_read_uint8_hex(uint8_t *value, const char *p);
370 
371 int
372 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val);
373 
374 int
375 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val);
376 
377 int
378 parser_read_uint16(uint16_t *value, const char *p);
379 
380 int
381 parser_read_uint32_val(const char *key, char *src, struct fips_val *val);
382 
383 int
384 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val);
385 
386 int
387 parser_read_uint32(uint32_t *value, char *p);
388 
389 int
390 parser_read_uint32_val(const char *key, char *src, struct fips_val *val);
391 
392 int
393 writeback_hex_str(const char *key, char *dst, struct fips_val *val);
394 
395 void
396 parse_write_hex_str(struct fips_val *src);
397 
398 int
399 update_info_vec(uint32_t count);
400 
401 typedef int (*fips_test_one_case_t)(void);
402 typedef int (*fips_prepare_op_t)(void);
403 typedef int (*fips_prepare_sym_xform_t)(struct rte_crypto_sym_xform *);
404 typedef int (*fips_prepare_asym_xform_t)(struct rte_crypto_asym_xform *);
405 
406 struct fips_test_ops {
407 	fips_prepare_sym_xform_t prepare_sym_xform;
408 	fips_prepare_asym_xform_t prepare_asym_xform;
409 	fips_prepare_op_t prepare_sym_op;
410 	fips_prepare_op_t prepare_asym_op;
411 	fips_test_one_case_t test;
412 };
413 
414 extern struct fips_test_ops test_ops;
415 
416 int prepare_aead_op(void);
417 
418 int prepare_auth_op(void);
419 
420 int prepare_gcm_xform(struct rte_crypto_sym_xform *xform);
421 
422 int prepare_gmac_xform(struct rte_crypto_sym_xform *xform);
423 
424 int parse_test_sha_hash_size(enum rte_crypto_auth_algorithm algo);
425 
426 #endif
427