fixed Whirlpool crash on Pentium 2 machines

pull/2/head
weidai 2007-08-13 23:53:09 +00:00
parent 2345ab6d80
commit ae88c18bf7
5 changed files with 33 additions and 6 deletions

View File

@ -260,7 +260,7 @@ NAMESPACE_END
#define CRYPTOPP_DISABLE_SSE2 #define CRYPTOPP_DISABLE_SSE2
#endif #endif
#if !defined(CRYPTOPP_DISABLE_ASM) && ((defined(CRYPTOPP_MSVC6PP_OR_LATER) && defined(_M_IX86)) || (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))) #if !defined(CRYPTOPP_DISABLE_ASM) && ((defined(_MSC_VER) && defined(_M_IX86)) || (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))))
#define CRYPTOPP_X86_ASM_AVAILABLE #define CRYPTOPP_X86_ASM_AVAILABLE
#if !defined(CRYPTOPP_DISABLE_SSE2) && (defined(CRYPTOPP_MSVC6PP_OR_LATER) || CRYPTOPP_GCC_VERSION >= 30300) #if !defined(CRYPTOPP_DISABLE_SSE2) && (defined(CRYPTOPP_MSVC6PP_OR_LATER) || CRYPTOPP_GCC_VERSION >= 30300)
@ -339,6 +339,8 @@ NAMESPACE_END
#define CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS #define CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
#endif #endif
#define CRYPTOPP_VERSION 552
// ***************** determine availability of OS features ******************** // ***************** determine availability of OS features ********************
#ifndef NO_OS_DEPENDENCE #ifndef NO_OS_DEPENDENCE

15
cpu.cpp
View File

@ -145,7 +145,7 @@ static bool TrySSE2()
} }
bool g_x86DetectionDone = false; bool g_x86DetectionDone = false;
bool g_hasSSE2 = false, g_hasSSSE3 = false, g_hasMMX = false, g_isP4 = false; bool g_hasISSE = false, g_hasSSE2 = false, g_hasSSSE3 = false, g_hasMMX = false, g_isP4 = false;
word32 g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE; word32 g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE;
void DetectX86Features() void DetectX86Features()
@ -161,6 +161,19 @@ void DetectX86Features()
g_hasSSE2 = TrySSE2(); g_hasSSE2 = TrySSE2();
g_hasSSSE3 = g_hasSSE2 && (cpuid1[2] & (1<<9)); g_hasSSSE3 = g_hasSSE2 && (cpuid1[2] & (1<<9));
if ((cpuid1[3] & (1 << 25)) != 0)
g_hasISSE = true;
else
{
word32 cpuid2[4];
CpuId(0x080000000, cpuid2);
if (cpuid2[0] >= 0x080000001)
{
CpuId(0x080000001, cpuid2);
g_hasISSE = (cpuid2[3] & (1 << 22)) != 0;
}
}
std::swap(cpuid[2], cpuid[3]); std::swap(cpuid[2], cpuid[3]);
if (memcmp(cpuid+1, "GenuineIntel", 12) == 0) if (memcmp(cpuid+1, "GenuineIntel", 12) == 0)
{ {

11
cpu.h
View File

@ -16,6 +16,7 @@ NAMESPACE_BEGIN(CryptoPP)
// these should not be used directly // these should not be used directly
extern CRYPTOPP_DLL bool g_x86DetectionDone; extern CRYPTOPP_DLL bool g_x86DetectionDone;
extern CRYPTOPP_DLL bool g_hasSSE2; extern CRYPTOPP_DLL bool g_hasSSE2;
extern CRYPTOPP_DLL bool g_hasISSE;
extern CRYPTOPP_DLL bool g_hasMMX; extern CRYPTOPP_DLL bool g_hasMMX;
extern CRYPTOPP_DLL bool g_hasSSSE3; extern CRYPTOPP_DLL bool g_hasSSSE3;
extern CRYPTOPP_DLL bool g_isP4; extern CRYPTOPP_DLL bool g_isP4;
@ -26,6 +27,7 @@ CRYPTOPP_DLL bool CpuId(word32 input, word32 *output);
#if CRYPTOPP_BOOL_X64 #if CRYPTOPP_BOOL_X64
inline bool HasSSE2() {return true;} inline bool HasSSE2() {return true;}
inline bool HasISSE() {return true;}
inline bool HasMMX() {return true;} inline bool HasMMX() {return true;}
#else #else
@ -36,6 +38,13 @@ inline bool HasSSE2()
return g_hasSSE2; return g_hasSSE2;
} }
inline bool HasISSE()
{
if (!g_x86DetectionDone)
DetectX86Features();
return g_hasISSE;
}
inline bool HasMMX() inline bool HasMMX()
{ {
if (!g_x86DetectionDone) if (!g_x86DetectionDone)
@ -79,9 +88,11 @@ inline bool IsP4() {return false;}
// assume MMX and SSE2 if intrinsics are enabled // assume MMX and SSE2 if intrinsics are enabled
#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE || CRYPTOPP_BOOL_X64 #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE || CRYPTOPP_BOOL_X64
inline bool HasSSE2() {return true;} inline bool HasSSE2() {return true;}
inline bool HasISSE() {return true;}
inline bool HasMMX() {return true;} inline bool HasMMX() {return true;}
#else #else
inline bool HasSSE2() {return false;} inline bool HasSSE2() {return false;}
inline bool HasISSE() {return false;}
inline bool HasMMX() {return false;} inline bool HasMMX() {return false;}
#endif #endif

View File

@ -229,6 +229,7 @@ bool TestSettings()
cout << endl; cout << endl;
bool hasMMX = HasMMX(); bool hasMMX = HasMMX();
bool hasISSE = HasISSE();
bool hasSSE2 = HasSSE2(); bool hasSSE2 = HasSSE2();
bool hasSSSE3 = HasSSSE3(); bool hasSSSE3 = HasSSSE3();
bool isP4 = IsP4(); bool isP4 = IsP4();
@ -242,7 +243,7 @@ bool TestSettings()
else else
cout << "passed: "; cout << "passed: ";
cout << "hasMMX == " << hasMMX << ", hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize; cout << "hasMMX == " << hasMMX << ", hasISSE == " << hasISSE << ", hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize;
if (!pass) if (!pass)
{ {

View File

@ -1,6 +1,6 @@
// whrlpool.cpp - originally modified by Kevin Springle from // whrlpool.cpp - originally modified by Kevin Springle from
// Paulo Barreto and Vincent Rijmen's public domain code, whirlpool.c. // Paulo Barreto and Vincent Rijmen's public domain code, whirlpool.c.
// Updated to Whirlpool version 3.0, optimized and MMX version added by Wei Dai // Updated to Whirlpool version 3.0, optimized and SSE version added by Wei Dai
// Any modifications are placed in the public domain // Any modifications are placed in the public domain
// This is the original introductory comment: // This is the original introductory comment:
@ -390,8 +390,8 @@ CRYPTOPP_ALIGN_DATA(16) static const word64 Whirlpool_C[4*256+R] CRYPTOPP_SECTIO
// Whirlpool basic transformation. Transforms state based on block. // Whirlpool basic transformation. Transforms state based on block.
void Whirlpool::Transform(word64 *digest, const word64 *block) void Whirlpool::Transform(word64 *digest, const word64 *block)
{ {
#if defined(CRYPTOPP_X86_ASM_AVAILABLE) #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
if (HasMMX()) if (HasISSE())
{ {
// MMX version has the same structure as C version below // MMX version has the same structure as C version below
#ifdef __GNUC__ #ifdef __GNUC__