xref: /isa-l_crypto/include/sha256_mb_internal.h (revision bc903ec10e5f6c9ca2afc637ff7065c046f4ee04)
1 /**********************************************************************
2   Copyright(c) 2024 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 #ifndef _SHA256_MB_INTERNAL_H_
31 #define _SHA256_MB_INTERNAL_H_
32 
33 /**
34  *  @file sha256_mb_internal.h
35  *  @brief Multi-buffer CTX API function prototypes and structures
36  *
37  */
38 
39 #include <stdint.h>
40 #include <string.h>
41 
42 #include "sha256_mb.h"
43 #include "multi_buffer.h"
44 #include "types.h"
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 #define ISAL_SHA256_X8_LANES        8
51 #define ISAL_SHA256_LOG2_BLOCK_SIZE 6
52 #define ISAL_SHA256_INITIAL_DIGEST                                                                 \
53         0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,        \
54                 0x5be0cd19
55 
56 /**
57  * @brief Initialize the SHA256 multi-buffer manager structure.
58  * @requires SSE4.1 or AVX or AVX2
59  *
60  * @param mgr	Structure holding context level state info
61  * @returns void
62  */
63 void
64 _sha256_ctx_mgr_init(ISAL_SHA256_HASH_CTX_MGR *mgr);
65 
66 /**
67  * @brief  Submit a new SHA256 job to the multi-buffer manager.
68  * @requires SSE4.1 or AVX or AVX2
69  *
70  * @param  mgr Structure holding context level state info
71  * @param  ctx Structure holding ctx job info
72  * @param  buffer Pointer to buffer to be processed
73  * @param  len Length of buffer (in bytes) to be processed
74  * @param  flags Input flag specifying job type (first, update, last or entire)
75  * @returns NULL if no jobs complete or pointer to jobs structure.
76  */
77 ISAL_SHA256_HASH_CTX *
78 _sha256_ctx_mgr_submit(ISAL_SHA256_HASH_CTX_MGR *mgr, ISAL_SHA256_HASH_CTX *ctx, const void *buffer,
79                        uint32_t len, ISAL_HASH_CTX_FLAG flags);
80 
81 /**
82  * @brief Finish all submitted SHA256 jobs and return when complete.
83  * @requires SSE4.1 or AVX or AVX2
84  *
85  * @param mgr	Structure holding context level state info
86  * @returns NULL if no jobs to complete or pointer to jobs structure.
87  */
88 ISAL_SHA256_HASH_CTX *
89 _sha256_ctx_mgr_flush(ISAL_SHA256_HASH_CTX_MGR *mgr);
90 
91 /*******************************************************************
92  * CTX level API function prototypes
93  ******************************************************************/
94 
95 /**
96  * @brief Initialize the context level SHA256 multi-buffer manager structure.
97  * @requires SSE4.1
98  *
99  * @param mgr Structure holding context level state info
100  * @returns void
101  */
102 void
103 _sha256_ctx_mgr_init_sse(ISAL_SHA256_HASH_CTX_MGR *mgr);
104 
105 /**
106  * @brief  Submit a new SHA256 job to the context level multi-buffer manager.
107  * @requires SSE4.1
108  *
109  * @param  mgr Structure holding context level state info
110  * @param  ctx Structure holding ctx job info
111  * @param  buffer Pointer to buffer to be processed
112  * @param  len Length of buffer (in bytes) to be processed
113  * @param  flags Input flag specifying job type (first, update, last or entire)
114  * @returns NULL if no jobs complete or pointer to jobs structure.
115  */
116 ISAL_SHA256_HASH_CTX *
117 _sha256_ctx_mgr_submit_sse(ISAL_SHA256_HASH_CTX_MGR *mgr, ISAL_SHA256_HASH_CTX *ctx,
118                            const void *buffer, uint32_t len, ISAL_HASH_CTX_FLAG flags);
119 
120 /**
121  * @brief Finish all submitted SHA256 jobs and return when complete.
122  * @requires SSE4.1
123  *
124  * @param mgr	Structure holding context level state info
125  * @returns NULL if no jobs to complete or pointer to jobs structure.
126  */
127 ISAL_SHA256_HASH_CTX *
128 _sha256_ctx_mgr_flush_sse(ISAL_SHA256_HASH_CTX_MGR *mgr);
129 
130 /**
131  * @brief Initialize the context level SHA256 multi-buffer manager structure.
132  * @requires SSE4.1 and SHANI
133  *
134  * @param mgr Structure holding context level state info
135  * @returns void
136  */
137 void
138 _sha256_ctx_mgr_init_sse_ni(ISAL_SHA256_HASH_CTX_MGR *mgr);
139 
140 /**
141  * @brief  Submit a new SHA256 job to the context level multi-buffer manager.
142  * @requires SSE4.1 and SHANI
143  *
144  * @param  mgr Structure holding context level state info
145  * @param  ctx Structure holding ctx job info
146  * @param  buffer Pointer to buffer to be processed
147  * @param  len Length of buffer (in bytes) to be processed
148  * @param  flags Input flag specifying job type (first, update, last or entire)
149  * @returns NULL if no jobs complete or pointer to jobs structure.
150  */
151 ISAL_SHA256_HASH_CTX *
152 _sha256_ctx_mgr_submit_sse_ni(ISAL_SHA256_HASH_CTX_MGR *mgr, ISAL_SHA256_HASH_CTX *ctx,
153                               const void *buffer, uint32_t len, ISAL_HASH_CTX_FLAG flags);
154 
155 /**
156  * @brief Finish all submitted SHA256 jobs and return when complete.
157  * @requires SSE4.1 and SHANI
158  *
159  * @param mgr	Structure holding context level state info
160  * @returns NULL if no jobs to complete or pointer to jobs structure.
161  */
162 ISAL_SHA256_HASH_CTX *
163 _sha256_ctx_mgr_flush_sse_ni(ISAL_SHA256_HASH_CTX_MGR *mgr);
164 
165 /**
166  * @brief Initialize the SHA256 multi-buffer manager structure.
167  * @requires AVX
168  *
169  * @param mgr Structure holding context level state info
170  * @returns void
171  */
172 void
173 _sha256_ctx_mgr_init_avx(ISAL_SHA256_HASH_CTX_MGR *mgr);
174 
175 /**
176  * @brief  Submit a new SHA256 job to the multi-buffer manager.
177  * @requires AVX
178  *
179  * @param  mgr Structure holding context level state info
180  * @param  ctx Structure holding ctx job info
181  * @param  buffer Pointer to buffer to be processed
182  * @param  len Length of buffer (in bytes) to be processed
183  * @param  flags Input flag specifying job type (first, update, last or entire)
184  * @returns NULL if no jobs complete or pointer to jobs structure.
185  */
186 ISAL_SHA256_HASH_CTX *
187 _sha256_ctx_mgr_submit_avx(ISAL_SHA256_HASH_CTX_MGR *mgr, ISAL_SHA256_HASH_CTX *ctx,
188                            const void *buffer, uint32_t len, ISAL_HASH_CTX_FLAG flags);
189 
190 /**
191  * @brief Finish all submitted SHA256 jobs and return when complete.
192  * @requires AVX
193  *
194  * @param mgr	Structure holding context level state info
195  * @returns NULL if no jobs to complete or pointer to jobs structure.
196  */
197 ISAL_SHA256_HASH_CTX *
198 _sha256_ctx_mgr_flush_avx(ISAL_SHA256_HASH_CTX_MGR *mgr);
199 
200 /**
201  * @brief Initialize the SHA256 multi-buffer manager structure.
202  * @requires AVX2
203  *
204  * @param mgr	Structure holding context level state info
205  * @returns void
206  */
207 void
208 _sha256_ctx_mgr_init_avx2(ISAL_SHA256_HASH_CTX_MGR *mgr);
209 
210 /**
211  * @brief  Submit a new SHA256 job to the multi-buffer manager.
212  * @requires AVX2
213  *
214  * @param  mgr Structure holding context level state info
215  * @param  ctx Structure holding ctx job info
216  * @param  buffer Pointer to buffer to be processed
217  * @param  len Length of buffer (in bytes) to be processed
218  * @param  flags Input flag specifying job type (first, update, last or entire)
219  * @returns NULL if no jobs complete or pointer to jobs structure.
220  */
221 ISAL_SHA256_HASH_CTX *
222 _sha256_ctx_mgr_submit_avx2(ISAL_SHA256_HASH_CTX_MGR *mgr, ISAL_SHA256_HASH_CTX *ctx,
223                             const void *buffer, uint32_t len, ISAL_HASH_CTX_FLAG flags);
224 
225 /**
226  * @brief Finish all submitted SHA256 jobs and return when complete.
227  * @requires AVX2
228  *
229  * @param mgr	Structure holding context level state info
230  * @returns NULL if no jobs to complete or pointer to jobs structure.
231  */
232 ISAL_SHA256_HASH_CTX *
233 _sha256_ctx_mgr_flush_avx2(ISAL_SHA256_HASH_CTX_MGR *mgr);
234 
235 /**
236  * @brief Initialize the SHA256 multi-buffer manager structure.
237  * @requires AVX512
238  *
239  * @param mgr	Structure holding context level state info
240  * @returns void
241  */
242 void
243 _sha256_ctx_mgr_init_avx512(ISAL_SHA256_HASH_CTX_MGR *mgr);
244 
245 /**
246  * @brief  Submit a new SHA256 job to the multi-buffer manager.
247  * @requires AVX512
248  *
249  * @param  mgr Structure holding context level state info
250  * @param  ctx Structure holding ctx job info
251  * @param  buffer Pointer to buffer to be processed
252  * @param  len Length of buffer (in bytes) to be processed
253  * @param  flags Input flag specifying job type (first, update, last or entire)
254  * @returns NULL if no jobs complete or pointer to jobs structure.
255  */
256 ISAL_SHA256_HASH_CTX *
257 _sha256_ctx_mgr_submit_avx512(ISAL_SHA256_HASH_CTX_MGR *mgr, ISAL_SHA256_HASH_CTX *ctx,
258                               const void *buffer, uint32_t len, ISAL_HASH_CTX_FLAG flags);
259 
260 /**
261  * @brief Finish all submitted SHA256 jobs and return when complete.
262  * @requires AVX512
263  *
264  * @param mgr	Structure holding context level state info
265  * @returns NULL if no jobs to complete or pointer to jobs structure.
266  */
267 ISAL_SHA256_HASH_CTX *
268 _sha256_ctx_mgr_flush_avx512(ISAL_SHA256_HASH_CTX_MGR *mgr);
269 
270 /**
271  * @brief Initialize the SHA256 multi-buffer manager structure.
272  * @requires AVX512 and SHANI
273  *
274  * @param mgr	Structure holding context level state info
275  * @returns void
276  */
277 void
278 _sha256_ctx_mgr_init_avx512_ni(ISAL_SHA256_HASH_CTX_MGR *mgr);
279 
280 /**
281  * @brief  Submit a new SHA256 job to the multi-buffer manager.
282  * @requires AVX512 and SHANI
283  *
284  * @param  mgr Structure holding context level state info
285  * @param  ctx Structure holding ctx job info
286  * @param  buffer Pointer to buffer to be processed
287  * @param  len Length of buffer (in bytes) to be processed
288  * @param  flags Input flag specifying job type (first, update, last or entire)
289  * @returns NULL if no jobs complete or pointer to jobs structure.
290  */
291 ISAL_SHA256_HASH_CTX *
292 _sha256_ctx_mgr_submit_avx512_ni(ISAL_SHA256_HASH_CTX_MGR *mgr, ISAL_SHA256_HASH_CTX *ctx,
293                                  const void *buffer, uint32_t len, ISAL_HASH_CTX_FLAG flags);
294 
295 /**
296  * @brief Finish all submitted SHA256 jobs and return when complete.
297  * @requires AVX512 and SHANI
298  *
299  * @param mgr	Structure holding context level state info
300  * @returns NULL if no jobs to complete or pointer to jobs structure.
301  */
302 ISAL_SHA256_HASH_CTX *
303 _sha256_ctx_mgr_flush_avx512_ni(ISAL_SHA256_HASH_CTX_MGR *mgr);
304 
305 /*******************************************************************
306  * Scheduler (internal) level out-of-order function prototypes
307  ******************************************************************/
308 void
309 _sha256_mb_mgr_init_sse(ISAL_SHA256_MB_JOB_MGR *state);
310 ISAL_SHA256_JOB *
311 _sha256_mb_mgr_submit_sse(ISAL_SHA256_MB_JOB_MGR *state, ISAL_SHA256_JOB *job);
312 ISAL_SHA256_JOB *
313 _sha256_mb_mgr_flush_sse(ISAL_SHA256_MB_JOB_MGR *state);
314 
315 #define _sha256_mb_mgr_init_avx _sha256_mb_mgr_init_sse
316 ISAL_SHA256_JOB *
317 _sha256_mb_mgr_submit_avx(ISAL_SHA256_MB_JOB_MGR *state, ISAL_SHA256_JOB *job);
318 ISAL_SHA256_JOB *
319 _sha256_mb_mgr_flush_avx(ISAL_SHA256_MB_JOB_MGR *state);
320 
321 void
322 _sha256_mb_mgr_init_avx2(ISAL_SHA256_MB_JOB_MGR *state);
323 ISAL_SHA256_JOB *
324 _sha256_mb_mgr_submit_avx2(ISAL_SHA256_MB_JOB_MGR *state, ISAL_SHA256_JOB *job);
325 ISAL_SHA256_JOB *
326 _sha256_mb_mgr_flush_avx2(ISAL_SHA256_MB_JOB_MGR *state);
327 
328 void
329 _sha256_mb_mgr_init_avx512(ISAL_SHA256_MB_JOB_MGR *state);
330 ISAL_SHA256_JOB *
331 _sha256_mb_mgr_submit_avx512(ISAL_SHA256_MB_JOB_MGR *state, ISAL_SHA256_JOB *job);
332 ISAL_SHA256_JOB *
333 _sha256_mb_mgr_flush_avx512(ISAL_SHA256_MB_JOB_MGR *state);
334 
335 void
336 _sha256_mb_mgr_init_sse_ni(ISAL_SHA256_MB_JOB_MGR *state);
337 ISAL_SHA256_JOB *
338 _sha256_mb_mgr_submit_sse_ni(ISAL_SHA256_MB_JOB_MGR *state, ISAL_SHA256_JOB *job);
339 ISAL_SHA256_JOB *
340 _sha256_mb_mgr_flush_sse_ni(ISAL_SHA256_MB_JOB_MGR *state);
341 
342 void
343 _sha256_mb_mgr_init_avx512_ni(ISAL_SHA256_MB_JOB_MGR *state);
344 ISAL_SHA256_JOB *
345 _sha256_mb_mgr_submit_avx512_ni(ISAL_SHA256_MB_JOB_MGR *state, ISAL_SHA256_JOB *job);
346 ISAL_SHA256_JOB *
347 _sha256_mb_mgr_flush_avx512_ni(ISAL_SHA256_MB_JOB_MGR *state);
348 
349 #ifdef __cplusplus
350 }
351 #endif
352 
353 #endif // _SHA256_MB_INTERNAL_H_
354