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