Botan 3.0.0-alpha0
Crypto and TLS for C&
Public Member Functions | Static Public Member Functions | List of all members
Botan::PBKDF Class Referenceabstract

#include <pbkdf.h>

Inheritance diagram for Botan::PBKDF:
Botan::OpenPGP_S2K Botan::PKCS5_PBKDF2

Public Member Functions

PBKDFclone () const
 
template<typename Alloc >
OctetString derive_key (size_t out_len, const std::string &passphrase, const std::vector< uint8_t, Alloc > &salt, size_t iterations) const
 
template<typename Alloc >
OctetString derive_key (size_t out_len, const std::string &passphrase, const std::vector< uint8_t, Alloc > &salt, std::chrono::milliseconds msec, size_t &iterations) const
 
OctetString derive_key (size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const
 
OctetString derive_key (size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
 
virtual std::string name () const =0
 
virtual std::unique_ptr< PBKDFnew_object () const =0
 
virtual size_t pbkdf (uint8_t out[], size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const =0
 
secure_vector< uint8_t > pbkdf_iterations (size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const
 
void pbkdf_iterations (uint8_t out[], size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const
 
secure_vector< uint8_t > pbkdf_timed (size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
 
void pbkdf_timed (uint8_t out[], size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
 
virtual ~PBKDF ()=default
 

Static Public Member Functions

static std::unique_ptr< PBKDFcreate (const std::string &algo_spec, const std::string &provider="")
 
static std::unique_ptr< PBKDFcreate_or_throw (const std::string &algo_spec, const std::string &provider="")
 
static std::vector< std::string > providers (const std::string &algo_spec)
 

Detailed Description

Base class for PBKDF (password based key derivation function) implementations. Converts a password into a key using a salt and iterated hashing to make brute force attacks harder.

Starting in 2.8 this functionality is also offered by PasswordHash. The PBKDF interface may be removed in a future release.

Definition at line 24 of file pbkdf.h.

Constructor & Destructor Documentation

◆ ~PBKDF()

virtual Botan::PBKDF::~PBKDF ( )
virtualdefault

Member Function Documentation

◆ clone()

PBKDF * Botan::PBKDF::clone ( ) const
inline
Returns
new instance of this same algorithm

Definition at line 59 of file pbkdf.h.

60 {
61 return this->new_object().release();
62 }
virtual std::unique_ptr< PBKDF > new_object() const =0

◆ create()

std::unique_ptr< PBKDF > Botan::PBKDF::create ( const std::string &  algo_spec,
const std::string &  provider = "" 
)
static

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 22 of file pbkdf.cpp.

24 {
25 const SCAN_Name req(algo_spec);
26
27#if defined(BOTAN_HAS_PBKDF2)
28 if(req.algo_name() == "PBKDF2")
29 {
30 // TODO OpenSSL
31
32 if(provider.empty() || provider == "base")
33 {
34 if(auto mac = MessageAuthenticationCode::create("HMAC(" + req.arg(0) + ")"))
35 return std::make_unique<PKCS5_PBKDF2>(mac.release());
36
37 if(auto mac = MessageAuthenticationCode::create(req.arg(0)))
38 return std::make_unique<PKCS5_PBKDF2>(mac.release());
39 }
40
41 return nullptr;
42 }
43#endif
44
45#if defined(BOTAN_HAS_PGP_S2K)
46 if(req.algo_name() == "OpenPGP-S2K" && req.arg_count() == 1)
47 {
48 if(auto hash = HashFunction::create(req.arg(0)))
49 return std::make_unique<OpenPGP_S2K>(hash.release());
50 }
51#endif
52
53 BOTAN_UNUSED(req);
54 BOTAN_UNUSED(provider);
55
56 return nullptr;
57 }
#define BOTAN_UNUSED(...)
Definition: assert.h:141
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:98
static std::unique_ptr< MessageAuthenticationCode > create(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:46
MechanismType hash

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

Referenced by create_or_throw().

◆ create_or_throw()

std::unique_ptr< PBKDF > Botan::PBKDF::create_or_throw ( const std::string &  algo_spec,
const std::string &  provider = "" 
)
static

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 61 of file pbkdf.cpp.

63 {
64 if(auto pbkdf = PBKDF::create(algo, provider))
65 {
66 return pbkdf;
67 }
68 throw Lookup_Error("PBKDF", algo, provider);
69 }
virtual size_t pbkdf(uint8_t out[], size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const =0
static std::unique_ptr< PBKDF > create(const std::string &algo_spec, const std::string &provider="")
Definition: pbkdf.cpp:22

References create(), and pbkdf().

Referenced by Botan::get_pbkdf().

◆ derive_key() [1/4]

template<typename Alloc >
OctetString Botan::PBKDF::derive_key ( size_t  out_len,
const std::string &  passphrase,
const std::vector< uint8_t, Alloc > &  salt,
size_t  iterations 
) const
inline

Derive a key from a passphrase

Parameters
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
iterationsthe number of iterations to use (use 10K or more)

Definition at line 184 of file pbkdf.h.

188 {
189 return pbkdf_iterations(out_len, passphrase, salt.data(), salt.size(), iterations);
190 }
void pbkdf_iterations(uint8_t out[], size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const
Definition: pbkdf.cpp:85

◆ derive_key() [2/4]

template<typename Alloc >
OctetString Botan::PBKDF::derive_key ( size_t  out_len,
const std::string &  passphrase,
const std::vector< uint8_t, Alloc > &  salt,
std::chrono::milliseconds  msec,
size_t &  iterations 
) const
inline

Derive a key from a passphrase using a certain amount of time

Parameters
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
msecis how long to run the PBKDF
iterationsis set to the number of iterations used

Definition at line 219 of file pbkdf.h.

224 {
225 return pbkdf_timed(out_len, passphrase, salt.data(), salt.size(), msec, iterations);
226 }
void pbkdf_timed(uint8_t out[], size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
Definition: pbkdf.cpp:76

◆ derive_key() [3/4]

OctetString Botan::PBKDF::derive_key ( size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
size_t  iterations 
) const
inline

Derive a key from a passphrase

Parameters
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
iterationsthe number of iterations to use (use 10K or more)

Definition at line 168 of file pbkdf.h.

172 {
173 return pbkdf_iterations(out_len, passphrase, salt, salt_len, iterations);
174 }
size_t salt_len
Definition: x509_obj.cpp:25

References salt_len.

Referenced by Botan::check_passhash9(), and Botan::generate_passhash9().

◆ derive_key() [4/4]

OctetString Botan::PBKDF::derive_key ( size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
std::chrono::milliseconds  msec,
size_t &  iterations 
) const
inline

Derive a key from a passphrase

Parameters
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
msecis how long to run the PBKDF
iterationsis set to the number of iterations used

Definition at line 201 of file pbkdf.h.

206 {
207 return pbkdf_timed(out_len, passphrase, salt, salt_len, msec, iterations);
208 }

References salt_len.

◆ name()

virtual std::string Botan::PBKDF::name ( ) const
pure virtual
Returns
name of this PBKDF

Implemented in Botan::PKCS5_PBKDF2, and Botan::OpenPGP_S2K.

Referenced by pbkdf_iterations().

◆ new_object()

virtual std::unique_ptr< PBKDF > Botan::PBKDF::new_object ( ) const
pure virtual
Returns
new instance of this same algorithm

Implemented in Botan::PKCS5_PBKDF2, and Botan::OpenPGP_S2K.

◆ pbkdf()

virtual size_t Botan::PBKDF::pbkdf ( uint8_t  out[],
size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
size_t  iterations,
std::chrono::milliseconds  msec 
) const
pure virtual

Derive a key from a passphrase for a number of iterations specified by either iterations or if iterations == 0 then running until msec time has elapsed.

Parameters
outbuffer to store the derived key, must be of out_len bytes
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
iterationsthe number of iterations to use (use 10K or more)
msecif iterations is zero, then instead the PBKDF is run until msec milliseconds has passed.
Returns
the number of iterations performed

Implemented in Botan::PKCS5_PBKDF2, and Botan::OpenPGP_S2K.

Referenced by create_or_throw(), pbkdf_iterations(), and pbkdf_timed().

◆ pbkdf_iterations() [1/2]

secure_vector< uint8_t > Botan::PBKDF::pbkdf_iterations ( size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
size_t  iterations 
) const

Derive a key from a passphrase for a number of iterations.

Parameters
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
iterationsthe number of iterations to use (use 10K or more)
Returns
the derived key

Definition at line 99 of file pbkdf.cpp.

103 {
104 secure_vector<uint8_t> out(out_len);
105 pbkdf_iterations(out.data(), out_len, passphrase, salt, salt_len, iterations);
106 return out;
107 }

References pbkdf_iterations(), and salt_len.

◆ pbkdf_iterations() [2/2]

void Botan::PBKDF::pbkdf_iterations ( uint8_t  out[],
size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
size_t  iterations 
) const

Derive a key from a passphrase for a number of iterations.

Parameters
outbuffer to store the derived key, must be of out_len bytes
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
iterationsthe number of iterations to use (use 10K or more)

Definition at line 85 of file pbkdf.cpp.

89 {
90 if(iterations == 0)
91 throw Invalid_Argument(name() + ": Invalid iteration count");
92
93 const size_t iterations_run = pbkdf(out, out_len, passphrase,
94 salt, salt_len, iterations,
95 std::chrono::milliseconds(0));
96 BOTAN_ASSERT_EQUAL(iterations, iterations_run, "Expected PBKDF iterations");
97 }
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:80
virtual std::string name() const =0

References BOTAN_ASSERT_EQUAL, name(), pbkdf(), and salt_len.

Referenced by pbkdf_iterations().

◆ pbkdf_timed() [1/2]

secure_vector< uint8_t > Botan::PBKDF::pbkdf_timed ( size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
std::chrono::milliseconds  msec,
size_t &  iterations 
) const

Derive a key from a passphrase, running until msec time has elapsed.

Parameters
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
msecif iterations is zero, then instead the PBKDF is run until msec milliseconds has passed.
iterationsset to the number iterations executed
Returns
the derived key

Definition at line 109 of file pbkdf.cpp.

114 {
115 secure_vector<uint8_t> out(out_len);
116 pbkdf_timed(out.data(), out_len, passphrase, salt, salt_len, msec, iterations);
117 return out;
118 }

References pbkdf_timed(), and salt_len.

◆ pbkdf_timed() [2/2]

void Botan::PBKDF::pbkdf_timed ( uint8_t  out[],
size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
std::chrono::milliseconds  msec,
size_t &  iterations 
) const

Derive a key from a passphrase, running until msec time has elapsed.

Parameters
outbuffer to store the derived key, must be of out_len bytes
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
msecif iterations is zero, then instead the PBKDF is run until msec milliseconds has passed.
iterationsset to the number iterations executed

Definition at line 76 of file pbkdf.cpp.

81 {
82 iterations = pbkdf(out, out_len, passphrase, salt, salt_len, 0, msec);
83 }

References pbkdf(), and salt_len.

Referenced by pbkdf_timed().

◆ providers()

std::vector< std::string > Botan::PBKDF::providers ( const std::string &  algo_spec)
static
Returns
list of available providers for this algorithm, empty if not available

Definition at line 71 of file pbkdf.cpp.

72 {
73 return probe_providers_of<PBKDF>(algo_spec);
74 }

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