Use MOVDQU for SSE2 in static transform (Issue 455)

Updated documentation
This commit is contained in:
Jeffrey Walton 2017-08-05 00:24:02 -04:00
parent a2cab93117
commit 20def29d33
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
2 changed files with 140 additions and 32 deletions

46
sha.cpp
View File

@ -723,8 +723,8 @@ static void CRYPTOPP_FASTCALL X86_SHA256_HashBlocks(word32 *state, const word32
ASJ( jnz, 2, f)
AS1( dec DWORD PTR K_END)
#endif
AS2( movdqa xmm0, XMMWORD_PTR [WORD_REG(cx)+0*16])
AS2( movdqa xmm1, XMMWORD_PTR [WORD_REG(cx)+1*16])
AS2( movdqu xmm0, XMMWORD_PTR [WORD_REG(cx)+0*16])
AS2( movdqu xmm1, XMMWORD_PTR [WORD_REG(cx)+1*16])
#endif
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32
@ -744,8 +744,8 @@ INTEL_NOPREFIX
#if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
ASL(0)
AS2( movdqa E(0), xmm1)
AS2( movdqa A(0), xmm0)
AS2( movdqu E(0), xmm1)
AS2( movdqu A(0), xmm0)
#endif
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32
ASL(3)
@ -812,12 +812,12 @@ INTEL_NOPREFIX
AS2( test DWORD PTR K_END, 1)
ASJ( jz, 4, f)
#endif
AS2( movdqa xmm1, XMMWORD_PTR [AS_REG_7+1*16])
AS2( movdqa xmm0, XMMWORD_PTR [AS_REG_7+0*16])
AS2( movdqu xmm1, XMMWORD_PTR [AS_REG_7+1*16])
AS2( movdqu xmm0, XMMWORD_PTR [AS_REG_7+0*16])
AS2( paddd xmm1, E(0))
AS2( paddd xmm0, A(0))
AS2( movdqa [AS_REG_7+1*16], xmm1)
AS2( movdqa [AS_REG_7+0*16], xmm0)
AS2( movdqu [AS_REG_7+1*16], xmm1)
AS2( movdqu [AS_REG_7+0*16], xmm0)
AS2( cmp WORD_REG(dx), DATA_END)
ATT_NOPREFIX
ASJ( jb, 0, b)
@ -1583,16 +1583,16 @@ CRYPTOPP_NAKED static void CRYPTOPP_FASTCALL SHA512_SSE2_Transform(word64 *state
AS2( lea esi, [esp+4+20*8+8]) // 16-byte alignment, then add 8
#endif
AS2( movdqa xmm0, [ecx+0*16])
AS2( movdqu xmm0, [ecx+0*16])
AS2( movdq2q mm4, xmm0)
AS2( movdqa [edi+0*16], xmm0)
AS2( movdqa xmm0, [ecx+1*16])
AS2( movdqa [edi+1*16], xmm0)
AS2( movdqa xmm0, [ecx+2*16])
AS2( movdqu [edi+0*16], xmm0)
AS2( movdqu xmm0, [ecx+1*16])
AS2( movdqu [edi+1*16], xmm0)
AS2( movdqu xmm0, [ecx+2*16])
AS2( movdq2q mm5, xmm0)
AS2( movdqa [edi+2*16], xmm0)
AS2( movdqa xmm0, [ecx+3*16])
AS2( movdqa [edi+3*16], xmm0)
AS2( movdqu [edi+2*16], xmm0)
AS2( movdqu xmm0, [ecx+3*16])
AS2( movdqu [edi+3*16], xmm0)
ASJ( jmp, 0, f)
#define SSE2_S0_S1(r, a, b, c) \
@ -1611,9 +1611,9 @@ CRYPTOPP_NAKED static void CRYPTOPP_FASTCALL SHA512_SSE2_Transform(word64 *state
AS2( pxor r, mm6)
#define SSE2_s0(r, a, b, c) \
AS2( movdqa xmm6, r)\
AS2( movdqu xmm6, r)\
AS2( psrlq r, a)\
AS2( movdqa xmm7, r)\
AS2( movdqu xmm7, r)\
AS2( psllq xmm6, 64-c)\
AS2( pxor xmm7, xmm6)\
AS2( psrlq r, b-a)\
@ -1624,9 +1624,9 @@ CRYPTOPP_NAKED static void CRYPTOPP_FASTCALL SHA512_SSE2_Transform(word64 *state
AS2( pxor r, xmm6)
#define SSE2_s1(r, a, b, c) \
AS2( movdqa xmm6, r)\
AS2( movdqu xmm6, r)\
AS2( psrlq r, a)\
AS2( movdqa xmm7, r)\
AS2( movdqu xmm7, r)\
AS2( psllq xmm6, 64-c)\
AS2( pxor xmm7, xmm6)\
AS2( psrlq r, b-a)\
@ -1684,7 +1684,7 @@ CRYPTOPP_NAKED static void CRYPTOPP_FASTCALL SHA512_SSE2_Transform(word64 *state
// data expansion, W[i-2] already in xmm0
AS2( movdqu xmm3, [esi])
AS2( paddq xmm3, [esi+(16-7)*8])
AS2( movdqa xmm2, [esi+(16-15)*8])
AS2( movdqu xmm2, [esi+(16-15)*8])
SSE2_s1(xmm0, 6, 19, 61)
AS2( paddq xmm0, xmm3)
SSE2_s0(xmm2, 1, 7, 8)
@ -1721,9 +1721,9 @@ CRYPTOPP_NAKED static void CRYPTOPP_FASTCALL SHA512_SSE2_Transform(word64 *state
ASJ( jne, 1, b)
#define SSE2_CombineState(i) \
AS2( movdqa xmm0, [edi+i*16])\
AS2( movdqu xmm0, [edi+i*16])\
AS2( paddq xmm0, [ecx+i*16])\
AS2( movdqa [ecx+i*16], xmm0)
AS2( movdqu [ecx+i*16], xmm0)
SSE2_CombineState(0)
SSE2_CombineState(1)

126
sha.h
View File

@ -25,12 +25,34 @@ NAMESPACE_BEGIN(CryptoPP)
class CRYPTOPP_DLL SHA1 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 20, SHA1>
{
public:
//! \brief Initialize state array
//! \param state the state of the hash
//! \details InitState sets a state array to SHA1 initial values
//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
//! member functions InitState and Transform. External classes, like SEAL and MDC,
//! can initialize state with a user provided key and operate the hash on the data
//! with the used supplied initial state.
//! \note On Intel platforms the state array must be 16-byte aligned for SSE2.
static void CRYPTOPP_API InitState(HashWordType *state);
//! \brief Operate the hash
//! \param digest the state of the hash
//! \param data the data to be digested
//! \details Transform operates the hash on <tt>data</tt>. When the call is invoked
//! <tt>data</tt> holds initial state. Upon return <tt>data</tt> holds the hash or
//! updated state.
//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
//! member functions InitState and Transform. External classes, like SEAL and MDC,
//! can initialize state with a user provided key and operate the hash on the data
//! with the used supplied initial state.
//! \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
static void CRYPTOPP_API Transform(word32 *digest, const word32 *data);
//! \brief The algorithm name
//! \returns C-style string "SHA-1"
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-1";}
#if CRYPTOPP_BOOL_SSE_SHA_INTRINSICS_AVAILABLE
size_t HashMultipleBlocks(const word32 *input, size_t length);
#endif
static void CRYPTOPP_API InitState(HashWordType *state);
static void CRYPTOPP_API Transform(word32 *digest, const word32 *data);
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-1";}
};
//! \class SHA256
@ -40,12 +62,34 @@ public:
class CRYPTOPP_DLL SHA256 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 32, SHA256, 32, true>
{
public:
//! \brief Initialize state array
//! \param state the state of the hash
//! \details InitState sets a state array to SHA256 initial values
//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
//! member functions InitState and Transform. External classes, like SEAL and MDC,
//! can initialize state with a user provided key and operate the hash on the data
//! with the used supplied initial state.
//! \note On Intel platforms the state array must be 16-byte aligned for SSE2.
static void CRYPTOPP_API InitState(HashWordType *state);
//! \brief Operate the hash
//! \param digest the state of the hash
//! \param data the data to be digested
//! \details Transform operates the hash on <tt>data</tt>. When the call is invoked
//! <tt>data</tt> holds initial state. Upon return <tt>data</tt> holds the hash or
//! updated state.
//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
//! member functions InitState and Transform. External classes, like SEAL and MDC,
//! can initialize state with a user provided key and operate the hash on the data
//! with the used supplied initial state.
//! \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
static void CRYPTOPP_API Transform(word32 *digest, const word32 *data);
//! \brief The algorithm name
//! \returns C-style string "SHA-256"
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-256";}
#if (defined(CRYPTOPP_X86_ASM_AVAILABLE) || defined(CRYPTOPP_X32_ASM_AVAILABLE) || defined(CRYPTOPP_X64_MASM_AVAILABLE)) && !defined(CRYPTOPP_DISABLE_SHA_ASM)
size_t HashMultipleBlocks(const word32 *input, size_t length);
#endif
static void CRYPTOPP_API InitState(HashWordType *state);
static void CRYPTOPP_API Transform(word32 *digest, const word32 *data);
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-256";}
};
//! \class SHA224
@ -55,12 +99,34 @@ public:
class CRYPTOPP_DLL SHA224 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 32, SHA224, 28, true>
{
public:
//! \brief Initialize state array
//! \param state the state of the hash
//! \details InitState sets a state array to SHA224 initial values
//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
//! member functions InitState and Transform. External classes, like SEAL and MDC,
//! can initialize state with a user provided key and operate the hash on the data
//! with the used supplied initial state.
//! \note On Intel platforms the state array must be 16-byte aligned for SSE2.
static void CRYPTOPP_API InitState(HashWordType *state);
//! \brief Operate the hash
//! \param digest the state of the hash
//! \param data the data to be digested
//! \details Transform operates the hash on <tt>data</tt>. When the call is invoked
//! <tt>data</tt> holds initial state. Upon return <tt>data</tt> holds the hash or
//! updated state.
//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
//! member functions InitState and Transform. External classes, like SEAL and MDC,
//! can initialize state with a user provided key and operate the hash on the data
//! with the used supplied initial state.
//! \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
static void CRYPTOPP_API Transform(word32 *digest, const word32 *data) {SHA256::Transform(digest, data);}
//! \brief The algorithm name
//! \returns C-style string "SHA-224"
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-224";}
#if (defined(CRYPTOPP_X86_ASM_AVAILABLE) || defined(CRYPTOPP_X32_ASM_AVAILABLE) || defined(CRYPTOPP_X64_MASM_AVAILABLE)) && !defined(CRYPTOPP_DISABLE_SHA_ASM)
size_t HashMultipleBlocks(const word32 *input, size_t length);
#endif
static void CRYPTOPP_API InitState(HashWordType *state);
static void CRYPTOPP_API Transform(word32 *digest, const word32 *data) {SHA256::Transform(digest, data);}
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-224";}
};
//! \class SHA512
@ -70,8 +136,29 @@ public:
class CRYPTOPP_DLL SHA512 : public IteratedHashWithStaticTransform<word64, BigEndian, 128, 64, SHA512, 64, (CRYPTOPP_BOOL_X86|CRYPTOPP_BOOL_X32)>
{
public:
//! \brief Initialize state array
//! \param state the state of the hash
//! \details InitState sets a state array to SHA512 initial values
//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
//! member functions InitState and Transform. External classes, like SEAL and MDC,
//! can initialize state with a user provided key and operate the hash on the data
//! with the used supplied initial state.
//! \note On Intel platforms the state array must be 16-byte aligned for SSE2.
static void CRYPTOPP_API InitState(HashWordType *state);
//! \brief Operate the hash
//! \param digest the state of the hash
//! \param data the data to be digested
//! \details Transform operates the hash on <tt>data</tt>. When the call is invoked
//! <tt>data</tt> holds initial state. Upon return <tt>data</tt> holds the hash or
//! updated state.
//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
//! member functions InitState and Transform. External classes, like SEAL and MDC,
//! can initialize state with a user provided key and operate the hash on the data
//! with the used supplied initial state.
//! \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
static void CRYPTOPP_API Transform(word64 *digest, const word64 *data);
//! \brief The algorithm name
//! \returns C-style string "SHA-512"
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-512";}
};
@ -82,8 +169,29 @@ public:
class CRYPTOPP_DLL SHA384 : public IteratedHashWithStaticTransform<word64, BigEndian, 128, 64, SHA384, 48, (CRYPTOPP_BOOL_X86|CRYPTOPP_BOOL_X32)>
{
public:
//! \brief Initialize state array
//! \param state the state of the hash
//! \details InitState sets a state array to SHA384 initial values
//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
//! member functions InitState and Transform. External classes, like SEAL and MDC,
//! can initialize state with a user provided key and operate the hash on the data
//! with the used supplied initial state.
//! \note On Intel platforms the state array must be 16-byte aligned for SSE2.
static void CRYPTOPP_API InitState(HashWordType *state);
//! \brief Operate the hash
//! \param digest the state of the hash
//! \param data the data to be digested
//! \details Transform operates the hash on <tt>data</tt>. When the call is invoked
//! <tt>data</tt> holds initial state. Upon return <tt>data</tt> holds the hash or
//! updated state.
//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
//! member functions InitState and Transform. External classes, like SEAL and MDC,
//! can initialize state with a user provided key and operate the hash on the data
//! with the used supplied initial state.
//! \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
static void CRYPTOPP_API Transform(word64 *digest, const word64 *data) {SHA512::Transform(digest, data);}
//! \brief The algorithm name
//! \returns C-style string "SHA-384"
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-384";}
};