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/int_utils.h>
15#include <botan/internal/loadstor.h>
16#include <botan/internal/mem_utils.h>
17#include <botan/internal/salsa20.h>
18#include <botan/internal/time_utils.h>
25constexpr size_t MAX_SCRYPT_N = 4194304;
26constexpr size_t MAX_SCRYPT_MEMORY_GB =
sizeof(size_t) == 4 ? 2 : 8;
27constexpr size_t MAX_SCRYPT_MEMORY_BYTES = MAX_SCRYPT_MEMORY_GB * 1024 * 1024 * 1024 + 2 * 1024 * 1024;
29std::optional<size_t> scrypt_memory_usage(
size_t N,
size_t r,
size_t p) {
31 const auto block_size =
checked_mul(
static_cast<size_t>(128), r);
33 if(block_size && blocks) {
34 return checked_mul(block_size.value(), blocks.value());
47 return std::make_unique<Scrypt>(32768, 8, 1);
51 uint64_t desired_msec,
52 std::optional<size_t> max_memory,
53 uint64_t tuning_msec)
const {
67 const size_t max_memory_bytes = std::min(MAX_SCRYPT_MEMORY_BYTES, max_memory.value_or(0) * 1024 * 1024);
73 auto scrypt_parameters_acceptable = [&](
size_t N,
size_t r) ->
bool {
74 if(N > MAX_SCRYPT_N) {
77 if(
const auto consumed = scrypt_memory_usage(N, r, 0)) {
78 if(max_memory_bytes > 0 && *consumed > max_memory_bytes) {
95 const uint64_t measured_time =
measure_cost(tuning_msec, [&]() {
96 uint8_t output[32] = {0};
97 pwdhash->derive_key(output,
sizeof(output),
"test", 4,
nullptr, 0);
100 const uint64_t target_nsec = desired_msec *
static_cast<uint64_t
>(1000000);
102 uint64_t est_nsec = measured_time;
105 if(scrypt_parameters_acceptable(N, r * 8)) {
106 if(target_nsec / est_nsec >= 5) {
113 while(scrypt_parameters_acceptable(N * 2, r)) {
114 if(target_nsec / est_nsec >= 2) {
123 if(target_nsec / est_nsec >= 2) {
124 p *= std::min<size_t>(1024,
static_cast<size_t>(target_nsec / est_nsec));
127 return std::make_unique<Scrypt>(N, r, p);
131 return std::make_unique<Scrypt>(N, r, p);
150 return std::make_unique<Scrypt>(N, r, p);
158 if(p == 0 || p > 1024) {
161 if(r == 0 || r > 256) {
164 if(N < 1 || N > MAX_SCRYPT_N) {
168 if(
const auto memory_usage = scrypt_memory_usage(N, r, p)) {
169 if(memory_usage > MAX_SCRYPT_MEMORY_BYTES) {
170 throw Invalid_Argument(
"Scrypt parameters exceed maximum allowed memory limit");
173 throw Invalid_Argument(
"Scrypt parameters are too large for this platform");
178 return fmt(
"Scrypt({},{},{})", m_N, m_r, m_p);
186 const auto consumption = scrypt_memory_usage(N, r, p);
188 return consumption.value();
193void scryptBlockMix(
size_t r, uint8_t* B, uint8_t* Y) {
195 std::array<uint8_t, 64> X{};
196 copy_mem(X.data(), &B[(2 * r - 1) * 64], 64);
198 for(
size_t i = 0; i != 2 * r; i++) {
199 xor_buf(X.data(), &B[64 * i], 64);
205 for(
size_t i = 0; i < r; ++i) {
206 copy_mem(&B[i * 64], &Y[(i * 2) * 64], 64);
209 for(
size_t i = 0; i < r; ++i) {
210 copy_mem(&B[(i + r) * 64], &Y[(i * 2 + 1) * 64], 64);
215 const size_t S = 128 * r;
217 for(
size_t i = 0; i != N; ++i) {
219 scryptBlockMix(r, B, &V[N * S]);
222 for(
size_t i = 0; i != N; ++i) {
226 scryptBlockMix(r, B, &V[N * S]);
234 const char* password,
236 const uint8_t salt[],
237 size_t salt_len)
const {
238 if(output_len == 0) {
246 const size_t S =
mul_or_throw(
size_t(128), r,
"Scrypt S size overflow");
256 throw Invalid_Argument(
"Scrypt cannot accept passphrases of the provided length");
259 pbkdf2(*hmac_sha256, B.data(), B.size(), salt, salt_len, 1);
262 for(
size_t i = 0; i != p; ++i) {
263 scryptROMmix(r, N, &B[128 * r * i], V);
266 pbkdf2(*hmac_sha256, output, output_len, B.data(), B.size(), 1);
#define BOTAN_ASSERT_NOMSG(expr)
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_params(size_t output_len, uint64_t desired_runtime_msec, std::optional< size_t > max_memory, uint64_t 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)
constexpr std::optional< T > checked_add(T a, T b)
constexpr T mul_or_throw(T a, T b, std::string_view msg)
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
std::string fmt(std::string_view format, const T &... args)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr std::optional< T > checked_mul(T a, T b)
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
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)
uint64_t measure_cost(uint64_t trial_msec, F func)