2
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
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.
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.
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/>.
18
#include "TestKeePass2RandomStream.h"
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"
28
QTEST_GUILESS_MAIN(TestKeePass2RandomStream)
30
void TestKeePass2RandomStream::initTestCase()
32
QVERIFY(Crypto::init());
35
void TestKeePass2RandomStream::test()
37
const QByteArray key("\x11\x22\x33\x44\x55\x66\x77\x88");
40
SymmetricCipher cipher;
41
QVERIFY(cipher.init(SymmetricCipher::Salsa20,
42
SymmetricCipher::Encrypt,
43
CryptoHash::hash(key, CryptoHash::Sha256),
44
KeePass2::INNER_STREAM_SALSA20_IV));
46
const QByteArray data(QByteArray::fromHex("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5"
47
"2b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6"
48
"1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e94"
49
"1e36b26bd1ebc670d1bd1d665620abf74f78a7f6d29809585a97daec58c6b050"));
52
cipherPad.fill('\0', Size);
53
QVERIFY(cipher.process(cipherPad));
55
QByteArray cipherData;
56
cipherData.resize(Size);
58
for (int i = 0; i < Size; i++) {
59
cipherData[i] = data[i] ^ cipherPad[i];
62
KeePass2RandomStream randomStream;
64
QVERIFY(randomStream.init(SymmetricCipher::Salsa20, key));
65
QByteArray randomStreamData;
66
randomStreamData.append(randomStream.process(data.mid(0, 7), &ok));
68
randomStreamData.append(randomStream.process(data.mid(7, 1), &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));
75
randomStreamData.append(randomStream.process(data.mid(64, 64), &ok));
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));
86
QCOMPARE(randomStreamData.size(), Size);
87
QCOMPARE(cipherData, cipherDataEncrypt);
88
QCOMPARE(randomStreamData, cipherData);