Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix performance regression on processors that allow unaligned mem access. #30

Merged
merged 1 commit into from
May 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lib/sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@

#endif /*ENDIANNESS SELECTION*/

#if (defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || \
defined(i386) || defined(__i386) || defined(__i386__) || defined(__i486__) || \
defined(__i586__) || defined(__i686__) || defined(_M_IX86) || defined(__X86__) || \
defined(_X86_) || defined(__THW_INTEL__) || defined(__I86__) || defined(__INTEL__) || \
defined(__386) || defined(_M_X64) || defined(_M_AMD64))

#define SHA1DC_ALLOW_UNALIGNED_ACCESS

#endif /*UNALIGNMENT DETECTION*/


#define rotate_right(x,n) (((x)>>(n))|((x)<<(32-(n))))
#define rotate_left(x,n) (((x)<<(n))|((x)>>(32-(n))))

Expand Down Expand Up @@ -1726,6 +1737,9 @@ void SHA1DCSetCallback(SHA1_CTX* ctx, collision_block_callback callback)
void SHA1DCUpdate(SHA1_CTX* ctx, const char* buf, size_t len)
{
unsigned left, fill;

const uint32_t* buffer_to_hash = NULL;

if (len == 0)
return;

Expand All @@ -1744,8 +1758,14 @@ void SHA1DCUpdate(SHA1_CTX* ctx, const char* buf, size_t len)
while (len >= 64)
{
ctx->total += 64;
memcpy(ctx->buffer, buf, 64);
sha1_process(ctx, (uint32_t*)(ctx->buffer));

#if defined(SHA1DC_ALLOW_UNALIGNED_ACCESS)
buffer_to_hash = (const uint32_t*)buf;
#else
buffer_to_hash = (const uint32_t*)ctx->buffer;
memcpy(ctx->buffer, buf, 64);
#endif /* defined(SHA1DC_ALLOW_UNALIGNED_ACCESS) */
sha1_process(ctx, buffer_to_hash);
buf += 64;
len -= 64;
}
Expand Down