aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/kat_passhash.cpp
blob: 6f66743c505bf13dfefdbe31cdbee7f6206f86a7 (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
#include "tests.h"

#include <botan/auto_rng.h>
#include <iostream>

#if defined(BOTAN_HAS_PASSHASH9)
  #include <botan/passhash9.h>
#endif

#if defined(BOTAN_HAS_BCRYPT)
  #include <botan/bcrypt.h>
#endif

using namespace Botan;

size_t test_bcrypt()
   {
   size_t fails = 0;

#if defined(BOTAN_HAS_BCRYPT)

   // Generated by jBCrypt 0.3
   if(!check_bcrypt("abc", "$2a$05$DfPyLs.G6.To9fXEFgUL1O6HpYw3jIXgPcl/L3Qt3jESuWmhxtmpS"))
      {
      std::cout << "Bcrypt test 1 failed\n";
      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\n";
      fails++;
      }

   AutoSeeded_RNG 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))
         {
         std::cout << "Gen and check for bcrypt failed: " << gen_hash << " not valid\n";
         ++fails;
         }
      }

   test_report("Bcrypt", 6, fails);

#endif

   return fails;
   }

size_t test_passhash9()
   {
   size_t fails = 0;

#if defined(BOTAN_HAS_PASSHASH9)
   const std::string input = "secret";
   const std::string fixed_hash =
      "$9$AAAKhiHXTIUhNhbegwBXJvk03XXJdzFMy+i3GFMIBYKtthTTmXZA";

   size_t ran = 0;

   ++ran;
   if(!check_passhash9(input, fixed_hash))
      {
      std::cout << "Passhash9 fixed input test failed\n";
      fails++;
      }

   AutoSeeded_RNG rng;

   for(byte alg_id = 0; alg_id <= 4; ++alg_id)
      {
      std::string gen_hash = generate_passhash9(input, rng, 2, alg_id);

      ++ran;
      if(!check_passhash9(input, gen_hash))
         {
         std::cout << "Passhash9 gen and check " << static_cast<int>(alg_id) << " failed\n";
         ++fails;
         }
      }

   test_report("Passhash9", ran, fails);
#endif

   return fails;
   }