From bb6ae3a014636b1ae686c47ccb09b15d4805bee3 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Mon, 13 Sep 2010 23:18:31 +0200 Subject: [PATCH] Add reset() and blockSize(). --- src/crypto/CryptoHash.cpp | 3 ++- src/crypto/SymmetricCipher.cpp | 31 +++++++++++++++++++++++++++++-- src/crypto/SymmetricCipher.h | 4 ++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/crypto/CryptoHash.cpp b/src/crypto/CryptoHash.cpp index 9421e7f1e..e216dbddf 100644 --- a/src/crypto/CryptoHash.cpp +++ b/src/crypto/CryptoHash.cpp @@ -43,7 +43,8 @@ CryptoHash::CryptoHash(CryptoHash::Algorithm algo) break; } - gcry_md_open(&d->ctx, algoGcrypt, 0); // TODO error handling + gcry_error_t error = gcry_md_open(&d->ctx, algoGcrypt, 0); + Q_ASSERT(error == 0); // TODO error handling d->hashLen = gcry_md_get_algo_dlen(algoGcrypt); } diff --git a/src/crypto/SymmetricCipher.cpp b/src/crypto/SymmetricCipher.cpp index bce17b475..90b77ce05 100644 --- a/src/crypto/SymmetricCipher.cpp +++ b/src/crypto/SymmetricCipher.cpp @@ -25,6 +25,8 @@ public: gcry_cipher_hd_t ctx; SymmetricCipher::Direction direction; QByteArray key; + QByteArray iv; + int blockSize; }; SymmetricCipher::SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode, @@ -35,6 +37,7 @@ SymmetricCipher::SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCiphe d->direction = direction; d->key = key; + d->iv = iv; int algoGcrypt; @@ -68,10 +71,15 @@ SymmetricCipher::SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCiphe error = gcry_cipher_open(&d->ctx, algoGcrypt, modeGcrypt, 0); Q_ASSERT(error == 0); // TODO real error checking - error = gcry_cipher_setkey(d->ctx, d->key.constData(), d->key.size()); // TODO is key copied to gcrypt data structure? + error = gcry_cipher_setkey(d->ctx, d->key.constData(), d->key.size()); Q_ASSERT(error == 0); - error = gcry_cipher_setiv(d->ctx, iv.constData(), iv.size()); + error = gcry_cipher_setiv(d->ctx, d->iv.constData(), d->iv.size()); Q_ASSERT(error == 0); + + size_t blockSizeT; + error = gcry_cipher_algo_info(algoGcrypt, GCRYCTL_GET_BLKLEN, 0, &blockSizeT); + Q_ASSERT(error == 0); + d->blockSize = blockSizeT; } SymmetricCipher::~SymmetricCipher() @@ -123,3 +131,22 @@ void SymmetricCipher::processInPlace(QByteArray& data) Q_ASSERT(error == 0); } + +void SymmetricCipher::reset() +{ + Q_D(SymmetricCipher); + + gcry_error_t error; + + error = gcry_cipher_reset(d->ctx); + Q_ASSERT(error == 0); + error = gcry_cipher_setiv(d->ctx, d->iv.constData(), d->iv.size()); + Q_ASSERT(error == 0); +} + +int SymmetricCipher::blockSize() const +{ + Q_D(const SymmetricCipher); + + return d->blockSize; +} diff --git a/src/crypto/SymmetricCipher.h b/src/crypto/SymmetricCipher.h index 43dde2478..6c1ca7205 100644 --- a/src/crypto/SymmetricCipher.h +++ b/src/crypto/SymmetricCipher.h @@ -45,9 +45,13 @@ public: SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode, SymmetricCipher::Direction direction, const QByteArray& key, const QByteArray& iv); ~SymmetricCipher(); + QByteArray process(const QByteArray& data); void processInPlace(QByteArray& data); + void reset(); + int blockSize() const; + private: SymmetricCipherPrivate* const d_ptr; Q_DECLARE_PRIVATE(SymmetricCipher);