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

AesKdf.cpp « kdf « crypto « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0b2130cfe7a7bb963de2df142cca47741d18ebcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 *  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
 *  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 "AesKdf.h"

#include <QtConcurrent>

#include "crypto/CryptoHash.h"
#include "format/KeePass2.h"

AesKdf::AesKdf()
    : Kdf::Kdf(KeePass2::KDF_AES_KDBX4)
{
}

/**
 * @param legacyKdbx3 initialize as legacy KDBX3 KDF
 */
AesKdf::AesKdf(bool legacyKdbx3)
    : Kdf::Kdf(legacyKdbx3 ? KeePass2::KDF_AES_KDBX3 : KeePass2::KDF_AES_KDBX4)
{
}

bool AesKdf::processParameters(const QVariantMap& p)
{
    bool ok;
    int rounds = p.value(KeePass2::KDFPARAM_AES_ROUNDS).toInt(&ok);
    if (!ok || !setRounds(rounds)) {
        return false;
    }

    QByteArray seed = p.value(KeePass2::KDFPARAM_AES_SEED).toByteArray();
    return setSeed(seed);
}

QVariantMap AesKdf::writeParameters()
{
    QVariantMap p;

    // always write old KDBX3 AES-KDF UUID for compatibility with other applications
    p.insert(KeePass2::KDFPARAM_UUID, KeePass2::KDF_AES_KDBX3.toRfc4122());

    p.insert(KeePass2::KDFPARAM_AES_ROUNDS, static_cast<quint64>(rounds()));
    p.insert(KeePass2::KDFPARAM_AES_SEED, seed());
    return p;
}

bool AesKdf::transform(const QByteArray& raw, QByteArray& result) const
{
    QByteArray resultLeft;
    QByteArray resultRight;

    QFuture<bool> future = QtConcurrent::run(transformKeyRaw, raw.left(16), m_seed, m_rounds, &resultLeft);

    bool rightResult = transformKeyRaw(raw.right(16), m_seed, m_rounds, &resultRight);
    bool leftResult = future.result();

    if (!rightResult || !leftResult) {
        return false;
    }

    QByteArray transformed;
    transformed.append(resultLeft);
    transformed.append(resultRight);

    result = CryptoHash::hash(transformed, CryptoHash::Sha256);
    return true;
}

bool AesKdf::transformKeyRaw(const QByteArray& key, const QByteArray& seed, int rounds, QByteArray* result)
{
    QByteArray iv(16, 0);
    SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Ecb, SymmetricCipher::Encrypt);
    if (!cipher.init(seed, iv)) {
        qWarning("AesKdf::transformKeyRaw: error in SymmetricCipher::init: %s", cipher.errorString().toUtf8().data());
        return false;
    }

    *result = key;

    if (!cipher.processInPlace(*result, rounds)) {
        qWarning("AesKdf::transformKeyRaw: error in SymmetricCipher::processInPlace: %s",
                 cipher.errorString().toUtf8().data());
        return false;
    }

    return true;
}

QSharedPointer<Kdf> AesKdf::clone() const
{
    return QSharedPointer<AesKdf>::create(*this);
}

int AesKdf::benchmarkImpl(int msec) const
{
    QByteArray key = QByteArray(16, '\x7E');
    QByteArray seed = QByteArray(32, '\x4B');
    QByteArray iv(16, 0);

    SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Ecb, SymmetricCipher::Encrypt);
    cipher.init(seed, iv);

    quint64 rounds = 1000000;
    QElapsedTimer timer;
    timer.start();

    if (!cipher.processInPlace(key, rounds)) {
        return -1;
    }

    return static_cast<int>(rounds * (static_cast<float>(msec) / timer.elapsed()));
}