1 /**********************************************************************
2 Copyright(c) 2011-2016 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 #include "sha512_mb.h"
31 #include "memcpy_inline.h"
35 # define inline __inline
38 static inline void hash_init_digest(SHA512_WORD_T
* digest
);
39 static inline uint32_t hash_pad(uint8_t padblock
[SHA512_BLOCK_SIZE
* 2], uint32_t total_len
);
40 static SHA512_HASH_CTX
*sha512_ctx_mgr_resubmit(SHA512_HASH_CTX_MGR
* mgr
,
41 SHA512_HASH_CTX
* ctx
);
43 void sha512_ctx_mgr_init_sse(SHA512_HASH_CTX_MGR
* mgr
)
45 sha512_mb_mgr_init_sse(&mgr
->mgr
);
48 SHA512_HASH_CTX
*sha512_ctx_mgr_submit_sse(SHA512_HASH_CTX_MGR
* mgr
, SHA512_HASH_CTX
* ctx
,
49 const void *buffer
, uint32_t len
,
52 if (flags
& (~HASH_ENTIRE
)) {
53 // User should not pass anything other than FIRST, UPDATE, or LAST
54 ctx
->error
= HASH_CTX_ERROR_INVALID_FLAGS
;
58 if (ctx
->status
& HASH_CTX_STS_PROCESSING
) {
59 // Cannot submit to a currently processing job.
60 ctx
->error
= HASH_CTX_ERROR_ALREADY_PROCESSING
;
64 if ((ctx
->status
& HASH_CTX_STS_COMPLETE
) && !(flags
& HASH_FIRST
)) {
65 // Cannot update a finished job.
66 ctx
->error
= HASH_CTX_ERROR_ALREADY_COMPLETED
;
70 if (flags
& HASH_FIRST
) {
72 hash_init_digest(ctx
->job
.result_digest
);
75 ctx
->total_length
= 0;
78 ctx
->partial_block_buffer_length
= 0;
80 // If we made it here, there were no errors during this call to submit
81 ctx
->error
= HASH_CTX_ERROR_NONE
;
83 // Store buffer ptr info from user
84 ctx
->incoming_buffer
= buffer
;
85 ctx
->incoming_buffer_length
= len
;
87 // Store the user's request flags and mark this ctx as currently being processed.
88 ctx
->status
= (flags
& HASH_LAST
) ?
89 (HASH_CTX_STS
) (HASH_CTX_STS_PROCESSING
| HASH_CTX_STS_LAST
) :
90 HASH_CTX_STS_PROCESSING
;
92 // Advance byte counter
93 ctx
->total_length
+= len
;
95 // If there is anything currently buffered in the extra blocks, append to it until it contains a whole block.
96 // Or if the user's buffer contains less than a whole block, append as much as possible to the extra block.
97 if ((ctx
->partial_block_buffer_length
) | (len
< SHA512_BLOCK_SIZE
)) {
98 // Compute how many bytes to copy from user buffer into extra block
99 uint32_t copy_len
= SHA512_BLOCK_SIZE
- ctx
->partial_block_buffer_length
;
104 // Copy and update relevant pointers and counters
105 memcpy_varlen(&ctx
->partial_block_buffer
106 [ctx
->partial_block_buffer_length
], buffer
, copy_len
);
108 ctx
->partial_block_buffer_length
+= copy_len
;
109 ctx
->incoming_buffer
= (const void *)((const char *)buffer
+ copy_len
);
110 ctx
->incoming_buffer_length
= len
- copy_len
;
112 // The extra block should never contain more than 1 block here
113 assert(ctx
->partial_block_buffer_length
<= SHA512_BLOCK_SIZE
);
115 // If the extra block buffer contains exactly 1 block, it can be hashed.
116 if (ctx
->partial_block_buffer_length
>= SHA512_BLOCK_SIZE
) {
117 ctx
->partial_block_buffer_length
= 0;
119 ctx
->job
.buffer
= ctx
->partial_block_buffer
;
123 (SHA512_HASH_CTX
*) sha512_mb_mgr_submit_sse(&mgr
->mgr
, &ctx
->job
);
127 return sha512_ctx_mgr_resubmit(mgr
, ctx
);
130 SHA512_HASH_CTX
*sha512_ctx_mgr_flush_sse(SHA512_HASH_CTX_MGR
* mgr
)
132 SHA512_HASH_CTX
*ctx
;
135 ctx
= (SHA512_HASH_CTX
*) sha512_mb_mgr_flush_sse(&mgr
->mgr
);
137 // If flush returned 0, there are no more jobs in flight.
141 // If flush returned a job, verify that it is safe to return to the user.
142 // If it is not ready, resubmit the job to finish processing.
143 ctx
= sha512_ctx_mgr_resubmit(mgr
, ctx
);
145 // If sha512_ctx_mgr_resubmit returned a job, it is ready to be returned.
149 // Otherwise, all jobs currently being managed by the SHA512_HASH_CTX_MGR still need processing. Loop.
153 static SHA512_HASH_CTX
*sha512_ctx_mgr_resubmit(SHA512_HASH_CTX_MGR
* mgr
,
154 SHA512_HASH_CTX
* ctx
)
157 if (ctx
->status
& HASH_CTX_STS_COMPLETE
) {
158 ctx
->status
= HASH_CTX_STS_COMPLETE
; // Clear PROCESSING bit
161 // If the extra blocks are empty, begin hashing what remains in the user's buffer.
162 if (ctx
->partial_block_buffer_length
== 0 && ctx
->incoming_buffer_length
) {
163 const void *buffer
= ctx
->incoming_buffer
;
164 uint32_t len
= ctx
->incoming_buffer_length
;
166 // Only entire blocks can be hashed. Copy remainder to extra blocks buffer.
167 uint32_t copy_len
= len
& (SHA512_BLOCK_SIZE
- 1);
171 memcpy_varlen(ctx
->partial_block_buffer
,
172 ((const char *)buffer
+ len
), copy_len
);
173 ctx
->partial_block_buffer_length
= copy_len
;
176 ctx
->incoming_buffer_length
= 0;
178 // len should be a multiple of the block size now
179 assert((len
% SHA512_BLOCK_SIZE
) == 0);
181 // Set len to the number of blocks to be hashed in the user's buffer
182 len
>>= SHA512_LOG2_BLOCK_SIZE
;
185 ctx
->job
.buffer
= (uint8_t *) buffer
;
187 ctx
= (SHA512_HASH_CTX
*) sha512_mb_mgr_submit_sse(&mgr
->mgr
,
192 // If the extra blocks are not empty, then we are either on the last block(s)
193 // or we need more user input before continuing.
194 if (ctx
->status
& HASH_CTX_STS_LAST
) {
195 uint8_t *buf
= ctx
->partial_block_buffer
;
196 uint32_t n_extra_blocks
= hash_pad(buf
, ctx
->total_length
);
199 (HASH_CTX_STS
) (HASH_CTX_STS_PROCESSING
| HASH_CTX_STS_COMPLETE
);
200 ctx
->job
.buffer
= buf
;
201 ctx
->job
.len
= (uint32_t) n_extra_blocks
;
202 ctx
= (SHA512_HASH_CTX
*) sha512_mb_mgr_submit_sse(&mgr
->mgr
,
208 ctx
->status
= HASH_CTX_STS_IDLE
;
215 static inline void hash_init_digest(SHA512_WORD_T
* digest
)
217 static const SHA512_WORD_T hash_initial_digest
[SHA512_DIGEST_NWORDS
] =
218 { SHA512_INITIAL_DIGEST
};
219 memcpy_fixedlen(digest
, hash_initial_digest
, sizeof(hash_initial_digest
));
222 static inline uint32_t hash_pad(uint8_t padblock
[SHA512_BLOCK_SIZE
* 2], uint32_t total_len
)
224 uint32_t i
= total_len
& (SHA512_BLOCK_SIZE
- 1);
226 memclr_fixedlen(&padblock
[i
], SHA512_BLOCK_SIZE
);
229 // Move i to the end of either 1st or 2nd extra block depending on length
230 i
+= ((SHA512_BLOCK_SIZE
- 1) & (0 - (total_len
+ SHA512_PADLENGTHFIELD_SIZE
+ 1))) +
231 1 + SHA512_PADLENGTHFIELD_SIZE
;
233 #if SHA512_PADLENGTHFIELD_SIZE == 16
234 *((uint64_t *) & padblock
[i
- 16]) = 0;
237 *((uint64_t *) & padblock
[i
- 8]) = _byteswap_uint64((uint64_t) total_len
<< 3);
239 return i
>> SHA512_LOG2_BLOCK_SIZE
; // Number of extra blocks to hash
247 struct slver sha512_ctx_mgr_init_sse_slver_00020163
;
248 struct slver sha512_ctx_mgr_init_sse_slver
= { 0x0163, 0x02, 0x00 };
250 struct slver sha512_ctx_mgr_submit_sse_slver_00020164
;
251 struct slver sha512_ctx_mgr_submit_sse_slver
= { 0x0164, 0x02, 0x00 };
253 struct slver sha512_ctx_mgr_flush_sse_slver_00020165
;
254 struct slver sha512_ctx_mgr_flush_sse_slver
= { 0x0165, 0x02, 0x00 };