7#include <botan/argon2.h>
8#include <botan/exceptn.h>
9#include <botan/internal/timer.h>
20 BOTAN_ARG_CHECK(m_p >= 1 && m_p <= 128,
"Invalid Argon2 threads parameter");
21 BOTAN_ARG_CHECK(m_M >= 8*m_p && m_M <= 8192*1024,
"Invalid Argon2 M parameter");
26 const char* password,
size_t password_len,
27 const uint8_t salt[],
size_t salt_len)
const
30 password, password_len,
34 m_family, m_p, m_M, m_t);
39std::string argon2_family_name(uint8_t f)
58 return argon2_family_name(m_family) +
"(" +
59 std::to_string(m_M) +
"," +
60 std::to_string(m_t) +
"," +
61 std::to_string(m_p) +
")";
66 if(m_family != 0 && m_family != 1 && m_family != 2)
72 return argon2_family_name(m_family);
76 std::chrono::milliseconds msec,
77 size_t max_memory)
const
79 const size_t max_kib = (max_memory == 0) ? 256*1024 : max_memory*1024;
83 const size_t tune_M = (msec >= std::chrono::milliseconds(500) ? 128 : 36) * 1024;
87 Timer timer(
"Argon2");
88 const auto tune_time = BOTAN_PBKDF_TUNING_TIME;
93 uint8_t output[64] = { 0 };
94 pwhash->derive_key(output,
sizeof(output),
104 const uint64_t measured_time = timer.
value() / (timer.
events() * (tune_M / M));
106 const uint64_t target_nsec = msec.count() *
static_cast<uint64_t
>(1000000);
118 uint64_t est_nsec = measured_time;
120 if(est_nsec < target_nsec && M < max_kib)
122 const uint64_t desired_cost_increase = (target_nsec + est_nsec - 1) / est_nsec;
123 const uint64_t mem_headroom = max_kib / M;
125 const uint64_t M_mult = std::min(desired_cost_increase, mem_headroom);
126 M *=
static_cast<size_t>(M_mult);
130 if(est_nsec < target_nsec)
132 const uint64_t desired_cost_increase = (target_nsec + est_nsec - 1) / est_nsec;
133 t *=
static_cast<size_t>(desired_cost_increase);
151 const size_t M = iter;
159 return std::unique_ptr<PasswordHash>(
new Argon2(m_family, M, t, p));
#define BOTAN_ARG_CHECK(expr, msg)
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)
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)