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

randombit / botan / 22048403227

16 Feb 2026 02:47AM UTC coverage: 90.036% (-0.008%) from 90.044%
22048403227

push

github

web-flow
Merge pull request #5315 from randombit/jack/no-chrono-in-pwdhash

Add PasswordHashFamily::tune_params

102325 of 113649 relevant lines covered (90.04%)

11636648.16 hits per line

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

81.19
/src/lib/pbkdf/scrypt/scrypt.cpp
1
/**
2
* (C) 2018 Jack Lloyd
3
* (C) 2018 Ribose Inc
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/scrypt.h>
9

10
#include <botan/exceptn.h>
11
#include <botan/pbkdf2.h>
12
#include <botan/internal/bit_ops.h>
13
#include <botan/internal/fmt.h>
14
#include <botan/internal/loadstor.h>
15
#include <botan/internal/mem_utils.h>
16
#include <botan/internal/salsa20.h>
17
#include <botan/internal/time_utils.h>
18

19
namespace Botan {
20

21
namespace {
22

23
size_t scrypt_memory_usage(size_t N, size_t r, size_t p) {
25✔
24
   return 128 * r * (N + p);
25✔
25
}
26

27
}  // namespace
28

29
std::string Scrypt_Family::name() const {
1✔
30
   return "Scrypt";
1✔
31
}
32

33
std::unique_ptr<PasswordHash> Scrypt_Family::default_params() const {
2✔
34
   return std::make_unique<Scrypt>(32768, 8, 1);
2✔
35
}
36

37
std::unique_ptr<PasswordHash> Scrypt_Family::tune_params(size_t /*output_length*/,
242✔
38
                                                         uint64_t desired_msec,
39
                                                         std::optional<size_t> max_memory,
40
                                                         uint64_t tuning_msec) const {
41
   /*
42
   * Some rough relations between scrypt parameters and runtime.
43
   * Denote here by stime(N,r,p) the msec it takes to run scrypt.
44
   *
45
   * Empirically for smaller sizes:
46
   * stime(N,8*r,p) / stime(N,r,p) is ~ 6-7
47
   * stime(N,r,8*p) / stime(N,r,8*p) is ~ 7
48
   * stime(2*N,r,p) / stime(N,r,p) is ~ 2
49
   *
50
   * Compute stime(8192,1,1) as baseline and extrapolate
51
   */
52

53
   // If max_memory is nullopt or zero this becomes zero and is ignored
54
   const size_t max_memory_bytes = max_memory.value_or(0) * 1024 * 1024;
242✔
55

56
   // Starting parameters
57
   size_t N = 8 * 1024;
242✔
58
   size_t r = 1;
242✔
59
   size_t p = 1;
242✔
60

61
   auto pwdhash = this->from_params(N, r, p);
242✔
62

63
   const uint64_t measured_time = measure_cost(tuning_msec, [&]() {
242✔
64
      uint8_t output[32] = {0};
242✔
65
      pwdhash->derive_key(output, sizeof(output), "test", 4, nullptr, 0);
242✔
66
   });
67

68
   const uint64_t target_nsec = desired_msec * static_cast<uint64_t>(1000000);
242✔
69

70
   uint64_t est_nsec = measured_time;
242✔
71

72
   // In below code we invoke scrypt_memory_usage with p == 0 as p contributes
73
   // (very slightly) to memory consumption, but N is the driving factor.
74
   // Including p leads to using an N half as large as what the user would expect.
75

76
   // First increase r by 8x if possible
77
   if(max_memory_bytes == 0 || scrypt_memory_usage(N, r * 8, 0) <= max_memory_bytes) {
242✔
78
      if(target_nsec / est_nsec >= 5) {
242✔
79
         r *= 8;
8✔
80
         est_nsec *= 5;
8✔
81
      }
82
   }
83

84
   // Now double N as many times as we can
85
   while(max_memory_bytes == 0 || scrypt_memory_usage(N * 2, r, 0) <= max_memory_bytes) {
251✔
86
      if(target_nsec / est_nsec >= 2) {
251✔
87
         N *= 2;
9✔
88
         est_nsec *= 2;
9✔
89
      } else {
90
         break;
91
      }
92
   }
93

94
   // If we have extra runtime budget, increment p
95
   if(target_nsec / est_nsec >= 2) {
242✔
96
      p *= std::min<size_t>(1024, static_cast<size_t>(target_nsec / est_nsec));
×
97
   }
98

99
   return std::make_unique<Scrypt>(N, r, p);
242✔
100
}
242✔
101

102
std::unique_ptr<PasswordHash> Scrypt_Family::from_params(size_t N, size_t r, size_t p) const {
511✔
103
   return std::make_unique<Scrypt>(N, r, p);
511✔
104
}
105

106
std::unique_ptr<PasswordHash> Scrypt_Family::from_iterations(size_t iter) const {
×
107
   const size_t r = 8;
×
108
   const size_t p = 1;
×
109

110
   size_t N = 8192;
×
111

112
   if(iter > 50000) {
×
113
      N = 16384;
×
114
   }
115
   if(iter > 100000) {
×
116
      N = 32768;
×
117
   }
118
   if(iter > 150000) {
×
119
      N = 65536;
×
120
   }
121

122
   return std::make_unique<Scrypt>(N, r, p);
×
123
}
124

