Improve performance of the key transformation.

This commit is contained in:
Felix Geyer 2012-05-07 14:31:18 +02:00
parent 7649c781dc
commit d5fc1bf0b4
8 changed files with 50 additions and 17 deletions

View File

@ -49,16 +49,6 @@ SymmetricCipherBackend* SymmetricCipher::createBackend(SymmetricCipher::Algorith
}
}
QByteArray SymmetricCipher::process(const QByteArray& data)
{
return m_backend->process(data);
}
void SymmetricCipher::processInPlace(QByteArray& data)
{
m_backend->processInPlace(data);
}
void SymmetricCipher::reset()
{
m_backend->reset();

View File

@ -21,7 +21,7 @@
#include <QtCore/QByteArray>
#include <QtCore/QScopedPointer>
class SymmetricCipherBackend;
#include "crypto/SymmetricCipherBackend.h"
class SymmetricCipher
{
@ -49,8 +49,18 @@ public:
SymmetricCipher::Direction direction, const QByteArray& key, const QByteArray& iv);
~SymmetricCipher();
QByteArray process(const QByteArray& data);
void processInPlace(QByteArray& data);
inline QByteArray process(const QByteArray& data) {
return m_backend->process(data);
}
inline void processInPlace(QByteArray& data) {
m_backend->processInPlace(data);
}
inline void processInPlace(QByteArray& data, int rounds) {
Q_ASSERT(rounds > 0);
m_backend->processInPlace(data, rounds);
}
void reset();
int blockSize() const;

View File

@ -18,7 +18,7 @@
#ifndef KEEPASSX_SYMMETRICCIPHERBACKEND_H
#define KEEPASSX_SYMMETRICCIPHERBACKEND_H
#include "crypto/SymmetricCipher.h"
#include <QtCore/QByteArray>
class SymmetricCipherBackend
{
@ -30,6 +30,7 @@ public:
virtual QByteArray process(const QByteArray& data) = 0;
virtual void processInPlace(QByteArray& data) = 0;
virtual void processInPlace(QByteArray& data, int rounds) = 0;
virtual void reset() = 0;
virtual int blockSize() const = 0;

View File

@ -113,6 +113,26 @@ void SymmetricCipherGcrypt::processInPlace(QByteArray& data)
Q_ASSERT(error == 0);
}
void SymmetricCipherGcrypt::processInPlace(QByteArray& data, int rounds)
{
// TODO check block size
gcry_error_t error;
if (m_direction == SymmetricCipher::Decrypt) {
for (int i = 0; i != rounds; ++i) {
error = gcry_cipher_decrypt(m_ctx, data.data(), data.size(), 0, 0);
Q_ASSERT(error == 0);
}
}
else {
for (int i = 0; i != rounds; ++i) {
error = gcry_cipher_encrypt(m_ctx, data.data(), data.size(), 0, 0);
Q_ASSERT(error == 0);
}
}
}
void SymmetricCipherGcrypt::reset()
{
gcry_error_t error;

View File

@ -20,6 +20,7 @@
#include <gcrypt.h>
#include "crypto/SymmetricCipher.h"
#include "crypto/SymmetricCipherBackend.h"
class SymmetricCipherGcrypt : public SymmetricCipherBackend
@ -34,6 +35,7 @@ public:
QByteArray process(const QByteArray& data);
void processInPlace(QByteArray& data);
void processInPlace(QByteArray& data, int rounds);
void reset();
int blockSize() const;

View File

@ -74,6 +74,16 @@ void SymmetricCipherSalsa20::processInPlace(QByteArray& data)
reinterpret_cast<u8*>(data.data()), data.size());
}
void SymmetricCipherSalsa20::processInPlace(QByteArray& data, int rounds)
{
Q_ASSERT((data.size() < blockSize()) || ((data.size() % blockSize()) == 0));
for (int i = 0; i != rounds; ++i) {
ECRYPT_encrypt_bytes(&m_ctx, reinterpret_cast<const u8*>(data.constData()),
reinterpret_cast<u8*>(data.data()), data.size());
}
}
void SymmetricCipherSalsa20::reset()
{
ECRYPT_ivsetup(&m_ctx, reinterpret_cast<const u8*>(m_iv.constData()));

View File

@ -18,6 +18,7 @@
#ifndef KEEPASSX_SYMMETRICCIPHERSALSA20_H
#define KEEPASSX_SYMMETRICCIPHERSALSA20_H
#include "crypto/SymmetricCipher.h"
#include "crypto/SymmetricCipherBackend.h"
#include "crypto/salsa20/ecrypt-sync.h"
@ -36,6 +37,7 @@ public:
QByteArray process(const QByteArray& data);
void processInPlace(QByteArray& data);
void processInPlace(QByteArray& data, int rounds);
void reset();
int blockSize() const;

View File

@ -94,9 +94,7 @@ QByteArray CompositeKey::transformKeyRaw(const QByteArray& key, const QByteArray
QByteArray result = key;
for (int i = 0; i < rounds; i++) {
cipher.processInPlace(result);
}
cipher.processInPlace(result, rounds);
return result;
}