]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/sha512_mb/sha512_ctx_sse.c
bump version to 18.2.2-pve1
[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 #include "endian_helper.h"
33
34 #ifdef _MSC_VER
35 # include <intrin.h>
36 # define inline __inline
37 #endif
38
39 static inline void hash_init_digest(SHA512_WORD_T * digest);
40 static inline uint32_t hash_pad(uint8_t padblock[SHA512_BLOCK_SIZE * 2], uint64_t total_len);
41 static SHA512_HASH_CTX *sha512_ctx_mgr_resubmit(SHA512_HASH_CTX_MGR * mgr,
42 SHA512_HASH_CTX * ctx);
43
44 void sha512_ctx_mgr_init_sse(SHA512_HASH_CTX_MGR * mgr)
45 {
46 sha512_mb_mgr_init_sse(&mgr->mgr);
47 }
48
49 SHA512_HASH_CTX *sha512_ctx_mgr_submit_sse(SHA512_HASH_CTX_MGR * mgr, SHA512_HASH_CTX * ctx,
50 const void *buffer, uint32_t len,
51 HASH_CTX_FLAG flags)
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 < SHA512_BLOCK_SIZE)) {
99 // Compute how many bytes to copy from user buffer into extra block
100 uint32_t copy_len = SHA512_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 <= SHA512_BLOCK_SIZE);
115
116 // If the extra block buffer contains exactly 1 block, it can be hashed.
117 if (ctx->partial_block_buffer_length >= SHA512_BLOCK_SIZE) {
118 ctx->partial_block_buffer_length = 0;
119
120 ctx->job.buffer = ctx->partial_block_buffer;
121 ctx->job.len = 1;
122
123 ctx =
124 (SHA512_HASH_CTX *) sha512_mb_mgr_submit_sse(&mgr->mgr, &ctx->job);
125 }
126 }
127
128 return sha512_ctx_mgr_resubmit(mgr, ctx);
129 }
130
131 SHA512_HASH_CTX *sha512_ctx_mgr_flush_sse(SHA512_HASH_CTX_MGR * mgr)
132 {
133 SHA512_HASH_CTX *ctx;
134
135 while (1) {
136 ctx = (SHA512_HASH_CTX *) sha512_mb_mgr_flush_sse(&mgr->mgr);
137
138 // If flush returned 0, there are no more jobs in flight.
139 if (!ctx)
140 return NULL;
141
142 // If flush returned a job, verify that it is safe to return to the user.
143 // If it is not ready, resubmit the job to finish processing.
144 ctx = sha512_ctx_mgr_resubmit(mgr, ctx);
145
146 // If sha512_ctx_mgr_resubmit returned a job, it is ready to be returned.
147 if (ctx)
148 return ctx;
149
150 // Otherwise, all jobs currently being managed by the SHA512_HASH_CTX_MGR still need processing. Loop.
151 }
152 }
153
154 static SHA512_HASH_CTX *sha512_ctx_mgr_resubmit(SHA512_HASH_CTX_MGR * mgr,
155 SHA512_HASH_CTX * ctx)
156 {
157 while (ctx) {
158 if (ctx->status & HASH_CTX_STS_COMPLETE) {
159 ctx->status = HASH_CTX_STS_COMPLETE; // Clear PROCESSING bit
160 return ctx;
161 }
162 // If the extra blocks are empty, begin hashing what remains in the user's buffer.
163 if (ctx->partial_block_buffer_length == 0 && ctx->incoming_buffer_length) {
164 const void *buffer = ctx->incoming_buffer;
165 uint32_t len = ctx->incoming_buffer_length;
166
167 // Only entire blocks can be hashed. Copy remainder to extra blocks buffer.
168 uint32_t copy_len = len & (SHA512_BLOCK_SIZE - 1);
169
170 if (copy_len) {
171 len -= copy_len;
172 memcpy_varlen(ctx->partial_block_buffer,
173 ((const char *)buffer + len), copy_len);
174 ctx->partial_block_buffer_length = copy_len;
175 }
176
177 ctx->incoming_buffer_length = 0;
178
179 // len should be a multiple of the block size now
180 assert((len % SHA512_BLOCK_SIZE) == 0);
181
182 // Set len to the number of blocks to be hashed in the user's buffer
183 len >>= SHA512_LOG2_BLOCK_SIZE;
184
185 if (len) {
186 ctx->job.buffer = (uint8_t *) buffer;
187 ctx->job.len = len;
188 ctx = (SHA512_HASH_CTX *) sha512_mb_mgr_submit_sse(&mgr->mgr,
189 &ctx->job);
190 continue;
191 }
192 }
193 // If the extra blocks are not empty, then we are either on the last block(s)
194 // or we need more user input before continuing.
195 if (ctx->status & HASH_CTX_STS_LAST) {
196 uint8_t *buf = ctx->partial_block_buffer;
197 uint32_t n_extra_blocks = hash_pad(buf, ctx->total_length);
198
199 ctx->status =
200 (HASH_CTX_STS) (HASH_CTX_STS_PROCESSING | HASH_CTX_STS_COMPLETE);
201 ctx->job.buffer = buf;
202 ctx->job.len = (uint32_t) n_extra_blocks;
203 ctx = (SHA512_HASH_CTX *) sha512_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(SHA512_WORD_T * digest)
217 {
218 static const SHA512_WORD_T hash_initial_digest[SHA512_DIGEST_NWORDS] =
219 { SHA512_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[SHA512_BLOCK_SIZE * 2], uint64_t total_len)
224 {
225 uint32_t i = (uint32_t) (total_len & (SHA512_BLOCK_SIZE - 1));
226
227 memclr_fixedlen(&padblock[i], SHA512_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 += ((SHA512_BLOCK_SIZE - 1) & (0 - (total_len + SHA512_PADLENGTHFIELD_SIZE + 1))) +
232 1 + SHA512_PADLENGTHFIELD_SIZE;
233
234 #if SHA512_PADLENGTHFIELD_SIZE == 16
235 *((uint64_t *) & padblock[i - 16]) = 0;
236 #endif
237
238 *((uint64_t *) & padblock[i - 8]) = to_be64((uint64_t) total_len << 3);
239
240 return i >> SHA512_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 sha512_ctx_mgr_init_sse_slver_00020163;
249 struct slver sha512_ctx_mgr_init_sse_slver = { 0x0163, 0x02, 0x00 };
250
251 struct slver sha512_ctx_mgr_submit_sse_slver_00020164;
252 struct slver sha512_ctx_mgr_submit_sse_slver = { 0x0164, 0x02, 0x00 };
253
254 struct slver sha512_ctx_mgr_flush_sse_slver_00020165;
255 struct slver sha512_ctx_mgr_flush_sse_slver = { 0x0165, 0x02, 0x00 };