1 /* SPDX-License-Identifier: BSD-3-Clause
7 #include <rte_pmd_dpaa2.h>
9 static unsigned int sbox(unsigned int x)
11 unsigned int a, b, c, d;
12 unsigned int oa, ob, oc, od;
19 oa = ((a & ~b & ~c & d) | (~a & b) | (~a & ~c & ~d) | (b & c)) & 0x1;
20 ob = ((a & ~b & d) | (~a & c & ~d) | (b & ~c)) & 0x1;
21 oc = ((a & ~b & c) | (a & ~b & ~d) | (~a & b & ~d) | (~a & c & ~d) |
23 od = ((a & ~b & c) | (~a & b & ~c) | (a & b & ~d) | (~a & c & d)) & 0x1;
25 return ((od << 3) | (oc << 2) | (ob << 1) | oa);
28 static unsigned int sbox_tbl[16];
30 static int pbox_tbl[16] = {5, 9, 0, 13,
35 static unsigned int mix_tbl[8][16];
37 static unsigned int stage(unsigned int input)
44 input ^= input >> 16; /* xor lower */
45 input ^= input << 16; /* move original lower to upper */
47 for (i = 0; i < 32; i += 4) /* sbox stage */
48 sbox_out |= (sbox_tbl[(input >> i) & 0xf]) << i;
51 for (i = 0; i < 16; i++)
52 pbox_out |= ((sbox_out >> i) & 0x10001) << pbox_tbl[i];
57 static unsigned int fast_stage(unsigned int input)
63 input ^= input >> 16; /* xor lower */
64 input ^= input << 16; /* move original lower to upper */
66 for (i = 0; i < 32; i += 4) /* sbox stage */
67 pbox_out |= mix_tbl[i >> 2][(input >> i) & 0xf];
72 static unsigned int fast_hash32(unsigned int x)
76 for (i = 0; i < 4; i++)
82 byte_crc32(unsigned char data /* new byte for the crc calculation */,
83 unsigned old_crc /* crc result of the last iteration */)
86 unsigned int crc, polynom = 0xedb88320;
87 /* the polynomial is built on the reversed version of
88 * the CRC polynomial with out the x64 element.
92 for (i = 0; i < 8; i++, data >>= 1)
93 crc = (crc >> 1) ^ (((crc ^ data) & 0x1) ? polynom : 0);
94 /* xor with polynomial is lsb of crc^data is 1 */
99 static unsigned int crc32_table[256];
101 static void init_crc32_table(void)
105 for (i = 0; i < 256; i++)
106 crc32_table[i] = byte_crc32((unsigned char)i, 0LL);
110 crc32_string(unsigned char *data,
111 int size, unsigned int old_crc)
117 for (i = 0; i < size; i++)
118 crc = (crc >> 8) ^ crc32_table[(crc ^ data[i]) & 0xff];
123 static void hash_init(void)
128 for (i = 0; i < 16; i++)
129 sbox_tbl[i] = sbox(i);
131 for (i = 0; i < 32; i += 4)
132 for (j = 0; j < 16; j++) {
137 unsigned int input = (0x88888888 ^ (8 << i)) | (j << i);
139 input ^= input << 16; /* (X^Y,Y) */
140 input ^= input >> 16; /* (X^Y,X) */
141 mix_tbl[i >> 2][j] = stage(input);
145 uint32_t rte_pmd_dpaa2_get_tlu_hash(uint8_t *data, int size)
152 return fast_hash32(crc32_string(data, size, 0x0));