Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause 2 : : * Copyright(c) 2019-2021 Broadcom 3 : : * All rights reserved. 4 : : */ 5 : : 6 : : #include <stdint.h> 7 : : #include <stdlib.h> 8 : : #include <stdbool.h> 9 : : #include <string.h> 10 : : #include "lookup3.h" 11 : : #include "rand.h" 12 : : 13 : : #include "hcapi_cfa_defs.h" 14 : : 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; 18 : : 19 : 0 : static void hcapi_cfa_seeds_init(void) 20 : : { 21 : : int i; 22 : : uint32_t r; 23 : : 24 [ # # ]: 0 : if (hcapi_cfa_lkup_init) 25 : : return; 26 : : 27 : 0 : hcapi_cfa_lkup_init = true; 28 : : 29 : : /* Initialize the lfsr */ 30 : 0 : rand_init(); 31 : : 32 : : /* RX and TX use the same seed values */ 33 : 0 : hcapi_cfa_lkup_lkup3_init_cfg = rand32(); 34 : : 35 [ # # ]: 0 : for (i = 0; i < HCAPI_CFA_LKUP_SEED_MEM_SIZE / 2; i++) { 36 : 0 : r = rand32(); 37 : 0 : hcapi_cfa_lkup_em_seed_mem[i * 2] = r; 38 : 0 : r = rand32(); 39 : 0 : hcapi_cfa_lkup_em_seed_mem[i * 2 + 1] = (r & 0x1); 40 : : } 41 : : } 42 : : 43 : 0 : static uint32_t hcapi_cfa_crc32_hash(uint8_t *key) 44 : : { 45 : : int i; 46 : : uint32_t index; 47 : : uint32_t val1, val2; 48 : : uint8_t temp[4]; 49 : : uint8_t *kptr = key; 50 : : 51 : : /* Do byte-wise XOR of the 52-byte HASH key first. */ 52 : 0 : index = *key; 53 : 0 : kptr--; 54 : : 55 [ # # ]: 0 : for (i = CFA_P58_EEM_KEY_MAX_SIZE - 2; i >= 0; i--) { 56 : 0 : index = index ^ *kptr; 57 : 0 : kptr--; 58 : : } 59 : : 60 : : /* Get seeds */ 61 : 0 : val1 = hcapi_cfa_lkup_em_seed_mem[index * 2]; 62 : 0 : val2 = hcapi_cfa_lkup_em_seed_mem[index * 2 + 1]; 63 : : 64 : 0 : temp[3] = (uint8_t)(val1 >> 24); 65 : 0 : temp[2] = (uint8_t)(val1 >> 16); 66 : 0 : temp[1] = (uint8_t)(val1 >> 8); 67 : 0 : temp[0] = (uint8_t)(val1 & 0xff); 68 : : val1 = 0; 69 : : 70 : : /* Start with seed */ 71 [ # # ]: 0 : if (!(val2 & 0x1)) 72 : 0 : val1 = hcapi_cfa_crc32i(~val1, temp, 4); 73 : : 74 : 0 : val1 = hcapi_cfa_crc32i(~val1, 75 : 0 : (key - (CFA_P58_EEM_KEY_MAX_SIZE - 1)), 76 : : CFA_P58_EEM_KEY_MAX_SIZE); 77 : : 78 : : /* End with seed */ 79 [ # # ]: 0 : if (val2 & 0x1) 80 : 0 : val1 = hcapi_cfa_crc32i(~val1, temp, 4); 81 : : 82 : 0 : return val1; 83 : : } 84 : : 85 : : static uint32_t hcapi_cfa_lookup3_hash(uint8_t *in_key) 86 : : { 87 : : uint32_t val1; 88 : : 89 : 0 : val1 = hashword(((uint32_t *)in_key), 90 : : CFA_P58_EEM_KEY_MAX_SIZE / (sizeof(uint32_t)), 91 : : hcapi_cfa_lkup_lkup3_init_cfg); 92 : : 93 : : return val1; 94 : : } 95 : : 96 : : 97 : : /** Approximation of HCAPI hcapi_cfa_key_hash() 98 : : * 99 : : * Return: 100 : : * 101 : : */ 102 : 0 : uint64_t hcapi_cfa_p58_key_hash(uint64_t *key_data, 103 : : uint16_t bitlen) 104 : : { 105 : : uint32_t key0_hash; 106 : : uint32_t key1_hash; 107 : : 108 : : /* 109 : : * Init the seeds if needed 110 : : */ 111 [ # # ]: 0 : if (!hcapi_cfa_lkup_init) 112 : 0 : hcapi_cfa_seeds_init(); 113 : : 114 : 0 : key0_hash = hcapi_cfa_crc32_hash(((uint8_t *)key_data) + 115 : 0 : (bitlen / 8) - 1); 116 : : 117 : : key1_hash = hcapi_cfa_lookup3_hash((uint8_t *)key_data); 118 : : 119 : 0 : return ((uint64_t)key0_hash) << 32 | (uint64_t)key1_hash; 120 : : }