]> git.proxmox.com Git - ceph.git/blame - ceph/src/crypto/isa-l/isa-l_crypto/sha1_mb/sha1_ctx_avx2.c
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / sha1_mb / sha1_ctx_avx2.c
CommitLineData
7c673cae
FG
1/**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
1e59de90 5 modification, are permitted provided that the following conditions
7c673cae
FG
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
1e59de90
TL
30#if defined(__clang__)
31# pragma clang attribute push (__attribute__((target("avx2"))), apply_to=function)
32#elif defined(__ICC)
33# pragma intel optimization_parameter target_arch=AVX2
34#elif defined(__ICL)
35# pragma [intel] optimization_parameter target_arch=AVX2
36#elif (__GNUC__ >= 5)
37# pragma GCC target("avx2")
38#endif
39
7c673cae
FG
40#include "sha1_mb.h"
41#include "memcpy_inline.h"
1e59de90 42#include "endian_helper.h"
7c673cae
FG
43
44#ifdef _MSC_VER
45# include <intrin.h>
46# define inline __inline
47#endif
48
49static inline void hash_init_digest(SHA1_WORD_T * digest);
1e59de90 50static inline uint32_t hash_pad(uint8_t padblock[SHA1_BLOCK_SIZE * 2], uint64_t total_len);
7c673cae
FG
51static SHA1_HASH_CTX *sha1_ctx_mgr_resubmit(SHA1_HASH_CTX_MGR * mgr, SHA1_HASH_CTX * ctx);
52
53void sha1_ctx_mgr_init_avx2(SHA1_HASH_CTX_MGR * mgr)
54{
55 sha1_mb_mgr_init_avx2(&mgr->mgr);
56}
57
58SHA1_HASH_CTX *sha1_ctx_mgr_submit_avx2(SHA1_HASH_CTX_MGR * mgr, SHA1_HASH_CTX * ctx,
59 const void *buffer, uint32_t len, HASH_CTX_FLAG flags)
60{
61 if (flags & (~HASH_ENTIRE)) {
62 // User should not pass anything other than FIRST, UPDATE, or LAST
63 ctx->error = HASH_CTX_ERROR_INVALID_FLAGS;
64 return ctx;
65 }
66
67 if (ctx->status & HASH_CTX_STS_PROCESSING) {
68 // Cannot submit to a currently processing job.
69 ctx->error = HASH_CTX_ERROR_ALREADY_PROCESSING;
70 return ctx;
71 }
72
73 if ((ctx->status & HASH_CTX_STS_COMPLETE) && !(flags & HASH_FIRST)) {
74 // Cannot update a finished job.
75 ctx->error = HASH_CTX_ERROR_ALREADY_COMPLETED;
76 return ctx;
77 }
78
79 if (flags & HASH_FIRST) {
80 // Init digest
81 hash_init_digest(ctx->job.result_digest);
82
83 // Reset byte counter
84 ctx->total_length = 0;
85
86 // Clear extra blocks
87 ctx->partial_block_buffer_length = 0;
88 }
89 // If we made it here, there were no errors during this call to submit
90 ctx->error = HASH_CTX_ERROR_NONE;
91
92 // Store buffer ptr info from user
93 ctx->incoming_buffer = buffer;
94 ctx->incoming_buffer_length = len;
95
96 // Store the user's request flags and mark this ctx as currently being processed.
97 ctx->status = (flags & HASH_LAST) ?
98 (HASH_CTX_STS) (HASH_CTX_STS_PROCESSING | HASH_CTX_STS_LAST) :
99 HASH_CTX_STS_PROCESSING;
100
101 // Advance byte counter
102 ctx->total_length += len;
103
104 // If there is anything currently buffered in the extra blocks, append to it until it contains a whole block.
105 // Or if the user's buffer contains less than a whole block, append as much as possible to the extra block.
106 if ((ctx->partial_block_buffer_length) | (len < SHA1_BLOCK_SIZE)) {
107 // Compute how many bytes to copy from user buffer into extra block
108 uint32_t copy_len = SHA1_BLOCK_SIZE - ctx->partial_block_buffer_length;
109 if (len < copy_len)
110 copy_len = len;
111
112 if (copy_len) {
113 // Copy and update relevant pointers and counters
114 memcpy_varlen(&ctx->partial_block_buffer
115 [ctx->partial_block_buffer_length], buffer, copy_len);
116
117 ctx->partial_block_buffer_length += copy_len;
118 ctx->incoming_buffer = (const void *)((const char *)buffer + copy_len);
119 ctx->incoming_buffer_length = len - copy_len;
120 }
121 // The extra block should never contain more than 1 block here
122 assert(ctx->partial_block_buffer_length <= SHA1_BLOCK_SIZE);
123
124 // If the extra block buffer contains exactly 1 block, it can be hashed.
125 if (ctx->partial_block_buffer_length >= SHA1_BLOCK_SIZE) {
126 ctx->partial_block_buffer_length = 0;
127
128 ctx->job.buffer = ctx->partial_block_buffer;
129 ctx->job.len = 1;
130
131 ctx = (SHA1_HASH_CTX *) sha1_mb_mgr_submit_avx2(&mgr->mgr, &ctx->job);
132 }
133 }
134
135 return sha1_ctx_mgr_resubmit(mgr, ctx);
136}
137
138SHA1_HASH_CTX *sha1_ctx_mgr_flush_avx2(SHA1_HASH_CTX_MGR * mgr)
139{
140 SHA1_HASH_CTX *ctx;
141
142 while (1) {
143 ctx = (SHA1_HASH_CTX *) sha1_mb_mgr_flush_avx2(&mgr->mgr);
144
145 // If flush returned 0, there are no more jobs in flight.
146 if (!ctx)
147 return NULL;
148
149 // If flush returned a job, verify that it is safe to return to the user.
150 // If it is not ready, resubmit the job to finish processing.
151 ctx = sha1_ctx_mgr_resubmit(mgr, ctx);
152
153 // If sha1_ctx_mgr_resubmit returned a job, it is ready to be returned.
154 if (ctx)
155 return ctx;
156
157 // Otherwise, all jobs currently being managed by the SHA1_HASH_CTX_MGR still need processing. Loop.
158 }
159}
160
161static SHA1_HASH_CTX *sha1_ctx_mgr_resubmit(SHA1_HASH_CTX_MGR * mgr, SHA1_HASH_CTX * ctx)
162{
163 while (ctx) {
164 if (ctx->status & HASH_CTX_STS_COMPLETE) {
165 ctx->status = HASH_CTX_STS_COMPLETE; // Clear PROCESSING bit
166 return ctx;
167 }
168 // If the extra blocks are empty, begin hashing what remains in the user's buffer.
169 if (ctx->partial_block_buffer_length == 0 && ctx->incoming_buffer_length) {
170 const void *buffer = ctx->incoming_buffer;
171 uint32_t len = ctx->incoming_buffer_length;
172
173 // Only entire blocks can be hashed. Copy remainder to extra blocks buffer.
174 uint32_t copy_len = len & (SHA1_BLOCK_SIZE - 1);
175
176 if (copy_len) {
177 len -= copy_len;
178 memcpy_fixedlen(ctx->partial_block_buffer,
179 ((const char *)buffer + len), copy_len);
180 ctx->partial_block_buffer_length = copy_len;
181 }
182
183 ctx->incoming_buffer_length = 0;
184
185 // len should be a multiple of the block size now
186 assert((len % SHA1_BLOCK_SIZE) == 0);
187
188 // Set len to the number of blocks to be hashed in the user's buffer
189 len >>= SHA1_LOG2_BLOCK_SIZE;
190
191 if (len) {
192 ctx->job.buffer = (uint8_t *) buffer;
193 ctx->job.len = len;
194 ctx = (SHA1_HASH_CTX *) sha1_mb_mgr_submit_avx2(&mgr->mgr,
195 &ctx->job);
196 continue;
197 }
198 }
199 // If the extra blocks are not empty, then we are either on the last block(s)
200 // or we need more user input before continuing.
201 if (ctx->status & HASH_CTX_STS_LAST) {
202 uint8_t *buf = ctx->partial_block_buffer;
203 uint32_t n_extra_blocks = hash_pad(buf, ctx->total_length);
204
205 ctx->status =
206 (HASH_CTX_STS) (HASH_CTX_STS_PROCESSING | HASH_CTX_STS_COMPLETE);
207 ctx->job.buffer = buf;
208 ctx->job.len = (uint32_t) n_extra_blocks;
209 ctx = (SHA1_HASH_CTX *) sha1_mb_mgr_submit_avx2(&mgr->mgr, &ctx->job);
210 continue;
211 }
212
213 if (ctx)
214 ctx->status = HASH_CTX_STS_IDLE;
215 return ctx;
216 }
217
218 return NULL;
219}
220
221static inline void hash_init_digest(SHA1_WORD_T * digest)
222{
223 static const SHA1_WORD_T hash_initial_digest[SHA1_DIGEST_NWORDS] =
224 { SHA1_INITIAL_DIGEST };
225 memcpy_fixedlen(digest, hash_initial_digest, sizeof(hash_initial_digest));
226}
227
1e59de90 228static inline uint32_t hash_pad(uint8_t padblock[SHA1_BLOCK_SIZE * 2], uint64_t total_len)
7c673cae 229{
1e59de90 230 uint32_t i = (uint32_t) (total_len & (SHA1_BLOCK_SIZE - 1));
7c673cae
FG
231
232 memclr_fixedlen(&padblock[i], SHA1_BLOCK_SIZE);
233 padblock[i] = 0x80;
234
235 // Move i to the end of either 1st or 2nd extra block depending on length
236 i += ((SHA1_BLOCK_SIZE - 1) & (0 - (total_len + SHA1_PADLENGTHFIELD_SIZE + 1))) + 1 +
237 SHA1_PADLENGTHFIELD_SIZE;
238
239#if SHA1_PADLENGTHFIELD_SIZE == 16
240 *((uint64_t *) & padblock[i - 16]) = 0;
241#endif
242
1e59de90 243 *((uint64_t *) & padblock[i - 8]) = to_be64((uint64_t) total_len << 3);
7c673cae
FG
244
245 return i >> SHA1_LOG2_BLOCK_SIZE; // Number of extra blocks to hash
246}
247
248struct slver {
249 uint16_t snum;
250 uint8_t ver;
251 uint8_t core;
252};
253struct slver sha1_ctx_mgr_init_avx2_slver_04020145;
254struct slver sha1_ctx_mgr_init_avx2_slver = { 0x0145, 0x02, 0x04 };
255
256struct slver sha1_ctx_mgr_submit_avx2_slver_04020146;
257struct slver sha1_ctx_mgr_submit_avx2_slver = { 0x0146, 0x02, 0x04 };
258
259struct slver sha1_ctx_mgr_flush_avx2_slver_04020147;
260struct slver sha1_ctx_mgr_flush_avx2_slver = { 0x0147, 0x02, 0x04 };
1e59de90
TL
261
262#if defined(__clang__)
263# pragma clang attribute pop
264#endif