forked from keepassxreboot/keepassxc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add crypto classes and tests. Link to libgcrypt.
- Loading branch information
Showing
15 changed files
with
684 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, <[email protected]> | ||
# Copyright (c) 2008, Modestas Vainius, <[email protected]> | ||
# | ||
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (C) 2010 Felix Geyer <[email protected]> | ||
* | ||
* 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (C) 2010 Felix Geyer <[email protected]> | ||
* | ||
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright (C) 2010 Felix Geyer <[email protected]> | ||
* | ||
* 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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (C) 2010 Felix Geyer <[email protected]> | ||
* | ||
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2010 Felix Geyer <[email protected]> | ||
* | ||
* 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() | ||
{ | ||
} |
Oops, something went wrong.