keepassxc

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

19
#include "TestSymmetricCipher.h"
20

21
#include <QBuffer>
22
#include <QTest>
23
#include <QVector>
24

25
#include "crypto/Crypto.h"
26
#include "format/KeePass2.h"
27
#include "streams/SymmetricCipherStream.h"
28

29
QTEST_GUILESS_MAIN(TestSymmetricCipher)
30
Q_DECLARE_METATYPE(SymmetricCipher::Mode);
31
Q_DECLARE_METATYPE(SymmetricCipher::Direction);
32

33
void TestSymmetricCipher::initTestCase()
34
{
35
    QVERIFY(Crypto::init());
36
}
37

38
void TestSymmetricCipher::testCipherUuidToMode()
39
{
40
    QCOMPARE(SymmetricCipher::cipherUuidToMode(KeePass2::CIPHER_AES128), SymmetricCipher::Aes128_CBC);
41
    QCOMPARE(SymmetricCipher::cipherUuidToMode(KeePass2::CIPHER_AES256), SymmetricCipher::Aes256_CBC);
42
    QCOMPARE(SymmetricCipher::cipherUuidToMode(KeePass2::CIPHER_TWOFISH), SymmetricCipher::Twofish_CBC);
43
    QCOMPARE(SymmetricCipher::cipherUuidToMode(KeePass2::CIPHER_CHACHA20), SymmetricCipher::ChaCha20);
44
    QCOMPARE(SymmetricCipher::cipherUuidToMode(QUuid()), SymmetricCipher::InvalidMode);
45
}
46

47
// clang-format off
48
void TestSymmetricCipher::testEncryptionDecryption_data()
49
{
50
    QTest::addColumn<SymmetricCipher::Mode>("mode");
51
    QTest::addColumn<SymmetricCipher::Direction>("direction");
52
    QTest::addColumn<QByteArray>("key");
53
    QTest::addColumn<QByteArray>("iv");
54
    QTest::addColumn<QByteArray>("plainText");
55
    QTest::addColumn<QByteArray>("cipherText");
56

57
    // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
58
    QTest::newRow("AES128-CBC Encryption")
59
        << SymmetricCipher::Aes128_CBC
60
        << SymmetricCipher::Encrypt
61
        << QByteArray::fromHex("2b7e151628aed2a6abf7158809cf4f3c")
62
        << QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
63
        << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a")
64
        << QByteArray::fromHex("7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b2");
65

66
    QTest::newRow("AES128-CBC Decryption")
67
        << SymmetricCipher::Aes128_CBC
68
        << SymmetricCipher::Decrypt
69
        << QByteArray::fromHex("2b7e151628aed2a6abf7158809cf4f3c")
70
        << QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
71
        << QByteArray::fromHex("7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b2")
72
        << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
73

74
    QTest::newRow("AES256-CBC Encryption")
75
        << SymmetricCipher::Aes256_CBC
76
        << SymmetricCipher::Encrypt
77
        << QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
78
        << QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
79
        << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a")
80
        << QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d");
81

82
    QTest::newRow("AES256-CBC Decryption")
83
        << SymmetricCipher::Aes256_CBC
84
        << SymmetricCipher::Decrypt
85
        << QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
86
        << QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
87
        << QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d")
88
        << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
89

90
    QTest::newRow("AES256-CTR Encryption")
91
        << SymmetricCipher::Aes256_CTR
92
        << SymmetricCipher::Encrypt
93
        << QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
94
        << QByteArray::fromHex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
95
        << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a")
96
        << QByteArray::fromHex("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5");
97

98
    QTest::newRow("AES256-CTR Decryption")
99
        << SymmetricCipher::Aes256_CTR
100
        << SymmetricCipher::Decrypt
101
        << QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
102
        << QByteArray::fromHex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
103
        << QByteArray::fromHex("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5")
104
        << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
105
}
106
// clang-format on
107

