Botan 3.7.1
Crypto and TLS for C&
Botan::Argon2_Family Class Referencefinal

#include <argon2.h>

Inheritance diagram for Botan::Argon2_Family:
Botan::PasswordHashFamily

Public Member Functions

 Argon2_Family (uint8_t family)
 
std::unique_ptr< PasswordHashdefault_params () const override
 
std::unique_ptr< PasswordHashfrom_iterations (size_t iter) const override
 
std::unique_ptr< PasswordHashfrom_params (size_t M, size_t t, size_t p) const override
 
std::string name () const override
 
std::unique_ptr< PasswordHashtune (size_t output_length, std::chrono::milliseconds msec, size_t max_memory, std::chrono::milliseconds 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 103 of file argon2.h.

Constructor & Destructor Documentation

◆ Argon2_Family()

Botan::Argon2_Family::Argon2_Family ( uint8_t family)

Definition at line 67 of file argon2pwhash.cpp.

67 : m_family(family) {
68 if(m_family != 0 && m_family != 1 && m_family != 2) {
69 throw Invalid_Argument("Unknown Argon2 family identifier");
70 }
71}

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 54 of file pwdhash.cpp.

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

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

Referenced by botan_pwdhash(), botan_pwdhash_timed(), and Botan::PasswordHashFamily::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 110 of file pwdhash.cpp.

111 {
112 if(auto pbkdf = PasswordHashFamily::create(algo, provider)) {
113 return pbkdf;
114 }
115 throw Lookup_Error("PasswordHashFamily", algo, provider);
116}
static std::unique_ptr< PasswordHashFamily > create(std::string_view algo_spec, std::string_view provider="")
Definition pwdhash.cpp:54

References Botan::PasswordHashFamily::create().

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

◆ default_params()

std::unique_ptr< PasswordHash > Botan::Argon2_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 129 of file argon2pwhash.cpp.

129 {
130 return this->from_params(128 * 1024, 1, 1);
131}
std::unique_ptr< PasswordHash > from_params(size_t M, size_t t, size_t p) const override

References from_params().

◆ from_iterations()

std::unique_ptr< PasswordHash > Botan::Argon2_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 133 of file argon2pwhash.cpp.

133 {
134 /*
135 These choices are arbitrary, but should not change in future
136 releases since they will break applications expecting deterministic
137 mapping from iteration count to params
138 */
139 const size_t M = iter;
140 const size_t t = 1;
141 const size_t p = 1;
142 return this->from_params(M, t, p);
143}

References from_params().

◆ from_params()

std::unique_ptr< PasswordHash > Botan::Argon2_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}

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

Implements Botan::PasswordHashFamily.

Definition at line 145 of file argon2pwhash.cpp.

145 {
146 return std::make_unique<Argon2>(m_family, M, t, p);
147}

Referenced by default_params(), from_iterations(), and tune().

◆ name()

std::string Botan::Argon2_Family::name ( ) const
overridevirtual
Returns
name of this PasswordHash

Implements Botan::PasswordHashFamily.

Definition at line 73 of file argon2pwhash.cpp.

73 {
74 return argon2_family_name(m_family);
75}

◆ providers()

std::vector< std::string > Botan::PasswordHashFamily::providers ( std::string_view algo_spec)
staticinherited
Returns
list of available providers for this algorithm, empty if not available

Definition at line 118 of file pwdhash.cpp.

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

References Botan::probe_providers_of().

◆ tune()

std::unique_ptr< PasswordHash > Botan::Argon2_Family::tune ( size_t output_length,
std::chrono::milliseconds msec,
size_t max_memory_usage_mb,
std::chrono::milliseconds 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 runing 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

Implements Botan::PasswordHashFamily.

Definition at line 77 of file argon2pwhash.cpp.

80 {
81 const size_t max_kib = (max_memory == 0) ? 256 * 1024 : max_memory * 1024;
82
83 // Tune with a large memory otherwise we measure cache vs RAM speeds and underestimate
84 // costs for larger params. Default is 36 MiB, or use 128 for long times.
85 const size_t tune_M = (msec >= std::chrono::milliseconds(200) ? 128 : 36) * 1024;
86 const size_t p = 1;
87 size_t t = 1;
88
89 size_t M = 4 * 1024;
90
91 auto pwhash = this->from_params(tune_M, t, p);
92
93 auto tune_fn = [&]() {
94 uint8_t output[64] = {0};
95 pwhash->derive_key(output, sizeof(output), "test", 4, nullptr, 0);
96 };
97
98 const uint64_t measured_time = measure_cost(tune_time, tune_fn) / (tune_M / M);
99
100 const uint64_t target_nsec = msec.count() * static_cast<uint64_t>(1000000);
101
102 /*
103 * Argon2 scaling rules:
104 * k*M, k*t, k*p all increase cost by about k
105 *
106 * First preference is to increase M up to max allowed value.
107 * Any remaining time budget is spent on increasing t.
108 */
109
110 uint64_t est_nsec = measured_time;
111
112 if(est_nsec < target_nsec && M < max_kib) {
113 const uint64_t desired_cost_increase = (target_nsec + est_nsec - 1) / est_nsec;
114 const uint64_t mem_headroom = max_kib / M;
115
116 const uint64_t M_mult = std::min(desired_cost_increase, mem_headroom);
117 M *= static_cast<size_t>(M_mult);
118 est_nsec *= M_mult;
119 }
120
121 if(est_nsec < target_nsec / 2) {
122 const uint64_t desired_cost_increase = (target_nsec + est_nsec - 1) / est_nsec;
123 t *= static_cast<size_t>(desired_cost_increase);
124 }
125
126 return this->from_params(M, t, p);
127}
uint64_t measure_cost(std::chrono::milliseconds trial_msec, F func)
Definition time_utils.h:21

References from_params(), and Botan::measure_cost().


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