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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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/timer.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 { return fmt("Bcrypt-PBKDF({})", m_iterations); }
×
22

23
std::string Bcrypt_PBKDF_Family::name() const { return "Bcrypt-PBKDF"; }
2✔
24

25
std::unique_ptr<PasswordHash> Bcrypt_PBKDF_Family::tune(size_t output_length,
1✔
26
                                                        std::chrono::milliseconds msec,
27
                                                        size_t /*max_memory*/,
28
                                                        std::chrono::milliseconds tune_time) const {
29
   Timer timer("Bcrypt_PBKDF");
1✔
30

31
   const size_t blocks = (output_length + 32 - 1) / 32;
1✔
32

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

37
   const size_t starting_iter = 2;
1✔
38

39
   auto pwhash = this->from_iterations(starting_iter);
1✔
40

41
   timer.run_until_elapsed(tune_time, [&]() {
1✔
42
      uint8_t output[32] = {0};
1✔
43
      pwhash->derive_key(output, sizeof(output), "test", 4, nullptr, 0);
1✔
44
   });
45

46
   if(timer.events() < blocks || timer.value() == 0) {
1✔
47
      return default_params();
×
48
   }
49

50
   const uint64_t measured_time = timer.value() / (timer.events() / blocks);
1✔
51

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

54
   const uint64_t desired_increase = target_nsec / measured_time;
1✔
55

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

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

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

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

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

75
namespace {
76

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

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

89
   const size_t BCRYPT_PBKDF_WORKFACTOR = 6;
443✔
90
   const size_t BCRYPT_PBKDF_ROUNDS = 64;
443✔
91

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

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

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

111
   xor_buf(out.data(), tmp.data(), BCRYPT_PBKDF_OUTPUT);
443✔
112
}
443✔
113

114
}  // namespace
115

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

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

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

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

135
   secure_vector<uint8_t> salt_hash(sha512->output_length());
40✔
136

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

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

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

148
      bcrypt_round(blowfish, pass_hash, salt_hash, out, tmp);
57✔
149

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

155
         bcrypt_round(blowfish, pass_hash, salt_hash, out, tmp);
386✔
156
      }
157

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

167
}  // 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

© 2025 Coveralls, Inc