Reformat code, fix minor style issues, make kdf() getter const
This commit is contained in:
committed by
Jonathan White
parent
d1a19a1009
commit
0d6ca0945b
@@ -28,10 +28,7 @@ public:
|
||||
int hashLen;
|
||||
};
|
||||
|
||||
CryptoHash::CryptoHash(CryptoHash::Algorithm algo)
|
||||
: CryptoHash::CryptoHash(algo, false) {}
|
||||
|
||||
CryptoHash::CryptoHash(CryptoHash::Algorithm algo, bool hmac)
|
||||
CryptoHash::CryptoHash(Algorithm algo, bool hmac)
|
||||
: d_ptr(new CryptoHashPrivate())
|
||||
{
|
||||
Q_D(CryptoHash);
|
||||
@@ -86,14 +83,14 @@ void CryptoHash::addData(const QByteArray& data)
|
||||
return;
|
||||
}
|
||||
|
||||
gcry_md_write(d->ctx, data.constData(), data.size());
|
||||
gcry_md_write(d->ctx, data.constData(), static_cast<size_t>(data.size()));
|
||||
}
|
||||
|
||||
void CryptoHash::setKey(const QByteArray& data)
|
||||
{
|
||||
Q_D(CryptoHash);
|
||||
|
||||
gcry_error_t error = gcry_md_setkey(d->ctx, data.constData(), data.size());
|
||||
gcry_error_t error = gcry_md_setkey(d->ctx, data.constData(), static_cast<size_t>(data.size()));
|
||||
if (error) {
|
||||
qWarning("Gcrypt error (setKey): %s", gcry_strerror(error));
|
||||
qWarning("Gcrypt error (setKey): %s", gcry_strsource(error));
|
||||
@@ -112,11 +109,11 @@ QByteArray CryptoHash::result() const
|
||||
{
|
||||
Q_D(const CryptoHash);
|
||||
|
||||
const char* result = reinterpret_cast<const char*>(gcry_md_read(d->ctx, 0));
|
||||
const auto* 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)
|
||||
QByteArray CryptoHash::hash(const QByteArray& data, Algorithm algo)
|
||||
{
|
||||
// replace with gcry_md_hash_buffer()?
|
||||
CryptoHash cryptoHash(algo);
|
||||
@@ -124,7 +121,7 @@ QByteArray CryptoHash::hash(const QByteArray& data, CryptoHash::Algorithm algo)
|
||||
return cryptoHash.result();
|
||||
}
|
||||
|
||||
QByteArray CryptoHash::hmac(const QByteArray& data, const QByteArray& key, CryptoHash::Algorithm algo)
|
||||
QByteArray CryptoHash::hmac(const QByteArray& data, const QByteArray& key, Algorithm algo)
|
||||
{
|
||||
// replace with gcry_md_hash_buffer()?
|
||||
CryptoHash cryptoHash(algo, true);
|
||||
|
||||
@@ -31,15 +31,14 @@ public:
|
||||
Sha512
|
||||
};
|
||||
|
||||
explicit CryptoHash(CryptoHash::Algorithm algo);
|
||||
explicit CryptoHash(CryptoHash::Algorithm algo, bool hmac);
|
||||
explicit CryptoHash(Algorithm algo, bool hmac = false);
|
||||
~CryptoHash();
|
||||
void addData(const QByteArray& data);
|
||||
void reset();
|
||||
QByteArray result() const;
|
||||
void setKey(const QByteArray& data);
|
||||
|
||||
static QByteArray hash(const QByteArray& data, CryptoHash::Algorithm algo);
|
||||
static QByteArray hash(const QByteArray& data, Algorithm algo);
|
||||
static QByteArray hmac(const QByteArray& data, const QByteArray& key, Algorithm algo);
|
||||
|
||||
private:
|
||||
|
||||
@@ -20,8 +20,7 @@
|
||||
#include "config-keepassx.h"
|
||||
#include "crypto/SymmetricCipherGcrypt.h"
|
||||
|
||||
SymmetricCipher::SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
|
||||
SymmetricCipher::Direction direction)
|
||||
SymmetricCipher::SymmetricCipher(Algorithm algo, Mode mode, Direction direction)
|
||||
: m_backend(createBackend(algo, mode, direction))
|
||||
, m_initialized(false)
|
||||
, m_algo(algo)
|
||||
@@ -55,14 +54,13 @@ bool SymmetricCipher::isInitalized() const
|
||||
return m_initialized;
|
||||
}
|
||||
|
||||
SymmetricCipherBackend* SymmetricCipher::createBackend(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
|
||||
SymmetricCipher::Direction direction)
|
||||
SymmetricCipherBackend* SymmetricCipher::createBackend(Algorithm algo, Mode mode, Direction direction)
|
||||
{
|
||||
switch (algo) {
|
||||
case SymmetricCipher::Aes256:
|
||||
case SymmetricCipher::Twofish:
|
||||
case SymmetricCipher::Salsa20:
|
||||
case SymmetricCipher::ChaCha20:
|
||||
case Aes256:
|
||||
case Twofish:
|
||||
case Salsa20:
|
||||
case ChaCha20:
|
||||
return new SymmetricCipherGcrypt(algo, mode, direction);
|
||||
|
||||
default:
|
||||
@@ -94,25 +92,25 @@ QString SymmetricCipher::errorString() const
|
||||
SymmetricCipher::Algorithm SymmetricCipher::cipherToAlgorithm(Uuid cipher)
|
||||
{
|
||||
if (cipher == KeePass2::CIPHER_AES) {
|
||||
return SymmetricCipher::Aes256;
|
||||
return Aes256;
|
||||
} else if (cipher == KeePass2::CIPHER_CHACHA20) {
|
||||
return SymmetricCipher::ChaCha20;
|
||||
return ChaCha20;
|
||||
} else if (cipher == KeePass2::CIPHER_TWOFISH) {
|
||||
return SymmetricCipher::Twofish;
|
||||
return Twofish;
|
||||
}
|
||||
|
||||
qWarning("SymmetricCipher::cipherToAlgorithm: invalid Uuid %s", cipher.toByteArray().toHex().data());
|
||||
return InvalidAlgorithm;
|
||||
}
|
||||
|
||||
Uuid SymmetricCipher::algorithmToCipher(SymmetricCipher::Algorithm algo)
|
||||
Uuid SymmetricCipher::algorithmToCipher(Algorithm algo)
|
||||
{
|
||||
switch (algo) {
|
||||
case SymmetricCipher::Aes256:
|
||||
case Aes256:
|
||||
return KeePass2::CIPHER_AES;
|
||||
case SymmetricCipher::ChaCha20:
|
||||
case ChaCha20:
|
||||
return KeePass2::CIPHER_CHACHA20;
|
||||
case SymmetricCipher::Twofish:
|
||||
case Twofish:
|
||||
return KeePass2::CIPHER_TWOFISH;
|
||||
default:
|
||||
qWarning("SymmetricCipher::algorithmToCipher: invalid algorithm %d", algo);
|
||||
@@ -120,12 +118,14 @@ Uuid SymmetricCipher::algorithmToCipher(SymmetricCipher::Algorithm algo)
|
||||
}
|
||||
}
|
||||
|
||||
int SymmetricCipher::algorithmIvSize(SymmetricCipher::Algorithm algo) {
|
||||
int SymmetricCipher::algorithmIvSize(Algorithm algo)
|
||||
{
|
||||
switch (algo) {
|
||||
case SymmetricCipher::ChaCha20:
|
||||
case ChaCha20:
|
||||
return 12;
|
||||
case SymmetricCipher::Aes256:
|
||||
case SymmetricCipher::Twofish:
|
||||
case Aes256:
|
||||
return 16;
|
||||
case Twofish:
|
||||
return 16;
|
||||
default:
|
||||
qWarning("SymmetricCipher::algorithmIvSize: invalid algorithm %d", algo);
|
||||
@@ -133,19 +133,21 @@ int SymmetricCipher::algorithmIvSize(SymmetricCipher::Algorithm algo) {
|
||||
}
|
||||
}
|
||||
|
||||
SymmetricCipher::Mode SymmetricCipher::algorithmMode(SymmetricCipher::Algorithm algo) {
|
||||
SymmetricCipher::Mode SymmetricCipher::algorithmMode(Algorithm algo)
|
||||
{
|
||||
switch (algo) {
|
||||
case SymmetricCipher::ChaCha20:
|
||||
return SymmetricCipher::Stream;
|
||||
case SymmetricCipher::Aes256:
|
||||
case SymmetricCipher::Twofish:
|
||||
return SymmetricCipher::Cbc;
|
||||
case ChaCha20:
|
||||
return Stream;
|
||||
case Aes256:
|
||||
case Twofish:
|
||||
return Cbc;
|
||||
default:
|
||||
qWarning("SymmetricCipher::algorithmMode: invalid algorithm %d", algo);
|
||||
return SymmetricCipher::InvalidMode;
|
||||
return InvalidMode;
|
||||
}
|
||||
}
|
||||
|
||||
SymmetricCipher::Algorithm SymmetricCipher::algorithm() const {
|
||||
SymmetricCipher::Algorithm SymmetricCipher::algorithm() const
|
||||
{
|
||||
return m_algo;
|
||||
}
|
||||
|
||||
@@ -53,22 +53,25 @@ public:
|
||||
Encrypt
|
||||
};
|
||||
|
||||
SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
|
||||
SymmetricCipher::Direction direction);
|
||||
SymmetricCipher(Algorithm algo, Mode mode, Direction direction);
|
||||
~SymmetricCipher();
|
||||
Q_DISABLE_COPY(SymmetricCipher)
|
||||
|
||||
bool init(const QByteArray& key, const QByteArray& iv);
|
||||
bool isInitalized() const;
|
||||
|
||||
inline QByteArray process(const QByteArray& data, bool* ok) {
|
||||
inline QByteArray process(const QByteArray& data, bool* ok)
|
||||
{
|
||||
return m_backend->process(data, ok);
|
||||
}
|
||||
|
||||
Q_REQUIRED_RESULT inline bool processInPlace(QByteArray& data) {
|
||||
Q_REQUIRED_RESULT inline bool processInPlace(QByteArray& data)
|
||||
{
|
||||
return m_backend->processInPlace(data);
|
||||
}
|
||||
|
||||
Q_REQUIRED_RESULT inline bool processInPlace(QByteArray& data, quint64 rounds) {
|
||||
Q_REQUIRED_RESULT inline bool processInPlace(QByteArray& data, quint64 rounds)
|
||||
{
|
||||
Q_ASSERT(rounds > 0);
|
||||
return m_backend->processInPlace(data, rounds);
|
||||
}
|
||||
@@ -85,14 +88,11 @@ public:
|
||||
static Mode algorithmMode(Algorithm algo);
|
||||
|
||||
private:
|
||||
static SymmetricCipherBackend* createBackend(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
|
||||
SymmetricCipher::Direction direction);
|
||||
static SymmetricCipherBackend* createBackend(Algorithm algo, Mode mode, Direction direction);
|
||||
|
||||
const QScopedPointer<SymmetricCipherBackend> m_backend;
|
||||
bool m_initialized;
|
||||
Algorithm m_algo;
|
||||
|
||||
Q_DISABLE_COPY(SymmetricCipher)
|
||||
};
|
||||
|
||||
#endif // KEEPASSX_SYMMETRICCIPHER_H
|
||||
|
||||
@@ -145,8 +145,7 @@ QByteArray SymmetricCipherGcrypt::process(const QByteArray& data, bool* ok)
|
||||
|
||||
if (m_direction == SymmetricCipher::Decrypt) {
|
||||
error = gcry_cipher_decrypt(m_ctx, result.data(), data.size(), data.constData(), data.size());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
error = gcry_cipher_encrypt(m_ctx, result.data(), data.size(), data.constData(), data.size());
|
||||
}
|
||||
|
||||
@@ -154,7 +153,7 @@ QByteArray SymmetricCipherGcrypt::process(const QByteArray& data, bool* ok)
|
||||
setErrorString(error);
|
||||
*ok = false;
|
||||
} else {
|
||||
*ok = true;
|
||||
*ok = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -168,8 +167,7 @@ bool SymmetricCipherGcrypt::processInPlace(QByteArray& data)
|
||||
|
||||
if (m_direction == SymmetricCipher::Decrypt) {
|
||||
error = gcry_cipher_decrypt(m_ctx, data.data(), data.size(), nullptr, 0);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
error = gcry_cipher_encrypt(m_ctx, data.data(), data.size(), nullptr, 0);
|
||||
}
|
||||
|
||||
@@ -199,8 +197,7 @@ bool SymmetricCipherGcrypt::processInPlace(QByteArray& data, quint64 rounds)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
for (quint64 i = 0; i != rounds; ++i) {
|
||||
error = gcry_cipher_encrypt(m_ctx, rawData, size, nullptr, 0);
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
#include "crypto/SymmetricCipher.h"
|
||||
#include "crypto/SymmetricCipherBackend.h"
|
||||
|
||||
class SymmetricCipherGcrypt : public SymmetricCipherBackend
|
||||
class SymmetricCipherGcrypt: public SymmetricCipherBackend
|
||||
{
|
||||
public:
|
||||
SymmetricCipherGcrypt(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2017 KeePassXC Team
|
||||
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* 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
|
||||
@@ -28,7 +28,7 @@ const QList<Kdf::Field> AesKdf::FIELDS = AesKdf::initFields();
|
||||
QList<Kdf::Field> AesKdf::initFields()
|
||||
{
|
||||
return QList<Kdf::Field> {
|
||||
Kdf::Field(static_cast<quint32>(Fields::ROUNDS), "Transform rounds", 1, UINT64_MAX, true),
|
||||
Kdf::Field(static_cast<quint32>(Fields::ROUNDS), "Transform rounds", 1, UINT64_MAX, true),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -67,7 +67,8 @@ bool AesKdf::transformKeyRaw(const QByteArray& key, const QByteArray& seed, quin
|
||||
*result = key;
|
||||
|
||||
if (!cipher.processInPlace(*result, rounds)) {
|
||||
qWarning("AesKdf::transformKeyRaw: error in SymmetricCipher::processInPlace: %s", cipher.errorString().toUtf8().data());
|
||||
qWarning("AesKdf::transformKeyRaw: error in SymmetricCipher::processInPlace: %s",
|
||||
cipher.errorString().toUtf8().data());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -129,20 +130,20 @@ const QList<Kdf::Field> AesKdf::fields() const
|
||||
quint64 AesKdf::field(quint32 id) const
|
||||
{
|
||||
switch (static_cast<Fields>(id)) {
|
||||
case Fields::ROUNDS:
|
||||
return m_rounds;
|
||||
default:
|
||||
return 0;
|
||||
case Fields::ROUNDS:
|
||||
return m_rounds;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool AesKdf::setField(quint32 id, quint64 val)
|
||||
{
|
||||
switch (static_cast<Fields>(id)) {
|
||||
case Fields::ROUNDS:
|
||||
return setRounds(val);
|
||||
default:
|
||||
return false;
|
||||
case Fields::ROUNDS:
|
||||
return setRounds(val);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,7 +166,8 @@ int AesKdf::benchmarkImpl(int msec) const
|
||||
break;
|
||||
}
|
||||
rounds += 10000;
|
||||
} while (!t.hasExpired(msec));
|
||||
}
|
||||
while (!t.hasExpired(msec));
|
||||
|
||||
return rounds;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2017 KeePassXC Team
|
||||
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* 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
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
#include "Kdf.h"
|
||||
|
||||
class AesKdf : public Kdf
|
||||
class AesKdf: public Kdf
|
||||
{
|
||||
public:
|
||||
AesKdf();
|
||||
@@ -40,7 +40,7 @@ public:
|
||||
bool setRounds(quint64 rounds);
|
||||
bool setSeed(const QByteArray& seed);
|
||||
|
||||
enum class Fields : quint32
|
||||
enum class Fields: quint32
|
||||
{
|
||||
ROUNDS,
|
||||
SEED
|
||||
@@ -55,7 +55,10 @@ private:
|
||||
quint64 m_rounds;
|
||||
QByteArray m_seed;
|
||||
|
||||
static bool transformKeyRaw(const QByteArray& key, const QByteArray& seed, quint64 rounds, QByteArray* result) Q_REQUIRED_RESULT;
|
||||
static bool transformKeyRaw(const QByteArray& key,
|
||||
const QByteArray& seed,
|
||||
quint64 rounds,
|
||||
QByteArray* result) Q_REQUIRED_RESULT;
|
||||
static QList<Kdf::Field> initFields();
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2017 KeePassXC Team
|
||||
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* 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
|
||||
@@ -23,11 +23,11 @@
|
||||
#include <QtConcurrent>
|
||||
|
||||
Kdf::Field::Field(quint32 id, const QString& name, quint64 min, quint64 max, bool benchmark)
|
||||
: m_id(id)
|
||||
, m_name(name)
|
||||
, m_min(min)
|
||||
, m_max(max)
|
||||
, m_benchmark(benchmark)
|
||||
: m_id(id)
|
||||
, m_name(name)
|
||||
, m_min(min)
|
||||
, m_max(max)
|
||||
, m_benchmark(benchmark)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -71,8 +71,7 @@ int Kdf::benchmark(int msec) const
|
||||
}
|
||||
|
||||
Kdf::BenchmarkThread::BenchmarkThread(int msec, const Kdf* kdf)
|
||||
: m_msec(msec)
|
||||
, m_kdf(kdf)
|
||||
: m_msec(msec), m_kdf(kdf)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -81,6 +80,7 @@ int Kdf::BenchmarkThread::rounds()
|
||||
return m_rounds;
|
||||
}
|
||||
|
||||
void Kdf::BenchmarkThread::run() {
|
||||
void Kdf::BenchmarkThread::run()
|
||||
{
|
||||
m_rounds = m_kdf->benchmarkImpl(m_msec);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2017 KeePassXC Team
|
||||
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* 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
|
||||
@@ -25,11 +25,13 @@
|
||||
class Kdf
|
||||
{
|
||||
public:
|
||||
enum class Type {
|
||||
enum class Type
|
||||
{
|
||||
AES
|
||||
};
|
||||
|
||||
class Field {
|
||||
class Field
|
||||
{
|
||||
public:
|
||||
Field(quint32 id, const QString& name, quint64 min, quint64 max, bool benchmark = false);
|
||||
|
||||
@@ -48,9 +50,11 @@ public:
|
||||
};
|
||||
|
||||
virtual ~Kdf() {}
|
||||
|
||||
virtual QByteArray seed() const = 0;
|
||||
virtual Type type() const = 0;
|
||||
virtual bool transform(const QByteArray& raw, QByteArray& result) const = 0;
|
||||
virtual void randomizeTransformSalt() = 0;
|
||||
virtual Type type() const = 0;
|
||||
virtual Kdf* clone() const = 0;
|
||||
|
||||
virtual const QList<Field> fields() const = 0;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2017 KeePassXC Team
|
||||
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* 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
|
||||
@@ -22,9 +22,9 @@
|
||||
#ifndef KEEPASSXC_KDF_P_H
|
||||
#define KEEPASSXC_KDF_P_H
|
||||
|
||||
class Kdf::BenchmarkThread : public QThread
|
||||
class Kdf::BenchmarkThread: public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit BenchmarkThread(int msec, const Kdf* kdf);
|
||||
|
||||
Reference in New Issue
Block a user