17dc32ad5SXiaodong Liu /********************************************************************** 27dc32ad5SXiaodong Liu Copyright(c) 2011-2017 Intel Corporation All rights reserved. 37dc32ad5SXiaodong Liu 47dc32ad5SXiaodong Liu Redistribution and use in source and binary forms, with or without 57dc32ad5SXiaodong Liu modification, are permitted provided that the following conditions 67dc32ad5SXiaodong Liu are met: 77dc32ad5SXiaodong Liu * Redistributions of source code must retain the above copyright 87dc32ad5SXiaodong Liu notice, this list of conditions and the following disclaimer. 97dc32ad5SXiaodong Liu * Redistributions in binary form must reproduce the above copyright 107dc32ad5SXiaodong Liu notice, this list of conditions and the following disclaimer in 117dc32ad5SXiaodong Liu the documentation and/or other materials provided with the 127dc32ad5SXiaodong Liu distribution. 137dc32ad5SXiaodong Liu * Neither the name of Intel Corporation nor the names of its 147dc32ad5SXiaodong Liu contributors may be used to endorse or promote products derived 157dc32ad5SXiaodong Liu from this software without specific prior written permission. 167dc32ad5SXiaodong Liu 177dc32ad5SXiaodong Liu THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 187dc32ad5SXiaodong Liu "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 197dc32ad5SXiaodong Liu LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 207dc32ad5SXiaodong Liu A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 217dc32ad5SXiaodong Liu OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 227dc32ad5SXiaodong Liu SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 237dc32ad5SXiaodong Liu LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 247dc32ad5SXiaodong Liu DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 257dc32ad5SXiaodong Liu THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 267dc32ad5SXiaodong Liu (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 277dc32ad5SXiaodong Liu OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 287dc32ad5SXiaodong Liu **********************************************************************/ 297dc32ad5SXiaodong Liu 307dc32ad5SXiaodong Liu #ifndef _MH_SHA256_H_ 317dc32ad5SXiaodong Liu #define _MH_SHA256_H_ 327dc32ad5SXiaodong Liu 337dc32ad5SXiaodong Liu /** 347dc32ad5SXiaodong Liu * @file mh_sha256.h 357dc32ad5SXiaodong Liu * @brief mh_sha256 function prototypes and structures 367dc32ad5SXiaodong Liu * 377dc32ad5SXiaodong Liu * Interface for mh_sha256 functions 387dc32ad5SXiaodong Liu * 397dc32ad5SXiaodong Liu * <b> mh_sha256 Init-Update..Update-Finalize </b> 407dc32ad5SXiaodong Liu * 417dc32ad5SXiaodong Liu * This file defines the interface to optimized functions used in mh_sha256. 427dc32ad5SXiaodong Liu * The definition of multi-hash SHA256(mh_sha256, for short) is: Pad the buffer 437dc32ad5SXiaodong Liu * in SHA256 style until the total length is a multiple of 4*16*16 447dc32ad5SXiaodong Liu * (words-width * parallel-segments * block-size); Hash the buffer in 457dc32ad5SXiaodong Liu * parallel, generating digests of 4*16*8 (words-width*parallel-segments* 467dc32ad5SXiaodong Liu * digest-size); Treat the set of digests as another data buffer, and 477dc32ad5SXiaodong Liu * generate a final SHA256 digest for it. 487dc32ad5SXiaodong Liu * 497dc32ad5SXiaodong Liu * 507dc32ad5SXiaodong Liu * Example 517dc32ad5SXiaodong Liu * \code 5215f45959SMarcel Cornu * uint32_t mh_sha256_digest[ISAL_SHA256_DIGEST_WORDS]; 53*f6da0bf4SMarcel Cornu * struct isal_mh_sha256_ctx *ctx; 547dc32ad5SXiaodong Liu * 55*f6da0bf4SMarcel Cornu * ctx = malloc(sizeof(struct isal_mh_sha256_ctx)); 5617a3a6ccSMarcel Cornu * isal_mh_sha256_init(ctx); 5717a3a6ccSMarcel Cornu * isal_mh_sha256_update(ctx, buff, block_len); 5817a3a6ccSMarcel Cornu * isal_mh_sha256_finalize(ctx, mh_sha256_digest); 597dc32ad5SXiaodong Liu * \endcode 607dc32ad5SXiaodong Liu */ 617dc32ad5SXiaodong Liu 627dc32ad5SXiaodong Liu #include <stdint.h> 6317a3a6ccSMarcel Cornu #include "types.h" 647dc32ad5SXiaodong Liu 657dc32ad5SXiaodong Liu #ifdef __cplusplus 667dc32ad5SXiaodong Liu extern "C" { 677dc32ad5SXiaodong Liu #endif 687dc32ad5SXiaodong Liu 6915f45959SMarcel Cornu /* 7015f45959SMarcel Cornu * Define enums from API v2.24, so applications that were using this version 7115f45959SMarcel Cornu * will still be compiled successfully. 7215f45959SMarcel Cornu * This list does not need to be extended for new definitions. 7315f45959SMarcel Cornu */ 7415f45959SMarcel Cornu #ifndef NO_COMPAT_ISAL_CRYPTO_API_2_24 7515f45959SMarcel Cornu /***** Previous hash constants and typedefs *****/ 7615f45959SMarcel Cornu #define HASH_SEGS ISAL_HASH_SEGS 7715f45959SMarcel Cornu #define SHA256_BLOCK_SIZE ISAL_SHA256_BLOCK_SIZE 7815f45959SMarcel Cornu #define MH_SHA256_BLOCK_SIZE ISAL_MH_SHA256_BLOCK_SIZE 7915f45959SMarcel Cornu #define SHA256_DIGEST_WORDS ISAL_SHA256_DIGEST_WORDS 8015f45959SMarcel Cornu #define AVX512_ALIGNED ISAL_AVX512_ALIGNED 8115f45959SMarcel Cornu 8215f45959SMarcel Cornu #define MH_SHA256_CTX_ERROR_NONE ISAL_MH_SHA256_CTX_ERROR_NONE 8315f45959SMarcel Cornu #define MH_SHA256_CTX_ERROR_NULL ISAL_MH_SHA256_CTX_ERROR_NULL 84*f6da0bf4SMarcel Cornu 85*f6da0bf4SMarcel Cornu #define mh_sha256_ctx isal_mh_sha256_ctx 8615f45959SMarcel Cornu #endif /* !NO_COMPAT_ISAL_CRYPTO_API_2_24 */ 8715f45959SMarcel Cornu 887dc32ad5SXiaodong Liu // External Interface Definition 8927316f25SMarcel Cornu #define ISAL_HASH_SEGS 16 9015f45959SMarcel Cornu #define ISAL_SHA256_BLOCK_SIZE 64 9115f45959SMarcel Cornu #define ISAL_MH_SHA256_BLOCK_SIZE (ISAL_HASH_SEGS * ISAL_SHA256_BLOCK_SIZE) 9215f45959SMarcel Cornu #define ISAL_SHA256_DIGEST_WORDS 8 9327316f25SMarcel Cornu #define ISAL_AVX512_ALIGNED 64 947dc32ad5SXiaodong Liu 957dc32ad5SXiaodong Liu /** @brief Holds info describing a single mh_sha256 967dc32ad5SXiaodong Liu * 977dc32ad5SXiaodong Liu * It is better to use heap to allocate this data structure to avoid stack overflow. 987dc32ad5SXiaodong Liu * 997dc32ad5SXiaodong Liu */ 100*f6da0bf4SMarcel Cornu struct isal_mh_sha256_ctx { 10115f45959SMarcel Cornu uint32_t mh_sha256_digest[ISAL_SHA256_DIGEST_WORDS]; //!< the digest of multi-hash SHA256 1027dc32ad5SXiaodong Liu 1037dc32ad5SXiaodong Liu uint64_t total_length; 1047dc32ad5SXiaodong Liu //!< Parameters for update feature, describe the lengths of input buffers in bytes 10515f45959SMarcel Cornu uint8_t partial_block_buffer[ISAL_MH_SHA256_BLOCK_SIZE * 2]; 1067dc32ad5SXiaodong Liu //!< Padding the tail of input data for SHA256 10715f45959SMarcel Cornu uint8_t mh_sha256_interim_digests[sizeof(uint32_t) * ISAL_SHA256_DIGEST_WORDS * 10815f45959SMarcel Cornu ISAL_HASH_SEGS]; 1091de5344dSMarcel Cornu //!< Storing the SHA256 interim digests of all 16 segments. Each time, it will be copied 1101de5344dSMarcel Cornu //!< to stack for 64-byte alignment purpose. 11115f45959SMarcel Cornu uint8_t frame_buffer[ISAL_MH_SHA256_BLOCK_SIZE + ISAL_AVX512_ALIGNED]; 1121de5344dSMarcel Cornu //!< Re-structure sha256 block data from different segments to fit big endian. Use 11327316f25SMarcel Cornu //!< ISAL_AVX512_ALIGNED for 64-byte alignment purpose. 1147dc32ad5SXiaodong Liu }; 1157dc32ad5SXiaodong Liu 1167dc32ad5SXiaodong Liu /** 117*f6da0bf4SMarcel Cornu * @enum isal_mh_sha256_ctx_error 1187dc32ad5SXiaodong Liu * @brief CTX error flags 1197dc32ad5SXiaodong Liu */ 120*f6da0bf4SMarcel Cornu enum isal_mh_sha256_ctx_error { 12115f45959SMarcel Cornu ISAL_MH_SHA256_CTX_ERROR_NONE = 0, //!< ISAL_MH_SHA256_CTX_ERROR_NONE 12215f45959SMarcel Cornu ISAL_MH_SHA256_CTX_ERROR_NULL = -1, //!< ISAL_MH_SHA256_CTX_ERROR_NULL 1237dc32ad5SXiaodong Liu }; 1247dc32ad5SXiaodong Liu 1257dc32ad5SXiaodong Liu /******************************************************************* 1267dc32ad5SXiaodong Liu * mh_sha256 API function prototypes 1277dc32ad5SXiaodong Liu ******************************************************************/ 1287dc32ad5SXiaodong Liu 1297dc32ad5SXiaodong Liu /** 130*f6da0bf4SMarcel Cornu * @brief Initialize the isal_mh_sha256_ctx structure. 1317dc32ad5SXiaodong Liu * 1327dc32ad5SXiaodong Liu * @param ctx Structure holding mh_sha256 info 1337dc32ad5SXiaodong Liu * @returns int Return 0 if the function runs without errors 13417a3a6ccSMarcel Cornu * @deprecated Please use isal_mh_sha256_init() instead. 1357dc32ad5SXiaodong Liu */ 13617a3a6ccSMarcel Cornu ISAL_DEPRECATED("Please use isal_mh_sha256_init() instead") 1371de5344dSMarcel Cornu int 138*f6da0bf4SMarcel Cornu mh_sha256_init(struct isal_mh_sha256_ctx *ctx); 1397dc32ad5SXiaodong Liu 1407dc32ad5SXiaodong Liu /** 1417dc32ad5SXiaodong Liu * @brief Multi-hash sha256 update. 1427dc32ad5SXiaodong Liu * 1437dc32ad5SXiaodong Liu * Can be called repeatedly to update hashes with new input data. 1447dc32ad5SXiaodong Liu * This function determines what instruction sets are enabled and selects the 1457dc32ad5SXiaodong Liu * appropriate version at runtime. 1467dc32ad5SXiaodong Liu * 1477dc32ad5SXiaodong Liu * @param ctx Structure holding mh_sha256 info 1487dc32ad5SXiaodong Liu * @param buffer Pointer to buffer to be processed 1497dc32ad5SXiaodong Liu * @param len Length of buffer (in bytes) to be processed 1507dc32ad5SXiaodong Liu * @returns int Return 0 if the function runs without errors 15117a3a6ccSMarcel Cornu * @deprecated Please use isal_mh_sha256_update() instead. 1527dc32ad5SXiaodong Liu */ 15317a3a6ccSMarcel Cornu ISAL_DEPRECATED("Please use isal_mh_sha256_update() instead") 1541de5344dSMarcel Cornu int 155*f6da0bf4SMarcel Cornu mh_sha256_update(struct isal_mh_sha256_ctx *ctx, const void *buffer, uint32_t len); 1567dc32ad5SXiaodong Liu 1577dc32ad5SXiaodong Liu /** 1587dc32ad5SXiaodong Liu * @brief Finalize the message digests for multi-hash sha256. 1597dc32ad5SXiaodong Liu * 1607dc32ad5SXiaodong Liu * Place the message digest in mh_sha256_digest which must have enough space 1617dc32ad5SXiaodong Liu * for the outputs. 1627dc32ad5SXiaodong Liu * This function determines what instruction sets are enabled and selects the 1637dc32ad5SXiaodong Liu * appropriate version at runtime. 1647dc32ad5SXiaodong Liu * 1657dc32ad5SXiaodong Liu * @param ctx Structure holding mh_sha256 info 1667dc32ad5SXiaodong Liu * @param mh_sha256_digest The digest of mh_sha256 1677dc32ad5SXiaodong Liu * @returns int Return 0 if the function runs without errors 16817a3a6ccSMarcel Cornu * @deprecated Please use isal_mh_sha256_finalize() instead. 1697dc32ad5SXiaodong Liu */ 17017a3a6ccSMarcel Cornu ISAL_DEPRECATED("Please use isal_mh_sha256_finalize() instead") 1711de5344dSMarcel Cornu int 172*f6da0bf4SMarcel Cornu mh_sha256_finalize(struct isal_mh_sha256_ctx *ctx, void *mh_sha256_digest); 1737dc32ad5SXiaodong Liu 1747dc32ad5SXiaodong Liu /** 1757dc32ad5SXiaodong Liu * @brief Multi-hash sha256 update. 1767dc32ad5SXiaodong Liu * 1777dc32ad5SXiaodong Liu * Can be called repeatedly to update hashes with new input data. 1787dc32ad5SXiaodong Liu * Base update() function that does not require SIMD support. 1797dc32ad5SXiaodong Liu * 1807dc32ad5SXiaodong Liu * @param ctx Structure holding mh_sha256 info 1817dc32ad5SXiaodong Liu * @param buffer Pointer to buffer to be processed 1827dc32ad5SXiaodong Liu * @param len Length of buffer (in bytes) to be processed 1837dc32ad5SXiaodong Liu * @returns int Return 0 if the function runs without errors 18417a3a6ccSMarcel Cornu * @deprecated Please use isal_mh_sha256_update() instead. 1857dc32ad5SXiaodong Liu */ 1861de5344dSMarcel Cornu int 187*f6da0bf4SMarcel Cornu mh_sha256_update_base(struct isal_mh_sha256_ctx *ctx, const void *buffer, uint32_t len); 1887dc32ad5SXiaodong Liu 1897dc32ad5SXiaodong Liu /** 1907dc32ad5SXiaodong Liu * @brief Finalize the message digests for multi-hash sha256. 1917dc32ad5SXiaodong Liu * 1927dc32ad5SXiaodong Liu * Place the message digests in mh_sha256_digest, 1937dc32ad5SXiaodong Liu * which must have enough space for the outputs. 1947dc32ad5SXiaodong Liu * Base Finalize() function that does not require SIMD support. 1957dc32ad5SXiaodong Liu * 1967dc32ad5SXiaodong Liu * @param ctx Structure holding mh_sha256 info 1977dc32ad5SXiaodong Liu * @param mh_sha256_digest The digest of mh_sha256 1987dc32ad5SXiaodong Liu * @returns int Return 0 if the function runs without errors 19917a3a6ccSMarcel Cornu * @deprecated Please use isal_mh_sha256_finalize() instead. 2007dc32ad5SXiaodong Liu */ 2011de5344dSMarcel Cornu int 202*f6da0bf4SMarcel Cornu mh_sha256_finalize_base(struct isal_mh_sha256_ctx *ctx, void *mh_sha256_digest); 2037dc32ad5SXiaodong Liu 2047dc32ad5SXiaodong Liu /** 205*f6da0bf4SMarcel Cornu * @brief Initialize the isal_mh_sha256_ctx structure. 2069de7f4ddSMarcel Cornu * 2079de7f4ddSMarcel Cornu * @param ctx Structure holding mh_sha256 info 2089de7f4ddSMarcel Cornu * @return Operation status 2099de7f4ddSMarcel Cornu * @retval 0 on success 2109de7f4ddSMarcel Cornu * @retval Non-zero \a ISAL_CRYPTO_ERR on failure 2119de7f4ddSMarcel Cornu */ 2121de5344dSMarcel Cornu int 213*f6da0bf4SMarcel Cornu isal_mh_sha256_init(struct isal_mh_sha256_ctx *ctx); 2149de7f4ddSMarcel Cornu 2159de7f4ddSMarcel Cornu /** 2169de7f4ddSMarcel Cornu * @brief Multi-hash sha256 update. 2179de7f4ddSMarcel Cornu * 2189de7f4ddSMarcel Cornu * Can be called repeatedly to update hashes with new input data. 2199de7f4ddSMarcel Cornu * This function determines what instruction sets are enabled and selects the 2209de7f4ddSMarcel Cornu * appropriate version at runtime. 2219de7f4ddSMarcel Cornu * 2229de7f4ddSMarcel Cornu * @param ctx Structure holding mh_sha256 info 2239de7f4ddSMarcel Cornu * @param buffer Pointer to buffer to be processed 2249de7f4ddSMarcel Cornu * @param len Length of buffer (in bytes) to be processed 2259de7f4ddSMarcel Cornu * @return Operation status 2269de7f4ddSMarcel Cornu * @retval 0 on success 2279de7f4ddSMarcel Cornu * @retval Non-zero \a ISAL_CRYPTO_ERR on failure 2289de7f4ddSMarcel Cornu */ 2291de5344dSMarcel Cornu int 230*f6da0bf4SMarcel Cornu isal_mh_sha256_update(struct isal_mh_sha256_ctx *ctx, const void *buffer, uint32_t len); 2319de7f4ddSMarcel Cornu 2329de7f4ddSMarcel Cornu /** 2339de7f4ddSMarcel Cornu * @brief Finalize the message digests for multi-hash sha256. 2349de7f4ddSMarcel Cornu * 2359de7f4ddSMarcel Cornu * Place the message digest in mh_sha256_digest which must have enough space 2369de7f4ddSMarcel Cornu * for the outputs. 2379de7f4ddSMarcel Cornu * This function determines what instruction sets are enabled and selects the 2389de7f4ddSMarcel Cornu * appropriate version at runtime. 2399de7f4ddSMarcel Cornu * 2409de7f4ddSMarcel Cornu * @param ctx Structure holding mh_sha256 info 2419de7f4ddSMarcel Cornu * @param mh_sha256_digest The digest of mh_sha256 2429de7f4ddSMarcel Cornu * @return Operation status 2439de7f4ddSMarcel Cornu * @retval 0 on success 2449de7f4ddSMarcel Cornu * @retval Non-zero \a ISAL_CRYPTO_ERR on failure 2459de7f4ddSMarcel Cornu */ 2461de5344dSMarcel Cornu int 247*f6da0bf4SMarcel Cornu isal_mh_sha256_finalize(struct isal_mh_sha256_ctx *ctx, void *mh_sha256_digest); 2489de7f4ddSMarcel Cornu 2497dc32ad5SXiaodong Liu #ifdef __cplusplus 2507dc32ad5SXiaodong Liu } 2517dc32ad5SXiaodong Liu #endif 2527dc32ad5SXiaodong Liu 2537dc32ad5SXiaodong Liu #endif 254