1 /**********************************************************************
2 Copyright(c) 2011-2020 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
30 #if defined(__clang__)
31 # pragma clang attribute push (__attribute__((target("avx2"))), apply_to=function)
33 # pragma intel optimization_parameter target_arch=AVX2
35 # pragma [intel] optimization_parameter target_arch=AVX2
37 # pragma GCC target("avx2")
41 #include "memcpy_inline.h"
42 #include "endian_helper.h"
46 # define inline __inline
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
);
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
);
57 void sm3_mb_mgr_init_avx2(SM3_MB_JOB_MGR
* state
)
60 state
->unused_lanes
= 0xF76543210;
61 state
->num_lanes_inuse
= 0;
62 for (j
= 0; j
< SM3_X8_LANES
; j
++) {
64 state
->ldata
[j
].job_in_lane
= 0;
68 void sm3_ctx_mgr_init_avx2(SM3_HASH_CTX_MGR
* mgr
)
70 sm3_mb_mgr_init_avx2(&mgr
->mgr
);
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
)
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
;
82 if (ctx
->status
& HASH_CTX_STS_PROCESSING
) {
83 // Cannot submit to a currently processing job.
84 ctx
->error
= HASH_CTX_ERROR_ALREADY_PROCESSING
;
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
;
94 if (flags
& HASH_FIRST
) {
96 hash_init_digest(ctx
->job
.result_digest
);
99 ctx
->total_length
= 0;
101 // Clear extra blocks
102 ctx
->partial_block_buffer_length
= 0;
104 // If we made it here, there were no errors during this call to submit
105 ctx
->error
= HASH_CTX_ERROR_NONE
;
107 // Store buffer ptr info from user
108 ctx
->incoming_buffer
= buffer
;
109 ctx
->incoming_buffer_length
= len
;
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
;
116 // Advance byte counter
117 ctx
->total_length
+= len
;
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
;
128 // Copy and update relevant pointers and counters
129 memcpy_varlen(&ctx
->partial_block_buffer
130 [ctx
->partial_block_buffer_length
], buffer
, copy_len
);
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
;
136 // The extra block should never contain more than 1 block here
137 assert(ctx
->partial_block_buffer_length
<= SM3_BLOCK_SIZE
);
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;
143 ctx
->job
.buffer
= ctx
->partial_block_buffer
;
145 ctx
= (SM3_HASH_CTX
*) sm3_mb_mgr_submit_avx2(&mgr
->mgr
, &ctx
->job
);
149 return sm3_ctx_mgr_resubmit(mgr
, ctx
);
152 SM3_HASH_CTX
*sm3_ctx_mgr_flush_avx2(SM3_HASH_CTX_MGR
* mgr
)
157 ctx
= (SM3_HASH_CTX
*) sm3_mb_mgr_flush_avx2(&mgr
->mgr
);
159 // If flush returned 0, there are no more jobs in flight.
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
);
167 // If sm3_ctx_mgr_resubmit returned a job, it is ready to be returned.
171 // Otherwise, all jobs currently being managed by the SM3_HASH_CTX_MGR still need processing. Loop.
175 static SM3_HASH_CTX
*sm3_ctx_mgr_resubmit(SM3_HASH_CTX_MGR
* mgr
, SM3_HASH_CTX
* ctx
)
178 if (ctx
->status
& HASH_CTX_STS_COMPLETE
) {
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
]);
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
;
192 // Only entire blocks can be hashed. Copy remainder to extra blocks buffer.
193 uint32_t copy_len
= len
& (SM3_BLOCK_SIZE
- 1);
197 memcpy_varlen(ctx
->partial_block_buffer
,
198 ((const char *)buffer
+ len
), copy_len
);
199 ctx
->partial_block_buffer_length
= copy_len
;
202 ctx
->incoming_buffer_length
= 0;
204 // len should be a multiple of the block size now
205 assert((len
% SM3_BLOCK_SIZE
) == 0);
207 // Set len to the number of blocks to be hashed in the user's buffer
208 len
>>= SM3_LOG2_BLOCK_SIZE
;
211 ctx
->job
.buffer
= (uint8_t *) buffer
;
213 ctx
= (SM3_HASH_CTX
*) sm3_mb_mgr_submit_avx2(&mgr
->mgr
,
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
);
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
);
233 ctx
->status
= HASH_CTX_STS_IDLE
;
240 static inline void hash_init_digest(SM3_WORD_T
* digest
)
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
));
247 static inline uint32_t hash_pad(uint8_t padblock
[SM3_BLOCK_SIZE
* 2], uint64_t total_len
)
249 uint32_t i
= (uint32_t) (total_len
& (SM3_BLOCK_SIZE
- 1));
251 memclr_fixedlen(&padblock
[i
], SM3_BLOCK_SIZE
);
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
;
258 #if SM3_PADLENGTHFIELD_SIZE == 16
259 *((uint64_t *) & padblock
[i
- 16]) = 0;
262 *((uint64_t *) & padblock
[i
- 8]) = to_be64((uint64_t) total_len
<< 3);
264 return i
>> SM3_LOG2_BLOCK_SIZE
; // Number of extra blocks to hash
273 struct slver sm3_ctx_mgr_init_avx2_slver_0000
;
274 struct slver sm3_ctx_mgr_init_avx2_slver
= { 0x2309, 0x00, 0x00 };
276 struct slver sm3_ctx_mgr_submit_avx2_slver_0000
;
277 struct slver sm3_ctx_mgr_submit_avx2_slver
= { 0x230a, 0x00, 0x00 };
279 struct slver sm3_ctx_mgr_flush_avx2_slver_0000
;
280 struct slver sm3_ctx_mgr_flush_avx2_slver
= { 0x230b, 0x00, 0x00 };
282 #if defined(__clang__)
283 # pragma clang attribute pop