From ff67abdec52b6412842c05dded4c3e2ba203d817 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Tue, 13 Sep 2016 20:44:14 -0400 Subject: [PATCH] Add virtual dtor for IteratedHash and ClonableImpl due to non-trivial data members Solaris is showing unusual signs with SunCC 5.13 and 5.14. One user is experiencing a SIGBUS in SHA512::Transform due to data alignment of 'data', which was only 2-byte aligned. The project experienced an exception "Coneable not implemented" during the hashing test after building with Cmake. Its not clear how much Cmake influenced the project's results. --- iterhash.h | 30 +++++++++++++++++++----------- sha.cpp | 5 ++++- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/iterhash.h b/iterhash.h index 0757687f..43d58d90 100644 --- a/iterhash.h +++ b/iterhash.h @@ -21,7 +21,7 @@ public: //! \brief Iterated hash base class //! \tparam T Hash word type //! \tparam BASE HashTransformation derived class -//! \details BASE should be derived from HashTransformation, MessageAuthenticationCode, or similar class. +//! \details IteratedHashBase provides an interface for block-based iterated hashes //! \sa HashTransformation, MessageAuthenticationCode template class CRYPTOPP_NO_VTABLE IteratedHashBase : public BASE @@ -49,7 +49,7 @@ public: //! \param length the size of the buffer, in bytes void Update(const byte *input, size_t length); - //! \brief Request space which can be written into by the caller + //! \brief Requests space which can be written into by the caller //! \param size the requested size of the buffer //! \details The purpose of this method is to help avoid extra memory allocations. //! \details size is an \a IN and \a OUT parameter and used as a hint. When the call is made, @@ -95,7 +95,7 @@ private: //! \tparam T_Endianness Endianness type of hash //! \tparam T_BlockSize Block size of the hash //! \tparam T_Base HashTransformation derived class -//! \details T_Base should be derived from HashTransformation, MessageAuthenticationCode, or similar class. +//! \details IteratedHash provides a default implementation for block-based iterated hashes //! \sa HashTransformation, MessageAuthenticationCode template class CRYPTOPP_NO_VTABLE IteratedHash : public IteratedHashBase @@ -104,6 +104,10 @@ public: typedef T_Endianness ByteOrderClass; typedef T_HashWordType HashWordType; +#ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 + virtual ~IteratedHash() { } +#endif + CRYPTOPP_CONSTANT(BLOCKSIZE = T_BlockSize) // BCB2006 workaround: can't use BLOCKSIZE here CRYPTOPP_COMPILE_ASSERT((T_BlockSize & (T_BlockSize - 1)) == 0); // blockSize is a power of 2 @@ -111,13 +115,13 @@ public: //! \brief Provides the block size of the hash //! \return the block size of the hash, in bytes //! \details BlockSize() returns T_BlockSize. - CRYPTOPP_CONSTEXPR unsigned int BlockSize() const {return T_BlockSize;} + unsigned int BlockSize() const {return T_BlockSize;} //! \brief Provides the byte order of the hash //! \returns the byte order of the hash as an enumeration //! \details GetByteOrder() returns T_Endianness::ToEnum(). //! \sa ByteOrder() - CRYPTOPP_CONSTEXPR ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();} + ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();} //! \brief Adjusts the byte ordering of the hash //! \param out the output buffer @@ -135,26 +139,30 @@ protected: }; //! \class IteratedHashWithStaticTransform -//! \brief Iterated hash with a static transformation function base class +//! \brief Iterated hash with a static transformation function //! \tparam T_HashWordType Hash word type //! \tparam T_Endianness Endianness type of hash //! \tparam T_BlockSize Block size of the hash //! \tparam T_StateSize Internal state size of the hash -//! \tparam T_Transform Static transformation class +//! \tparam T_Transform HashTransformation derived class //! \tparam T_DigestSize Digest size of the hash //! \tparam T_StateAligned Flag indicating if state is 16-byte aligned -//! \details T_Transform should be derived from HashTransformation, MessageAuthenticationCode, or similar class. //! \sa HashTransformation, MessageAuthenticationCode template class CRYPTOPP_NO_VTABLE IteratedHashWithStaticTransform : public ClonableImpl, T_Transform> > { public: + +#ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 + virtual ~IteratedHashWithStaticTransform() { } +#endif + CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize ? T_DigestSize : T_StateSize) - //! Provides the digest size of the hash - //! \return the digest size of the hash. - //! details DigestSize() returns DIGESTSIZE. + //! \brief Provides the digest size of the hash + //! \return the digest size of the hash, in bytes + //! \details DigestSize() returns DIGESTSIZE. unsigned int DigestSize() const {return DIGESTSIZE;}; protected: diff --git a/sha.cpp b/sha.cpp index bbde30a3..4e847501 100644 --- a/sha.cpp +++ b/sha.cpp @@ -643,7 +643,7 @@ void SHA512::InitState(HashWordType *state) #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE && (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32) CRYPTOPP_ALIGN_DATA(16) static const word64 SHA512_K[80] CRYPTOPP_SECTION_ALIGN16 = { #else -static const word64 SHA512_K[80] = { +CRYPTOPP_ALIGN_DATA(16) static const word64 SHA512_K[80] CRYPTOPP_SECTION_ALIGN16 = { #endif W64LIT(0x428a2f98d728ae22), W64LIT(0x7137449123ef65cd), W64LIT(0xb5c0fbcfec4d3b2f), W64LIT(0xe9b5dba58189dbbc), @@ -886,6 +886,9 @@ CRYPTOPP_NAKED static void CRYPTOPP_FASTCALL SHA512_SSE2_Transform(word64 *state void SHA512::Transform(word64 *state, const word64 *data) { + assert(IsAlignedOn(state, GetAlignmentOf())); + assert(IsAlignedOn(data, GetAlignmentOf())); + #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE && (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32) if (HasSSE2()) {