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 "TestSymmetricCipher.h"
19 
20 #include <QBuffer>
21 #include <QTest>
22 
23 #include "crypto/Crypto.h"
24 #include "crypto/SymmetricCipher.h"
25 #include "streams/SymmetricCipherStream.h"
26 
QTEST_GUILESS_MAIN(TestSymmetricCipher)27 QTEST_GUILESS_MAIN(TestSymmetricCipher)
28 
29 void TestSymmetricCipher::initTestCase()
30 {
31     QVERIFY(Crypto::init());
32 }
33 
testAes256CbcEncryption()34 void TestSymmetricCipher::testAes256CbcEncryption()
35 {
36     // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
37 
38     QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
39     QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f");
40     QByteArray plainText = QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
41     plainText.append(QByteArray::fromHex("ae2d8a571e03ac9c9eb76fac45af8e51"));
42     QByteArray cipherText = QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd6");
43     cipherText.append(QByteArray::fromHex("9cfc4e967edb808d679f777bc6702c7d"));
44     bool ok;
45 
46     SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Encrypt);
47     QVERIFY(cipher.init(key, iv));
48     QCOMPARE(cipher.blockSize(), 16);
49 
50     QCOMPARE(cipher.process(plainText, &ok),
51              cipherText);
52     QVERIFY(ok);
53 
54     QBuffer buffer;
55     SymmetricCipherStream stream(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc,
56                                  SymmetricCipher::Encrypt);
57     QVERIFY(stream.init(key, iv));
58     buffer.open(QIODevice::WriteOnly);
59     QVERIFY(stream.open(QIODevice::WriteOnly));
60     QVERIFY(stream.reset());
61 
62     buffer.reset();
63     buffer.buffer().clear();
64     QCOMPARE(stream.write(plainText.left(16)), qint64(16));
65     QCOMPARE(buffer.data(), cipherText.left(16));
66     QVERIFY(stream.reset());
67     // make sure padding is written
68     QCOMPARE(buffer.data().size(), 32);
69 
70     buffer.reset();
71     buffer.buffer().clear();
72     QCOMPARE(stream.write(plainText.left(10)), qint64(10));
73     QVERIFY(buffer.data().isEmpty());
74 
75     QVERIFY(stream.reset());
76     buffer.reset();
77     buffer.buffer().clear();
78     QCOMPARE(stream.write(plainText.left(10)), qint64(10));
79     stream.close();
80     QCOMPARE(buffer.data().size(), 16);
81 }
82 
testAes256CbcDecryption()83 void TestSymmetricCipher::testAes256CbcDecryption()
84 {
85     QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
86     QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f");
87     QByteArray cipherText = QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd6");
88     cipherText.append(QByteArray::fromHex("9cfc4e967edb808d679f777bc6702c7d"));
89     QByteArray plainText = QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
90     plainText.append(QByteArray::fromHex("ae2d8a571e03ac9c9eb76fac45af8e51"));
91     bool ok;
92 
93     SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Decrypt);
94     QVERIFY(cipher.init(key, iv));
95     QCOMPARE(cipher.blockSize(), 16);
96 
97     QCOMPARE(cipher.process(cipherText, &ok),
98              plainText);
99     QVERIFY(ok);
100 
101     // padded with 16 0x16 bytes
102     QByteArray cipherTextPadded = cipherText + QByteArray::fromHex("3a3aa5e0213db1a9901f9036cf5102d2");
103     QBuffer buffer(&cipherTextPadded);
104     SymmetricCipherStream stream(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc,
105                                  SymmetricCipher::Decrypt);
106     QVERIFY(stream.init(key, iv));
107     buffer.open(QIODevice::ReadOnly);
108     QVERIFY(stream.open(QIODevice::ReadOnly));
109 
110     QCOMPARE(stream.read(10),
111              plainText.left(10));
112     buffer.reset();
113     QVERIFY(stream.reset());
114     QCOMPARE(stream.read(20),
115              plainText.left(20));
116     buffer.reset();
117     QVERIFY(stream.reset());
118     QCOMPARE(stream.read(16),
119              plainText.left(16));
120     buffer.reset();
121     QVERIFY(stream.reset());
122     QCOMPARE(stream.read(100),
123              plainText);
124 }
125 
testSalsa20()126 void TestSymmetricCipher::testSalsa20()
127 {
128     // http://www.ecrypt.eu.org/stream/svn/viewcvs.cgi/ecrypt/trunk/submissions/salsa20/full/verified.test-vectors?logsort=rev&rev=210&view=markup
129 
130     QByteArray key = QByteArray::fromHex("F3F4F5F6F7F8F9FAFBFCFDFEFF000102030405060708090A0B0C0D0E0F101112");
131     QByteArray iv = QByteArray::fromHex("0000000000000000");
132     bool ok;
133 
134     SymmetricCipher cipher(SymmetricCipher::Salsa20, SymmetricCipher::Stream, SymmetricCipher::Encrypt);
135     QVERIFY(cipher.init(key, iv));
136 
137     QByteArray cipherTextA;
138     for (int i = 0; i < 8; i++) {
139         cipherTextA.append(cipher.process(QByteArray(64, '\0'), &ok));
140         QVERIFY(ok);
141     }
142     cipher.reset();
143 
144     QByteArray cipherTextB = cipher.process(QByteArray(512, '\0'), &ok);
145     QVERIFY(ok);
146     cipher.reset();
147 
148     QByteArray expectedCipherText1;
149     expectedCipherText1.append(QByteArray::fromHex("B4C0AFA503BE7FC29A62058166D56F8F"));
150     expectedCipherText1.append(QByteArray::fromHex("5D27DC246F75B9AD8760C8C39DFD8749"));
151     expectedCipherText1.append(QByteArray::fromHex("2D3B76D5D9637F009EADA14458A52DFB"));
152     expectedCipherText1.append(QByteArray::fromHex("09815337E72672681DDDC24633750D83"));
153 
154     QByteArray expectedCipherText2;
155     expectedCipherText2.append(QByteArray::fromHex("DBBA0683DF48C335A9802EEF02522563"));
156     expectedCipherText2.append(QByteArray::fromHex("54C9F763C3FDE19131A6BB7B85040624"));
157     expectedCipherText2.append(QByteArray::fromHex("B1D6CD4BF66D16F7482236C8602A6D58"));
158     expectedCipherText2.append(QByteArray::fromHex("505EEDCCA0B77AED574AB583115124B9"));
159 
160     QByteArray expectedCipherText3;
161     expectedCipherText3.append(QByteArray::fromHex("F0C5F98BAE05E019764EF6B65E0694A9"));
162     expectedCipherText3.append(QByteArray::fromHex("04CB9EC9C10C297B1AB1A6052365BB78"));
163     expectedCipherText3.append(QByteArray::fromHex("E55D3C6CB9F06184BA7D425A92E7E987"));
164     expectedCipherText3.append(QByteArray::fromHex("757FC5D9AFD7082418DD64125CA6F2B6"));
165 
166     QByteArray expectedCipherText4;
167     expectedCipherText4.append(QByteArray::fromHex("5A5FB5C8F0AFEA471F0318A4A2792F7A"));
168     expectedCipherText4.append(QByteArray::fromHex("A5C67B6D6E0F0DDB79961C34E3A564BA"));
169     expectedCipherText4.append(QByteArray::fromHex("2EECE78D9AFF45E510FEAB1030B102D3"));
170     expectedCipherText4.append(QByteArray::fromHex("9DFCECB77F5798F7D2793C0AB09C7A04"));
171 
172     QCOMPARE(cipherTextA.mid(0, 64), expectedCipherText1);
173     QCOMPARE(cipherTextA.mid(192, 64), expectedCipherText2);
174     QCOMPARE(cipherTextA.mid(256, 64), expectedCipherText3);
175     QCOMPARE(cipherTextA.mid(448, 64), expectedCipherText4);
176 
177     QCOMPARE(cipherTextB.mid(0, 64), expectedCipherText1);
178     QCOMPARE(cipherTextB.mid(192, 64), expectedCipherText2);
179     QCOMPARE(cipherTextB.mid(256, 64), expectedCipherText3);
180     QCOMPARE(cipherTextB.mid(448, 64), expectedCipherText4);
181 }
182 
testPadding()183 void TestSymmetricCipher::testPadding()
184 {
185     QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
186     QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f");
187     QByteArray plainText = QByteArray::fromHex("6bc1bee22e409f96e93d");
188 
189     QBuffer buffer;
190     buffer.open(QIODevice::ReadWrite);
191 
192     SymmetricCipherStream streamEnc(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc,
193                                     SymmetricCipher::Encrypt);
194     QVERIFY(streamEnc.init(key, iv));
195     streamEnc.open(QIODevice::WriteOnly);
196     streamEnc.write(plainText);
197     streamEnc.close();
198     buffer.reset();
199     // make sure padding is written
200     QCOMPARE(buffer.buffer().size(), 16);
201 
202     SymmetricCipherStream streamDec(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc,
203                                     SymmetricCipher::Decrypt);
204     QVERIFY(streamDec.init(key, iv));
205     streamDec.open(QIODevice::ReadOnly);
206     QByteArray decrypted = streamDec.readAll();
207     QCOMPARE(decrypted, plainText);
208 }
209 
testStreamReset()210 void TestSymmetricCipher::testStreamReset()
211 {
212     QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
213     QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f");
214 
215     QBuffer buffer;
216     QVERIFY(buffer.open(QIODevice::WriteOnly));
217     SymmetricCipherStream writer(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc,
218                                  SymmetricCipher::Encrypt);
219     QVERIFY(writer.init(key, iv));
220     QVERIFY(writer.open(QIODevice::WriteOnly));
221     QCOMPARE(writer.write(QByteArray(4, 'Z')), qint64(4));
222     // test if reset() and close() write only one block
223     QVERIFY(writer.reset());
224     QVERIFY(writer.reset());
225     writer.close();
226     QCOMPARE(buffer.buffer().size(), 16);
227 }
228