xref: /dpdk/examples/fips_validation/fips_validation.h (revision 927cb43fe90f95eb696e890682749749d8149bb2)
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 };
168 
169 enum fips_rsa_test_types {
170 	RSA_AFT = 0,
171 	RSA_GDT,
172 	RSA_KAT
173 };
174 
175 enum fips_ecdsa_test_types {
176 	ECDSA_AFT = 0,
177 };
178 
179 struct aesavs_interim_data {
180 	enum fips_aesavs_test_types test_type;
181 	uint32_t cipher_algo;
182 	uint32_t key_len;
183 };
184 
185 struct hmac_interim_data {
186 	enum rte_crypto_auth_algorithm algo;
187 };
188 
189 struct tdes_interim_data {
190 	enum fips_tdes_test_types test_type;
191 	enum fips_tdes_test_mode test_mode;
192 	uint32_t nb_keys;
193 };
194 
195 struct ccm_interim_data {
196 	enum fips_ccm_test_types test_type;
197 	uint32_t aad_len;
198 	uint32_t pt_len;
199 	uint32_t digest_len;
200 	uint32_t key_len;
201 	uint32_t iv_len;
202 };
203 
204 struct sha_interim_data {
205 	/* keep algo always on top as it is also used in asym digest */
206 	enum rte_crypto_auth_algorithm algo;
207 	enum fips_sha_test_types test_type;
208 };
209 
210 struct gcm_interim_data {
211 	uint8_t is_gmac;
212 	uint8_t gen_iv;
213 };
214 
215 enum xts_tweak_modes {
216 	XTS_TWEAK_MODE_HEX = 0,
217 	XTS_TWEAK_MODE_NUMBER
218 };
219 
220 struct xts_interim_data {
221 	enum xts_tweak_modes tweak_mode;
222 };
223 
224 struct rsa_interim_data {
225 	enum rte_crypto_auth_algorithm auth;
226 	uint16_t modulo;
227 	uint16_t saltlen;
228 	enum rte_crypto_rsa_padding_type padding;
229 	enum rte_crypto_rsa_priv_key_type privkey;
230 	uint8_t random_msg;
231 };
232 
233 struct ecdsa_interim_data {
234 	enum rte_crypto_auth_algorithm auth;
235 	enum rte_crypto_curve_id curve_id;
236 	uint8_t curve_len;
237 	uint8_t random_msg;
238 	uint8_t pubkey_gen;
239 };
240 
241 #ifdef USE_JANSSON
242 /*
243  * Maximum length of buffer to hold any json string.
244  * Esp, in asym op, modulo bits decide char buffer size.
245  * max = (modulo / 4)
246  */
247 #define FIPS_TEST_JSON_BUF_LEN (4096 / 4)
248 
249 struct fips_test_json_info {
250 	/* Information used for reading from json */
251 	json_t *json_root;
252 	json_t *json_vector_set;
253 	json_t *json_test_group;
254 	json_t *json_test_case;
255 	/* Location of json write output */
256 	json_t *json_write_root;
257 	json_t *json_write_group;
258 	json_t *json_write_set;
259 	json_t *json_write_case;
260 	/* Other info */
261 	uint8_t is_sample;
262 };
263 #endif /* USE_JANSSON */
264 
265 struct fips_test_interim_info {
266 	FILE *fp_rd;
267 	FILE *fp_wr;
268 	enum file_types file_type;
269 	enum fips_test_algorithms algo;
270 	char *one_line_text;
271 	char *vec[MAX_LINE_PER_VECTOR];
272 	uint32_t vec_start_off;
273 	uint32_t nb_vec_lines;
274 	char device_name[MAX_STRING_SIZE];
275 	char file_name[MAX_FILE_NAME_SIZE];
276 	float version;
277 
278 	union {
279 		struct aesavs_interim_data aes_data;
280 		struct hmac_interim_data hmac_data;
281 		struct tdes_interim_data tdes_data;
282 		struct ccm_interim_data ccm_data;
283 		struct sha_interim_data sha_data;
284 		struct gcm_interim_data gcm_data;
285 		struct xts_interim_data xts_data;
286 		struct rsa_interim_data rsa_data;
287 		struct ecdsa_interim_data ecdsa_data;
288 	} interim_info;
289 
290 	enum fips_test_op op;
291 
292 	const struct fips_test_callback *callbacks;
293 	const struct fips_test_callback *interim_callbacks;
294 	const struct fips_test_callback *writeback_callbacks;
295 
296 	post_prcess_t parse_interim_writeback;
297 	post_prcess_t post_interim_writeback;
298 	post_prcess_t parse_writeback;
299 	post_prcess_t kat_check;
300 };
301 
302 extern struct fips_test_vector vec;
303 extern struct fips_test_interim_info info;
304 
305 #ifdef USE_JANSSON
306 extern struct fips_test_json_info json_info;
307 #endif /* USE_JANSSON */
308 
309 int
310 fips_test_init(const char *req_file_path, const char *rsp_file_path,
311 		const char *device_name);
312 
313 void
314 fips_test_clear(void);
315 
316 int
317 fips_test_fetch_one_block(void);
318 
319 int
320 fips_test_parse_one_case(void);
321 
322 void
323 fips_test_write_one_case(void);
324 
325 #ifdef USE_JANSSON
326 int
327 fips_test_parse_one_json_vector_set(void);
328 
329 int
330 fips_test_parse_one_json_group(void);
331 
332 int
333 fips_test_parse_one_json_case(void);
334 
335 int
336 parse_test_gcm_json_init(void);
337 
338 int
339 parse_test_hmac_json_init(void);
340 
341 int
342 parse_test_hmac_json_algorithm(void);
343 
344 int
345 parse_test_cmac_json_init(void);
346 
347 int
348 parse_test_aes_json_init(void);
349 
350 int
351 parse_test_xts_json_init(void);
352 
353 int
354 parse_test_sha_json_init(void);
355 
356 int
357 parse_test_sha_json_algorithm(void);
358 
359 int
360 parse_test_sha_json_test_type(void);
361 
362 int
363 parse_test_tdes_json_init(void);
364 
365 int
366 parse_test_rsa_json_init(void);
367 
368 int
369 parse_test_ecdsa_json_init(void);
370 
371 int
372 fips_test_randomize_message(struct fips_val *msg, struct fips_val *rand);
373 #endif /* USE_JANSSON */
374 
375 int
376 parse_test_aes_init(void);
377 
378 int
379 parse_test_tdes_init(void);
380 
381 int
382 parse_test_hmac_init(void);
383 
384 int
385 parse_test_gcm_init(void);
386 
387 int
388 parse_test_cmac_init(void);
389 
390 int
391 parse_test_ccm_init(void);
392 
393 int
394 parse_test_sha_init(void);
395 
396 int
397 parse_test_xts_init(void);
398 
399 int
400 parser_read_uint8_hex(uint8_t *value, const char *p);
401 
402 int
403 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val);
404 
405 int
406 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val);
407 
408 int
409 parser_read_uint16(uint16_t *value, const char *p);
410 
411 int
412 parser_read_uint32_val(const char *key, char *src, struct fips_val *val);
413 
414 int
415 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val);
416 
417 int
418 parser_read_uint32(uint32_t *value, char *p);
419 
420 int
421 parser_read_uint32_val(const char *key, char *src, struct fips_val *val);
422 
423 int
424 writeback_hex_str(const char *key, char *dst, struct fips_val *val);
425 
426 void
427 parse_write_hex_str(struct fips_val *src);
428 
429 int
430 update_info_vec(uint32_t count);
431 
432 typedef int (*fips_test_one_case_t)(void);
433 typedef int (*fips_prepare_op_t)(void);
434 typedef int (*fips_prepare_sym_xform_t)(struct rte_crypto_sym_xform *);
435 typedef int (*fips_prepare_asym_xform_t)(struct rte_crypto_asym_xform *);
436 
437 struct fips_test_ops {
438 	fips_prepare_sym_xform_t prepare_sym_xform;
439 	fips_prepare_asym_xform_t prepare_asym_xform;
440 	fips_prepare_op_t prepare_sym_op;
441 	fips_prepare_op_t prepare_asym_op;
442 	fips_test_one_case_t test;
443 };
444 
445 extern struct fips_test_ops test_ops;
446 
447 int prepare_aead_op(void);
448 
449 int prepare_auth_op(void);
450 
451 int prepare_gcm_xform(struct rte_crypto_sym_xform *xform);
452 
453 int prepare_gmac_xform(struct rte_crypto_sym_xform *xform);
454 
455 int parse_test_sha_hash_size(enum rte_crypto_auth_algorithm algo);
456 
457 #endif
458