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