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