Botan 3.13.0
Crypto and TLS for C&
Botan::Bcrypt_PBKDF_Family Class Referencefinal

#include <bcrypt_pbkdf.h>

Inheritance diagram for Botan::Bcrypt_PBKDF_Family:
Botan::PasswordHashFamily

Public Member Functions

 Bcrypt_PBKDF_Family ()=default
std::unique_ptr< PasswordHashdefault_params () const override
std::unique_ptr< PasswordHashfrom_iterations (size_t iterations) const override
std::unique_ptr< PasswordHashfrom_params (size_t iterations, size_t, size_t) const override
std::string name () const override
std::unique_ptr< PasswordHashtune (size_t output_length, std::chrono::milliseconds msec, size_t max_memory_usage_mb=0, std::chrono::milliseconds tuning_msec=std::chrono::milliseconds(10)) const
std::unique_ptr< PasswordHashtune_params (size_t output_len, uint64_t desired_runtime_msec, std::optional< size_t > max_memory, uint64_t tune_msec) const override

Static Public Member Functions

static std::unique_ptr< PasswordHashFamilycreate (std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< PasswordHashFamilycreate_or_throw (std::string_view algo_spec, std::string_view provider="")
static std::vector< std::string > providers (std::string_view algo_spec)

Detailed Description

Definition at line 48 of file bcrypt_pbkdf.h.

Constructor & Destructor Documentation

◆ Bcrypt_PBKDF_Family()

Botan::Bcrypt_PBKDF_Family::Bcrypt_PBKDF_Family ( )
default

Member Function Documentation

◆ create()

std::unique_ptr< PasswordHashFamily > Botan::PasswordHashFamily::create ( std::string_view algo_spec,
std::string_view provider = "" )
staticinherited

Create an instance based on a name If provider is empty then best available is chosen.

Parameters
algo_specalgorithm name
providerprovider implementation to choose
Returns
a null pointer if the algo/provider combination cannot be found

Definition at line 58 of file pwdhash.cpp.

58 {
59 const SCAN_Name req(algo_spec);
60
61#if defined(BOTAN_HAS_PBKDF2)
62 if(req.algo_name() == "PBKDF2") {
63 if(provider.empty() || provider == "base") {
64 if(auto mac = MessageAuthenticationCode::create("HMAC(" + req.arg(0) + ")")) {
65 return std::make_unique<PBKDF2_Family>(std::move(mac));
66 }
67
68 if(auto mac = MessageAuthenticationCode::create(req.arg(0))) {
69 return std::make_unique<PBKDF2_Family>(std::move(mac));
70 }
71 }
72
73 return nullptr;
74 }
75#endif
76
77#if defined(BOTAN_HAS_SCRYPT)
78 if(req.algo_name() == "Scrypt") {
79 return std::make_unique<Scrypt_Family>();
80 }
81#endif
82
83#if defined(BOTAN_HAS_ARGON2)
84 if(req.algo_name() == "Argon2d") {
85 return std::make_unique<Argon2_Family>(static_cast<uint8_t>(0));
86 } else if(req.algo_name() == "Argon2i") {
87 return std::make_unique<Argon2_Family>(static_cast<uint8_t>(1));
88 } else if(req.algo_name() == "Argon2id") {
89 return std::make_unique<Argon2_Family>(static_cast<uint8_t>(2));
90 }
91#endif
92
93#if defined(BOTAN_HAS_PBKDF_BCRYPT)
94 if(req.algo_name() == "Bcrypt-PBKDF") {
95 return std::make_unique<Bcrypt_PBKDF_Family>();
96 }
97#endif
98
99#if defined(BOTAN_HAS_PGP_S2K)
100 if(req.algo_name() == "OpenPGP-S2K" && req.arg_count() == 1) {
101 if(auto hash = HashFunction::create(req.arg(0))) {
102 return std::make_unique<RFC4880_S2K_Family>(std::move(hash));
103 }
104 }
105#endif
106
107#if defined(BOTAN_HAS_PKCS12_KDF)
108 if(req.algo_name() == "PKCS12-KDF" && req.arg_count() == 2) {
109 if(auto hash = HashFunction::create(req.arg(0))) {
110 const auto id_param = req.arg_as_integer(1);
111 return std::make_unique<PKCS12_KDF_Family>(std::move(hash), id_param);
112 }
113 }
114#endif
115
116 BOTAN_UNUSED(req);
117 BOTAN_UNUSED(provider);
118
119 return nullptr;
120}
#define BOTAN_UNUSED
Definition assert.h:144
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:111
static std::unique_ptr< MessageAuthenticationCode > create(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:50

References Botan::SCAN_Name::algo_name(), Botan::SCAN_Name::arg(), Botan::SCAN_Name::arg_as_integer(), Botan::SCAN_Name::arg_count(), BOTAN_UNUSED, Botan::HashFunction::create(), and Botan::MessageAuthenticationCode::create().

Referenced by botan_pwdhash(), botan_pwdhash_timed(), and create_or_throw().

◆ create_or_throw()

std::unique_ptr< PasswordHashFamily > Botan::PasswordHashFamily::create_or_throw ( std::string_view algo_spec,
std::string_view provider = "" )
staticinherited

Create an instance based on a name, or throw if the algo/provider combination cannot be found. If provider is empty then best available is chosen.

Definition at line 123 of file pwdhash.cpp.

124 {
125 if(auto pbkdf = PasswordHashFamily::create(algo, provider)) {
126 return pbkdf;
127 }
128 throw Lookup_Error("PasswordHashFamily", algo, provider);
129}
static std::unique_ptr< PasswordHashFamily > create(std::string_view algo_spec, std::string_view provider="")
Definition pwdhash.cpp:58

References create().

Referenced by Botan::argon2(), Botan::argon2_check_pwhash(), Botan::argon2_generate_pwhash(), Botan::bcrypt_pbkdf(), Botan::CryptoBox::decrypt_bin(), Botan::CryptoBox::encrypt(), Botan::scrypt(), and Botan::scrypt().

◆ default_params()

std::unique_ptr< PasswordHash > Botan::Bcrypt_PBKDF_Family::default_params ( ) const
overridevirtual

Return some default parameter set for this PBKDF that should be good enough for most users. The value returned may change over time as processing power and attacks improve.

Implements Botan::PasswordHashFamily.

Definition at line 68 of file bcrypt_pbkdf.cpp.

68 {
69 return this->from_iterations(32); // About 100 ms on fast machine
70}
std::unique_ptr< PasswordHash > from_iterations(size_t iterations) const override

References from_iterations().

Referenced by Bcrypt_PBKDF_Family(), and tune_params().

◆ from_iterations()

std::unique_ptr< PasswordHash > Botan::Bcrypt_PBKDF_Family::from_iterations ( size_t iterations) const
overridevirtual

Return a parameter chosen based on a rough approximation with the specified iteration count. The exact value this returns for a particular algorithm may change from over time. Think of it as an alternative to tune, where time is expressed in terms of PBKDF2 iterations rather than milliseconds.

Implements Botan::PasswordHashFamily.

Definition at line 72 of file bcrypt_pbkdf.cpp.

72 {
73 return std::make_unique<Bcrypt_PBKDF>(iterations);
74}

Referenced by Bcrypt_PBKDF_Family(), default_params(), from_params(), and tune_params().

◆ from_params()

std::unique_ptr< PasswordHash > Botan::Bcrypt_PBKDF_Family::from_params ( size_t i1,
size_t i2,
size_t i3 ) const
overridevirtual

Create a password hash using some scheme specific format. Parameters are as follows:

  • For PBKDF2, PGP-S2K, and Bcrypt-PBKDF, i1 is iterations
  • Scrypt uses N, r, p for i{1-3}
  • Argon2 family uses memory (in KB), iterations, and parallelism for i{1-3}
  • PKCS12-KDF uses iterations for i1 (the hash and id are fixed by the family name, e.g. "PKCS12-KDF(SHA-256,1)")

All unneeded parameters should be set to 0 or left blank.

Implements Botan::PasswordHashFamily.

Definition at line 76 of file bcrypt_pbkdf.cpp.

76 {
77 return this->from_iterations(iterations);
78}

References from_iterations().

Referenced by Bcrypt_PBKDF_Family().

◆ name()

std::string Botan::Bcrypt_PBKDF_Family::name ( ) const
overridevirtual

Return the name of this password hash family

Returns
name of this PasswordHash

Implements Botan::PasswordHashFamily.

Definition at line 26 of file bcrypt_pbkdf.cpp.

26 {
27 return "Bcrypt-PBKDF";
28}

Referenced by Bcrypt_PBKDF_Family().

◆ providers()

std::vector< std::string > Botan::PasswordHashFamily::providers ( std::string_view algo_spec)
staticinherited

List the providers available for a given password hash

Returns
list of available providers for this algorithm, empty if not available

Definition at line 131 of file pwdhash.cpp.

131 {
133}
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})
Definition scan_name.h:99