108
void TestSymmetricCipher::testEncryptionDecryption()
109
{
110
    QFETCH(SymmetricCipher::Mode, mode);
111
    QFETCH(SymmetricCipher::Direction, direction);
112
    QFETCH(QByteArray, key);
113
    QFETCH(QByteArray, iv);
114
    QFETCH(QByteArray, plainText);
115
    QFETCH(QByteArray, cipherText);
116

117
    QByteArray data = plainText;
118
    SymmetricCipher cipher;
119
    QVERIFY(cipher.init(mode, direction, key, iv));
120
    QVERIFY(cipher.process(data));
121
    QCOMPARE(data.left(16), cipherText.left(16));
122

123
    QBuffer buffer;
124
    buffer.open(QIODevice::WriteOnly);
125
    SymmetricCipherStream stream(&buffer);
126
    QVERIFY(stream.init(mode, direction, key, iv));
127
    QVERIFY(stream.open(QIODevice::WriteOnly));
128
    QCOMPARE(stream.write(plainText.left(16)), qint64(16));
129
    stream.close();
130
    QCOMPARE(buffer.data().left(16), cipherText.left(16));
131
}
132

133
void TestSymmetricCipher::testAesCbcPadding_data()
134
{
135
    QTest::addColumn<SymmetricCipher::Mode>("mode");
136
    QTest::addColumn<QByteArray>("key");
137
    QTest::addColumn<QByteArray>("iv");
138
    QTest::addColumn<QByteArray>("cipherText");
139
    QTest::addColumn<QByteArray>("plainText");
140
    QTest::addColumn<QByteArray>("padding");
141

142
    // clang-format off
143
    QTest::newRow("AES128") << SymmetricCipher::Aes128_CBC
144
                            << QByteArray::fromHex("2b7e151628aed2a6abf7158809cf4f3c")
145
                            << QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
146
                            << QByteArray::fromHex("7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b2")
147
                            << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51")
148
                            << QByteArray::fromHex("55e21d7100b988ffec32feeafaf23538");
149

150
    QTest::newRow("AES256") << SymmetricCipher::Aes256_CBC
151
                            << QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
152
                            << QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
153
                            << QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d")
154
                            << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51")
155
                            << QByteArray::fromHex("3a3aa5e0213db1a9901f9036cf5102d2");
156
    // clang-format on
157
}
158

159
void TestSymmetricCipher::testAesCbcPadding()
160
{
161
    QFETCH(SymmetricCipher::Mode, mode);
162
    QFETCH(QByteArray, key);
163
    QFETCH(QByteArray, iv);
164
    QFETCH(QByteArray, cipherText);
165
    QFETCH(QByteArray, plainText);
166
    QFETCH(QByteArray, padding);
167

168
    // padded with 16 0x10 bytes
169
    QByteArray cipherTextPadded = cipherText + padding;
170

171
    QBuffer buffer(&cipherTextPadded);
172
    SymmetricCipherStream stream(&buffer);
173
    QVERIFY(stream.init(mode, SymmetricCipher::Decrypt, key, iv));
174
    buffer.open(QIODevice::ReadOnly);
175
    QVERIFY(stream.open(QIODevice::ReadOnly));
176

177
    QCOMPARE(stream.read(10), plainText.left(10));
178
    buffer.reset();
179
    QVERIFY(stream.reset());
180
    QVERIFY(stream.init(mode, SymmetricCipher::Decrypt, key, iv));
181
    QCOMPARE(stream.read(20), plainText.left(20));
182
    buffer.reset();
183
    QVERIFY(stream.reset());
184
    QVERIFY(stream.init(mode, SymmetricCipher::Decrypt, key, iv));
185
    QCOMPARE(stream.read(16), plainText.left(16));
186
    buffer.reset();
187
    QVERIFY(stream.reset());
188
    QVERIFY(stream.init(mode, SymmetricCipher::Decrypt, key, iv));
189
    QCOMPARE(stream.read(100), plainText);
190
}
191

