]>
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 _MD5_MB_H_ | |
31 | #define _MD5_MB_H_ | |
32 | ||
33 | /** | |
34 | * @file md5_mb.h | |
35 | * @brief Multi-buffer CTX API MD5 function prototypes and structures | |
36 | * | |
37 | * Interface for multi-buffer MD5 functions | |
38 | * | |
39 | * <b> Multi-buffer MD5 Entire or First-Update..Update-Last </b> | |
40 | * | |
41 | * The interface to this multi-buffer hashing code is carried out through the | |
42 | * context-level (CTX) init, submit and flush functions and the MD5_HASH_CTX_MGR and | |
43 | * MD5_HASH_CTX objects. Numerous MD5_HASH_CTX objects may be instantiated by the | |
44 | * application for use with a single MD5_HASH_CTX_MGR. | |
45 | * | |
46 | * The CTX interface functions carry out the initialization and padding of the jobs | |
47 | * entered by the user and add them to the multi-buffer manager. The lower level "scheduler" | |
48 | * layer then processes the jobs in an out-of-order manner. The scheduler layer functions | |
49 | * are internal and are not intended to be invoked directly. Jobs can be submitted | |
50 | * to a CTX as a complete buffer to be hashed, using the HASH_ENTIRE flag, or as partial | |
51 | * jobs which can be started using the HASH_FIRST flag, and later resumed or finished | |
52 | * using the HASH_UPDATE and HASH_LAST flags respectively. | |
53 | * | |
54 | * <b>Note:</b> The submit function does not require data buffers to be block sized. | |
55 | * | |
56 | * The MD5 CTX interface functions are available for 4 architectures: SSE, AVX, AVX2 and | |
57 | * AVX512. In addition, a multibinary interface is provided, which selects the appropriate | |
58 | * architecture-specific function at runtime. | |
59 | * | |
60 | * <b>Usage:</b> The application creates a MD5_HASH_CTX_MGR object and initializes it | |
61 | * with a call to md5_ctx_mgr_init*() function, where henceforth "*" stands for the | |
62 | * relevant suffix for each architecture; _sse, _avx, _avx2, _avx512 (or no suffix for the | |
63 | * multibinary version). The MD5_HASH_CTX_MGR object will be used to schedule processor | |
64 | * resources, with up to 8 MD5_HASH_CTX objects (or 16 in AVX2 case, 32 in AVX512 case) | |
65 | * being processed at a time. | |
66 | * | |
67 | * Each MD5_HASH_CTX must be initialized before first use by the hash_ctx_init macro | |
68 | * defined in multi_buffer.h. After initialization, the application may begin computing | |
69 | * a hash by giving the MD5_HASH_CTX to a MD5_HASH_CTX_MGR using the submit functions | |
70 | * md5_ctx_mgr_submit*() with the HASH_FIRST flag set. When the MD5_HASH_CTX is | |
71 | * returned to the application (via this or a later call to md5_ctx_mgr_submit*() or | |
72 | * md5_ctx_mgr_flush*()), the application can then re-submit it with another call to | |
73 | * md5_ctx_mgr_submit*(), but without the HASH_FIRST flag set. | |
74 | * | |
75 | * Ideally, on the last buffer for that hash, md5_ctx_mgr_submit_sse is called with | |
76 | * HASH_LAST, although it is also possible to submit the hash with HASH_LAST and a zero | |
77 | * length if necessary. When a MD5_HASH_CTX is returned after having been submitted with | |
78 | * HASH_LAST, it will contain a valid hash. The MD5_HASH_CTX can be reused immediately | |
79 | * by submitting with HASH_FIRST. | |
80 | * | |
81 | * For example, you would submit hashes with the following flags for the following numbers | |
82 | * of buffers: | |
83 | * <ul> | |
84 | * <li> one buffer: HASH_FIRST | HASH_LAST (or, equivalently, HASH_ENTIRE) | |
85 | * <li> two buffers: HASH_FIRST, HASH_LAST | |
86 | * <li> three buffers: HASH_FIRST, HASH_UPDATE, HASH_LAST | |
87 | * etc. | |
88 | * </ul> | |
89 | * | |
90 | * The order in which MD5_CTX objects are returned is in general different from the order | |
91 | * in which they are submitted. | |
92 | * | |
93 | * A few possible error conditions exist: | |
94 | * <ul> | |
95 | * <li> Submitting flags other than the allowed entire/first/update/last values | |
96 | * <li> Submitting a context that is currently being managed by a MD5_HASH_CTX_MGR. | |
97 | * <li> Submitting a context after HASH_LAST is used but before HASH_FIRST is set. | |
98 | * </ul> | |
99 | * | |
100 | * These error conditions are reported by returning the MD5_HASH_CTX immediately after | |
101 | * a submit with its error member set to a non-zero error code (defined in | |
102 | * multi_buffer.h). No changes are made to the MD5_HASH_CTX_MGR in the case of an | |
103 | * error; no processing is done for other hashes. | |
104 | * | |
105 | */ | |
106 | ||
107 | #include <stdint.h> | |
108 | #include "multi_buffer.h" | |
109 | #include "types.h" | |
110 | ||
111 | #ifdef __cplusplus | |
112 | extern "C" { | |
113 | #endif | |
114 | ||
115 | // Hash Constants and Typedefs | |
116 | #define MD5_DIGEST_NWORDS 4 | |
117 | #define MD5_MAX_LANES 32 | |
118 | #define MD5_MIN_LANES 8 | |
119 | #define MD5_BLOCK_SIZE 64 | |
120 | #define MD5_LOG2_BLOCK_SIZE 6 | |
121 | #define MD5_PADLENGTHFIELD_SIZE 8 | |
122 | #define MD5_INITIAL_DIGEST \ | |
123 | 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 | |
124 | ||
125 | typedef uint32_t md5_digest_array[MD5_DIGEST_NWORDS][MD5_MAX_LANES]; | |
126 | typedef uint32_t MD5_WORD_T; | |
127 | ||
128 | /** @brief Scheduler layer - Holds info describing a single MD5 job for the multi-buffer manager */ | |
129 | ||
130 | typedef struct { | |
131 | uint8_t* buffer; //!< pointer to data buffer for this job | |
132 | uint32_t len; //!< length of buffer for this job in blocks. | |
133 | DECLARE_ALIGNED(uint32_t result_digest[MD5_DIGEST_NWORDS],64); | |
134 | JOB_STS status; //!< output job status | |
135 | void* user_data; //!< pointer for user's job-related data | |
136 | } MD5_JOB; | |
137 | ||
138 | /** @brief Scheduler layer - Holds arguments for submitted MD5 job */ | |
139 | ||
140 | typedef struct { | |
141 | md5_digest_array digest; | |
142 | uint8_t* data_ptr[MD5_MAX_LANES]; | |
143 | } MD5_MB_ARGS_X32; | |
144 | ||
145 | /** @brief Scheduler layer - Lane data */ | |
146 | ||
147 | typedef struct { | |
148 | MD5_JOB *job_in_lane; | |
149 | } MD5_LANE_DATA; | |
150 | ||
151 | /** @brief Scheduler layer - Holds state for multi-buffer MD5 jobs */ | |
152 | ||
153 | typedef struct { | |
154 | MD5_MB_ARGS_X32 args; | |
155 | uint32_t lens[MD5_MAX_LANES]; | |
156 | uint64_t unused_lanes[4]; //!< each byte or nibble is index (0...31 or 15) of unused lanes. | |
157 | MD5_LANE_DATA ldata[MD5_MAX_LANES]; | |
158 | uint32_t num_lanes_inuse; | |
159 | } MD5_MB_JOB_MGR; | |
160 | ||
161 | /** @brief Context layer - Holds state for multi-buffer MD5 jobs */ | |
162 | ||
163 | typedef struct { | |
164 | MD5_MB_JOB_MGR mgr; | |
165 | } MD5_HASH_CTX_MGR; | |
166 | ||
167 | /** @brief Context layer - Holds info describing a single MD5 job for the multi-buffer CTX manager */ | |
168 | ||
169 | typedef struct { | |
170 | MD5_JOB job; // Must be at struct offset 0. | |
171 | HASH_CTX_STS status; //!< Context status flag | |
172 | HASH_CTX_ERROR error; //!< Context error flag | |
1e59de90 | 173 | uint64_t total_length; //!< Running counter of length processed for this CTX's job |
7c673cae FG |
174 | const void* incoming_buffer; //!< pointer to data input buffer for this CTX's job |
175 | uint32_t incoming_buffer_length; //!< length of buffer for this job in bytes. | |
176 | uint8_t partial_block_buffer[MD5_BLOCK_SIZE * 2]; //!< CTX partial blocks | |
177 | uint32_t partial_block_buffer_length; | |
178 | void* user_data; //!< pointer for user to keep any job-related data | |
179 | } MD5_HASH_CTX; | |
180 | ||
181 | /******************************************************************* | |
182 | * CTX level API function prototypes | |
183 | ******************************************************************/ | |
184 | ||
185 | /** | |
186 | * @brief Initialize the context level MD5 multi-buffer manager structure. | |
187 | * @requires SSE4.1 | |
188 | * | |
189 | * @param mgr Structure holding context level state info | |
190 | * @returns void | |
191 | */ | |
192 | void md5_ctx_mgr_init_sse (MD5_HASH_CTX_MGR* mgr); | |
193 | ||
194 | /** | |
195 | * @brief Submit a new MD5 job to the context level multi-buffer manager. | |
196 | * @requires SSE4.1 | |
197 | * | |
198 | * @param mgr Structure holding context level state info | |
199 | * @param ctx Structure holding ctx job info | |
200 | * @param buffer Pointer to buffer to be processed | |
201 | * @param len Length of buffer (in bytes) to be processed | |
202 | * @param flags Input flag specifying job type (first, update, last or entire) | |
203 | * @returns NULL if no jobs complete or pointer to jobs structure. | |
204 | */ | |
205 | MD5_HASH_CTX* md5_ctx_mgr_submit_sse (MD5_HASH_CTX_MGR* mgr, MD5_HASH_CTX* ctx, | |
206 | const void* buffer, uint32_t len, HASH_CTX_FLAG flags); | |
207 | ||
208 | /** | |
209 | * @brief Finish all submitted MD5 jobs and return when complete. | |
210 | * @requires SSE4.1 | |
211 | * | |
212 | * @param mgr Structure holding context level state info | |
213 | * @returns NULL if no jobs to complete or pointer to jobs structure. | |
214 | */ | |
215 | MD5_HASH_CTX* md5_ctx_mgr_flush_sse (MD5_HASH_CTX_MGR* mgr); | |
216 | ||
217 | /** | |
218 | * @brief Initialize the MD5 multi-buffer manager structure. | |
219 | * @requires AVX | |
220 | * | |
221 | * @param mgr Structure holding context level state info | |
222 | * @returns void | |
223 | */ | |
224 | void md5_ctx_mgr_init_avx (MD5_HASH_CTX_MGR* mgr); | |
225 | ||
226 | /** | |
227 | * @brief Submit a new MD5 job to the multi-buffer manager. | |
228 | * @requires AVX | |
229 | * | |
230 | * @param mgr Structure holding context level state info | |
231 | * @param ctx Structure holding ctx job info | |
232 | * @param buffer Pointer to buffer to be processed | |
233 | * @param len Length of buffer (in bytes) to be processed | |
234 | * @param flags Input flag specifying job type (first, update, last or entire) | |
235 | * @returns NULL if no jobs complete or pointer to jobs structure. | |
236 | */ | |
237 | MD5_HASH_CTX* md5_ctx_mgr_submit_avx (MD5_HASH_CTX_MGR* mgr, MD5_HASH_CTX* ctx, | |
238 | const void* buffer, uint32_t len, HASH_CTX_FLAG flags); | |
239 | ||
240 | /** | |
241 | * @brief Finish all submitted MD5 jobs and return when complete. | |
242 | * @requires AVX | |
243 | * | |
244 | * @param mgr Structure holding context level state info | |
245 | * @returns NULL if no jobs to complete or pointer to jobs structure. | |
246 | */ | |
247 | MD5_HASH_CTX* md5_ctx_mgr_flush_avx (MD5_HASH_CTX_MGR* mgr); | |
248 | ||
249 | /** | |
250 | * @brief Initialize the MD5 multi-buffer manager structure. | |
251 | * @requires AVX2 | |
252 | * | |
253 | * @param mgr Structure holding context level state info | |
254 | * @returns void | |
255 | */ | |
256 | void md5_ctx_mgr_init_avx2 (MD5_HASH_CTX_MGR* mgr); | |
257 | ||
258 | /** | |
259 | * @brief Submit a new MD5 job to the multi-buffer manager. | |
260 | * @requires AVX2 | |
261 | * | |
262 | * @param mgr Structure holding context level state info | |
263 | * @param ctx Structure holding ctx job info | |
264 | * @param buffer Pointer to buffer to be processed | |
265 | * @param len Length of buffer (in bytes) to be processed | |
266 | * @param flags Input flag specifying job type (first, update, last or entire) | |
267 | * @returns NULL if no jobs complete or pointer to jobs structure. | |
268 | */ | |
269 | MD5_HASH_CTX* md5_ctx_mgr_submit_avx2 (MD5_HASH_CTX_MGR* mgr, MD5_HASH_CTX* ctx, | |
270 | const void* buffer, uint32_t len, HASH_CTX_FLAG flags); | |
271 | ||
272 | /** | |
273 | * @brief Finish all submitted MD5 jobs and return when complete. | |
274 | * @requires AVX2 | |
275 | * | |
276 | * @param mgr Structure holding context level state info | |
277 | * @returns NULL if no jobs to complete or pointer to jobs structure. | |
278 | */ | |
279 | MD5_HASH_CTX* md5_ctx_mgr_flush_avx2 (MD5_HASH_CTX_MGR* mgr); | |
280 | ||
281 | /** | |
282 | * @brief Initialize the MD5 multi-buffer manager structure. | |
283 | * @requires AVX512 | |
284 | * | |
285 | * @param mgr Structure holding context level state info | |
286 | * @returns void | |
287 | */ | |
288 | void md5_ctx_mgr_init_avx512 (MD5_HASH_CTX_MGR* mgr); | |
289 | ||
290 | /** | |
291 | * @brief Submit a new MD5 job to the multi-buffer manager. | |
292 | * @requires AVX512 | |
293 | * | |
294 | * @param mgr Structure holding context level state info | |
295 | * @param ctx Structure holding ctx job info | |
296 | * @param buffer Pointer to buffer to be processed | |
297 | * @param len Length of buffer (in bytes) to be processed | |
298 | * @param flags Input flag specifying job type (first, update, last or entire) | |
299 | * @returns NULL if no jobs complete or pointer to jobs structure. | |
300 | */ | |
301 | MD5_HASH_CTX* md5_ctx_mgr_submit_avx512 (MD5_HASH_CTX_MGR* mgr, MD5_HASH_CTX* ctx, | |
302 | const void* buffer, uint32_t len, HASH_CTX_FLAG flags); | |
303 | ||
304 | /** | |
305 | * @brief Finish all submitted MD5 jobs and return when complete. | |
306 | * @requires AVX512 | |
307 | * | |
308 | * @param mgr Structure holding context level state info | |
309 | * @returns NULL if no jobs to complete or pointer to jobs structure. | |
310 | */ | |
311 | MD5_HASH_CTX* md5_ctx_mgr_flush_avx512 (MD5_HASH_CTX_MGR* mgr); | |
312 | ||
313 | /******************** multibinary function prototypes **********************/ | |
314 | ||
315 | /** | |
316 | * @brief Initialize the MD5 multi-buffer manager structure. | |
317 | * @requires SSE4.1 or AVX or AVX2 or AVX512 | |
318 | * | |
319 | * @param mgr Structure holding context level state info | |
320 | * @returns void | |
321 | */ | |
322 | void md5_ctx_mgr_init (MD5_HASH_CTX_MGR* mgr); | |
323 | ||
324 | /** | |
325 | * @brief Submit a new MD5 job to the multi-buffer manager. | |
326 | * @requires SSE4.1 or AVX or AVX2 or AVX512 | |
327 | * | |
328 | * @param mgr Structure holding context level state info | |
329 | * @param ctx Structure holding ctx job info | |
330 | * @param buffer Pointer to buffer to be processed | |
331 | * @param len Length of buffer (in bytes) to be processed | |
332 | * @param flags Input flag specifying job type (first, update, last or entire) | |
333 | * @returns NULL if no jobs complete or pointer to jobs structure. | |
334 | */ | |
335 | MD5_HASH_CTX* md5_ctx_mgr_submit (MD5_HASH_CTX_MGR* mgr, MD5_HASH_CTX* ctx, | |
336 | const void* buffer, uint32_t len, HASH_CTX_FLAG flags); | |
337 | ||
338 | /** | |
339 | * @brief Finish all submitted MD5 jobs and return when complete. | |
340 | * @requires SSE4.1 or AVX or AVX2 or AVX512 | |
341 | * | |
342 | * @param mgr Structure holding context level state info | |
343 | * @returns NULL if no jobs to complete or pointer to jobs structure. | |
344 | */ | |
345 | MD5_HASH_CTX* md5_ctx_mgr_flush (MD5_HASH_CTX_MGR* mgr); | |
346 | ||
347 | ||
348 | /******************************************************************* | |
349 | * Scheduler (internal) level out-of-order function prototypes | |
350 | ******************************************************************/ | |
351 | ||
352 | void md5_mb_mgr_init_sse (MD5_MB_JOB_MGR *state); | |
353 | MD5_JOB* md5_mb_mgr_submit_sse (MD5_MB_JOB_MGR *state, MD5_JOB* job); | |
354 | MD5_JOB* md5_mb_mgr_flush_sse (MD5_MB_JOB_MGR *state); | |
355 | ||
356 | #define md5_mb_mgr_init_avx md5_mb_mgr_init_sse | |
357 | MD5_JOB* md5_mb_mgr_submit_avx (MD5_MB_JOB_MGR *state, MD5_JOB* job); | |
358 | MD5_JOB* md5_mb_mgr_flush_avx (MD5_MB_JOB_MGR *state); | |
359 | ||
360 | void md5_mb_mgr_init_avx2 (MD5_MB_JOB_MGR *state); | |
361 | MD5_JOB* md5_mb_mgr_submit_avx2 (MD5_MB_JOB_MGR *state, MD5_JOB* job); | |
362 | MD5_JOB* md5_mb_mgr_flush_avx2 (MD5_MB_JOB_MGR *state); | |
363 | ||
364 | void md5_mb_mgr_init_avx512 (MD5_MB_JOB_MGR *state); | |
365 | MD5_JOB* md5_mb_mgr_submit_avx512 (MD5_MB_JOB_MGR *state, MD5_JOB* job); | |
366 | MD5_JOB* md5_mb_mgr_flush_avx512 (MD5_MB_JOB_MGR *state); | |
367 | ||
368 | #ifdef __cplusplus | |
369 | } | |
370 | #endif | |
371 | ||
372 | #endif // _MD5_MB_H_ |