Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Geyer <debfx@fobos.de>2010-09-11 21:49:30 +0400
committerFelix Geyer <debfx@fobos.de>2010-09-11 21:49:30 +0400
commit6a2034fa248385d1d1637d4c3444045e50e0c5f5 (patch)
tree7526ece1e918ea4daa1de072bab37e8d99489980
parentf5dd24fdbecb94f07398bacbe8fd905b5c83f806 (diff)
Add crypto classes and tests. Link to libgcrypt.
-rw-r--r--CMakeLists.txt4
-rw-r--r--LICENSE.BSD23
-rw-r--r--cmake/FindLibgcrypt.cmake30
-rw-r--r--src/CMakeLists.txt6
-rw-r--r--src/crypto/Crypto.cpp80
-rw-r--r--src/crypto/Crypto.h32
-rw-r--r--src/crypto/CryptoHash.cpp88
-rw-r--r--src/crypto/CryptoHash.h46
-rw-r--r--src/crypto/Random.cpp39
-rw-r--r--src/crypto/Random.h33
-rw-r--r--src/crypto/SymmetricCipher.cpp125
-rw-r--r--src/crypto/SymmetricCipher.h56
-rw-r--r--tests/CMakeLists.txt16
-rw-r--r--tests/TestCryptoHash.cpp59
-rw-r--r--tests/TestSymmetricCipher.cpp52
15 files changed, 684 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1d5331fec..d158b360d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,6 +17,8 @@ project(KeePassX)
cmake_minimum_required(VERSION 2.6.0)
+set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake )
+
option(WITH_TESTS "Enable building of unit tests" ON)
set( KEEPASSX_VERSION "0.9.0" )
@@ -41,6 +43,8 @@ include(${QT_USE_FILE})
find_package(Automoc4 REQUIRED)
+find_package(Libgcrypt REQUIRED)
+
add_subdirectory(src)
if( WITH_TESTS )
add_subdirectory(tests)
diff --git a/LICENSE.BSD b/LICENSE.BSD
new file mode 100644
index 000000000..8c786d336
--- /dev/null
+++ b/LICENSE.BSD
@@ -0,0 +1,23 @@
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. The name of the author may not be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
diff --git a/cmake/FindLibgcrypt.cmake b/cmake/FindLibgcrypt.cmake
new file mode 100644
index 000000000..c12da3707
--- /dev/null
+++ b/cmake/FindLibgcrypt.cmake
@@ -0,0 +1,30 @@
+# - Try to find the GNU Libgcrypt library
+# Once done this will define
+#
+# LIBGCRYPT_FOUND - system has the Libgcrypt library
+# LIBGCRYPT_LIBS - The libraries needed to use Libgcrypt
+
+# Copyright (c) 2006, Pino Toscano, <toscano.pino@tiscali.it>
+# Copyright (c) 2008, Modestas Vainius, <modestas@vainius.eu>
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+# For details see the accompanying LICENSE.BSD file.
+
+include(CheckIncludeFiles)
+
+check_include_files(gcrypt.h HAVE_GCRYPT_H)
+
+if (HAVE_GCRYPT_H)
+ set(LIBGCRYPT_HEADERS_FOUND TRUE)
+endif (HAVE_GCRYPT_H)
+
+if (LIBGCRYPT_HEADERS_FOUND)
+ find_library(LIBGCRYPT_LIBS NAMES gcrypt )
+endif (LIBGCRYPT_HEADERS_FOUND)
+
+if (LIBGCRYPT_LIBS)
+ set(LIBGCRYPT_FOUND TRUE)
+ message(STATUS "Libgcrypt found: ${LIBGCRYPT_LIBS}")
+elseif (Libgcrypt_FIND_REQUIRED)
+ message(FATAL_ERROR "Could not find Libgcrypt")
+endif (LIBGCRYPT_LIBS)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 606799fc7..0a1c0ba91 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -24,6 +24,10 @@ set(keepassx_SOURCES
core/Metadata.cpp
core/TimeInfo.cpp
core/Uuid.cpp
+ crypto/Crypto.cpp
+ crypto/CryptoHash.cpp
+ crypto/Random.cpp
+ crypto/SymmetricCipher.cpp
format/KeePass2XmlReader.cpp
format/KeePass2XmlWriter.cpp
gui/DatabaseWidget.cpp
@@ -36,4 +40,4 @@ set(keepassx_SOURCES
automoc4_add_library( keepassx_core STATIC ${keepassx_SOURCES} )
automoc4_add_executable( ${PROGNAME} WIN32 MACOSX_BUNDLE main.cpp )
-target_link_libraries( ${PROGNAME} keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} )
+target_link_libraries( ${PROGNAME} keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${LIBGCRYPT_LIBS} )
diff --git a/src/crypto/Crypto.cpp b/src/crypto/Crypto.cpp
new file mode 100644
index 000000000..8bba447c9
--- /dev/null
+++ b/src/crypto/Crypto.cpp
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "Crypto.h"
+
+#include <QtCore/QMutex>
+
+#include <gcrypt.h>
+
+bool Crypto::m_initiated(false);
+
+int gcry_qt_mutex_init(void** p_sys)
+{
+ *p_sys = new QMutex();
+ return 0;
+}
+
+int gcry_qt_mutex_destroy(void** p_sys)
+{
+ delete reinterpret_cast<QMutex*>(*p_sys);
+ return 0;
+}
+
+int gcry_qt_mutex_lock(void** p_sys)
+{
+ reinterpret_cast<QMutex*>(*p_sys)->lock();
+ return 0;
+}
+
+int gcry_qt_mutex_unlock(void** p_sys)
+{
+ reinterpret_cast<QMutex*>(*p_sys)->unlock();
+ return 0;
+}
+
+static const struct gcry_thread_cbs gcry_threads_qt =
+{
+ GCRY_THREAD_OPTION_USER,
+ NULL,
+ gcry_qt_mutex_init,
+ gcry_qt_mutex_destroy,
+ gcry_qt_mutex_lock,
+ gcry_qt_mutex_unlock
+};
+
+Crypto::Crypto()
+{
+}
+
+void Crypto::init()
+{
+ if (m_initiated) {
+ return;
+ }
+
+ gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_qt);
+ gcry_check_version(0);
+ gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
+
+ m_initiated = true;
+}
+
+bool Crypto::selfTest()
+{
+ return (gcry_control(GCRYCTL_SELFTEST) == 0);
+}
diff --git a/src/crypto/Crypto.h b/src/crypto/Crypto.h
new file mode 100644
index 000000000..fb2b0e75a
--- /dev/null
+++ b/src/crypto/Crypto.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef KEEPASSX_CRYPTO_H
+#define KEEPASSX_CRYPTO_H
+
+class Crypto
+{
+public:
+ static void init();
+ static bool selfTest();
+
+private:
+ Crypto();
+ static bool m_initiated;
+};
+
+#endif // KEEPASSX_CRYPTO_H
diff --git a/src/crypto/CryptoHash.cpp b/src/crypto/CryptoHash.cpp
new file mode 100644
index 000000000..9421e7f1e
--- /dev/null
+++ b/src/crypto/CryptoHash.cpp
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "CryptoHash.h"
+
+#include "gcrypt.h"
+
+class CryptoHashPrivate
+{
+public:
+ gcry_md_hd_t ctx;
+ int hashLen;
+};
+
+CryptoHash::CryptoHash(CryptoHash::Algorithm algo)
+ : d_ptr(new CryptoHashPrivate())
+{
+ Q_D(CryptoHash);
+
+ int algoGcrypt;
+
+ switch (algo) {
+ case CryptoHash::Sha256:
+ algoGcrypt = GCRY_MD_SHA256;
+ break;
+
+ default:
+ Q_ASSERT(false);
+ break;
+ }
+
+ gcry_md_open(&d->ctx, algoGcrypt, 0); // TODO error handling
+
+ d->hashLen = gcry_md_get_algo_dlen(algoGcrypt);
+}
+
+CryptoHash::~CryptoHash()
+{
+ Q_D(CryptoHash);
+
+ gcry_md_close(d->ctx);
+
+ delete d_ptr;
+}
+
+void CryptoHash::addData(const QByteArray& data)
+{
+ Q_D(CryptoHash);
+
+ gcry_md_write(d->ctx, data.constData(), data.size());
+}
+
+void CryptoHash::reset()
+{
+ Q_D(CryptoHash);
+
+ gcry_md_reset(d->ctx);
+}
+
+QByteArray CryptoHash::result() const
+{
+ Q_D(const CryptoHash);
+
+ const char* result = reinterpret_cast<const char*>(gcry_md_read(d->ctx, 0));
+ return QByteArray(result, d->hashLen);
+}
+
+QByteArray CryptoHash::hash(const QByteArray& data, CryptoHash::Algorithm algo)
+{
+ // replace with gcry_md_hash_buffer()?
+ CryptoHash cryptoHash(algo);
+ cryptoHash.addData(data);
+ return cryptoHash.result();
+}
diff --git a/src/crypto/CryptoHash.h b/src/crypto/CryptoHash.h
new file mode 100644
index 000000000..eafd7375f
--- /dev/null
+++ b/src/crypto/CryptoHash.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef KEEPASSX_CRYPTOHASH_H
+#define KEEPASSX_CRYPTOHASH_H
+
+#include <QtCore/QByteArray>
+
+class CryptoHashPrivate;
+
+class CryptoHash
+{
+public:
+ enum Algorithm
+ {
+ Sha256
+ };
+
+ CryptoHash(CryptoHash::Algorithm algo);
+ ~CryptoHash();
+ void addData(const QByteArray& data);
+ void reset();
+ QByteArray result() const;
+
+ static QByteArray hash(const QByteArray& data, CryptoHash::Algorithm algo);
+
+private:
+ CryptoHashPrivate* const d_ptr;
+ Q_DECLARE_PRIVATE(CryptoHash);
+};
+
+#endif // KEEPASSX_CRYPTOHASH_H
diff --git a/src/crypto/Random.cpp b/src/crypto/Random.cpp
new file mode 100644
index 000000000..0d6d77168
--- /dev/null
+++ b/src/crypto/Random.cpp
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "Random.h"
+
+#include <gcrypt.h>
+
+void Random::randomize(QByteArray& ba)
+{
+ gcry_randomize(ba.data(), ba.size(), GCRY_STRONG_RANDOM);
+}
+
+QByteArray Random::randomArray(int len)
+{
+ QByteArray ba;
+ ba.resize(len);
+
+ randomize(ba);
+
+ return ba;
+}
+
+Random::Random()
+{
+}
diff --git a/src/crypto/Random.h b/src/crypto/Random.h
new file mode 100644
index 000000000..a93f5a3c7
--- /dev/null
+++ b/src/crypto/Random.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef KEEPASSX_RANDOM_H
+#define KEEPASSX_RANDOM_H
+
+#include <QtCore/QByteArray>
+
+class Random
+{
+public:
+ static void randomize(QByteArray& ba);
+ static QByteArray randomArray(int len);
+
+private:
+ Random();
+};
+
+#endif // KEEPASSX_RANDOM_H
diff --git a/src/crypto/SymmetricCipher.cpp b/src/crypto/SymmetricCipher.cpp
new file mode 100644
index 000000000..bce17b475
--- /dev/null
+++ b/src/crypto/SymmetricCipher.cpp
@@ -0,0 +1,125 @@
+/*
+* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
+*
+* 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 <http://www.gnu.org/licenses/>.
+*/
+
+#include "SymmetricCipher.h"
+
+#include <gcrypt.h>
+
+class SymmetricCipherPrivate
+{
+public:
+ gcry_cipher_hd_t ctx;
+ SymmetricCipher::Direction direction;
+ QByteArray key;
+};
+
+SymmetricCipher::SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
+ SymmetricCipher::Direction direction, const QByteArray& key, const QByteArray& iv)
+ : d_ptr(new SymmetricCipherPrivate())
+{
+ Q_D(SymmetricCipher);
+
+ d->direction = direction;
+ d->key = key;
+
+ int algoGcrypt;
+
+ switch (algo) {
+ case SymmetricCipher::Aes256:
+ algoGcrypt = GCRY_CIPHER_AES256;
+ break;
+
+ default:
+ Q_ASSERT(false);
+ break;
+ }
+
+ int modeGcrypt;
+
+ switch (mode) {
+ case SymmetricCipher::Ecb:
+ modeGcrypt = GCRY_CIPHER_MODE_ECB;
+ break;
+
+ case SymmetricCipher::Cbc:
+ modeGcrypt = GCRY_CIPHER_MODE_CBC;
+ break;
+
+ default:
+ Q_ASSERT(false);
+ break;
+ }
+
+ gcry_error_t error;
+
+ 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?
+ Q_ASSERT(error == 0);
+ error = gcry_cipher_setiv(d->ctx, iv.constData(), iv.size());
+ Q_ASSERT(error == 0);
+}
+
+SymmetricCipher::~SymmetricCipher()
+{
+ Q_D(SymmetricCipher);
+
+ gcry_cipher_close(d->ctx);
+
+ delete d_ptr;
+}
+
+QByteArray SymmetricCipher::process(const QByteArray& data)
+{
+ Q_D(SymmetricCipher);
+
+ // TODO check block size
+
+ QByteArray result;
+ result.resize(data.size());
+
+ gcry_error_t error;
+
+ if (d->direction == SymmetricCipher::Decrypt) {
+ error = gcry_cipher_decrypt(d->ctx, result.data(), data.size(), data.constData(), data.size());
+ }
+ else {
+ error = gcry_cipher_encrypt(d->ctx, result.data(), data.size(), data.constData(), data.size());
+ }
+
+ Q_ASSERT(error == 0);
+
+ return result;
+}
+
+void SymmetricCipher::processInPlace(QByteArray& data)
+{
+ Q_D(SymmetricCipher);
+
+ // TODO check block size
+
+ gcry_error_t error;
+
+ if (d->direction == SymmetricCipher::Decrypt) {
+ error = gcry_cipher_decrypt(d->ctx, data.data(), data.size(), 0, 0);
+ }
+ else {
+ error = gcry_cipher_encrypt(d->ctx, data.data(), data.size(), 0, 0);
+ }
+
+ Q_ASSERT(error == 0);
+}
diff --git a/src/crypto/SymmetricCipher.h b/src/crypto/SymmetricCipher.h
new file mode 100644
index 000000000..43dde2478
--- /dev/null
+++ b/src/crypto/SymmetricCipher.h
@@ -0,0 +1,56 @@
+/*
+* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
+*
+* 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 <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef KEEPASSX_SYMMETRICCIPHER_H
+#define KEEPASSX_SYMMETRICCIPHER_H
+
+#include <QtCore/QByteArray>
+
+class SymmetricCipherPrivate;
+
+class SymmetricCipher
+{
+public:
+ enum Algorithm
+ {
+ Aes256
+ };
+
+ enum Mode
+ {
+ Cbc,
+ Ecb
+ };
+
+ enum Direction
+ {
+ Decrypt,
+ Encrypt
+ };
+
+ 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);
+
+private:
+ SymmetricCipherPrivate* const d_ptr;
+ Q_DECLARE_PRIVATE(SymmetricCipher);
+};
+
+#endif // KEEPASSX_SYMMETRICCIPHER_H
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 55523bdaa..04fe54b4d 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -63,14 +63,22 @@ macro (ADD_UNIT_TEST _test_NAME)
endmacro (ADD_UNIT_TEST)
+# TODO automocify?
+
add_unit_test( testgroup TestGroup.cpp )
-target_link_libraries( testgroup keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} )
+target_link_libraries( testgroup keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBGCRYPT_LIBS} )
add_unit_test( testkeepass2xmlreader TestKeePass2XmlReader.cpp )
-target_link_libraries( testkeepass2xmlreader keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} )
+target_link_libraries( testkeepass2xmlreader keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBGCRYPT_LIBS} )
add_unit_test( testgroupmodel TestGroupModel.cpp modeltest.cpp )
-target_link_libraries( testgroupmodel keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} )
+target_link_libraries( testgroupmodel keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBGCRYPT_LIBS} )
add_unit_test( testentrymodel TestEntryModel.cpp modeltest.cpp )
-target_link_libraries( testentrymodel keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} )
+target_link_libraries( testentrymodel keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBGCRYPT_LIBS} )
+
+add_unit_test( testcryptohash TestCryptoHash.cpp )
+target_link_libraries( testcryptohash keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBGCRYPT_LIBS} )
+
+add_unit_test( testsymmetriccipher TestSymmetricCipher.cpp )
+target_link_libraries( testsymmetriccipher keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBGCRYPT_LIBS} )
diff --git a/tests/TestCryptoHash.cpp b/tests/TestCryptoHash.cpp
new file mode 100644
index 000000000..e81d82313
--- /dev/null
+++ b/tests/TestCryptoHash.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <QtTest/QTest>
+
+#include "crypto/Crypto.h"
+#include "crypto/CryptoHash.h"
+
+class TestCryptoHash : public QObject
+{
+ Q_OBJECT
+
+private Q_SLOTS:
+ void initTestCase();
+ void test();
+};
+
+void TestCryptoHash::initTestCase()
+{
+ Crypto::init();
+}
+
+void TestCryptoHash::test()
+{
+ // TODO move somewhere else
+ QVERIFY(Crypto::selfTest());
+
+ CryptoHash cryptoHash1(CryptoHash::Sha256);
+ QCOMPARE(QString(cryptoHash1.result().toHex()),
+ QString("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
+
+ QByteArray source2 = QString("KeePassX").toAscii();
+ QString result2 = CryptoHash::hash(source2, CryptoHash::Sha256).toHex();
+ QCOMPARE(result2, QString("0b56e5f65263e747af4a833bd7dd7ad26a64d7a4de7c68e52364893dca0766b4"));
+
+ CryptoHash cryptoHash3(CryptoHash::Sha256);
+ cryptoHash3.addData(QString("KeePa").toAscii());
+ cryptoHash3.addData(QString("ssX").toAscii());
+ QCOMPARE(QString(cryptoHash3.result().toHex()),
+ QString("0b56e5f65263e747af4a833bd7dd7ad26a64d7a4de7c68e52364893dca0766b4"));
+}
+
+QTEST_MAIN(TestCryptoHash);
+
+#include "TestCryptoHash.moc"
diff --git a/tests/TestSymmetricCipher.cpp b/tests/TestSymmetricCipher.cpp
new file mode 100644
index 000000000..ed45c9dbb
--- /dev/null
+++ b/tests/TestSymmetricCipher.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <QtTest/QTest>
+
+#include "crypto/Crypto.h"
+#include "crypto/SymmetricCipher.h"
+
+class TestSymmetricCipher : public QObject
+{
+ Q_OBJECT
+
+private Q_SLOTS:
+ void initTestCase();
+ void testAes256CbcEncryption();
+};
+
+void TestSymmetricCipher::initTestCase()
+{
+ Crypto::init();
+}
+
+void TestSymmetricCipher::testAes256CbcEncryption()
+{
+ // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
+
+ QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
+ QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f");
+ QByteArray plain = QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
+
+ SymmetricCipher encrypt(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Encrypt, key, iv);
+ QCOMPARE(QString(encrypt.process(plain).toHex()),
+ QString("f58c4c04d6e5f1ba779eabfb5f7bfbd6"));
+}
+
+QTEST_MAIN(TestSymmetricCipher);
+
+#include "TestSymmetricCipher.moc"