TestSymmetricCipher.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. #include "TestSymmetricCipher.h"
  19. #include <QBuffer>
  20. #include <QTest>
  21. #include <QVector>
  22. #include "crypto/Crypto.h"
  23. #include "format/KeePass2.h"
  24. #include "streams/SymmetricCipherStream.h"
  25. QTEST_GUILESS_MAIN(TestSymmetricCipher)
  26. Q_DECLARE_METATYPE(SymmetricCipher::Mode);
  27. Q_DECLARE_METATYPE(SymmetricCipher::Direction);
  28. void TestSymmetricCipher::initTestCase()
  29. {
  30. QVERIFY(Crypto::init());
  31. }
  32. void TestSymmetricCipher::testCipherUuidToMode()
  33. {
  34. QCOMPARE(SymmetricCipher::cipherUuidToMode(KeePass2::CIPHER_AES128), SymmetricCipher::Aes128_CBC);
  35. QCOMPARE(SymmetricCipher::cipherUuidToMode(KeePass2::CIPHER_AES256), SymmetricCipher::Aes256_CBC);
  36. QCOMPARE(SymmetricCipher::cipherUuidToMode(KeePass2::CIPHER_TWOFISH), SymmetricCipher::Twofish_CBC);
  37. QCOMPARE(SymmetricCipher::cipherUuidToMode(KeePass2::CIPHER_CHACHA20), SymmetricCipher::ChaCha20);
  38. QCOMPARE(SymmetricCipher::cipherUuidToMode(QUuid()), SymmetricCipher::InvalidMode);
  39. }
  40. // clang-format off
  41. void TestSymmetricCipher::testEncryptionDecryption_data()
  42. {
  43. QTest::addColumn<SymmetricCipher::Mode>("mode");
  44. QTest::addColumn<SymmetricCipher::Direction>("direction");
  45. QTest::addColumn<QByteArray>("key");
  46. QTest::addColumn<QByteArray>("iv");
  47. QTest::addColumn<QByteArray>("plainText");
  48. QTest::addColumn<QByteArray>("cipherText");
  49. // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
  50. QTest::newRow("AES128-CBC Encryption")
  51. << SymmetricCipher::Aes128_CBC
  52. << SymmetricCipher::Encrypt
  53. << QByteArray::fromHex("2b7e151628aed2a6abf7158809cf4f3c")
  54. << QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
  55. << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a")
  56. << QByteArray::fromHex("7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b2");
  57. QTest::newRow("AES128-CBC Decryption")
  58. << SymmetricCipher::Aes128_CBC
  59. << SymmetricCipher::Decrypt
  60. << QByteArray::fromHex("2b7e151628aed2a6abf7158809cf4f3c")
  61. << QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
  62. << QByteArray::fromHex("7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b2")
  63. << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
  64. QTest::newRow("AES256-CBC Encryption")
  65. << SymmetricCipher::Aes256_CBC
  66. << SymmetricCipher::Encrypt
  67. << QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
  68. << QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
  69. << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a")
  70. << QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d");
  71. QTest::newRow("AES256-CBC Decryption")
  72. << SymmetricCipher::Aes256_CBC
  73. << SymmetricCipher::Decrypt
  74. << QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
  75. << QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
  76. << QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d")
  77. << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
  78. QTest::newRow("AES256-CTR Encryption")
  79. << SymmetricCipher::Aes256_CTR
  80. << SymmetricCipher::Encrypt
  81. << QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
  82. << QByteArray::fromHex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
  83. << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a")
  84. << QByteArray::fromHex("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5");
  85. QTest::newRow("AES256-CTR Decryption")
  86. << SymmetricCipher::Aes256_CTR
  87. << SymmetricCipher::Decrypt
  88. << QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
  89. << QByteArray::fromHex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
  90. << QByteArray::fromHex("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5")
  91. << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
  92. }
  93. // clang-format on
  94. void TestSymmetricCipher::testEncryptionDecryption()
  95. {
  96. QFETCH(SymmetricCipher::Mode, mode);
  97. QFETCH(SymmetricCipher::Direction, direction);
  98. QFETCH(QByteArray, key);
  99. QFETCH(QByteArray, iv);
  100. QFETCH(QByteArray, plainText);
  101. QFETCH(QByteArray, cipherText);
  102. QByteArray data = plainText;
  103. SymmetricCipher cipher;
  104. QVERIFY(cipher.init(mode, direction, key, iv));
  105. QVERIFY(cipher.process(data));
  106. QCOMPARE(data.left(16), cipherText.left(16));
  107. QBuffer buffer;
  108. buffer.open(QIODevice::WriteOnly);
  109. SymmetricCipherStream stream(&buffer);
  110. QVERIFY(stream.init(mode, direction, key, iv));
  111. QVERIFY(stream.open(QIODevice::WriteOnly));
  112. QCOMPARE(stream.write(plainText.left(16)), qint64(16));
  113. stream.close();
  114. QCOMPARE(buffer.data().left(16), cipherText.left(16));
  115. }
  116. void TestSymmetricCipher::testAesCbcPadding_data()
  117. {
  118. QTest::addColumn<SymmetricCipher::Mode>("mode");
  119. QTest::addColumn<QByteArray>("key");
  120. QTest::addColumn<QByteArray>("iv");
  121. QTest::addColumn<QByteArray>("cipherText");
  122. QTest::addColumn<QByteArray>("plainText");
  123. QTest::addColumn<QByteArray>("padding");
  124. // clang-format off
  125. QTest::newRow("AES128") << SymmetricCipher::Aes128_CBC
  126. << QByteArray::fromHex("2b7e151628aed2a6abf7158809cf4f3c")
  127. << QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
  128. << QByteArray::fromHex("7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b2")
  129. << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51")
  130. << QByteArray::fromHex("55e21d7100b988ffec32feeafaf23538");
  131. QTest::newRow("AES256") << SymmetricCipher::Aes256_CBC
  132. << QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
  133. << QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
  134. << QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d")
  135. << QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51")
  136. << QByteArray::fromHex("3a3aa5e0213db1a9901f9036cf5102d2");
  137. // clang-format on
  138. }
  139. void TestSymmetricCipher::testAesCbcPadding()
  140. {
  141. QFETCH(SymmetricCipher::Mode, mode);
  142. QFETCH(QByteArray, key);
  143. QFETCH(QByteArray, iv);
  144. QFETCH(QByteArray, cipherText);
  145. QFETCH(QByteArray, plainText);
  146. QFETCH(QByteArray, padding);
  147. // padded with 16 0x10 bytes
  148. QByteArray cipherTextPadded = cipherText + padding;
  149. QBuffer buffer(&cipherTextPadded);
  150. SymmetricCipherStream stream(&buffer);
  151. QVERIFY(stream.init(mode, SymmetricCipher::Decrypt, key, iv));
  152. buffer.open(QIODevice::ReadOnly);
  153. QVERIFY(stream.open(QIODevice::ReadOnly));
  154. QCOMPARE(stream.read(10), plainText.left(10));
  155. buffer.reset();
  156. QVERIFY(stream.reset());
  157. QVERIFY(stream.init(mode, SymmetricCipher::Decrypt, key, iv));
  158. QCOMPARE(stream.read(20), plainText.left(20));
  159. buffer.reset();
  160. QVERIFY(stream.reset());
  161. QVERIFY(stream.init(mode, SymmetricCipher::Decrypt, key, iv));
  162. QCOMPARE(stream.read(16), plainText.left(16));
  163. buffer.reset();
  164. QVERIFY(stream.reset());
  165. QVERIFY(stream.init(mode, SymmetricCipher::Decrypt, key, iv));
  166. QCOMPARE(stream.read(100), plainText);
  167. }
  168. void TestSymmetricCipher::testAesKdf()
  169. {
  170. auto key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
  171. auto data = QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
  172. auto result = QByteArray::fromHex("f3eed1bdb5d2a03c064b5a7e3db181f8");
  173. QVERIFY(SymmetricCipher::aesKdf(key, 1, data));
  174. QCOMPARE(data, result);
  175. // TODO: Test multiple rounds of AES KDF
  176. }
  177. void TestSymmetricCipher::testTwofish256CbcEncryption()
  178. {
  179. // NIST MCT Known-Answer Tests (cbc_e_m.txt)
  180. // https://www.schneier.com/code/twofish-kat.zip
  181. QVector<QByteArray> keys{QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
  182. QByteArray::fromHex("D0A260EB41755B19374BABF259A79DB3EA7162E65490B03B1AE4871FB35EF23B"),
  183. QByteArray::fromHex("8D55E4849A4DED08D89881E6708EDD26BEEE942073DFB3790B2798B240ACD74A"),
  184. QByteArray::fromHex("606EFDC2066A837AF0430EBE4CF1F21071CCB236C33B4B9D82404FDB05C74621"),
  185. QByteArray::fromHex("B119AA9485CEEEB4CC778AF21121E54DE4BDBA3498C61C8FD9004AA0C71909C3")};
  186. QVector<QByteArray> ivs{QByteArray::fromHex("00000000000000000000000000000000"),
  187. QByteArray::fromHex("EA7162E65490B03B1AE4871FB35EF23B"),
  188. QByteArray::fromHex("549FF6C6274F034211C31FADF3F22571"),
  189. QByteArray::fromHex("CF222616B0E4F8E48967D769456B916B"),
  190. QByteArray::fromHex("957108025BFD57125B40057BC2DE4FE2")};
  191. QVector<QByteArray> plainTexts{QByteArray::fromHex("00000000000000000000000000000000"),
  192. QByteArray::fromHex("D0A260EB41755B19374BABF259A79DB3"),
  193. QByteArray::fromHex("5DF7846FDB38B611EFD32A1429294095"),
  194. QByteArray::fromHex("ED3B19469C276E7228DB8F583C7F2F36"),
  195. QByteArray::fromHex("D177575683A46DCE3C34844C5DD0175D")};
  196. QVector<QByteArray> cipherTexts{QByteArray::fromHex("EA7162E65490B03B1AE4871FB35EF23B"),
  197. QByteArray::fromHex("549FF6C6274F034211C31FADF3F22571"),
  198. QByteArray::fromHex("CF222616B0E4F8E48967D769456B916B"),
  199. QByteArray::fromHex("957108025BFD57125B40057BC2DE4FE2"),
  200. QByteArray::fromHex("6F725C5950133F82EF021A94CADC8508")};
  201. SymmetricCipher cipher;
  202. for (int i = 0; i < keys.size(); ++i) {
  203. QVERIFY(cipher.init(SymmetricCipher::Twofish_CBC, SymmetricCipher::Encrypt, keys[i], ivs[i]));
  204. QByteArray ptNext = plainTexts[i];
  205. QByteArray ctPrev = ivs[i];
  206. QByteArray ctCur;
  207. QCOMPARE(cipher.blockSize(cipher.mode()), 16);
  208. for (int j = 0; j < 5000; ++j) {
  209. if (!cipher.process(ptNext)) {
  210. break;
  211. }
  212. ctCur = ptNext;
  213. ptNext = ctPrev;
  214. ctPrev = ctCur;
  215. if (!cipher.process(ptNext)) {
  216. break;
  217. }
  218. ctCur = ptNext;
  219. ptNext = ctPrev;
  220. ctPrev = ctCur;
  221. }
  222. QCOMPARE(ctCur, cipherTexts[i]);
  223. }
  224. }
  225. void TestSymmetricCipher::testTwofish256CbcDecryption()
  226. {
  227. // NIST MCT Known-Answer Tests (cbc_d_m.txt)
  228. // https://www.schneier.com/code/twofish-kat.zip
  229. QVector<QByteArray> keys{QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
  230. QByteArray::fromHex("1B1FE8F5A911CD4C0D800EDCE8ED0A942CBA6271A1044F90C30BA8FE91E1C163"),
  231. QByteArray::fromHex("EBA31FF8D2A24FDD769A937353E23257294A33394E4D17A668060AD8230811A1"),
  232. QByteArray::fromHex("1DCF1915C389AB273F80F897BF008F058ED89F58A95C1BE523C4B11295ED2D0F"),
  233. QByteArray::fromHex("491B9A66D3ED4EF19F02180289D5B1A1C2596AE568540A95DC5244198A9B8869")};
  234. QVector<QByteArray> ivs{QByteArray::fromHex("00000000000000000000000000000000"),
  235. QByteArray::fromHex("1B1FE8F5A911CD4C0D800EDCE8ED0A94"),
  236. QByteArray::fromHex("F0BCF70D7BB382917B1A9DAFBB0F38C3"),
  237. QByteArray::fromHex("F66C06ED112BE4FA491A6BE4ECE2BD52"),
  238. QByteArray::fromHex("54D483731064E5D6A082E09536D53EA4")};
  239. QVector<QByteArray> plainTexts{QByteArray::fromHex("2CBA6271A1044F90C30BA8FE91E1C163"),
  240. QByteArray::fromHex("05F05148EF495836AB0DA226B2E9D0C2"),
  241. QByteArray::fromHex("A792AC61E7110C434BC2BBCAB6E53CAE"),
  242. QByteArray::fromHex("4C81F5BDC1081170FF96F50B1F76A566"),
  243. QByteArray::fromHex("BD959F5B787037631A37051EA5F369F8")};
  244. QVector<QByteArray> cipherTexts{QByteArray::fromHex("00000000000000000000000000000000"),
  245. QByteArray::fromHex("2CBA6271A1044F90C30BA8FE91E1C163"),
  246. QByteArray::fromHex("05F05148EF495836AB0DA226B2E9D0C2"),
  247. QByteArray::fromHex("A792AC61E7110C434BC2BBCAB6E53CAE"),
  248. QByteArray::fromHex("4C81F5BDC1081170FF96F50B1F76A566")};
  249. SymmetricCipher cipher;
  250. for (int i = 0; i < keys.size(); ++i) {
  251. QVERIFY(cipher.init(SymmetricCipher::Twofish_CBC, SymmetricCipher::Decrypt, keys[i], ivs[i]));
  252. QByteArray ctNext = cipherTexts[i];
  253. QByteArray ptCur;
  254. QCOMPARE(cipher.blockSize(cipher.mode()), 16);
  255. for (int j = 0; j < 5000; ++j) {
  256. if (!cipher.process(ctNext)) {
  257. break;
  258. }
  259. if (!cipher.process(ctNext)) {
  260. break;
  261. }
  262. }
  263. QCOMPARE(ctNext, plainTexts[i]);
  264. }
  265. }
  266. void TestSymmetricCipher::testSalsa20()
  267. {
  268. // http://www.ecrypt.eu.org/stream/svn/viewcvs.cgi/ecrypt/trunk/submissions/salsa20/full/verified.test-vectors?logsort=rev&rev=210&view=markup
  269. QByteArray key = QByteArray::fromHex("F3F4F5F6F7F8F9FAFBFCFDFEFF000102030405060708090A0B0C0D0E0F101112");
  270. QByteArray iv = QByteArray::fromHex("0000000000000000");
  271. SymmetricCipher cipher;
  272. QVERIFY(cipher.init(SymmetricCipher::Salsa20, SymmetricCipher::Encrypt, key, iv));
  273. QByteArray cipherTextA;
  274. for (int i = 0; i < 8; i++) {
  275. QByteArray data(64, '\0');
  276. QVERIFY(cipher.process(data));
  277. cipherTextA.append(data);
  278. }
  279. // Re-initialize
  280. QVERIFY(cipher.init(SymmetricCipher::Salsa20, SymmetricCipher::Encrypt, key, iv));
  281. QByteArray cipherTextB(512, '\0');
  282. QVERIFY(cipher.process(cipherTextB));
  283. cipher.reset();
  284. QByteArray expectedCipherText1;
  285. expectedCipherText1.append(QByteArray::fromHex("B4C0AFA503BE7FC29A62058166D56F8F"));
  286. expectedCipherText1.append(QByteArray::fromHex("5D27DC246F75B9AD8760C8C39DFD8749"));
  287. expectedCipherText1.append(QByteArray::fromHex("2D3B76D5D9637F009EADA14458A52DFB"));
  288. expectedCipherText1.append(QByteArray::fromHex("09815337E72672681DDDC24633750D83"));
  289. QByteArray expectedCipherText2;
  290. expectedCipherText2.append(QByteArray::fromHex("DBBA0683DF48C335A9802EEF02522563"));
  291. expectedCipherText2.append(QByteArray::fromHex("54C9F763C3FDE19131A6BB7B85040624"));
  292. expectedCipherText2.append(QByteArray::fromHex("B1D6CD4BF66D16F7482236C8602A6D58"));
  293. expectedCipherText2.append(QByteArray::fromHex("505EEDCCA0B77AED574AB583115124B9"));
  294. QByteArray expectedCipherText3;
  295. expectedCipherText3.append(QByteArray::fromHex("F0C5F98BAE05E019764EF6B65E0694A9"));
  296. expectedCipherText3.append(QByteArray::fromHex("04CB9EC9C10C297B1AB1A6052365BB78"));
  297. expectedCipherText3.append(QByteArray::fromHex("E55D3C6CB9F06184BA7D425A92E7E987"));
  298. expectedCipherText3.append(QByteArray::fromHex("757FC5D9AFD7082418DD64125CA6F2B6"));
  299. QByteArray expectedCipherText4;
  300. expectedCipherText4.append(QByteArray::fromHex("5A5FB5C8F0AFEA471F0318A4A2792F7A"));
  301. expectedCipherText4.append(QByteArray::fromHex("A5C67B6D6E0F0DDB79961C34E3A564BA"));
  302. expectedCipherText4.append(QByteArray::fromHex("2EECE78D9AFF45E510FEAB1030B102D3"));
  303. expectedCipherText4.append(QByteArray::fromHex("9DFCECB77F5798F7D2793C0AB09C7A04"));
  304. QCOMPARE(cipherTextA.mid(0, 64), expectedCipherText1);
  305. QCOMPARE(cipherTextA.mid(192, 64), expectedCipherText2);
  306. QCOMPARE(cipherTextA.mid(256, 64), expectedCipherText3);
  307. QCOMPARE(cipherTextA.mid(448, 64), expectedCipherText4);
  308. QCOMPARE(cipherTextB.mid(0, 64), expectedCipherText1);
  309. QCOMPARE(cipherTextB.mid(192, 64), expectedCipherText2);
  310. QCOMPARE(cipherTextB.mid(256, 64), expectedCipherText3);
  311. QCOMPARE(cipherTextB.mid(448, 64), expectedCipherText4);
  312. }
  313. void TestSymmetricCipher::testChaCha20()
  314. {
  315. // https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7
  316. {
  317. QByteArray key = QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000");
  318. QByteArray iv = QByteArray::fromHex("0000000000000000");
  319. SymmetricCipher cipher;
  320. QByteArray data(64, 0);
  321. QVERIFY(cipher.init(SymmetricCipher::ChaCha20, SymmetricCipher::Encrypt, key, iv));
  322. QVERIFY(cipher.process(data));
  323. QCOMPARE(data,
  324. QByteArray::fromHex("76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7"
  325. "724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586"));
  326. }
  327. {
  328. QByteArray key = QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000001");
  329. QByteArray iv = QByteArray::fromHex("0000000000000000");
  330. SymmetricCipher cipher;
  331. QByteArray data(64, 0);
  332. QVERIFY(cipher.init(SymmetricCipher::ChaCha20, SymmetricCipher::Encrypt, key, iv));
  333. QVERIFY(cipher.process(data));
  334. QCOMPARE(data,
  335. QByteArray::fromHex("4540f05a9f1fb296d7736e7b208e3c96eb4fe1834688d2604f450952ed432d41bbe2a0b6ea7566d2a"
  336. "5d1e7e20d42af2c53d792b1c43fea817e9ad275ae546963"));
  337. }
  338. {
  339. QByteArray key = QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000");
  340. QByteArray iv = QByteArray::fromHex("0000000000000001");
  341. SymmetricCipher cipher;
  342. QByteArray data(60, 0);
  343. QVERIFY(cipher.init(SymmetricCipher::ChaCha20, SymmetricCipher::Encrypt, key, iv));
  344. QVERIFY(cipher.process(data));
  345. QCOMPARE(data,
  346. QByteArray::fromHex("de9cba7bf3d69ef5e786dc63973f653a0b49e015adbff7134fcb7df137821031e85a050278a708452"
  347. "7214f73efc7fa5b5277062eb7a0433e445f41e3"));
  348. }
  349. {
  350. QByteArray key = QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000");
  351. QByteArray iv = QByteArray::fromHex("0100000000000000");
  352. SymmetricCipher cipher;
  353. QByteArray data(64, 0);
  354. QVERIFY(cipher.init(SymmetricCipher::ChaCha20, SymmetricCipher::Encrypt, key, iv));
  355. QVERIFY(cipher.process(data));
  356. QCOMPARE(data,
  357. QByteArray::fromHex("ef3fdfd6c61578fbf5cf35bd3dd33b8009631634d21e42ac33960bd138e50d32111e4caf237ee53ca"
  358. "8ad6426194a88545ddc497a0b466e7d6bbdb0041b2f586b"));
  359. }
  360. }
  361. void TestSymmetricCipher::testPadding()
  362. {
  363. QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
  364. QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f");
  365. QByteArray plainText = QByteArray::fromHex("6bc1bee22e409f96e93d");
  366. QBuffer buffer;
  367. buffer.open(QIODevice::ReadWrite);
  368. SymmetricCipherStream streamEnc(&buffer);
  369. QVERIFY(streamEnc.init(SymmetricCipher::Aes256_CBC, SymmetricCipher::Encrypt, key, iv));
  370. streamEnc.open(QIODevice::WriteOnly);
  371. streamEnc.write(plainText);
  372. streamEnc.close();
  373. buffer.reset();
  374. // make sure padding is written
  375. QCOMPARE(buffer.buffer().size(), 16);
  376. SymmetricCipherStream streamDec(&buffer);
  377. QVERIFY(streamDec.init(SymmetricCipher::Aes256_CBC, SymmetricCipher::Decrypt, key, iv));
  378. streamDec.open(QIODevice::ReadOnly);
  379. QByteArray decrypted = streamDec.readAll();
  380. QCOMPARE(decrypted, plainText);
  381. }
  382. void TestSymmetricCipher::testStreamReset()
  383. {
  384. QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
  385. QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f");
  386. QBuffer buffer;
  387. QVERIFY(buffer.open(QIODevice::WriteOnly));
  388. SymmetricCipherStream writer(&buffer);
  389. QVERIFY(writer.init(SymmetricCipher::Aes256_CBC, SymmetricCipher::Encrypt, key, iv));
  390. QVERIFY(writer.open(QIODevice::WriteOnly));
  391. QCOMPARE(writer.write(QByteArray(4, 'Z')), qint64(4));
  392. // test if reset() and close() write only one block
  393. QVERIFY(writer.reset());
  394. QVERIFY(writer.reset());
  395. writer.close();
  396. QCOMPARE(buffer.buffer().size(), 16);
  397. }