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