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