192
void TestSymmetricCipher::testAesKdf()
193
{
194
    auto key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
195
    auto data = QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
196
    auto result = QByteArray::fromHex("f3eed1bdb5d2a03c064b5a7e3db181f8");
197

198
    QVERIFY(SymmetricCipher::aesKdf(key, 1, data));
199
    QCOMPARE(data, result);
200

201
    // TODO: Test multiple rounds of AES KDF
202
}
203

204
void TestSymmetricCipher::testTwofish256CbcEncryption()
205
{
206
    // NIST MCT Known-Answer Tests (cbc_e_m.txt)
207
    // https://www.schneier.com/code/twofish-kat.zip
208

209
    QVector<QByteArray> keys{QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
210
                             QByteArray::fromHex("D0A260EB41755B19374BABF259A79DB3EA7162E65490B03B1AE4871FB35EF23B"),
211
                             QByteArray::fromHex("8D55E4849A4DED08D89881E6708EDD26BEEE942073DFB3790B2798B240ACD74A"),
212
                             QByteArray::fromHex("606EFDC2066A837AF0430EBE4CF1F21071CCB236C33B4B9D82404FDB05C74621"),
213
                             QByteArray::fromHex("B119AA9485CEEEB4CC778AF21121E54DE4BDBA3498C61C8FD9004AA0C71909C3")};
214
    QVector<QByteArray> ivs{QByteArray::fromHex("00000000000000000000000000000000"),
215
                            QByteArray::fromHex("EA7162E65490B03B1AE4871FB35EF23B"),
216
                            QByteArray::fromHex("549FF6C6274F034211C31FADF3F22571"),
217
                            QByteArray::fromHex("CF222616B0E4F8E48967D769456B916B"),
218
                            QByteArray::fromHex("957108025BFD57125B40057BC2DE4FE2")};
219
    QVector<QByteArray> plainTexts{QByteArray::fromHex("00000000000000000000000000000000"),
220
                                   QByteArray::fromHex("D0A260EB41755B19374BABF259A79DB3"),
221
                                   QByteArray::fromHex("5DF7846FDB38B611EFD32A1429294095"),
222
                                   QByteArray::fromHex("ED3B19469C276E7228DB8F583C7F2F36"),
223
                                   QByteArray::fromHex("D177575683A46DCE3C34844C5DD0175D")};
224
    QVector<QByteArray> cipherTexts{QByteArray::fromHex("EA7162E65490B03B1AE4871FB35EF23B"),
225
                                    QByteArray::fromHex("549FF6C6274F034211C31FADF3F22571"),
226
                                    QByteArray::fromHex("CF222616B0E4F8E48967D769456B916B"),
227
                                    QByteArray::fromHex("957108025BFD57125B40057BC2DE4FE2"),
228
                                    QByteArray::fromHex("6F725C5950133F82EF021A94CADC8508")};
229

230
    SymmetricCipher cipher;
231

232
    for (int i = 0; i < keys.size(); ++i) {
233
        QVERIFY(cipher.init(SymmetricCipher::Twofish_CBC, SymmetricCipher::Encrypt, keys[i], ivs[i]));
234
        QByteArray ptNext = plainTexts[i];
235
        QByteArray ctPrev = ivs[i];
236
        QByteArray ctCur;
237
        QCOMPARE(cipher.blockSize(cipher.mode()), 16);
238
        for (int j = 0; j < 5000; ++j) {
239
            if (!cipher.process(ptNext)) {
240
                break;
241
            }
242
            ctCur = ptNext;
243
            ptNext = ctPrev;
244
            ctPrev = ctCur;
245

246
            if (!cipher.process(ptNext)) {
247
                break;
248
            }
249
            ctCur = ptNext;
250
            ptNext = ctPrev;
251
            ctPrev = ctCur;
252
        }
253

254
        QCOMPARE(ctCur, cipherTexts[i]);
255
    }
256
}
257