125
Scrypt::Scrypt(size_t N, size_t r, size_t p) : m_N(N), m_r(r), m_p(p) {
755✔
126
   if(!is_power_of_2(N)) {
755✔
127
      throw Invalid_Argument("Scrypt N parameter must be a power of 2");
×
128
   }
129

130
   if(p == 0 || p > 1024) {
755✔
131
      throw Invalid_Argument("Invalid or unsupported scrypt p");
×
132
   }
133
   if(r == 0 || r > 256) {
755✔
134
      throw Invalid_Argument("Invalid or unsupported scrypt r");
×
135
   }
136
   if(N < 1 || N > 4194304) {
755✔
137
      throw Invalid_Argument("Invalid or unsupported scrypt N");
×
138
   }
139
}
755✔
140

141
std::string Scrypt::to_string() const {
4✔
142
   return fmt("Scrypt({},{},{})", m_N, m_r, m_p);
4✔
143
}
144

145
size_t Scrypt::total_memory_usage() const {
20✔
146
   const size_t N = memory_param();
20✔
147
   const size_t p = parallelism();
20✔
148
   const size_t r = iterations();
20✔
149

150
   return scrypt_memory_usage(N, r, p);
20✔
151
}
152

153
namespace {
154

155
void scryptBlockMix(size_t r, uint8_t* B, uint8_t* Y) {
17,466,840✔
156
   uint32_t B32[16];
17,466,840✔
157
   secure_vector<uint8_t> X(64);
17,466,840✔
158
   copy_mem(X.data(), &B[(2 * r - 1) * 64], 64);
17,466,840✔
159

160
   for(size_t i = 0; i != 2 * r; i++) {
130,913,960✔
161
      xor_buf(X.data(), &B[64 * i], 64);
113,447,120✔
162
      load_le<uint32_t>(B32, X.data(), 16);
113,447,120✔
163
      Salsa20::salsa_core(X.data(), B32, 8);
113,447,120✔
164
      copy_mem(&Y[64 * i], X.data(), 64);
113,447,120✔
165
   }
166

167
   for(size_t i = 0; i < r; ++i) {
74,190,400✔
168
      copy_mem(&B[i * 64], &Y[(i * 2) * 64], 64);
56,723,560✔
169
   }
170

171
   for(size_t i = 0; i < r; ++i) {
74,190,400✔
172
      copy_mem(&B[(i + r) * 64], &Y[(i * 2 + 1) * 64], 64);
56,723,560✔
173
   }
174
}
17,466,840✔
175

176
void scryptROMmix(size_t r, size_t N, uint8_t* B, secure_vector<uint8_t>& V) {
1,118✔
177
   const size_t S = 128 * r;
1,118✔
178

179
   for(size_t i = 0; i != N; ++i) {
8,734,538✔
180
      copy_mem(&V[S * i], B, S);
8,733,420✔
181
      scryptBlockMix(r, B, &V[N * S]);
8,733,420✔
182
   }
183

184
   for(size_t i = 0; i != N; ++i) {
8,734,538✔
185
      // compiler doesn't know here that N is power of 2
186
      const size_t j = load_le<uint32_t>(&B[(2 * r - 1) * 64], 0) & (N - 1);
8,733,420✔
187
      xor_buf(B, &V[j * S], S);
8,733,420✔
188
      scryptBlockMix(r, B, &V[N * S]);
8,733,420✔
189
   }
190
}
1,118✔
191

192
}  // namespace
193

194
void Scrypt::derive_key(uint8_t output[],
755✔
195
                        size_t output_len,
196
                        const char* password,
197
                        size_t password_len,
198
                        const uint8_t salt[],
199
                        size_t salt_len) const {
200
   const size_t N = memory_param();
755✔
201
   const size_t p = parallelism();
755✔
202
   const size_t r = iterations();
755✔
203

204
   const size_t S = 128 * r;
755✔
205
   secure_vector<uint8_t> B(p * S);
755✔
206
   // temp space
207
   secure_vector<uint8_t> V((N + 1) * S);
755✔
208

209
   auto hmac_sha256 = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
755✔
210

211
   try {
755✔
212
      hmac_sha256->set_key(as_span_of_bytes(password, password_len));
755✔
213
   } catch(Invalid_Key_Length&) {
×
214
      throw Invalid_Argument("Scrypt cannot accept passphrases of the provided length");
×
215
   }
×
216

217
   pbkdf2(*hmac_sha256, B.data(), B.size(), salt, salt_len, 1);
755✔
218

219
   // these can be parallel
220
   for(size_t i = 0; i != p; ++i) {
1,873✔
221
      scryptROMmix(r, N, &B[128 * r * i], V);
1,118✔
222
   }
223

224
   pbkdf2(*hmac_sha256, output, output_len, B.data(), B.size(), 1);
755✔
225
}
2,265✔
226

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