xref: /isa-l_crypto/rolling_hash/rolling_hashx_internal.h (revision 1e0b122e090c8ad3a8d8c56e182f754ea26fe70d)
181c7feeeSMarcel Cornu /**********************************************************************
281c7feeeSMarcel Cornu   Copyright(c) 2024 Intel Corporation All rights reserved.
381c7feeeSMarcel Cornu 
481c7feeeSMarcel Cornu   Redistribution and use in source and binary forms, with or without
581c7feeeSMarcel Cornu   modification, are permitted provided that the following conditions
681c7feeeSMarcel Cornu   are met:
781c7feeeSMarcel Cornu     * Redistributions of source code must retain the above copyright
881c7feeeSMarcel Cornu       notice, this list of conditions and the following disclaimer.
981c7feeeSMarcel Cornu     * Redistributions in binary form must reproduce the above copyright
1081c7feeeSMarcel Cornu       notice, this list of conditions and the following disclaimer in
1181c7feeeSMarcel Cornu       the documentation and/or other materials provided with the
1281c7feeeSMarcel Cornu       distribution.
1381c7feeeSMarcel Cornu     * Neither the name of Intel Corporation nor the names of its
1481c7feeeSMarcel Cornu       contributors may be used to endorse or promote products derived
1581c7feeeSMarcel Cornu       from this software without specific prior written permission.
1681c7feeeSMarcel Cornu 
1781c7feeeSMarcel Cornu   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1881c7feeeSMarcel Cornu   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1981c7feeeSMarcel Cornu   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2081c7feeeSMarcel Cornu   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2181c7feeeSMarcel Cornu   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2281c7feeeSMarcel Cornu   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2381c7feeeSMarcel Cornu   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2481c7feeeSMarcel Cornu   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2581c7feeeSMarcel Cornu   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2681c7feeeSMarcel Cornu   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2781c7feeeSMarcel Cornu   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2881c7feeeSMarcel Cornu **********************************************************************/
2981c7feeeSMarcel Cornu 
3081c7feeeSMarcel Cornu /**
3181c7feeeSMarcel Cornu  *  @file  rolling_hashx_internal.h
3281c7feeeSMarcel Cornu  *  @brief Internal fingerprint functions based on rolling hash
3381c7feeeSMarcel Cornu  *
3481c7feeeSMarcel Cornu  *  rolling_hash2 - checks hash in a sliding window based on random 64-bit hash.
3581c7feeeSMarcel Cornu  */
3681c7feeeSMarcel Cornu 
3781c7feeeSMarcel Cornu #ifndef _ROLLING_INTERNAL_HASHX_H_
3881c7feeeSMarcel Cornu #define _ROLLING_INTERNAL_HASHX_H_
3981c7feeeSMarcel Cornu 
4081c7feeeSMarcel Cornu #ifdef __cplusplus
4181c7feeeSMarcel Cornu extern "C" {
4281c7feeeSMarcel Cornu #endif
4381c7feeeSMarcel Cornu 
4481c7feeeSMarcel Cornu #include <stdint.h>
4581c7feeeSMarcel Cornu #include "rolling_hashx.h"
4681c7feeeSMarcel Cornu 
4781c7feeeSMarcel Cornu /**
4881c7feeeSMarcel Cornu  * @brief Initialize state object for rolling hash2
4981c7feeeSMarcel Cornu  *
5081c7feeeSMarcel Cornu  * @param state Structure holding state info on current rolling hash
5181c7feeeSMarcel Cornu  * @param w     Window width (1 <= w <= 32)
5281c7feeeSMarcel Cornu  * @returns 0 - success, -1 - failure
5381c7feeeSMarcel Cornu  */
5481c7feeeSMarcel Cornu int
55*1e0b122eSMarcel Cornu _rolling_hash2_init(struct isal_rh_state2 *state, uint32_t w);
5681c7feeeSMarcel Cornu 
5781c7feeeSMarcel Cornu /**
5881c7feeeSMarcel Cornu  * @brief Reset the hash state history
5981c7feeeSMarcel Cornu  *
6081c7feeeSMarcel Cornu  * @param state Structure holding state info on current rolling hash
6181c7feeeSMarcel Cornu  * @param init_bytes Optional window size buffer to pre-init hash
6281c7feeeSMarcel Cornu  * @returns none
6381c7feeeSMarcel Cornu  */
6481c7feeeSMarcel Cornu void
65*1e0b122eSMarcel Cornu _rolling_hash2_reset(struct isal_rh_state2 *state, uint8_t *init_bytes);
6681c7feeeSMarcel Cornu 
6781c7feeeSMarcel Cornu /**
6881c7feeeSMarcel Cornu  * @brief Run rolling hash function until trigger met or max length reached
6981c7feeeSMarcel Cornu  *
7081c7feeeSMarcel Cornu  * Checks for trigger based on a random hash in a sliding window.
7181c7feeeSMarcel Cornu  * @param state   Structure holding state info on current rolling hash
7281c7feeeSMarcel Cornu  * @param buffer  Pointer to input buffer to run windowed hash on
7381c7feeeSMarcel Cornu  * @param max_len Max length to run over input
7481c7feeeSMarcel Cornu  * @param mask    Mask bits ORed with hash before test with trigger
7581c7feeeSMarcel Cornu  * @param trigger Match value to compare with windowed hash at each input byte
7681c7feeeSMarcel Cornu  * @param offset  Offset from buffer to match, set if match found
7781c7feeeSMarcel Cornu  * @returns ISAL_FINGERPRINT_RET_HIT - match found, ISAL_FINGERPRINT_RET_MAX - exceeded max length
7881c7feeeSMarcel Cornu  */
7981c7feeeSMarcel Cornu int
80*1e0b122eSMarcel Cornu _rolling_hash2_run(struct isal_rh_state2 *state, uint8_t *buffer, uint32_t max_len, uint32_t mask,
8181c7feeeSMarcel Cornu                    uint32_t trigger, uint32_t *offset);
8281c7feeeSMarcel Cornu 
8381c7feeeSMarcel Cornu /**
8481c7feeeSMarcel Cornu  * @brief Generate an appropriate mask to target mean hit rate
8581c7feeeSMarcel Cornu  *
8681c7feeeSMarcel Cornu  * @param mean  Target chunk size in bytes
8781c7feeeSMarcel Cornu  * @param shift Bits to rotate result to get independent masks
8881c7feeeSMarcel Cornu  * @returns 32-bit mask value
8981c7feeeSMarcel Cornu  */
9081c7feeeSMarcel Cornu uint32_t
9181c7feeeSMarcel Cornu _rolling_hashx_mask_gen(long mean, int shift);
9281c7feeeSMarcel Cornu 
9381c7feeeSMarcel Cornu #ifdef __cplusplus
9481c7feeeSMarcel Cornu }
9581c7feeeSMarcel Cornu #endif
9681c7feeeSMarcel Cornu 
9781c7feeeSMarcel Cornu #endif // _ROLLING_INTERNAL_HASHX_H_
98