aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_hash.cpp
blob: 80bf5224db9727b88926e4ff559aa1ba62794aec (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* (C) 2014,2015,2018,2019 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_HASH)
   #include <botan/hash.h>
#endif

namespace Botan_Tests {

#if defined(BOTAN_HAS_HASH)

namespace {

class Invalid_Hash_Name_Tests final : public Test
   {
   public:
      std::vector<Test::Result> run() override
         {
         Test::Result result("Invalid HashFunction names");
         test_invalid_name(result, "NonExistentHash");
         test_invalid_name(result, "Blake2b(9)", "Bad output bits size for BLAKE2b");
         test_invalid_name(result, "Comb4P(MD5,MD5)", "Comb4P: Must use two distinct hashes");
         test_invalid_name(result, "Comb4P(MD5,SHA-256)", "Comb4P: Incompatible hashes MD5 and SHA-256");
         test_invalid_name(result, "Keccak-1600(160)", "Keccak_1600: Invalid output length 160");
         test_invalid_name(result, "SHA-3(160)", "SHA_3: Invalid output length 160");

         return {result};
         }

   private:
      void test_invalid_name(Result& result,
                             const std::string& name,
                             const std::string& expected_msg = "") const
         {
         try
            {
            auto hash = Botan::HashFunction::create_or_throw(name);
            result.test_failure("Was successfully able to create " + name);
            }
         catch(Botan::Invalid_Argument& e)
            {
            const std::string msg = e.what();
            const std::string full_msg = "" + expected_msg;
            result.test_eq("expected error message", msg, full_msg);
            }
         catch(Botan::Lookup_Error& e)
            {
            const std::string algo_not_found_msg = "Unavailable Hash " + name;
            const std::string msg = e.what();
            result.test_eq("expected error message", msg, algo_not_found_msg);
            }
         catch(std::exception& e)
            {
            result.test_failure("some unknown exception", e.what());
            }
         catch(...)
            {
            result.test_failure("some unknown exception");
            }
         }
   };

BOTAN_REGISTER_TEST("hash", "invalid_name_hash", Invalid_Hash_Name_Tests);

class Hash_Function_Tests final : public Text_Based_Test
   {
   public:
      Hash_Function_Tests() : Text_Based_Test("hash", "In,Out") {}

      std::vector<std::string> possible_providers(const std::string& algo) override
         {
         return provider_filter(Botan::HashFunction::providers(algo));
         }

      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override
         {
         const std::vector<uint8_t> input    = vars.get_req_bin("In");
         const std::vector<uint8_t> expected = vars.get_req_bin("Out");

         Test::Result result(algo);

         const std::vector<std::string> providers = possible_providers(algo);

         if(providers.empty())
            {
            result.note_missing("hash " + algo);
            return result;
            }

         for(auto const& provider_ask : providers)
            {
            std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(algo, provider_ask));

            if(!hash)
               {
               result.test_failure("Hash " + algo + " supported by " + provider_ask + " but not found");
               continue;
               }

            std::unique_ptr<Botan::HashFunction> clone(hash->clone());

            const std::string provider(hash->provider());
            result.test_is_nonempty("provider", provider);
            result.test_eq(provider, hash->name(), algo);
            result.test_eq(provider, hash->name(), clone->name());

            hash->update(input);
            result.test_eq(provider, "hashing", hash->final(), expected);

            clone->update(input);
            result.test_eq(provider, "hashing (clone)", clone->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);

            // Test that misaligned inputs work

            if(input.size() > 0)
               {
               std::vector<uint8_t> misaligned = input;
               const size_t current_alignment = reinterpret_cast<uintptr_t>(misaligned.data()) % 16;

               const size_t bytes_to_misalign = 15 - current_alignment;

               for(size_t i = 0; i != bytes_to_misalign; ++i)
                  misaligned.insert(misaligned.begin(), 0x23);

               hash->update(&misaligned[bytes_to_misalign], input.size());
               result.test_eq(provider, "hashing misaligned data", hash->final(), expected);
               }

            if(input.size() > 5)
               {
               hash->update(input[0]);

               std::unique_ptr<Botan::HashFunction> fork = hash->copy_state();
               // verify fork copy doesn't affect original computation
               fork->update(&input[1], input.size() - 2);

               size_t so_far = 1;
               while(so_far < input.size())
                  {
                  size_t take = Test::rng().next_byte() % (input.size() - so_far);

                  if(input.size() - so_far == 1)
                     take = 1;

                  hash->update(&input[so_far], take);
                  so_far += take;
                  }
               result.test_eq(provider, "hashing split", hash->final(), expected);

               fork->update(&input[input.size() - 1], 1);
               result.test_eq(provider, "hashing split", fork->final(), expected);
               }

            if(hash->hash_block_size() > 0)
               {
               // GOST-34.11 uses 32 byte block
               result.test_gte("If hash_block_size is set, it is large", hash->hash_block_size(), 32);
               }
            }

