1 /********************************************************************** 2 Copyright(c) 2011-2017 Intel Corporation All rights reserved. 3 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions 6 are met: 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above copyright 10 notice, this list of conditions and the following disclaimer in 11 the documentation and/or other materials provided with the 12 distribution. 13 * Neither the name of Intel Corporation nor the names of its 14 contributors may be used to endorse or promote products derived 15 from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 **********************************************************************/ 29 30 /** 31 * @file rolling_hashx.h 32 * @brief Fingerprint functions based on rolling hash 33 * 34 * rolling_hash2 - checks hash in a sliding window based on random 64-bit hash. 35 */ 36 37 #ifndef _ROLLING_HASHX_H_ 38 #define _ROLLING_HASHX_H_ 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif 43 44 #include <stdint.h> 45 #include "types.h" 46 47 /* 48 * Define enums from API v2.24, so applications that were using this version 49 * will still be compiled successfully. 50 * This list does not need to be extended for new definitions. 51 */ 52 #ifndef NO_COMPAT_ISAL_CRYPTO_API_2_24 53 /***** Previous hash constants and typedefs *****/ 54 #define FINGERPRINT_RET_HIT ISAL_FINGERPRINT_RET_HIT 55 #define FINGERPRINT_RET_MAX ISAL_FINGERPRINT_RET_MAX 56 #define FINGERPRINT_RET_OTHER ISAL_FINGERPRINT_RET_OTHER 57 58 #define FINGERPRINT_MAX_WINDOW ISAL_FINGERPRINT_MAX_WINDOW 59 60 #define rh_state2 isal_rh_state2 61 #endif /* !NO_COMPAT_ISAL_CRYPTO_API_2_24 */ 62 63 /** 64 *@brief rolling hash return values 65 */ 66 enum { 67 ISAL_FINGERPRINT_RET_HIT = 0, //!< Fingerprint trigger hit 68 ISAL_FINGERPRINT_RET_MAX, //!< Fingerprint max length reached before hit 69 ISAL_FINGERPRINT_RET_OTHER //!< Fingerprint function error returned 70 }; 71 72 #define ISAL_FINGERPRINT_MAX_WINDOW 48 73 74 /** 75 * @brief Context for rolling_hash2 functions 76 */ 77 struct isal_rh_state2 { 78 uint8_t history[ISAL_FINGERPRINT_MAX_WINDOW]; 79 uint64_t table1[256]; 80 uint64_t table2[256]; 81 uint64_t hash; 82 uint32_t w; 83 }; 84 85 /** 86 * @brief Initialize state object for rolling hash2 87 * 88 * @param state Structure holding state info on current rolling hash 89 * @param w Window width (1 <= w <= 32) 90 * @returns 0 - success, -1 - failure 91 * @deprecated Please use isal_rolling_hash2_init() instead. 92 */ 93 ISAL_DEPRECATED("Please use isal_rolling_hash2_init() instead") 94 int 95 rolling_hash2_init(struct isal_rh_state2 *state, uint32_t w); 96 97 /** 98 * @brief Reset the hash state history 99 * 100 * @param state Structure holding state info on current rolling hash 101 * @param init_bytes Optional window size buffer to pre-init hash 102 * @returns none 103 * @deprecated Please use isal_rolling_hash2_reset() instead. 104 */ 105 ISAL_DEPRECATED("Please use isal_rolling_hash2_reset() instead") 106 void 107 rolling_hash2_reset(struct isal_rh_state2 *state, uint8_t *init_bytes); 108 109 /** 110 * @brief Run rolling hash function until trigger met or max length reached 111 * 112 * Checks for trigger based on a random hash in a sliding window. 113 * @param state Structure holding state info on current rolling hash 114 * @param buffer Pointer to input buffer to run windowed hash on 115 * @param max_len Max length to run over input 116 * @param mask Mask bits ORed with hash before test with trigger 117 * @param trigger Match value to compare with windowed hash at each input byte 118 * @param offset Offset from buffer to match, set if match found 119 * @returns ISAL_FINGERPRINT_RET_HIT - match found, ISAL_FINGERPRINT_RET_MAX - exceeded max length 120 * @deprecated Please use isal_rolling_hash2_run() instead. 121 */ 122 ISAL_DEPRECATED("Please use isal_rolling_hash2_run() instead") 123 int 124 rolling_hash2_run(struct isal_rh_state2 *state, uint8_t *buffer, uint32_t max_len, uint32_t mask, 125 uint32_t trigger, uint32_t *offset); 126 127 /** 128 * @brief Generate an appropriate mask to target mean hit rate 129 * 130 * @param mean Target chunk size in bytes 131 * @param shift Bits to rotate result to get independent masks 132 * @returns 32-bit mask value 133 * @deprecated Please use isal_rolling_hashx_mask_gen() instead. 134 */ 135 ISAL_DEPRECATED("Please use isal_rolling_hashx_mask_gen() instead") 136 uint32_t 137 rolling_hashx_mask_gen(long mean, int shift); 138 139 /** 140 * @brief Initialize state object for rolling hash2 141 * 142 * @param[in] state Structure holding state info on current rolling hash 143 * @param[in] w Window width (1 <= w <= 32) 144 * @return Operation status 145 * @retval 0 on success 146 * @retval Non-zero \a ISAL_CRYPTO_ERR on failure 147 */ 148 int 149 isal_rolling_hash2_init(struct isal_rh_state2 *state, const uint32_t w); 150 151 /** 152 * @brief Reset the hash state history 153 * 154 * @param[in] state Structure holding state info on current rolling hash 155 * @param[in] init_bytes Optional window size buffer to pre-init hash 156 * @return Operation status 157 * @retval 0 on success 158 * @retval Non-zero \a ISAL_CRYPTO_ERR on failure 159 */ 160 int 161 isal_rolling_hash2_reset(struct isal_rh_state2 *state, const uint8_t *init_bytes); 162 163 /** 164 * @brief Run rolling hash function until trigger met or max length reached 165 * 166 * Checks for trigger based on a random hash in a sliding window. 167 * @param[in] state Structure holding state info on current rolling hash 168 * @param[in] buffer Pointer to input buffer to run windowed hash on 169 * @param[in] max_len Max length to run over input 170 * @param[in] mask Mask bits ORed with hash before test with trigger 171 * @param[in] trigger Match value to compare with windowed hash at each input byte 172 * @param[out] offset Offset from buffer to match, set if match found 173 * @param[out] match Pointer to fingerprint result status to set 174 * ISAL_FINGERPRINT_RET_HIT - match found 175 * ISAL_FINGERPRINT_RET_MAX - exceeded max length 176 * ISAL_FINGERPRINT_RET_OTHER - error 177 * @return Operation status 178 * @retval 0 on success 179 * @retval Non-zero \a ISAL_CRYPTO_ERR on failure 180 */ 181 int 182 isal_rolling_hash2_run(struct isal_rh_state2 *state, const uint8_t *buffer, const uint32_t max_len, 183 const uint32_t mask, const uint32_t trigger, uint32_t *offset, int *match); 184 185 /** 186 * @brief Generate an appropriate mask to target mean hit rate 187 * 188 * @param[in] mean Target chunk size in bytes 189 * @param[in] shift Bits to rotate result to get independent masks 190 * @param[out] mask Generated 32-bit mask value 191 * @return Operation status 192 * @retval 0 on success 193 * @retval Non-zero \a ISAL_CRYPTO_ERR on failure 194 */ 195 int 196 isal_rolling_hashx_mask_gen(const uint32_t mean, const uint32_t shift, uint32_t *mask); 197 198 #ifdef __cplusplus 199 } 200 #endif 201 202 #endif // _ROLLING_HASHX_H_ 203