keepassxc

Форк
0
/
AesKdf.cpp 
108 строк · 2.8 Кб
1
/*
2
 *  Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
3
 *
4
 *  This program is free software: you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation, either version 2 or (at your option)
7
 *  version 3 of the License.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#include "AesKdf.h"
19

20
#include <QtConcurrent>
21

22
#include "crypto/CryptoHash.h"
23
#include "crypto/SymmetricCipher.h"
24
#include "format/KeePass2.h"
25

26
AesKdf::AesKdf()
27
    : Kdf::Kdf(KeePass2::KDF_AES_KDBX4)
28
{
29
}
30

31
/**
32
 * @param legacyKdbx3 initialize as legacy KDBX3 KDF
33
 */
34
AesKdf::AesKdf(bool legacyKdbx3)
35
    : Kdf::Kdf(legacyKdbx3 ? KeePass2::KDF_AES_KDBX3 : KeePass2::KDF_AES_KDBX4)
36
{
37
}
38

39
bool AesKdf::processParameters(const QVariantMap& p)
40
{
41
    bool ok;
42
    int rounds = p.value(KeePass2::KDFPARAM_AES_ROUNDS).toInt(&ok);
43
    if (!ok || !setRounds(rounds)) {
44
        return false;
45
    }
46

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

51
QVariantMap AesKdf::writeParameters()
52
{
53
    QVariantMap p;
54

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

58
    p.insert(KeePass2::KDFPARAM_AES_ROUNDS, static_cast<quint64>(rounds()));
59
    p.insert(KeePass2::KDFPARAM_AES_SEED, seed());
60
    return p;
61
}
62

63
bool AesKdf::transform(const QByteArray& raw, QByteArray& result) const
64
{
65
    return transformKeyRaw(raw, m_seed, m_rounds, &result);
66
}
67

68
bool AesKdf::transformKeyRaw(const QByteArray& key, const QByteArray& seed, int rounds, QByteArray* result)
69
{
70
    if (!result) {
71
        return false;
72
    }
73

74
    auto out = key;
75
    SymmetricCipher::aesKdf(seed, rounds, out);
76
    *result = CryptoHash::hash(out, CryptoHash::Sha256);
77
    return true;
78
}
79

80
QSharedPointer<Kdf> AesKdf::clone() const
81
{
82
    return QSharedPointer<AesKdf>::create(*this);
83
}
84

85
int AesKdf::benchmark(int msec) const
86
{
87
    QByteArray key(16, '\x7E');
88
    QByteArray seed(32, '\x4B');
89

90
    int trials = 3;
91
    int rounds = 1000000;
92

93
    QElapsedTimer timer;
94
    timer.start();
95
    for (int i = 0; i < trials; ++i) {
96
        QByteArray result;
97
        if (!transformKeyRaw(key, seed, rounds, &result)) {
98
            return rounds;
99
        }
100
    }
101

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

105
QString AesKdf::toString() const
106
{
107
    return QObject::tr("AES (%1 rounds)").arg(QString::number(rounds()));
108
}
109

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.