9#include <botan/pbkdf2.h>
11#include <botan/exceptn.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/timer.h>
19void pbkdf2_set_key(MessageAuthenticationCode& prf,
const char* password,
size_t password_len) {
22 }
catch(Invalid_Key_Length&) {
23 throw Invalid_Argument(
"PBKDF2 cannot accept passphrase of the given size");
27size_t tune_pbkdf2(MessageAuthenticationCode& prf,
29 std::chrono::milliseconds msec,
30 std::chrono::milliseconds tune_time = std::chrono::milliseconds(10)) {
31 if(output_length == 0) {
35 const size_t prf_sz = prf.output_length();
39 const size_t trial_iterations = 2000;
43 Timer timer(
"PBKDF2");
45 prf.set_key(
nullptr, 0);
47 timer.run_until_elapsed(tune_time, [&]() {
48 uint8_t out[12] = {0};
49 uint8_t salt[12] = {0};
50 pbkdf2(prf, out,
sizeof(out), salt,
sizeof(salt), trial_iterations);
53 if(timer.events() == 0) {
54 return trial_iterations;
57 const uint64_t duration_nsec = timer.value() / timer.events();
59 const uint64_t desired_nsec =
static_cast<uint64_t
>(msec.count()) * 1000000;
61 if(duration_nsec > desired_nsec) {
62 return trial_iterations;
65 const size_t blocks_needed = (output_length + prf_sz - 1) / prf_sz;
67 const size_t multiplier =
static_cast<size_t>(desired_nsec / duration_nsec / blocks_needed);
70 return trial_iterations;
72 return trial_iterations * multiplier;
81 std::string_view password,
85 std::chrono::milliseconds msec) {
87 iterations = tune_pbkdf2(prf, out_len, msec);
92 pbkdf2.derive_key(out, out_len, password.data(), password.size(), salt, salt_len);
100 const uint8_t salt[],
103 if(iterations == 0) {
118 uint32_t counter = 1;
120 const size_t prf_output = std::min<size_t>(prf_sz, out_len);
122 prf.
update(salt, salt_len);
126 xor_buf(out, U.data(), prf_output);
128 for(
size_t i = 1; i != iterations; ++i) {
131 xor_buf(out, U.data(), prf_output);
134 out_len -= prf_output;
142 std::string_view password,
143 const uint8_t salt[],
146 std::chrono::milliseconds msec)
const {
147 if(iterations == 0) {
148 iterations = tune_pbkdf2(*m_mac, key_len, msec);
153 pbkdf2.derive_key(key, key_len, password.data(), password.size(), salt, salt_len);
159 return fmt(
"PBKDF2({})", m_mac->name());
163 return std::make_unique<PKCS5_PBKDF2>(m_mac->new_object());
169 m_prf(prf.new_object()), m_iterations(tune_pbkdf2(*m_prf, olen, msec)) {}
172 return fmt(
"PBKDF2({},{})", m_prf->name(), m_iterations);
177 const char* password,
178 const size_t password_len,
179 const uint8_t salt[],
180 size_t salt_len)
const {
181 pbkdf2_set_key(*m_prf, password, password_len);
182 pbkdf2(*m_prf, out, out_len, salt, salt_len, m_iterations);
186 return fmt(
"PBKDF2({})", m_prf->name());
190 std::chrono::milliseconds msec,
192 std::chrono::milliseconds tune_time)
const {
193 auto iterations = tune_pbkdf2(*m_prf, output_len, msec, tune_time);
194 return std::make_unique<PBKDF2>(*m_prf, iterations);
198 return std::make_unique<PBKDF2>(*m_prf, 150000);
202 return std::make_unique<PBKDF2>(*m_prf, iter);
206 return std::make_unique<PBKDF2>(*m_prf, iter);
#define BOTAN_ASSERT_NOMSG(expr)
void update(const uint8_t in[], size_t length)
virtual size_t output_length() const =0
void update_be(uint16_t val)
void final(uint8_t out[])
std::string name() const override
std::unique_ptr< PasswordHash > default_params() const override
std::unique_ptr< PasswordHash > from_iterations(size_t iter) const override
std::unique_ptr< PasswordHash > tune(size_t output_len, std::chrono::milliseconds msec, size_t max_memory, std::chrono::milliseconds tune_msec) const override
std::unique_ptr< PasswordHash > from_params(size_t iter, size_t, size_t) const override
PBKDF2(const MessageAuthenticationCode &prf, size_t iter)
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
size_t pbkdf(uint8_t output_buf[], size_t output_len, std::string_view passphrase, const uint8_t salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const override
std::string name() const override
std::unique_ptr< PBKDF > new_object() const override
std::string fmt(std::string_view format, const T &... args)
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)
constexpr void clear_mem(T *ptr, size_t n)
const uint8_t * cast_char_ptr_to_uint8(const char *s)