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