258
void TestSymmetricCipher::testTwofish256CbcDecryption()
259
{
260
    // NIST MCT Known-Answer Tests (cbc_d_m.txt)
261
    // https://www.schneier.com/code/twofish-kat.zip
262

263
    QVector<QByteArray> keys{QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
264
                             QByteArray::fromHex("1B1FE8F5A911CD4C0D800EDCE8ED0A942CBA6271A1044F90C30BA8FE91E1C163"),
265
                             QByteArray::fromHex("EBA31FF8D2A24FDD769A937353E23257294A33394E4D17A668060AD8230811A1"),
266
                             QByteArray::fromHex("1DCF1915C389AB273F80F897BF008F058ED89F58A95C1BE523C4B11295ED2D0F"),
267
                             QByteArray::fromHex("491B9A66D3ED4EF19F02180289D5B1A1C2596AE568540A95DC5244198A9B8869")};
268
    QVector<QByteArray> ivs{QByteArray::fromHex("00000000000000000000000000000000"),
269
                            QByteArray::fromHex("1B1FE8F5A911CD4C0D800EDCE8ED0A94"),
270
                            QByteArray::fromHex("F0BCF70D7BB382917B1A9DAFBB0F38C3"),
271
                            QByteArray::fromHex("F66C06ED112BE4FA491A6BE4ECE2BD52"),
272
                            QByteArray::fromHex("54D483731064E5D6A082E09536D53EA4")};
273
    QVector<QByteArray> plainTexts{QByteArray::fromHex("2CBA6271A1044F90C30BA8FE91E1C163"),
274
                                   QByteArray::fromHex("05F05148EF495836AB0DA226B2E9D0C2"),
275
                                   QByteArray::fromHex("A792AC61E7110C434BC2BBCAB6E53CAE"),
276
                                   QByteArray::fromHex("4C81F5BDC1081170FF96F50B1F76A566"),
277
                                   QByteArray::fromHex("BD959F5B787037631A37051EA5F369F8")};
278
    QVector<QByteArray> cipherTexts{QByteArray::fromHex("00000000000000000000000000000000"),
279
                                    QByteArray::fromHex("2CBA6271A1044F90C30BA8FE91E1C163"),
280
                                    QByteArray::fromHex("05F05148EF495836AB0DA226B2E9D0C2"),
281
                                    QByteArray::fromHex("A792AC61E7110C434BC2BBCAB6E53CAE"),
282
                                    QByteArray::fromHex("4C81F5BDC1081170FF96F50B1F76A566")};
283

284
    SymmetricCipher cipher;
285

286
    for (int i = 0; i < keys.size(); ++i) {
287
        QVERIFY(cipher.init(SymmetricCipher::Twofish_CBC, SymmetricCipher::Decrypt, keys[i], ivs[i]));
288
        QByteArray ctNext = cipherTexts[i];
289
        QByteArray ptCur;
290
        QCOMPARE(cipher.blockSize(cipher.mode()), 16);
291
        for (int j = 0; j < 5000; ++j) {
292
            if (!cipher.process(ctNext)) {
293
                break;
294
            }
295

296
            if (!cipher.process(ctNext)) {
297
                break;
298
            }
299
        }
300

301
        QCOMPARE(ctNext, plainTexts[i]);
302
    }
303
}
304

