aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_passhash.cpp
diff options
context:
space:
mode:
authorMatej Kenda <[email protected]>2015-11-19 16:51:27 +0100
committerMatej Kenda <[email protected]>2015-11-19 16:51:27 +0100
commitfb103b7d1fe333b3d7424a36ea2f9b90df8b49ef (patch)
tree4606a4ba9cc981cc0d975f0ec7724d060424e5e1 /src/tests/test_passhash.cpp
parent94d89769739ebe05e048f217b03672fb0c336fca (diff)
parent9bff61f4c577661bf4a62a860baf190d4ea8ed6a (diff)
Merge branch 'master' of github.com:randombit/botan into fix_algo_registry_locking_windows
Diffstat (limited to 'src/tests/test_passhash.cpp')
-rw-r--r--src/tests/test_passhash.cpp139
1 files changed, 72 insertions, 67 deletions
diff --git a/src/tests/test_passhash.cpp b/src/tests/test_passhash.cpp
index 4bc69125b..e9606062c 100644
--- a/src/tests/test_passhash.cpp
+++ b/src/tests/test_passhash.cpp
@@ -6,94 +6,99 @@
#include "tests.h"
-#include <iostream>
+#if defined(BOTAN_HAS_BCRYPT)
+ #include <botan/bcrypt.h>
+#endif
#if defined(BOTAN_HAS_PASSHASH9)
#include <botan/passhash9.h>
#endif
-#if defined(BOTAN_HAS_BCRYPT)
- #include <botan/bcrypt.h>
-#endif
-
-using namespace Botan;
+namespace Botan_Tests {
-size_t test_bcrypt()
- {
- size_t fails = 0;
+namespace {
#if defined(BOTAN_HAS_BCRYPT)
+class Bcrypt_Tests : public Text_Based_Test
+ {
+ public:
+ Bcrypt_Tests() : Text_Based_Test(Test::data_file("bcrypt.vec"), {"Password","Passhash"}) {}
- // Generated by jBCrypt 0.3
- if(!check_bcrypt("abc", "$2a$05$DfPyLs.G6.To9fXEFgUL1O6HpYw3jIXgPcl/L3Qt3jESuWmhxtmpS"))
- {
- std::cout << "Bcrypt test 1 failed" << std::endl;
- fails++;
- }
-
- // http://www.openwall.com/lists/john-dev/2011/06/19/2
- if(!check_bcrypt("\xA3",
- "$2a$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq"))
- {
- std::cout << "Bcrypt test 2 failed" << std::endl;
- fails++;
- }
-
- auto& rng = test_rng();
-
- for(u16bit level = 1; level != 5; ++level)
- {
- const std::string input = "some test passphrase 123";
- const std::string gen_hash = generate_bcrypt(input, rng, level);
-
- if(!check_bcrypt(input, gen_hash))
+ Test::Result run_one_test(const std::string&, const VarMap& vars) override
{
- std::cout << "Gen and check for bcrypt failed: " << gen_hash << " not valid" << std::endl;
- ++fails;
- }
- }
+ // Encoded as binary so we can test binary inputs
+ const std::vector<byte> password_vec = get_req_bin(vars, "Password");
+ const std::string password(reinterpret_cast<const char*>(password_vec.data()),
+ password_vec.size());
- test_report("Bcrypt", 6, fails);
+ const std::string passhash = get_req_str(vars, "Passhash");
-#endif
+ Test::Result result("bcrypt");
+ result.test_eq("correct hash accepted", Botan::check_bcrypt(password, passhash), true);
- return fails;
- }
+ const size_t max_level = 1 + std::min<size_t>(Test::soak_level() / 2, 10);
-size_t test_passhash9()
- {
- size_t fails = 0;
+ for(size_t level = 1; level <= max_level; ++level)
+ {
+ const std::string gen_hash = generate_bcrypt(password, Test::rng(), level);
+ result.test_eq("generated hash accepted", Botan::check_bcrypt(password, gen_hash), true);
+ }
-#if defined(BOTAN_HAS_PASSHASH9)
- const std::string input = "secret";
- const std::string fixed_hash =
- "$9$AAAKhiHXTIUhNhbegwBXJvk03XXJdzFMy+i3GFMIBYKtthTTmXZA";
-
- size_t ran = 0;
+ return result;
+ }
+ };
- ++ran;
- if(!check_passhash9(input, fixed_hash))
- {
- std::cout << "Passhash9 fixed input test failed" << std::endl;
- fails++;
- }
+BOTAN_REGISTER_TEST("bcrypt", Bcrypt_Tests);
- auto& rng = test_rng();
+#endif
- for(byte alg_id = 0; alg_id <= 4; ++alg_id)
- {
- std::string gen_hash = generate_passhash9(input, rng, 2, alg_id);
+#if defined(BOTAN_HAS_PASSHASH9)
+class Passhash9_Tests : public Text_Based_Test
+ {
+ public:
+ Passhash9_Tests() : Text_Based_Test(Test::data_file("passhash9.vec"), {"Password","Passhash"}) {}
- ++ran;
- if(!check_passhash9(input, gen_hash))
+ Test::Result run_one_test(const std::string&, const VarMap& vars) override
{
- std::cout << "Passhash9 gen and check " << static_cast<int>(alg_id) << " failed" << std::endl;
- ++fails;
+ // Encoded as binary so we can test binary inputs
+ const std::vector<byte> password_vec = get_req_bin(vars, "Password");
+ const std::string password(reinterpret_cast<const char*>(password_vec.data()),
+ password_vec.size());
+
+ const std::string passhash = get_req_str(vars, "Passhash");
+
+ Test::Result result("passhash9");
+ result.test_eq("correct hash accepted", Botan::check_passhash9(password, passhash), true);
+
+ for(byte alg_id = 0; alg_id <= 4; ++alg_id)
+ {
+ const std::string gen_hash = Botan::generate_passhash9(password, Test::rng(), 2, alg_id);
+
+ if(!result.test_eq("generated hash accepted", Botan::check_passhash9(password, gen_hash), true))
+ {
+ result.test_note("hash was " + gen_hash);
+ }
+ }
+
+ const size_t max_level = 1 + std::min<size_t>(Test::soak_level() / 2, 10);
+
+ for(size_t level = 1; level <= max_level; ++level)
+ {
+ const std::string gen_hash = Botan::generate_passhash9(password, Test::rng(), level);
+ if(!result.test_eq("generated hash accepted", Botan::check_passhash9(password, gen_hash), true))
+ {
+ result.test_note("hash was " + gen_hash);
+ }
+ }
+
+ return result;
}
- }
+ };
+
+BOTAN_REGISTER_TEST("passhash9", Passhash9_Tests);
- test_report("Passhash9", ran, fails);
#endif
- return fails;
- }
+}
+
+}