Coverage Report

Created: 2024-11-21 06:38

/src/botan/src/lib/pbkdf/scrypt/scrypt.cpp
Line
Count
Source (jump to first uncovered line)
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
0
size_t scrypt_memory_usage(size_t N, size_t r, size_t p) {
23
0
   return 128 * r * (N + p);
24
0
}
25
26
}  // namespace
27
28
0
std::string Scrypt_Family::name() const {
29
0
   return "Scrypt";
30
0
}
31
32
0
std::unique_ptr<PasswordHash> Scrypt_Family::default_params() const {
33
0
   return std::make_unique<Scrypt>(32768, 8, 1);
34
0
}
35
36
std::unique_ptr<PasswordHash> Scrypt_Family::tune(size_t output_length,
37
                                                  std::chrono::milliseconds msec,
38
                                                  size_t max_memory_usage_mb,
39
0
                                                  std::chrono::milliseconds tune_time) const {
40
0
   BOTAN_UNUSED(output_length);
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
   // This is zero if max_memory_usage_mb == 0 (unbounded)
55
0
   const size_t max_memory_usage = max_memory_usage_mb * 1024 * 1024;
56
57
   // Starting parameters
58
0
   size_t N = 8 * 1024;
59
0
   size_t r = 1;
60
0
   size_t p = 1;
61
62
0
   Timer timer("Scrypt");
63
64
0
   auto pwdhash = this->from_params(N, r, p);
65
66
0
   timer.run_until_elapsed(tune_time, [&]() {
67
0
      uint8_t output[32] = {0};
68
0
      pwdhash->derive_key(output, sizeof(output), "test", 4, nullptr, 0);
69
0
   });
70
71
   // No timer events seems strange, perhaps something is wrong - give
72
   // up on this and just return default params
73
0
   if(timer.events() == 0) {
74
0
      return default_params();
75
0
   }
76
77
   // nsec per eval of scrypt with initial params
78
0
   const uint64_t measured_time = timer.value() / timer.events();
79
80
0
   const uint64_t target_nsec = msec.count() * static_cast<uint64_t>(1000000);
81
82
0
   uint64_t est_nsec = measured_time;
83
84
   // In below code we invoke scrypt_memory_usage with p == 0 as p contributes
85
   // (very slightly) to memory consumption, but N is the driving factor.
86
   // Including p leads to using an N half as large as what the user would expect.
87
88
   // First increase r by 8x if possible
89
0
   if(max_memory_usage == 0 || scrypt_memory_usage(N, r * 8, 0) <= max_memory_usage) {
90
0
      if(target_nsec / est_nsec >= 5) {
91
0
         r *= 8;
92
0
         est_nsec *= 5;
93
0
      }
94
0
   }
95
96
   // Now double N as many times as we can
97
0
   while(max_memory_usage == 0 || scrypt_memory_usage(N * 2, r, 0) <= max_memory_usage) {
98
0
      if(target_nsec / est_nsec >= 2) {
99
0
         N *= 2;
100
0
         est_nsec *= 2;
101
0
      } else {
102
0
         break;
103
0
      }
104
0
   }
105
106
   // If we have extra runtime budget, increment p
107
0
   if(target_nsec / est_nsec >= 2) {
108
0
      p *= std::min<size_t>(1024, static_cast<size_t>(target_nsec / est_nsec));
109
0
   }
110
111
0
   return std::make_unique<Scrypt>(N, r, p);
112
0
}
113
114
0
std::unique_ptr<PasswordHash> Scrypt_Family::from_params(size_t N, size_t r, size_t p) const {
115
0
   return std::make_unique<Scrypt>(N, r, p);
116
0
}
117
118
0
std::unique_ptr<PasswordHash> Scrypt_Family::from_iterations(size_t iter) const {
119
0
   const size_t r = 8;
120
0
   const size_t p = 1;
121
122
0
   size_t N = 8192;
123
124
0
   if(iter > 50000) {
125
0
      N = 16384;
126
0
   }
127
0
   if(iter > 100000) {
128
0
      N = 32768;
129
0
   }
130
0
   if(iter > 150000) {
131
0
      N = 65536;
132
0
   }
133
134
0
   return std::make_unique<Scrypt>(N, r, p);
135
0
}
136
137
0
Scrypt::Scrypt(size_t N, size_t r, size_t p) : m_N(N), m_r(r), m_p(p) {
138
0
   if(!is_power_of_2(N)) {
139
0
      throw Invalid_Argument("Scrypt N parameter must be a power of 2");
140
0
   }
141
142
0
   if(p == 0 || p > 1024) {
143
0
      throw Invalid_Argument("Invalid or unsupported scrypt p");
144
0
   }
145
0
   if(r == 0 || r > 256) {
146
0
      throw Invalid_Argument("Invalid or unsupported scrypt r");
147
0
   }
148
0
   if(N < 1 || N > 4194304) {
149
0
      throw Invalid_Argument("Invalid or unsupported scrypt N");
150
0
   }
151
0
}
152
153
0
std::string Scrypt::to_string() const {
154
0
   return fmt("Scrypt({},{},{})", m_N, m_r, m_p);
155
0
}
156
157
0
size_t Scrypt::total_memory_usage() const {
158
0
   const size_t N = memory_param();
159
0
   const size_t p = parallelism();
160
0
   const size_t r = iterations();
161
162
0
   return scrypt_memory_usage(N, r, p);
163
0
}
164
165
namespace {
166
167
0
void scryptBlockMix(size_t r, uint8_t* B, uint8_t* Y) {
168
0
   uint32_t B32[16];
169
0
   secure_vector<uint8_t> X(64);
170
0
   copy_mem(X.data(), &B[(2 * r - 1) * 64], 64);
171
172
0
   for(size_t i = 0; i != 2 * r; i++) {
173
0
      xor_buf(X.data(), &B[64 * i], 64);
174
0
      load_le<uint32_t>(B32, X.data(), 16);
175
0
      Salsa20::salsa_core(X.data(), B32, 8);
176
0
      copy_mem(&Y[64 * i], X.data(), 64);
177
0
   }
178
179
0
   for(size_t i = 0; i < r; ++i) {
180
0
      copy_mem(&B[i * 64], &Y[(i * 2) * 64], 64);
181
0
   }
182
183
0
   for(size_t i = 0; i < r; ++i) {
184
0
      copy_mem(&B[(i + r) * 64], &Y[(i * 2 + 1) * 64], 64);
185
0
   }
186
0
}
187
188
0
void scryptROMmix(size_t r, size_t N, uint8_t* B, secure_vector<uint8_t>& V) {
189
0
   const size_t S = 128 * r;
190
191
0
   for(size_t i = 0; i != N; ++i) {
192
0
      copy_mem(&V[S * i], B, S);
193
0
      scryptBlockMix(r, B, &V[N * S]);
194
0
   }
195
196
0
   for(size_t i = 0; i != N; ++i) {
197
      // compiler doesn't know here that N is power of 2
198
0
      const size_t j = load_le<uint32_t>(&B[(2 * r - 1) * 64], 0) & (N - 1);
199
0
      xor_buf(B, &V[j * S], S);
200
0
      scryptBlockMix(r, B, &V[N * S]);
201
0
   }
202
0
}
203
204
}  // namespace
205
206
void Scrypt::derive_key(uint8_t output[],
207
                        size_t output_len,
208
                        const char* password,
209
                        size_t password_len,
210
                        const uint8_t salt[],
211
0
                        size_t salt_len) const {
212
0
   const size_t N = memory_param();
213
0
   const size_t p = parallelism();
214
0
   const size_t r = iterations();
215
216
0
   const size_t S = 128 * r;
217
0
   secure_vector<uint8_t> B(p * S);
218
   // temp space
219
0
   secure_vector<uint8_t> V((N + 1) * S);
220
221
0
   auto hmac_sha256 = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
222
223
0
   try {
224
0
      hmac_sha256->set_key(cast_char_ptr_to_uint8(password), password_len);
225
0
   } catch(Invalid_Key_Length&) {
226
0
      throw Invalid_Argument("Scrypt cannot accept passphrases of the provided length");
227
0
   }
228
229
0
   pbkdf2(*hmac_sha256, B.data(), B.size(), salt, salt_len, 1);
230
231
   // these can be parallel
232
0
   for(size_t i = 0; i != p; ++i) {
233
0
      scryptROMmix(r, N, &B[128 * r * i], V);
234
0
   }
235
236
0
   pbkdf2(*hmac_sha256, output, output_len, B.data(), B.size(), 1);
237
0
}
238
239
}  // namespace Botan