Catch and handle all errors from libgcrypt.

This commit is contained in:
Felix Geyer 2015-05-09 19:47:53 +02:00
parent a7f4e2d0cd
commit a762cef0a9
29 changed files with 622 additions and 194 deletions

View file

@ -22,22 +22,12 @@
SymmetricCipherGcrypt::SymmetricCipherGcrypt(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
SymmetricCipher::Direction direction)
: m_algo(gcryptAlgo(algo))
: m_ctx(Q_NULLPTR)
, m_algo(gcryptAlgo(algo))
, m_mode(gcryptMode(mode))
, m_direction(direction)
, m_blockSize(-1)
{
Q_ASSERT(Crypto::initalized());
gcry_error_t error;
error = gcry_cipher_open(&m_ctx, m_algo, m_mode, 0);
Q_ASSERT(error == 0); // TODO: real error checking
size_t blockSizeT;
error = gcry_cipher_algo_info(m_algo, GCRYCTL_GET_BLKLEN, Q_NULLPTR, &blockSizeT);
Q_ASSERT(error == 0);
m_blockSize = blockSizeT;
}
SymmetricCipherGcrypt::~SymmetricCipherGcrypt()
@ -83,21 +73,65 @@ int SymmetricCipherGcrypt::gcryptMode(SymmetricCipher::Mode mode)
}
}
void SymmetricCipherGcrypt::setKey(const QByteArray& key)
void SymmetricCipherGcrypt::setErrorString(gcry_error_t err)
{
const char* gcryptError = gcry_strerror(err);
const char* gcryptErrorSource = gcry_strsource(err);
m_errorString = QString("%1/%2").arg(QString::fromLocal8Bit(gcryptErrorSource),
QString::fromLocal8Bit(gcryptError));
}
bool SymmetricCipherGcrypt::init()
{
Q_ASSERT(Crypto::initalized());
gcry_error_t error;
error = gcry_cipher_open(&m_ctx, m_algo, m_mode, 0);
if (error != 0) {
setErrorString(error);
return false;
}
size_t blockSizeT;
error = gcry_cipher_algo_info(m_algo, GCRYCTL_GET_BLKLEN, Q_NULLPTR, &blockSizeT);
if (error != 0) {
setErrorString(error);
return false;
}
m_blockSize = blockSizeT;
return true;
}
bool SymmetricCipherGcrypt::setKey(const QByteArray& key)
{
m_key = key;
gcry_error_t error = gcry_cipher_setkey(m_ctx, m_key.constData(), m_key.size());
Q_ASSERT(error == 0);
if (error != 0) {
setErrorString(error);
return false;
}
return true;
}
void SymmetricCipherGcrypt::setIv(const QByteArray& iv)
bool SymmetricCipherGcrypt::setIv(const QByteArray& iv)
{
m_iv = iv;
gcry_error_t error = gcry_cipher_setiv(m_ctx, m_iv.constData(), m_iv.size());
Q_ASSERT(error == 0);
if (error != 0) {
setErrorString(error);
return false;
}
return true;
}
QByteArray SymmetricCipherGcrypt::process(const QByteArray& data)
QByteArray SymmetricCipherGcrypt::process(const QByteArray& data, bool* ok)
{
// TODO: check block size
@ -113,12 +147,16 @@ QByteArray SymmetricCipherGcrypt::process(const QByteArray& data)
error = gcry_cipher_encrypt(m_ctx, result.data(), data.size(), data.constData(), data.size());
}
Q_ASSERT(error == 0);
if (error != 0) {
setErrorString(error);
*ok = false;
}
*ok = true;
return result;
}
void SymmetricCipherGcrypt::processInPlace(QByteArray& data)
bool SymmetricCipherGcrypt::processInPlace(QByteArray& data)
{
// TODO: check block size
@ -131,10 +169,15 @@ void SymmetricCipherGcrypt::processInPlace(QByteArray& data)
error = gcry_cipher_encrypt(m_ctx, data.data(), data.size(), Q_NULLPTR, 0);
}
Q_ASSERT(error == 0);
if (error != 0) {
setErrorString(error);
return false;
}
return true;
}
void SymmetricCipherGcrypt::processInPlace(QByteArray& data, quint64 rounds)
bool SymmetricCipherGcrypt::processInPlace(QByteArray& data, quint64 rounds)
{
// TODO: check block size
@ -146,28 +189,52 @@ void SymmetricCipherGcrypt::processInPlace(QByteArray& data, quint64 rounds)
if (m_direction == SymmetricCipher::Decrypt) {
for (quint64 i = 0; i != rounds; ++i) {
error = gcry_cipher_decrypt(m_ctx, rawData, size, Q_NULLPTR, 0);
Q_ASSERT(error == 0);
if (error != 0) {
setErrorString(error);
return false;
}
}
}
else {
for (quint64 i = 0; i != rounds; ++i) {
error = gcry_cipher_encrypt(m_ctx, rawData, size, Q_NULLPTR, 0);
Q_ASSERT(error == 0);
if (error != 0) {
setErrorString(error);
return false;
}
}
}
return true;
}
void SymmetricCipherGcrypt::reset()
bool SymmetricCipherGcrypt::reset()
{
gcry_error_t error;
error = gcry_cipher_reset(m_ctx);
Q_ASSERT(error == 0);
if (error != 0) {
setErrorString(error);
return false;
}
error = gcry_cipher_setiv(m_ctx, m_iv.constData(), m_iv.size());
Q_ASSERT(error == 0);
if (error != 0) {
setErrorString(error);
return false;
}
return true;
}
int SymmetricCipherGcrypt::blockSize() const
{
return m_blockSize;
}
QString SymmetricCipherGcrypt::errorString() const
{
return m_errorString;
}