7#include <botan/bcrypt_pbkdf.h>
10#include <botan/internal/blowfish.h>
11#include <botan/internal/fmt.h>
12#include <botan/internal/loadstor.h>
13#include <botan/internal/time_utils.h>
22 return fmt(
"Bcrypt-PBKDF({})", m_iterations);
26 return "Bcrypt-PBKDF";
30 std::chrono::milliseconds msec,
32 std::chrono::milliseconds tune_time)
const {
33 const size_t blocks = (output_length + 32 - 1) / 32;
39 const size_t starting_iter = 2;
43 auto tune_fn = [&]() {
44 uint8_t output[32] = {0};
45 pwhash->derive_key(output,
sizeof(output),
"test", 4,
nullptr, 0);
48 const uint64_t measured_time =
measure_cost(tune_time, tune_fn) / blocks;
50 const uint64_t target_nsec = msec.count() *
static_cast<uint64_t
>(1000000);
52 const uint64_t desired_increase = target_nsec / measured_time;
54 if(desired_increase == 0) {
58 return this->
from_iterations(
static_cast<size_t>(desired_increase * starting_iter));
66 return std::make_unique<Bcrypt_PBKDF>(iter);
80 const size_t BCRYPT_PBKDF_OUTPUT = 32;
83 alignas(64)
static const uint8_t BCRYPT_PBKDF_MAGIC[BCRYPT_PBKDF_OUTPUT] = {
84 0x4F, 0x78, 0x79, 0x63, 0x68, 0x72, 0x6F, 0x6D, 0x61, 0x74, 0x69, 0x63, 0x42, 0x6C, 0x6F, 0x77,
85 0x66, 0x69, 0x73, 0x68, 0x53, 0x77, 0x61, 0x74, 0x44, 0x79, 0x6E, 0x61, 0x6D, 0x69, 0x74, 0x65};
87 const size_t BCRYPT_PBKDF_WORKFACTOR = 6;
88 const size_t BCRYPT_PBKDF_ROUNDS = 64;
91 pass_hash.data(), pass_hash.size(), salt_hash.data(), salt_hash.size(), BCRYPT_PBKDF_WORKFACTOR,
true);
93 copy_mem(tmp.data(), BCRYPT_PBKDF_MAGIC, BCRYPT_PBKDF_OUTPUT);
94 for(
size_t i = 0; i != BCRYPT_PBKDF_ROUNDS; ++i) {
104 for(
size_t i = 0; i != 32 / 4; ++i) {
106 store_be(w, &tmp[
sizeof(uint32_t) * i]);
109 xor_buf(out.data(), tmp.data(), BCRYPT_PBKDF_OUTPUT);
116 const char* password,
118 const uint8_t salt[],
119 size_t salt_len)
const {
121 if(output_len == 0) {
125 BOTAN_ARG_CHECK(output_len <= 10 * 1024 * 1024,
"Too much output for Bcrypt PBKDF");
127 const size_t BCRYPT_BLOCK_SIZE = 32;
128 const size_t blocks = (output_len + BCRYPT_BLOCK_SIZE - 1) / BCRYPT_BLOCK_SIZE;
131 const auto pass_hash = sha512->process(
reinterpret_cast<const uint8_t*
>(password), password_len);
139 for(
size_t block = 0; block != blocks; ++block) {
142 sha512->update(salt, salt_len);
143 sha512->update_be(
static_cast<uint32_t
>(block + 1));
144 sha512->final(salt_hash.data());
146 bcrypt_round(blowfish, pass_hash, salt_hash, out, tmp);
148 for(
size_t r = 1; r < m_iterations; ++r) {
151 sha512->final(salt_hash.data());
153 bcrypt_round(blowfish, pass_hash, salt_hash, out, tmp);
156 for(
size_t i = 0; i != BCRYPT_BLOCK_SIZE; ++i) {
157 const size_t dest = i * blocks + block;
158 if(dest < output_len) {
159 output[dest] = out[i];
#define BOTAN_ARG_CHECK(expr, msg)
std::unique_ptr< PasswordHash > from_params(size_t i, size_t, size_t) const override
std::unique_ptr< PasswordHash > default_params() 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::string name() 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
Bcrypt_PBKDF(size_t iterations)
std::string to_string() const override
void encrypt(const uint8_t in[], uint8_t out[]) const
void salted_set_key(const uint8_t key[], size_t key_length, const uint8_t salt[], size_t salt_length, size_t workfactor, bool salt_first=false)
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
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)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr auto store_be(ParamTs &&... params)
constexpr void clear_mem(T *ptr, size_t n)