xref: /isa-l_crypto/mh_sha256/mh_sha256_internal.h (revision 9999e13a5707fddf47ed4073f70fb5ea5ee542b0)
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 #ifndef _MH_SHA256_INTERNAL_H_
31 #define _MH_SHA256_INTERNAL_H_
32 
33 /**
34  *  @file mh_sha256_internal.h
35  *  @brief mh_sha256 internal function prototypes and macros
36  *
37  *  Interface for mh_sha256 internal functions
38  *
39  */
40 #include <stdint.h>
41 #include "mh_sha256.h"
42 #include "endian_helper.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 #ifdef _MSC_VER
49 #define inline __inline
50 #endif
51 
52 // 64byte pointer align
53 #define ALIGN_64(pointer) (((uint64_t) (pointer) + 0x3F) & (~0x3F))
54 
55 /*******************************************************************
56  *mh_sha256 constants and macros
57  ******************************************************************/
58 /* mh_sha256 constants */
59 #define MH_SHA256_H0 0x6a09e667UL
60 #define MH_SHA256_H1 0xbb67ae85UL
61 #define MH_SHA256_H2 0x3c6ef372UL
62 #define MH_SHA256_H3 0xa54ff53aUL
63 #define MH_SHA256_H4 0x510e527fUL
64 #define MH_SHA256_H5 0x9b05688cUL
65 #define MH_SHA256_H6 0x1f83d9abUL
66 #define MH_SHA256_H7 0x5be0cd19UL
67 
68 /* mh_sha256 macros */
69 #define ror32(x, r) (((x) >> (r)) ^ ((x) << (32 - (r))))
70 
71 #define S0(w) (ror32(w, 7) ^ ror32(w, 18) ^ (w >> 3))
72 #define S1(w) (ror32(w, 17) ^ ror32(w, 19) ^ (w >> 10))
73 
74 #define s0(a)        (ror32(a, 2) ^ ror32(a, 13) ^ ror32(a, 22))
75 #define s1(e)        (ror32(e, 6) ^ ror32(e, 11) ^ ror32(e, 25))
76 #define maj(a, b, c) ((a & b) ^ (a & c) ^ (b & c))
77 #define ch(e, f, g)  ((e & f) ^ (g & ~e))
78 
79 /*******************************************************************
80  * SHA256 API internal function prototypes
81  ******************************************************************/
82 
83 /**
84  * @brief Initialize the mh_sha256_ctx structure.
85  *
86  * @param  ctx Structure holding mh_sha256 info
87  * @returns int Return 0 if the function runs without errors
88  */
89 int
90 _mh_sha256_init(struct mh_sha256_ctx *ctx);
91 
92 /**
93  * @brief Multi-hash sha256 update.
94  *
95  * Can be called repeatedly to update hashes with new input data.
96  * This function determines what instruction sets are enabled and selects the
97  * appropriate version at runtime.
98  *
99  * @param  ctx Structure holding mh_sha256 info
100  * @param  buffer Pointer to buffer to be processed
101  * @param  len Length of buffer (in bytes) to be processed
102  * @returns int Return 0 if the function runs without errors
103  */
104 int
105 _mh_sha256_update(struct mh_sha256_ctx *ctx, const void *buffer, uint32_t len);
106 
107 /**
108  * @brief Finalize the message digests for multi-hash sha256.
109  *
110  * Place the message digest in mh_sha256_digest which must have enough space
111  * for the outputs.
112  * This function determines what instruction sets are enabled and selects the
113  * appropriate version at runtime.
114  *
115  * @param   ctx Structure holding mh_sha256 info
116  * @param   mh_sha256_digest The digest of mh_sha256
117  * @returns int Return 0 if the function runs without errors
118  */
119 int
120 _mh_sha256_finalize(struct mh_sha256_ctx *ctx, void *mh_sha256_digest);
121 
122 /*******************************************************************
123  * multi-types of mh_sha256 internal API
124  *
125  * XXXX		The multi-binary version
126  * XXXX_base	The C code version which used to display the algorithm
127  * XXXX_sse	The version uses a ASM function optimized for SSE
128  * XXXX_avx	The version uses a ASM function optimized for AVX
129  * XXXX_avx2	The version uses a ASM function optimized for AVX2
130  * XXXX_avx512	The version uses a ASM function optimized for AVX512
131  *
132  ******************************************************************/
133 
134 /**
135  * @brief Multi-hash sha256 update.
136  *
137  * Can be called repeatedly to update hashes with new input data.
138  * Base update() function that does not require SIMD support.
139  *
140  * @param   ctx Structure holding mh_sha256 info
141  * @param   buffer Pointer to buffer to be processed
142  * @param   len Length of buffer (in bytes) to be processed
143  * @returns int Return 0 if the function runs without errors
144  *
145  */
146 int
147 _mh_sha256_update_base(struct mh_sha256_ctx *ctx, const void *buffer, uint32_t len);
148 
149 /**
150  * @brief Finalize the message digests for multi-hash sha256.
151  *
152  * Place the message digests in mh_sha256_digest,
153  * which must have enough space for the outputs.
154  * Base Finalize() function that does not require SIMD support.
155  *
156  * @param   ctx Structure holding mh_sha256 info
157  * @param   mh_sha256_digest The digest of mh_sha256
158  * @returns int Return 0 if the function runs without errors
159  *
160  */
161 int
162 _mh_sha256_finalize_base(struct mh_sha256_ctx *ctx, void *mh_sha256_digest);
163 
164 /**
165  * @brief Multi-hash sha256 update.
166  *
167  * Can be called repeatedly to update hashes with new input data.
168  * @requires SSE
169  *
170  * @param   ctx Structure holding mh_sha256 info
171  * @param   buffer Pointer to buffer to be processed
172  * @param   len Length of buffer (in bytes) to be processed
173  * @returns int Return 0 if the function runs without errors
174  *
175  */
176 int
177 _mh_sha256_update_sse(struct mh_sha256_ctx *ctx, const void *buffer, uint32_t len);
178 
179 /**
180  * @brief Multi-hash sha256 update.
181  *
182  * Can be called repeatedly to update hashes with new input data.
183  * @requires AVX
184  *
185  * @param   ctx Structure holding mh_sha256 info
186  * @param   buffer Pointer to buffer to be processed
187  * @param   len Length of buffer (in bytes) to be processed
188  * @returns int Return 0 if the function runs without errors
189  *
190  */
191 int
192 _mh_sha256_update_avx(struct mh_sha256_ctx *ctx, const void *buffer, uint32_t len);
193 
194 /**
195  * @brief Multi-hash sha256 update.
196  *
197  * Can be called repeatedly to update hashes with new input data.
198  * @requires AVX2
199  *
200  * @param   ctx Structure holding mh_sha256 info
201  * @param   buffer Pointer to buffer to be processed
202  * @param   len Length of buffer (in bytes) to be processed
203  * @returns int Return 0 if the function runs without errors
204  *
205  */
206 int
207 _mh_sha256_update_avx2(struct mh_sha256_ctx *ctx, const void *buffer, uint32_t len);
208 
209 /**
210  * @brief Multi-hash sha256 update.
211  *
212  * Can be called repeatedly to update hashes with new input data.
213  * @requires AVX512
214  *
215  * @param   ctx Structure holding mh_sha256 info
216  * @param   buffer Pointer to buffer to be processed
217  * @param   len Length of buffer (in bytes) to be processed
218  * @returns int Return 0 if the function runs without errors
219  *
220  */
221 int
222 _mh_sha256_update_avx512(struct mh_sha256_ctx *ctx, const void *buffer, uint32_t len);
223 
224 /**
225  * @brief Finalize the message digests for combined multi-hash and murmur.
226  *
227  * Place the message digest in mh_sha256_digest which must have enough space
228  * for the outputs.
229  *
230  * @requires SSE
231  *
232  * @param   ctx Structure holding mh_sha256 info
233  * @param   mh_sha256_digest The digest of mh_sha256
234  * @returns int Return 0 if the function runs without errors
235  *
236  */
237 int
238 _mh_sha256_finalize_sse(struct mh_sha256_ctx *ctx, void *mh_sha256_digest);
239 
240 /**
241  * @brief Finalize the message digests for combined multi-hash and murmur.
242  *
243  * Place the message digest in mh_sha256_digest which must have enough space
244  * for the outputs.
245  *
246  * @requires AVX
247  *
248  * @param   ctx Structure holding mh_sha256 info
249  * @param   mh_sha256_digest The digest of mh_sha256
250  * @returns int Return 0 if the function runs without errors
251  *
252  */
253 int
254 _mh_sha256_finalize_avx(struct mh_sha256_ctx *ctx, void *mh_sha256_digest);
255 
256 /**
257  * @brief Finalize the message digests for combined multi-hash and murmur.
258  *
259  * Place the message digest in mh_sha256_digest which must have enough space
260  * for the outputs.
261  *
262  * @requires AVX2
263  *
264  * @param   ctx Structure holding mh_sha256 info
265  * @param   mh_sha256_digest The digest of mh_sha256
266  * @returns int Return 0 if the function runs without errors
267  *
268  */
269 int
270 _mh_sha256_finalize_avx2(struct mh_sha256_ctx *ctx, void *mh_sha256_digest);
271 
272 /**
273  * @brief Finalize the message digests for combined multi-hash and murmur.
274  *
275  * Place the message digest in mh_sha256_digest which must have enough space
276  * for the outputs.
277  *
278  * @requires AVX512
279  *
280  * @param   ctx Structure holding mh_sha256 info
281  * @param   mh_sha256_digest The digest of mh_sha256
282  * @returns int Return 0 if the function runs without errors
283  *
284  */
285 int
286 _mh_sha256_finalize_avx512(struct mh_sha256_ctx *ctx, void *mh_sha256_digest);
287 
288 /**
289  * @brief Performs complete SHA256 algorithm.
290  *
291  * @param input  Pointer to buffer containing the input message.
292  * @param digest Pointer to digest to update.
293  * @param len	  Length of buffer.
294  * @returns None
295  */
296 void
297 sha256_for_mh_sha256(const uint8_t *input_data, uint32_t *digest, const uint32_t len);
298 
299 /**
300  * @brief Calculate sha256 digest of blocks which size is ISAL_SHA256_BLOCK_SIZE
301  *
302  * @param data   Pointer to data buffer containing the input message.
303  * @param digest Pointer to sha256 digest.
304  * @returns None
305  */
306 void
307 sha256_single_for_mh_sha256(const uint8_t *data, uint32_t digest[]);
308 
309 /*******************************************************************
310  * mh_sha256 API internal function prototypes
311  * Multiple versions of Update and Finalize functions are supplied which use
312  * multiple versions of block and tail process subfunctions.
313  ******************************************************************/
314 
315 /**
316  * @brief  Tail process for multi-hash sha256.
317  *
318  * Calculate the remainder of input data which is less than ISAL_MH_SHA256_BLOCK_SIZE.
319  * It will output the final SHA256 digest based on mh_sha256_segs_digests.
320  *
321  * This function determines what instruction sets are enabled and selects the
322  * appropriate version at runtime.
323  *
324  * @param  partial_buffer Pointer to the start addr of remainder
325  * @param  total_len The total length of all sections of input data.
326  * @param  mh_sha256_segs_digests The digests of all 16 segments .
327  * @param  frame_buffer Pointer to buffer which is a temp working area
328  * @returns none
329  *
330  */
331 void
332 _mh_sha256_tail(uint8_t *partial_buffer, uint32_t total_len,
333                 uint32_t (*mh_sha256_segs_digests)[ISAL_HASH_SEGS], uint8_t *frame_buffer,
334                 uint32_t mh_sha256_digest[ISAL_SHA256_DIGEST_WORDS]);
335 
336 /**
337  * @brief  Tail process for multi-hash sha256.
338  *
339  * Calculate the remainder of input data which is less than ISAL_MH_SHA256_BLOCK_SIZE.
340  * It will output the final SHA256 digest based on mh_sha256_segs_digests.
341  *
342  * @param  partial_buffer Pointer to the start addr of remainder
343  * @param  total_len The total length of all sections of input data.
344  * @param  mh_sha256_segs_digests The digests of all 16 segments .
345  * @param  frame_buffer Pointer to buffer which is a temp working area
346  * @param  mh_sha256_digest mh_sha256 digest
347  * @returns none
348  *
349  */
350 void
351 _mh_sha256_tail_base(uint8_t *partial_buffer, uint32_t total_len,
352                      uint32_t (*mh_sha256_segs_digests)[ISAL_HASH_SEGS], uint8_t *frame_buffer,
353                      uint32_t mh_sha256_digest[ISAL_SHA256_DIGEST_WORDS]);
354 
355 /**
356  * @brief  Tail process for multi-hash sha256.
357  *
358  * Calculate the remainder of input data which is less than ISAL_MH_SHA256_BLOCK_SIZE.
359  * It will output the final SHA256 digest based on mh_sha256_segs_digests.
360  *
361  * @requires SSE
362  *
363  * @param  partial_buffer Pointer to the start addr of remainder
364  * @param  total_len The total length of all sections of input data.
365  * @param  mh_sha256_segs_digests The digests of all 16 segments .
366  * @param  frame_buffer Pointer to buffer which is a temp working area
367  * @param  mh_sha256_digest mh_sha256 digest
368  * @returns none
369  *
370  */
371 void
372 _mh_sha256_tail_sse(uint8_t *partial_buffer, uint32_t total_len,
373                     uint32_t (*mh_sha256_segs_digests)[ISAL_HASH_SEGS], uint8_t *frame_buffer,
374                     uint32_t mh_sha256_digest[ISAL_SHA256_DIGEST_WORDS]);
375 
376 /**
377  * @brief  Tail process for multi-hash sha256.
378  *
379  * Calculate the remainder of input data which is less than ISAL_MH_SHA256_BLOCK_SIZE.
380  * It will output the final SHA256 digest based on mh_sha256_segs_digests.
381  *
382  * @requires AVX
383  *
384  * @param  partial_buffer Pointer to the start addr of remainder
385  * @param  total_len The total length of all sections of input data.
386  * @param  mh_sha256_segs_digests The digests of all 16 segments .
387  * @param  frame_buffer Pointer to buffer which is a temp working area
388  * @param  mh_sha256_digest mh_sha256 digest
389  * @returns none
390  *
391  */
392 void
393 _mh_sha256_tail_avx(uint8_t *partial_buffer, uint32_t total_len,
394                     uint32_t (*mh_sha256_segs_digests)[ISAL_HASH_SEGS], uint8_t *frame_buffer,
395                     uint32_t mh_sha256_digest[ISAL_SHA256_DIGEST_WORDS]);
396 
397 /**
398  * @brief  Tail process for multi-hash sha256.
399  *
400  * Calculate the remainder of input data which is less than ISAL_MH_SHA256_BLOCK_SIZE.
401  * It will output the final SHA256 digest based on mh_sha256_segs_digests.
402  *
403  * @requires AVX2
404  *
405  * @param  partial_buffer Pointer to the start addr of remainder
406  * @param  total_len The total length of all sections of input data.
407  * @param  mh_sha256_segs_digests The digests of all 16 segments .
408  * @param  frame_buffer Pointer to buffer which is a temp working area
409  * @param  mh_sha256_digest mh_sha256 digest
410  * @returns none
411  *
412  */
413 void
414 _mh_sha256_tail_avx2(uint8_t *partial_buffer, uint32_t total_len,
415                      uint32_t (*mh_sha256_segs_digests)[ISAL_HASH_SEGS], uint8_t *frame_buffer,
416                      uint32_t mh_sha256_digest[ISAL_SHA256_DIGEST_WORDS]);
417 
418 /**
419  * @brief  Tail process for multi-hash sha256.
420  *
421  * Calculate the remainder of input data which is less than ISAL_MH_SHA256_BLOCK_SIZE.
422  * It will output the final SHA256 digest based on mh_sha256_segs_digests.
423  *
424  * @requires AVX512
425  *
426  * @param  partial_buffer Pointer to the start addr of remainder
427  * @param  total_len The total length of all sections of input data.
428  * @param  mh_sha256_segs_digests The digests of all 16 segments .
429  * @param  frame_buffer Pointer to buffer which is a temp working area
430  * @param  mh_sha256_digest mh_sha256 digest
431  * @returns none
432  *
433  */
434 void
435 _mh_sha256_tail_avx512(uint8_t *partial_buffer, uint32_t total_len,
436                        uint32_t (*mh_sha256_segs_digests)[ISAL_HASH_SEGS], uint8_t *frame_buffer,
437                        uint32_t mh_sha256_digest[ISAL_SHA256_DIGEST_WORDS]);
438 
439 /**
440  * @brief  Calculate mh_sha256 digest of blocks which size is ISAL_MH_SHA256_BLOCK_SIZE*N.
441  *
442  * This function determines what instruction sets are enabled and selects the
443  * appropriate version at runtime.
444  *
445  * @param  input_data Pointer to input data to be processed
446  * @param  digests 16 segments digests
447  * @param  frame_buffer Pointer to buffer which is a temp working area
448  * @param  num_blocks The number of blocks.
449  * @returns none
450  *
451  */
452 void
453 _mh_sha256_block(const uint8_t *input_data,
454                  uint32_t digests[ISAL_SHA256_DIGEST_WORDS][ISAL_HASH_SEGS],
455                  uint8_t frame_buffer[ISAL_MH_SHA256_BLOCK_SIZE], uint32_t num_blocks);
456 
457 /**
458  * @brief  Calculate mh_sha256 digest of blocks which size is ISAL_MH_SHA256_BLOCK_SIZE*N.
459  *
460  * @param  input_data Pointer to input data to be processed
461  * @param  digests 16 segments digests
462  * @param  frame_buffer Pointer to buffer which is a temp working area
463  * @param  num_blocks The number of blocks.
464  * @returns none
465  *
466  */
467 void
468 _mh_sha256_block_base(const uint8_t *input_data,
469                       uint32_t digests[ISAL_SHA256_DIGEST_WORDS][ISAL_HASH_SEGS],
470                       uint8_t frame_buffer[ISAL_MH_SHA256_BLOCK_SIZE], uint32_t num_blocks);
471 
472 /**
473  * @brief  Calculate mh_sha256 digest of blocks which size is ISAL_MH_SHA256_BLOCK_SIZE*N.
474  *
475  * @requires SSE
476  * @param  input_data Pointer to input data to be processed
477  * @param  digests 16 segments digests
478  * @param  frame_buffer Pointer to buffer which is a temp working area
479  * @param  num_blocks The number of blocks.
480  * @returns none
481  *
482  */
483 void
484 _mh_sha256_block_sse(const uint8_t *input_data,
485                      uint32_t digests[ISAL_SHA256_DIGEST_WORDS][ISAL_HASH_SEGS],
486                      uint8_t frame_buffer[ISAL_MH_SHA256_BLOCK_SIZE], uint32_t num_blocks);
487 
488 /**
489  * @brief  Calculate mh_sha256 digest of blocks which size is ISAL_MH_SHA256_BLOCK_SIZE*N.
490  *
491  * @requires AVX
492  *
493  * @param  input_data Pointer to input data to be processed
494  * @param  digests 16 segments digests
495  * @param  frame_buffer Pointer to buffer which is a temp working area
496  * @param  num_blocks The number of blocks.
497  * @returns none
498  *
499  */
500 void
501 _mh_sha256_block_avx(const uint8_t *input_data,
502                      uint32_t digests[ISAL_SHA256_DIGEST_WORDS][ISAL_HASH_SEGS],
503                      uint8_t frame_buffer[ISAL_MH_SHA256_BLOCK_SIZE], uint32_t num_blocks);
504 
505 /**
506  * @brief  Calculate mh_sha256 digest of blocks which size is ISAL_MH_SHA256_BLOCK_SIZE*N.
507  *
508  * @requires AVX2
509  *
510  * @param  input_data Pointer to input data to be processed
511  * @param  digests 16 segments digests
512  * @param  frame_buffer Pointer to buffer which is a temp working area
513  * @param  num_blocks The number of blocks.
514  * @returns none
515  *
516  */
517 void
518 _mh_sha256_block_avx2(const uint8_t *input_data,
519                       uint32_t digests[ISAL_SHA256_DIGEST_WORDS][ISAL_HASH_SEGS],
520                       uint8_t frame_buffer[ISAL_MH_SHA256_BLOCK_SIZE], uint32_t num_blocks);
521 
522 /**
523  * @brief  Calculate mh_sha256 digest of blocks which size is ISAL_MH_SHA256_BLOCK_SIZE*N.
524  *
525  * @requires AVX512
526  *
527  * @param  input_data Pointer to input data to be processed
528  * @param  digests 16 segments digests
529  * @param  frame_buffer Pointer to buffer which is a temp working area
530  * @param  num_blocks The number of blocks.
531  * @returns none
532  *
533  */
534 void
535 _mh_sha256_block_avx512(const uint8_t *input_data,
536                         uint32_t digests[ISAL_SHA256_DIGEST_WORDS][ISAL_HASH_SEGS],
537                         uint8_t frame_buffer[ISAL_MH_SHA256_BLOCK_SIZE], uint32_t num_blocks);
538 
539 #ifdef __cplusplus
540 }
541 #endif
542 
543 #endif
544