         return result;
         }

   };

BOTAN_REGISTER_TEST("hash", "hash", Hash_Function_Tests);

class Hash_NIST_MonteCarlo_Tests final : public Text_Based_Test
   {
   public:
      Hash_NIST_MonteCarlo_Tests() : Text_Based_Test("hash_mc.vec", "Seed,Count,Output") {}

      std::vector<std::string> possible_providers(const std::string& algo) override
         {
         return provider_filter(Botan::HashFunction::providers(algo));
         }

      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override
         {
         const std::vector<uint8_t> seed = vars.get_req_bin("Seed");
         const size_t count = vars.get_req_sz("Count");
         const std::vector<uint8_t> expected = vars.get_req_bin("Output");

         Test::Result result("NIST Monte Carlo " + algo);

         const std::vector<std::string> providers = possible_providers(algo);

         if(providers.empty())
            {
            result.note_missing("hash " + algo);
            return result;
            }

         for(auto const& provider_ask : providers)
            {
            std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(algo, provider_ask));

            if(!hash)
               {
               result.test_failure("Hash " + algo + " supported by " + provider_ask + " but not found");
               continue;
               }

            std::vector<std::vector<uint8_t>> input;
            input.push_back(seed);
            input.push_back(seed);
            input.push_back(seed);

            std::vector<uint8_t> buf(hash->output_length());

            for(size_t j = 0; j <= count; ++j)
               {
               for(size_t i = 3; i != 1003; ++i)
                  {
                  hash->update(input[0]);
                  hash->update(input[1]);
                  hash->update(input[2]);

                  hash->final(input[0].data());
                  input[0].swap(input[1]);
                  input[1].swap(input[2]);
                  }

               if(j < count)
                  {
                  input[0] = input[2];
                  input[1] = input[2];
                  }
               }

            result.test_eq("Output is expected", input[2], expected);
            }

         return result;
         }
   };

BOTAN_REGISTER_TEST("hash", "hash_nist_mc", Hash_NIST_MonteCarlo_Tests);

class Hash_LongRepeat_Tests final : public Text_Based_Test
   {
   public:
      Hash_LongRepeat_Tests() : Text_Based_Test("hash_rep.vec", "Input,TotalLength,Digest") {}

      std::vector<std::string> possible_providers(const std::string& algo) override
         {
         return provider_filter(Botan::HashFunction::providers(algo));
         }

      // repeating the output several times reduces buffering overhead during processing
      static std::vector<uint8_t> expand_input(const std::vector<uint8_t>& input, size_t min_len)
         {
         std::vector<uint8_t> output;
         output.reserve(min_len);

         while(output.size() < min_len)
            output.insert(output.end(), input.begin(), input.end());

         return output;
         }

      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override
         {
         const std::vector<uint8_t> input = expand_input(vars.get_req_bin("Input"), 256);
         const size_t total_len = vars.get_req_sz("TotalLength");
         const std::vector<uint8_t> expected = vars.get_req_bin("Digest");

         Test::Result result("Long input " + algo);

         const std::vector<std::string> providers = possible_providers(algo);

         if(total_len > 1000000 && Test::run_long_tests() == false)
            {
            return result;
            }

         if(providers.empty())
            {
            result.note_missing("hash " + algo);
            return result;
            }

         for(auto const& provider_ask : providers)
            {
            std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(algo, provider_ask));

            if(!hash)
               {
               result.test_failure("Hash " + algo + " supported by " + provider_ask + " but not found");
               continue;
               }

            const size_t full_inputs = total_len / input.size();
            const size_t leftover = total_len % input.size();

            for(size_t i = 0; i != full_inputs; ++i)
               hash->update(input);

            if(leftover > 0)
               hash->update(input.data(), leftover);

            std::vector<uint8_t> output(hash->output_length());
            hash->final(output.data());
            result.test_eq("Output is expected", output, expected);
            }

         return result;
         }
   };

BOTAN_REGISTER_TEST("hash", "hash_rep", Hash_LongRepeat_Tests);

}

#endif

}