]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/sha512_mb/sha512_ctx_sse.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / sha512_mb / sha512_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 "sha512_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(SHA512_WORD_T * digest);
39 static inline uint32_t hash_pad(uint8_t padblock[SHA512_BLOCK_SIZE * 2], uint32_t total_len);
40 static SHA512_HASH_CTX *sha512_ctx_mgr_resubmit(SHA512_HASH_CTX_MGR * mgr,
41 SHA512_HASH_CTX * ctx);
42
43 void sha512_ctx_mgr_init_sse(SHA512_HASH_CTX_MGR * mgr)
44 {
45 sha512_mb_mgr_init_sse(&mgr->mgr);
46 }
47
48 SHA512_HASH_CTX *sha512_ctx_mgr_submit_sse(SHA512_HASH_CTX_MGR * mgr, SHA512_HASH_CTX * ctx,
49 const void *buffer, uint32_t len,
50 HASH_CTX_FLAG flags)
51 {
52 if (flags & (~HASH_ENTIRE)) {
53 // User should not pass anything other than FIRST, UPDATE, or LAST
54 ctx->error = HASH_CTX_ERROR_INVALID_FLAGS;
55 return ctx;
56 }
57
58 if (ctx->status & HASH_CTX_STS_PROCESSING) {
59 // Cannot submit to a currently processing job.
60 ctx->error = HASH_CTX_ERROR_ALREADY_PROCESSING;
61 return ctx;
62 }
63
64 if ((ctx->status & HASH_CTX_STS_COMPLETE) && !(flags & HASH_FIRST)) {
65 // Cannot update a finished job.
66 ctx->error = HASH_CTX_ERROR_ALREADY_COMPLETED;
67 return ctx;
68 }
69
70 if (flags & HASH_FIRST) {
71 // Init digest
72 hash_init_digest(ctx->job.result_digest);
73
74 // Reset byte counter
75 ctx->total_length = 0;
76
77 // Clear extra blocks
78 ctx->partial_block_buffer_length = 0;
79 }
80 // If we made it here, there were no errors during this call to submit
81 ctx->error = HASH_CTX_ERROR_NONE;
82
83 // Store buffer ptr info from user
84 ctx->incoming_buffer = buffer;
85 ctx->incoming_buffer_length = len;
86
87 // Store the user's request flags and mark this ctx as currently being processed.
88 ctx->status = (flags & HASH_LAST) ?
89 (HASH_CTX_STS) (HASH_CTX_STS_PROCESSING | HASH_CTX_STS_LAST) :
90 HASH_CTX_STS_PROCESSING;
91
92 // Advance byte counter
93 ctx->total_length += len;
94
95 // If there is anything currently buffered in the extra blocks, append to it until it contains a whole block.
96 // Or if the user's buffer contains less than a whole block, append as much as possible to the extra block.
97 if ((ctx->partial_block_buffer_length) | (len < SHA512_BLOCK_SIZE)) {
98 // Compute how many bytes to copy from user buffer into extra block
99 uint32_t copy_len = SHA512_BLOCK_SIZE - ctx->partial_block_buffer_length;
100 if (len < copy_len)
101 copy_len = len;
102
103 if (copy_len) {
104 // Copy and update relevant pointers and counters
105 memcpy_varlen(&ctx->partial_block_buffer
106 [ctx->partial_block_buffer_length], buffer, copy_len);
107
108 ctx->partial_block_buffer_length += copy_len;
109 ctx->incoming_buffer = (const void *)((const char *)buffer + copy_len);
110 ctx->incoming_buffer_length = len - copy_len;
111 }
112 // The extra block should never contain more than 1 block here
113 assert(ctx->partial_block_buffer_length <= SHA512_BLOCK_SIZE);
114
115 // If the extra block buffer contains exactly 1 block, it can be hashed.
116 if (ctx->partial_block_buffer_length >= SHA512_BLOCK_SIZE) {
117 ctx->partial_block_buffer_length = 0;
118
119 ctx->job.buffer = ctx->partial_block_buffer;
120 ctx->job.len = 1;
121
122 ctx =
123 (SHA512_HASH_CTX *) sha512_mb_mgr_submit_sse(&mgr->mgr, &ctx->job);
124 }
125 }
126
127 return sha512_ctx_mgr_resubmit(mgr, ctx);
128 }
129
130 SHA512_HASH_CTX *sha512_ctx_mgr_flush_sse(SHA512_HASH_CTX_MGR * mgr)
131 {
132 SHA512_HASH_CTX *ctx;
133
134 while (1) {
135 ctx = (SHA512_HASH_CTX *) sha512_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 = sha512_ctx_mgr_resubmit(mgr, ctx);
144
145 // If sha512_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 SHA512_HASH_CTX_MGR still need processing. Loop.
150 }
151 }
152
153 static SHA512_HASH_CTX *sha512_ctx_mgr_resubmit(SHA512_HASH_CTX_MGR * mgr,
154 SHA512_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 & (SHA512_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 % SHA512_BLOCK_SIZE) == 0);
180
181 // Set len to the number of blocks to be hashed in the user's buffer
182 len >>= SHA512_LOG2_BLOCK_SIZE;
183
184 if (len) {
185 ctx->job.buffer = (uint8_t *) buffer;
186 ctx->job.len = len;
187 ctx = (SHA512_HASH_CTX *) sha512_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 ctx = (SHA512_HASH_CTX *) sha512_mb_mgr_submit_sse(&mgr->mgr,
203 &ctx->job);
204 continue;
205 }
206
207 if (ctx)
208 ctx->status = HASH_CTX_STS_IDLE;
209 return ctx;
210 }
211
212 return NULL;
213 }
214
215 static inline void hash_init_digest(SHA512_WORD_T * digest)
216 {
217 static const SHA512_WORD_T hash_initial_digest[SHA512_DIGEST_NWORDS] =
218 { SHA512_INITIAL_DIGEST };
219 memcpy_fixedlen(digest, hash_initial_digest, sizeof(hash_initial_digest));
220 }
221
222 static inline uint32_t hash_pad(uint8_t padblock[SHA512_BLOCK_SIZE * 2], uint32_t total_len)
223 {
224 uint32_t i = total_len & (SHA512_BLOCK_SIZE - 1);
225
226 memclr_fixedlen(&padblock[i], SHA512_BLOCK_SIZE);
227 padblock[i] = 0x80;
228
229 // Move i to the end of either 1st or 2nd extra block depending on length
230 i += ((SHA512_BLOCK_SIZE - 1) & (0 - (total_len + SHA512_PADLENGTHFIELD_SIZE + 1))) +
231 1 + SHA512_PADLENGTHFIELD_SIZE;
232
233 #if SHA512_PADLENGTHFIELD_SIZE == 16
234 *((uint64_t *) & padblock[i - 16]) = 0;
235 #endif
236
237 *((uint64_t *) & padblock[i - 8]) = _byteswap_uint64((uint64_t) total_len << 3);
238
239 return i >> SHA512_LOG2_BLOCK_SIZE; // Number of extra blocks to hash
240 }
241
242 struct slver {
243 uint16_t snum;
244 uint8_t ver;
245 uint8_t core;
246 };
247 struct slver sha512_ctx_mgr_init_sse_slver_00020163;
248 struct slver sha512_ctx_mgr_init_sse_slver = { 0x0163, 0x02, 0x00 };
249
250 struct slver sha512_ctx_mgr_submit_sse_slver_00020164;
251 struct slver sha512_ctx_mgr_submit_sse_slver = { 0x0164, 0x02, 0x00 };
252
253 struct slver sha512_ctx_mgr_flush_sse_slver_00020165;
254 struct slver sha512_ctx_mgr_flush_sse_slver = { 0x0165, 0x02, 0x00 };