]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/sha256_mb/sha256_ctx_sse.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / sha256_mb / sha256_ctx_sse.c
1 /**********************************************************************
2 Copyright(c) 2011-2016 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 #include "sha256_mb.h"
31 #include "memcpy_inline.h"
32
33 #ifdef _MSC_VER
34 # include <intrin.h>
35 # define inline __inline
36 #endif
37
38 static inline void hash_init_digest(SHA256_WORD_T * digest);
39 static inline uint32_t hash_pad(uint8_t padblock[SHA256_BLOCK_SIZE * 2], uint32_t total_len);
40 static SHA256_HASH_CTX *sha256_ctx_mgr_resubmit(SHA256_HASH_CTX_MGR * mgr,
41 SHA256_HASH_CTX * ctx);
42
43 void sha256_ctx_mgr_init_sse(SHA256_HASH_CTX_MGR * mgr)
44 {
45 sha256_mb_mgr_init_sse(&mgr->mgr);
46 }
47
48 SHA256_HASH_CTX *sha256_ctx_mgr_submit_sse(SHA256_HASH_CTX_MGR * mgr, SHA256_HASH_CTX * ctx,
49 const void *buffer, uint32_t len,
50 HASH_CTX_FLAG flags)
51 {
52
53 if (flags & (~HASH_ENTIRE)) {
54 // User should not pass anything other than FIRST, UPDATE, or LAST
55 ctx->error = HASH_CTX_ERROR_INVALID_FLAGS;
56 return ctx;
57 }
58
59 if (ctx->status & HASH_CTX_STS_PROCESSING) {
60 // Cannot submit to a currently processing job.
61 ctx->error = HASH_CTX_ERROR_ALREADY_PROCESSING;
62 return ctx;
63 }
64
65 if ((ctx->status & HASH_CTX_STS_COMPLETE) && !(flags & HASH_FIRST)) {
66 // Cannot update a finished job.
67 ctx->error = HASH_CTX_ERROR_ALREADY_COMPLETED;
68 return ctx;
69 }
70
71 if (flags & HASH_FIRST) {
72 // Init digest
73 hash_init_digest(ctx->job.result_digest);
74
75 // Reset byte counter
76 ctx->total_length = 0;
77
78 // Clear extra blocks
79 ctx->partial_block_buffer_length = 0;
80 }
81 // If we made it here, there were no errors during this call to submit
82 ctx->error = HASH_CTX_ERROR_NONE;
83
84 // Store buffer ptr info from user
85 ctx->incoming_buffer = buffer;
86 ctx->incoming_buffer_length = len;
87
88 // Store the user's request flags and mark this ctx as currently being processed.
89 ctx->status = (flags & HASH_LAST) ?
90 (HASH_CTX_STS) (HASH_CTX_STS_PROCESSING | HASH_CTX_STS_LAST) :
91 HASH_CTX_STS_PROCESSING;
92
93 // Advance byte counter
94 ctx->total_length += len;
95
96 // If there is anything currently buffered in the extra blocks, append to it until it contains a whole block.
97 // Or if the user's buffer contains less than a whole block, append as much as possible to the extra block.
98 if ((ctx->partial_block_buffer_length) | (len < SHA256_BLOCK_SIZE)) {
99 // Compute how many bytes to copy from user buffer into extra block
100 uint32_t copy_len = SHA256_BLOCK_SIZE - ctx->partial_block_buffer_length;
101 if (len < copy_len)
102 copy_len = len;
103
104 if (copy_len) {
105 // Copy and update relevant pointers and counters
106 memcpy_varlen(&ctx->partial_block_buffer
107 [ctx->partial_block_buffer_length], buffer, copy_len);
108
109 ctx->partial_block_buffer_length += copy_len;
110 ctx->incoming_buffer = (const void *)((const char *)buffer + copy_len);
111 ctx->incoming_buffer_length = len - copy_len;
112 }
113 // The extra block should never contain more than 1 block here
114 assert(ctx->partial_block_buffer_length <= SHA256_BLOCK_SIZE);
115
116 // If the extra block buffer contains exactly 1 block, it can be hashed.
117 if (ctx->partial_block_buffer_length >= SHA256_BLOCK_SIZE) {
118 ctx->partial_block_buffer_length = 0;
119
120 ctx->job.buffer = ctx->partial_block_buffer;
121 ctx->job.len = 1;
122 ctx = (SHA256_HASH_CTX *) sha256_mb_mgr_submit_sse(&mgr->mgr,
123 &ctx->job);
124 }
125 }
126
127 return sha256_ctx_mgr_resubmit(mgr, ctx);
128 }
129
130 SHA256_HASH_CTX *sha256_ctx_mgr_flush_sse(SHA256_HASH_CTX_MGR * mgr)
131 {
132 SHA256_HASH_CTX *ctx;
133
134 while (1) {
135 ctx = (SHA256_HASH_CTX *) sha256_mb_mgr_flush_sse(&mgr->mgr);
136
137 // If flush returned 0, there are no more jobs in flight.
138 if (!ctx)
139 return NULL;
140
141 // If flush returned a job, verify that it is safe to return to the user.
142 // If it is not ready, resubmit the job to finish processing.
143 ctx = sha256_ctx_mgr_resubmit(mgr, ctx);
144
145 // If sha256_ctx_mgr_resubmit returned a job, it is ready to be returned.
146 if (ctx)
147 return ctx;
148
149 // Otherwise, all jobs currently being managed by the SHA256_HASH_CTX_MGR still need processing. Loop.
150 }
151 }
152
153 static SHA256_HASH_CTX *sha256_ctx_mgr_resubmit(SHA256_HASH_CTX_MGR * mgr,
154 SHA256_HASH_CTX * ctx)
155 {
156 while (ctx) {
157 if (ctx->status & HASH_CTX_STS_COMPLETE) {
158 ctx->status = HASH_CTX_STS_COMPLETE; // Clear PROCESSING bit
159 return ctx;
160 }
161 // If the extra blocks are empty, begin hashing what remains in the user's buffer.
162 if (ctx->partial_block_buffer_length == 0 && ctx->incoming_buffer_length) {
163 const void *buffer = ctx->incoming_buffer;
164 uint32_t len = ctx->incoming_buffer_length;
165
166 // Only entire blocks can be hashed. Copy remainder to extra blocks buffer.
167 uint32_t copy_len = len & (SHA256_BLOCK_SIZE - 1);
168
169 if (copy_len) {
170 len -= copy_len;
171 memcpy_varlen(ctx->partial_block_buffer,
172 ((const char *)buffer + len), copy_len);
173 ctx->partial_block_buffer_length = copy_len;
174 }
175
176 ctx->incoming_buffer_length = 0;
177
178 // len should be a multiple of the block size now
179 assert((len % SHA256_BLOCK_SIZE) == 0);
180
181 // Set len to the number of blocks to be hashed in the user's buffer
182 len >>= SHA256_LOG2_BLOCK_SIZE;
183
184 if (len) {
185 ctx->job.buffer = (uint8_t *) buffer;
186 ctx->job.len = len;
187 ctx = (SHA256_HASH_CTX *) sha256_mb_mgr_submit_sse(&mgr->mgr,
188 &ctx->job);
189 continue;
190 }
191 }
192 // If the extra blocks are not empty, then we are either on the last block(s)
193 // or we need more user input before continuing.
194 if (ctx->status & HASH_CTX_STS_LAST) {
195 uint8_t *buf = ctx->partial_block_buffer;
196 uint32_t n_extra_blocks = hash_pad(buf, ctx->total_length);
197
198 ctx->status =
199 (HASH_CTX_STS) (HASH_CTX_STS_PROCESSING | HASH_CTX_STS_COMPLETE);
200 ctx->job.buffer = buf;
201 ctx->job.len = (uint32_t) n_extra_blocks;
202
203 ctx = (SHA256_HASH_CTX *) sha256_mb_mgr_submit_sse(&mgr->mgr,
204 &ctx->job);
205 continue;
206 }
207
208 if (ctx)
209 ctx->status = HASH_CTX_STS_IDLE;
210 return ctx;
211 }
212
213 return NULL;
214 }
215
216 static inline void hash_init_digest(SHA256_WORD_T * digest)
217 {
218 static const SHA256_WORD_T hash_initial_digest[SHA256_DIGEST_NWORDS] =
219 { SHA256_INITIAL_DIGEST };
220 memcpy_fixedlen(digest, hash_initial_digest, sizeof(hash_initial_digest));
221 }
222
223 static inline uint32_t hash_pad(uint8_t padblock[SHA256_BLOCK_SIZE * 2], uint32_t total_len)
224 {
225 uint32_t i = total_len & (SHA256_BLOCK_SIZE - 1);
226
227 memclr_fixedlen(&padblock[i], SHA256_BLOCK_SIZE);
228 padblock[i] = 0x80;
229
230 // Move i to the end of either 1st or 2nd extra block depending on length
231 i += ((SHA256_BLOCK_SIZE - 1) & (0 - (total_len + SHA256_PADLENGTHFIELD_SIZE + 1))) +
232 1 + SHA256_PADLENGTHFIELD_SIZE;
233
234 #if SHA256_PADLENGTHFIELD_SIZE == 16
235 *((uint64_t *) & padblock[i - 16]) = 0;
236 #endif
237
238 *((uint64_t *) & padblock[i - 8]) = _byteswap_uint64((uint64_t) total_len << 3);
239
240 return i >> SHA256_LOG2_BLOCK_SIZE; // Number of extra blocks to hash
241 }
242
243 struct slver {
244 uint16_t snum;
245 uint8_t ver;
246 uint8_t core;
247 };
248 struct slver sha256_ctx_mgr_init_sse_slver_00020151;
249 struct slver sha256_ctx_mgr_init_sse_slver = { 0x0151, 0x02, 0x00 };
250
251 struct slver sha256_ctx_mgr_submit_sse_slver_00020152;
252 struct slver sha256_ctx_mgr_submit_sse_slver = { 0x0152, 0x02, 0x00 };
253
254 struct slver sha256_ctx_mgr_flush_sse_slver_00020153;
255 struct slver sha256_ctx_mgr_flush_sse_slver = { 0x0153, 0x02, 0x00 };