305
void TestSymmetricCipher::testSalsa20()
306
{
307
    // http://www.ecrypt.eu.org/stream/svn/viewcvs.cgi/ecrypt/trunk/submissions/salsa20/full/verified.test-vectors?logsort=rev&rev=210&view=markup
308

309
    QByteArray key = QByteArray::fromHex("F3F4F5F6F7F8F9FAFBFCFDFEFF000102030405060708090A0B0C0D0E0F101112");
310
    QByteArray iv = QByteArray::fromHex("0000000000000000");
311

312
    SymmetricCipher cipher;
313
    QVERIFY(cipher.init(SymmetricCipher::Salsa20, SymmetricCipher::Encrypt, key, iv));
314

315
    QByteArray cipherTextA;
316
    for (int i = 0; i < 8; i++) {
317
        QByteArray data(64, '\0');
318
        QVERIFY(cipher.process(data));
319
        cipherTextA.append(data);
320
    }
321

322
    // Re-initialize
323
    QVERIFY(cipher.init(SymmetricCipher::Salsa20, SymmetricCipher::Encrypt, key, iv));
324

325
    QByteArray cipherTextB(512, '\0');
326
    QVERIFY(cipher.process(cipherTextB));
327
    cipher.reset();
328

329
    QByteArray expectedCipherText1;
330
    expectedCipherText1.append(QByteArray::fromHex("B4C0AFA503BE7FC29A62058166D56F8F"));
331
    expectedCipherText1.append(QByteArray::fromHex("5D27DC246F75B9AD8760C8C39DFD8749"));
332
    expectedCipherText1.append(QByteArray::fromHex("2D3B76D5D9637F009EADA14458A52DFB"));
333
    expectedCipherText1.append(QByteArray::fromHex("09815337E72672681DDDC24633750D83"));
334

335
    QByteArray expectedCipherText2;
336
    expectedCipherText2.append(QByteArray::fromHex("DBBA0683DF48C335A9802EEF02522563"));
337
    expectedCipherText2.append(QByteArray::fromHex("54C9F763C3FDE19131A6BB7B85040624"));
338
    expectedCipherText2.append(QByteArray::fromHex("B1D6CD4BF66D16F7482236C8602A6D58"));
339
    expectedCipherText2.append(QByteArray::fromHex("505EEDCCA0B77AED574AB583115124B9"));
340

341
    QByteArray expectedCipherText3;
342
    expectedCipherText3.append(QByteArray::fromHex("F0C5F98BAE05E019764EF6B65E0694A9"));
343
    expectedCipherText3.append(QByteArray::fromHex("04CB9EC9C10C297B1AB1A6052365BB78"));
344
    expectedCipherText3.append(QByteArray::fromHex("E55D3C6CB9F06184BA7D425A92E7E987"));
345
    expectedCipherText3.append(QByteArray::fromHex("757FC5D9AFD7082418DD64125CA6F2B6"));
346

347
    QByteArray expectedCipherText4;
348
    expectedCipherText4.append(QByteArray::fromHex("5A5FB5C8F0AFEA471F0318A4A2792F7A"));
349
    expectedCipherText4.append(QByteArray::fromHex("A5C67B6D6E0F0DDB79961C34E3A564BA"));
350
    expectedCipherText4.append(QByteArray::fromHex("2EECE78D9AFF45E510FEAB1030B102D3"));
351
    expectedCipherText4.append(QByteArray::fromHex("9DFCECB77F5798F7D2793C0AB09C7A04"));
352

353
    QCOMPARE(cipherTextA.mid(0, 64), expectedCipherText1);
354
    QCOMPARE(cipherTextA.mid(192, 64), expectedCipherText2);
355
    QCOMPARE(cipherTextA.mid(256, 64), expectedCipherText3);
356
    QCOMPARE(cipherTextA.mid(448, 64), expectedCipherText4);
357

358
    QCOMPARE(cipherTextB.mid(0, 64), expectedCipherText1);
359
    QCOMPARE(cipherTextB.mid(192, 64), expectedCipherText2);
360
    QCOMPARE(cipherTextB.mid(256, 64), expectedCipherText3);
361
    QCOMPARE(cipherTextB.mid(448, 64), expectedCipherText4);
362
}
363