References Botan::probe_providers_of().

◆ tune()

std::unique_ptr< PasswordHash > Botan::PasswordHashFamily::tune ( size_t output_length,
std::chrono::milliseconds msec,
size_t max_memory_usage_mb = 0,
std::chrono::milliseconds tuning_msec = std::chrono::milliseconds(10) ) const
inlineinherited

Return a new parameter set tuned for this machine

Return a password hash instance tuned to run for approximately msec milliseconds when producing an output of length output_length. (Accuracy may vary, use the command line utility botan pbkdf_tune to check.)

The parameters will be selected to use at most max_memory_usage_mb megabytes of memory, or if left as zero any size is allowed.

This function works by running a short tuning loop to estimate the performance of the algorithm, then scaling the parameters appropriately to hit the target size. The length of time the tuning loop runs can be controlled using the tuning_msec parameter.

Parameters
output_lengthhow long the output length will be
msecthe desired execution time in milliseconds
max_memory_usage_mbsome password hash functions can use a tunable amount of memory, in this case max_memory_usage limits the amount of RAM the returned parameters will require, in mebibytes (2**20 bytes). It may require some small amount above the request. Set to zero to place no limit at all.
tuning_msechow long to run the tuning loop

TODO(Botan4) remove this

Definition at line 282 of file pwdhash.h.

