From 7790f2e7baa6231bf268bed515ce2c774bf98f93 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Mon, 7 May 2012 14:38:10 +0200 Subject: [PATCH] Add CompositeKey::transformKeyBenchmark(). This method tests how many key transformation rounds can be calculated within a specific time. --- src/CMakeLists.txt | 1 + src/keys/CompositeKey.cpp | 47 +++++++++++++++++++++++++++++++++++++++ src/keys/CompositeKey.h | 2 ++ src/keys/CompositeKey_p.h | 39 ++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 src/keys/CompositeKey_p.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d446d58da..680c8307b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -99,6 +99,7 @@ set(keepassx_MOC gui/GroupModel.h gui/GroupView.h gui/MainWindow.h + keys/CompositeKey_p.h streams/HashedBlockStream.h streams/LayeredStream.h streams/qtiocompressor.h diff --git a/src/keys/CompositeKey.cpp b/src/keys/CompositeKey.cpp index 0c7c43537..0dbcacce9 100644 --- a/src/keys/CompositeKey.cpp +++ b/src/keys/CompositeKey.cpp @@ -16,8 +16,10 @@ */ #include "CompositeKey.h" +#include "CompositeKey_p.h" #include +#include #include "crypto/CryptoHash.h" #include "crypto/SymmetricCipher.h" @@ -103,3 +105,48 @@ void CompositeKey::addKey(const Key& key) { m_keys.append(key.clone()); } + +int CompositeKey::transformKeyBenchmark(int msec) +{ + TransformKeyBenchmarkThread thread1(msec); + TransformKeyBenchmarkThread thread2(msec); + + thread1.start(); + thread2.start(); + + thread1.wait(); + thread2.wait(); + + return qMin(thread1.rounds(), thread2.rounds()); +} + + +TransformKeyBenchmarkThread::TransformKeyBenchmarkThread(int msec) + : m_msec(msec) + , m_rounds(0) +{ + Q_ASSERT(msec > 0); +} + +int TransformKeyBenchmarkThread::rounds() +{ + return m_rounds; +} + +void TransformKeyBenchmarkThread::run() +{ + QByteArray key = QByteArray('\x7E', 32); + QByteArray seed = QByteArray('\x4B', 32); + QByteArray iv(16, 0); + + SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Ecb, + SymmetricCipher::Encrypt, seed, iv); + + QTime t; + t.start(); + + do { + cipher.processInPlace(key, 100); + m_rounds += 100; + } while (t.elapsed() < m_msec); +} diff --git a/src/keys/CompositeKey.h b/src/keys/CompositeKey.h index 3fa438681..e95d521fd 100644 --- a/src/keys/CompositeKey.h +++ b/src/keys/CompositeKey.h @@ -36,6 +36,8 @@ public: QByteArray transform(const QByteArray& seed, int rounds) const; void addKey(const Key& key); + static int transformKeyBenchmark(int msec); + private: static QByteArray transformKeyRaw(const QByteArray& key, const QByteArray& seed, int rounds); diff --git a/src/keys/CompositeKey_p.h b/src/keys/CompositeKey_p.h new file mode 100644 index 000000000..c704f086b --- /dev/null +++ b/src/keys/CompositeKey_p.h @@ -0,0 +1,39 @@ +/* +* Copyright (C) 2012 Felix Geyer +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 2 or (at your option) +* version 3 of the License. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +#ifndef KEEPASSX_COMPOSITEKEY_P_H +#define KEEPASSX_COMPOSITEKEY_P_H + +#include + +class TransformKeyBenchmarkThread : public QThread +{ + Q_OBJECT + +public: + explicit TransformKeyBenchmarkThread(int msec); + int rounds(); + +protected: + void run(); + +private: + int m_msec; + int m_rounds; +}; + +#endif // KEEPASSX_COMPOSITEKEY_P_H