364
void TestSymmetricCipher::testChaCha20()
365
{
366
    // https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7
367
    {
368
        QByteArray key = QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000");
369
        QByteArray iv = QByteArray::fromHex("0000000000000000");
370
        SymmetricCipher cipher;
371
        QByteArray data(64, 0);
372
        QVERIFY(cipher.init(SymmetricCipher::ChaCha20, SymmetricCipher::Encrypt, key, iv));
373
        QVERIFY(cipher.process(data));
374
        QCOMPARE(data,
375
                 QByteArray::fromHex("76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7"
376
                                     "724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586"));
377
    }
378

379
    {
380
        QByteArray key = QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000001");
381
        QByteArray iv = QByteArray::fromHex("0000000000000000");
382
        SymmetricCipher cipher;
383
        QByteArray data(64, 0);
384
        QVERIFY(cipher.init(SymmetricCipher::ChaCha20, SymmetricCipher::Encrypt, key, iv));
385
        QVERIFY(cipher.process(data));
386
        QCOMPARE(data,
387
                 QByteArray::fromHex("4540f05a9f1fb296d7736e7b208e3c96eb4fe1834688d2604f450952ed432d41bbe2a0b6ea7566d2a"
388
                                     "5d1e7e20d42af2c53d792b1c43fea817e9ad275ae546963"));
389
    }
390

391
    {
392
        QByteArray key = QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000");
393
        QByteArray iv = QByteArray::fromHex("0000000000000001");
394
        SymmetricCipher cipher;
395
        QByteArray data(60, 0);
396
        QVERIFY(cipher.init(SymmetricCipher::ChaCha20, SymmetricCipher::Encrypt, key, iv));
397
        QVERIFY(cipher.process(data));
398
        QCOMPARE(data,
399
                 QByteArray::fromHex("de9cba7bf3d69ef5e786dc63973f653a0b49e015adbff7134fcb7df137821031e85a050278a708452"
400
                                     "7214f73efc7fa5b5277062eb7a0433e445f41e3"));
401
    }
402

403
    {
404
        QByteArray key = QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000");
405
        QByteArray iv = QByteArray::fromHex("0100000000000000");
406
        SymmetricCipher cipher;
407
        QByteArray data(64, 0);
408
        QVERIFY(cipher.init(SymmetricCipher::ChaCha20, SymmetricCipher::Encrypt, key, iv));
409
        QVERIFY(cipher.process(data));
410
        QCOMPARE(data,
411
                 QByteArray::fromHex("ef3fdfd6c61578fbf5cf35bd3dd33b8009631634d21e42ac33960bd138e50d32111e4caf237ee53ca"
412
                                     "8ad6426194a88545ddc497a0b466e7d6bbdb0041b2f586b"));
413
    }
414
}
415

416
void TestSymmetricCipher::testPadding()
417
{
418
    QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
419
    QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f");
420
    QByteArray plainText = QByteArray::fromHex("6bc1bee22e409f96e93d");
421

422
    QBuffer buffer;
423
    buffer.open(QIODevice::ReadWrite);
424

425
    SymmetricCipherStream streamEnc(&buffer);
426
    QVERIFY(streamEnc.init(SymmetricCipher::Aes256_CBC, SymmetricCipher::Encrypt, key, iv));
427
    streamEnc.open(QIODevice::WriteOnly);
428
    streamEnc.write(plainText);
429
    streamEnc.close();
430
    buffer.reset();
431
    // make sure padding is written
432
    QCOMPARE(buffer.buffer().size(), 16);
433

434
    SymmetricCipherStream streamDec(&buffer);
435
    QVERIFY(streamDec.init(SymmetricCipher::Aes256_CBC, SymmetricCipher::Decrypt, key, iv));
436
    streamDec.open(QIODevice::ReadOnly);
437
    QByteArray decrypted = streamDec.readAll();
438
    QCOMPARE(decrypted, plainText);
439
}
440

441
void TestSymmetricCipher::testStreamReset()
442
{
443
    QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
444
    QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f");
445

446
    QBuffer buffer;
447
    QVERIFY(buffer.open(QIODevice::WriteOnly));
448
    SymmetricCipherStream writer(&buffer);
449
    QVERIFY(writer.init(SymmetricCipher::Aes256_CBC, SymmetricCipher::Encrypt, key, iv));
450
    QVERIFY(writer.open(QIODevice::WriteOnly));
451
    QCOMPARE(writer.write(QByteArray(4, 'Z')), qint64(4));
452
    // test if reset() and close() write only one block
453
    QVERIFY(writer.reset());
454
    QVERIFY(writer.reset());
455
    writer.close();
456
    QCOMPARE(buffer.buffer().size(), 16);
457
}
458

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

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

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

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