44#include < argon2.h>
55#include < sodium.h>
66#include < openssl/evp.h>
7+ #include < openssl/rand.h>
78
89QByteArray EncryptionEngine::readKeyfile (const QString& keyfilePath) {
910 if (keyfilePath.isEmpty ()) {
@@ -18,41 +19,22 @@ QByteArray EncryptionEngine::readKeyfile(const QString& keyfilePath) {
1819}
1920
2021QByteArray EncryptionEngine::deriveKey (const QString& password, const QByteArray& salt, const QStringList& keyfilePaths, const QString& kdf, int iterations) {
21- QByteArray key (EVP_MAX_KEY_LENGTH, 0 );
2222 QByteArray passwordWithKeyfile = password.toUtf8 ();
2323
2424 for (const QString &keyfilePath : keyfilePaths) {
2525 passwordWithKeyfile.append (readKeyfile (keyfilePath));
2626 }
2727
28- if (kdf == " PBKDF2" ) {
29- if (!PKCS5_PBKDF2_HMAC (passwordWithKeyfile.data (), passwordWithKeyfile.size (), reinterpret_cast <const unsigned char *>(salt.data ()), salt.size (), iterations, EVP_sha256 (), key.size (), reinterpret_cast <unsigned char *>(key.data ()))) {
30- qDebug () << " PBKDF2 key derivation failed" ;
31- return QByteArray ();
32- }
33- } else if (kdf == " Argon2" ) {
34- if (argon2i_hash_raw (iterations, 1 << 16 , 1 , passwordWithKeyfile.data (), passwordWithKeyfile.size (), reinterpret_cast <const unsigned char *>(salt.data ()), salt.size (), reinterpret_cast <unsigned char *>(key.data ()), key.size ()) != ARGON2_OK) {
35- qDebug () << " Argon2 key derivation failed" ;
36- return QByteArray ();
37- }
38- } else if (kdf == " Scrypt" ) {
39- unsigned long long opslimit = iterations;
40- if (crypto_pwhash_scryptsalsa208sha256 (reinterpret_cast <unsigned char *>(key.data ()), static_cast <unsigned long long >(key.size ()),
41- passwordWithKeyfile.constData (), static_cast <unsigned long long >(passwordWithKeyfile.size ()),
42- reinterpret_cast <const unsigned char *>(salt.data ()),
43- opslimit,
44- crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE) != 0 ) {
45- qDebug () << " Scrypt key derivation failed" ;
46- return QByteArray ();
47- }
48- }
49-
50- return key;
28+ return performKeyDerivation (passwordWithKeyfile, salt, kdf, iterations, EVP_MAX_KEY_LENGTH);
5129}
5230
5331QByteArray EncryptionEngine::deriveKey (const QString &password, const QString &salt, const QString &kdf, int iterations, int keySize) {
54- QByteArray key (keySize, 0 );
5532 QByteArray passwordWithKeyfile = password.toUtf8 ();
33+ return performKeyDerivation (passwordWithKeyfile, salt.toUtf8 (), kdf, iterations, keySize);
34+ }
35+
36+ QByteArray EncryptionEngine::performKeyDerivation (const QByteArray& passwordWithKeyfile, const QByteArray& salt, const QString& kdf, int iterations, int keySize) {
37+ QByteArray key (keySize, 0 );
5638
5739 if (kdf == " PBKDF2" ) {
5840 if (!PKCS5_PBKDF2_HMAC (passwordWithKeyfile.data (), passwordWithKeyfile.size (), reinterpret_cast <const unsigned char *>(salt.data ()), salt.size (), iterations, EVP_sha256 (), key.size (), reinterpret_cast <unsigned char *>(key.data ()))) {
@@ -74,6 +56,9 @@ QByteArray EncryptionEngine::deriveKey(const QString &password, const QString &s
7456 qDebug () << " Scrypt key derivation failed" ;
7557 return QByteArray ();
7658 }
59+ } else {
60+ qDebug () << " Unknown KDF" ;
61+ return QByteArray ();
7762 }
7863
7964 return key;
0 commit comments