]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /********************************************************************** |
2 | Copyright(c) 2011-2016 Intel Corporation All rights reserved. | |
3 | ||
4 | Redistribution and use in source and binary forms, with or without | |
1e59de90 | 5 | modification, are permitted provided that the following conditions |
7c673cae FG |
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 | #ifndef _MH_SHA1_H_ | |
31 | #define _MH_SHA1_H_ | |
32 | ||
33 | /** | |
34 | * @file mh_sha1.h | |
35 | * @brief mh_sha1 function prototypes and structures | |
36 | * | |
37 | * Interface for mh_sha1 functions | |
38 | * | |
39 | * <b> mh_sha1 Init-Update..Update-Finalize </b> | |
40 | * | |
41 | * This file defines the interface to optimized functions used in mh_sha1. | |
42 | * The definition of multi-hash SHA1(mh_sha1, for short) is: Pad the buffer | |
43 | * in SHA1 style until the total length is a multiple of 4*16*16 | |
44 | * (words-width * parallel-segments * block-size); Hash the buffer in | |
45 | * parallel, generating digests of 4*16*5 (words-width*parallel-segments* | |
46 | * digest-size); Treat the set of digests as another data buffer, and | |
47 | * generate a final SHA1 digest for it. | |
48 | * | |
49 | * | |
50 | * Example | |
51 | * \code | |
52 | * uint32_t mh_sha1_digest[SHA1_DIGEST_WORDS]; | |
53 | * struct mh_sha1_ctx *ctx; | |
54 | * | |
55 | * ctx = malloc(sizeof(struct mh_sha1_ctx)); | |
56 | * mh_sha1_init(ctx); | |
57 | * mh_sha1_update(ctx, buff, block_len); | |
58 | * mh_sha1_finalize(ctx, mh_sha1_digest); | |
59 | * \endcode | |
60 | */ | |
61 | ||
62 | #include <stdint.h> | |
63 | ||
64 | #ifdef __cplusplus | |
65 | extern "C" { | |
66 | #endif | |
67 | ||
68 | ||
69 | // External Interface Definition | |
70 | #define HASH_SEGS 16 | |
71 | #define SHA1_BLOCK_SIZE 64 | |
72 | #define MH_SHA1_BLOCK_SIZE (HASH_SEGS * SHA1_BLOCK_SIZE) | |
73 | #define SHA1_DIGEST_WORDS 5 | |
74 | #define AVX512_ALIGNED 64 | |
75 | ||
76 | /** @brief Holds info describing a single mh_sha1 | |
77 | * | |
78 | * It is better to use heap to allocate this data structure to avoid stack overflow. | |
79 | * | |
80 | */ | |
81 | struct mh_sha1_ctx { | |
82 | uint32_t mh_sha1_digest[SHA1_DIGEST_WORDS]; //!< the digest of multi-hash SHA1 | |
83 | ||
84 | uint64_t total_length; | |
85 | //!< Parameters for update feature, describe the lengths of input buffers in bytes | |
86 | uint8_t partial_block_buffer [MH_SHA1_BLOCK_SIZE * 2]; | |
87 | //!< Padding the tail of input data for SHA1 | |
88 | uint8_t mh_sha1_interim_digests[sizeof(uint32_t) * SHA1_DIGEST_WORDS * HASH_SEGS]; | |
89 | //!< Storing the SHA1 interim digests of all 16 segments. Each time, it will be copied to stack for 64-byte alignment purpose. | |
90 | uint8_t frame_buffer[MH_SHA1_BLOCK_SIZE + AVX512_ALIGNED]; | |
91 | //!< Re-structure sha1 block data from different segments to fit big endian. Use AVX512_ALIGNED for 64-byte alignment purpose. | |
92 | }; | |
93 | ||
94 | /** | |
95 | * @enum mh_sha1_ctx_error | |
96 | * @brief CTX error flags | |
97 | */ | |
98 | enum mh_sha1_ctx_error{ | |
1e59de90 TL |
99 | MH_SHA1_CTX_ERROR_NONE = 0, //!< MH_SHA1_CTX_ERROR_NONE |
100 | MH_SHA1_CTX_ERROR_NULL = -1, //!< MH_SHA1_CTX_ERROR_NULL | |
7c673cae FG |
101 | }; |
102 | ||
103 | ||
104 | /******************************************************************* | |
105 | * mh_sha1 API function prototypes | |
106 | ******************************************************************/ | |
107 | ||
108 | /** | |
109 | * @brief Initialize the mh_sha1_ctx structure. | |
110 | * | |
111 | * @param ctx Structure holding mh_sha1 info | |
112 | * @returns int Return 0 if the function runs without errors | |
113 | */ | |
114 | int mh_sha1_init (struct mh_sha1_ctx* ctx); | |
115 | ||
116 | /** | |
117 | * @brief Multi-hash sha1 update. | |
118 | * | |
119 | * Can be called repeatedly to update hashes with new input data. | |
120 | * This function determines what instruction sets are enabled and selects the | |
121 | * appropriate version at runtime. | |
122 | * | |
123 | * @param ctx Structure holding mh_sha1 info | |
124 | * @param buffer Pointer to buffer to be processed | |
125 | * @param len Length of buffer (in bytes) to be processed | |
126 | * @returns int Return 0 if the function runs without errors | |
127 | */ | |
128 | int mh_sha1_update (struct mh_sha1_ctx * ctx, const void* buffer, uint32_t len); | |
129 | ||
130 | /** | |
131 | * @brief Finalize the message digests for multi-hash sha1. | |
132 | * | |
133 | * Place the message digest in mh_sha1_digest which must have enough space | |
134 | * for the outputs. | |
135 | * This function determines what instruction sets are enabled and selects the | |
136 | * appropriate version at runtime. | |
137 | * | |
138 | * @param ctx Structure holding mh_sha1 info | |
139 | * @param mh_sha1_digest The digest of mh_sha1 | |
140 | * @returns int Return 0 if the function runs without errors | |
141 | */ | |
142 | int mh_sha1_finalize (struct mh_sha1_ctx* ctx, void* mh_sha1_digest); | |
143 | ||
144 | /******************************************************************* | |
145 | * multi-types of mh_sha1 internal API | |
146 | * | |
147 | * XXXX The multi-binary version | |
148 | * XXXX_base The C code version which used to display the algorithm | |
149 | * XXXX_sse The version uses a ASM function optimized for SSE | |
150 | * XXXX_avx The version uses a ASM function optimized for AVX | |
151 | * XXXX_avx2 The version uses a ASM function optimized for AVX2 | |
152 | * XXXX_avx512 The version uses a ASM function optimized for AVX512 | |
153 | * | |
154 | ******************************************************************/ | |
155 | ||
156 | /** | |
157 | * @brief Multi-hash sha1 update. | |
158 | * | |
159 | * Can be called repeatedly to update hashes with new input data. | |
160 | * Base update() function that does not require SIMD support. | |
161 | * | |
162 | * @param ctx Structure holding mh_sha1 info | |
163 | * @param buffer Pointer to buffer to be processed | |
164 | * @param len Length of buffer (in bytes) to be processed | |
165 | * @returns int Return 0 if the function runs without errors | |
166 | * | |
167 | */ | |
168 | int mh_sha1_update_base (struct mh_sha1_ctx* ctx, const void* buffer, uint32_t len); | |
169 | ||
170 | /** | |
171 | * @brief Multi-hash sha1 update. | |
172 | * | |
173 | * Can be called repeatedly to update hashes with new input data. | |
174 | * @requires SSE | |
175 | * | |
176 | * @param ctx Structure holding mh_sha1 info | |
177 | * @param buffer Pointer to buffer to be processed | |
178 | * @param len Length of buffer (in bytes) to be processed | |
179 | * @returns int Return 0 if the function runs without errors | |
180 | * | |
181 | */ | |
182 | int mh_sha1_update_sse (struct mh_sha1_ctx * ctx, | |
183 | const void* buffer, uint32_t len); | |
184 | ||
185 | /** | |
186 | * @brief Multi-hash sha1 update. | |
187 | * | |
188 | * Can be called repeatedly to update hashes with new input data. | |
189 | * @requires AVX | |
190 | * | |
191 | * @param ctx Structure holding mh_sha1 info | |
192 | * @param buffer Pointer to buffer to be processed | |
193 | * @param len Length of buffer (in bytes) to be processed | |
194 | * @returns int Return 0 if the function runs without errors | |
195 | * | |
196 | */ | |
197 | int mh_sha1_update_avx (struct mh_sha1_ctx * ctx, | |
198 | const void* buffer, uint32_t len); | |
199 | ||
200 | /** | |
201 | * @brief Multi-hash sha1 update. | |
202 | * | |
203 | * Can be called repeatedly to update hashes with new input data. | |
204 | * @requires AVX2 | |
205 | * | |
206 | * @param ctx Structure holding mh_sha1 info | |
207 | * @param buffer Pointer to buffer to be processed | |
208 | * @param len Length of buffer (in bytes) to be processed | |
209 | * @returns int Return 0 if the function runs without errors | |
210 | * | |
211 | */ | |
212 | int mh_sha1_update_avx2 (struct mh_sha1_ctx * ctx, | |
213 | const void* buffer, uint32_t len); | |
214 | ||
215 | /** | |
216 | * @brief Multi-hash sha1 update. | |
217 | * | |
218 | * Can be called repeatedly to update hashes with new input data. | |
219 | * @requires AVX512 | |
220 | * | |
221 | * @param ctx Structure holding mh_sha1 info | |
222 | * @param buffer Pointer to buffer to be processed | |
223 | * @param len Length of buffer (in bytes) to be processed | |
224 | * @returns int Return 0 if the function runs without errors | |
225 | * | |
226 | */ | |
227 | int mh_sha1_update_avx512 (struct mh_sha1_ctx * ctx, | |
228 | const void* buffer, uint32_t len); | |
229 | ||
230 | ||
231 | /** | |
232 | * @brief Finalize the message digests for multi-hash sha1. | |
233 | * | |
234 | * Place the message digests in mh_sha1_digest, | |
235 | * which must have enough space for the outputs. | |
236 | * Base Finalize() function that does not require SIMD support. | |
237 | * | |
238 | * @param ctx Structure holding mh_sha1 info | |
239 | * @param mh_sha1_digest The digest of mh_sha1 | |
240 | * @returns int Return 0 if the function runs without errors | |
241 | * | |
242 | */ | |
243 | int mh_sha1_finalize_base (struct mh_sha1_ctx* ctx, | |
244 | void* mh_sha1_digest); | |
245 | ||
246 | /** | |
247 | * @brief Finalize the message digests for combined multi-hash and murmur. | |
248 | * | |
249 | * Place the message digest in mh_sha1_digest which must have enough space | |
250 | * for the outputs. | |
251 | * | |
252 | * @requires SSE | |
253 | * | |
254 | * @param ctx Structure holding mh_sha1 info | |
255 | * @param mh_sha1_digest The digest of mh_sha1 | |
256 | * @returns int Return 0 if the function runs without errors | |
257 | * | |
258 | */ | |
259 | int mh_sha1_finalize_sse (struct mh_sha1_ctx* ctx, | |
260 | void* mh_sha1_digest); | |
261 | ||
262 | /** | |
263 | * @brief Finalize the message digests for combined multi-hash and murmur. | |
264 | * | |
265 | * Place the message digest in mh_sha1_digest which must have enough space | |
266 | * for the outputs. | |
267 | * | |
268 | * @requires AVX | |
269 | * | |
270 | * @param ctx Structure holding mh_sha1 info | |
271 | * @param mh_sha1_digest The digest of mh_sha1 | |
272 | * @returns int Return 0 if the function runs without errors | |
273 | * | |
274 | */ | |
275 | int mh_sha1_finalize_avx (struct mh_sha1_ctx* ctx, | |
276 | void* mh_sha1_digest); | |
277 | ||
278 | /** | |
279 | * @brief Finalize the message digests for combined multi-hash and murmur. | |
280 | * | |
281 | * Place the message digest in mh_sha1_digest which must have enough space | |
282 | * for the outputs. | |
283 | * | |
284 | * @requires AVX2 | |
285 | * | |
286 | * @param ctx Structure holding mh_sha1 info | |
287 | * @param mh_sha1_digest The digest of mh_sha1 | |
288 | * @returns int Return 0 if the function runs without errors | |
289 | * | |
290 | */ | |
291 | int mh_sha1_finalize_avx2 (struct mh_sha1_ctx* ctx, | |
292 | void* mh_sha1_digest); | |
293 | ||
294 | /** | |
295 | * @brief Finalize the message digests for combined multi-hash and murmur. | |
296 | * | |
297 | * Place the message digest in mh_sha1_digest which must have enough space | |
298 | * for the outputs. | |
299 | * | |
300 | * @requires AVX512 | |
301 | * | |
302 | * @param ctx Structure holding mh_sha1 info | |
303 | * @param mh_sha1_digest The digest of mh_sha1 | |
304 | * @returns int Return 0 if the function runs without errors | |
305 | * | |
306 | */ | |
307 | int mh_sha1_finalize_avx512 (struct mh_sha1_ctx* ctx, | |
308 | void* mh_sha1_digest); | |
309 | ||
310 | #ifdef __cplusplus | |
311 | } | |
312 | #endif | |
313 | ||
314 | #endif | |
315 |