1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/*
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "tests.h"
#if defined(BOTAN_HAS_BCRYPT)
#include <botan/bcrypt.h>
#endif
#if defined(BOTAN_HAS_PASSHASH9)
#include <botan/passhash9.h>
#endif
namespace Botan_Tests {
namespace {
#if defined(BOTAN_HAS_BCRYPT)
class Bcrypt_Tests final : public Text_Based_Test
{
public:
Bcrypt_Tests() : Text_Based_Test("passhash/bcrypt.vec", "Password,Passhash") {}
Test::Result run_one_test(const std::string&, const VarMap& vars) override
{
// Encoded as binary so we can test binary inputs
const std::vector<uint8_t> password_vec = vars.get_req_bin("Password");
const std::string password(reinterpret_cast<const char*>(password_vec.data()),
password_vec.size());
const std::string passhash = vars.get_req_str("Passhash");
Test::Result result("bcrypt");
result.test_eq("correct hash accepted", Botan::check_bcrypt(password, passhash), true);
// self-test low levels for each test password
for(uint16_t level = 4; level <= 6; ++level)
{
const std::string gen_hash = Botan::generate_bcrypt(password, Test::rng(), level);
result.test_eq("generated hash accepted", Botan::check_bcrypt(password, gen_hash), true);
}
return result;
}
std::vector<Test::Result> run_final_tests() override
{
Test::Result result("bcrypt");
uint64_t start = Test::timestamp();
const std::string password = "ag00d1_2BE5ur3";
const uint16_t max_level = (Test::run_long_tests() ? 15 : 10);
for(uint16_t level = 4; level <= max_level; ++level)
{
const std::string gen_hash = Botan::generate_bcrypt(password, Test::rng(), level);
result.test_eq("generated hash accepted", Botan::check_bcrypt(password, gen_hash), true);
}
result.test_throws("Invalid bcrypt version rejected",
"Unknown bcrypt version 'q'",
[]() { Botan::generate_bcrypt("pass", Test::rng(), 4, 'q'); });
result.set_ns_consumed(Test::timestamp() - start);
return {result};
}
};
BOTAN_REGISTER_TEST("bcrypt", Bcrypt_Tests);
#endif
#if defined(BOTAN_HAS_PASSHASH9)
class Passhash9_Tests final : public Text_Based_Test
{
public:
Passhash9_Tests() : Text_Based_Test("passhash/passhash9.vec", "Password,Passhash,PRF") {}
Test::Result run_one_test(const std::string&, const VarMap& vars) override
{
// Encoded as binary so we can test binary inputs
const std::vector<uint8_t> password_vec = vars.get_req_bin("Password");
const std::string password(reinterpret_cast<const char*>(password_vec.data()),
password_vec.size());
const std::string passhash = vars.get_req_str("Passhash");
const std::size_t prf = vars.get_req_sz("PRF");
Test::Result result("passhash9");
if(Botan::is_passhash9_alg_supported(uint8_t(prf)))
{
result.test_eq("correct hash accepted", Botan::check_passhash9(password, passhash), true);
}
for(uint8_t alg_id = 0; alg_id <= 4; ++alg_id)
{
if(Botan::is_passhash9_alg_supported(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 uint16_t max_level = (Test::run_long_tests() ? 14 : 8);
for(uint16_t level = 1; level <= max_level; ++level)
{
const uint8_t alg_id = 1; // default used by generate_passhash9()
if(Botan::is_passhash9_alg_supported(alg_id))
{
const std::string gen_hash = Botan::generate_passhash9(password, Test::rng(), level, alg_id);
if(!result.test_eq("generated hash accepted", Botan::check_passhash9(password, gen_hash), true))
{
result.test_note("hash was " + gen_hash);
}
}
}
return result;
}
std::vector<Test::Result> run_final_tests() override
{
Test::Result result("passhash9");
result.confirm("Unknown algorithm is unknown",
Botan::is_passhash9_alg_supported(255) == false);
result.test_throws("Throws if algorithm not supported",
"Passhash9: Algorithm id 255 is not defined",
[]() { Botan::generate_passhash9("pass", Test::rng(), 3, 255); });
result.test_throws("Throws if iterations is too high",
"Requested passhash9 work factor 513 is too large",
[]() { Botan::check_passhash9("floof", "$9$AgIB3c5J3kvAuML84sZ5hWT9WzJtiYRPLCEARaujS7I6IKbNCwp0"); });
return {result};
}
};
BOTAN_REGISTER_TEST("passhash9", Passhash9_Tests);
#endif
}
}
|