examples/fips_validation: implement JSON CMAC test
[dpdk.git] / examples / fips_validation / fips_validation.h
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 RTE_HAS_JANSSON
9 #include <jansson.h>
10 #endif /* RTE_HAS_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_GCM,
38                 FIPS_TEST_ALGO_AES_CMAC,
39                 FIPS_TEST_ALGO_AES_CCM,
40                 FIPS_TEST_ALGO_HMAC,
41                 FIPS_TEST_ALGO_TDES,
42                 FIPS_TEST_ALGO_SHA,
43                 FIPS_TEST_ALGO_AES_XTS,
44                 FIPS_TEST_ALGO_MAX
45 };
46
47 enum file_types {
48         FIPS_TYPE_REQ = 1,
49         FIPS_TYPE_FAX,
50         FIPS_TYPE_RSP,
51         FIPS_TYPE_JSON,
52 };
53
54 enum fips_test_op {
55         FIPS_TEST_ENC_AUTH_GEN = 1,
56         FIPS_TEST_DEC_AUTH_VERIF,
57 };
58
59 #define MAX_LINE_PER_VECTOR            16
60
61 struct fips_val {
62         uint8_t *val;
63         uint32_t len;
64 };
65
66 struct fips_test_vector {
67         union {
68                 struct {
69                         struct fips_val key;
70                         struct fips_val digest;
71                         struct fips_val auth_aad;
72                         struct fips_val aad;
73                 } cipher_auth;
74                 struct {
75                         struct fips_val key;
76                         struct fips_val digest;
77                         struct fips_val aad;
78                 } aead;
79         };
80
81         struct fips_val pt;
82         struct fips_val ct;
83         struct fips_val iv;
84
85         enum rte_crypto_op_status status;
86 };
87
88 typedef int (*post_prcess_t)(struct fips_val *val);
89
90 typedef int (*parse_callback_t)(const char *key, char *text,
91                 struct fips_val *val);
92
93 struct fips_test_callback {
94         const char *key;
95         parse_callback_t cb;
96         struct fips_val *val;
97 };
98
99 enum fips_aesavs_test_types {
100         AESAVS_TYPE_GFXBOX = 1,
101         AESAVS_TYPE_KEYSBOX,
102         AESAVS_TYPE_VARKEY,
103         AESAVS_TYPE_VARTXT,
104         AESAVS_TYPE_MMT,
105         AESAVS_TYPE_MCT,
106 };
107
108 enum fips_tdes_test_types {
109         TDES_INVERSE_PERMUTATION = 0,
110         TDES_PERMUTATION,
111         TDES_SUBSTITUTION_TABLE,
112         TDES_VARIABLE_KEY,
113         TDES_VARIABLE_TEXT,
114         TDES_KAT,
115         TDES_MCT, /* Monte Carlo (Modes) Test */
116         TDES_MMT /* Multi block Message Test */
117 };
118
119 enum fips_tdes_test_mode {
120         TDES_MODE_CBC = 0,
121         TDES_MODE_ECB
122 };
123
124 enum fips_ccm_test_types {
125         CCM_VADT        = 1, /* Variable Associated Data Test */
126         CCM_VPT,                 /* Variable Payload Test */
127         CCM_VNT,                 /* Variable Nonce Test */
128         CCM_VTT,                 /* Variable Tag Test */
129         CCM_DVPT,        /*  Decryption-Verification Process Test */
130 };
131
132 enum fips_sha_test_types {
133         SHA_KAT = 0,
134         SHA_MCT
135 };
136
137 struct aesavs_interim_data {
138         enum fips_aesavs_test_types test_type;
139         uint32_t cipher_algo;
140         uint32_t key_len;
141 };
142
143 struct hmac_interim_data {
144         enum rte_crypto_auth_algorithm algo;
145 };
146
147 struct tdes_interim_data {
148         enum fips_tdes_test_types test_type;
149         enum fips_tdes_test_mode test_mode;
150         uint32_t nb_keys;
151 };
152
153 struct ccm_interim_data {
154         enum fips_ccm_test_types test_type;
155         uint32_t aad_len;
156         uint32_t pt_len;
157         uint32_t digest_len;
158         uint32_t key_len;
159         uint32_t iv_len;
160 };
161
162 struct sha_interim_data {
163         enum fips_sha_test_types test_type;
164         enum rte_crypto_auth_algorithm algo;
165 };
166
167 struct gcm_interim_data {
168         uint8_t is_gmac;
169         uint8_t gen_iv;
170 };
171
172 #ifdef RTE_HAS_JANSSON
173 struct fips_test_json_info {
174         /* Information used for reading from json */
175         json_t *json_root;
176         json_t *json_vector_set;
177         json_t *json_test_group;
178         json_t *json_test_case;
179         /* Location of json write output */
180         json_t *json_write_root;
181         json_t *json_write_group;
182         json_t *json_write_set;
183         json_t *json_write_case;
184         /* Other info */
185         uint8_t is_sample;
186 };
187 #endif /* RTE_HAS_JANSSON */
188
189 struct fips_test_interim_info {
190         FILE *fp_rd;
191         FILE *fp_wr;
192         enum file_types file_type;
193         enum fips_test_algorithms algo;
194         char *one_line_text;
195         char *vec[MAX_LINE_PER_VECTOR];
196         uint32_t vec_start_off;
197         uint32_t nb_vec_lines;
198         char device_name[MAX_STRING_SIZE];
199         char file_name[MAX_FILE_NAME_SIZE];
200         float version;
201
202         union {
203                 struct aesavs_interim_data aes_data;
204                 struct hmac_interim_data hmac_data;
205                 struct tdes_interim_data tdes_data;
206                 struct ccm_interim_data ccm_data;
207                 struct sha_interim_data sha_data;
208                 struct gcm_interim_data gcm_data;
209         } interim_info;
210
211         enum fips_test_op op;
212
213         const struct fips_test_callback *callbacks;
214         const struct fips_test_callback *interim_callbacks;
215         const struct fips_test_callback *writeback_callbacks;
216
217         post_prcess_t parse_writeback;
218         post_prcess_t kat_check;
219 };
220
221 extern struct fips_test_vector vec;
222 extern struct fips_test_interim_info info;
223
224 #ifdef RTE_HAS_JANSSON
225 extern struct fips_test_json_info json_info;
226 #endif /* RTE_HAS_JANSSON */
227
228 int
229 fips_test_init(const char *req_file_path, const char *rsp_file_path,
230                 const char *device_name);
231
232 void
233 fips_test_clear(void);
234
235 int
236 fips_test_fetch_one_block(void);
237
238 int
239 fips_test_parse_one_case(void);
240
241 void
242 fips_test_write_one_case(void);
243
244 #ifdef RTE_HAS_JANSSON
245 int
246 fips_test_parse_one_json_vector_set(void);
247
248 int
249 fips_test_parse_one_json_group(void);
250
251 int
252 fips_test_parse_one_json_case(void);
253
254 int
255 parse_test_gcm_json_init(void);
256
257 int
258 parse_test_hmac_json_init(void);
259
260 int
261 parse_test_hmac_json_algorithm(void);
262
263 int
264 parse_test_cmac_json_init(void);
265 #endif /* RTE_HAS_JANSSON */
266
267 int
268 parse_test_aes_init(void);
269
270 int
271 parse_test_tdes_init(void);
272
273 int
274 parse_test_hmac_init(void);
275
276 int
277 parse_test_gcm_init(void);
278
279 int
280 parse_test_cmac_init(void);
281
282 int
283 parse_test_ccm_init(void);
284
285 int
286 parse_test_sha_init(void);
287
288 int
289 parse_test_xts_init(void);
290
291 int
292 parser_read_uint8_hex(uint8_t *value, const char *p);
293
294 int
295 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val);
296
297 int
298 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val);
299
300 int
301 parser_read_uint16(uint16_t *value, const char *p);
302
303 int
304 parser_read_uint32_val(const char *key, char *src, struct fips_val *val);
305
306 int
307 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val);
308
309 int
310 parser_read_uint32(uint32_t *value, char *p);
311
312 int
313 parser_read_uint32_val(const char *key, char *src, struct fips_val *val);
314
315 int
316 writeback_hex_str(const char *key, char *dst, struct fips_val *val);
317
318 void
319 parse_write_hex_str(struct fips_val *src);
320
321 int
322 update_info_vec(uint32_t count);
323
324 typedef int (*fips_test_one_case_t)(void);
325 typedef int (*fips_prepare_op_t)(void);
326 typedef int (*fips_prepare_xform_t)(struct rte_crypto_sym_xform *);
327
328 struct fips_test_ops {
329         fips_prepare_xform_t prepare_xform;
330         fips_prepare_op_t prepare_op;
331         fips_test_one_case_t test;
332 };
333
334 extern struct fips_test_ops test_ops;
335
336 int prepare_aead_op(void);
337
338 int prepare_auth_op(void);
339
340 int prepare_gcm_xform(struct rte_crypto_sym_xform *xform);
341
342 int prepare_gmac_xform(struct rte_crypto_sym_xform *xform);
343
344 #endif