Add Power8 SHA256 and SHA512 support (GH #513)

pull/484/merge
Jeffrey Walton 2017-09-22 09:39:36 -04:00
parent 3bd01f73ba
commit ced7cff64f
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
3 changed files with 42 additions and 15 deletions

View File

@ -75,22 +75,15 @@ bool CPU_ProbeAltivec()
result = false; result = false;
else else
{ {
CRYPTOPP_ALIGN_DATA(16)
const byte b1[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; const byte b1[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
CRYPTOPP_ALIGN_DATA(16)
const byte b2[16] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; const byte b2[16] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
CRYPTOPP_ALIGN_DATA(16) byte b3[16]; byte b3[16];
#if defined(CRYPTOPP_XLC_VERSION)
const uint8x16_p8 v1 = VectorLoad(0, b1); const uint8x16_p8 v1 = (uint8x16_p8)VectorLoad(0, b1);
const uint8x16_p8 v2 = VectorLoad(0, b2); const uint8x16_p8 v2 = (uint8x16_p8)VectorLoad(0, b2);
const uint8x16_p8 v3 = VectorXor(v1, v2); const uint8x16_p8 v3 = (uint8x16_p8)VectorXor(v1, v2);
vec_st(v3, 0, (byte*)b3); VectorStore(v3, b3);
#elif defined(CRYPTOPP_GCC_VERSION)
const uint64x2_p8 v1 = (uint64x2_p8)VectorLoad(0, b1);
const uint64x2_p8 v2 = (uint64x2_p8)VectorLoad(0, b2);
const uint64x2_p8 v3 = (uint64x2_p8)VectorXor(v1, v2);
vec_st((uint8x16_p8)v3, 0, (byte*)b3);
#endif
result = (0 == std::memcmp(b2, b3, 16)); result = (0 == std::memcmp(b2, b3, 16));
} }
@ -210,6 +203,8 @@ bool CPU_ProbeAES()
uint8x16_p8 s = (uint8x16_p8)VectorLoad(0, state); uint8x16_p8 s = (uint8x16_p8)VectorLoad(0, state);
s = VectorEncrypt(s, k); s = VectorEncrypt(s, k);
s = VectorEncryptLast(s, k); s = VectorEncryptLast(s, k);
s = VectorDecrypt(s, k);
s = VectorDecryptLast(s, k);
VectorStore(s, r); VectorStore(s, r);
result = (0 != std::memcmp(r, z, 16)); result = (0 != std::memcmp(r, z, 16));
@ -247,7 +242,16 @@ bool CPU_ProbeSHA256()
result = false; result = false;
else else
{ {
byte r[16], z[16] = {0};
uint8x16_p8 x = ((uint8x16_p8){0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0});
x = VectorSHA256<0,0>(x);
x = VectorSHA256<0,1>(x);
x = VectorSHA256<1,0>(x);
x = VectorSHA256<1,1>(x);
VectorStore(x, r);
result = (0 != std::memcmp(r, z, 16));
} }
sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR); sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
@ -282,7 +286,16 @@ bool CPU_ProbeSHA512()
result = false; result = false;
else else
{ {
byte r[16], z[16] = {0};
uint8x16_p8 x = ((uint8x16_p8){0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0});
x = VectorSHA512<0,0>(x);
x = VectorSHA512<0,1>(x);
x = VectorSHA512<1,0>(x);
x = VectorSHA512<1,1>(x);
VectorStore(x, r);
result = (0 != std::memcmp(r, z, 16));
} }
sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR); sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);

View File

@ -991,7 +991,7 @@ void SHA512_HashMultipleBlocks_POWER8(word64 *state, const word64 *data, size_t
CRYPTOPP_ASSERT(state); CRYPTOPP_ASSERT(state);
CRYPTOPP_ASSERT(data); CRYPTOPP_ASSERT(data);
CRYPTOPP_ASSERT(length >= SHA512::BLOCKSIZE); CRYPTOPP_ASSERT(length >= SHA512::BLOCKSIZE);
CRYPTOPP_ASSERT(0); CRYPTOPP_ASSERT(0);
} }

14
sha.cpp
View File

@ -783,6 +783,13 @@ size_t SHA224::HashMultipleBlocks(const word32 *input, size_t length)
return length & (SHA256::BLOCKSIZE - 1); return length & (SHA256::BLOCKSIZE - 1);
} }
#endif #endif
#if CRYPTOPP_POWER8_SHA_AVAILABLE
if (HasSHA256())
{
SHA256_HashMultipleBlocks_POWER8(m_state, input, length, BIG_ENDIAN_ORDER);
return length & (SHA256::BLOCKSIZE - 1);
}
#endif
const bool noReverse = NativeByteOrderIs(this->GetByteOrder()); const bool noReverse = NativeByteOrderIs(this->GetByteOrder());
word32 *dataBuf = this->DataBuf(); word32 *dataBuf = this->DataBuf();
@ -1164,6 +1171,13 @@ void SHA512::Transform(word64 *state, const word64 *data)
return; return;
} }
#endif #endif
#if CRYPTOPP_POWER8_SHA_AVAILABLE
if (HasSHA512())
{
SHA512_HashMultipleBlocks_POWER8(state, data, SHA512::BLOCKSIZE, BIG_ENDIAN_ORDER);
return;
}
#endif
SHA512_HashBlock_CXX(state, data); SHA512_HashBlock_CXX(state, data);
} }