SymmetricCipher: Add keySize(), don't rely on state for sizes

This additionally makes keySize() and blockSize() work before
setting the key and IV. Required for SSH agent decryption.
This commit is contained in:
Toni Spets 2017-10-29 17:07:56 +02:00
parent a81a5fa31b
commit 248ae9d4ba
5 changed files with 28 additions and 11 deletions

View File

@ -74,6 +74,11 @@ bool SymmetricCipher::reset()
return m_backend->reset();
}
int SymmetricCipher::keySize() const
{
return m_backend->keySize();
}
int SymmetricCipher::blockSize() const
{
return m_backend->blockSize();

View File

@ -70,6 +70,7 @@ public:
}
bool reset();
int keySize() const;
int blockSize() const;
QString errorString() const;

View File

@ -33,6 +33,7 @@ public:
Q_REQUIRED_RESULT virtual bool processInPlace(QByteArray& data, quint64 rounds) = 0;
virtual bool reset() = 0;
virtual int keySize() const = 0;
virtual int blockSize() const = 0;
virtual QString errorString() const = 0;

View File

@ -26,7 +26,6 @@ SymmetricCipherGcrypt::SymmetricCipherGcrypt(SymmetricCipher::Algorithm algo, Sy
, m_algo(gcryptAlgo(algo))
, m_mode(gcryptMode(mode))
, m_direction(direction)
, m_blockSize(-1)
{
}
@ -95,14 +94,6 @@ bool SymmetricCipherGcrypt::init()
return false;
}
size_t blockSizeT;
error = gcry_cipher_algo_info(m_algo, GCRYCTL_GET_BLKLEN, nullptr, &blockSizeT);
if (error != 0) {
setErrorString(error);
return false;
}
m_blockSize = blockSizeT;
return true;
}
@ -237,9 +228,28 @@ bool SymmetricCipherGcrypt::reset()
return true;
}
int SymmetricCipherGcrypt::keySize() const
{
gcry_error_t error;
size_t keySizeT;
error = gcry_cipher_algo_info(m_algo, GCRYCTL_GET_KEYLEN, nullptr, &keySizeT);
if (error != 0)
return -1;
return keySizeT;
}
int SymmetricCipherGcrypt::blockSize() const
{
return m_blockSize;
gcry_error_t error;
size_t blockSizeT;
error = gcry_cipher_algo_info(m_algo, GCRYCTL_GET_BLKLEN, nullptr, &blockSizeT);
if (error != 0)
return -1;
return blockSizeT;
}
QString SymmetricCipherGcrypt::errorString() const

View File

@ -39,6 +39,7 @@ public:
Q_REQUIRED_RESULT bool processInPlace(QByteArray& data, quint64 rounds);
bool reset();
int keySize() const;
int blockSize() const;
QString errorString() const;
@ -54,7 +55,6 @@ private:
const SymmetricCipher::Direction m_direction;
QByteArray m_key;
QByteArray m_iv;
int m_blockSize;
QString m_errorString;
};