xref: /dpdk/examples/fips_validation/fips_validation.h (revision 487eec3401b7a1664982f39da139980a4f5b3adc)
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 #define FIPS_PARSE_ERR(fmt, args)					\
9 	RTE_LOG(ERR, USER1, "FIPS parse error" ## fmt ## "\n", ## args)
10 
11 #define ERR_MSG_SIZE		128
12 #define MAX_CASE_LINE		15
13 #define MAX_LINE_CHAR		204800 /*< max number of characters per line */
14 #define MAX_NB_TESTS		10240
15 #define MAX_BUF_SIZE		2048
16 #define MAX_STRING_SIZE		64
17 #define MAX_DIGEST_SIZE		64
18 
19 #define POSITIVE_TEST		0
20 #define NEGATIVE_TEST		-1
21 
22 #define REQ_FILE_PERFIX		"req"
23 #define RSP_FILE_PERFIX		"rsp"
24 #define FAX_FILE_PERFIX		"fax"
25 
26 enum fips_test_algorithms {
27 		FIPS_TEST_ALGO_AES = 0,
28 		FIPS_TEST_ALGO_AES_GCM,
29 		FIPS_TEST_ALGO_AES_CMAC,
30 		FIPS_TEST_ALGO_AES_CCM,
31 		FIPS_TEST_ALGO_HMAC,
32 		FIPS_TEST_ALGO_TDES,
33 		FIPS_TEST_ALGO_SHA,
34 		FIPS_TEST_ALGO_MAX
35 };
36 
37 enum file_types {
38 	FIPS_TYPE_REQ = 1,
39 	FIPS_TYPE_FAX,
40 	FIPS_TYPE_RSP
41 };
42 
43 enum fips_test_op {
44 	FIPS_TEST_ENC_AUTH_GEN = 1,
45 	FIPS_TEST_DEC_AUTH_VERIF,
46 };
47 
48 #define MAX_LINE_PER_VECTOR            16
49 
50 struct fips_val {
51 	uint8_t *val;
52 	uint32_t len;
53 };
54 
55 struct fips_test_vector {
56 	union {
57 		struct {
58 			struct fips_val key;
59 			struct fips_val digest;
60 			struct fips_val auth_aad;
61 			struct fips_val aad;
62 		} cipher_auth;
63 		struct {
64 			struct fips_val key;
65 			struct fips_val digest;
66 			struct fips_val aad;
67 		} aead;
68 	};
69 
70 	struct fips_val pt;
71 	struct fips_val ct;
72 	struct fips_val iv;
73 
74 	enum rte_crypto_op_status status;
75 };
76 
77 typedef int (*post_prcess_t)(struct fips_val *val);
78 
79 typedef int (*parse_callback_t)(const char *key, char *text,
80 		struct fips_val *val);
81 
82 struct fips_test_callback {
83 	const char *key;
84 	parse_callback_t cb;
85 	struct fips_val *val;
86 };
87 
88 enum fips_aesavs_test_types {
89 	AESAVS_TYPE_GFXBOX = 1,
90 	AESAVS_TYPE_KEYSBOX,
91 	AESAVS_TYPE_VARKEY,
92 	AESAVS_TYPE_VARTXT,
93 	AESAVS_TYPE_MMT,
94 	AESAVS_TYPE_MCT,
95 };
96 
97 enum fips_tdes_test_types {
98 	TDES_INVERSE_PERMUTATION = 0,
99 	TDES_PERMUTATION,
100 	TDES_SUBSTITUTION_TABLE,
101 	TDES_VARIABLE_KEY,
102 	TDES_VARIABLE_TEXT,
103 	TDES_KAT,
104 	TDES_MCT, /* Monte Carlo (Modes) Test */
105 	TDES_MMT /* Multi block Message Test */
106 };
107 
108 enum fips_tdes_test_mode {
109 	TDES_MODE_CBC = 0,
110 	TDES_MODE_ECB
111 };
112 
113 enum fips_ccm_test_types {
114 	CCM_VADT	= 1, /* Variable Associated Data Test */
115 	CCM_VPT,		 /* Variable Payload Test */
116 	CCM_VNT,		 /* Variable Nonce Test */
117 	CCM_VTT,		 /* Variable Tag Test */
118 	CCM_DVPT,	 /*  Decryption-Verification Process Test */
119 };
120 
121 enum fips_sha_test_types {
122 	SHA_KAT = 0,
123 	SHA_MCT
124 };
125 
126 struct aesavs_interim_data {
127 	enum fips_aesavs_test_types test_type;
128 	uint32_t cipher_algo;
129 	uint32_t key_len;
130 };
131 
132 struct hmac_interim_data {
133 	enum rte_crypto_auth_algorithm algo;
134 };
135 
136 struct tdes_interim_data {
137 	enum fips_tdes_test_types test_type;
138 	enum fips_tdes_test_mode test_mode;
139 	uint32_t nb_keys;
140 };
141 
142 struct ccm_interim_data {
143 	enum fips_ccm_test_types test_type;
144 	uint32_t aad_len;
145 	uint32_t pt_len;
146 	uint32_t digest_len;
147 	uint32_t key_len;
148 	uint32_t iv_len;
149 };
150 
151 struct sha_interim_data {
152 	enum fips_sha_test_types test_type;
153 	enum rte_crypto_auth_algorithm algo;
154 };
155 
156 struct fips_test_interim_info {
157 	FILE *fp_rd;
158 	FILE *fp_wr;
159 	enum file_types file_type;
160 	enum fips_test_algorithms algo;
161 	char *one_line_text;
162 	char *vec[MAX_LINE_PER_VECTOR];
163 	uint32_t nb_vec_lines;
164 	char device_name[MAX_STRING_SIZE];
165 	char file_name[MAX_STRING_SIZE];
166 
167 	union {
168 		struct aesavs_interim_data aes_data;
169 		struct hmac_interim_data hmac_data;
170 		struct tdes_interim_data tdes_data;
171 		struct ccm_interim_data ccm_data;
172 		struct sha_interim_data sha_data;
173 	} interim_info;
174 
175 	enum fips_test_op op;
176 
177 	const struct fips_test_callback *callbacks;
178 	const struct fips_test_callback *interim_callbacks;
179 	const struct fips_test_callback *writeback_callbacks;
180 
181 	post_prcess_t parse_writeback;
182 	post_prcess_t kat_check;
183 };
184 
185 extern struct fips_test_vector vec;
186 extern struct fips_test_interim_info info;
187 
188 int
189 fips_test_init(const char *req_file_path, const char *rsp_file_path,
190 		const char *device_name);
191 
192 void
193 fips_test_clear(void);
194 
195 int
196 fips_test_fetch_one_block(void);
197 
198 int
199 fips_test_parse_one_case(void);
200 
201 void
202 fips_test_write_one_case(void);
203 
204 int
205 parse_test_aes_init(void);
206 
207 int
208 parse_test_tdes_init(void);
209 
210 int
211 parse_test_hmac_init(void);
212 
213 int
214 parse_test_gcm_init(void);
215 
216 int
217 parse_test_cmac_init(void);
218 
219 int
220 parse_test_ccm_init(void);
221 
222 int
223 parse_test_sha_init(void);
224 
225 int
226 parser_read_uint8_hex(uint8_t *value, const char *p);
227 
228 int
229 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val);
230 
231 int
232 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val);
233 
234 int
235 parser_read_uint32_val(const char *key, char *src, struct fips_val *val);
236 
237 int
238 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val);
239 
240 int
241 parser_read_uint32(uint32_t *value, char *p);
242 
243 int
244 parser_read_uint32_val(const char *key, char *src, struct fips_val *val);
245 
246 int
247 writeback_hex_str(const char *key, char *dst, struct fips_val *val);
248 
249 void
250 parse_write_hex_str(struct fips_val *src);
251 
252 int
253 update_info_vec(uint32_t count);
254 
255 #endif
256