285 {
286 std::optional<size_t> max_memory_opt;
287 if(max_memory_usage_mb > 0) {
288 max_memory_opt = max_memory_usage_mb;
289 }
290
291 return this->tune_params(output_length,
292 static_cast<uint64_t>(msec.count()),
293 max_memory_opt,
294 static_cast<uint64_t>(tuning_msec.count()));
295 }
virtual std::unique_ptr< PasswordHash > tune_params(size_t output_length, uint64_t desired_runtime_msec, std::optional< size_t > max_memory_usage_mb={}, uint64_t tuning_msec=10) const =0

References tune(), and tune_params().

Referenced by tune().

◆ tune_params()

std::unique_ptr< PasswordHash > Botan::Bcrypt_PBKDF_Family::tune_params ( size_t output_length,
uint64_t desired_runtime_msec,
std::optional< size_t > max_memory_usage_mb,
uint64_t tuning_msec ) const
overridevirtual

Return a new parameter set tuned for this machine

Return a password hash instance tuned to run for approximately msec milliseconds when producing an output of length output_length. (Accuracy may vary, use the command line utility botan pbkdf_tune to check.)

The parameters will be selected to use at most max_memory_usage_mb megabytes of memory, or if left as zero any size is allowed.

This function works by running a short tuning loop to estimate the performance of the algorithm, then scaling the parameters appropriately to hit the target size. The length of time the tuning loop runs can be controlled using the tuning_msec parameter.

Parameters
output_lengthhow long the output length will be
desired_runtime_msecthe desired execution time in milliseconds
max_memory_usage_mbsome password hash functions can use a tunable amount of memory, in this case max_memory_usage limits the amount of RAM the returned parameters will require, in mebibytes (2**20 bytes). It may require some small amount above the request. Set to nullopt to place no limit at all.
tuning_msechow long to run the tuning loop

Implements Botan::PasswordHashFamily.

Definition at line 30 of file bcrypt_pbkdf.cpp.

33 {
34 const size_t blocks = (output_length + 32 - 1) / 32;
35
36 if(blocks == 0) {
37 return default_params();
38 }
39
40 const size_t starting_iter = 2;
41
42 auto pwhash = this->from_iterations(starting_iter);
43
44 auto tune_fn = [&]() {
45 uint8_t output[32] = {0};
46 pwhash->derive_key(output, sizeof(output), "test", 4, nullptr, 0);
47 };
48
49 // Cost of deriving a single 32-byte block at starting_iter
50 const uint64_t measured_time = measure_cost(tune_msec, tune_fn);
51
52 if(measured_time == 0) {
53 return this->from_iterations(starting_iter);
54 }
55
56 const uint64_t target_nsec = desired_msec * static_cast<uint64_t>(1000000);
57
58 // Output cost grows linearly in blocks, so divide the budget across them
59 const uint64_t desired_increase = target_nsec / measured_time / blocks;
60
61 if(desired_increase == 0) {
62 return this->from_iterations(starting_iter);
63 }
64
65 return this->from_iterations(static_cast<size_t>(desired_increase * starting_iter));
66}
std::unique_ptr< PasswordHash > default_params() const override
uint64_t measure_cost(uint64_t trial_msec, F func)
Definition time_utils.h:19

References default_params(), from_iterations(), and Botan::measure_cost().

Referenced by Bcrypt_PBKDF_Family().


The documentation for this class was generated from the following files: