xref: /isa-l_crypto/include/mh_sha1.h (revision 46bddbb784bcf0d2d12d16395f565df9044ef0fc)
11b4d6043SGreg Tucker /**********************************************************************
21b4d6043SGreg Tucker   Copyright(c) 2011-2016 Intel Corporation All rights reserved.
31b4d6043SGreg Tucker 
41b4d6043SGreg Tucker   Redistribution and use in source and binary forms, with or without
51b4d6043SGreg Tucker   modification, are permitted provided that the following conditions
61b4d6043SGreg Tucker   are met:
71b4d6043SGreg Tucker     * Redistributions of source code must retain the above copyright
81b4d6043SGreg Tucker       notice, this list of conditions and the following disclaimer.
91b4d6043SGreg Tucker     * Redistributions in binary form must reproduce the above copyright
101b4d6043SGreg Tucker       notice, this list of conditions and the following disclaimer in
111b4d6043SGreg Tucker       the documentation and/or other materials provided with the
121b4d6043SGreg Tucker       distribution.
131b4d6043SGreg Tucker     * Neither the name of Intel Corporation nor the names of its
141b4d6043SGreg Tucker       contributors may be used to endorse or promote products derived
151b4d6043SGreg Tucker       from this software without specific prior written permission.
161b4d6043SGreg Tucker 
171b4d6043SGreg Tucker   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
181b4d6043SGreg Tucker   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191b4d6043SGreg Tucker   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
201b4d6043SGreg Tucker   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
211b4d6043SGreg Tucker   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
221b4d6043SGreg Tucker   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
231b4d6043SGreg Tucker   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241b4d6043SGreg Tucker   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251b4d6043SGreg Tucker   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261b4d6043SGreg Tucker   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
271b4d6043SGreg Tucker   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281b4d6043SGreg Tucker **********************************************************************/
291b4d6043SGreg Tucker 
301b4d6043SGreg Tucker #ifndef _MH_SHA1_H_
311b4d6043SGreg Tucker #define _MH_SHA1_H_
321b4d6043SGreg Tucker 
331b4d6043SGreg Tucker /**
341b4d6043SGreg Tucker  *  @file mh_sha1.h
351b4d6043SGreg Tucker  *  @brief mh_sha1 function prototypes and structures
361b4d6043SGreg Tucker  *
371b4d6043SGreg Tucker  *  Interface for mh_sha1 functions
381b4d6043SGreg Tucker  *
391b4d6043SGreg Tucker  * <b> mh_sha1  Init-Update..Update-Finalize </b>
401b4d6043SGreg Tucker  *
411b4d6043SGreg Tucker  * This file defines the interface to optimized functions used in mh_sha1.
421b4d6043SGreg Tucker  * The definition of multi-hash SHA1(mh_sha1, for short) is: Pad the buffer
431b4d6043SGreg Tucker  * in SHA1 style until the total length is a multiple of 4*16*16
441b4d6043SGreg Tucker  * (words-width * parallel-segments * block-size); Hash the buffer in
451b4d6043SGreg Tucker  * parallel, generating digests of 4*16*5 (words-width*parallel-segments*
461b4d6043SGreg Tucker  * digest-size); Treat the set of digests as another data buffer, and
471b4d6043SGreg Tucker  * generate a final SHA1 digest for it.
481b4d6043SGreg Tucker  *
491b4d6043SGreg Tucker  *
501b4d6043SGreg Tucker  * Example
511b4d6043SGreg Tucker  * \code
5227316f25SMarcel Cornu  * uint32_t mh_sha1_digest[ISAL_SHA1_DIGEST_WORDS];
53*46bddbb7SMarcel Cornu  * struct isal_mh_sha1_ctx *ctx;
541b4d6043SGreg Tucker  *
55*46bddbb7SMarcel Cornu  * ctx = malloc(sizeof(struct isal_mh_sha1_ctx));
560cc393e5SMarcel Cornu  * isal_mh_sha1_init(ctx);
570cc393e5SMarcel Cornu  * isal_mh_sha1_update(ctx, buff, block_len);
580cc393e5SMarcel Cornu  * isal_mh_sha1_finalize(ctx, mh_sha1_digest);
591b4d6043SGreg Tucker  * \endcode
601b4d6043SGreg Tucker  */
611b4d6043SGreg Tucker 
621b4d6043SGreg Tucker #include <stdint.h>
637b1c723eSMarcel Cornu #include "types.h"
641b4d6043SGreg Tucker 
651b4d6043SGreg Tucker #ifdef __cplusplus
661b4d6043SGreg Tucker extern "C" {
671b4d6043SGreg Tucker #endif
681b4d6043SGreg Tucker 
6927316f25SMarcel Cornu /*
7027316f25SMarcel Cornu  * Define enums from API v2.24, so applications that were using this version
7127316f25SMarcel Cornu  * will still be compiled successfully.
7227316f25SMarcel Cornu  * This list does not need to be extended for new definitions.
7327316f25SMarcel Cornu  */
7427316f25SMarcel Cornu #ifndef NO_COMPAT_ISAL_CRYPTO_API_2_24
7527316f25SMarcel Cornu /***** Previous hash constants and typedefs *****/
7627316f25SMarcel Cornu #define HASH_SEGS          ISAL_HASH_SEGS
7727316f25SMarcel Cornu #define SHA1_BLOCK_SIZE    ISAL_SHA1_BLOCK_SIZE
7827316f25SMarcel Cornu #define MH_SHA1_BLOCK_SIZE ISAL_MH_SHA1_BLOCK_SIZE
7927316f25SMarcel Cornu #define SHA1_DIGEST_WORDS  ISAL_SHA1_DIGEST_WORDS
8027316f25SMarcel Cornu #define AVX512_ALIGNED     ISAL_AVX512_ALIGNED
8127316f25SMarcel Cornu 
8227316f25SMarcel Cornu #define MH_SHA1_CTX_ERROR_NONE ISAL_MH_SHA1_CTX_ERROR_NONE
8327316f25SMarcel Cornu #define MH_SHA1_CTX_ERROR_NULL ISAL_MH_SHA1_CTX_ERROR_NULL
84*46bddbb7SMarcel Cornu 
85*46bddbb7SMarcel Cornu #define mh_sha1_ctx isal_mh_sha1_ctx
8627316f25SMarcel Cornu #endif /* !NO_COMPAT_ISAL_CRYPTO_API_2_24 */
8727316f25SMarcel Cornu 
881b4d6043SGreg Tucker // External Interface Definition
8927316f25SMarcel Cornu #define ISAL_HASH_SEGS          16
9027316f25SMarcel Cornu #define ISAL_SHA1_BLOCK_SIZE    64
9127316f25SMarcel Cornu #define ISAL_MH_SHA1_BLOCK_SIZE (ISAL_HASH_SEGS * ISAL_SHA1_BLOCK_SIZE)
9227316f25SMarcel Cornu #define ISAL_SHA1_DIGEST_WORDS  5
9327316f25SMarcel Cornu #define ISAL_AVX512_ALIGNED     64
941b4d6043SGreg Tucker 
951b4d6043SGreg Tucker /** @brief Holds info describing a single mh_sha1
961b4d6043SGreg Tucker  *
971b4d6043SGreg Tucker  * It is better to use heap to allocate this data structure to avoid stack overflow.
981b4d6043SGreg Tucker  *
991b4d6043SGreg Tucker  */
100*46bddbb7SMarcel Cornu struct isal_mh_sha1_ctx {
10127316f25SMarcel Cornu         uint32_t mh_sha1_digest[ISAL_SHA1_DIGEST_WORDS]; //!< the digest of multi-hash SHA1
1021b4d6043SGreg Tucker 
1031b4d6043SGreg Tucker         uint64_t total_length;
1041b4d6043SGreg Tucker         //!<  Parameters for update feature, describe the lengths of input buffers in bytes
10527316f25SMarcel Cornu         uint8_t partial_block_buffer[ISAL_MH_SHA1_BLOCK_SIZE * 2];
1061b4d6043SGreg Tucker         //!<  Padding the tail of input data for SHA1
10727316f25SMarcel Cornu         uint8_t mh_sha1_interim_digests[sizeof(uint32_t) * ISAL_SHA1_DIGEST_WORDS * ISAL_HASH_SEGS];
1081de5344dSMarcel Cornu         //!<  Storing the SHA1 interim digests of  all 16 segments. Each time, it will be copied to
1091de5344dSMarcel Cornu         //!<  stack for 64-byte alignment purpose.
11027316f25SMarcel Cornu         uint8_t frame_buffer[ISAL_MH_SHA1_BLOCK_SIZE + ISAL_AVX512_ALIGNED];
1111de5344dSMarcel Cornu         //!<  Re-structure sha1 block data from different segments to fit big endian. Use
11227316f25SMarcel Cornu         //!<  ISAL_AVX512_ALIGNED for 64-byte alignment purpose.
1131b4d6043SGreg Tucker };
1141b4d6043SGreg Tucker 
1151b4d6043SGreg Tucker /**
116*46bddbb7SMarcel Cornu  *  @enum isal_mh_sha1_ctx_error
1171b4d6043SGreg Tucker  *  @brief CTX error flags
1181b4d6043SGreg Tucker  */
119*46bddbb7SMarcel Cornu enum isal_mh_sha1_ctx_error {
12027316f25SMarcel Cornu         ISAL_MH_SHA1_CTX_ERROR_NONE = 0,  //!< ISAL_MH_SHA1_CTX_ERROR_NONE
12127316f25SMarcel Cornu         ISAL_MH_SHA1_CTX_ERROR_NULL = -1, //!< ISAL_MH_SHA1_CTX_ERROR_NULL
1221b4d6043SGreg Tucker };
1231b4d6043SGreg Tucker 
1241b4d6043SGreg Tucker /*******************************************************************
1251b4d6043SGreg Tucker  * mh_sha1 API function prototypes
1261b4d6043SGreg Tucker  ******************************************************************/
1271b4d6043SGreg Tucker 
1281b4d6043SGreg Tucker /**
129*46bddbb7SMarcel Cornu  * @brief Initialize the isal_mh_sha1_ctx structure.
1301b4d6043SGreg Tucker  *
1311b4d6043SGreg Tucker  * @param  ctx Structure holding mh_sha1 info
1321b4d6043SGreg Tucker  * @returns int Return 0 if the function runs without errors
1337b1c723eSMarcel Cornu  * @deprecated Please use isal_mh_sha1_init() instead.
1341b4d6043SGreg Tucker  */
1357b1c723eSMarcel Cornu ISAL_DEPRECATED("Please use isal_mh_sha1_init() instead")
1361de5344dSMarcel Cornu int
137*46bddbb7SMarcel Cornu mh_sha1_init(struct isal_mh_sha1_ctx *ctx);
1381b4d6043SGreg Tucker 
1391b4d6043SGreg Tucker /**
1401b4d6043SGreg Tucker  * @brief Multi-hash sha1 update.
1411b4d6043SGreg Tucker  *
1421b4d6043SGreg Tucker  * Can be called repeatedly to update hashes with new input data.
1431b4d6043SGreg Tucker  * This function determines what instruction sets are enabled and selects the
1441b4d6043SGreg Tucker  * appropriate version at runtime.
1451b4d6043SGreg Tucker  *
1461b4d6043SGreg Tucker  * @param  ctx Structure holding mh_sha1 info
1471b4d6043SGreg Tucker  * @param  buffer Pointer to buffer to be processed
1481b4d6043SGreg Tucker  * @param  len Length of buffer (in bytes) to be processed
1491b4d6043SGreg Tucker  * @returns int Return 0 if the function runs without errors
1507b1c723eSMarcel Cornu  * @deprecated Please use isal_mh_sha1_update() instead.
1511b4d6043SGreg Tucker  */
1527b1c723eSMarcel Cornu ISAL_DEPRECATED("Please use isal_mh_sha1_update() instead")
1531de5344dSMarcel Cornu int
154*46bddbb7SMarcel Cornu mh_sha1_update(struct isal_mh_sha1_ctx *ctx, const void *buffer, uint32_t len);
1551b4d6043SGreg Tucker 
1561b4d6043SGreg Tucker /**
1571b4d6043SGreg Tucker  * @brief Finalize the message digests for multi-hash sha1.
1581b4d6043SGreg Tucker  *
1591b4d6043SGreg Tucker  * Place the message digest in mh_sha1_digest which must have enough space
1601b4d6043SGreg Tucker  * for the outputs.
1611b4d6043SGreg Tucker  * This function determines what instruction sets are enabled and selects the
1621b4d6043SGreg Tucker  * appropriate version at runtime.
1631b4d6043SGreg Tucker  *
1641b4d6043SGreg Tucker  * @param   ctx Structure holding mh_sha1 info
1651b4d6043SGreg Tucker  * @param   mh_sha1_digest The digest of mh_sha1
1661b4d6043SGreg Tucker  * @returns int Return 0 if the function runs without errors
1677b1c723eSMarcel Cornu  * @deprecated Please use isal_mh_sha1_finalize() instead.
1681b4d6043SGreg Tucker  */
1697b1c723eSMarcel Cornu ISAL_DEPRECATED("Please use isal_mh_sha1_finalize() instead")
1701de5344dSMarcel Cornu int
171*46bddbb7SMarcel Cornu mh_sha1_finalize(struct isal_mh_sha1_ctx *ctx, void *mh_sha1_digest);
1721b4d6043SGreg Tucker 
1731b4d6043SGreg Tucker /**
1741b4d6043SGreg Tucker  * @brief Multi-hash sha1 update.
1751b4d6043SGreg Tucker  *
1761b4d6043SGreg Tucker  * Can be called repeatedly to update hashes with new input data.
1771b4d6043SGreg Tucker  * Base update() function that does not require SIMD support.
1781b4d6043SGreg Tucker  *
1791b4d6043SGreg Tucker  * @param   ctx Structure holding mh_sha1 info
1801b4d6043SGreg Tucker  * @param   buffer Pointer to buffer to be processed
1811b4d6043SGreg Tucker  * @param   len Length of buffer (in bytes) to be processed
1821b4d6043SGreg Tucker  * @returns int Return 0 if the function runs without errors
1837b1c723eSMarcel Cornu  * @deprecated Please use isal_mh_sha1_update() instead.
1841b4d6043SGreg Tucker  */
1851de5344dSMarcel Cornu int
186*46bddbb7SMarcel Cornu mh_sha1_update_base(struct isal_mh_sha1_ctx *ctx, const void *buffer, uint32_t len);
1871b4d6043SGreg Tucker 
1881b4d6043SGreg Tucker /**
1891b4d6043SGreg Tucker  * @brief Finalize the message digests for multi-hash sha1.
1901b4d6043SGreg Tucker  *
1911b4d6043SGreg Tucker  * Place the message digests in mh_sha1_digest,
1921b4d6043SGreg Tucker  * which must have enough space for the outputs.
1931b4d6043SGreg Tucker  * Base Finalize() function that does not require SIMD support.
1941b4d6043SGreg Tucker  *
1951b4d6043SGreg Tucker  * @param   ctx Structure holding mh_sha1 info
1961b4d6043SGreg Tucker  * @param   mh_sha1_digest The digest of mh_sha1
1971b4d6043SGreg Tucker  * @returns int Return 0 if the function runs without errors
1987b1c723eSMarcel Cornu  * @deprecated Please use isal_mh_sha1_finalize() instead.
1991b4d6043SGreg Tucker  */
2001de5344dSMarcel Cornu int
201*46bddbb7SMarcel Cornu mh_sha1_finalize_base(struct isal_mh_sha1_ctx *ctx, void *mh_sha1_digest);
2021b4d6043SGreg Tucker 
2031b4d6043SGreg Tucker /**
204*46bddbb7SMarcel Cornu  * @brief Initialize the isal_mh_sha1_ctx structure.
205f80afdf4SMarcel Cornu  *
206f80afdf4SMarcel Cornu  * @param  ctx Structure holding mh_sha1 info
207f80afdf4SMarcel Cornu  * @return Operation status
208f80afdf4SMarcel Cornu  * @retval 0 on success
209f80afdf4SMarcel Cornu  * @retval Non-zero \a ISAL_CRYPTO_ERR on failure
210f80afdf4SMarcel Cornu  */
2111de5344dSMarcel Cornu int
212*46bddbb7SMarcel Cornu isal_mh_sha1_init(struct isal_mh_sha1_ctx *ctx);
213f80afdf4SMarcel Cornu 
214f80afdf4SMarcel Cornu /**
215f80afdf4SMarcel Cornu  * @brief Multi-hash sha1 update.
216f80afdf4SMarcel Cornu  *
217f80afdf4SMarcel Cornu  * Can be called repeatedly to update hashes with new input data.
218f80afdf4SMarcel Cornu  * This function determines what instruction sets are enabled and selects the
219f80afdf4SMarcel Cornu  * appropriate version at runtime.
220f80afdf4SMarcel Cornu  *
221f80afdf4SMarcel Cornu  * @param  ctx Structure holding mh_sha1 info
222f80afdf4SMarcel Cornu  * @param  buffer Pointer to buffer to be processed
223f80afdf4SMarcel Cornu  * @param  len Length of buffer (in bytes) to be processed
224f80afdf4SMarcel Cornu  * @return Operation status
225f80afdf4SMarcel Cornu  * @retval 0 on success
226f80afdf4SMarcel Cornu  * @retval Non-zero \a ISAL_CRYPTO_ERR on failure
227f80afdf4SMarcel Cornu  */
2281de5344dSMarcel Cornu int
229*46bddbb7SMarcel Cornu isal_mh_sha1_update(struct isal_mh_sha1_ctx *ctx, const void *buffer, uint32_t len);
230f80afdf4SMarcel Cornu 
231f80afdf4SMarcel Cornu /**
232f80afdf4SMarcel Cornu  * @brief Finalize the message digests for multi-hash sha1.
233f80afdf4SMarcel Cornu  *
234f80afdf4SMarcel Cornu  * Place the message digest in mh_sha1_digest which must have enough space
235f80afdf4SMarcel Cornu  * for the outputs.
236f80afdf4SMarcel Cornu  * This function determines what instruction sets are enabled and selects the
237f80afdf4SMarcel Cornu  * appropriate version at runtime.
238f80afdf4SMarcel Cornu  *
239f80afdf4SMarcel Cornu  * @param  ctx Structure holding mh_sha1 info
240f80afdf4SMarcel Cornu  * @param  mh_sha1_digest The digest of mh_sha1
241f80afdf4SMarcel Cornu  * @return Operation status
242f80afdf4SMarcel Cornu  * @retval 0 on success
243f80afdf4SMarcel Cornu  * @retval Non-zero \a ISAL_CRYPTO_ERR on failure
244f80afdf4SMarcel Cornu  */
2451de5344dSMarcel Cornu int
246*46bddbb7SMarcel Cornu isal_mh_sha1_finalize(struct isal_mh_sha1_ctx *ctx, void *mh_sha1_digest);
247f80afdf4SMarcel Cornu 
2481b4d6043SGreg Tucker #ifdef __cplusplus
2491b4d6043SGreg Tucker }
2501b4d6043SGreg Tucker #endif
2511b4d6043SGreg Tucker 
2521b4d6043SGreg Tucker #endif
253