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 **********************************************************************/
31 #include "memcpy_inline.h"
35 #define inline __inline
38 static inline void hash_init_digest(MD5_WORD_T
* digest
);
39 static inline uint32_t hash_pad(uint8_t padblock
[MD5_BLOCK_SIZE
* 2], uint32_t total_len
);
40 static MD5_HASH_CTX
*md5_ctx_mgr_resubmit(MD5_HASH_CTX_MGR
* mgr
, MD5_HASH_CTX
* ctx
);
42 void md5_ctx_mgr_init_avx2(MD5_HASH_CTX_MGR
* mgr
)
44 md5_mb_mgr_init_avx2(&mgr
->mgr
);
47 MD5_HASH_CTX
*md5_ctx_mgr_submit_avx2(MD5_HASH_CTX_MGR
* mgr
, MD5_HASH_CTX
* ctx
,
48 const void *buffer
, uint32_t len
, HASH_CTX_FLAG flags
)
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
;
56 if (ctx
->status
& HASH_CTX_STS_PROCESSING
) {
57 // Cannot submit to a currently processing job.
58 ctx
->error
= HASH_CTX_ERROR_ALREADY_PROCESSING
;
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
;
68 if (flags
& HASH_FIRST
) {
70 hash_init_digest(ctx
->job
.result_digest
);
73 ctx
->total_length
= 0;
76 ctx
->partial_block_buffer_length
= 0;
78 // If we made it here, there were no errors during this call to submit
79 ctx
->error
= HASH_CTX_ERROR_NONE
;
81 // Store buffer ptr info from user
82 ctx
->incoming_buffer
= buffer
;
83 ctx
->incoming_buffer_length
= len
;
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
;
90 // Advance byte counter
91 ctx
->total_length
+= len
;
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
< MD5_BLOCK_SIZE
)) {
96 // Compute how many bytes to copy from user buffer into extra block
97 uint32_t copy_len
= MD5_BLOCK_SIZE
- ctx
->partial_block_buffer_length
;
102 // Copy and update relevant pointers and counters
103 memcpy_varlen(&ctx
->partial_block_buffer
104 [ctx
->partial_block_buffer_length
], buffer
, copy_len
);
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
;
110 // The extra block should never contain more than 1 block here
111 assert(ctx
->partial_block_buffer_length
<= MD5_BLOCK_SIZE
);
113 // If the extra block buffer contains exactly 1 block, it can be hashed.
114 if (ctx
->partial_block_buffer_length
>= MD5_BLOCK_SIZE
) {
115 ctx
->partial_block_buffer_length
= 0;
117 ctx
->job
.buffer
= ctx
->partial_block_buffer
;
119 ctx
= (MD5_HASH_CTX
*) md5_mb_mgr_submit_avx2(&mgr
->mgr
, &ctx
->job
);
123 return md5_ctx_mgr_resubmit(mgr
, ctx
);
126 MD5_HASH_CTX
*md5_ctx_mgr_flush_avx2(MD5_HASH_CTX_MGR
* mgr
)
131 ctx
= (MD5_HASH_CTX
*) md5_mb_mgr_flush_avx2(&mgr
->mgr
);
133 // If flush returned 0, there are no more jobs in flight.
137 // If flush returned a job, verify that it is safe to return to the user.
138 // If it is not ready, resubmit the job to finish processing.
139 ctx
= md5_ctx_mgr_resubmit(mgr
, ctx
);
141 // If md5_ctx_mgr_resubmit returned a job, it is ready to be returned.
145 // Otherwise, all jobs currently being managed by the HASH_CTX_MGR still need processing. Loop.
149 static MD5_HASH_CTX
*md5_ctx_mgr_resubmit(MD5_HASH_CTX_MGR
* mgr
, MD5_HASH_CTX
* ctx
)
153 if (ctx
->status
& HASH_CTX_STS_COMPLETE
) {
154 ctx
->status
= HASH_CTX_STS_COMPLETE
; // Clear PROCESSING bit
157 // If the extra blocks are empty, begin hashing what remains in the user's buffer.
158 if (ctx
->partial_block_buffer_length
== 0 && ctx
->incoming_buffer_length
) {
159 const void *buffer
= ctx
->incoming_buffer
;
160 uint32_t len
= ctx
->incoming_buffer_length
;
162 // Only entire blocks can be hashed. Copy remainder to extra blocks buffer.
163 uint32_t copy_len
= len
& (MD5_BLOCK_SIZE
- 1);
167 //memcpy(ctx->partial_block_buffer, ((const char*)buffer + len), copy_len);
168 memcpy_varlen(ctx
->partial_block_buffer
,
169 ((const char *)buffer
+ len
), copy_len
);
170 ctx
->partial_block_buffer_length
= copy_len
;
173 ctx
->incoming_buffer_length
= 0;
175 // len should be a multiple of the block size now
176 assert((len
% MD5_BLOCK_SIZE
) == 0);
178 // Set len to the number of blocks to be hashed in the user's buffer
179 len
>>= MD5_LOG2_BLOCK_SIZE
;
182 ctx
->job
.buffer
= (uint8_t *) buffer
;
184 ctx
= (MD5_HASH_CTX
*) md5_mb_mgr_submit_avx2(&mgr
->mgr
,
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
) {
193 uint8_t *buf
= ctx
->partial_block_buffer
;
194 uint32_t n_extra_blocks
= hash_pad(buf
, ctx
->total_length
);
197 (HASH_CTX_STS
) (HASH_CTX_STS_PROCESSING
| HASH_CTX_STS_COMPLETE
);
199 ctx
->job
.buffer
= buf
;
200 ctx
->job
.len
= (uint32_t) n_extra_blocks
;
201 ctx
= (MD5_HASH_CTX
*) md5_mb_mgr_submit_avx2(&mgr
->mgr
, &ctx
->job
);
206 ctx
->status
= HASH_CTX_STS_IDLE
;
213 static inline void hash_init_digest(MD5_WORD_T
* digest
)
215 static const MD5_WORD_T hash_initial_digest
[MD5_DIGEST_NWORDS
] =
216 { MD5_INITIAL_DIGEST
};
217 //memcpy(digest, hash_initial_digest, sizeof(hash_initial_digest));
218 memcpy_fixedlen(digest
, hash_initial_digest
, sizeof(hash_initial_digest
));
221 static inline uint32_t hash_pad(uint8_t padblock
[MD5_BLOCK_SIZE
* 2], uint32_t total_len
)
223 uint32_t i
= total_len
& (MD5_BLOCK_SIZE
- 1);
225 // memset(&padblock[i], 0, MD5_BLOCK_SIZE);
226 memclr_fixedlen(&padblock
[i
], MD5_BLOCK_SIZE
);
229 i
+= ((MD5_BLOCK_SIZE
- 1) & (0 - (total_len
+ MD5_PADLENGTHFIELD_SIZE
+ 1))) + 1 +
230 MD5_PADLENGTHFIELD_SIZE
;
232 *((uint64_t *) & padblock
[i
- 8]) = ((uint64_t) total_len
<< 3);
234 return i
>> MD5_LOG2_BLOCK_SIZE
; // Number of extra blocks to hash
242 struct slver md5_ctx_mgr_init_avx2_slver_04020186
;
243 struct slver md5_ctx_mgr_init_avx2_slver
= { 0x0186, 0x02, 0x04 };
245 struct slver md5_ctx_mgr_submit_avx2_slver_04020187
;
246 struct slver md5_ctx_mgr_submit_avx2_slver
= { 0x0187, 0x02, 0x04 };
248 struct slver md5_ctx_mgr_flush_avx2_slver_04020188
;
249 struct slver md5_ctx_mgr_flush_avx2_slver
= { 0x0188, 0x02, 0x04 };