1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2021 Broadcom
13 #include "hcapi_cfa_defs.h"
15 static uint32_t hcapi_cfa_lkup_lkup3_init_cfg;
16 static uint32_t hcapi_cfa_lkup_em_seed_mem[HCAPI_CFA_LKUP_SEED_MEM_SIZE];
17 static bool hcapi_cfa_lkup_init;
19 static void hcapi_cfa_seeds_init(void)
24 if (hcapi_cfa_lkup_init)
27 hcapi_cfa_lkup_init = true;
29 /* Initialize the lfsr */
32 /* RX and TX use the same seed values */
33 hcapi_cfa_lkup_lkup3_init_cfg = rand32();
35 for (i = 0; i < HCAPI_CFA_LKUP_SEED_MEM_SIZE / 2; i++) {
37 hcapi_cfa_lkup_em_seed_mem[i * 2] = r;
39 hcapi_cfa_lkup_em_seed_mem[i * 2 + 1] = (r & 0x1);
43 static uint32_t hcapi_cfa_crc32_hash(uint8_t *key)
51 /* Do byte-wise XOR of the 52-byte HASH key first. */
55 for (i = CFA_P58_EEM_KEY_MAX_SIZE - 2; i >= 0; i--) {
56 index = index ^ *kptr;
61 val1 = hcapi_cfa_lkup_em_seed_mem[index * 2];
62 val2 = hcapi_cfa_lkup_em_seed_mem[index * 2 + 1];
64 temp[3] = (uint8_t)(val1 >> 24);
65 temp[2] = (uint8_t)(val1 >> 16);
66 temp[1] = (uint8_t)(val1 >> 8);
67 temp[0] = (uint8_t)(val1 & 0xff);
72 val1 = hcapi_cfa_crc32i(~val1, temp, 4);
74 val1 = hcapi_cfa_crc32i(~val1,
75 (key - (CFA_P58_EEM_KEY_MAX_SIZE - 1)),
76 CFA_P58_EEM_KEY_MAX_SIZE);
80 val1 = hcapi_cfa_crc32i(~val1, temp, 4);
85 static uint32_t hcapi_cfa_lookup3_hash(uint8_t *in_key)
89 val1 = hashword(((uint32_t *)in_key) + 1,
90 CFA_P58_EEM_KEY_MAX_SIZE / (sizeof(uint32_t)),
91 hcapi_cfa_lkup_lkup3_init_cfg);
97 /** Approximation of HCAPI hcapi_cfa_key_hash()
102 uint64_t hcapi_cfa_p58_key_hash(uint64_t *key_data,
109 * Init the seeds if needed
111 if (!hcapi_cfa_lkup_init)
112 hcapi_cfa_seeds_init();
114 key0_hash = hcapi_cfa_crc32_hash(((uint8_t *)key_data) +
117 key1_hash = hcapi_cfa_lookup3_hash((uint8_t *)key_data);
119 return ((uint64_t)key0_hash) << 32 | (uint64_t)key1_hash;