diff --git a/blake2.cpp b/blake2.cpp index 69b5d068..52db3403 100644 --- a/blake2.cpp +++ b/blake2.cpp @@ -203,7 +203,11 @@ void BLAKE2_Base::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 BLAKE2_Base::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::Restart(const BLAKE2_ParameterBlock& bloc m_state.t[1] = counter[1]; } + for(unsigned int i = 0; i < BLAKE2_IV::IVSIZE; ++i) + m_state.h[i] ^= ReadWord(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::IVSIZE; ++i) - m_state.h[i] ^= ReadWord(m_block, i); } template @@ -539,6 +540,7 @@ static inline void BLAKE2_SSE2_Compress64(const byte* input, BLAKE2_State::iv[4]) ), _mm_loadu_si128( (const __m128i *)(&state.t[0]) ) ); row4h = _mm_xor_si128( _mm_loadu_si128( (const __m128i *)(&BLAKE2_IV::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 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 BLAKE2_Base : public SimpleKeyingInterfaceImpl > { public: + CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH = BLAKE2_Info::DEFAULT_KEYLENGTH); + CRYPTOPP_CONSTANT(MIN_KEYLENGTH = BLAKE2_Info::MIN_KEYLENGTH); + CRYPTOPP_CONSTANT(MAX_KEYLENGTH = BLAKE2_Info::MAX_KEYLENGTH); + CRYPTOPP_CONSTANT(DIGESTSIZE = BLAKE2_Info::DIGESTSIZE); CRYPTOPP_CONSTANT(BLOCKSIZE = BLAKE2_Info::BLOCKSIZE); CRYPTOPP_CONSTANT(ALIGNSIZE = BLAKE2_Info::ALIGNSIZE); @@ -254,14 +263,13 @@ private: class BLAKE2b : public BLAKE2_Base { public: - CRYPTOPP_CONSTANT(DIGESTSIZE = BLAKE2_Info::DIGESTSIZE); - typedef BLAKE2_Base BaseClass; // Early Visual Studio workaround + typedef BLAKE2_Base ThisBase; // Early Visual Studio workaround typedef BLAKE2_ParameterBlock 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 { public: - CRYPTOPP_CONSTANT(DIGESTSIZE = BLAKE2_Info::DIGESTSIZE); - typedef BLAKE2_Base BaseClass; // Early Visual Studio workaround + typedef BLAKE2_Base ThisBase; // Early Visual Studio workaround typedef BLAKE2_ParameterBlock 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