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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "tests.h"
#include <botan/hash.h>
#if defined (BOTAN_HAS_PARALLEL_HASH)
#include <botan/par_hash.h>
#endif
namespace Botan_Tests {
namespace {
class Hash_Function_Tests : public Text_Based_Test
{
public:
Hash_Function_Tests() : Text_Based_Test("hash", {"In", "Out"}) {}
Test::Result run_one_test(const std::string& algo, const VarMap& vars) override
{
const std::vector<uint8_t> input = get_req_bin(vars, "In");
const std::vector<uint8_t> expected = get_req_bin(vars, "Out");
Test::Result result(algo);
const std::vector<std::string> providers = Botan::HashFunction::providers(algo);
if(providers.empty())
{
result.note_missing("hash " + algo);
return result;
}
for(auto&& provider: providers)
{
std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(algo, provider));
if(!hash)
{
result.note_missing(algo + " from " + provider);
continue;
}
result.test_eq(provider, hash->name(), algo);
hash->update(input);
result.test_eq(provider, "hashing", hash->final(), expected);
// Test to make sure clear() resets what we need it to
hash->update("some discarded input");
hash->clear();
hash->update(nullptr, 0); // this should be effectively ignored
hash->update(input);
result.test_eq(provider, "hashing after clear", hash->final(), expected);
if(input.size() > 1)
{
hash->update(input[0]);
hash->update(&input[1], input.size() - 1);
result.test_eq(provider, "hashing split", hash->final(), expected);
}
}
return result;
}
};
BOTAN_REGISTER_TEST("hash", Hash_Function_Tests);
#if defined(BOTAN_HAS_PARALLEL_HASH)
Test::Result test_clone()
{
Test::Result result("Parallel hash");
std::string algo = "Parallel(MD5,SHA-160)";
std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(algo));
if(!hash)
{
result.note_missing(algo);
return result;
}
hash->update("");
result.test_eq("Parallel hashing", hash->final(), "D41D8CD98F00B204E9800998ECF8427EDA39A3EE5E"
"6B4B0D3255BFEF95601890AFD80709");
std::unique_ptr<Botan::HashFunction> hash_clone(hash->clone());
hash_clone->clear();
hash_clone->update("");
result.test_eq("Parallel hashing (clone)", hash_clone->final(), "D41D8CD98F00B204E9800998ECF8427"
"EDA39A3EE5E6B4B0D3255BFEF95601890AFD80709");
return result;
}
Test::Result test_ctor()
{
Test::Result result("Parallel hash");
std::unique_ptr<Botan::HashFunction> sha256(Botan::HashFunction::create("SHA-256"));
if(!sha256)
{
result.note_missing("SHA-256");
return result;
}
std::unique_ptr<Botan::HashFunction> sha512(Botan::HashFunction::create("SHA-512"));
if(!sha512)
{
result.note_missing("SHA-512");
return result;
}
std::vector<Botan::HashFunction*> hashes = { sha256.get(), sha512.get() };
Botan::Parallel par_hash(hashes);
par_hash.update("");
result.test_eq("Parallel hashing", par_hash.final(), "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B"
"934CA495991B7852B855CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9C"
"E47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E");
return result;
}
class Parallel_Hash_Tests : public Test
{
public:
std::vector<Test::Result> run() override
{
std::vector<Test::Result> results;
std::vector<std::function<Test::Result()>> fns =
{
test_clone,
test_ctor
};
for(size_t i = 0; i != fns.size(); ++i)
{
try
{
results.push_back(fns[ i ]());
}
catch(std::exception& e)
{
results.push_back(Test::Result::Failure("Parallel hash tests " + std::to_string(i), e.what()));
}
}
return results;
}
};
BOTAN_REGISTER_TEST("par_hash", Parallel_Hash_Tests);
#endif
}
}
|