mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-02-22 15:39:57 -05:00
Support custom backends in Random.
Useful for tests.
This commit is contained in:
parent
e087baeb48
commit
9c788a6e84
@ -212,7 +212,7 @@ void Database::setKey(const CompositeKey& key, const QByteArray& transformSeed,
|
|||||||
|
|
||||||
void Database::setKey(const CompositeKey& key)
|
void Database::setKey(const CompositeKey& key)
|
||||||
{
|
{
|
||||||
setKey(key, Random::randomArray(32));
|
setKey(key, randomGen()->randomArray(32));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Database::hasKey() const
|
bool Database::hasKey() const
|
||||||
|
@ -40,20 +40,20 @@ QString PasswordGenerator::generatePassword(int length,
|
|||||||
|
|
||||||
if (flags & CharFromEveryGroup) {
|
if (flags & CharFromEveryGroup) {
|
||||||
for (int i = 0; i < groups.size(); i++) {
|
for (int i = 0; i < groups.size(); i++) {
|
||||||
int pos = Random::randomUInt(groups[i].size());
|
int pos = randomGen()->randomUInt(groups[i].size());
|
||||||
|
|
||||||
password.append(groups[i][pos]);
|
password.append(groups[i][pos]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = groups.size(); i < length; i++) {
|
for (int i = groups.size(); i < length; i++) {
|
||||||
int pos = Random::randomUInt(passwordChars.size());
|
int pos = randomGen()->randomUInt(passwordChars.size());
|
||||||
|
|
||||||
password.append(passwordChars[pos]);
|
password.append(passwordChars[pos]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// shuffle chars
|
// shuffle chars
|
||||||
for (int i = (password.size() - 1); i >= 1; i--) {
|
for (int i = (password.size() - 1); i >= 1; i--) {
|
||||||
int j = Random::randomUInt(i + 1);
|
int j = randomGen()->randomUInt(i + 1);
|
||||||
|
|
||||||
QChar tmp = password[i];
|
QChar tmp = password[i];
|
||||||
password[i] = password[j];
|
password[i] = password[j];
|
||||||
@ -62,7 +62,7 @@ QString PasswordGenerator::generatePassword(int length,
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
int pos = Random::randomUInt(passwordChars.size());
|
int pos = randomGen()->randomUInt(passwordChars.size());
|
||||||
|
|
||||||
password.append(passwordChars[pos]);
|
password.append(passwordChars[pos]);
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ Uuid::Uuid(const QByteArray& data)
|
|||||||
|
|
||||||
Uuid Uuid::random()
|
Uuid Uuid::random()
|
||||||
{
|
{
|
||||||
return Uuid(Random::randomArray(Length));
|
return Uuid(randomGen()->randomArray(Length));
|
||||||
}
|
}
|
||||||
|
|
||||||
QString Uuid::toBase64() const
|
QString Uuid::toBase64() const
|
||||||
|
@ -25,9 +25,17 @@
|
|||||||
#define QUINT32_MAX 4294967295U
|
#define QUINT32_MAX 4294967295U
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
class RandomBackendGcrypt : public RandomBackend
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void randomize(void* data, int len) Q_DECL_OVERRIDE;
|
||||||
|
};
|
||||||
|
|
||||||
|
Random* Random::m_instance(Q_NULLPTR);
|
||||||
|
|
||||||
void Random::randomize(QByteArray& ba)
|
void Random::randomize(QByteArray& ba)
|
||||||
{
|
{
|
||||||
randomize(ba.data(), ba.size());
|
m_backend->randomize(ba.data(), ba.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray Random::randomArray(int len)
|
QByteArray Random::randomArray(int len)
|
||||||
@ -51,7 +59,7 @@ quint32 Random::randomUInt(quint32 limit)
|
|||||||
// To avoid modulo bias:
|
// To avoid modulo bias:
|
||||||
// Make sure rand is below the largest number where rand%limit==0
|
// Make sure rand is below the largest number where rand%limit==0
|
||||||
do {
|
do {
|
||||||
randomize(&rand, 4);
|
m_backend->randomize(&rand, 4);
|
||||||
} while (rand > ceil);
|
} while (rand > ceil);
|
||||||
|
|
||||||
return (rand % limit);
|
return (rand % limit);
|
||||||
@ -62,13 +70,32 @@ quint32 Random::randomUIntRange(quint32 min, quint32 max)
|
|||||||
return min + randomUInt(max - min);
|
return min + randomUInt(max - min);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Random::randomize(void* data, int len)
|
Random* Random::instance()
|
||||||
|
{
|
||||||
|
if (!m_instance) {
|
||||||
|
m_instance = new Random(new RandomBackendGcrypt());
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Random::createWithBackend(RandomBackend* backend)
|
||||||
|
{
|
||||||
|
Q_ASSERT(backend);
|
||||||
|
Q_ASSERT(!m_instance);
|
||||||
|
|
||||||
|
m_instance = new Random(backend);
|
||||||
|
}
|
||||||
|
|
||||||
|
Random::Random(RandomBackend* backend)
|
||||||
|
: m_backend(backend)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void RandomBackendGcrypt::randomize(void* data, int len)
|
||||||
{
|
{
|
||||||
Q_ASSERT(Crypto::initalized());
|
Q_ASSERT(Crypto::initalized());
|
||||||
|
|
||||||
gcry_randomize(data, len, GCRY_STRONG_RANDOM);
|
gcry_randomize(data, len, GCRY_STRONG_RANDOM);
|
||||||
}
|
}
|
||||||
|
|
||||||
Random::Random()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
@ -19,26 +19,45 @@
|
|||||||
#define KEEPASSX_RANDOM_H
|
#define KEEPASSX_RANDOM_H
|
||||||
|
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
|
#include <QScopedPointer>
|
||||||
|
|
||||||
|
class RandomBackend
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void randomize(void* data, int len) = 0;
|
||||||
|
virtual ~RandomBackend() {}
|
||||||
|
};
|
||||||
|
|
||||||
class Random
|
class Random
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void randomize(QByteArray& ba);
|
void randomize(QByteArray& ba);
|
||||||
static QByteArray randomArray(int len);
|
QByteArray randomArray(int len);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a random quint32 in the range [0, @p limit)
|
* Generate a random quint32 in the range [0, @p limit)
|
||||||
*/
|
*/
|
||||||
static quint32 randomUInt(quint32 limit);
|
quint32 randomUInt(quint32 limit);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a random quint32 in the range [@p min, @p max)
|
* Generate a random quint32 in the range [@p min, @p max)
|
||||||
*/
|
*/
|
||||||
static quint32 randomUIntRange(quint32 min, quint32 max);
|
quint32 randomUIntRange(quint32 min, quint32 max);
|
||||||
|
|
||||||
|
static Random* instance();
|
||||||
|
static void createWithBackend(RandomBackend* backend);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void randomize(void* data, int len);
|
Random(RandomBackend* backend);
|
||||||
Random();
|
|
||||||
|
QScopedPointer<RandomBackend> m_backend;
|
||||||
|
static Random* m_instance;
|
||||||
|
|
||||||
|
Q_DISABLE_COPY(Random)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline Random* randomGen() {
|
||||||
|
return Random::instance();
|
||||||
|
}
|
||||||
|
|
||||||
#endif // KEEPASSX_RANDOM_H
|
#endif // KEEPASSX_RANDOM_H
|
||||||
|
@ -45,10 +45,10 @@ void KeePass2Writer::writeDatabase(QIODevice* device, Database* db)
|
|||||||
m_error = false;
|
m_error = false;
|
||||||
m_errorStr.clear();
|
m_errorStr.clear();
|
||||||
|
|
||||||
QByteArray masterSeed = Random::randomArray(32);
|
QByteArray masterSeed = randomGen()->randomArray(32);
|
||||||
QByteArray encryptionIV = Random::randomArray(16);
|
QByteArray encryptionIV = randomGen()->randomArray(16);
|
||||||
QByteArray protectedStreamKey = Random::randomArray(32);
|
QByteArray protectedStreamKey = randomGen()->randomArray(32);
|
||||||
QByteArray startBytes = Random::randomArray(32);
|
QByteArray startBytes = randomGen()->randomArray(32);
|
||||||
QByteArray endOfHeader = "\r\n\r\n";
|
QByteArray endOfHeader = "\r\n\r\n";
|
||||||
|
|
||||||
CryptoHash hash(CryptoHash::Sha256);
|
CryptoHash hash(CryptoHash::Sha256);
|
||||||
|
@ -121,7 +121,7 @@ void FileKey::create(QIODevice* device)
|
|||||||
|
|
||||||
xmlWriter.writeStartElement("Key");
|
xmlWriter.writeStartElement("Key");
|
||||||
|
|
||||||
QByteArray data = Random::randomArray(32);
|
QByteArray data = randomGen()->randomArray(32);
|
||||||
xmlWriter.writeTextElement("Data", QString::fromAscii(data.toBase64()));
|
xmlWriter.writeTextElement("Data", QString::fromAscii(data.toBase64()));
|
||||||
|
|
||||||
xmlWriter.writeEndElement();
|
xmlWriter.writeEndElement();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user