]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/sm3_mb/sm3_ctx_avx2.c
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / sm3_mb / sm3_ctx_avx2.c
1 /**********************************************************************
2 Copyright(c) 2011-2020 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 #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
40 #include "sm3_mb.h"
41 #include "memcpy_inline.h"
42 #include "endian_helper.h"
43
44 #ifdef _MSC_VER
45 # include <intrin.h>
46 # define inline __inline
47 #endif
48
49 static inline void hash_init_digest(SM3_WORD_T * digest);
50 static inline uint32_t hash_pad(uint8_t padblock[SM3_BLOCK_SIZE * 2], uint64_t total_len);
51 static SM3_HASH_CTX *sm3_ctx_mgr_resubmit(SM3_HASH_CTX_MGR * mgr, SM3_HASH_CTX * ctx);
52
53 void sm3_mb_mgr_init_avx2(SM3_MB_JOB_MGR * state);
54 SM3_JOB *sm3_mb_mgr_submit_avx2(SM3_MB_JOB_MGR * state, SM3_JOB * job);
55 SM3_JOB *sm3_mb_mgr_flush_avx2(SM3_MB_JOB_MGR * state);
56
57 void sm3_mb_mgr_init_avx2(SM3_MB_JOB_MGR * state)
58 {
59 unsigned int j;
60 state->unused_lanes = 0xF76543210;
61 state->num_lanes_inuse = 0;
62 for (j = 0; j < SM3_X8_LANES; j++) {
63 state->lens[j] = 0;
64 state->ldata[j].job_in_lane = 0;
65 }
66 }
67
68 void sm3_ctx_mgr_init_avx2(SM3_HASH_CTX_MGR * mgr)
69 {
70 sm3_mb_mgr_init_avx2(&mgr->mgr);
71 }
72
73 SM3_HASH_CTX *sm3_ctx_mgr_submit_avx2(SM3_HASH_CTX_MGR * mgr, SM3_HASH_CTX * ctx,
74 const void *buffer, uint32_t len, HASH_CTX_FLAG flags)
75 {
76 if (flags & (~HASH_ENTIRE)) {
77 // User should not pass anything other than FIRST, UPDATE, or LAST
78 ctx->error = HASH_CTX_ERROR_INVALID_FLAGS;
79 return ctx;
80 }
81
82 if (ctx->status & HASH_CTX_STS_PROCESSING) {
83 // Cannot submit to a currently processing job.
84 ctx->error = HASH_CTX_ERROR_ALREADY_PROCESSING;
85 return ctx;
86 }
87
88 if ((ctx->status & HASH_CTX_STS_COMPLETE) && !(flags & HASH_FIRST)) {
89 // Cannot update a finished job.
90 ctx->error = HASH_CTX_ERROR_ALREADY_COMPLETED;
91 return ctx;
92 }
93
94 if (flags & HASH_FIRST) {
95 // Init digest
96 hash_init_digest(ctx->job.result_digest);
97
98 // Reset byte counter
99 ctx->total_length = 0;
100
101 // Clear extra blocks
102 ctx->partial_block_buffer_length = 0;
103 }
104 // If we made it here, there were no errors during this call to submit
105 ctx->error = HASH_CTX_ERROR_NONE;
106
107 // Store buffer ptr info from user
108 ctx->incoming_buffer = buffer;
109 ctx->incoming_buffer_length = len;
110
111 // Store the user's request flags and mark this ctx as currently being processed.
112 ctx->status = (flags & HASH_LAST) ?
113 (HASH_CTX_STS) (HASH_CTX_STS_PROCESSING | HASH_CTX_STS_LAST) :
114 HASH_CTX_STS_PROCESSING;
115
116 // Advance byte counter
117 ctx->total_length += len;
118
119 // If there is anything currently buffered in the extra blocks, append to it until it contains a whole block.
120 // Or if the user's buffer contains less than a whole block, append as much as possible to the extra block.
121 if ((ctx->partial_block_buffer_length) | (len < SM3_BLOCK_SIZE)) {
122 // Compute how many bytes to copy from user buffer into extra block
123 uint32_t copy_len = SM3_BLOCK_SIZE - ctx->partial_block_buffer_length;
124 if (len < copy_len)
125 copy_len = len;
126
127 if (copy_len) {
128 // Copy and update relevant pointers and counters
129 memcpy_varlen(&ctx->partial_block_buffer
130 [ctx->partial_block_buffer_length], buffer, copy_len);
131
132 ctx->partial_block_buffer_length += copy_len;
133 ctx->incoming_buffer = (const void *)((const char *)buffer + copy_len);
134 ctx->incoming_buffer_length = len - copy_len;
135 }
136 // The extra block should never contain more than 1 block here
137 assert(ctx->partial_block_buffer_length <= SM3_BLOCK_SIZE);
138
139 // If the extra block buffer contains exactly 1 block, it can be hashed.
140 if (ctx->partial_block_buffer_length >= SM3_BLOCK_SIZE) {
141 ctx->partial_block_buffer_length = 0;
142
143 ctx->job.buffer = ctx->partial_block_buffer;
144 ctx->job.len = 1;
145 ctx = (SM3_HASH_CTX *) sm3_mb_mgr_submit_avx2(&mgr->mgr, &ctx->job);
146 }
147 }
148
149 return sm3_ctx_mgr_resubmit(mgr, ctx);
150 }
151
152 SM3_HASH_CTX *sm3_ctx_mgr_flush_avx2(SM3_HASH_CTX_MGR * mgr)
153 {
154 SM3_HASH_CTX *ctx;
155
156 while (1) {
157 ctx = (SM3_HASH_CTX *) sm3_mb_mgr_flush_avx2(&mgr->mgr);
158
159 // If flush returned 0, there are no more jobs in flight.
160 if (!ctx)
161 return NULL;
162
163 // If flush returned a job, verify that it is safe to return to the user.
164 // If it is not ready, resubmit the job to finish processing.
165 ctx = sm3_ctx_mgr_resubmit(mgr, ctx);
166
167 // If sm3_ctx_mgr_resubmit returned a job, it is ready to be returned.
168 if (ctx)
169 return ctx;
170
171 // Otherwise, all jobs currently being managed by the SM3_HASH_CTX_MGR still need processing. Loop.
172 }
173 }
174
175 static SM3_HASH_CTX *sm3_ctx_mgr_resubmit(SM3_HASH_CTX_MGR * mgr, SM3_HASH_CTX * ctx)
176 {
177 while (ctx) {
178 if (ctx->status & HASH_CTX_STS_COMPLETE) {
179 unsigned int j;
180 ctx->status = HASH_CTX_STS_COMPLETE; // Clear PROCESSING bit
181 for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
182 ctx->job.result_digest[j] =
183 byteswap32(ctx->job.result_digest[j]);
184 }
185 return ctx;
186 }
187 // If the extra blocks are empty, begin hashing what remains in the user's buffer.
188 if (ctx->partial_block_buffer_length == 0 && ctx->incoming_buffer_length) {
189 const void *buffer = ctx->incoming_buffer;
190 uint32_t len = ctx->incoming_buffer_length;
191
192 // Only entire blocks can be hashed. Copy remainder to extra blocks buffer.
193 uint32_t copy_len = len & (SM3_BLOCK_SIZE - 1);
194
195 if (copy_len) {
196 len -= copy_len;
197 memcpy_varlen(ctx->partial_block_buffer,
198 ((const char *)buffer + len), copy_len);
199 ctx->partial_block_buffer_length = copy_len;
200 }
201
202 ctx->incoming_buffer_length = 0;
203
204 // len should be a multiple of the block size now
205 assert((len % SM3_BLOCK_SIZE) == 0);
206
207 // Set len to the number of blocks to be hashed in the user's buffer
208 len >>= SM3_LOG2_BLOCK_SIZE;
209
210 if (len) {
211 ctx->job.buffer = (uint8_t *) buffer;
212 ctx->job.len = len;
213 ctx = (SM3_HASH_CTX *) sm3_mb_mgr_submit_avx2(&mgr->mgr,
214 &ctx->job);
215 continue;
216 }
217 }
218 // If the extra blocks are not empty, then we are either on the last block(s)
219 // or we need more user input before continuing.
220 if (ctx->status & HASH_CTX_STS_LAST) {
221 uint8_t *buf = ctx->partial_block_buffer;
222 uint32_t n_extra_blocks = hash_pad(buf, ctx->total_length);
223
224 ctx->status =
225 (HASH_CTX_STS) (HASH_CTX_STS_PROCESSING | HASH_CTX_STS_COMPLETE);
226 ctx->job.buffer = buf;
227 ctx->job.len = (uint32_t) n_extra_blocks;
228 ctx = (SM3_HASH_CTX *) sm3_mb_mgr_submit_avx2(&mgr->mgr, &ctx->job);
229 continue;
230 }
231
232 if (ctx)
233 ctx->status = HASH_CTX_STS_IDLE;
234 return ctx;
235 }
236
237 return NULL;
238 }
239
240 static inline void hash_init_digest(SM3_WORD_T * digest)
241 {
242 static const SM3_WORD_T hash_initial_digest[SM3_DIGEST_NWORDS] =
243 { SM3_INITIAL_DIGEST };
244 memcpy_fixedlen(digest, hash_initial_digest, sizeof(hash_initial_digest));
245 }
246
247 static inline uint32_t hash_pad(uint8_t padblock[SM3_BLOCK_SIZE * 2], uint64_t total_len)
248 {
249 uint32_t i = (uint32_t) (total_len & (SM3_BLOCK_SIZE - 1));
250
251 memclr_fixedlen(&padblock[i], SM3_BLOCK_SIZE);
252 padblock[i] = 0x80;
253
254 // Move i to the end of either 1st or 2nd extra block depending on length
255 i += ((SM3_BLOCK_SIZE - 1) & (0 - (total_len + SM3_PADLENGTHFIELD_SIZE + 1))) +
256 1 + SM3_PADLENGTHFIELD_SIZE;
257
258 #if SM3_PADLENGTHFIELD_SIZE == 16
259 *((uint64_t *) & padblock[i - 16]) = 0;
260 #endif
261
262 *((uint64_t *) & padblock[i - 8]) = to_be64((uint64_t) total_len << 3);
263
264 return i >> SM3_LOG2_BLOCK_SIZE; // Number of extra blocks to hash
265 }
266
267 struct slver {
268 uint16_t snum;
269 uint8_t ver;
270 uint8_t core;
271 };
272
273 struct slver sm3_ctx_mgr_init_avx2_slver_0000;
274 struct slver sm3_ctx_mgr_init_avx2_slver = { 0x2309, 0x00, 0x00 };
275
276 struct slver sm3_ctx_mgr_submit_avx2_slver_0000;
277 struct slver sm3_ctx_mgr_submit_avx2_slver = { 0x230a, 0x00, 0x00 };
278
279 struct slver sm3_ctx_mgr_flush_avx2_slver_0000;
280 struct slver sm3_ctx_mgr_flush_avx2_slver = { 0x230b, 0x00, 0x00 };
281
282 #if defined(__clang__)
283 # pragma clang attribute pop
284 #endif