TestKeePass2RandomStream.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "TestKeePass2RandomStream.h"
  18. #include "crypto/Crypto.h"
  19. #include "crypto/CryptoHash.h"
  20. #include "crypto/SymmetricCipher.h"
  21. #include "format/KeePass2.h"
  22. #include "format/KeePass2RandomStream.h"
  23. #include <QTest>
  24. QTEST_GUILESS_MAIN(TestKeePass2RandomStream)
  25. void TestKeePass2RandomStream::initTestCase()
  26. {
  27. QVERIFY(Crypto::init());
  28. }
  29. void TestKeePass2RandomStream::test()
  30. {
  31. const QByteArray key("\x11\x22\x33\x44\x55\x66\x77\x88");
  32. const int Size = 128;
  33. SymmetricCipher cipher;
  34. QVERIFY(cipher.init(SymmetricCipher::Salsa20,
  35. SymmetricCipher::Encrypt,
  36. CryptoHash::hash(key, CryptoHash::Sha256),
  37. KeePass2::INNER_STREAM_SALSA20_IV));
  38. const QByteArray data(QByteArray::fromHex("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5"
  39. "2b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6"
  40. "1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e94"
  41. "1e36b26bd1ebc670d1bd1d665620abf74f78a7f6d29809585a97daec58c6b050"));
  42. QByteArray cipherPad;
  43. cipherPad.fill('\0', Size);
  44. QVERIFY(cipher.process(cipherPad));
  45. QByteArray cipherData;
  46. cipherData.resize(Size);
  47. for (int i = 0; i < Size; i++) {
  48. cipherData[i] = data[i] ^ cipherPad[i];
  49. }
  50. KeePass2RandomStream randomStream;
  51. bool ok;
  52. QVERIFY(randomStream.init(SymmetricCipher::Salsa20, key));
  53. QByteArray randomStreamData;
  54. randomStreamData.append(randomStream.process(data.mid(0, 7), &ok));
  55. QVERIFY(ok);
  56. randomStreamData.append(randomStream.process(data.mid(7, 1), &ok));
  57. QVERIFY(ok);
  58. QByteArray tmpData = data.mid(8, 12);
  59. QVERIFY(randomStream.processInPlace(tmpData));
  60. randomStreamData.append(tmpData);
  61. randomStreamData.append(randomStream.process(data.mid(20, 44), &ok));
  62. QVERIFY(ok);
  63. randomStreamData.append(randomStream.process(data.mid(64, 64), &ok));
  64. QVERIFY(ok);
  65. SymmetricCipher cipherEncrypt;
  66. QVERIFY(cipherEncrypt.init(SymmetricCipher::Salsa20,
  67. SymmetricCipher::Encrypt,
  68. CryptoHash::hash(key, CryptoHash::Sha256),
  69. KeePass2::INNER_STREAM_SALSA20_IV));
  70. QByteArray cipherDataEncrypt = data;
  71. QVERIFY(cipherEncrypt.process(cipherDataEncrypt));
  72. QCOMPARE(randomStreamData.size(), Size);
  73. QCOMPARE(cipherData, cipherDataEncrypt);
  74. QCOMPARE(randomStreamData, cipherData);
  75. }