7#include <botan/argon2.h>
8#include <botan/exceptn.h>
9#include <botan/internal/timer.h>
22 const char* password,
size_t password_len,
23 const uint8_t salt[],
size_t salt_len)
const
26 password, password_len,
30 m_family, m_p, m_M, m_t);
35std::string argon2_family_name(uint8_t f)
54 return argon2_family_name(m_family) +
"(" +
62 if(m_family != 0 && m_family != 1 && m_family != 2)
68 return argon2_family_name(m_family);
72 std::chrono::milliseconds msec,
73 size_t max_memory)
const
75 const size_t max_kib = (max_memory == 0) ? 256*1024 : max_memory*1024;
79 const size_t tune_M = (msec >= std::chrono::milliseconds(500) ? 128 : 36) * 1024;
83 Timer timer(
"Argon2");
84 const auto tune_time = BOTAN_PBKDF_TUNING_TIME;
87 uint8_t output[64] = { 0 };
88 argon2(output,
sizeof(output),
"test", 4,
nullptr, 0,
nullptr, 0,
nullptr, 0, m_family, p, tune_M, t);
96 const uint64_t measured_time = timer.
value() / (timer.
events() * (tune_M / M));
98 const uint64_t target_nsec = msec.count() *
static_cast<uint64_t
>(1000000);
110 uint64_t est_nsec = measured_time;
112 if(est_nsec < target_nsec && M < max_kib)
114 const uint64_t desired_cost_increase = (target_nsec + est_nsec - 1) / est_nsec;
115 const uint64_t mem_headroom = max_kib / M;
117 const uint64_t M_mult = std::min(desired_cost_increase, mem_headroom);
118 M *=
static_cast<size_t>(M_mult);
122 if(est_nsec < target_nsec)
124 const uint64_t desired_cost_increase = (target_nsec + est_nsec - 1) / est_nsec;
125 t *=
static_cast<size_t>(desired_cost_increase);
143 const size_t M = iter;
151 return std::unique_ptr<PasswordHash>(
new Argon2(m_family, M, t, p));
std::unique_ptr< PasswordHash > from_params(size_t M, size_t t, size_t p) const override
std::unique_ptr< PasswordHash > tune(size_t output_length, std::chrono::milliseconds msec, size_t max_memory) const override
std::string name() const override
Argon2_Family(uint8_t family)
std::unique_ptr< PasswordHash > default_params() const override
std::unique_ptr< PasswordHash > from_iterations(size_t iter) const override
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
std::string to_string() const override
Argon2(uint8_t family, size_t M, size_t t, size_t p)
void run_until_elapsed(std::chrono::milliseconds msec, F f)
std::string to_string(const BER_Object &obj)
void argon2(uint8_t output[], size_t output_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len, const uint8_t key[], size_t key_len, const uint8_t ad[], size_t ad_len, uint8_t mode, size_t threads, size_t M, size_t t)