• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 12130573488

03 Dec 2024 12:33AM UTC coverage: 91.264% (+0.006%) from 91.258%
12130573488

push

github

web-flow
Merge pull request #4456 from randombit/jack/split-timer

Split up Timer logic

93394 of 102334 relevant lines covered (91.26%)

11563440.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.96
/src/lib/pbkdf/bcrypt_pbkdf/bcrypt_pbkdf.cpp
1
/*
2
* (C) 2018,2019 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include <botan/bcrypt_pbkdf.h>
8

9
#include <botan/hash.h>
10
#include <botan/internal/blowfish.h>
11
#include <botan/internal/fmt.h>
12
#include <botan/internal/loadstor.h>
13
#include <botan/internal/time_utils.h>
14

15
namespace Botan {
16

17
Bcrypt_PBKDF::Bcrypt_PBKDF(size_t iterations) : m_iterations(iterations) {
40✔
18
   BOTAN_ARG_CHECK(m_iterations > 0, "Invalid Bcrypt-PBKDF iterations");
40✔
19
}
40✔
20

21
std::string Bcrypt_PBKDF::to_string() const {
×
22
   return fmt("Bcrypt-PBKDF({})", m_iterations);
×
23
}
24

25
std::string Bcrypt_PBKDF_Family::name() const {
2✔
26
   return "Bcrypt-PBKDF";
2✔
27
}
28

29
std::unique_ptr<PasswordHash> Bcrypt_PBKDF_Family::tune(size_t output_length,
1✔
30
                                                        std::chrono::milliseconds msec,
31
                                                        size_t /*max_memory*/,
32
                                                        std::chrono::milliseconds tune_time) const {
33
   const size_t blocks = (output_length + 32 - 1) / 32;
1✔
34

35
   if(blocks == 0) {
1✔
36
      return default_params();
×
37
   }
38

39
   const size_t starting_iter = 2;
1✔
40

41
   auto pwhash = this->from_iterations(starting_iter);
1✔
42

43
   auto tune_fn = [&]() {
2✔
44
      uint8_t output[32] = {0};
1✔
45
      pwhash->derive_key(output, sizeof(output), "test", 4, nullptr, 0);
1✔
46
   };
1✔
47

48
   const uint64_t measured_time = measure_cost(tune_time, tune_fn) / blocks;
1✔
49

50
   const uint64_t target_nsec = msec.count() * static_cast<uint64_t>(1000000);
1✔
51

52
   const uint64_t desired_increase = target_nsec / measured_time;
1✔
53

54
   if(desired_increase == 0) {
1✔
55
      return this->from_iterations(starting_iter);
1✔
56
   }
57

58
   return this->from_iterations(static_cast<size_t>(desired_increase * starting_iter));
×
59
}
1✔
60

61
std::unique_ptr<PasswordHash> Bcrypt_PBKDF_Family::default_params() const {
1✔
62
   return this->from_iterations(32);  // About 100 ms on fast machine
1✔
63
}
64

65
std::unique_ptr<PasswordHash> Bcrypt_PBKDF_Family::from_iterations(size_t iter) const {
40✔
66
   return std::make_unique<Bcrypt_PBKDF>(iter);
40✔
67
}
68

69
std::unique_ptr<PasswordHash> Bcrypt_PBKDF_Family::from_params(size_t iter, size_t /*t*/, size_t /*p*/) const {
1✔
70
   return this->from_iterations(iter);
1✔
71
}
72

