P4C
The P4 Compiler
Loading...
Searching...
No Matches
bfn_hash_algorithm.h
1#ifndef BACKENDS_TOFINO_BF_UTILS_INCLUDE_DYNAMIC_HASH_BFN_HASH_ALGORITHM_H_
2#define BACKENDS_TOFINO_BF_UTILS_INCLUDE_DYNAMIC_HASH_BFN_HASH_ALGORITHM_H_
3
4#ifndef __KERNEL__
5#include <stdbool.h>
6#include <stdint.h>
7#include <string.h>
8#endif
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14#define BF_UTILS_ALGO_NAME_LEN 30
15
16typedef enum {
17 CRC_8,
18 CRC_8_DARC,
19 CRC_8_I_CODE,
20 CRC_8_ITU,
21 CRC_8_MAXIM,
22 CRC_8_ROHC,
23 CRC_8_WCDMA,
24 CRC_16,
25 CRC_16_BYPASS,
26 CRC_16_DDS_110,
27 CRC_16_DECT,
28 CRC_16_DECT_R,
29 CRC_16_DECT_X,
30 CRC_16_DNP,
31 CRC_16_EN_13757,
32 CRC_16_GENIBUS,
33 CRC_16_MAXIM,
34 CRC_16_MCRF4XX,
35 CRC_16_RIELLO,
36 CRC_16_T10_DIF,
37 CRC_16_TELEDISK,
38 CRC_16_USB,
39 X_25,
40 XMODEM,
41 MODBUS,
42 KERMIT,
43 CRC_CCITT_FALSE,
44 CRC_AUG_CCITT,
45 CRC_32,
46 CRC_32_BZIP2,
47 CRC_32C,
48 CRC_32D,
49 CRC_32_MPEG,
50 POSIX,
51 CRC_32Q,
52 JAMCRC,
53 XFER,
54 CRC_64,
55 CRC_64_GO_ISO,
56 CRC_64_WE,
57 CRC_64_JONES,
58 CRC_INVALID
59} bfn_crc_alg_t;
60
61typedef enum { IDENTITY_DYN, CRC_DYN, RANDOM_DYN, XOR_DYN, INVALID_DYN } bfn_hash_alg_type_t;
62
63static inline void crc_alg_type_to_str(bfn_crc_alg_t crc_type, char *crc_name) {
64 if (!crc_name) {
65 return;
66 }
67 switch (crc_type) {
68 case CRC_8:
69 strncpy(crc_name, "CRC_8\0", BF_UTILS_ALGO_NAME_LEN);
70 break;
71 case CRC_8_DARC:
72 strncpy(crc_name, "CRC_8_DARC\0", BF_UTILS_ALGO_NAME_LEN);
73 break;
74 case CRC_8_I_CODE:
75 strncpy(crc_name, "CRC_8_I_CODE\0", BF_UTILS_ALGO_NAME_LEN);
76 break;
77 case CRC_8_ITU:
78 strncpy(crc_name, "CRC_8_ITU\0", BF_UTILS_ALGO_NAME_LEN);
79 break;
80 case CRC_8_MAXIM:
81 strncpy(crc_name, "CRC_8_MAXIM\0", BF_UTILS_ALGO_NAME_LEN);
82 break;
83 case CRC_8_ROHC:
84 strncpy(crc_name, "CRC_8_ROHC\0", BF_UTILS_ALGO_NAME_LEN);
85 break;
86 case CRC_8_WCDMA:
87 strncpy(crc_name, "CRC_8_WCDMA\0", BF_UTILS_ALGO_NAME_LEN);
88 break;
89 case CRC_16:
90 strncpy(crc_name, "CRC_16\0", BF_UTILS_ALGO_NAME_LEN);
91 break;
92 case CRC_16_BYPASS:
93 strncpy(crc_name, "CRC_16_BYPASS\0", BF_UTILS_ALGO_NAME_LEN);
94 break;
95 case CRC_16_DDS_110:
96 strncpy(crc_name, "CRC_16_DDS_110\0", BF_UTILS_ALGO_NAME_LEN);
97 break;
98 case CRC_16_DECT:
99 strncpy(crc_name, "CRC_16_DECT\0", BF_UTILS_ALGO_NAME_LEN);
100 break;
101 case CRC_16_DECT_R:
102 strncpy(crc_name, "CRC_16_DECT_R\0", BF_UTILS_ALGO_NAME_LEN);
103 break;
104 case CRC_16_DECT_X:
105 strncpy(crc_name, "CRC_16_DECT_X\0", BF_UTILS_ALGO_NAME_LEN);
106 break;
107 case CRC_16_DNP:
108 strncpy(crc_name, "CRC_16_DNP\0", BF_UTILS_ALGO_NAME_LEN);
109 break;
110 case CRC_16_EN_13757:
111 strncpy(crc_name, "CRC_16_EN_13757\0", BF_UTILS_ALGO_NAME_LEN);
112 break;
113 case CRC_16_GENIBUS:
114 strncpy(crc_name, "CRC_16_GENIBUS\0", BF_UTILS_ALGO_NAME_LEN);
115 break;
116 case CRC_16_MAXIM:
117 strncpy(crc_name, "CRC_16_MAXIM\0", BF_UTILS_ALGO_NAME_LEN);
118 break;
119 case CRC_16_MCRF4XX:
120 strncpy(crc_name, "CRC_16_MCRF4XX\0", BF_UTILS_ALGO_NAME_LEN);
121 break;
122 case CRC_16_RIELLO:
123 strncpy(crc_name, "CRC_16_RIELLO\0", BF_UTILS_ALGO_NAME_LEN);
124 break;
125 case CRC_16_T10_DIF:
126 strncpy(crc_name, "CRC_16_T10_DIF\0", BF_UTILS_ALGO_NAME_LEN);
127 break;
128 case CRC_16_TELEDISK:
129 strncpy(crc_name, "CRC_16_TELEDISK\0", BF_UTILS_ALGO_NAME_LEN);
130 break;
131 case CRC_16_USB:
132 strncpy(crc_name, "CRC_16_USB\0", BF_UTILS_ALGO_NAME_LEN);
133 break;
134 case X_25:
135 strncpy(crc_name, "X_25\0", BF_UTILS_ALGO_NAME_LEN);
136 break;
137 case XMODEM:
138 strncpy(crc_name, "XMODEM\0", BF_UTILS_ALGO_NAME_LEN);
139 break;
140 case MODBUS:
141 strncpy(crc_name, "MODBUS\0", BF_UTILS_ALGO_NAME_LEN);
142 break;
143 case KERMIT:
144 strncpy(crc_name, "KERMIT\0", BF_UTILS_ALGO_NAME_LEN);
145 break;
146 case CRC_CCITT_FALSE:
147 strncpy(crc_name, "CRC_CCITT_FALSE\0", BF_UTILS_ALGO_NAME_LEN);
148 break;
149 case CRC_AUG_CCITT:
150 strncpy(crc_name, "CRC_AUG_CCITT\0", BF_UTILS_ALGO_NAME_LEN);
151 break;
152 case CRC_32:
153 strncpy(crc_name, "CRC_32\0", BF_UTILS_ALGO_NAME_LEN);
154 break;
155 case CRC_32_BZIP2:
156 strncpy(crc_name, "CRC_32_BZIP2\0", BF_UTILS_ALGO_NAME_LEN);
157 break;
158 case CRC_32C:
159 strncpy(crc_name, "CRC_32C\0", BF_UTILS_ALGO_NAME_LEN);
160 break;
161 case CRC_32D:
162 strncpy(crc_name, "CRC_32D\0", BF_UTILS_ALGO_NAME_LEN);
163 break;
164 case CRC_32_MPEG:
165 strncpy(crc_name, "CRC_32_MPEG\0", BF_UTILS_ALGO_NAME_LEN);
166 break;
167 case POSIX:
168 strncpy(crc_name, "POSIX\0", BF_UTILS_ALGO_NAME_LEN);
169 break;
170 case CRC_32Q:
171 strncpy(crc_name, "CRC_32Q\0", BF_UTILS_ALGO_NAME_LEN);
172 break;
173 case JAMCRC:
174 strncpy(crc_name, "JAMCRC\0", BF_UTILS_ALGO_NAME_LEN);
175 break;
176 case XFER:
177 strncpy(crc_name, "XFER\0", BF_UTILS_ALGO_NAME_LEN);
178 break;
179 case CRC_64:
180 strncpy(crc_name, "CRC_64\0", BF_UTILS_ALGO_NAME_LEN);
181 break;
182 case CRC_64_GO_ISO:
183 strncpy(crc_name, "CRC_64_GO_ISO\0", BF_UTILS_ALGO_NAME_LEN);
184 break;
185 case CRC_64_WE:
186 strncpy(crc_name, "CRC_64_WE\0", BF_UTILS_ALGO_NAME_LEN);
187 break;
188 case CRC_64_JONES:
189 strncpy(crc_name, "CRC_64_JONES\0", BF_UTILS_ALGO_NAME_LEN);
190 break;
191 case CRC_INVALID:
192 default:
193 strncpy(crc_name, "CRC_INVALID\0", BF_UTILS_ALGO_NAME_LEN);
194 break;
195 }
196 return;
197}
198
199static inline bfn_crc_alg_t crc_alg_str_to_type(const char *crc_name) {
200 if (crc_name == NULL) {
201 return CRC_INVALID;
202 }
203 if (!strcmp(crc_name, "crc_8") || !strcmp(crc_name, "CRC_8")) {
204 return CRC_8;
205 }
206 if (!strcmp(crc_name, "crc_8_darc") || !strcmp(crc_name, "CRC_8_DARC")) {
207 return CRC_8_DARC;
208 }
209 if (!strcmp(crc_name, "crc_8_i_code") || !strcmp(crc_name, "CRC_8_I_CODE")) {
210 return CRC_8_I_CODE;
211 }
212 if (!strcmp(crc_name, "crc_8_itu") || !strcmp(crc_name, "CRC_8_ITU")) {
213 return CRC_8_ITU;
214 }
215 if (!strcmp(crc_name, "crc_8_maxim") || !strcmp(crc_name, "CRC_8_MAXIM")) {
216 return CRC_8_MAXIM;
217 }
218 if (!strcmp(crc_name, "crc_8_rohc") || !strcmp(crc_name, "CRC_8_ROHC")) {
219 return CRC_8_ROHC;
220 }
221 if (!strcmp(crc_name, "crc_8_wcdma") || !strcmp(crc_name, "CRC_8_WCDMA")) {
222 return CRC_8_WCDMA;
223 }
224 if (!strcmp(crc_name, "crc_16") || !strcmp(crc_name, "CRC_16")) {
225 return CRC_16;
226 }
227 if (!strcmp(crc_name, "crc_16_bypass") || !strcmp(crc_name, "CRC_16_BYPASS")) {
228 return CRC_16_BYPASS;
229 }
230 if (!strcmp(crc_name, "crc_16_dds_110") || !strcmp(crc_name, "CRC_16_DDS_110")) {
231 return CRC_16_DDS_110;
232 }
233 if (!strcmp(crc_name, "crc_16_dect") || !strcmp(crc_name, "CRC_16_DECT")) {
234 return CRC_16_DECT;
235 }
236 if (!strcmp(crc_name, "crc_16_dect_r") || !strcmp(crc_name, "CRC_16_DECT_R")) {
237 return CRC_16_DECT_R;
238 }
239 if (!strcmp(crc_name, "crc_16_dect_x") || !strcmp(crc_name, "CRC_16_DECT_X")) {
240 return CRC_16_DECT_X;
241 }
242 if (!strcmp(crc_name, "crc_16_dnp") || !strcmp(crc_name, "CRC_16_DNP")) {
243 return CRC_16_DNP;
244 }
245 if (!strcmp(crc_name, "crc_16_en_13757") || !strcmp(crc_name, "CRC_16_EN_13757")) {
246 return CRC_16_EN_13757;
247 }
248 if (!strcmp(crc_name, "crc_16_genibus") || !strcmp(crc_name, "CRC_16_GENIBUS")) {
249 return CRC_16_GENIBUS;
250 }
251 if (!strcmp(crc_name, "crc_16_maxim") || !strcmp(crc_name, "CRC_16_MAXIM")) {
252 return CRC_16_MAXIM;
253 }
254 if (!strcmp(crc_name, "crc_16_mcrf4xx") || !strcmp(crc_name, "CRC_16_MCRF4XX")) {
255 return CRC_16_MCRF4XX;
256 }
257 if (!strcmp(crc_name, "crc_16_riello") || !strcmp(crc_name, "CRC_16_RIELLO")) {
258 return CRC_16_RIELLO;
259 }
260 if (!strcmp(crc_name, "crc_16_t10_dif") || !strcmp(crc_name, "CRC_16_T10_DIF")) {
261 return CRC_16_T10_DIF;
262 }
263 if (!strcmp(crc_name, "crc_16_teledisk") || !strcmp(crc_name, "CRC_16_TELEDISK")) {
264 return CRC_16_TELEDISK;
265 }
266 if (!strcmp(crc_name, "crc_16_usb") || !strcmp(crc_name, "CRC_16_USB")) {
267 return CRC_16_USB;
268 }
269 if (!strcmp(crc_name, "x_25") || !strcmp(crc_name, "X_25")) {
270 return X_25;
271 }
272 if (!strcmp(crc_name, "xmodem") || !strcmp(crc_name, "XMODEM")) {
273 return XMODEM;
274 }
275 if (!strcmp(crc_name, "modbus") || !strcmp(crc_name, "MODBUS")) {
276 return MODBUS;
277 }
278 if (!strcmp(crc_name, "kermit") || !strcmp(crc_name, "KERMIT")) {
279 return KERMIT;
280 }
281 if (!strcmp(crc_name, "crc_ccitt_false") || !strcmp(crc_name, "CRC_CCITT_FALSE")) {
282 return CRC_CCITT_FALSE;
283 }
284 if (!strcmp(crc_name, "crc_aug_ccitt") || !strcmp(crc_name, "CRC_AUG_CCITT")) {
285 return CRC_AUG_CCITT;
286 }
287 if (!strcmp(crc_name, "crc_32") || !strcmp(crc_name, "CRC_32")) {
288 return CRC_32;
289 }
290 if (!strcmp(crc_name, "crc_32_bzip2") || !strcmp(crc_name, "CRC_32_BZIP2")) {
291 return CRC_32_BZIP2;
292 }
293 if (!strcmp(crc_name, "crc_32c") || !strcmp(crc_name, "CRC_32C")) {
294 return CRC_32C;
295 }
296 if (!strcmp(crc_name, "crc_32d") || !strcmp(crc_name, "CRC_32D")) {
297 return CRC_32D;
298 }
299 if (!strcmp(crc_name, "crc_32_mpeg") || !strcmp(crc_name, "CRC_32_MPEG")) {
300 return CRC_32_MPEG;
301 }
302 if (!strcmp(crc_name, "posix") || !strcmp(crc_name, "POSIX")) {
303 return POSIX;
304 }
305 if (!strcmp(crc_name, "crc_32q") || !strcmp(crc_name, "CRC_32Q")) {
306 return CRC_32Q;
307 }
308 if (!strcmp(crc_name, "jamcrc") || !strcmp(crc_name, "JAMCRC")) {
309 return JAMCRC;
310 }
311 if (!strcmp(crc_name, "xfer") || !strcmp(crc_name, "XFER")) {
312 return XFER;
313 }
314 if (!strcmp(crc_name, "crc_64") || !strcmp(crc_name, "CRC_64")) {
315 return CRC_64;
316 }
317 if (!strcmp(crc_name, "crc_64_go_iso") || !strcmp(crc_name, "CRC_64_GO_ISO")) {
318 return CRC_64_GO_ISO;
319 }
320 if (!strcmp(crc_name, "crc_64_we") || !strcmp(crc_name, "CRC_64_WE")) {
321 return CRC_64_WE;
322 }
323 if (!strcmp(crc_name, "crc_64_jones") || !strcmp(crc_name, "CRC_64_JONES")) {
324 return CRC_64_JONES;
325 }
326 return CRC_INVALID;
327}
328
329static inline void hash_alg_type_to_str(bfn_hash_alg_type_t alg_type, char *alg_name) {
330 if (alg_name == NULL) {
331 return;
332 }
333 switch (alg_type) {
334 case IDENTITY_DYN:
335 strncpy(alg_name, "IDENTITY\0", BF_UTILS_ALGO_NAME_LEN);
336 break;
337 case CRC_DYN:
338 strncpy(alg_name, "CRC\0", BF_UTILS_ALGO_NAME_LEN);
339 break;
340 case RANDOM_DYN:
341 strncpy(alg_name, "RANDOM\0", BF_UTILS_ALGO_NAME_LEN);
342 break;
343 case XOR_DYN:
344 strncpy(alg_name, "XOR\0", BF_UTILS_ALGO_NAME_LEN);
345 break;
346 case INVALID_DYN:
347 default:
348 strncpy(alg_name, "INVALID\0", BF_UTILS_ALGO_NAME_LEN);
349 break;
350 }
351 return;
352}
353
354static inline bfn_hash_alg_type_t hash_alg_str_to_type(const char *alg_name) {
355 if (alg_name == NULL) {
356 return INVALID_DYN;
357 }
358 if (!strcmp(alg_name, "identity") || !strcmp(alg_name, "IDENTITY")) {
359 return IDENTITY_DYN;
360 }
361 if (!strcmp(alg_name, "crc") || !strcmp(alg_name, "CRC")) {
362 return CRC_DYN;
363 }
364 if (!strcmp(alg_name, "random") || !strcmp(alg_name, "RANDOM")) {
365 return RANDOM_DYN;
366 }
367 if (!strcmp(alg_name, "xor") || !strcmp(alg_name, "XOR")) {
368 return XOR_DYN;
369 }
370 return INVALID_DYN;
371}
372
377typedef struct crc_alg_info_ {
378 char *crc_name;
379 uint64_t poly;
380 bool reverse;
381 uint64_t init;
382 uint64_t final_xor;
384
385typedef struct bfn_hash_algorithm_ {
386 bfn_hash_alg_type_t hash_alg;
387 bool msb;
388 bool extend;
389
390 /* -- All fields below are algorithm dependent. Most of them
391 * has meaning just for the CRC algorithm. */
392 bfn_crc_alg_t crc_type; // CRC, will be Invalid if unknown crc
393 int hash_bit_width; // CRC, XOR
394 bool reverse; // CRC
395 uint64_t poly; // CRC
396 uint64_t init; // CRC
397 uint64_t final_xor; // CRC
398 uint8_t **crc_matrix; // CRC
400
406bool verify_algorithm(bfn_hash_algorithm_t *alg, char **error_message);
407
418void initialize_algorithm(bfn_hash_algorithm_t *alg, bfn_hash_alg_type_t hash_alg_type, bool msb,
419 bool extend, bfn_crc_alg_t crc_alg);
420
421void initialize_crc_matrix(bfn_hash_algorithm_t *alg);
422
423void calculate_crc(bfn_hash_algorithm_t *alg, uint32_t hash_output_bits, uint8_t *stream,
424 uint32_t stream_len, uint8_t *crc);
425
426#ifdef __cplusplus
427}
428#endif
429
430#endif /* BACKENDS_TOFINO_BF_UTILS_INCLUDE_DYNAMIC_HASH_BFN_HASH_ALGORITHM_H_ */
Definition bfn_hash_algorithm.h:385
Definition bfn_hash_algorithm.h:377