keepassxc

Форк
0
/
TestKeePass2RandomStream.cpp 
89 строк · 3.2 Кб
1
/*
2
 *  Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
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 "TestKeePass2RandomStream.h"
19

20
#include "crypto/Crypto.h"
21
#include "crypto/CryptoHash.h"
22
#include "crypto/SymmetricCipher.h"
23
#include "format/KeePass2.h"
24
#include "format/KeePass2RandomStream.h"
25

26
#include <QTest>
27

28
QTEST_GUILESS_MAIN(TestKeePass2RandomStream)
29

30
void TestKeePass2RandomStream::initTestCase()
31
{
32
    QVERIFY(Crypto::init());
33
}
34

35
void TestKeePass2RandomStream::test()
36
{
37
    const QByteArray key("\x11\x22\x33\x44\x55\x66\x77\x88");
38
    const int Size = 128;
39

40
    SymmetricCipher cipher;
41
    QVERIFY(cipher.init(SymmetricCipher::Salsa20,
42
                        SymmetricCipher::Encrypt,
43
                        CryptoHash::hash(key, CryptoHash::Sha256),
44
                        KeePass2::INNER_STREAM_SALSA20_IV));
45

46
    const QByteArray data(QByteArray::fromHex("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5"
47
                                              "2b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6"
48
                                              "1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e94"
49
                                              "1e36b26bd1ebc670d1bd1d665620abf74f78a7f6d29809585a97daec58c6b050"));
50

51
    QByteArray cipherPad;
52
    cipherPad.fill('\0', Size);
53
    QVERIFY(cipher.process(cipherPad));
54

55
    QByteArray cipherData;
56
    cipherData.resize(Size);
57

58
    for (int i = 0; i < Size; i++) {
59
        cipherData[i] = data[i] ^ cipherPad[i];
60
    }
61

62
    KeePass2RandomStream randomStream;
63
    bool ok;
64
    QVERIFY(randomStream.init(SymmetricCipher::Salsa20, key));
65
    QByteArray randomStreamData;
66
    randomStreamData.append(randomStream.process(data.mid(0, 7), &ok));
67
    QVERIFY(ok);
68
    randomStreamData.append(randomStream.process(data.mid(7, 1), &ok));
69
    QVERIFY(ok);
70
    QByteArray tmpData = data.mid(8, 12);
71
    QVERIFY(randomStream.processInPlace(tmpData));
72
    randomStreamData.append(tmpData);
73
    randomStreamData.append(randomStream.process(data.mid(20, 44), &ok));
74
    QVERIFY(ok);
75
    randomStreamData.append(randomStream.process(data.mid(64, 64), &ok));
76
    QVERIFY(ok);
77

78
    SymmetricCipher cipherEncrypt;
79
    QVERIFY(cipherEncrypt.init(SymmetricCipher::Salsa20,
80
                               SymmetricCipher::Encrypt,
81
                               CryptoHash::hash(key, CryptoHash::Sha256),
82
                               KeePass2::INNER_STREAM_SALSA20_IV));
83
    QByteArray cipherDataEncrypt = data;
84
    QVERIFY(cipherEncrypt.process(cipherDataEncrypt));
85

86
    QCOMPARE(randomStreamData.size(), Size);
87
    QCOMPARE(cipherData, cipherDataEncrypt);
88
    QCOMPARE(randomStreamData, cipherData);
89
}
90

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

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

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

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