]>
git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/include/mh_sha1.h
1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
35 * @brief mh_sha1 function prototypes and structures
37 * Interface for mh_sha1 functions
39 * <b> mh_sha1 Init-Update..Update-Finalize </b>
41 * This file defines the interface to optimized functions used in mh_sha1.
42 * The definition of multi-hash SHA1(mh_sha1, for short) is: Pad the buffer
43 * in SHA1 style until the total length is a multiple of 4*16*16
44 * (words-width * parallel-segments * block-size); Hash the buffer in
45 * parallel, generating digests of 4*16*5 (words-width*parallel-segments*
46 * digest-size); Treat the set of digests as another data buffer, and
47 * generate a final SHA1 digest for it.
52 * uint32_t mh_sha1_digest[SHA1_DIGEST_WORDS];
53 * struct mh_sha1_ctx *ctx;
55 * ctx = malloc(sizeof(struct mh_sha1_ctx));
57 * mh_sha1_update(ctx, buff, block_len);
58 * mh_sha1_finalize(ctx, mh_sha1_digest);
69 // External Interface Definition
71 #define SHA1_BLOCK_SIZE 64
72 #define MH_SHA1_BLOCK_SIZE (HASH_SEGS * SHA1_BLOCK_SIZE)
73 #define SHA1_DIGEST_WORDS 5
74 #define AVX512_ALIGNED 64
76 /** @brief Holds info describing a single mh_sha1
78 * It is better to use heap to allocate this data structure to avoid stack overflow.
82 uint32_t mh_sha1_digest
[SHA1_DIGEST_WORDS
]; //!< the digest of multi-hash SHA1
84 uint64_t total_length
;
85 //!< Parameters for update feature, describe the lengths of input buffers in bytes
86 uint8_t partial_block_buffer
[MH_SHA1_BLOCK_SIZE
* 2];
87 //!< Padding the tail of input data for SHA1
88 uint8_t mh_sha1_interim_digests
[sizeof(uint32_t) * SHA1_DIGEST_WORDS
* HASH_SEGS
];
89 //!< Storing the SHA1 interim digests of all 16 segments. Each time, it will be copied to stack for 64-byte alignment purpose.
90 uint8_t frame_buffer
[MH_SHA1_BLOCK_SIZE
+ AVX512_ALIGNED
];
91 //!< Re-structure sha1 block data from different segments to fit big endian. Use AVX512_ALIGNED for 64-byte alignment purpose.
95 * @enum mh_sha1_ctx_error
96 * @brief CTX error flags
98 enum mh_sha1_ctx_error
{
99 MH_SHA1_CTX_ERROR_NONE
= 0, //!< MH_SHA1_MURMUR3_CTX_ERROR_NONE
100 MH_SHA1_CTX_ERROR_NULL
= -1, //!< MH_SHA1_MURMUR3_CTX_ERROR_NULL
104 /*******************************************************************
105 * mh_sha1 API function prototypes
106 ******************************************************************/
109 * @brief Initialize the mh_sha1_ctx structure.
111 * @param ctx Structure holding mh_sha1 info
112 * @returns int Return 0 if the function runs without errors
114 int mh_sha1_init (struct mh_sha1_ctx
* ctx
);
117 * @brief Multi-hash sha1 update.
119 * Can be called repeatedly to update hashes with new input data.
120 * This function determines what instruction sets are enabled and selects the
121 * appropriate version at runtime.
123 * @param ctx Structure holding mh_sha1 info
124 * @param buffer Pointer to buffer to be processed
125 * @param len Length of buffer (in bytes) to be processed
126 * @returns int Return 0 if the function runs without errors
128 int mh_sha1_update (struct mh_sha1_ctx
* ctx
, const void* buffer
, uint32_t len
);
131 * @brief Finalize the message digests for multi-hash sha1.
133 * Place the message digest in mh_sha1_digest which must have enough space
135 * This function determines what instruction sets are enabled and selects the
136 * appropriate version at runtime.
138 * @param ctx Structure holding mh_sha1 info
139 * @param mh_sha1_digest The digest of mh_sha1
140 * @returns int Return 0 if the function runs without errors
142 int mh_sha1_finalize (struct mh_sha1_ctx
* ctx
, void* mh_sha1_digest
);
144 /*******************************************************************
145 * multi-types of mh_sha1 internal API
147 * XXXX The multi-binary version
148 * XXXX_base The C code version which used to display the algorithm
149 * XXXX_sse The version uses a ASM function optimized for SSE
150 * XXXX_avx The version uses a ASM function optimized for AVX
151 * XXXX_avx2 The version uses a ASM function optimized for AVX2
152 * XXXX_avx512 The version uses a ASM function optimized for AVX512
154 ******************************************************************/
157 * @brief Multi-hash sha1 update.
159 * Can be called repeatedly to update hashes with new input data.
160 * Base update() function that does not require SIMD support.
162 * @param ctx Structure holding mh_sha1 info
163 * @param buffer Pointer to buffer to be processed
164 * @param len Length of buffer (in bytes) to be processed
165 * @returns int Return 0 if the function runs without errors
168 int mh_sha1_update_base (struct mh_sha1_ctx
* ctx
, const void* buffer
, uint32_t len
);
171 * @brief Multi-hash sha1 update.
173 * Can be called repeatedly to update hashes with new input data.
176 * @param ctx Structure holding mh_sha1 info
177 * @param buffer Pointer to buffer to be processed
178 * @param len Length of buffer (in bytes) to be processed
179 * @returns int Return 0 if the function runs without errors
182 int mh_sha1_update_sse (struct mh_sha1_ctx
* ctx
,
183 const void* buffer
, uint32_t len
);
186 * @brief Multi-hash sha1 update.
188 * Can be called repeatedly to update hashes with new input data.
191 * @param ctx Structure holding mh_sha1 info
192 * @param buffer Pointer to buffer to be processed
193 * @param len Length of buffer (in bytes) to be processed
194 * @returns int Return 0 if the function runs without errors
197 int mh_sha1_update_avx (struct mh_sha1_ctx
* ctx
,
198 const void* buffer
, uint32_t len
);
201 * @brief Multi-hash sha1 update.
203 * Can be called repeatedly to update hashes with new input data.
206 * @param ctx Structure holding mh_sha1 info
207 * @param buffer Pointer to buffer to be processed
208 * @param len Length of buffer (in bytes) to be processed
209 * @returns int Return 0 if the function runs without errors
212 int mh_sha1_update_avx2 (struct mh_sha1_ctx
* ctx
,
213 const void* buffer
, uint32_t len
);
216 * @brief Multi-hash sha1 update.
218 * Can be called repeatedly to update hashes with new input data.
221 * @param ctx Structure holding mh_sha1 info
222 * @param buffer Pointer to buffer to be processed
223 * @param len Length of buffer (in bytes) to be processed
224 * @returns int Return 0 if the function runs without errors
227 int mh_sha1_update_avx512 (struct mh_sha1_ctx
* ctx
,
228 const void* buffer
, uint32_t len
);
232 * @brief Finalize the message digests for multi-hash sha1.
234 * Place the message digests in mh_sha1_digest,
235 * which must have enough space for the outputs.
236 * Base Finalize() function that does not require SIMD support.
238 * @param ctx Structure holding mh_sha1 info
239 * @param mh_sha1_digest The digest of mh_sha1
240 * @returns int Return 0 if the function runs without errors
243 int mh_sha1_finalize_base (struct mh_sha1_ctx
* ctx
,
244 void* mh_sha1_digest
);
247 * @brief Finalize the message digests for combined multi-hash and murmur.
249 * Place the message digest in mh_sha1_digest which must have enough space
254 * @param ctx Structure holding mh_sha1 info
255 * @param mh_sha1_digest The digest of mh_sha1
256 * @returns int Return 0 if the function runs without errors
259 int mh_sha1_finalize_sse (struct mh_sha1_ctx
* ctx
,
260 void* mh_sha1_digest
);
263 * @brief Finalize the message digests for combined multi-hash and murmur.
265 * Place the message digest in mh_sha1_digest which must have enough space
270 * @param ctx Structure holding mh_sha1 info
271 * @param mh_sha1_digest The digest of mh_sha1
272 * @returns int Return 0 if the function runs without errors
275 int mh_sha1_finalize_avx (struct mh_sha1_ctx
* ctx
,
276 void* mh_sha1_digest
);
279 * @brief Finalize the message digests for combined multi-hash and murmur.
281 * Place the message digest in mh_sha1_digest which must have enough space
286 * @param ctx Structure holding mh_sha1 info
287 * @param mh_sha1_digest The digest of mh_sha1
288 * @returns int Return 0 if the function runs without errors
291 int mh_sha1_finalize_avx2 (struct mh_sha1_ctx
* ctx
,
292 void* mh_sha1_digest
);
295 * @brief Finalize the message digests for combined multi-hash and murmur.
297 * Place the message digest in mh_sha1_digest which must have enough space
302 * @param ctx Structure holding mh_sha1 info
303 * @param mh_sha1_digest The digest of mh_sha1
304 * @returns int Return 0 if the function runs without errors
307 int mh_sha1_finalize_avx512 (struct mh_sha1_ctx
* ctx
,
308 void* mh_sha1_digest
);