73
namespace {
74

75
void bcrypt_round(Blowfish& blowfish,
443✔
76
                  const secure_vector<uint8_t>& pass_hash,
77
                  const secure_vector<uint8_t>& salt_hash,
78
                  secure_vector<uint8_t>& out,
79
                  secure_vector<uint8_t>& tmp) {
80
   const size_t BCRYPT_PBKDF_OUTPUT = 32;
443✔
81

82
   // "OxychromaticBlowfishSwatDynamite"
83
   alignas(64) static const uint8_t BCRYPT_PBKDF_MAGIC[BCRYPT_PBKDF_OUTPUT] = {
443✔
84
      0x4F, 0x78, 0x79, 0x63, 0x68, 0x72, 0x6F, 0x6D, 0x61, 0x74, 0x69, 0x63, 0x42, 0x6C, 0x6F, 0x77,
85
      0x66, 0x69, 0x73, 0x68, 0x53, 0x77, 0x61, 0x74, 0x44, 0x79, 0x6E, 0x61, 0x6D, 0x69, 0x74, 0x65};
86

87
   const size_t BCRYPT_PBKDF_WORKFACTOR = 6;
443✔
88
   const size_t BCRYPT_PBKDF_ROUNDS = 64;
443✔
89

90
   blowfish.salted_set_key(
443✔
91
      pass_hash.data(), pass_hash.size(), salt_hash.data(), salt_hash.size(), BCRYPT_PBKDF_WORKFACTOR, true);
92

93
   copy_mem(tmp.data(), BCRYPT_PBKDF_MAGIC, BCRYPT_PBKDF_OUTPUT);
443✔
94
   for(size_t i = 0; i != BCRYPT_PBKDF_ROUNDS; ++i) {
28,795✔
95
      blowfish.encrypt(tmp);
56,704✔
96
   }
97

98
   /*
99
   Bcrypt PBKDF loads the Blowfish output as big endian for no reason
100
   in particular. We can't just swap everything once at the end
101
   because the (big-endian) values are fed into SHA-512 to generate
102
   the salt for the next round
103
   */
104
   for(size_t i = 0; i != 32 / 4; ++i) {
3,987✔
105
      const uint32_t w = load_le<uint32_t>(tmp.data(), i);
3,544✔
106
      store_be(w, &tmp[sizeof(uint32_t) * i]);
3,544✔
107
   }
108

109
   xor_buf(out.data(), tmp.data(), BCRYPT_PBKDF_OUTPUT);
443✔
110
}
443✔
111

112
}  // namespace
113

114
void Bcrypt_PBKDF::derive_key(uint8_t output[],
40✔
115
                              size_t output_len,
116
                              const char* password,
117
                              size_t password_len,
118
                              const uint8_t salt[],
119
                              size_t salt_len) const {
120
   // No output desired, so we are all done already...
121
   if(output_len == 0) {
40✔
122
      return;
×
123
   }
124

125
   BOTAN_ARG_CHECK(output_len <= 10 * 1024 * 1024, "Too much output for Bcrypt PBKDF");
40✔
126

127
   const size_t BCRYPT_BLOCK_SIZE = 32;
40✔
128
   const size_t blocks = (output_len + BCRYPT_BLOCK_SIZE - 1) / BCRYPT_BLOCK_SIZE;
40✔
129

130
   auto sha512 = HashFunction::create_or_throw("SHA-512");
40✔
131
   const auto pass_hash = sha512->process(reinterpret_cast<const uint8_t*>(password), password_len);
40✔
132

133
   secure_vector<uint8_t> salt_hash(sha512->output_length());
40✔
134

135
   Blowfish blowfish;
40✔
136
   secure_vector<uint8_t> out(BCRYPT_BLOCK_SIZE);
40✔
137
   secure_vector<uint8_t> tmp(BCRYPT_BLOCK_SIZE);
40✔
138

139
   for(size_t block = 0; block != blocks; ++block) {
97✔
140
      clear_mem(out.data(), out.size());
57✔
141

142
      sha512->update(salt, salt_len);
57✔
143
      sha512->update_be(static_cast<uint32_t>(block + 1));
57✔
144
      sha512->final(salt_hash.data());
57✔
145

146
      bcrypt_round(blowfish, pass_hash, salt_hash, out, tmp);
57✔
147

148
      for(size_t r = 1; r < m_iterations; ++r) {
443✔
149
         // Next salt is H(prev_output)
150
         sha512->update(tmp);
386✔
151
         sha512->final(salt_hash.data());
386✔
152

153
         bcrypt_round(blowfish, pass_hash, salt_hash, out, tmp);
386✔
154
      }
155

156
      for(size_t i = 0; i != BCRYPT_BLOCK_SIZE; ++i) {
1,881✔
157
         const size_t dest = i * blocks + block;
1,824✔
158
         if(dest < output_len) {
1,824✔
159
            output[dest] = out[i];
1,546✔
160
         }
161
      }
162
   }
163
}
200✔
164

165
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc