Add constants for MIN_KEYLENGTH, MAX_KEYLENGTH, DEFAULT_KEYLENGTH. Fix keyed hash calculation. Fix tree mode variable initialization. Cleanup whitespace in BLAKE2_SSE2_Compress64

pull/157/head
Jeffrey Walton 2016-04-18 00:46:59 -04:00
parent 5607bd571f
commit 1b661bb688
2 changed files with 26 additions and 17 deletions

View File

@ -203,7 +203,11 @@ void BLAKE2_Base<W, T_64bit>::UncheckedSetKey(const byte *key, unsigned int leng
{
AlignedSecByteBlock k(KEYBLOCKSIZE);
memcpy_s(k, KEYBLOCKSIZE, key, length);
memset(k+length, 0x00, KEYBLOCKSIZE-length);
const size_t rem = KEYBLOCKSIZE-length;
if (rem)
memset(k+length, 0x00, rem);
m_key.swap(k);
}
else
@ -232,7 +236,7 @@ template <class W, bool T_64bit>
BLAKE2_Base<W, T_64bit>::BLAKE2_Base(const byte *key, size_t keyLength, const byte* salt, size_t saltLength,
const byte* personalization, size_t personalizationLength, bool treeMode, unsigned int digestSize)
: m_block(ParameterBlock(digestSize, keyLength, salt, saltLength,
personalization, personalizationLength)), m_digestSize(digestSize), m_treeMode(false)
personalization, personalizationLength)), m_digestSize(digestSize), m_treeMode(treeMode)
{
this->ThrowIfInvalidKeyLength(keyLength);
this->ThrowIfInvalidTruncatedSize(digestSize);
@ -268,16 +272,13 @@ void BLAKE2_Base<W, T_64bit>::Restart(const BLAKE2_ParameterBlock<T_64bit>& bloc
m_state.t[1] = counter[1];
}
for(unsigned int i = 0; i < BLAKE2_IV<T_64bit>::IVSIZE; ++i)
m_state.h[i] ^= ReadWord<W, T_64bit>(m_block, i);
// When BLAKE2 is keyed, the input stream is simply {key||message}. Key it
// during Restart to avoid FirstPut and friends. Key size == 0 means no key.
if (m_key.size())
{
// Key is properly sized and padded
Update(m_key, m_key.size());
}
for(unsigned int i = 0; i < BLAKE2_IV<T_64bit>::IVSIZE; ++i)
m_state.h[i] ^= ReadWord<W, T_64bit>(m_block, i);
}
template <class W, bool T_64bit>
@ -539,6 +540,7 @@ static inline void BLAKE2_SSE2_Compress64(const byte* input, BLAKE2_State<word64
const word64 m14 = ((const word64*)input)[14];
const word64 m15 = ((const word64*)input)[15];
row1l = _mm_loadu_si128( (const __m128i *)(&state.h[0]) );
row1h = _mm_loadu_si128( (const __m128i *)(&state.h[2]) );
row2l = _mm_loadu_si128( (const __m128i *)(&state.h[4]) );
@ -548,6 +550,7 @@ static inline void BLAKE2_SSE2_Compress64(const byte* input, BLAKE2_State<word64
row4l = _mm_xor_si128( _mm_loadu_si128( (const __m128i *)(&BLAKE2_IV<true>::iv[4]) ), _mm_loadu_si128( (const __m128i *)(&state.t[0]) ) );
row4h = _mm_xor_si128( _mm_loadu_si128( (const __m128i *)(&BLAKE2_IV<true>::iv[6]) ), _mm_loadu_si128( (const __m128i *)(&state.f[0]) ) );
b0 = _mm_set_epi64x(m2, m0);
b1 = _mm_set_epi64x(m6, m4);
row1l = _mm_add_epi64(_mm_add_epi64(row1l, b0), row2l);
@ -564,7 +567,6 @@ static inline void BLAKE2_SSE2_Compress64(const byte* input, BLAKE2_State<word64
row2h = _mm_xor_si128(_mm_srli_epi64(row2h,24),_mm_slli_epi64(row2h, 40 ));
b0 = _mm_set_epi64x(m3, m1);
b1 = _mm_set_epi64x(m7, m5);
row1l = _mm_add_epi64(_mm_add_epi64(row1l, b0), row2l);

View File

@ -37,6 +37,11 @@ NAMESPACE_BEGIN(CryptoPP)
template <bool T_64bit>
struct CRYPTOPP_NO_VTABLE BLAKE2_Info : public VariableKeyLength<0,0,(T_64bit ? 64 : 32),1,SimpleKeyingInterface::NOT_RESYNCHRONIZABLE>
{
typedef VariableKeyLength<0,0,(T_64bit ? 64 : 32),1,SimpleKeyingInterface::NOT_RESYNCHRONIZABLE> KeyBase;
CRYPTOPP_CONSTANT(MIN_KEYLENGTH = KeyBase::MIN_KEYLENGTH);
CRYPTOPP_CONSTANT(MAX_KEYLENGTH = KeyBase::MAX_KEYLENGTH);
CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH = KeyBase::DEFAULT_KEYLENGTH);
CRYPTOPP_CONSTANT(BLOCKSIZE = (T_64bit ? 128 : 64))
CRYPTOPP_CONSTANT(DIGESTSIZE = (T_64bit ? 64 : 32))
CRYPTOPP_CONSTANT(SALTSIZE = (T_64bit ? 16 : 8))
@ -175,6 +180,10 @@ template <class W, bool T_64bit>
class BLAKE2_Base : public SimpleKeyingInterfaceImpl<MessageAuthenticationCode, BLAKE2_Info<T_64bit> >
{
public:
CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH = BLAKE2_Info<T_64bit>::DEFAULT_KEYLENGTH);
CRYPTOPP_CONSTANT(MIN_KEYLENGTH = BLAKE2_Info<T_64bit>::MIN_KEYLENGTH);
CRYPTOPP_CONSTANT(MAX_KEYLENGTH = BLAKE2_Info<T_64bit>::MAX_KEYLENGTH);
CRYPTOPP_CONSTANT(DIGESTSIZE = BLAKE2_Info<T_64bit>::DIGESTSIZE);
CRYPTOPP_CONSTANT(BLOCKSIZE = BLAKE2_Info<T_64bit>::BLOCKSIZE);
CRYPTOPP_CONSTANT(ALIGNSIZE = BLAKE2_Info<T_64bit>::ALIGNSIZE);
@ -254,14 +263,13 @@ private:
class BLAKE2b : public BLAKE2_Base<word64, true>
{
public:
CRYPTOPP_CONSTANT(DIGESTSIZE = BLAKE2_Info<true>::DIGESTSIZE);
typedef BLAKE2_Base<word64, true> BaseClass; // Early Visual Studio workaround
typedef BLAKE2_Base<word64, true> ThisBase; // Early Visual Studio workaround
typedef BLAKE2_ParameterBlock<true> ParameterBlock;
CRYPTOPP_COMPILE_ASSERT(sizeof(ParameterBlock) == 64);
//! \brief Construct a BLAKE2b hash
//! \param digestSize the digest size, in bytes
BLAKE2b(bool treeMode=false, unsigned int digestSize = DIGESTSIZE) : BaseClass(treeMode, digestSize) {}
BLAKE2b(bool treeMode=false, unsigned int digestSize = DIGESTSIZE) : ThisBase(treeMode, digestSize) {}
//! \brief Construct a BLAKE2b hash
//! \param key a byte array used to key the cipher
@ -275,7 +283,7 @@ public:
BLAKE2b(const byte *key, size_t keyLength, const byte* salt = NULL, size_t saltLength = 0,
const byte* personalization = NULL, size_t personalizationLength = 0,
bool treeMode=false, unsigned int digestSize = DIGESTSIZE)
: BaseClass(key, keyLength, salt, saltLength, personalization, personalizationLength, treeMode, digestSize) {}
: ThisBase(key, keyLength, salt, saltLength, personalization, personalizationLength, treeMode, digestSize) {}
};
//! \brief The BLAKE2s cryptographic hash function
@ -289,14 +297,13 @@ public:
class BLAKE2s : public BLAKE2_Base<word32, false>
{
public:
CRYPTOPP_CONSTANT(DIGESTSIZE = BLAKE2_Info<false>::DIGESTSIZE);
typedef BLAKE2_Base<word32, false> BaseClass; // Early Visual Studio workaround
typedef BLAKE2_Base<word32, false> ThisBase; // Early Visual Studio workaround
typedef BLAKE2_ParameterBlock<false> ParameterBlock;
CRYPTOPP_COMPILE_ASSERT(sizeof(ParameterBlock) == 32);
//! \brief Construct a BLAKE2b hash
//! \param digestSize the digest size, in bytes
BLAKE2s(bool treeMode=false, unsigned int digestSize = DIGESTSIZE) : BaseClass(treeMode, digestSize) {}
BLAKE2s(bool treeMode=false, unsigned int digestSize = DIGESTSIZE) : ThisBase(treeMode, digestSize) {}
//! \brief Construct a BLAKE2b hash
//! \param key a byte array used to key the cipher
@ -310,7 +317,7 @@ public:
BLAKE2s(const byte *key, size_t keyLength, const byte* salt = NULL, size_t saltLength = 0,
const byte* personalization = NULL, size_t personalizationLength = 0,
bool treeMode=false, unsigned int digestSize = DIGESTSIZE)
: BaseClass(key, keyLength, salt, saltLength, personalization, personalizationLength, treeMode, digestSize) {}
: ThisBase(key, keyLength, salt, saltLength, personalization, personalizationLength, treeMode, digestSize) {}
};
NAMESPACE_END