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

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 hits per line

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

78.3
/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/salsa20.h>
16
#include <botan/internal/timer.h>
17

18
namespace Botan {
19

20
namespace {
21

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

26
}  // namespace
27

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

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

36
std::unique_ptr<PasswordHash> Scrypt_Family::tune(size_t output_length,
103✔
37
                                                  std::chrono::milliseconds msec,
38
                                                  size_t max_memory_usage_mb,
39
                                                  std::chrono::milliseconds tune_time) const {
40
   BOTAN_UNUSED(output_length);
103✔
41

42
   /*
43
   * Some rough relations between scrypt parameters and runtime.
44
   * Denote here by stime(N,r,p) the msec it takes to run scrypt.
45
   *
46
   * Emperically for smaller sizes:
47
   * stime(N,8*r,p) / stime(N,r,p) is ~ 6-7
48
   * stime(N,r,8*p) / stime(N,r,8*p) is ~ 7
49
   * stime(2*N,r,p) / stime(N,r,p) is ~ 2
50
   *
51
   * Compute stime(8192,1,1) as baseline and extrapolate
52
   */
53

54
   // We include a bit of slop here (the + 512) to handle the fact that scrypt's
55
   // memory consumption is modified by all three parameters, and otherwise we
56
   // stop before hitting the desired target.
57

58
   const size_t max_memory_usage = max_memory_usage_mb * 1024 * 1024 + 512;
103✔
59
   // Starting parameters
60
   size_t N = 8 * 1024;
103✔
61
   size_t r = 1;
103✔
62
   size_t p = 1;
103✔
63

64
   Timer timer("Scrypt");
103✔
65

66
   auto pwdhash = this->from_params(N, r, p);
103✔
67

68
   timer.run_until_elapsed(tune_time, [&]() {
103✔
69
      uint8_t output[32] = {0};
103✔
70
      pwdhash->derive_key(output, sizeof(output), "test", 4, nullptr, 0);
103✔
71
   });
72

73
   // No timer events seems strange, perhaps something is wrong - give
74
   // up on this and just return default params
75
   if(timer.events() == 0) {
103✔
76
      return default_params();
×
77
   }
78

79
   // nsec per eval of scrypt with initial params
80
   const uint64_t measured_time = timer.value() / timer.events();
103✔
81

82
   const uint64_t target_nsec = msec.count() * static_cast<uint64_t>(1000000);
103✔
83

84
   uint64_t est_nsec = measured_time;
103✔
85

86
   // First move increase r by 8x if possible
87

88
   if(max_memory_usage == 0 || scrypt_memory_usage(N, r * 8, p) <= max_memory_usage) {
103✔
89
      if(target_nsec / est_nsec >= 5) {
4✔
90
         r *= 8;
×
91
         est_nsec *= 5;
×
92
      }
93
   }
94

95
   // Now double N as many times as we can
96

97
   while(max_memory_usage == 0 || scrypt_memory_usage(N * 2, r, p) <= max_memory_usage) {
103✔
98
      if(target_nsec / est_nsec >= 2) {
4✔
99
         N *= 2;
×
100
         est_nsec *= 2;
×
101
      } else {
102
         break;
103
      }
104
   }
105

106
   // If we have extra runtime budget, increment p
107

108
   if(target_nsec / est_nsec > 2) {
103✔
109
      p *= std::min<size_t>(1024, static_cast<size_t>(target_nsec / est_nsec));
6✔
110
   }
111

112
   return std::make_unique<Scrypt>(N, r, p);
103✔
113
}
103✔
114

115
std::unique_ptr<PasswordHash> Scrypt_Family::from_params(size_t N, size_t r, size_t p) const {
233✔
116
   return std::make_unique<Scrypt>(N, r, p);
233✔
117
}
118

119
std::unique_ptr<PasswordHash> Scrypt_Family::from_iterations(size_t iter) const {
×
120
   const size_t r = 8;
×
121
   const size_t p = 1;
×
122

123
   size_t N = 8192;
×
124

125
   if(iter > 50000) {
×
126
      N = 16384;
×
127
   }
128
   if(iter > 100000) {
×
129
      N = 32768;
×
130
   }
131
   if(iter > 150000) {
×
132
      N = 65536;
×
133
   }
134

135
   return std::make_unique<Scrypt>(N, r, p);
×
136
}
137

138
Scrypt::Scrypt(size_t N, size_t r, size_t p) : m_N(N), m_r(r), m_p(p) {
338✔
139
   if(!is_power_of_2(N)) {
338✔
140
      throw Invalid_Argument("Scrypt N parameter must be a power of 2");
×
141
   }
142

143
   if(p == 0 || p > 1024) {
338✔
144
      throw Invalid_Argument("Invalid or unsupported scrypt p");
×
145
   }
146
   if(r == 0 || r > 256) {
338✔
147
      throw Invalid_Argument("Invalid or unsupported scrypt r");
×
148
   }
149
   if(N < 1 || N > 4194304) {
338✔
150
      throw Invalid_Argument("Invalid or unsupported scrypt N");
×
151
   }
152
}
338✔
153

154
std::string Scrypt::to_string() const {
4✔
155
   return fmt("Scrypt({},{},{})", m_N, m_r, m_p);
4✔
156
}
157

158
size_t Scrypt::total_memory_usage() const {
20✔
159
   const size_t N = memory_param();
20✔
160
   const size_t p = parallelism();
20✔
161
   const size_t r = iterations();
20✔
162

163
   return scrypt_memory_usage(N, r, p);
20✔
164
}
165

166
namespace {
167

168
void scryptBlockMix(size_t r, uint8_t* B, uint8_t* Y) {
10,618,328✔
169
   uint32_t B32[16];
10,618,328✔
170
   secure_vector<uint8_t> X(64);
10,618,328✔
171
   copy_mem(X.data(), &B[(2 * r - 1) * 64], 64);
10,618,328✔
172

173
   for(size_t i = 0; i != 2 * r; i++) {
106,239,656✔
174
      xor_buf(X.data(), &B[64 * i], 64);
95,621,328✔
175
      load_le<uint32_t>(B32, X.data(), 16);
95,621,328✔
176
      Salsa20::salsa_core(X.data(), B32, 8);
95,621,328✔
177
      copy_mem(&Y[64 * i], X.data(), 64);
95,621,328✔
178
   }
179

180
   for(size_t i = 0; i < r; ++i) {
58,428,992✔
181
      copy_mem(&B[i * 64], &Y[(i * 2) * 64], 64);
47,810,664✔
182
   }
183

184
   for(size_t i = 0; i < r; ++i) {
58,428,992✔
185
      copy_mem(&B[(i + r) * 64], &Y[(i * 2 + 1) * 64], 64);
47,810,664✔
186
   }
187
}
10,618,328✔
188

189
void scryptROMmix(size_t r, size_t N, uint8_t* B, secure_vector<uint8_t>& V) {
723✔
190
   const size_t S = 128 * r;
723✔
191

192
   for(size_t i = 0; i != N; ++i) {
5,309,887✔
193
      copy_mem(&V[S * i], B, S);
5,309,164✔
194
      scryptBlockMix(r, B, &V[N * S]);
5,309,164✔
195
   }
196

197
   for(size_t i = 0; i != N; ++i) {
5,309,887✔
198
      // compiler doesn't know here that N is power of 2
199
      const size_t j = load_le<uint32_t>(&B[(2 * r - 1) * 64], 0) & (N - 1);
5,309,164✔
200
      xor_buf(B, &V[j * S], S);
5,309,164✔
201
      scryptBlockMix(r, B, &V[N * S]);
5,309,164✔
202
   }
203
}
723✔
204

205
}  // namespace
206

207
void Scrypt::derive_key(uint8_t output[],
338✔
208
                        size_t output_len,
209
                        const char* password,
210
                        size_t password_len,
211
                        const uint8_t salt[],
212
                        size_t salt_len) const {
213
   const size_t N = memory_param();
338✔
214
   const size_t p = parallelism();
338✔
215
   const size_t r = iterations();
338✔
216

217
   const size_t S = 128 * r;
338✔
218
   secure_vector<uint8_t> B(p * S);
338✔
219
   // temp space
220
   secure_vector<uint8_t> V((N + 1) * S);
338✔
221

222
   auto hmac_sha256 = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
338✔
223

224
   try {
338✔
225
      hmac_sha256->set_key(cast_char_ptr_to_uint8(password), password_len);
338✔
226
   } catch(Invalid_Key_Length&) {
×
227
      throw Invalid_Argument("Scrypt cannot accept passphrases of the provided length");
×
228
   }
×
229

230
   pbkdf2(*hmac_sha256, B.data(), B.size(), salt, salt_len, 1);
338✔
231

232
   // these can be parallel
233
   for(size_t i = 0; i != p; ++i) {
1,061✔
234
      scryptROMmix(r, N, &B[128 * r * i], V);
723✔
235
   }
236

237
   pbkdf2(*hmac_sha256, output, output_len, B.data(), B.size(), 1);
338✔
238
}
1,014✔
239

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