8#include <botan/scrypt.h>
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>
23size_t scrypt_memory_usage(
size_t N,
size_t r,
size_t p) {
24 return 128 * r * (N + p);
34 return std::make_unique<Scrypt>(32768, 8, 1);
38 std::chrono::milliseconds msec,
39 size_t max_memory_usage_mb,
40 std::chrono::milliseconds tune_time)
const {
56 const size_t max_memory_usage = max_memory_usage_mb * 1024 * 1024;
65 const uint64_t measured_time =
measure_cost(tune_time, [&]() {
66 uint8_t output[32] = {0};
67 pwdhash->derive_key(output,
sizeof(output),
"test", 4,
nullptr, 0);
70 const uint64_t target_nsec = msec.count() *
static_cast<uint64_t
>(1000000);
72 uint64_t est_nsec = measured_time;
79 if(max_memory_usage == 0 || scrypt_memory_usage(N, r * 8, 0) <= max_memory_usage) {
80 if(target_nsec / est_nsec >= 5) {
87 while(max_memory_usage == 0 || scrypt_memory_usage(N * 2, r, 0) <= max_memory_usage) {
88 if(target_nsec / est_nsec >= 2) {
97 if(target_nsec / est_nsec >= 2) {
98 p *= std::min<size_t>(1024,
static_cast<size_t>(target_nsec / est_nsec));
101 return std::make_unique<Scrypt>(N, r, p);
105 return std::make_unique<Scrypt>(N, r, p);
124 return std::make_unique<Scrypt>(N, r, p);
132 if(p == 0 || p > 1024) {
135 if(r == 0 || r > 256) {
138 if(N < 1 || N > 4194304) {
144 return fmt(
"Scrypt({},{},{})", m_N, m_r, m_p);
152 return scrypt_memory_usage(N, r, p);
157void scryptBlockMix(
size_t r, uint8_t* B, uint8_t* Y) {
160 copy_mem(X.data(), &B[(2 * r - 1) * 64], 64);
162 for(
size_t i = 0; i != 2 * r; i++) {
163 xor_buf(X.data(), &B[64 * i], 64);
169 for(
size_t i = 0; i < r; ++i) {
170 copy_mem(&B[i * 64], &Y[(i * 2) * 64], 64);
173 for(
size_t i = 0; i < r; ++i) {
174 copy_mem(&B[(i + r) * 64], &Y[(i * 2 + 1) * 64], 64);
179 const size_t S = 128 * r;
181 for(
size_t i = 0; i != N; ++i) {
183 scryptBlockMix(r, B, &V[N * S]);
186 for(
size_t i = 0; i != N; ++i) {
190 scryptBlockMix(r, B, &V[N * S]);
198 const char* password,
200 const uint8_t salt[],
201 size_t salt_len)
const {
206 const size_t S = 128 * r;
216 throw Invalid_Argument(
"Scrypt cannot accept passphrases of the provided length");
219 pbkdf2(*hmac_sha256, B.data(), B.size(), salt, salt_len, 1);
222 for(
size_t i = 0; i != p; ++i) {
223 scryptROMmix(r, N, &B[128 * r * i], V);
226 pbkdf2(*hmac_sha256, output, output_len, B.data(), B.size(), 1);
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
static void salsa_core(uint8_t output[64], const uint32_t input[16], size_t rounds)
std::unique_ptr< PasswordHash > from_params(size_t N, size_t r, size_t p) const override
std::unique_ptr< PasswordHash > tune(size_t output_length, std::chrono::milliseconds msec, size_t max_memory, std::chrono::milliseconds tune_msec) const override
std::unique_ptr< PasswordHash > from_iterations(size_t iter) const override
std::string name() const override
std::unique_ptr< PasswordHash > default_params() const override
std::string to_string() const override
size_t memory_param() const override
size_t iterations() const override
size_t parallelism() const override
size_t total_memory_usage() const override
Scrypt(size_t N, size_t r, size_t p)
void derive_key(uint8_t out[], size_t out_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len) const override
BOTAN_FORCE_INLINE constexpr bool is_power_of_2(T arg)
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
constexpr void copy_mem(T *out, const T *in, size_t n)
std::string fmt(std::string_view format, const T &... args)
constexpr auto load_le(ParamTs &&... params)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
std::vector< T, secure_allocator< T > > secure_vector
uint64_t measure_cost(std::chrono::milliseconds trial_msec, F func)
size_t pbkdf2(MessageAuthenticationCode &prf, uint8_t out[], size_t out_len, std::string_view password, const uint8_t salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec)