Disable X32 for SHA (GH #686)

Thanks to Peter Cordes for the feedback
pull/668/merge
Jeffrey Walton 2018-07-15 11:39:41 -04:00
parent 7d1eb4fc8b
commit 31533db421
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
2 changed files with 14 additions and 25 deletions

37
sha.cpp
View File

@ -44,7 +44,7 @@
// Clang 3.3 integrated assembler crash on Linux // Clang 3.3 integrated assembler crash on Linux
// http://github.com/weidai11/cryptopp/issues/264 // http://github.com/weidai11/cryptopp/issues/264
// Clang 3.4.1 (x86) crash on FreeBSD 10.3. Clang 3.4.1 (x64) works fine. // Clang 3.4.1 (x86) crash on FreeBSD 10.3. Clang 3.4.1 (x64) works fine.
#if defined(CRYPTOPP_LLVM_CLANG_VERSION) && (CRYPTOPP_LLVM_CLANG_VERSION < 30500) #if (defined(CRYPTOPP_LLVM_CLANG_VERSION) && (CRYPTOPP_LLVM_CLANG_VERSION < 30500)) || CRYPTOPP_BOOL_X32
# define CRYPTOPP_DISABLE_SHA_ASM # define CRYPTOPP_DISABLE_SHA_ASM
#endif #endif
@ -383,7 +383,7 @@ void SHA256::InitState(HashWordType *state)
} }
#endif // Not CRYPTOPP_GENERATE_X64_MASM #endif // Not CRYPTOPP_GENERATE_X64_MASM
#if (defined(CRYPTOPP_X86_ASM_AVAILABLE) || defined(CRYPTOPP_X32_ASM_AVAILABLE) || defined(CRYPTOPP_GENERATE_X64_MASM)) #if defined(CRYPTOPP_X86_ASM_AVAILABLE)
ANONYMOUS_NAMESPACE_BEGIN ANONYMOUS_NAMESPACE_BEGIN
@ -407,9 +407,7 @@ void CRYPTOPP_FASTCALL SHA256_HashMultipleBlocks_SSE2(word32 *state, const word3
#define DATA_SAVE [BASE+8*4+16*4+2*WORD_SZ] #define DATA_SAVE [BASE+8*4+16*4+2*WORD_SZ]
#define DATA_END [BASE+8*4+16*4+3*WORD_SZ] #define DATA_END [BASE+8*4+16*4+3*WORD_SZ]
#define Kt(i) WORD_REG(si)+(i)*4 #define Kt(i) WORD_REG(si)+(i)*4
#if CRYPTOPP_BOOL_X32 #if CRYPTOPP_BOOL_X86
#define BASE esp+8
#elif CRYPTOPP_BOOL_X86
#define BASE esp+4 #define BASE esp+4
#elif defined(__GNUC__) #elif defined(__GNUC__)
#define BASE r8 #define BASE r8
@ -520,7 +518,7 @@ void CRYPTOPP_FASTCALL SHA256_HashMultipleBlocks_SSE2(word32 *state, const word3
lea rsi, [?SHA256_K@CryptoPP@@3QBIB + 48*4] lea rsi, [?SHA256_K@CryptoPP@@3QBIB + 48*4]
#endif #endif
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 #if CRYPTOPP_BOOL_X86
#ifndef __GNUC__ #ifndef __GNUC__
AS2( mov edi, [len]) AS2( mov edi, [len])
AS2( lea WORD_REG(si), [SHA256_K+48*4]) AS2( lea WORD_REG(si), [SHA256_K+48*4])
@ -542,7 +540,7 @@ void CRYPTOPP_FASTCALL SHA256_HashMultipleBlocks_SSE2(word32 *state, const word3
AS2( mov K_END, WORD_REG(si)) AS2( mov K_END, WORD_REG(si))
#if CRYPTOPP_SSE2_ASM_AVAILABLE #if CRYPTOPP_SSE2_ASM_AVAILABLE
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 #if CRYPTOPP_BOOL_X86
AS2( test edi, 1) AS2( test edi, 1)
ASJ( jnz, 2, f) ASJ( jnz, 2, f)
AS1( dec DWORD PTR K_END) AS1( dec DWORD PTR K_END)
@ -551,7 +549,7 @@ void CRYPTOPP_FASTCALL SHA256_HashMultipleBlocks_SSE2(word32 *state, const word3
AS2( movdqu xmm1, XMMWORD_PTR [WORD_REG(cx)+1*16]) AS2( movdqu xmm1, XMMWORD_PTR [WORD_REG(cx)+1*16])
#endif #endif
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 #if CRYPTOPP_BOOL_X86
#if CRYPTOPP_SSE2_ASM_AVAILABLE #if CRYPTOPP_SSE2_ASM_AVAILABLE
ASJ( jmp, 0, f) ASJ( jmp, 0, f)
#endif #endif
@ -571,13 +569,13 @@ INTEL_NOPREFIX
AS2( movdqu E(0), xmm1) AS2( movdqu E(0), xmm1)
AS2( movdqu A(0), xmm0) AS2( movdqu A(0), xmm0)
#endif #endif
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 #if CRYPTOPP_BOOL_X86
ASL(3) ASL(3)
#endif #endif
AS2( sub WORD_REG(si), 48*4) AS2( sub WORD_REG(si), 48*4)
SWAP_COPY(0) SWAP_COPY(1) SWAP_COPY(2) SWAP_COPY(3) SWAP_COPY(0) SWAP_COPY(1) SWAP_COPY(2) SWAP_COPY(3)
SWAP_COPY(4) SWAP_COPY(5) SWAP_COPY(6) SWAP_COPY(7) SWAP_COPY(4) SWAP_COPY(5) SWAP_COPY(6) SWAP_COPY(7)
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 #if CRYPTOPP_BOOL_X86
SWAP_COPY(8) SWAP_COPY(9) SWAP_COPY(10) SWAP_COPY(11) SWAP_COPY(8) SWAP_COPY(9) SWAP_COPY(10) SWAP_COPY(11)
SWAP_COPY(12) SWAP_COPY(13) SWAP_COPY(14) SWAP_COPY(15) SWAP_COPY(12) SWAP_COPY(13) SWAP_COPY(14) SWAP_COPY(15)
#endif #endif
@ -632,7 +630,7 @@ INTEL_NOPREFIX
AS2( mov DATA_SAVE, WORD_REG(dx)) AS2( mov DATA_SAVE, WORD_REG(dx))
#if CRYPTOPP_SSE2_ASM_AVAILABLE #if CRYPTOPP_SSE2_ASM_AVAILABLE
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 #if CRYPTOPP_BOOL_X86
AS2( test DWORD PTR K_END, 1) AS2( test DWORD PTR K_END, 1)
ASJ( jz, 4, f) ASJ( jz, 4, f)
#endif #endif
@ -648,7 +646,7 @@ INTEL_NOPREFIX
INTEL_NOPREFIX INTEL_NOPREFIX
#endif #endif
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 #if CRYPTOPP_BOOL_X86
#if CRYPTOPP_SSE2_ASM_AVAILABLE #if CRYPTOPP_SSE2_ASM_AVAILABLE
ASJ( jmp, 5, f) ASJ( jmp, 5, f)
ASL(4) // non-SSE2 ASL(4) // non-SSE2
@ -708,7 +706,7 @@ INTEL_NOPREFIX
ANONYMOUS_NAMESPACE_END ANONYMOUS_NAMESPACE_END
#endif // CRYPTOPP_X86_ASM_AVAILABLE or CRYPTOPP_GENERATE_X64_MASM #endif // CRYPTOPP_X86_ASM_AVAILABLE
#ifndef CRYPTOPP_GENERATE_X64_MASM #ifndef CRYPTOPP_GENERATE_X64_MASM
@ -957,7 +955,7 @@ const word64 SHA512_K[80] = {
W64LIT(0x5fcb6fab3ad6faec), W64LIT(0x6c44198c4a475817) W64LIT(0x5fcb6fab3ad6faec), W64LIT(0x6c44198c4a475817)
}; };
#if CRYPTOPP_SSE2_ASM_AVAILABLE && (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32) #if CRYPTOPP_SSE2_ASM_AVAILABLE && (CRYPTOPP_BOOL_X86)
// Anonymous namespace removed due to a new compile error. // Anonymous namespace removed due to a new compile error.
// g++ -DNDEBUG -g2 -O3 -pthread -pipe -c sha.cpp // g++ -DNDEBUG -g2 -O3 -pthread -pipe -c sha.cpp
@ -988,13 +986,8 @@ CRYPTOPP_NAKED void CRYPTOPP_FASTCALL SHA512_HashBlock_SSE2(word64 *state, const
AS_PUSH_IF86( ax) AS_PUSH_IF86( ax)
AS2( xor eax, eax) AS2( xor eax, eax)
#if CRYPTOPP_BOOL_X32
AS2( lea edi, [esp+8+8*8]) // start at middle of state buffer. will decrement pointer each round to avoid copying
AS2( lea esi, [esp+8+20*8+8]) // 16-byte alignment, then add 8
#else
AS2( lea edi, [esp+4+8*8]) // start at middle of state buffer. will decrement pointer each round to avoid copying AS2( lea edi, [esp+4+8*8]) // start at middle of state buffer. will decrement pointer each round to avoid copying
AS2( lea esi, [esp+4+20*8+8]) // 16-byte alignment, then add 8 AS2( lea esi, [esp+4+20*8+8]) // 16-byte alignment, then add 8
#endif
AS2( movdqu xmm0, [ecx+0*16]) AS2( movdqu xmm0, [ecx+0*16])
AS2( movdq2q mm4, xmm0) AS2( movdq2q mm4, xmm0)
@ -1125,11 +1118,7 @@ CRYPTOPP_NAKED void CRYPTOPP_FASTCALL SHA512_HashBlock_SSE2(word64 *state, const
// do housekeeping every 8 rounds // do housekeeping every 8 rounds
AS2( mov esi, 0xf) AS2( mov esi, 0xf)
AS2( and esi, eax) AS2( and esi, eax)
#if CRYPTOPP_BOOL_X32
AS2( lea esi, [esp+8+20*8+8+esi*8])
#else
AS2( lea esi, [esp+4+20*8+8+esi*8]) AS2( lea esi, [esp+4+20*8+8+esi*8])
#endif
AS2( add edi, 8*8) AS2( add edi, 8*8)
AS2( cmp eax, 80) AS2( cmp eax, 80)
ASJ( jne, 1, b) ASJ( jne, 1, b)
@ -1248,7 +1237,7 @@ void SHA512::Transform(word64 *state, const word64 *data)
CRYPTOPP_ASSERT(state); CRYPTOPP_ASSERT(state);
CRYPTOPP_ASSERT(data); CRYPTOPP_ASSERT(data);
#if CRYPTOPP_SSE2_ASM_AVAILABLE && (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32) #if CRYPTOPP_SSE2_ASM_AVAILABLE && (CRYPTOPP_BOOL_X86)
if (HasSSE2()) if (HasSSE2())
{ {
SHA512_HashBlock_SSE2(state, data); SHA512_HashBlock_SSE2(state, data);

2
sha.h
View File

@ -11,7 +11,7 @@
#include "config.h" #include "config.h"
#include "iterhash.h" #include "iterhash.h"
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32) #if (CRYPTOPP_BOOL_X86)
# define SHA_X86_ALIGN16 true # define SHA_X86_ALIGN16 true
#else #else
# define SHA_X86_ALIGN16 false # define SHA_X86_ALIGN16 false