Sync with Upstream master

pull/461/head
Jeffrey Walton 2017-08-12 19:24:00 -04:00
commit 5e6e6c4eaa
2 changed files with 19 additions and 6 deletions

19
gcm.cpp
View File

@ -113,7 +113,13 @@ void GCM_Base::SetKeyWithoutResync(const byte *userKey, size_t keylength, const
BlockCipher &blockCipher = AccessBlockCipher();
blockCipher.SetKey(userKey, keylength, params);
if (blockCipher.BlockSize() != REQUIRED_BLOCKSIZE)
// GCM is only defined for 16-byte block ciphers at the moment.
// However, variable blocksize support means we have to defer
// blocksize checks to runtime after the key is set. Also see
// https://github.com/weidai11/cryptopp/issues/408.
const unsigned int blockSize = blockCipher.BlockSize();
CRYPTOPP_ASSERT(blockSize == REQUIRED_BLOCKSIZE);
if (blockSize != REQUIRED_BLOCKSIZE)
throw InvalidArgument(AlgorithmName() + ": block size of underlying block cipher is not 16");
int tableSize, i, j, k;
@ -150,7 +156,7 @@ void GCM_Base::SetKeyWithoutResync(const byte *userKey, size_t keylength, const
m_buffer.resize(3*REQUIRED_BLOCKSIZE + tableSize);
byte *mulTable = MulTable();
byte *hashKey = HashKey();
memset(hashKey, 0, REQUIRED_BLOCKSIZE);
memset(hashKey, 0, blockSize);
blockCipher.ProcessBlock(hashKey);
#if CRYPTOPP_CLMUL_AVAILABLE
@ -287,6 +293,13 @@ void GCM_Base::Resync(const byte *iv, size_t len)
BlockCipher &cipher = AccessBlockCipher();
byte *hashBuffer = HashBuffer();
// GCM is only defined for 16-byte block ciphers at the moment.
// However, variable blocksize support means we have to defer
// blocksize checks to runtime after the key is set. Also see
// https://github.com/weidai11/cryptopp/issues/408.
const unsigned int blockSize = cipher.BlockSize();
CRYPTOPP_ASSERT(blockSize == REQUIRED_BLOCKSIZE);
if (len == 12)
{
memcpy(hashBuffer, iv, len);
@ -318,7 +331,7 @@ void GCM_Base::Resync(const byte *iv, size_t len)
}
if (m_state >= State_IVSet)
m_ctr.Resynchronize(hashBuffer, REQUIRED_BLOCKSIZE);
m_ctr.Resynchronize(hashBuffer, blockSize);
else
m_ctr.SetCipherWithIV(cipher, hashBuffer);

6
gcm.h
View File

@ -74,9 +74,9 @@ protected:
virtual GCM_TablesOption GetTablesOption() const =0;
const BlockCipher & GetBlockCipher() const {return const_cast<GCM_Base *>(this)->AccessBlockCipher();};
byte *HashBuffer() {return m_buffer+REQUIRED_BLOCKSIZE;}
byte *HashKey() {return m_buffer+2*REQUIRED_BLOCKSIZE;}
byte *MulTable() {return m_buffer+3*REQUIRED_BLOCKSIZE;}
byte *HashBuffer() {return m_buffer+GetBlockCipher().BlockSize();}
byte *HashKey() {return m_buffer+2*GetBlockCipher().BlockSize();}
byte *MulTable() {return m_buffer+3*GetBlockCipher().BlockSize();}
inline void ReverseHashBufferIfNeeded();
class CRYPTOPP_DLL GCTR : public CTR_Mode_ExternalCipher::Encryption