TestTotp.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2017 Weslly Honorato <weslly@protonmail.com>
  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 "TestTotp.h"
  19. #include "TestGlobal.h"
  20. #include "crypto/Crypto.h"
  21. #include "totp/totp.h"
  22. QTEST_GUILESS_MAIN(TestTotp)
  23. void TestTotp::initTestCase()
  24. {
  25. QVERIFY(Crypto::init());
  26. }
  27. void TestTotp::testParseSecret()
  28. {
  29. // OTP URL Parsing
  30. QString secret = "otpauth://totp/"
  31. "ACME%20Co:john@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm="
  32. "SHA1&digits=6&period=30";
  33. auto settings = Totp::parseSettings(secret);
  34. QVERIFY(!settings.isNull());
  35. QCOMPARE(settings->key, QString("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ"));
  36. QCOMPARE(settings->custom, false);
  37. QCOMPARE(settings->format, Totp::StorageFormat::OTPURL);
  38. QCOMPARE(settings->digits, 6u);
  39. QCOMPARE(settings->step, 30u);
  40. QCOMPARE(settings->algorithm, Totp::Algorithm::Sha1);
  41. // OTP URL with non-default hash type
  42. secret = "otpauth://totp/"
  43. "ACME%20Co:john@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm="
  44. "SHA512&digits=6&period=30";
  45. settings = Totp::parseSettings(secret);
  46. QVERIFY(!settings.isNull());
  47. QCOMPARE(settings->key, QString("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ"));
  48. QCOMPARE(settings->custom, true);
  49. QCOMPARE(settings->format, Totp::StorageFormat::OTPURL);
  50. QCOMPARE(settings->digits, 6u);
  51. QCOMPARE(settings->step, 30u);
  52. QCOMPARE(settings->algorithm, Totp::Algorithm::Sha512);
  53. // KeeOTP Parsing
  54. secret = "key=HXDMVJECJJWSRBY%3d&step=25&size=8&otpHashMode=Sha256";
  55. settings = Totp::parseSettings(secret);
  56. QVERIFY(!settings.isNull());
  57. QCOMPARE(settings->key, QString("HXDMVJECJJWSRBY="));
  58. QCOMPARE(settings->custom, true);
  59. QCOMPARE(settings->format, Totp::StorageFormat::KEEOTP);
  60. QCOMPARE(settings->digits, 8u);
  61. QCOMPARE(settings->step, 25u);
  62. QCOMPARE(settings->algorithm, Totp::Algorithm::Sha256);
  63. // Semi-colon delineated "TOTP Settings"
  64. secret = "gezdgnbvgy3tqojqgezdgnbvgy3tqojq";
  65. settings = Totp::parseSettings("30;8", secret);
  66. QVERIFY(!settings.isNull());
  67. QCOMPARE(settings->key, QString("gezdgnbvgy3tqojqgezdgnbvgy3tqojq"));
  68. QCOMPARE(settings->custom, true);
  69. QCOMPARE(settings->format, Totp::StorageFormat::LEGACY);
  70. QCOMPARE(settings->digits, 8u);
  71. QCOMPARE(settings->step, 30u);
  72. QCOMPARE(settings->algorithm, Totp::Algorithm::Sha1);
  73. // Bare secret (no "TOTP Settings" attribute)
  74. secret = "gezdgnbvgy3tqojqgezdgnbvgy3tqojq";
  75. settings = Totp::parseSettings("", secret);
  76. QVERIFY(!settings.isNull());
  77. QCOMPARE(settings->key, QString("gezdgnbvgy3tqojqgezdgnbvgy3tqojq"));
  78. QCOMPARE(settings->custom, false);
  79. QCOMPARE(settings->format, Totp::StorageFormat::LEGACY);
  80. QCOMPARE(settings->digits, 6u);
  81. QCOMPARE(settings->step, 30u);
  82. QCOMPARE(settings->algorithm, Totp::Algorithm::Sha1);
  83. }
  84. void TestTotp::testTotpCode()
  85. {
  86. // Test vectors from RFC 6238
  87. // https://tools.ietf.org/html/rfc6238#appendix-B
  88. auto settings = Totp::createSettings("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", Totp::DEFAULT_DIGITS, Totp::DEFAULT_STEP);
  89. // Test 6 digit TOTP (default)
  90. quint64 time = 1234567890;
  91. QCOMPARE(Totp::generateTotp(settings, time), QString("005924"));
  92. time = 1111111109;
  93. QCOMPARE(Totp::generateTotp(settings, time), QString("081804"));
  94. // Test 8 digit TOTP (custom)
  95. settings->digits = 8;
  96. settings->custom = true;
  97. time = 1111111111;
  98. QCOMPARE(Totp::generateTotp(settings, time), QString("14050471"));
  99. time = 2000000000;
  100. QCOMPARE(Totp::generateTotp(settings, time), QString("69279037"));
  101. }
  102. void TestTotp::testSteamTotp()
  103. {
  104. // OTP URL Parsing
  105. QString secret = "otpauth://totp/"
  106. "test:test@example.com?secret=63BEDWCQZKTQWPESARIERL5DTTQFCJTK&issuer=Valve&algorithm="
  107. "SHA1&digits=5&period=30&encoder=steam";
  108. auto settings = Totp::parseSettings(secret);
  109. QCOMPARE(settings->key, QString("63BEDWCQZKTQWPESARIERL5DTTQFCJTK"));
  110. QCOMPARE(settings->encoder.shortName, Totp::STEAM_SHORTNAME);
  111. QCOMPARE(settings->format, Totp::StorageFormat::OTPURL);
  112. QCOMPARE(settings->digits, Totp::STEAM_DIGITS);
  113. QCOMPARE(settings->step, 30u);
  114. // These time/value pairs were created by running the Steam Guard function of the
  115. // Steam mobile app with a throw-away steam account. The above secret was extracted
  116. // from the Steam app's data for use in testing here.
  117. quint64 time = 1511200518;
  118. QCOMPARE(Totp::generateTotp(settings, time), QString("FR8RV"));
  119. time = 1511200714;
  120. QCOMPARE(Totp::generateTotp(settings, time), QString("9P3VP"));
  121. }
  122. void TestTotp::testEntryHistory()
  123. {
  124. Entry entry;
  125. uint step = 16;
  126. uint digits = 6;
  127. auto settings = Totp::createSettings("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", digits, step);
  128. // Test that entry starts without TOTP
  129. QCOMPARE(entry.historyItems().size(), 0);
  130. QVERIFY(!entry.hasTotp());
  131. // Add TOTP to entry
  132. entry.setTotp(settings);
  133. QCOMPARE(entry.historyItems().size(), 1);
  134. QVERIFY(entry.hasTotp());
  135. QCOMPARE(entry.totpSettings()->key, QString("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"));
  136. // Change key and verify settings changed
  137. settings->key = "foo";
  138. entry.setTotp(settings);
  139. QCOMPARE(entry.historyItems().size(), 2);
  140. QCOMPARE(entry.totpSettings()->key, QString("foo"));
  141. }