diff options
author | Dan Shumow <shumow@gmail.com> | 2017-05-18 22:04:44 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-18 22:04:44 +0300 |
commit | db45d67ada5ed259840b1f9eee4bba1131f457e7 (patch) | |
tree | 782db922241d958e862f633b6138eae04841d69a | |
parent | 33a694a9ee1b79c24be45f9eab5ac0e1aeeaf271 (diff) |
Fix performance regression on processors that allow unaligned memory access. (#30)
-rw-r--r-- | lib/sha1.c | 24 |
1 files changed, 22 insertions, 2 deletions
@@ -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)))) @@ -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; @@ -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; } |